home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / mesch12a.zip / matrixio.c < prev    next >
C/C++ Source or Header  |  1994-01-13  |  14KB  |  523 lines

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