| 1 | #ifndef __MATIO_H |
|---|
| 2 | #define __MATIO_H |
|---|
| 3 | #include <stdio.h> |
|---|
| 4 | #include <stdlib.h> |
|---|
| 5 | #include <cstring> |
|---|
| 6 | // Reading and writing matrices over double |
|---|
| 7 | |
|---|
| 8 | // Reading a matrice from a (eventually zipped) file |
|---|
| 9 | double * read_dbl(char * mat_file,int* tni,int* tnj) { |
|---|
| 10 | char *UT; |
|---|
| 11 | int is_gzipped = 0; |
|---|
| 12 | size_t s = strlen(mat_file); |
|---|
| 13 | double* X=NULL; |
|---|
| 14 | const char * File_Name = mat_file; |
|---|
| 15 | |
|---|
| 16 | FILE* FileDes = fopen(File_Name, "r"); |
|---|
| 17 | if (FileDes != NULL) { |
|---|
| 18 | char * tmp = new char[200];// usigned long tni, tnj; |
|---|
| 19 | fscanf(FileDes,"%d %d %s\n",tni, tnj, tmp) ; |
|---|
| 20 | int n=*tni; |
|---|
| 21 | int p=*tnj; |
|---|
| 22 | X = new double[n*p]; |
|---|
| 23 | for (int i=0;i<n*p;++i) |
|---|
| 24 | X[i] = (double) 0; |
|---|
| 25 | long i,j; long val; |
|---|
| 26 | fscanf(FileDes,"%ld %ld %ld\n",&i, &j, &val) ; |
|---|
| 27 | while(i && j) { |
|---|
| 28 | X[p*(i-1)+j-1] = (double) val; |
|---|
| 29 | fscanf(FileDes,"%ld %ld %ld\n",&i, &j, &val) ; |
|---|
| 30 | } |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | fclose(FileDes); |
|---|
| 34 | return X; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | // Displays a matrix |
|---|
| 38 | std::ostream& write_dbl(std::ostream& c, |
|---|
| 39 | double* E, |
|---|
| 40 | int n, int m, int id){ |
|---|
| 41 | |
|---|
| 42 | for (int i = 0; i<n;++i){ |
|---|
| 43 | for (int j=0; j<m;++j) |
|---|
| 44 | c << *(E+j+id*i) << " "; |
|---|
| 45 | c << std::endl; |
|---|
| 46 | } |
|---|
| 47 | return c << std::endl; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | // Reading and writing matrices over field |
|---|
| 51 | |
|---|
| 52 | // Reading a matrice from a (eventually zipped) file |
|---|
| 53 | template<class Field> |
|---|
| 54 | typename Field::Element * read_field(const Field& F,char * mat_file,int* tni,int* tnj) |
|---|
| 55 | { |
|---|
| 56 | char *UT; |
|---|
| 57 | int is_gzipped = 0; |
|---|
| 58 | size_t s = strlen(mat_file); |
|---|
| 59 | typename Field::Element zero; |
|---|
| 60 | F.init(zero,0.0); |
|---|
| 61 | typename Field::Element * X=NULL; |
|---|
| 62 | const char * File_Name = mat_file; |
|---|
| 63 | FILE* FileDes = fopen(File_Name, "r"); |
|---|
| 64 | if (FileDes != NULL) { |
|---|
| 65 | char tmp[200];// usigned long tni, tnj; |
|---|
| 66 | fscanf(FileDes,"%d %d %s\n",tni, tnj, tmp) ; |
|---|
| 67 | |
|---|
| 68 | int n=*tni; |
|---|
| 69 | int p=*tnj; |
|---|
| 70 | X = new typename Field::Element[n*p]; |
|---|
| 71 | for (int i=0;i<n*p;++i) |
|---|
| 72 | X[i] = zero; |
|---|
| 73 | long i,j; long val; |
|---|
| 74 | fscanf(FileDes,"%ld %ld %ld\n",&i, &j, &val) ; |
|---|
| 75 | while(i && j) { |
|---|
| 76 | F.init(X[p*(i-1)+j-1],double(val)); |
|---|
| 77 | fscanf(FileDes,"%ld %ld %ld\n",&i, &j, &val) ; |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | fclose(FileDes); |
|---|
| 82 | return X; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | // Displays a matrix |
|---|
| 86 | template<class Field> |
|---|
| 87 | std::ostream& write_field(const Field& F,std::ostream& c, |
|---|
| 88 | const typename Field::Element* E, |
|---|
| 89 | int n, int m, int id){ |
|---|
| 90 | |
|---|
| 91 | typename Field::Element tmp; |
|---|
| 92 | //#if DEBUG |
|---|
| 93 | for (int i = 0; i<n;++i){ |
|---|
| 94 | for (int j=0; j<m;++j){ |
|---|
| 95 | F.convert(tmp,*(E+j+id*i)); |
|---|
| 96 | c << tmp << " ";} |
|---|
| 97 | c << std::endl; |
|---|
| 98 | } |
|---|
| 99 | return c << std::endl; |
|---|
| 100 | //#endif |
|---|
| 101 | } |
|---|
| 102 | #endif |
|---|