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
#include "matrix.h"
4
#include "getput.h"
5
6
/**************************************************************************\
7
@---------------------------------------------------------------------------
8
@---------------------------------------------------------------------------
9
@ FILE: put_mat.c
10
@---------------------------------------------------------------------------
11
@---------------------------------------------------------------------------
12
@
13
\**************************************************************************/
14
/*{{{}}}*/
15
/*{{{ put_mat*/
16
/*
17
@
18
@ void put_mat( mat, file_name, comment, options );
19
@ void fput_mat (outfile, mat, comment, options)
20
@
21
@ result: void
22
@ arguments:
23
@ matrix_TYP *mat -- the matrix to be written
24
@ char * file_name -- the name of the file the matrix will be written to
25
@ if file_name == NULL, we write to stdout
26
@ char * comment -- optional comment written to the output-file
27
@ unsigned int options -- options to controll the output of the matrix
28
@
29
@ Currently, there are two options. The one is called PM_RATIONAL and
30
@ has a value of 0x00000001 (defined in ../../include/matrix.h ). It
31
@ causes the matrix to be written with numerator and denominator for
32
@ each entry if it isn't
33
@ integral. The flag is still ignored for matrices that have array.N
34
@ allocated. If PM_NORMALIZED is not set, even rational matrices are
35
@ written in lcd-representation]
36
@ The other flag is called PM_SHORTCUT and causes the flag-entries
37
@ of the matrix not to be ignored, i.e. the matrix is not printed as
38
@ an NxM array, if it is symmetric, diagonal or even scalar.
39
@
40
@ This is the first time since I started to compet with this confused
41
@ source-code that I discover that someone used bit-flags, wich is a
42
@ nice thing, though the one forgot to #define and document them.
43
@
44
@ These flags should be used the following way:
45
@
46
@ put_mat( ... , PM_RATIONAL );
47
@ or put_mat( ... , PM_SHORTCUT );
48
@ or put_mat( ... , PM_SHORTCUT | PM_RATIONAL );
49
@ or just put_mat( ... , 0UL );
50
@
51
@ Note: "0UL" means just "(unsigned long)0"
52
@-------------------------------------------------------------------------
53
@
54
*/
55
void put_mat (mat, file_name, comment, options)
56
matrix_TYP *mat;
57
char file_name[], comment[] ;
58
unsigned long options;
59
{
60
FILE *outfile;
61
62
63
64
65
/*
66
* Open output file
67
*/
68
if ( file_name != NULL ) {
69
if ( (outfile = fopen (file_name, "w")) == NULL ) {
70
perror("put_mat: Error in fopen()");
71
exit (4);
72
}
73
} else {
74
outfile = stdout;
75
}
76
fput_mat (outfile, mat, comment, options);
77
if ( outfile != stdout ) {
78
fclose (outfile);
79
#if 0
80
fprintf (stderr, "%s written to %s\n", comment, file_name);
81
#endif
82
}
83
fflush(stdout);
84
}
85
86
/*}}} */
87
/*{{{ fput_mat*/
88
89
90
/*--------------------------------------------------------------------*\
91
| the same as put_mat, but output_file must already have been |
92
| opened |
93
\*--------------------------------------------------------------------*/
94
95
void fput_mat (outfile, mat, comment, options)
96
FILE *outfile;
97
matrix_TYP *mat;
98
char comment[] ;
99
unsigned long options;
100
{
101
int *max_lenZ;
102
flag_TYP flags;
103
rational d, *max_lenQ;
104
boolean Normalized;
105
int i, j, str_len;
106
char string[132];
107
char format[32];
108
109
Normalized = !(options & PM_RATIONAL);
110
/*
111
* claus: I've got to correct that one day! Normalized means to
112
* print out a matrix with kgv.
113
*/
114
if ( mat->array.N != NULL ) Normalized = 0;
115
116
flags = mat->flags;
117
if ( !(options & PM_SHORTCUT ) )
118
{
119
flags.Symmetric =
120
flags.Diagonal =
121
flags.Scalar = FALSE;
122
}
123
124
/*
125
* Print header line
126
*/
127
fprintf (outfile, "%d", mat->rows);
128
if ( mat->rows != mat->cols ) {
129
fprintf (outfile, "x%d", mat->cols);
130
} else {
131
if ( flags.Symmetric ) {
132
if ( flags.Diagonal ) {
133
fprintf (outfile, "d");
134
if ( flags.Scalar ) {
135
fprintf (outfile, "0");
136
} else {
137
fprintf (outfile, "1"); /* ! Scalar */
138
}
139
} else {
140
fprintf (outfile, "x0"); /* !Diagonal */
141
}
142
} /* Symmetric */
143
}
144
if ( !flags.Integral && mat->array.N == NULL ) {
145
fprintf (outfile, "\t/");
146
if ( Normalized ) {
147
fprintf (outfile, "%d\t", mat->kgv);
148
} else {
149
fprintf (outfile, "0\t" );
150
}
151
}
152
fprintf (outfile, "\t%% %s\n", comment);
153
154
/*
155
* print matrix
156
*/
157
if ( flags.Integral || Normalized ) {
158
if ( flags.Diagonal ) {
159
if ( flags.Scalar ) {
160
fprintf (outfile, "%d\n", mat->array.SZ[0][0]);
161
} else {
162
for ( i = 0 ; i < mat->rows; i++ ) {
163
fprintf (outfile, "%d ", mat->array.SZ[i][i]);
164
}
165
fprintf (outfile, "\n");
166
} /* !Scalar */
167
} else { /* !Diagonal */
168
max_lenZ = (int *)malloc((unsigned)mat->cols*sizeof(int));
169
for ( i = 0 ; i < mat->cols; i++ ) {
170
max_lenZ[i] = 0;
171
}
172
for ( i = 0; i < mat->rows; i++ ) {
173
for(j=0; j<(flags.Symmetric ? i+1 : mat->cols); j++) {
174
str_len=sprintf(string,"%d",mat->array.SZ[i][j]);
175
if ( str_len >= max_lenZ[j] ) {
176
max_lenZ[j] = str_len + 1;
177
}
178
}
179
}
180
for (i = 0; i < mat->rows; i++) {
181
for (j=0; j < (flags.Symmetric ? i+1 : mat->cols); j++)
182
fprintf(outfile,"%*d",max_lenZ[j],mat->array.SZ[i][j]);
183
fprintf (outfile, "\n");
184
}
185
free (max_lenZ);
186
}
187
} else { /* !Integral && !Normalized */
188
if ( flags.Diagonal ) {
189
if ( flags.Scalar ) {
190
d.z = mat->array.SZ[0][0];
191
if ( mat->array.N != NULL ) {
192
d.n = mat->array.N[0][0];
193
} else {
194
d.n = mat->kgv;
195
}
196
Normal (&d);
197
fprintf (outfile, "%d/%d\n", d.z, d.n);
198
} else {
199
for ( i = 0 ; i < mat->cols; i++ ) {
200
d.z = mat->array.SZ[i][i];
201
if ( mat->array.N != NULL ) {
202
d.n = mat->array.N[i][i];
203
} else {
204
d.n = mat->kgv;
205
}
206
Normal (&d);
207
if ( d.n == 1 ) {
208
fprintf (outfile, " %d", d.z);
209
} else {
210
fprintf (outfile, " %d/%d", d.z, d.n );
211
}
212
}
213
fprintf (outfile, "\n");
214
}
215
} else { /* !Diagonal */
216
max_lenQ=(rational *)malloc(mat->cols*sizeof(rational));
217
for (i = 0; i < mat->cols; i++ ) {
218
max_lenQ[i] = Zero;
219
}
220
for (i = 0; i < mat->rows; i++) {
221
for( j = 0; j < (flags.Symmetric ? i+1 : mat->cols); j++ ) {
222
d.z = mat->array.SZ[i][j];
223
if ( mat->array.N != NULL ) {
224
d.n = mat->array.N[i][j];
225
} else {
226
d.n = mat->kgv;
227
}
228
Normal (&d);
229
if((str_len=sprintf(string,"%d",d.z))>=max_lenQ[j].z) {
230
max_lenQ[j].z = str_len + 1;
231
}
232
if((str_len=sprintf(string,"%d",d.n))>max_lenQ[j].n ) {
233
max_lenQ[j].n = str_len;
234
}
235
}
236
}
237
for ( i = 0; i < mat->rows; i++) {
238
for(j=0;j<(flags.Symmetric ? i+1 : mat->cols); j++ ) {
239
d.z = mat->array.SZ[i][j];
240
if ( mat->array.N != NULL ) {
241
d.n = mat->array.N[i][j];
242
} else {
243
d.n = mat->kgv;
244
}
245
Normal (&d);
246
if ( d.n == 1 ) {
247
sprintf(format,"%%%dd",max_lenQ[j].z);
248
fprintf(outfile,"%*d",max_lenQ[j].z+max_lenQ[j].n+1,d.z);
249
} else {
250
sprintf(format,"%%%dd/%%-%dd",max_lenQ[j].z,max_lenQ[j].n);
251
fprintf(outfile,format,d.z,d.n);
252
}
253
}
254
fprintf (outfile, "\n");
255
}
256
free (max_lenQ);
257
}
258
}
259
}
260
/*}}} */
261
262