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

563640 views
1
#include "typedef.h"
2
#include "matrix.h"
3
#include "tools.h"
4
5
/**************************************************************************\
6
@---------------------------------------------------------------------------
7
@---------------------------------------------------------------------------
8
@ FILE: gauss_mat.c
9
@---------------------------------------------------------------------------
10
@---------------------------------------------------------------------------
11
@
12
\**************************************************************************/
13
14
/*{{{}}}*/
15
/*{{{ tgauss*/
16
/*
17
@-------------------------------------------------------------------
18
@ int tgauss(mat)
19
@
20
@ Integral Gauss-algorithm where the matrix itself is changed
21
@ The Return-value is the rank of the matrix. The matrix is not
22
@ shrinked to its rank-size.
23
@-------------------------------------------------------------------
24
*/
25
26
int tgauss(mat)
27
matrix_TYP *mat;
28
{
29
int i,j,k,l;
30
int *nzero;
31
int **Z, *r, flag,ww, waste;
32
int rg, rows, cols, tmp, actrow;
33
34
/* put_mat(mat,NULL,NULL,0); */
35
flag = waste = 0;
36
rows = mat->rows;
37
cols = mat->cols;
38
Z = mat->array.SZ;
39
mat->flags.Symmetric =
40
mat->flags.Diagonal = FALSE;
41
nzero = (int *)malloc((cols+1)*sizeof(int));
42
/*COUNTER++;*/
43
actrow = 0;
44
for (i = 0; i < cols; i++) {
45
rg = 0;
46
do {
47
/* put_mat(mat,NULL,NULL,0); */
48
/*
49
* Find the minimum of the non-zero entries of the i-th col
50
*/
51
flag = 0;
52
tmp = actrow;
53
ww = ((actrow < rows) && (Z[actrow][i] != 0)) ? Z[actrow][i] : 0;
54
for (j = actrow+1; j < rows; j++) {
55
if (Z[j][i] != 0) {
56
flag = 1;
57
if(ww != 0) {
58
if ( abs(Z[j][i]) < abs(ww)) {
59
ww = Z[j][i];
60
tmp = j;
61
}
62
} else {
63
ww = Z[j][i];
64
tmp = j;
65
}
66
}
67
}
68
/*===========================================================*\
69
|| Swap the tmp-row into the actrow-th rows and clear the ||
70
|| i-th column downwards. ||
71
\*===========================================================*/
72
if (ww != 0) rg = 1;
73
if (flag) {
74
if (tmp != actrow) {
75
r = Z[actrow]; Z[actrow] = Z[tmp]; Z[tmp] = r;
76
}
77
k = 0;
78
waste = 0;
79
for(j = i; j < cols; j++) {
80
if (Z[actrow][j] != 0) {
81
nzero[k++] = j;
82
}
83
}
84
nzero[k] = -1;
85
for(tmp = actrow+1; tmp < rows; tmp++)
86
if(Z[tmp][i] != 0) {
87
k = 0;
88
waste= min_div(Z[tmp][i], Z[actrow][i]);
89
/* printf("mindiv: %d\n",waste); */
90
while(nzero[k] >= 0) {
91
Z[tmp][nzero[k]] -= waste * Z[actrow][nzero[k]];
92
k++;
93
}
94
}
95
}
96
/*
97
* repeat the steps until every entry down from the i-th row
98
* is zero.
99
*/
100
} while(flag);
101
actrow += rg;
102
}
103
104
/*
105
* determine the rank of the matrix
106
*/
107
for(i = 0; i < cols; i++) {
108
nzero[i] = 0;
109
}
110
i = rows-1;
111
tmp = 4*cols;
112
while((i >= 0) && (memcmp(Z[i],nzero,tmp) == 0)) {
113
i--;
114
}
115
i++;
116
rows = i;
117
118
/*===========================================================*\
119
|| 03.02.92: Try to minimize the entries of the upper right ||
120
|| triangular matrix by doing the subtraction upwards ||
121
\*===========================================================*/
122
123
memset(nzero,0,cols*sizeof(int));
124
for( i = 1; i < rows; i++) {
125
tmp = 0;
126
k = 0;
127
while(Z[i][tmp] == 0) {
128
tmp++;
129
}
130
for (j = tmp; j < cols; j++) {
131
if(Z[i][j] != 0 ) {
132
nzero[k++] = j;
133
}
134
}
135
for(j = i-1; j >= 0; j--) {
136
waste= min_div(Z[j][tmp],Z[i][tmp]);
137
if (waste != 0) {
138
for (l = 0; l <k; l++) {
139
Z[j][nzero[l]] -= waste * Z[i][nzero[l]];
140
}
141
/*put_mat(mat,NULL,NULL,0); */
142
}
143
}
144
}
145
free(nzero);
146
return(rows);
147
}
148
149
150
151
152
153
/**************************************************************************\
154
@---------------------------------------------------------------------------
155
@ int row_gauss(M)
156
@ matrix_TYP *M;
157
@
158
@ integral gauss applied to the rows of M,
159
$ return is the rank of the matrix.
160
@ M is changed!
161
@---------------------------------------------------------------------------
162
@
163
\**************************************************************************/
164
int row_gauss(M)
165
matrix_TYP *M;
166
{
167
int i,j;
168
int step;
169
int a1,a2,gcd;
170
int tester, *v;
171
int spos = 0;
172
int x,y;
173
174
for(step = 0; step < M->rows; step++)
175
{
176
tester = FALSE;
177
while(tester == FALSE)
178
{
179
i = step;
180
while(i<M->rows && M->array.SZ[i][spos] == 0)
181
i++;
182
if(i<M->rows)
183
{
184
tester = TRUE;
185
if(i != step)
186
{
187
v = M->array.SZ[i];
188
M->array.SZ[i] = M->array.SZ[step];
189
M->array.SZ[step] = v;
190
}
191
}
192
else
193
spos++;
194
if(spos == M->cols)
195
return(step);
196
}
197
for(i=step+1;i<M->rows;i++)
198
{
199
if(M->array.SZ[i][spos] != 0)
200
{
201
gcd_darstell(M->array.SZ[step][spos], M->array.SZ[i][spos], &a1, &a2, &gcd);
202
if(a1 == 0)
203
{
204
v = M->array.SZ[step];
205
M->array.SZ[step] = M->array.SZ[i];
206
M->array.SZ[i] = v;
207
j = a1; a1 = a2; a2 = j;
208
}
209
M->array.SZ[step][spos] /= gcd;
210
M->array.SZ[i][spos] /= gcd;
211
for(j=spos + 1; j<M->cols; j++)
212
{
213
x = M->array.SZ[step][j];
214
y = M->array.SZ[i][j];
215
M->array.SZ[step][j] = a1 * x + a2 * y;
216
M->array.SZ[i][j] = M->array.SZ[step][spos] * y - M->array.SZ[i][spos] * x;
217
}
218
M->array.SZ[step][spos] = gcd;
219
M->array.SZ[i][spos] = 0;
220
}
221
}
222
}
223
return(M->rows);
224
}
225
226
/*}}} */
227
/*{{{ ggauss*/
228
/*
229
@-------------------------------------------------------------------
230
@ matrix_TYP *ggauss(mat)
231
@ matrix_TYP *mat;
232
@
233
@ Integral Gauss-algorithm where a copy of mat is changed
234
@ and the number of rows is shrinked to the rank of the matrix.
235
@-------------------------------------------------------------------
236
*/
237
matrix_TYP *ggauss(mat)
238
matrix_TYP *mat;
239
{
240
matrix_TYP *elt;
241
int rank;
242
243
elt = copy_mat( mat );
244
/******************
245
rank = tgauss( elt );
246
******************/
247
rank = row_gauss( elt );
248
if(rank != elt->rows) {
249
real_mat( elt, rank, elt->cols);
250
}
251
return( elt );
252
}
253
254
/*}}} */
255
256