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
/*{{{}}}*/
2
/*{{{ include*/
3
#include "typedef.h"
4
#include "tools.h"
5
#include "matrix.h"
6
#include "getput.h"
7
#include <string.h>
8
9
/*}}} */
10
11
/**************************************************************************\
12
@---------------------------------------------------------------------------
13
@---------------------------------------------------------------------------
14
@ FILE: get_mat.c
15
@---------------------------------------------------------------------------
16
@---------------------------------------------------------------------------
17
@
18
\**************************************************************************/
19
/*-----------------------------------------------------------*\
20
| Read matrix from infile |
21
\*-----------------------------------------------------------*/
22
23
/*{{{ static alloc_N_if_neccessary*/
24
static void
25
alloc_N_if_neccessary( mat, flags )
26
matrix_TYP *mat;
27
flag_TYP *flags;
28
{
29
30
if ( mat->array.N == NULL ) {
31
if ( mat->prime != 0 ) {
32
fprintf(stderr,"get_mat: Error: matrix over GF(%d) tries to be rational\n",
33
mat->prime);
34
exit( 3 );
35
} else if ( mat->kgv != 1 ) {
36
fprintf(stderr,"get_mat: Error: matrix tries to use kgv %d and rational representation.\n",
37
mat->kgv );
38
exit( 3 );
39
} else {
40
flags->Integral = FALSE;
41
mat->array.N = (int **)malloc2dim( mat->rows, mat->cols,
42
sizeof(int) );
43
memset2dim( (char **) mat->array.N,
44
mat->rows, mat->cols, sizeof(int), (char *)&Zero.n );
45
}
46
}
47
}
48
49
/*}}} */
50
/*{{{ fget_mat*/
51
52
/**************************************************************************\
53
@---------------------------------------------------------------------------
54
@ matrix_TYP *fget_mat (infile)
55
@ FILE *infile;
56
@ reads a matrix from infile
57
@---------------------------------------------------------------------------
58
@
59
\**************************************************************************/
60
61
matrix_TYP *fget_mat (infile)
62
FILE *infile;
63
64
{
65
int rM, cM;
66
int **M;
67
matrix_TYP *mat;
68
flag_TYP flags;
69
char string[256], *str ;
70
int i, j ;
71
72
73
74
75
flags.Integral =
76
flags.Symmetric =
77
flags.Diagonal =
78
flags.Scalar = FALSE;
79
/*------------------------------------------------------------*\
80
| Read and scan header line |
81
\*------------------------------------------------------------*/
82
fscanf (infile, "%[ \t\n]", string);
83
fscanf (infile, "%[^\n]",string);
84
strtok (string, "%");
85
sscanf (string, "%d", &rM);
86
if ( (str = strpbrk (string, "xd")) != NULL ) {
87
sscanf ( ++str, "%d", &cM);
88
if ( str[-1] == 'x' ) {
89
if ( cM == 0 ) {
90
cM = rM;
91
flags.Symmetric = TRUE;
92
}
93
} else {
94
flags.Symmetric = flags.Diagonal = TRUE;
95
flags.Scalar = (cM == 0);
96
cM = rM;
97
}
98
}
99
else {
100
cM = rM;
101
}
102
/*
103
* WARNING: You can read matrizes either of prime_Typ or with
104
* kgv. If both are set in the headline, the second entry
105
* will be ignored.
106
*/
107
mat = init_mat(rM,cM,"");
108
if ( (str = strchr (string, '/')) != NULL ) {
109
if(str[1] == 'p') {
110
str += 2;
111
sscanf(str, "%d", &mat->prime);
112
flags.Integral = TRUE;
113
} else {
114
sscanf(++str, "%d", &mat->kgv);
115
flags.Integral = FALSE;
116
}
117
} else {
118
flags.Integral = TRUE;
119
}
120
M = mat->array.SZ;
121
for ( i = 0; i < rM; i++) {
122
/*
123
* Read the matrix
124
*/
125
if ( flags.Scalar ) {
126
fscanf( infile, "%s", string );
127
if ( strchr( string, '/') == NULL ) {
128
sscanf(string, "%d", &M[0][0]);
129
for ( i = 1; i < rM; i++ )M[i][i]= M[0][0];
130
} else {
131
alloc_N_if_neccessary( mat, &flags );
132
sscanf(string, "%d/%d", &M[0][0],&mat->array.N[0][0]);
133
for ( i = 1; i < rM; i++ ) {
134
M[i][i]= M[0][0];
135
mat->array.N[i][i] = mat->array.N[0][0];
136
}
137
}
138
} else {
139
if ( flags.Diagonal ) {
140
for ( i = 0; i < cM; i++ ) {
141
fscanf( infile, "%s", string );
142
if ( strchr( string, '/') == NULL ) {
143
sscanf(string, "%d", &M[i][i]);
144
} else {
145
alloc_N_if_neccessary( mat, &flags );
146
sscanf(string, "%d/%d", &M[i][i],&mat->array.N[i][i]);
147
}
148
}
149
} else {
150
for ( i = 0; i < rM; i++ ) {
151
for ( j = 0; j < ( flags.Symmetric ? i+1 : cM ); j++ ) {
152
fscanf( infile, "%s", string );
153
if ( strchr( string, '/') == NULL ) {
154
sscanf(string, "%d", &M[i][j]);
155
} else {
156
alloc_N_if_neccessary( mat, &flags );
157
sscanf(string, "%d/%d", &M[i][j],&mat->array.N[i][j]);
158
}
159
}
160
}
161
if ( flags.Symmetric ) {
162
if ( mat->array.N != NULL ) {
163
for ( i = 0; i < rM; i++) {
164
for ( j = 0; j < i; j++) {
165
M[j][i]= M[i][j];
166
mat->array.N[j][i] = mat->array.N[i][j];
167
}
168
}
169
} else {
170
for ( i = 0; i < rM; i++) {
171
for ( j = 0; j < i; j++) {
172
M[j][i]= M[i][j];
173
}
174
}
175
}
176
}
177
}
178
}
179
}
180
mat->flags.Integral = flags.Integral;
181
mat->flags.Symmetric = flags.Symmetric;
182
mat->flags.Diagonal = flags.Diagonal;
183
mat->flags.Scalar = flags.Scalar;
184
Check_mat(mat);
185
return ( mat );
186
}
187
188
189
/*}}} */
190
/*{{{ get_mat*/
191
192
/**************************************************************************\
193
@---------------------------------------------------------------------------
194
@ matrix_TYP *get_mat (file_name)
195
@ char *file_name;
196
@ reads a matrix from the file with name 'file_name'
197
@---------------------------------------------------------------------------
198
@
199
\**************************************************************************/
200
matrix_TYP *get_mat (file_name)
201
char *file_name;
202
203
{
204
matrix_TYP *mat;
205
FILE *infile;
206
207
/*------------------------------------------------------------*\
208
| Open input file |
209
\*------------------------------------------------------------*/
210
if ( file_name == NULL )
211
infile = stdin;
212
else
213
if ( (infile = fopen (file_name, "r")) == NULL ) {
214
215
fprintf (stderr, "Could not open input-file %s\n", file_name);
216
exit (4);
217
}
218
/*------------------------------------------------------------*\
219
| Call fget_mat |
220
\*------------------------------------------------------------*/
221
mat = fget_mat(infile);
222
/*------------------------------------------------------------*\
223
| Close input file |
224
\*------------------------------------------------------------*/
225
if ( infile != stdin )
226
fclose (infile);
227
return ( mat );
228
}
229
230
/*}}} */
231
/*{{{ mget_mat*/
232
/*
233
@-------------------------------------------------------------------------
234
@
235
@ mat_array = mget_mat( file_name, anz );
236
@
237
@ differs from fmget_mat() only in specifying the file name instead of the
238
@ file-pointer.
239
@
240
@-------------------------------------------------------------------------
241
*/
242
matrix_TYP **mget_mat (file_name, anz)
243
char *file_name;
244
int *anz;
245
{
246
matrix_TYP **mat;
247
FILE *infile;
248
249
250
251
252
/*
253
* Open input file
254
*/
255
if ( file_name == NULL ) {
256
infile = stdin;
257
} else {
258
if ( (infile = fopen (file_name, "r")) == NULL ) {
259
perror("mget_mat: Error: Could not open input-file");
260
exit (4);
261
}
262
}
263
mat = fmget_mat( infile, anz );
264
/*
265
* Close input file
266
*/
267
if ( infile != stdin ) {
268
fclose (infile);
269
}
270
return ( mat );
271
}
272
273
/*}}} */
274
/*{{{ fmget_mat*/
275
/*
276
@-------------------------------------------------------------------------
277
@
278
@ mat_array = fmget_mat( infile, anz );
279
@
280
@ matrix_TYP **mat_array: array of matrices read from infile
281
@
282
@ FILE *infile: the file the matrices are read from
283
@ int *anz: the number of matrices that have been read from infile.
284
@ is set by the function.
285
@-------------------------------------------------------------------------
286
@
287
*/
288
matrix_TYP **fmget_mat (infile, anz)
289
FILE *infile;
290
int *anz;
291
{
292
matrix_TYP **mat;
293
char string[512];
294
int k ;
295
296
/*
297
* Open input file
298
*/
299
fscanf (infile, "%[^\n]",string);
300
if ( string[0] != '#' ) {
301
*anz = 1;
302
mat = (matrix_TYP **)malloc(sizeof(matrix_TYP *));
303
rewind(infile);
304
mat[0] = fget_mat(infile);
305
} else {
306
sscanf (string, "#%u", anz);
307
mat = (matrix_TYP **)malloc(*anz*sizeof(matrix_TYP *));
308
for ( k = 0; k < *anz; k++) {
309
mat[k] = fget_mat(infile);
310
}
311
}
312
return ( mat );
313
}
314
/*}}} */
315
316