| 1 | // Check if array == refArray |
| 2 | void compareArray(const char* ID, const void* array, const void* refArray, int size, |
| 3 | int isInteger) |
| 4 | { |
| 5 | Real EPS = 1e-5; //precision |
| 6 | printf("Checking %s\n",ID); |
| 7 | Real maxError = 0.0; |
| 8 | for (mwSize i=0; i<size; i++) |
| 9 | { |
| 10 | Real error = isInteger |
| 11 | ? fabs(((Int*)array)[i] - ((Int*)refArray)[i]) |
| 12 | : fabs(((Real*)array)[i] - ((Real*)refArray)[i]); |
| 13 | if (error >= maxError) |
| 14 | maxError = error; |
| 15 | } |
| 16 | if (maxError >= EPS) |
| 17 | printf(" Inaccuracy: max(abs(error)) = %g >= %g\n",maxError,EPS); |
| 18 | else |
| 19 | printf(" OK\n"); |
| 20 | } |
| 21 | |
| 22 | void compareArray_real(const char* ID, const void* array, const void* refArray, int size) |
| 23 | { |
| 24 | return compareArray(ID, array, refArray, size, 0); |
| 25 | } |
| 26 | |
| 27 | void compareArray_int(const char* ID, const void* array, const void* refArray, int size) |
| 28 | { |
| 29 | return compareArray(ID, array, refArray, size, 1); |
| 30 | } |
| 31 | |
| 32 | // Read array by columns (as in MATLAB) and return by-rows encoding |
| 33 | void* readArray(const char* fileName, int isInteger) |
| 34 | { |
| 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"); |
| 40 | free(fullFileName); |
| 41 | |
| 42 | // first pass to know how many elements to allocate |
| 43 | // /////................... TODO |
| 44 | |
| 45 | int d = 1; |
| 46 | for (int i=0; i<nbDims; i++) |
| 47 | totalDim *= dimensions[i]; |
| 48 | size_t elementSize = isInteger |
| 49 | ? sizeof(Int) |
| 50 | : sizeof(Real); |
| 51 | |
| 52 | // read all values, and convert them to by-rows matrices format |
| 53 | void* matlabArray = malloc(totalDim*elementSize); |
| 54 | char curChar = ' '; |
| 55 | char bufferNum[64]; |
| 56 | for (mwSize u=0; u<totalDim; u++) |
| 57 | { |
| 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) |
| 62 | int bufferIndex = 0; |
| 63 | while (!feof(file) && curChar!=' ' && curChar!='\n' && curChar!='\t' && curChar!=',') |
| 64 | { |
| 65 | bufferNum[bufferIndex++] = curChar; |
| 66 | curChar = fgetc(file); |
| 67 | } |
| 68 | bufferNum[bufferIndex] = 0; |
| 69 | // transform string into Real, and store it at appropriate location |
| 70 | if (isInteger) |
| 71 | ((Int*)matlabArray)[u] = atoi(bufferNum); |
| 72 | else |
| 73 | ((Real*)matlabArray)[u] = atof(bufferNum); |
| 74 | } |
| 75 | fclose(file); |
| 76 | |
| 77 | void* brArray = matlabToBrArray(matlabArray, dimensions, nbDims, isInteger); |
| 78 | free(matlabArray); |
| 79 | return brArray; |
| 80 | } |
| 81 | |