AlgebraTools.cpp 10 KB

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