PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mesch/matrixio.c

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