-
Notifications
You must be signed in to change notification settings - Fork 18
/
zmatio.c
450 lines (400 loc) · 11.8 KB
/
zmatio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/**************************************************************************
**
** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
**
** Meschach Library
**
** This Meschach Library is provided "as is" without any express
** or implied warranty of any kind with respect to this software.
** In particular the authors shall not be liable for any direct,
** indirect, special, incidental or consequential damages arising
** in any way from use of the software.
**
** Everyone is granted permission to copy, modify and redistribute this
** Meschach Library, provided:
** 1. All copies contain this copyright notice.
** 2. All modified copies shall carry a notice stating who
** made the last modification and the date of such modification.
** 3. No charge is made for this software or works derived from it.
** This clause shall not be construed as constraining other software
** distributed on the same medium as this software, nor is a
** distribution fee considered a charge.
**
***************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include "zmatrix.h"
static char rcsid[] = "$Id: zmatio.c,v 1.1 1994/01/13 04:25:18 des Exp $";
/* local variables */
static char line[MAXLINE];
/**************************************************************************
Input routines
**************************************************************************/
#ifndef ANSI_C
complex z_finput(fp)
FILE *fp;
#else
complex z_finput(FILE *fp)
#endif
{
int io_code;
complex z;
skipjunk(fp);
if ( isatty(fileno(fp)) )
{
do {
fprintf(stderr,"real and imag parts: ");
if ( fgets(line,MAXLINE,fp) == NULL )
error(E_EOF,"z_finput");
#if REAL == DOUBLE
io_code = sscanf(line,"%lf%lf",&z.re,&z.im);
#elif REAL == FLOAT
io_code = sscanf(line,"%f%f",&z.re,&z.im);
#endif
} while ( io_code != 2 );
}
else
#if REAL == DOUBLE
if ( (io_code=fscanf(fp," (%lf,%lf)",&z.re,&z.im)) < 2 )
#elif REAL == FLOAT
if ( (io_code=fscanf(fp," (%f,%f)",&z.re,&z.im)) < 2 )
#endif
error((io_code == EOF) ? E_EOF : E_FORMAT,"z_finput");
return z;
}
#ifndef ANSI_C
ZMAT *zm_finput(fp,a)
FILE *fp;
ZMAT *a;
#else
ZMAT *zm_finput(FILE *fp,ZMAT *a)
#endif
{
ZMAT *izm_finput(),*bzm_finput();
if ( isatty(fileno(fp)) )
return izm_finput(fp,a);
else
return bzm_finput(fp,a);
}
/* izm_finput -- interactive input of matrix */
#ifndef ANSI_C
ZMAT *izm_finput(fp,mat)
FILE *fp;
ZMAT *mat;
#else
ZMAT *izm_finput(FILE *fp, ZMAT *mat)
#endif
{
char c;
unsigned int i, j, m, n, dynamic;
int io_code;
/* dynamic set to TRUE if memory allocated here */
/* get matrix size */
if ( mat != ZMNULL && mat->m<MAXDIM && mat->n<MAXDIM )
{ m = mat->m; n = mat->n; dynamic = FALSE; }
else
{
dynamic = TRUE;
do
{
fprintf(stderr,"ComplexMatrix: rows cols:");
if ( fgets(line,MAXLINE,fp)==NULL )
error(E_INPUT,"izm_finput");
} while ( sscanf(line,"%u%u",&m,&n)<2 || m>MAXDIM || n>MAXDIM );
mat = zm_get(m,n);
}
/* input elements */
for ( i=0; i<m; i++ )
{
redo:
fprintf(stderr,"row %u:\n",i);
for ( j=0; j<n; j++ )
do
{
redo2:
fprintf(stderr,"entry (%u,%u): ",i,j);
if ( !dynamic )
fprintf(stderr,"old (%14.9g,%14.9g) new: ",
mat->me[i][j].re,mat->me[i][j].im);
if ( fgets(line,MAXLINE,fp)==NULL )
error(E_INPUT,"izm_finput");
if ( (*line == 'b' || *line == 'B') && j > 0 )
{ j--; dynamic = FALSE; goto redo2; }
if ( (*line == 'f' || *line == 'F') && j < n-1 )
{ j++; dynamic = FALSE; goto redo2; }
} while ( *line=='\0' ||
#if REAL == DOUBLE
sscanf(line,"%lf%lf",
#elif REAL == FLOAT
sscanf(line,"%f%f",
#endif
&mat->me[i][j].re,&mat->me[i][j].im)<1 );
fprintf(stderr,"Continue: ");
io_code = fscanf(fp,"%c",&c);
if ( io_code < 1 || c == 'n' || c == 'N' )
{ dynamic = FALSE; goto redo; }
if ( (c == 'b' || c == 'B') /* && i > 0 */ )
{ if ( i > 0 )
i--;
dynamic = FALSE; goto redo;
}
}
return (mat);
}
/* bzm_finput -- batch-file input of matrix */
#ifndef ANSI_C
ZMAT *bzm_finput(fp,mat)
FILE *fp;
ZMAT *mat;
#else
ZMAT *bzm_finput(FILE *fp,ZMAT *mat)
#endif
{
unsigned int i,j,m,n,dummy;
int io_code;
/* get dimension */
skipjunk(fp);
if ((io_code=fscanf(fp," ComplexMatrix: %u by %u",&m,&n)) < 2 ||
m>MAXDIM || n>MAXDIM )
error(io_code==EOF ? E_EOF : E_FORMAT,"bzm_finput");
/* allocate memory if necessary */
if ( mat==ZMNULL || mat->m<m || mat->n<n )
mat = zm_resize(mat,m,n);
/* get entries */
for ( i=0; i<m; i++ )
{
skipjunk(fp);
if ( fscanf(fp," row %u:",&dummy) < 1 )
error(E_FORMAT,"bzm_finput");
for ( j=0; j<n; j++ )
{
/* printf("bzm_finput: j = %d\n", j); */
#if REAL == DOUBLE
if ((io_code=fscanf(fp," ( %lf , %lf )",
#elif REAL == FLOAT
if ((io_code=fscanf(fp," ( %f , %f )",
#endif
&mat->me[i][j].re,&mat->me[i][j].im)) < 2 )
error(io_code==EOF ? E_EOF : E_FORMAT,"bzm_finput");
}
}
return (mat);
}
#ifndef ANSI_C
ZVEC *zv_finput(fp,x)
FILE *fp;
ZVEC *x;
#else
ZVEC *zv_finput(FILE *fp,ZVEC *x)
#endif
{
ZVEC *izv_finput(),*bzv_finput();
if ( isatty(fileno(fp)) )
return izv_finput(fp,x);
else
return bzv_finput(fp,x);
}
/* izv_finput -- interactive input of vector */
#ifndef ANSI_C
ZVEC *izv_finput(fp,vec)
FILE *fp;
ZVEC *vec;
#else
ZVEC *izv_finput(FILE *fp,ZVEC *vec)
#endif
{
unsigned int i,dim,dynamic; /* dynamic set if memory allocated here */
/* get vector dimension */
if ( vec != ZVNULL && vec->dim<MAXDIM )
{ dim = vec->dim; dynamic = FALSE; }
else
{
dynamic = TRUE;
do
{
fprintf(stderr,"ComplexVector: dim: ");
if ( fgets(line,MAXLINE,fp)==NULL )
error(E_INPUT,"izv_finput");
} while ( sscanf(line,"%u",&dim)<1 || dim>MAXDIM );
vec = zv_get(dim);
}
/* input elements */
for ( i=0; i<dim; i++ )
do
{
redo:
fprintf(stderr,"entry %u: ",i);
if ( !dynamic )
fprintf(stderr,"old (%14.9g,%14.9g) new: ",
vec->ve[i].re,vec->ve[i].im);
if ( fgets(line,MAXLINE,fp)==NULL )
error(E_INPUT,"izv_finput");
if ( (*line == 'b' || *line == 'B') && i > 0 )
{ i--; dynamic = FALSE; goto redo; }
if ( (*line == 'f' || *line == 'F') && i < dim-1 )
{ i++; dynamic = FALSE; goto redo; }
} while ( *line=='\0' ||
#if REAL == DOUBLE
sscanf(line,"%lf%lf",
#elif REAL == FLOAT
sscanf(line,"%f%f",
#endif
&vec->ve[i].re,&vec->ve[i].im) < 2 );
return (vec);
}
/* bzv_finput -- batch-file input of vector */
#ifndef ANSI_C
ZVEC *bzv_finput(fp,vec)
FILE *fp;
ZVEC *vec;
#else
ZVEC *bzv_finput(FILE *fp, ZVEC *vec)
#endif
{
unsigned int i,dim;
int io_code;
/* get dimension */
skipjunk(fp);
if ((io_code=fscanf(fp," ComplexVector: dim:%u",&dim)) < 1 ||
dim>MAXDIM )
error(io_code==EOF ? 7 : 6,"bzv_finput");
/* allocate memory if necessary */
if ( vec==ZVNULL || vec->dim<dim )
vec = zv_resize(vec,dim);
/* get entries */
skipjunk(fp);
for ( i=0; i<dim; i++ )
#if REAL == DOUBLE
if ((io_code=fscanf(fp," (%lf,%lf)",
#elif REAL == FLOAT
if ((io_code=fscanf(fp," (%f,%f)",
#endif
&vec->ve[i].re,&vec->ve[i].im)) < 2 )
error(io_code==EOF ? 7 : 6,"bzv_finput");
return (vec);
}
/**************************************************************************
Output routines
**************************************************************************/
static const char *zformat = " (%14.9g, %14.9g) ";
#ifndef ANSI_C
char *setzformat(f_string)
char *f_string;
#else
const char *setzformat(const char *f_string)
#endif
{
const char *old_f_string;
old_f_string = zformat;
if ( f_string != (char *)NULL && *f_string != '\0' )
zformat = f_string;
return old_f_string;
}
#ifndef ANSI_C
void z_foutput(fp,z)
FILE *fp;
complex z;
#else
void z_foutput(FILE *fp,complex z)
#endif
{
fprintf(fp,zformat,z.re,z.im);
putc('\n',fp);
}
#ifndef ANSI_C
void zm_foutput(fp,a)
FILE *fp;
ZMAT *a;
#else
void zm_foutput(FILE *fp,ZMAT *a)
#endif
{
unsigned int i, j, tmp;
if ( a == ZMNULL )
{ fprintf(fp,"ComplexMatrix: NULL\n"); return; }
fprintf(fp,"ComplexMatrix: %d by %d\n",a->m,a->n);
if ( a->me == (complex **)NULL )
{ fprintf(fp,"NULL\n"); return; }
for ( i=0; i<a->m; i++ ) /* for each row... */
{
fprintf(fp,"row %u: ",i);
for ( j=0, tmp=1; j<a->n; j++, tmp++ )
{ /* for each col in row... */
fprintf(fp,zformat,a->me[i][j].re,a->me[i][j].im);
if ( ! (tmp % 2) ) putc('\n',fp);
}
if ( tmp % 2 != 1 ) putc('\n',fp);
}
}
#ifndef ANSI_C
void zv_foutput(fp,x)
FILE *fp;
ZVEC *x;
#else
void zv_foutput(FILE *fp,ZVEC *x)
#endif
{
unsigned int i, tmp;
if ( x == ZVNULL )
{ fprintf(fp,"ComplexVector: NULL\n"); return; }
fprintf(fp,"ComplexVector: dim: %d\n",x->dim);
if ( x->ve == (complex *)NULL )
{ fprintf(fp,"NULL\n"); return; }
for ( i=0, tmp=0; i<x->dim; i++, tmp++ )
{
fprintf(fp,zformat,x->ve[i].re,x->ve[i].im);
if ( (tmp % 2) == 1 ) putc('\n',fp);
}
if ( (tmp % 2) != 0 ) putc('\n',fp);
}
#ifndef ANSI_C
void zm_dump(fp,a)
FILE *fp;
ZMAT *a;
#else
void zm_dump(FILE *fp, ZMAT *a)
#endif
{
unsigned int i, j, tmp;
if ( a == ZMNULL )
{ fprintf(fp,"ComplexMatrix: NULL\n"); return; }
fprintf(fp,"ComplexMatrix: %d by %d @ 0x%lx\n",a->m,a->n,(long)a);
fprintf(fp,"\tmax_m = %d, max_n = %d, max_size = %d\n",
a->max_m, a->max_n, a->max_size);
if ( a->me == (complex **)NULL )
{ fprintf(fp,"NULL\n"); return; }
fprintf(fp,"a->me @ 0x%lx\n",(long)(a->me));
fprintf(fp,"a->base @ 0x%lx\n",(long)(a->base));
for ( i=0; i<a->m; i++ ) /* for each row... */
{
fprintf(fp,"row %u: @ 0x%lx ",i,(long)(a->me[i]));
for ( j=0, tmp=1; j<a->n; j++, tmp++ )
{ /* for each col in row... */
fprintf(fp,zformat,a->me[i][j].re,a->me[i][j].im);
if ( ! (tmp % 2) ) putc('\n',fp);
}
if ( tmp % 2 != 1 ) putc('\n',fp);
}
}
#ifndef ANSI_C
void zv_dump(fp,x)
FILE *fp;
ZVEC *x;
#else
void zv_dump(FILE *fp,ZVEC *x)
#endif
{
unsigned int i, tmp;
if ( ! x )
{ fprintf(fp,"ComplexVector: NULL\n"); return; }
fprintf(fp,"ComplexVector: dim: %d @ 0x%lx\n",x->dim,(long)(x));
if ( ! x->ve )
{ fprintf(fp,"NULL\n"); return; }
fprintf(fp,"x->ve @ 0x%lx\n",(long)(x->ve));
for ( i=0, tmp=0; i<x->dim; i++, tmp++ )
{
fprintf(fp,zformat,x->ve[i].re,x->ve[i].im);
if ( tmp % 2 == 1 ) putc('\n',fp);
}
if ( tmp % 2 != 0 ) putc('\n',fp);
}