AlgebraTools.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /**
  2. * @file AlgebraTools.cpp
  3. * @brief AlgebraTools Class
  4. * @author Michael Koch
  5. * @date 08/19/2008
  6. */
  7. #include "AlgebraTools.h"
  8. using namespace OBJREC;
  9. using namespace std;
  10. using namespace NICE;
  11. //calculate mean
  12. // refactor-nice.pl: check this substitution
  13. // old: void AlgebraTools::calculateMean(const Matrix &data, Vector &mean)
  14. void AlgebraTools::calculateMean(const NICE::Matrix &data, NICE::Vector &mean)
  15. {
  16. uint features = data.rows();
  17. uint dimension = data.cols();
  18. mean = Vector(dimension);
  19. for (uint k = 0;k < mean.size();k++)
  20. {
  21. mean[k] = 0.0;
  22. }
  23. for (uint r = 0;r < features;r++)
  24. {
  25. for (uint c = 0;c < dimension;c++)
  26. {
  27. // refactor-nice.pl: check this substitution
  28. // old: if (isnan(data[r][c]) || isinf(data[r][c]))
  29. if(isnan(data(r, c)) ||(isnan(data(r, c))))
  30. {
  31. //strange data
  32. }
  33. else
  34. {
  35. // refactor-nice.pl: check this substitution
  36. // old: mean[c] = mean[c] + data[r][c];
  37. mean[c] = mean[c] + data(r, c);
  38. }
  39. }
  40. }
  41. for (uint r = 0;r < mean.size();r++)
  42. {
  43. if (features > 0)
  44. {
  45. mean[r] = mean[r] / (double)features;
  46. }
  47. }
  48. // cout<<"mean-calc"<<mean<<endl;
  49. }
  50. //calculate mean
  51. void AlgebraTools::calculateMean(const NICE::Matrix &data, NICE::Vector &mean, NICE::Vector &sigma)
  52. {
  53. uint features = data.rows();
  54. uint dimension = data.cols();
  55. mean = Vector(dimension);
  56. sigma = Vector(dimension);
  57. for (uint k = 0;k < mean.size();k++)
  58. {
  59. mean[k] = 0.0;
  60. sigma[k] = 0.0;
  61. }
  62. for (uint r = 0;r < features;r++)
  63. {
  64. for (uint c = 0;c < dimension;c++)
  65. {
  66. // refactor-nice.pl: check this substitution
  67. // old: if (isnan(data[r][c]) || isinf(data[r][c]))
  68. if(isnan(data(r, c)) ||(isnan(data(r, c))))
  69. {
  70. //strange data
  71. }
  72. else
  73. {
  74. // refactor-nice.pl: check this substitution
  75. // old: mean[c] = mean[c] + data[r][c];
  76. mean[c] = mean[c] + data(r, c);
  77. }
  78. }
  79. }
  80. for (uint k = 0;k < mean.size();k++)
  81. {
  82. if (features > 0)
  83. {
  84. mean[k] = mean[k] / (double)features;
  85. }
  86. }
  87. // cout<<"mean-calc"<<mean<<endl;
  88. //calc sigma
  89. for (uint r = 0;r < features;r++)
  90. {
  91. for (uint c = 0;c < dimension;c++)
  92. {
  93. // refactor-nice.pl: check this substitution
  94. // old: sigma[c] = sigma[c] + (mean[c] - data[r][c]) * (mean[c] - data[r][c]);
  95. sigma[c] = sigma[c] + (mean[c] -data(r, c)) * (mean[c] -data(r, c));
  96. }
  97. }
  98. for (uint k = 0;k < sigma.size();k++)
  99. {
  100. if (features > 0)
  101. {
  102. sigma[k] = sigma[k] / features;
  103. sigma[k] = sqrt(sigma[k]);
  104. }
  105. }
  106. }
  107. // refactor-nice.pl: check this substitution
  108. // old: double AlgebraTools::trace(const Matrix &M)
  109. double AlgebraTools::trace(const NICE::Matrix &M)
  110. {
  111. if (M.cols() != M.rows())
  112. {
  113. throw("non quadratic Matrix");
  114. }
  115. else
  116. {
  117. uint n = M.rows();
  118. double sum = 0.0;
  119. for (uint i = 0;i < n;i++)
  120. {
  121. // refactor-nice.pl: check this substitution
  122. // old: sum += M[i][i];
  123. sum +=M(i, i);
  124. }
  125. return sum;
  126. }
  127. }
  128. double AlgebraTools::matrixRowColNorm(const NICE::Matrix &M)
  129. {
  130. //max row norm
  131. double maxr = 0.0, maxc = 0.0;
  132. for (uint r = 0;r < M.rows();r++)
  133. {
  134. double sum = 0.0;
  135. for (uint c = 0;c < M.cols();c++)
  136. {
  137. sum +=fabs(M(r, c));
  138. }
  139. if (r == 0)
  140. {
  141. maxr = sum;
  142. }
  143. else
  144. {
  145. if (sum > maxr)
  146. {
  147. maxr = sum;
  148. }
  149. }
  150. }
  151. //max col norm
  152. for (uint c = 0;c < M.cols();c++)
  153. {
  154. double sum = 0.0;
  155. for (uint r = 0;r < M.rows();r++)
  156. {
  157. sum +=fabs(M(r, c));
  158. }
  159. if (c == 0)
  160. {
  161. maxc = sum;
  162. }
  163. else
  164. {
  165. if (sum > maxc)
  166. {
  167. maxc = sum;
  168. }
  169. }
  170. }
  171. if (maxr > maxc)
  172. {
  173. return maxr;
  174. }
  175. else
  176. {
  177. return maxc;
  178. }
  179. }
  180. void AlgebraTools::orthogonalize(Matrix &M, bool iterative)
  181. {
  182. #if 1
  183. fprintf (stderr, "AlgebraTools::orthogonalize: FIX THIS CODE !\n");
  184. exit(-1);
  185. #else
  186. //iterative
  187. if (iterative)
  188. {
  189. NICE::Vector onevec = Vector(M.rows());
  190. for (uint k = 0;k < onevec.size();k++)
  191. {
  192. onevec[k] = 1.0;
  193. }
  194. NICE::Matrix One = diag(onevec);
  195. NICE::Matrix Mt = M.transpose();
  196. NICE::Matrix error;
  197. error.multiply (M, Mt);
  198. error -= One;
  199. double errornorm = matrixRowColNorm(error);
  200. double deltaerror = 2.0;
  201. //reduceddim
  202. while (deltaerror > 1.0e-03)
  203. {
  204. double norm = matrixRowColNorm(M);
  205. if (norm > 0.0)
  206. {
  207. // refactor: M = (1.0 / norm) * M;
  208. M *= (1.0 / norm);
  209. }
  210. Mt = M.transpose();
  211. M = 1.5 * M - 0.5 * ((M * Mt) * M);
  212. Mt = M.transpose();
  213. //convergencetest
  214. error = (M * (Mt) - One);
  215. deltaerror = errornorm;
  216. errornorm = matrixRowColNorm(error);
  217. deltaerror = fabs(deltaerror - errornorm);
  218. }
  219. }
  220. else
  221. {
  222. //noniteratve
  223. // refactor-nice.pl: check this substitution
  224. // old: Matrix D, eigenvectors;
  225. NICE::Matrix D, eigenvectors;
  226. // refactor-nice.pl: check this substitution
  227. // old: Vector eigenvalue;
  228. NICE::Vector eigenvalue;
  229. // refactor-nice.pl: check this substitution
  230. // old: Matrix MMt = M*!M;
  231. NICE::Matrix MMt;
  232. MMt.multiply ( M, M.transpose() );
  233. Eigenvalue(MMt, eigenvalue, eigenvectors);
  234. D = diag(eigenvalue);
  235. for (int d = 0;d < D.rows();d++)
  236. {
  237. // refactor-nice.pl: check this substitution
  238. // old: if (D[d][d] > 0.0)
  239. if(D(d, d) > 0.0)
  240. {
  241. // refactor-nice.pl: check this substitution
  242. // old: D[d][d] = 1.0 / sqrt(D[d][d]);
  243. D(d, d) = 1.0 /D(d, d));
  244. }
  245. }
  246. // refactor-nice.pl: check this substitution
  247. // old: Matrix Et = !eigenvectors;
  248. NICE::Matrix Et = !eigenvectors;
  249. // refactor-nice.pl: check this substitution
  250. // old: Matrix MMtinvsqrt = eigenvectors * D * Et;
  251. NICE::Matrix MMtinvsqrt = eigenvectors * D * Et;
  252. M = MMtinvsqrt * M;
  253. }
  254. #endif
  255. }
  256. // refactor-nice.pl: check this substitution
  257. // old: Vector AlgebraTools::meanOfMatrix(const Matrix &M)
  258. NICE::Vector AlgebraTools::meanOfMatrix(const NICE::Matrix &M)
  259. {
  260. #if 1
  261. fprintf (stderr, "AlgebraTools:: FIX THIS CODE !\n");
  262. exit(-1);
  263. #else
  264. // refactor-nice.pl: check this substitution
  265. // old: Vector meanvec = Vector(M.cols());
  266. NICE::Vector meanvec = Vector(M.cols());
  267. //initialize meanvec
  268. for (uint k = 0;k < meanvec.size();k++)
  269. {
  270. meanvec[k] = 0.0;
  271. }
  272. //calculate mean
  273. for (int f = 0;f < M.rows();f++)
  274. {
  275. meanvec = M[f] + meanvec;
  276. }
  277. if (M.rows() > 0)
  278. {
  279. meanvec = 1.0 / M.rows() * meanvec;
  280. }
  281. return meanvec;
  282. #endif
  283. }
  284. // refactor-nice.pl: check this substitution
  285. // old: double AlgebraTools::meanOfVector(const Vector &vec)
  286. double AlgebraTools::meanOfVector(const NICE::Vector &vec)
  287. {
  288. #if 1
  289. fprintf (stderr, "AlgebraTools:: FIX THIS CODE !\n");
  290. exit(-1);
  291. #else
  292. double mean = 0.0;
  293. for (uint k = 0;k < vec.size();k++)
  294. {
  295. mean += vec[k];
  296. }
  297. if (vec.size() > 0)
  298. {
  299. mean /= vec.size();
  300. }
  301. return mean;
  302. #endif
  303. }
  304. // refactor-nice.pl: check this substitution
  305. // old: Matrix AlgebraTools::vectorToMatrix(const Vector &vec)
  306. NICE::Matrix AlgebraTools::vectorToMatrix(const NICE::Vector &vec)
  307. {
  308. if (vec.size() > 0)
  309. {
  310. // refactor-nice.pl: check this substitution
  311. // old: Matrix D = Matrix(vec.size(), 1, 0);
  312. NICE::Matrix D = Matrix(vec.size(), 1, 0);
  313. for (uint k = 0;k < vec.size();k++)
  314. {
  315. // refactor-nice.pl: check this substitution
  316. // old: D[k][0] = vec[k];
  317. D(k, 0) = vec[k];
  318. }
  319. return D;
  320. }
  321. else
  322. {
  323. return Matrix();
  324. }
  325. }
  326. // refactor-nice.pl: check this substitution
  327. // old: Vector AlgebraTools::matrixToVector(const Matrix &mat)
  328. NICE::Vector AlgebraTools::matrixToVector(const NICE::Matrix &mat)
  329. {
  330. if (mat.rows() < 1 && mat.cols() < 1)
  331. {
  332. fprintf(stderr, "Matrix has no entries!");
  333. }
  334. if (mat.rows() > mat.cols())
  335. {
  336. // refactor-nice.pl: check this substitution
  337. // old: Vector tmp = Vector(mat.rows());
  338. NICE::Vector tmp (mat.rows());
  339. tmp.set(0.0);
  340. for (uint i = 0;i < mat.rows();i++)
  341. {
  342. // refactor-nice.pl: check this substitution
  343. // old: tmp[i] = mat[i][0];
  344. tmp[i] =mat(i, 0);
  345. }
  346. return tmp;
  347. }
  348. else
  349. {
  350. // refactor-nice.pl: check this substitution
  351. // old: Vector tmp = Vector(mat.cols());
  352. NICE::Vector tmp = Vector(mat.cols());
  353. for (uint i = 0;i < mat.cols();i++)
  354. {
  355. // refactor-nice.pl: check this substitution
  356. // old: tmp[i] = mat[0][i];
  357. tmp[i] =mat(0, i);
  358. }
  359. return tmp;
  360. }
  361. }
  362. // refactor-nice.pl: check this substitution
  363. // old: Matrix AlgebraTools::diag(const Vector &diagonalelements)
  364. NICE::Matrix AlgebraTools::diag(const NICE::Vector &diagonalelements)
  365. {
  366. if (diagonalelements.size() > 0)
  367. {
  368. // refactor-nice.pl: check this substitution
  369. // old: Matrix D = Matrix(diagonalelements.size(), diagonalelements.size(), 0);
  370. NICE::Matrix D (diagonalelements.size(), diagonalelements.size());
  371. D.set(0.0);
  372. for (uint k = 0;k < diagonalelements.size();k++)
  373. {
  374. // refactor-nice.pl: check this substitution
  375. // old: D[k][k] = diagonalelements[k];
  376. D(k, k) = diagonalelements[k];
  377. }
  378. return D;
  379. }
  380. else
  381. {
  382. return Matrix();
  383. }
  384. }