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