Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

563580 views
1
#include "typedef.h"
2
#include "tools.h"
3
4
/**************************************************************************\
5
@---------------------------------------------------------------------------
6
@---------------------------------------------------------------------------
7
@ FILE: p_mat_det.c
8
@---------------------------------------------------------------------------
9
@---------------------------------------------------------------------------
10
@
11
\**************************************************************************/
12
13
14
/**************************************************************************\
15
@---------------------------------------------------------------------------
16
@ int p_mat_det(Mat, prime)
17
@ matrix_TYP *Mat;
18
@ int prime;
19
@
20
@ Calculates the determinant of Mat modulo prime.
21
@ The entries of Mat are not changed.
22
@---------------------------------------------------------------------------
23
@
24
\**************************************************************************/
25
int p_mat_det(Mat, prime)
26
matrix_TYP *Mat;
27
int prime;
28
{
29
int i,j;
30
int step, dim;
31
int **M, *tmp;
32
int det, msi;
33
34
dim = Mat->cols;
35
if(prime < 0)
36
prime = -prime;
37
if(Mat->rows != Mat->cols)
38
{
39
fprintf(stderr, "error: can't calculate the determinante of a non-square matrix\n");
40
exit(3);
41
}
42
if((M = (int **)malloc(dim *sizeof(int *))) == 0)
43
{
44
fprintf(stderr, "malloc of M in 'p_mat_det' failed\n");
45
exit(2);
46
}
47
for(i=0;i<dim;i++)
48
{
49
if((M[i] = (int *)malloc(dim *sizeof(int))) == 0)
50
{
51
fprintf(stderr, "malloc of M[%d] in 'p_mat_det' failed\n", i);
52
exit(2);
53
}
54
}
55
for(i=0;i<dim;i++)
56
for(j=0;j<dim;j++)
57
M[i][j] = Mat->array.SZ[i][j] % prime;
58
det = 1;
59
for(step = 0; step < dim;step++)
60
{
61
/************************************************************************\
62
| search for non zero entry in the colmn no. step and
63
| swap it to M[step][step]
64
\************************************************************************/
65
for(i=step; i<dim && M[i][step] == 0; i++);
66
if(i == dim)
67
{
68
for(j=0;j<dim;j++)
69
free(M[j]);
70
free(M);
71
return(0);
72
}
73
if(i != step){
74
tmp = M[step]; M[step] = M[i]; M[i] = tmp; det = -det;
75
}
76
msi = p_inv(M[step][step], prime);
77
i = (msi * M[step][step])%prime;
78
if(i<0) i+=prime;
79
if(i != 1)
80
printf("TEST %d\n", i);
81
82
/* multiplying the step-th row with msi */
83
for(i=step+1;i<dim;i++){
84
M[step][i] *= msi;
85
M[step][i] %=prime;
86
}
87
88
/* changed tilman 9/12/96 from:
89
det *= msi; det %=prime;
90
to :*/
91
92
det *= M[step][step]; det %=prime;
93
94
M[step][step] = 1;
95
96
/************************************************************************\
97
| clear column no. step
98
\************************************************************************/
99
for(i=step+1;i<dim;i++)
100
{
101
if(M[i][step] != 0)
102
{
103
for(j=step+1;j<dim;j++)
104
{
105
M[i][j] -= M[i][step] * M[step][j];
106
M[i][j] %= prime;
107
}
108
M[i][step] = 0;
109
}
110
}
111
}
112
for(i=0;i<dim;i++)
113
free(M[i]);
114
free(M);
115
det %= prime;
116
if(det < -(prime/2))
117
det += prime;
118
if(det > (prime/2))
119
det -=prime;
120
if(prime == 2 && det == -1)
121
det = 1;
122
return(det);
123
}
124
125