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 "matrix.h"
3
#include "tools.h"
4
5
static int n;
6
7
/************************************************************************\
8
| The Matrix M is transformed to a matrix M' such that
9
| M' = Trf * M is Gauss-reduced, with Trf integral with determinante +-1.
10
\************************************************************************/
11
12
int Trf_gauss(M, Trf)
13
matrix_TYP *M, *Trf;
14
{
15
int i,j;
16
int step;
17
int a1,a2,gcd;
18
int tester, *v;
19
int spos = 0;
20
int x,y;
21
int Mi, Ms;
22
23
if(Trf->cols != M->rows)
24
{
25
printf("Error in Trf_gauss: matrices M and Trf are not compatible\n");
26
exit(3);
27
}
28
if(Trf->cols != Trf->rows)
29
{
30
printf("Error in Trf_gauss: Trf must be a square matrix\n");
31
exit(3);
32
}
33
n = Trf->rows;
34
for(i=0;i<n;i++)
35
for(j=0;j<n; j++)
36
Trf->array.SZ[i][j] = 0;
37
for(i=0;i<n;i++)
38
Trf->array.SZ[i][i] = 1;
39
for(step = 0; step < n; step++)
40
{
41
tester = FALSE;
42
while(tester == FALSE)
43
{
44
i = step;
45
while(i<n && M->array.SZ[i][spos] == 0)
46
i++;
47
if(i<n)
48
{
49
tester = TRUE;
50
if(i != step)
51
{
52
v = M->array.SZ[i];
53
M->array.SZ[i] = M->array.SZ[step];
54
M->array.SZ[step] = v;
55
v = Trf->array.SZ[i];
56
Trf->array.SZ[i] = Trf->array.SZ[step];
57
Trf->array.SZ[step] = v;
58
}
59
}
60
else
61
spos++;
62
if(spos == M->cols)
63
return(step);
64
}
65
for(i=step+1;i<n;i++)
66
{
67
if(M->array.SZ[i][spos] != 0)
68
{
69
gcd_darstell(M->array.SZ[step][spos], M->array.SZ[i][spos], &a1, &a2, &gcd);
70
if(a1 == 0)
71
{
72
v = M->array.SZ[step];
73
M->array.SZ[step] = M->array.SZ[i];
74
M->array.SZ[i] = v;
75
v = Trf->array.SZ[step];
76
Trf->array.SZ[step] = Trf->array.SZ[i];
77
Trf->array.SZ[i] = v;
78
j = a1; a1 = a2; a2 = j;
79
}
80
Ms = M->array.SZ[step][spos] / gcd;
81
Mi = M->array.SZ[i][spos] / gcd;
82
M->array.SZ[step][spos] /= gcd;
83
M->array.SZ[i][spos] /= gcd;
84
for(j=0;j<n;j++)
85
{
86
x = Trf->array.SZ[step][j];
87
y = Trf->array.SZ[i][j];
88
Trf->array.SZ[step][j] = a1 * x + a2 * y;
89
Trf->array.SZ[i][j] = Ms * y - Mi * x;
90
}
91
for(j=spos + 1; j<M->cols; j++)
92
{
93
x = M->array.SZ[step][j];
94
y = M->array.SZ[i][j];
95
M->array.SZ[step][j] = a1 * x + a2 * y;
96
M->array.SZ[i][j] = Ms * y - Mi * x;
97
}
98
M->array.SZ[step][spos] = gcd;
99
M->array.SZ[i][spos] = 0;
100
}
101
}
102
}
103
return(n);
104
}
105
106
/************************************************************************\
107
| solve_mat(M) calculates an Matrix X with MX^{tr} = 0, such that
108
| the rows of X are a Z-basis of the solution space.
109
\************************************************************************/
110
matrix_TYP *solve_mat(M)
111
matrix_TYP *M;
112
{
113
matrix_TYP *M1, *M1t, *Trf, *X, *erg;
114
int i,rang, n;
115
116
extern matrix_TYP *copy_mat();
117
extern matrix_TYP *tr_pose();
118
extern matrix_TYP *init_mat();
119
extern matrix_TYP *mat_mul();
120
121
M1 = copy_mat(M);
122
row_gauss(M1);
123
M1t = tr_pose(M1);
124
n = M1t->rows;
125
Trf = init_mat(n, n, "");
126
rang = Trf_gauss(M1t, Trf);
127
X = init_mat(n-rang, n, "");
128
for(i=0;i<n-rang;i++)
129
X->array.SZ[i][rang+i] = 1;
130
erg = mat_mul(X, Trf);
131
free_mat(X); free_mat(Trf);
132
free_mat(M1); free_mat(M1t);
133
return(erg);
134
}
135
136