Commit | Line | Data |
---|---|---|
1d3c1faa BA |
1 | #include "ioutils.h" |
2 | #include <string.h> | |
3 | #include <stdio.h> | |
4 | ||
5 | // Check if array == refArray | |
6 | void compareArray(const char* ID, const void* array, const void* refArray, mwSize size, int isInteger) | |
7 | { | |
8 | Real EPS = 1e-5; //precision | |
9 | printf("Checking %s\n",ID); | |
10 | Real maxError = 0.0; | |
11 | for (mwSize i=0; i<size; i++) | |
12 | { | |
13 | Real error = isInteger | |
14 | ? fabs(((Int*)array)[i] - ((Int*)refArray)[i]) | |
15 | : fabs(((Real*)array)[i] - ((Real*)refArray)[i]); | |
16 | if (error >= maxError) | |
17 | maxError = error; | |
18 | } | |
19 | if (maxError >= EPS) | |
20 | printf(" Inaccuracy: max(abs(error)) = %g >= %g\n",maxError,EPS); | |
21 | else | |
22 | printf(" OK\n"); | |
23 | } | |
24 | ||
25 | // Next function is a generalization of : | |
26 | //~ Real* brToMatlabArray(Real* brArray, int dimX, int dimY, int dimZ, int dimW) | |
27 | //~ { | |
28 | //~ Real* matlabArray = (Real*)malloc(dimX*dimY*dimZ*dimW*sizeof(Real)); | |
29 | //~ for (int u=0; u<dimX*dimY*dimZ*dimW; u++) | |
30 | //~ { | |
31 | //~ int xIndex = u / (dimY*dimZ*dimW); | |
32 | //~ int yIndex = (u % (dimY*dimZ*dimW)) / (dimZ*dimW); | |
33 | //~ int zIndex = (u % (dimZ*dimW)) / dimW; | |
34 | //~ int wIndex = u % dimW; | |
35 | //~ matlabArray[xIndex+yIndex*dimX+zIndex*dimX*dimY+wIndex*dimX*dimY*dimZ] = brArray[u]; | |
36 | //~ } | |
37 | //~ return matlabArray; | |
38 | //~ } | |
39 | ||
40 | // Auxiliary to convert from ours ("by-rows") encoding to MATLAB | |
41 | void* brToMatlabArray(const void* brArray, const mwSize* dimensions, int nbDims, int isInteger) | |
42 | { | |
43 | mwSize totalDim = 1; | |
44 | for (int i=0; i<nbDims; i++) | |
45 | totalDim *= dimensions[i]; | |
46 | size_t elementSize = isInteger | |
47 | ? sizeof(Int) | |
48 | : sizeof(Real); | |
49 | ||
50 | void* matlabArray = malloc(totalDim*elementSize); | |
51 | for (mwSize u=0; u<totalDim; u++) | |
52 | { | |
53 | mwSize prodDimLeft = totalDim; | |
54 | mwSize prodDimRight = totalDim / dimensions[0]; | |
55 | mwSize prodDimInIndex = 1; | |
56 | mwSize index = 0; | |
57 | for (int v=0; v<nbDims; v++) | |
58 | { | |
59 | index += ((u % prodDimLeft) / prodDimRight) * prodDimInIndex; | |
60 | prodDimInIndex *= dimensions[v]; | |
61 | prodDimLeft /= dimensions[v]; | |
62 | if (v+1 < nbDims) | |
63 | prodDimRight /= dimensions[v+1]; | |
64 | } | |
65 | if (isInteger) | |
66 | ((Int*)matlabArray)[index] = ((Int*)brArray)[u]; | |
67 | else | |
68 | ((Real*)matlabArray)[index] = ((Real*)brArray)[u]; | |
69 | } | |
70 | return matlabArray; | |
71 | } | |
72 | ||
73 | // Next function is a generalization of : | |
74 | //~ Real* matlabToBrArray(Real* matlabArray, int dimX, int dimY, int dimZ, int dimU) | |
75 | //~ { | |
76 | //~ Real* brArray = (Real*)malloc(dimX*dimY*dimZ*dimU*sizeof(Real)); | |
77 | //~ for (int u=0; u<dimX*dimY*dimZ*dimU; u++) | |
78 | //~ { | |
79 | //~ int xIndex = u % dimX; | |
80 | //~ int yIndex = (u % (dimX*dimY)) / dimX; | |
81 | //~ int zIndex = (u % (dimX*dimY*dimZ)) / (dimX*dimY); | |
82 | //~ int uIndex = u / (dimX*dimY*dimZ); | |
83 | //~ brArray[xIndex*dimY*dimZ*dimU+yIndex*dimZ*dimU+zIndex*dimU+uIndex] = matlabArray[u]; | |
84 | //~ } | |
85 | //~ return brArray; | |
86 | //~ } | |
87 | ||
88 | // Auxiliary to convert from MATLAB encoding to ours ("by-rows") | |
89 | void* matlabToBrArray(const void* matlabArray, const mwSize* dimensions, int nbDims, int isInteger) | |
90 | { | |
91 | mwSize totalDim = 1; | |
92 | for (int i=0; i<nbDims; i++) | |
93 | totalDim *= dimensions[i]; | |
94 | size_t elementSize = isInteger | |
95 | ? sizeof(Int) | |
96 | : sizeof(Real); | |
97 | ||
98 | void* brArray = malloc(totalDim*elementSize); | |
99 | for (mwSize u=0; u<totalDim; u++) | |
100 | { | |
101 | mwSize prodDimLeft = dimensions[0]; | |
102 | mwSize prodDimRight = 1; | |
103 | mwSize prodDimInIndex = totalDim / dimensions[0]; | |
104 | mwSize index = 0; | |
105 | for (int v=0; v<nbDims; v++) | |
106 | { | |
107 | index += ((u % prodDimLeft) / prodDimRight) * prodDimInIndex; | |
108 | if (v+1 < nbDims) | |
109 | { | |
110 | prodDimInIndex /= dimensions[v+1]; | |
111 | prodDimLeft *= dimensions[v+1]; | |
112 | } | |
113 | prodDimRight *= dimensions[v]; | |
114 | } | |
115 | if (isInteger) | |
116 | ((Int*)brArray)[index] = ((Int*)matlabArray)[u]; | |
117 | else | |
118 | ((Real*)brArray)[index] = ((Real*)matlabArray)[u]; | |
119 | } | |
120 | return brArray; | |
121 | } | |
122 | ||
123 | // Read array by columns (as in MATLAB) and return by-rows encoding | |
124 | void* readArray(const char* fileName, const mwSize* dimensions, int nbDims, int isInteger) | |
125 | { | |
126 | // need to prepend '../data/' (not really nice code...) | |
127 | char* fullFileName = (char*)calloc(8+strlen(fileName)+1,sizeof(char)); | |
128 | strcat(fullFileName, "../data/"); | |
129 | strcat(fullFileName, fileName); | |
130 | FILE* file = fopen(fullFileName, "r"); | |
131 | free(fullFileName); | |
132 | ||
133 | mwSize totalDim = 1; | |
134 | for (int i=0; i<nbDims; i++) | |
135 | totalDim *= dimensions[i]; | |
136 | size_t elementSize = isInteger | |
137 | ? sizeof(Int) | |
138 | : sizeof(Real); | |
139 | ||
140 | // read all values, and convert them to by-rows matrices format | |
141 | void* matlabArray = malloc(totalDim*elementSize); | |
142 | char curChar = ' '; | |
143 | char bufferNum[64]; | |
144 | for (mwSize u=0; u<totalDim; u++) | |
145 | { | |
146 | // position to next non-separator character | |
147 | while (!feof(file) && (curChar==' ' || curChar=='\n' || curChar=='\t' || curChar==',')) | |
148 | curChar = fgetc(file); | |
149 | // read number (as a string) | |
150 | int bufferIndex = 0; | |
151 | while (!feof(file) && curChar!=' ' && curChar!='\n' && curChar!='\t' && curChar!=',') | |
152 | { | |
153 | bufferNum[bufferIndex++] = curChar; | |
154 | curChar = fgetc(file); | |
155 | } | |
156 | bufferNum[bufferIndex] = 0; | |
157 | // transform string into Real, and store it at appropriate location | |
158 | if (isInteger) | |
159 | ((Int*)matlabArray)[u] = atoi(bufferNum); | |
160 | else | |
161 | ((Real*)matlabArray)[u] = atof(bufferNum); | |
162 | } | |
163 | fclose(file); | |
164 | ||
165 | void* brArray = matlabToBrArray(matlabArray, dimensions, nbDims, isInteger); | |
166 | free(matlabArray); | |
167 | return brArray; | |
168 | } |