zmatio.c 11 KB

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