home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / mesch12a.zip / zmatlab.c < prev    next >
C/C++ Source or Header  |  1994-01-13  |  6KB  |  215 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. /*
  28.     This file contains routines for import/exporting complex data
  29.     to/from MATLAB. The main routines are:
  30.             ZMAT *zm_save(FILE *fp,ZMAT *A,char *name)
  31.             ZVEC *zv_save(FILE *fp,ZVEC *x,char *name)
  32.             complex z_save(FILE *fp,complex z,char *name)
  33.             ZMAT *zm_load(FILE *fp,char **name)
  34. */
  35.  
  36. #include        <stdio.h>
  37. #include        "zmatrix.h"
  38. #include    "matlab.h"
  39.  
  40. static char rcsid[] = "$Id: zmatlab.c,v 1.1 1994/01/13 04:24:57 des Exp $";
  41.  
  42. /* zm_save -- save matrix in ".mat" file for MATLAB
  43.    -- returns matrix to be saved */
  44. ZMAT    *zm_save(fp,A,name)
  45. FILE    *fp;
  46. ZMAT    *A;
  47. char    *name;
  48. {
  49.     int     i, j;
  50.     matlab  mat;
  51.     
  52.     if ( ! A )
  53.     error(E_NULL,"zm_save");
  54.     
  55.     mat.type = 1000*MACH_ID + 100*ORDER + 10*PRECISION + 0;
  56.     mat.m = A->m;
  57.     mat.n = A->n;
  58.     mat.imag = TRUE;
  59.     mat.namlen = (name == (char *)NULL) ? 1 : strlen(name)+1;
  60.     
  61.     /* write header */
  62.     fwrite(&mat,sizeof(matlab),1,fp);
  63.     /* write name */
  64.     if ( name == (char *)NULL )
  65.     fwrite("",sizeof(char),1,fp);
  66.     else
  67.     fwrite(name,sizeof(char),(int)(mat.namlen),fp);
  68.     /* write actual data */
  69.     for ( i = 0; i < A->m; i++ )
  70.     for ( j = 0; j < A->n; j++ )
  71.         fwrite(&(A->me[i][j].re),sizeof(Real),1,fp);
  72.     for ( i = 0; i < A->m; i++ )
  73.     for ( j = 0; j < A->n; j++ )
  74.         fwrite(&(A->me[i][j].im),sizeof(Real),1,fp);
  75.     
  76.     return A;
  77. }
  78.  
  79.  
  80. /* zv_save -- save vector in ".mat" file for MATLAB
  81.    -- saves it as a row vector
  82.    -- returns vector to be saved */
  83. ZVEC    *zv_save(fp,x,name)
  84. FILE    *fp;
  85. ZVEC    *x;
  86. char    *name;
  87. {
  88.     int    i;
  89.     matlab  mat;
  90.     
  91.     if ( ! x )
  92.     error(E_NULL,"zv_save");
  93.     
  94.     mat.type = 1000*MACH_ID + 100*ORDER + 10*PRECISION + 0;
  95.     mat.m = x->dim;
  96.     mat.n = 1;
  97.     mat.imag = TRUE;
  98.     mat.namlen = (name == (char *)NULL) ? 1 : strlen(name)+1;
  99.     
  100.     /* write header */
  101.     fwrite(&mat,sizeof(matlab),1,fp);
  102.     /* write name */
  103.     if ( name == (char *)NULL )
  104.     fwrite("",sizeof(char),1,fp);
  105.     else
  106.     fwrite(name,sizeof(char),(int)(mat.namlen),fp);
  107.     /* write actual data */
  108.     for ( i = 0; i < x->dim; i++ )
  109.     fwrite(&(x->ve[i].re),sizeof(Real),1,fp);
  110.     for ( i = 0; i < x->dim; i++ )
  111.     fwrite(&(x->ve[i].im),sizeof(Real),1,fp);
  112.     
  113.     return x;
  114. }
  115.  
  116. /* z_save -- saves complex number in ".mat" file for MATLAB
  117.     -- returns complex number to be saved */
  118. complex    z_save(fp,z,name)
  119. FILE    *fp;
  120. complex    z;
  121. char    *name;
  122. {
  123.     matlab  mat;
  124.     
  125.     mat.type = 1000*MACH_ID + 100*ORDER + 10*PRECISION + 0;
  126.     mat.m = 1;
  127.     mat.n = 1;
  128.     mat.imag = TRUE;
  129.     mat.namlen = (name == (char *)NULL) ? 1 : strlen(name)+1;
  130.     
  131.     /* write header */
  132.     fwrite(&mat,sizeof(matlab),1,fp);
  133.     /* write name */
  134.     if ( name == (char *)NULL )
  135.     fwrite("",sizeof(char),1,fp);
  136.     else
  137.     fwrite(name,sizeof(char),(int)(mat.namlen),fp);
  138.     /* write actual data */
  139.     fwrite(&z,sizeof(complex),1,fp);
  140.     
  141.     return z;
  142. }
  143.  
  144.  
  145.  
  146. /* zm_load -- loads in a ".mat" file variable as produced by MATLAB
  147.    -- matrix returned; imaginary parts ignored */
  148. ZMAT    *zm_load(fp,name)
  149. FILE    *fp;
  150. char    **name;
  151. {
  152.     ZMAT     *A;
  153.     int     i;
  154.     int     m_flag, o_flag, p_flag, t_flag;
  155.     float   f_temp;
  156.     double  d_temp;
  157.     matlab  mat;
  158.     
  159.     if ( fread(&mat,sizeof(matlab),1,fp) != 1 )
  160.     error(E_FORMAT,"zm_load");
  161.     if ( mat.type >= 10000 )    /* don't load a sparse matrix! */
  162.     error(E_FORMAT,"zm_load");
  163.     m_flag = (mat.type/1000) % 10;
  164.     o_flag = (mat.type/100) % 10;
  165.     p_flag = (mat.type/10) % 10;
  166.     t_flag = (mat.type) % 10;
  167.     if ( m_flag != MACH_ID )
  168.     error(E_FORMAT,"zm_load");
  169.     if ( t_flag != 0 )
  170.     error(E_FORMAT,"zm_load");
  171.     if ( p_flag != DOUBLE_PREC && p_flag != SINGLE_PREC )
  172.     error(E_FORMAT,"zm_load");
  173.     *name = (char *)malloc((unsigned)(mat.namlen)+1);
  174.     if ( fread(*name,sizeof(char),(unsigned)(mat.namlen),fp) == 0 )
  175.     error(E_FORMAT,"zm_load");
  176.     A = zm_get((unsigned)(mat.m),(unsigned)(mat.n));
  177.     for ( i = 0; i < A->m*A->n; i++ )
  178.     {
  179.     if ( p_flag == DOUBLE_PREC )
  180.         fread(&d_temp,sizeof(double),1,fp);
  181.     else
  182.     {
  183.         fread(&f_temp,sizeof(float),1,fp);
  184.         d_temp = f_temp;
  185.     }
  186.     if ( o_flag == ROW_ORDER )
  187.         A->me[i / A->n][i % A->n].re = d_temp;
  188.     else if ( o_flag == COL_ORDER )
  189.         A->me[i % A->m][i / A->m].re = d_temp;
  190.     else
  191.         error(E_FORMAT,"zm_load");
  192.     }
  193.     
  194.     if ( mat.imag )         /* skip imaginary part */
  195.     for ( i = 0; i < A->m*A->n; i++ )
  196.     {
  197.         if ( p_flag == DOUBLE_PREC )
  198.         fread(&d_temp,sizeof(double),1,fp);
  199.         else
  200.         {
  201.         fread(&f_temp,sizeof(float),1,fp);
  202.         d_temp = f_temp;
  203.         }
  204.         if ( o_flag == ROW_ORDER )
  205.         A->me[i / A->n][i % A->n].im = d_temp;
  206.         else if ( o_flag == COL_ORDER )
  207.         A->me[i % A->m][i / A->m].im = d_temp;
  208.         else
  209.         error(E_FORMAT,"zm_load");
  210.     }
  211.     
  212.     return A;
  213. }
  214.  
  215.