c++ - Unexpected results in subtraction of double in function -
i'm trying implement eigenfaces algorithm in c++. made database creation part , i'm setting recognition part.
the problem if variables correctly read file "prova.txt"
, once passed function odd happens: whole imd
array contains values differ expected ones (i've same algorithm running in matlab reference).
i'm python programmer know it's silly c++ user, can't find errors in code (probably not optimized , full of redundancies btw). i'm posting link "prova.txt"
after code.
u
, omega
matrices, linearized in order able pass them input arguments of function.
#include <iostream> #include <cmath> #include <fstream.h> #include <cstdlib> using namespace std; int confrontodb(double*, double, double, double*, double*, double*); int confrontodb(double* test, double h, double m, double* m, double* u, double* omega) { int urows = m; int ulines = h; double om[urows]; double imd[ulines]; double d[urows][urows]; double dist[urows]; double accum; double minimo; int ind = 0; // subtract average "med" test sample "test" (int = 0; < ulines; i++) { imd[i] = test[i] - m[i]; } // project "imd" on u eigenspace (om = u'*imd) (int = 0; < urows; i++) { (int j = 0; j < ulines; j++) { om[i] += u[urows * j + i] * imd[j]; }; }; // generate matrix in each column copy of om (int = 0; < urows; i++) { (int j = 0; j < urows; j++) { d[i][j] = om[i]; }; }; // subtract omega d (int = 0; < urows; i++) { (int j = 0; j < urows; j++) { d[i][j] = d[i][j] - omega[i * urows + j]; }; }; // norm each column of d (int = 0; < urows; i++) { accum = 0; (int j = 0; j < urows; j++) { accum += d[i][j] * d[i][j]; }; dist[i] = sqrt(accum); }; // minimum , maximum distance minimo = dist[0]; double massimo = dist[0]; (int = 0; < urows; i++) { if (dist[i] < minimo) { ind = i; minimo = dist[i]; } else if (dist[i] > massimo) { massimo = dist[i]; }; }; cout << "minimo " << minimo << endl; cout << "massimo " << massimo << endl; return ind; } int main(int argc, char* argv[]) { ifstream f; f.open("prova.txt"); double* omega; omega = (double*)calloc(198 * 198, sizeof(double)); (int = 0; < 198 * 198; i++) { f >> omega[i]; }; (int = 0; < 198; i++) { (int j = 0; j < 198; j++) { cout << " " << omega[i * 198 + j]; } cout << endl; } double* u; u = (double*)calloc(4001 * 198, sizeof(double)); // float u[4001*198]; (int = 0; < 4001 * 198; i++) { f >> u[i]; }; double* med; med = (double*)calloc(4001, sizeof(double)); (int = 0; < 4001; i++) { f >> med[i]; }; double* test; test = (double*)calloc(4001, sizeof(double)); (int = 0; < 4001; i++) { f >> test[i]; }; f.close(); cout << "etichetta riconosciuta " << confrontodb(test, 4001, 198, med, u, omega) << endl; free(med); free(test); free(u); free(omega); return 0; }
link "prova.txt"
(it's 13 megabytes)
expected results:
imdmatlab = -2.5252525e-01 -1.3080808e+00 -1.8080808e+00 -2.7676768e+00 -4.1161616e+00 -3.1969697e+00 -2.5707071e+00 -2.6616162e+00 -3.1616162e+00 -2.3181818e+00 -2.1767677e+00 [...]
with actual code, first imd
element correct, second has 1/1000 error , goes randomly, these elements should enough check).
Comments
Post a Comment