File: zmatio.c

package info (click to toggle)
meschach 1.2b-14
  • links: PTS
  • area: main
  • in suites: buster
  • size: 2,164 kB
  • ctags: 2,188
  • sloc: ansic: 21,959; makefile: 484
file content (400 lines) | stat: -rw-r--r-- 10,649 bytes parent folder | download | duplicates (7)
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

/**************************************************************************
**
** 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
  **************************************************************************/

complex	z_finput(fp)
FILE	*fp;
{
    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;
}


ZMAT	*zm_finput(fp,a)
FILE    *fp;
ZMAT	*a;
{
     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 */
ZMAT     *izm_finput(fp,mat)
FILE    *fp;
ZMAT     *mat;
{
     char       c;
     u_int      i, j, m, n, dynamic;
     /* 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: ");
	  fscanf(fp,"%c",&c);
	  if ( 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 */
ZMAT     *bzm_finput(fp,mat)
FILE    *fp;
ZMAT     *mat;
{
     u_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);
}

ZVEC     *zv_finput(fp,x)
FILE    *fp;
ZVEC     *x;
{
     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 */
ZVEC     *izv_finput(fp,vec)
FILE    *fp;
ZVEC     *vec;
{
     u_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 */
ZVEC     *bzv_finput(fp,vec)
FILE    *fp;
ZVEC    *vec;
{
     u_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 char    *zformat = " (%14.9g, %14.9g) ";

char	*setzformat(f_string)
char    *f_string;
{
    char	*old_f_string;
    old_f_string = zformat;
    if ( f_string != (char *)NULL && *f_string != '\0' )
	zformat = f_string;

    return old_f_string;
}

void	z_foutput(fp,z)
FILE	*fp;
complex	z;
{
    fprintf(fp,zformat,z.re,z.im);
    putc('\n',fp);
}

void    zm_foutput(fp,a)
FILE    *fp;
ZMAT     *a;
{
     u_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);
     }
}

void    zv_foutput(fp,x)
FILE    *fp;
ZVEC     *x;
{
     u_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);
}


void    zm_dump(fp,a)
FILE    *fp;
ZMAT     *a;
{
	u_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);
     }
}



void    zv_dump(fp,x)
FILE    *fp;
ZVEC     *x;
{
     u_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);
}