1 // Check if array == refArray
2 void compareArray(const char* ID
, const void* array
, const void* refArray
, int size
,
5 Real EPS
= 1e-5; //precision
6 printf("Checking %s\n",ID
);
8 for (mwSize i
=0; i
<size
; i
++)
10 Real error
= isInteger
11 ? fabs(((Int
*)array
)[i
] - ((Int
*)refArray
)[i
])
12 : fabs(((Real
*)array
)[i
] - ((Real
*)refArray
)[i
]);
13 if (error
>= maxError
)
17 printf(" Inaccuracy: max(abs(error)) = %g >= %g\n",maxError
,EPS
);
22 void compareArray_real(const char* ID
, const void* array
, const void* refArray
, int size
)
24 return compareArray(ID
, array
, refArray
, size
, 0);
27 void compareArray_int(const char* ID
, const void* array
, const void* refArray
, int size
)
29 return compareArray(ID
, array
, refArray
, size
, 1);
32 // Read array by columns (as in MATLAB) and return by-rows encoding
33 void* readArray(const char* fileName
, int isInteger
)
35 // need to prepend '../data/' (not really nice code...)
36 char* fullFileName
= (char*)calloc(5+strlen(fileName
)+1,sizeof(char));
37 strcat(fullFileName
, "data/");
38 strcat(fullFileName
, fileName
);
39 FILE* file
= fopen(fullFileName
, "r");
42 // first pass to know how many elements to allocate
43 // /////................... TODO
46 for (int i
=0; i
<nbDims
; i
++)
47 totalDim
*= dimensions
[i
];
48 size_t elementSize
= isInteger
52 // read all values, and convert them to by-rows matrices format
53 void* matlabArray
= malloc(totalDim
*elementSize
);
56 for (mwSize u
=0; u
<totalDim
; u
++)
58 // position to next non-separator character
59 while (!feof(file
) && (curChar
==' ' || curChar
=='\n' || curChar
=='\t' || curChar
==','))
60 curChar
= fgetc(file
);
61 // read number (as a string)
63 while (!feof(file
) && curChar
!=' ' && curChar
!='\n' && curChar
!='\t' && curChar
!=',')
65 bufferNum
[bufferIndex
++] = curChar
;
66 curChar
= fgetc(file
);
68 bufferNum
[bufferIndex
] = 0;
69 // transform string into Real, and store it at appropriate location
71 ((Int
*)matlabArray
)[u
] = atoi(bufferNum
);
73 ((Real
*)matlabArray
)[u
] = atof(bufferNum
);
77 void* brArray
= matlabToBrArray(matlabArray
, dimensions
, nbDims
, isInteger
);