zmatlab.c 6.0 KB

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