/[escript]/branches/windows_from_1456_trunk_1620_merged_in/paso/src/mmio.c
ViewVC logotype

Diff of /branches/windows_from_1456_trunk_1620_merged_in/paso/src/mmio.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1625 by phornby, Wed Jul 2 06:56:57 2008 UTC revision 1626 by phornby, Fri Jul 11 08:41:07 2008 UTC
# Line 37  int mm_read_unsymmetric_sparse(const cha Line 37  int mm_read_unsymmetric_sparse(const cha
37      int M, N, nz;      int M, N, nz;
38      int i;      int i;
39      double *val;      double *val;
40      int *I, *J;      int *Ip, *Jp;
41    
42      if ((f = fopen(fname, "r")) == NULL)      if ((f = fopen(fname, "r")) == NULL)
43              return -1;              return -1;
# Line 75  int mm_read_unsymmetric_sparse(const cha Line 75  int mm_read_unsymmetric_sparse(const cha
75    
76      /* reseve memory for matrices */      /* reseve memory for matrices */
77    
78      I = MEMALLOC(nz, int);      Ip = MEMALLOC(nz, int);
79      J = MEMALLOC(nz, int);      Jp = MEMALLOC(nz, int);
80      val = MEMALLOC(nz, double);      val = MEMALLOC(nz, double);
81    
82      *val_ = val;      *val_ = val;
83      *I_ = I;      *I_ = Ip;
84      *J_ = J;      *J_ = Jp;
85    
86      /* NOTE: when reading in doubles, ANSI C requires the use of the "l"  */      /* NOTE: when reading in doubles, ANSI C requires the use of the "l"  */
87      /*   specifier as in "%lg", "%lf", "%le", otherwise errors will occur */      /*   specifier as in "%lg", "%lf", "%le", otherwise errors will occur */
# Line 89  int mm_read_unsymmetric_sparse(const cha Line 89  int mm_read_unsymmetric_sparse(const cha
89    
90      for (i=0; i<nz; i++)      for (i=0; i<nz; i++)
91      {      {
92          fscanf(f, "%d %d %lg\n", &I[i], &J[i], &val[i]);          fscanf(f, "%d %d %lg\n", &Ip[i], &Jp[i], &val[i]);
93          I[i]--;  /* adjust from 1-based to 0-based */          Ip[i]--;  /* adjust from 1-based to 0-based */
94          J[i]--;          Jp[i]--;
95      }      }
96      fclose(f);      fclose(f);
97    
# Line 275  int mm_write_mtx_array_size(FILE *f, int Line 275  int mm_write_mtx_array_size(FILE *f, int
275  /*-------------------------------------------------------------------------*/  /*-------------------------------------------------------------------------*/
276    
277  /******************************************************************/  /******************************************************************/
278  /* use when I[], J[], and val[]J, and val[] are already allocated */  /* use when Ip[], Jp[], and val[] are already allocated           */
279  /******************************************************************/  /******************************************************************/
280    
281  int mm_read_mtx_crd_data(FILE *f, int M, int N, int nz, int I[], int J[],  int mm_read_mtx_crd_data(FILE *f, int M, int N, int nz, int Ip[], int Jp[],
282          double val[], MM_typecode matcode)          double val[], MM_typecode matcode)
283  {  {
284      int i;      int i;
285      if (mm_is_complex(matcode))      if (mm_is_complex(matcode))
286      {      {
287          for (i=0; i<nz; i++)          for (i=0; i<nz; i++)
288              if (fscanf(f, "%d %d %lg %lg", &I[i], &J[i], &val[2*i], &val[2*i+1])              if (fscanf(f, "%d %d %lg %lg", &Ip[i], &Jp[i], &val[2*i], &val[2*i+1])
289                  != 4) return MM_PREMATURE_EOF;                  != 4) return MM_PREMATURE_EOF;
290      }      }
291      else if (mm_is_real(matcode))      else if (mm_is_real(matcode))
292      {      {
293          for (i=0; i<nz; i++)          for (i=0; i<nz; i++)
294          {          {
295              if (fscanf(f, "%d %d %lg\n", &I[i], &J[i], &val[i])              if (fscanf(f, "%d %d %lg\n", &Ip[i], &Jp[i], &val[i])
296                  != 3) return MM_PREMATURE_EOF;                  != 3) return MM_PREMATURE_EOF;
297    
298          }          }
# Line 301  int mm_read_mtx_crd_data(FILE *f, int M, Line 301  int mm_read_mtx_crd_data(FILE *f, int M,
301      else if (mm_is_pattern(matcode))      else if (mm_is_pattern(matcode))
302      {      {
303          for (i=0; i<nz; i++)          for (i=0; i<nz; i++)
304              if (fscanf(f, "%d %d", &I[i], &J[i])              if (fscanf(f, "%d %d", &Ip[i], &Jp[i])
305                  != 2) return MM_PREMATURE_EOF;                  != 2) return MM_PREMATURE_EOF;
306      }      }
307      else      else
# Line 311  int mm_read_mtx_crd_data(FILE *f, int M, Line 311  int mm_read_mtx_crd_data(FILE *f, int M,
311    
312  }  }
313    
314  int mm_read_mtx_crd_entry(FILE *f, int *I, int *J,  int mm_read_mtx_crd_entry(FILE *f, int *Ip, int *Jp,
315          double *real, double *imag, MM_typecode matcode)          double *real, double *imag, MM_typecode matcode)
316  {  {
317      if (mm_is_complex(matcode))      if (mm_is_complex(matcode))
318      {      {
319              if (fscanf(f, "%d %d %lg %lg", I, J, real, imag)              if (fscanf(f, "%d %d %lg %lg", Ip, Jp, real, imag)
320                  != 4) return MM_PREMATURE_EOF;                  != 4) return MM_PREMATURE_EOF;
321      }      }
322      else if (mm_is_real(matcode))      else if (mm_is_real(matcode))
323      {      {
324              if (fscanf(f, "%d %d %lg\n", I, J, real)              if (fscanf(f, "%d %d %lg\n", Ip, Jp, real)
325                  != 3) return MM_PREMATURE_EOF;                  != 3) return MM_PREMATURE_EOF;
326    
327      }      }
328    
329      else if (mm_is_pattern(matcode))      else if (mm_is_pattern(matcode))
330      {      {
331              if (fscanf(f, "%d %d", I, J) != 2) return MM_PREMATURE_EOF;              if (fscanf(f, "%d %d", Ip, Jp) != 2) return MM_PREMATURE_EOF;
332      }      }
333      else      else
334          return MM_UNSUPPORTED_TYPE;          return MM_UNSUPPORTED_TYPE;
# Line 346  int mm_read_mtx_crd_entry(FILE *f, int * Line 346  int mm_read_mtx_crd_entry(FILE *f, int *
346                              (nz pairs of real/imaginary values)                              (nz pairs of real/imaginary values)
347  ************************************************************************/  ************************************************************************/
348    
349  int mm_read_mtx_crd(char *fname, int *M, int *N, int *nz, int **I, int **J,  int mm_read_mtx_crd(char *fname, int *M, int *N, int *nz, int **Ip, int **Jp,
350          double **val, MM_typecode *matcode)          double **val, MM_typecode *matcode)
351  {  {
352      int ret_code;      int ret_code;
# Line 369  int mm_read_mtx_crd(char *fname, int *M, Line 369  int mm_read_mtx_crd(char *fname, int *M,
369          return ret_code;          return ret_code;
370    
371    
372      *I = MEMALLOC(*nz, int);      *Ip = MEMALLOC(*nz, int);
373      *J = MEMALLOC(*nz, int);      *Jp = MEMALLOC(*nz, int);
374      *val = NULL;      *val = NULL;
375    
376      if (mm_is_complex(*matcode))      if (mm_is_complex(*matcode))
377      {      {
378          *val = MEMALLOC(*nz * 2, double);          *val = MEMALLOC(*nz * 2, double);
379          ret_code = mm_read_mtx_crd_data(f, *M, *N, *nz, *I, *J, *val,          ret_code = mm_read_mtx_crd_data(f, *M, *N, *nz, *Ip, *Jp, *val,
380                  *matcode);                  *matcode);
381          if (ret_code != 0) return ret_code;          if (ret_code != 0) return ret_code;
382      }      }
383      else if (mm_is_real(*matcode))      else if (mm_is_real(*matcode))
384      {      {
385          *val = MEMALLOC(*nz, double);          *val = MEMALLOC(*nz, double);
386          ret_code = mm_read_mtx_crd_data(f, *M, *N, *nz, *I, *J, *val,          ret_code = mm_read_mtx_crd_data(f, *M, *N, *nz, *Ip, *Jp, *val,
387                  *matcode);                  *matcode);
388          if (ret_code != 0) return ret_code;          if (ret_code != 0) return ret_code;
389      }      }
390    
391      else if (mm_is_pattern(*matcode))      else if (mm_is_pattern(*matcode))
392      {      {
393          ret_code = mm_read_mtx_crd_data(f, *M, *N, *nz, *I, *J, *val,          ret_code = mm_read_mtx_crd_data(f, *M, *N, *nz, *Ip, *Jp, *val,
394                  *matcode);                  *matcode);
395          if (ret_code != 0) return ret_code;          if (ret_code != 0) return ret_code;
396      }      }
# Line 408  int mm_write_banner(FILE *f, MM_typecode Line 408  int mm_write_banner(FILE *f, MM_typecode
408          return 0;          return 0;
409  }  }
410    
411  int mm_write_mtx_crd(char fname[], int M, int N, int nz, int I[], int J[],  int mm_write_mtx_crd(char fname[], int M, int N, int nz, int Ip[], int Jp[],
412          double val[], MM_typecode matcode)          double val[], MM_typecode matcode)
413  {  {
414      FILE *f;      FILE *f;
# Line 430  int mm_write_mtx_crd(char fname[], int M Line 430  int mm_write_mtx_crd(char fname[], int M
430      /* print values */      /* print values */
431      if (mm_is_pattern(matcode))      if (mm_is_pattern(matcode))
432          for (i=0; i<nz; i++)          for (i=0; i<nz; i++)
433              fprintf(f, "%d %d\n", I[i], J[i]);              fprintf(f, "%d %d\n", Ip[i], Jp[i]);
434      else      else
435      if (mm_is_real(matcode))      if (mm_is_real(matcode))
436          for (i=0; i<nz; i++)          for (i=0; i<nz; i++)
437              fprintf(f, "%d %d %20.16g\n", I[i], J[i], val[i]);              fprintf(f, "%d %d %20.16g\n", Ip[i], Jp[i], val[i]);
438      else      else
439      if (mm_is_complex(matcode))      if (mm_is_complex(matcode))
440          for (i=0; i<nz; i++)          for (i=0; i<nz; i++)
441              fprintf(f, "%d %d %20.16g %20.16g\n", I[i], J[i], val[2*i],              fprintf(f, "%d %d %20.16g %20.16g\n", Ip[i], Jp[i], val[2*i],
442                          val[2*i+1]);                          val[2*i+1]);
443      else      else
444      {      {

Legend:
Removed from v.1625  
changed lines
  Added in v.1626

  ViewVC Help
Powered by ViewVC 1.1.26