NEURON
matrixio.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 /* 1.6 matrixio.c 11/25/87 */
29 
30 
31 #include <stdio.h>
32 #include <ctype.h>
33 #include "matrix.h"
34 
35 static char rcsid[] = "matrixio.c,v 1.1 1997/12/04 17:55:35 hines Exp";
36 
37 
38 /* local variables */
39 static char line[MAXLINE];
40 
41 
42 /**************************************************************************
43  Input routines
44  **************************************************************************/
45 /* skipjunk -- skips white spaces and strings of the form #....\n
46  Here .... is a comment string */
48 FILE *fp;
49 {
50  int c;
51 
52  for ( ; ; ) /* forever do... */
53  {
54  /* skip blanks */
55  do
56  c = getc(fp);
57  while ( isspace(c) );
58 
59  /* skip comments (if any) */
60  if ( c == '#' )
61  /* yes it is a comment (line) */
62  while ( (c=getc(fp)) != '\n' )
63  ;
64  else
65  {
66  ungetc(c,fp);
67  break;
68  }
69  }
70  return 0;
71 }
72 
74 FILE *fp;
75 MAT *a;
76 {
77  MAT *im_finput(),*bm_finput();
78 
79  if ( isatty(fileno(fp)) )
80  return im_finput(fp,a);
81  else
82  return bm_finput(fp,a);
83 }
84 
85 /* im_finput -- interactive input of matrix */
87 FILE *fp;
88 MAT *mat;
89 {
90  char c;
91  u_int i, j, m, n, dynamic;
92  /* dynamic set to TRUE if memory allocated here */
93 
94  /* get matrix size */
95  if ( mat != (MAT *)NULL && mat->m<MAXDIM && mat->n<MAXDIM )
96  { m = mat->m; n = mat->n; dynamic = FALSE; }
97  else
98  {
99  dynamic = TRUE;
100  do
101  {
102  fprintf(stderr,"Matrix: rows cols:");
103  if ( fgets(line,MAXLINE,fp)==NULL )
104  error(E_INPUT,"im_finput");
105  } while ( sscanf(line,"%u%u",&m,&n)<2 || m>MAXDIM || n>MAXDIM );
106  mat = m_get(m,n);
107  }
108 
109  /* input elements */
110  for ( i=0; i<m; i++ )
111  {
112  redo:
113  fprintf(stderr,"row %u:\n",i);
114  for ( j=0; j<n; j++ )
115  do
116  {
117  redo2:
118  fprintf(stderr,"entry (%u,%u): ",i,j);
119  if ( !dynamic )
120  fprintf(stderr,"old %14.9g new: ",
121  mat->me[i][j]);
122  if ( fgets(line,MAXLINE,fp)==NULL )
123  error(E_INPUT,"im_finput");
124  if ( (*line == 'b' || *line == 'B') && j > 0 )
125  { j--; dynamic = FALSE; goto redo2; }
126  if ( (*line == 'f' || *line == 'F') && j < n-1 )
127  { j++; dynamic = FALSE; goto redo2; }
128 #if REAL == DOUBLE
129  } while ( *line=='\0' || sscanf(line,"%lf",&mat->me[i][j])<1 );
130 #elif REAL == FLOAT
131  } while ( *line=='\0' || sscanf(line,"%f",&mat->me[i][j])<1 );
132 #endif
133  fprintf(stderr,"Continue: ");
134  if(fscanf(fp,"%c",&c) != 1) {
135  error(E_INPUT, "im_finput");
136  }
137  if ( c == 'n' || c == 'N' )
138  { dynamic = FALSE; goto redo; }
139  if ( (c == 'b' || c == 'B') /* && i > 0 */ )
140  { if ( i > 0 )
141  i--;
142  dynamic = FALSE; goto redo;
143  }
144  }
145 
146  return (mat);
147 }
148 
149 /* bm_finput -- batch-file input of matrix */
151 FILE *fp;
152 MAT *mat;
153 {
154  u_int i,j,m,n,dummy;
155  int io_code;
156 
157  /* get dimension */
158  skipjunk(fp);
159  if ((io_code=fscanf(fp," Matrix: %u by %u",&m,&n)) < 2 ||
160  m>MAXDIM || n>MAXDIM )
161  error(io_code==EOF ? E_EOF : E_FORMAT,"bm_finput");
162 
163  /* allocate memory if necessary */
164  if ( mat==(MAT *)NULL )
165  mat = m_resize(mat,m,n);
166 
167  /* get entries */
168  for ( i=0; i<m; i++ )
169  {
170  skipjunk(fp);
171  if ( fscanf(fp," row %u:",&dummy) < 1 )
172  error(E_FORMAT,"bm_finput");
173  for ( j=0; j<n; j++ )
174 #if REAL == DOUBLE
175  if ((io_code=fscanf(fp,"%lf",&mat->me[i][j])) < 1 )
176 #elif REAL == FLOAT
177  if ((io_code=fscanf(fp,"%f",&mat->me[i][j])) < 1 )
178 #endif
179  error(io_code==EOF ? 7 : 6,"bm_finput");
180  }
181 
182  return (mat);
183 }
184 
186 FILE *fp;
187 PERM *px;
188 {
189  PERM *ipx_finput(),*bpx_finput();
190 
191  if ( isatty(fileno(fp)) )
192  return ipx_finput(fp,px);
193  else
194  return bpx_finput(fp,px);
195 }
196 
197 
198 /* ipx_finput -- interactive input of permutation */
200 FILE *fp;
201 PERM *px;
202 {
203  u_int i,j,size,dynamic; /* dynamic set if memory allocated here */
204  u_int entry,ok;
205 
206  /* get permutation size */
207  if ( px!=(PERM *)NULL && px->size<MAXDIM )
208  { size = px->size; dynamic = FALSE; }
209  else
210  {
211  dynamic = TRUE;
212  do
213  {
214  fprintf(stderr,"Permutation: size: ");
215  if ( fgets(line,MAXLINE,fp)==NULL )
216  error(E_INPUT,"ipx_finput");
217  } while ( sscanf(line,"%u",&size)<1 || size>MAXDIM );
218  px = px_get(size);
219  }
220 
221  /* get entries */
222  i = 0;
223  while ( i<size )
224  {
225  /* input entry */
226  do
227  {
228  redo:
229  fprintf(stderr,"entry %u: ",i);
230  if ( !dynamic )
231  fprintf(stderr,"old: %u->%u new: ",
232  i,px->pe[i]);
233  if ( fgets(line,MAXLINE,fp)==NULL )
234  error(E_INPUT,"ipx_finput");
235  if ( (*line == 'b' || *line == 'B') && i > 0 )
236  { i--; dynamic = FALSE; goto redo; }
237  } while ( *line=='\0' || sscanf(line,"%u",&entry) < 1 );
238  /* check entry */
239  ok = (entry < size);
240  for ( j=0; j<i; j++ )
241  ok &= (entry != px->pe[j]);
242  if ( ok )
243  {
244  px->pe[i] = entry;
245  i++;
246  }
247  }
248 
249  return (px);
250 }
251 
252 /* bpx_finput -- batch-file input of permutation */
254 FILE *fp;
255 PERM *px;
256 {
257  u_int i,j,size,entry,ok;
258  int io_code;
259 
260  /* get size of permutation */
261  skipjunk(fp);
262  if ((io_code=fscanf(fp," Permutation: size:%u",&size)) < 1 ||
263  size>MAXDIM )
264  error(io_code==EOF ? 7 : 6,"bpx_finput");
265 
266  /* allocate memory if necessary */
267  if ( px==(PERM *)NULL || px->size<size )
268  px = px_resize(px,size);
269 
270  /* get entries */
271  skipjunk(fp);
272  i = 0;
273  while ( i<size )
274  {
275  /* input entry */
276  if ((io_code=fscanf(fp,"%*u -> %u",&entry)) < 1 )
277  error(io_code==EOF ? 7 : 6,"bpx_finput");
278  /* check entry */
279  ok = (entry < size);
280  for ( j=0; j<i; j++ )
281  ok &= (entry != px->pe[j]);
282  if ( ok )
283  {
284  px->pe[i] = entry;
285  i++;
286  }
287  else
288  error(E_BOUNDS,"bpx_finput");
289  }
290 
291  return (px);
292 }
293 
294 
296 FILE *fp;
297 VEC *x;
298 {
299  VEC *ifin_vec(),*bfin_vec();
300 
301  if ( isatty(fileno(fp)) )
302  return ifin_vec(fp,x);
303  else
304  return bfin_vec(fp,x);
305 }
306 
307 /* ifin_vec -- interactive input of vector */
309 FILE *fp;
310 VEC *vec;
311 {
312  u_int i,dim,dynamic; /* dynamic set if memory allocated here */
313 
314  /* get vector dimension */
315  if ( vec != (VEC *)NULL && vec->dim<MAXDIM )
316  { dim = vec->dim; dynamic = FALSE; }
317  else
318  {
319  dynamic = TRUE;
320  do
321  {
322  fprintf(stderr,"Vector: dim: ");
323  if ( fgets(line,MAXLINE,fp)==NULL )
324  error(E_INPUT,"ifin_vec");
325  } while ( sscanf(line,"%u",&dim)<1 || dim>MAXDIM );
326  vec = v_get(dim);
327  }
328 
329  /* input elements */
330  for ( i=0; i<dim; i++ )
331  do
332  {
333  redo:
334  fprintf(stderr,"entry %u: ",i);
335  if ( !dynamic )
336  fprintf(stderr,"old %14.9g new: ",vec->ve[i]);
337  if ( fgets(line,MAXLINE,fp)==NULL )
338  error(E_INPUT,"ifin_vec");
339  if ( (*line == 'b' || *line == 'B') && i > 0 )
340  { i--; dynamic = FALSE; goto redo; }
341  if ( (*line == 'f' || *line == 'F') && i < dim-1 )
342  { i++; dynamic = FALSE; goto redo; }
343 #if REAL == DOUBLE
344  } while ( *line=='\0' || sscanf(line,"%lf",&vec->ve[i]) < 1 );
345 #elif REAL == FLOAT
346  } while ( *line=='\0' || sscanf(line,"%f",&vec->ve[i]) < 1 );
347 #endif
348 
349  return (vec);
350 }
351 
352 /* bfin_vec -- batch-file input of vector */
354 FILE *fp;
355 VEC *vec;
356 {
357  u_int i,dim;
358  int io_code;
359 
360  /* get dimension */
361  skipjunk(fp);
362  if ((io_code=fscanf(fp," Vector: dim:%u",&dim)) < 1 ||
363  dim>MAXDIM )
364  error(io_code==EOF ? 7 : 6,"bfin_vec");
365 
366  /* allocate memory if necessary */
367  if ( vec==(VEC *)NULL )
368  vec = v_resize(vec,dim);
369 
370  /* get entries */
371  skipjunk(fp);
372  for ( i=0; i<dim; i++ )
373 #if REAL == DOUBLE
374  if ((io_code=fscanf(fp,"%lf",&vec->ve[i])) < 1 )
375 #elif REAL == FLOAT
376  if ((io_code=fscanf(fp,"%f",&vec->ve[i])) < 1 )
377 #endif
378  error(io_code==EOF ? 7 : 6,"bfin_vec");
379 
380  return (vec);
381 }
382 
383 /**************************************************************************
384  Output routines
385  **************************************************************************/
386 static char *format = "%14.9g ";
387 
388 char *setformat(f_string)
389 char *f_string;
390 {
391  char *old_f_string;
392  old_f_string = format;
393  if ( f_string != (char *)NULL && *f_string != '\0' )
394  format = f_string;
395 
396  return old_f_string;
397 }
398 
399 void m_foutput(fp,a)
400 FILE *fp;
401 MAT *a;
402 {
403  u_int i, j, tmp;
404 
405  if ( a == (MAT *)NULL )
406  { fprintf(fp,"Matrix: NULL\n"); return; }
407  fprintf(fp,"Matrix: %d by %d\n",a->m,a->n);
408  if ( a->me == (Real **)NULL )
409  { fprintf(fp,"NULL\n"); return; }
410  for ( i=0; i<a->m; i++ ) /* for each row... */
411  {
412  fprintf(fp,"row %u: ",i);
413  for ( j=0, tmp=2; j<a->n; j++, tmp++ )
414  { /* for each col in row... */
415  fprintf(fp,format,a->me[i][j]);
416  if ( ! (tmp % 5) ) putc('\n',fp);
417  }
418  if ( tmp % 5 != 1 ) putc('\n',fp);
419  }
420 }
421 
422 void px_foutput(fp,px)
423 FILE *fp;
424 PERM *px;
425 {
426  u_int i;
427 
428  if ( px == (PERM *)NULL )
429  { fprintf(fp,"Permutation: NULL\n"); return; }
430  fprintf(fp,"Permutation: size: %u\n",px->size);
431  if ( px->pe == (u_int *)NULL )
432  { fprintf(fp,"NULL\n"); return; }
433  for ( i=0; i<px->size; i++ )
434  if ( ! (i % 8) && i != 0 )
435  fprintf(fp,"\n %u->%u ",i,px->pe[i]);
436  else
437  fprintf(fp,"%u->%u ",i,px->pe[i]);
438  fprintf(fp,"\n");
439 }
440 
441 void v_foutput(fp,x)
442 FILE *fp;
443 VEC *x;
444 {
445  u_int i, tmp;
446 
447  if ( x == (VEC *)NULL )
448  { fprintf(fp,"Vector: NULL\n"); return; }
449  fprintf(fp,"Vector: dim: %d\n",x->dim);
450  if ( x->ve == (Real *)NULL )
451  { fprintf(fp,"NULL\n"); return; }
452  for ( i=0, tmp=0; i<x->dim; i++, tmp++ )
453  {
454  fprintf(fp,format,x->ve[i]);
455  if ( tmp % 5 == 4 ) putc('\n',fp);
456  }
457  if ( tmp % 5 != 0 ) putc('\n',fp);
458 }
459 
460 
461 void m_dump(fp,a)
462 FILE *fp;
463 MAT *a;
464 {
465  u_int i, j, tmp;
466 
467  if ( a == (MAT *)NULL )
468  { fprintf(fp,"Matrix: NULL\n"); return; }
469  fprintf(fp,"Matrix: %d by %d @ 0x%p\n",a->m,a->n,a);
470  fprintf(fp,"\tmax_m = %d, max_n = %d, max_size = %d\n",
471  a->max_m, a->max_n, a->max_size);
472  if ( a->me == (Real **)NULL )
473  { fprintf(fp,"NULL\n"); return; }
474  fprintf(fp,"a->me @ 0x%p\n",(a->me));
475  fprintf(fp,"a->base @ 0x%p\n",(a->base));
476  for ( i=0; i<a->m; i++ ) /* for each row... */
477  {
478  fprintf(fp,"row %u: @ 0x%p ",i,(a->me[i]));
479  for ( j=0, tmp=2; j<a->n; j++, tmp++ )
480  { /* for each col in row... */
481  fprintf(fp,format,a->me[i][j]);
482  if ( ! (tmp % 5) ) putc('\n',fp);
483  }
484  if ( tmp % 5 != 1 ) putc('\n',fp);
485  }
486 }
487 
488 void px_dump(fp,px)
489 FILE *fp;
490 PERM *px;
491 {
492  u_int i;
493 
494  if ( ! px )
495  { fprintf(fp,"Permutation: NULL\n"); return; }
496  fprintf(fp,"Permutation: size: %u @ 0x%p\n",px->size,(px));
497  if ( ! px->pe )
498  { fprintf(fp,"NULL\n"); return; }
499  fprintf(fp,"px->pe @ 0x%p\n",(px->pe));
500  for ( i=0; i<px->size; i++ )
501  fprintf(fp,"%u->%u ",i,px->pe[i]);
502  fprintf(fp,"\n");
503 }
504 
505 
506 void v_dump(fp,x)
507 FILE *fp;
508 VEC *x;
509 {
510  u_int i, tmp;
511 
512  if ( ! x )
513  { fprintf(fp,"Vector: NULL\n"); return; }
514  fprintf(fp,"Vector: dim: %d @ 0x%p\n",x->dim,(x));
515  if ( ! x->ve )
516  { fprintf(fp,"NULL\n"); return; }
517  fprintf(fp,"x->ve @ 0x%p\n",(x->ve));
518  for ( i=0, tmp=0; i<x->dim; i++, tmp++ )
519  {
520  fprintf(fp,format,x->ve[i]);
521  if ( tmp % 5 == 4 ) putc('\n',fp);
522  }
523  if ( tmp % 5 != 0 ) putc('\n',fp);
524 }
525 
MAT * m_get(int, int)
Definition: memory.c:36
char * setformat(char *f_string)
Definition: matrixio.c:388
#define E_INPUT
Definition: err.h:101
#define Real
Definition: machine.h:189
static Object ** v_resize(void *v)
Definition: ivocvect.cpp:1297
MAT * m_finput(FILE *fp, MAT *a)
Definition: matrixio.c:73
#define DOUBLE
Definition: machine.h:166
#define REAL
Definition: machine.h:191
#define TRUE
Definition: err.c:57
void v_dump(FILE *fp, VEC *x)
Definition: matrixio.c:506
static double dummy
Definition: ocptrvector.cpp:27
u_int n
Definition: matrix.h:74
void px_dump(FILE *fp, PERM *px)
Definition: matrixio.c:488
void m_dump(FILE *fp, MAT *a)
Definition: matrixio.c:461
Real * ve
Definition: matrix.h:69
MAT * bm_finput(FILE *fp, MAT *mat)
Definition: matrixio.c:150
Definition: matrix.h:67
static Frame * fp
Definition: code.cpp:154
u_int size
Definition: matrix.h:88
uint32_t u_int
Definition: machine.h:38
int const size_t const size_t n
Definition: nrngsl.h:12
PERM * bpx_finput(FILE *fp, PERM *px)
Definition: matrixio.c:253
#define E_FORMAT
Definition: err.h:100
MAT * im_finput(FILE *fp, MAT *mat)
Definition: matrixio.c:86
PERM * px_resize(PERM *, int)
Definition: memory.c:399
u_int dim
Definition: matrix.h:68
void m_foutput(FILE *fp, MAT *a)
Definition: matrixio.c:399
VEC * ifin_vec(FILE *fp, VEC *vec)
Definition: matrixio.c:308
static char line[MAXLINE]
Definition: matrixio.c:39
#define E_BOUNDS
Definition: err.h:96
size_t j
fprintf(stderr, "Don't know the location of params at %p\, pp)
#define E_EOF
Definition: err.h:112
static char * format
Definition: matrixio.c:386
static double v_get(void *v)
Definition: ivocvect.cpp:1309
PERM * ipx_finput(FILE *fp, PERM *px)
Definition: matrixio.c:199
u_int max_m
Definition: matrix.h:75
VEC * v_finput(FILE *fp, VEC *x)
Definition: matrixio.c:295
void v_foutput(FILE *fp, VEC *x)
Definition: matrixio.c:441
u_int max_n
Definition: matrix.h:75
void px_foutput(FILE *fp, PERM *px)
Definition: matrixio.c:422
Real * base
Definition: matrix.h:76
static Object ** m_resize(void *v)
Definition: matrix.cpp:190
PERM * px_get(int)
#define FALSE
Definition: err.c:56
#define i
Definition: md1redef.h:12
#define c
static char rcsid[]
Definition: matrixio.c:35
#define error(err_num, fn_name)
Definition: err.h:73
u_int m
Definition: matrix.h:74
#define MAXDIM
Definition: matrix.h:218
PERM * px_finput(FILE *fp, PERM *px)
Definition: matrixio.c:185
#define MAXLINE
Definition: matrix.h:168
Definition: matrix.h:87
VEC * bfin_vec(FILE *fp, VEC *vec)
Definition: matrixio.c:353
return NULL
Definition: cabcode.cpp:461
Real ** me
Definition: matrix.h:76
Definition: matrix.h:73
u_int * pe
Definition: matrix.h:88
int skipjunk(FILE *fp)
Definition: matrixio.c:47
u_int max_size
Definition: matrix.h:75