36a1d8e9705331323b8742e890893311d8787cdb
5 // Check if array == refArray
6 void compareArray(const char* ID
, const void* array
, const void* refArray
, mwSize size
, int isInteger
)
8 Real EPS
= 1e-5; //precision
9 printf("Checking %s\n",ID
);
11 for (mwSize i
=0; i
<size
; i
++)
13 Real error
= isInteger
14 ? fabs(((Int
*)array
)[i
] - ((Int
*)refArray
)[i
])
15 : fabs(((Real
*)array
)[i
] - ((Real
*)refArray
)[i
]);
16 if (error
>= maxError
)
20 printf(" Inaccuracy: max(abs(error)) = %g >= %g\n",maxError
,EPS
);
25 // Next function is a generalization of :
26 //~ Real* brToMatlabArray(Real* brArray, int dimX, int dimY, int dimZ, int dimW)
28 //~ Real* matlabArray = (Real*)malloc(dimX*dimY*dimZ*dimW*sizeof(Real));
29 //~ for (int u=0; u<dimX*dimY*dimZ*dimW; u++)
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];
37 //~ return matlabArray;
40 // Auxiliary to convert from ours ("by-rows") encoding to MATLAB
41 void* brToMatlabArray(const void* brArray
, const mwSize
* dimensions
, int nbDims
, int isInteger
)
44 for (int i
=0; i
<nbDims
; i
++)
45 totalDim
*= dimensions
[i
];
46 size_t elementSize
= isInteger
50 void* matlabArray
= malloc(totalDim
*elementSize
);
51 for (mwSize u
=0; u
<totalDim
; u
++)
53 mwSize prodDimLeft
= totalDim
;
54 mwSize prodDimRight
= totalDim
/ dimensions
[0];
55 mwSize prodDimInIndex
= 1;
57 for (int v
=0; v
<nbDims
; v
++)
59 index
+= ((u
% prodDimLeft
) / prodDimRight
) * prodDimInIndex
;
60 prodDimInIndex
*= dimensions
[v
];
61 prodDimLeft
/= dimensions
[v
];
63 prodDimRight
/= dimensions
[v
+1];
66 ((Int
*)matlabArray
)[index
] = ((Int
*)brArray
)[u
];
68 ((Real
*)matlabArray
)[index
] = ((Real
*)brArray
)[u
];
73 // Next function is a generalization of :
74 //~ Real* matlabToBrArray(Real* matlabArray, int dimX, int dimY, int dimZ, int dimU)
76 //~ Real* brArray = (Real*)malloc(dimX*dimY*dimZ*dimU*sizeof(Real));
77 //~ for (int u=0; u<dimX*dimY*dimZ*dimU; u++)
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];
88 // Auxiliary to convert from MATLAB encoding to ours ("by-rows")
89 void* matlabToBrArray(const void* matlabArray
, const mwSize
* dimensions
, int nbDims
, int isInteger
)
92 for (int i
=0; i
<nbDims
; i
++)
93 totalDim
*= dimensions
[i
];
94 size_t elementSize
= isInteger
98 void* brArray
= malloc(totalDim
*elementSize
);
99 for (mwSize u
=0; u
<totalDim
; u
++)
101 mwSize prodDimLeft
= dimensions
[0];
102 mwSize prodDimRight
= 1;
103 mwSize prodDimInIndex
= totalDim
/ dimensions
[0];
105 for (int v
=0; v
<nbDims
; v
++)
107 index
+= ((u
% prodDimLeft
) / prodDimRight
) * prodDimInIndex
;
110 prodDimInIndex
/= dimensions
[v
+1];
111 prodDimLeft
*= dimensions
[v
+1];
113 prodDimRight
*= dimensions
[v
];
116 ((Int
*)brArray
)[index
] = ((Int
*)matlabArray
)[u
];
118 ((Real
*)brArray
)[index
] = ((Real
*)matlabArray
)[u
];
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
)
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");
134 for (int i
=0; i
<nbDims
; i
++)
135 totalDim
*= dimensions
[i
];
136 size_t elementSize
= isInteger
140 // read all values, and convert them to by-rows matrices format
141 void* matlabArray
= malloc(totalDim
*elementSize
);
144 for (mwSize u
=0; u
<totalDim
; u
++)
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)
151 while (!feof(file
) && curChar
!=' ' && curChar
!='\n' && curChar
!='\t' && curChar
!=',')
153 bufferNum
[bufferIndex
++] = curChar
;
154 curChar
= fgetc(file
);
156 bufferNum
[bufferIndex
] = 0;
157 // transform string into Real, and store it at appropriate location
159 ((Int
*)matlabArray
)[u
] = atoi(bufferNum
);
161 ((Real
*)matlabArray
)[u
] = atof(bufferNum
);
165 void* brArray
= matlabToBrArray(matlabArray
, dimensions
, nbDims
, isInteger
);