testILSConjugateGradientsLanczos.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /**
  2. * @file testILSConjugateGradientsLanczos.cpp
  3. * @author Paul Bodesheim
  4. * @date 23/01/2012
  5. * @brief test routine for Iterative Linear Solver: Conjugate Gradients Method (CGM) using Lanczos vectors
  6. */
  7. #include "core/vector/MatrixT.h"
  8. #include "core/vector/VectorT.h"
  9. #include <stdio.h>
  10. #include <ctime>
  11. #include "iostream"
  12. #include "core/basics/Exception.h"
  13. #include "core/vector/Algorithms.h"
  14. // #include "core/algebra/ILSConjugateGradients.h"
  15. #include "core/algebra/GMStandard.h"
  16. #include "core/algebra/ILSConjugateGradientsLanczos.h"
  17. using namespace std;
  18. using namespace NICE;
  19. int main(int argc, char* argv[])
  20. {
  21. int mySize = 20; // number of equations
  22. FILE * logfile;
  23. std::string logfilename;
  24. if ( argc < 2 )
  25. logfilename = "/home/bodesheim/testILS-CGM-Lanczos.log";
  26. else
  27. logfilename = argv[1];
  28. logfile = fopen(logfilename.c_str(), "w");
  29. // generate matrix A
  30. Matrix A(mySize,mySize,0.0);
  31. fprintf(logfile, "A:\n");
  32. for (uint i = 0; i < A.rows(); i++)
  33. {
  34. for (uint j = 0; j < A.cols(); j++)
  35. {
  36. if ( j == i ) A(i,j) = (i+1)+(j+1);
  37. else {
  38. A(i,j) = sqrt((i+1)*(j+1));
  39. }
  40. fprintf(logfile, "%f ",A(i,j));
  41. }
  42. fprintf(logfile, "\n");
  43. }
  44. // generate vector b (RHS of LS)
  45. Vector b(mySize,0.0);
  46. fprintf(logfile, "b:\n");
  47. for (uint i = 0; i < b.size(); i++)
  48. {
  49. b(i) = (i+1)*sqrt(i+1);
  50. fprintf(logfile, "%f ",b(i));
  51. }
  52. fprintf(logfile, "\n");
  53. // solve Ax = b
  54. Vector x(mySize,0.0);
  55. ILSConjugateGradientsLanczos cgm(true,mySize);
  56. //tic
  57. time_t start = clock();
  58. cgm.solveLin(GMStandard(A),b,x);
  59. //toc
  60. float duration = (float) (clock() - start);
  61. std::cerr << "Time for solveLin: " << duration/CLOCKS_PER_SEC << std::endl;
  62. fprintf(logfile, "x:\n");
  63. for (uint i = 0; i < x.size(); i++)
  64. {
  65. fprintf(logfile, "%f ",x(i));
  66. }
  67. fprintf(logfile, "\n");
  68. // check result
  69. Vector Ax(mySize,0.0);
  70. Ax = A*x;
  71. fprintf(logfile, "A*x:\n");
  72. for (uint i = 0; i < Ax.size(); i++)
  73. {
  74. fprintf(logfile, "%f ",Ax(i));
  75. }
  76. fprintf(logfile, "\n");
  77. fclose(logfile);
  78. return 0;
  79. }
  80. // Algorithm without using class ILSConjugateGradientsLanczos works for 10x10 matrix A
  81. // int main(int argc, char* argv[])
  82. // {
  83. //
  84. // int mySize = 10; // number of equations
  85. // FILE * logfile;
  86. // std::string logfilename;
  87. //
  88. // if ( argc < 2 )
  89. // logfilename = "/home/bodesheim/testILS-CGM-Lanczos.log";
  90. // else
  91. // logfilename = argv[1];
  92. //
  93. // logfile = fopen(logfilename.c_str(), "w");
  94. //
  95. // // generate matrix A
  96. // Matrix A(mySize,mySize,0.0);
  97. // fprintf(logfile, "A:\n");
  98. // for (uint i = 0; i < A.rows(); i++)
  99. // {
  100. // for (uint j = 0; j < A.cols(); j++)
  101. // {
  102. // if ( j == i ) A(i,j) = (i+1)+(j+1);
  103. // else {
  104. //
  105. // A(i,j) = sqrt((i+1)*(j+1));
  106. // }
  107. //
  108. // fprintf(logfile, "%f ",A(i,j));
  109. // }
  110. // fprintf(logfile, "\n");
  111. // }
  112. //
  113. // // generate vector b (RHS of LS)
  114. // Vector b(mySize,0.0);
  115. // fprintf(logfile, "b:\n");
  116. // for (uint i = 0; i < b.size(); i++)
  117. // {
  118. // b(i) = (i+1)*sqrt(i+1);
  119. // fprintf(logfile, "%f ",b(i));
  120. // }
  121. // fprintf(logfile, "\n");
  122. //
  123. // double beta1 = b.normL2();
  124. // fprintf(logfile, "norm of b: %f\n\n",beta1);
  125. //
  126. // // init some helpers
  127. // Matrix Tmatrix(mySize,mySize,0.0);
  128. // Matrix Vmatrix(mySize,mySize,0.0);
  129. // Matrix Cmatrix(mySize,mySize,0.0);
  130. // Matrix Dmatrix(mySize,mySize,0.0);
  131. // Matrix Lmatrix(mySize,mySize,0.0);
  132. // Vector p(mySize,0.0);
  133. //
  134. // Vector Av(mySize,0.0);
  135. // Vector v_new(mySize,0.0);
  136. // Vector v_old(mySize,0.0);
  137. // Vector v_older(mySize,0.0);
  138. // Vector c_new(mySize,0.0);
  139. // Vector c_old(mySize,0.0);
  140. //
  141. // Vector x(mySize,0.0);
  142. //
  143. // double d_new = 0;
  144. // double d_old = 0;
  145. // double l_new = 0;
  146. // double p_new = 0;
  147. // double p_old = 0;
  148. // double alpha = 0;
  149. // double beta = 0;
  150. //
  151. // for (uint iter = 0; iter < Tmatrix.rows(); iter++)
  152. // {
  153. //
  154. // if ( iter == 0 ) {
  155. //
  156. // v_new = (1/beta1)*b; // init v1
  157. // Av.multiply(A,v_new);
  158. // alpha = v_new.scalarProduct(Av);
  159. // d_new=alpha;
  160. // p_new = beta1/d_new;
  161. //
  162. // Tmatrix(iter,iter)=alpha;
  163. //
  164. // } else {
  165. //
  166. // v_new = Av - (alpha*v_old) - (beta*v_older);
  167. //
  168. // beta = v_new.normL2();
  169. // v_new.normalizeL2();
  170. //
  171. // Av.multiply(A,v_new);
  172. // alpha = v_new.scalarProduct(Av);
  173. //
  174. // l_new = beta/sqrt(d_old);
  175. // d_new = alpha-(l_new*l_new);
  176. //
  177. // l_new/=sqrt(d_old);
  178. //
  179. // p_new = -p_old*l_new*d_old/d_new;
  180. //
  181. // Tmatrix(iter,iter-1)=beta;
  182. // Tmatrix(iter-1,iter)=beta;
  183. // Tmatrix(iter,iter)=alpha;
  184. // Lmatrix(iter,iter-1)=l_new;
  185. // }
  186. //
  187. // c_new = v_new - (l_new*c_old);
  188. //
  189. // x+=(p_new*c_new);
  190. //
  191. // fprintf(logfile, "\n x after iteration %d:\n",iter+1);
  192. // for (uint i = 0; i < x.size(); i++)
  193. // {
  194. // fprintf(logfile, "%f ",x(i));
  195. // }
  196. //
  197. // Dmatrix(iter,iter)=d_new;
  198. // Lmatrix(iter,iter)=1;
  199. // Vmatrix.getColumnRef(iter) = v_new;
  200. // Cmatrix.getColumnRef(iter) = c_new;
  201. // p(iter)=p_new;
  202. //
  203. // d_old = d_new;
  204. // p_old = p_new;
  205. // v_older = v_old;
  206. // v_old = v_new;
  207. // c_old = c_new;
  208. // }
  209. //
  210. // fprintf(logfile, "\n\n Result of CGM w/o Lanczos: \n");
  211. // ILSConjugateGradients cgm(true,x.size());
  212. // Vector xCGM (x.size(),0.0);
  213. // cgm.solveLin(GMStandard(A),b,xCGM);
  214. // for (uint i = 0; i < xCGM.size(); i++)
  215. // {
  216. // fprintf(logfile, "%f ",xCGM(i));
  217. // }
  218. // fprintf(logfile, "\n");
  219. //
  220. // fprintf(logfile, "\n\n Result using QR decomp: \n");
  221. // solveLinearEquationQR(A, b, xCGM);
  222. // for (uint i = 0; i < xCGM.size(); i++)
  223. // {
  224. // fprintf(logfile, "%f ",xCGM(i));
  225. // }
  226. // fprintf(logfile, "\n");
  227. //
  228. // fprintf(logfile, "\n\n Check Iterative Results: \n");
  229. // Vector Cp(p.size(),0.0);
  230. // Cp.multiply(Cmatrix,p);
  231. // fprintf(logfile, "\nC*p:\n");
  232. // for (uint i = 0; i < Cp.size(); i++)
  233. // {
  234. // fprintf(logfile, "%f ",Cp(i));
  235. // }
  236. // fprintf(logfile, "\n");
  237. //
  238. // fprintf(logfile, "\ntransposed Vmatrix:\n");
  239. // for (uint i = 0; i < Vmatrix.rows(); i++)
  240. // {
  241. // for (uint j = 0; j < Vmatrix.cols(); j++)
  242. // {
  243. // fprintf(logfile, "%f ",Vmatrix(j,i));
  244. // }
  245. // fprintf(logfile, "\n");
  246. // }
  247. //
  248. // fprintf(logfile, "\nL times C^T:\n");
  249. // Matrix LC (Lmatrix.rows(),Cmatrix.rows(),0.0);
  250. // LC.multiply(Lmatrix,Cmatrix.transpose());
  251. // for (uint i = 0; i < LC.rows(); i++)
  252. // {
  253. // for (uint j = 0; j < LC.cols(); j++)
  254. // {
  255. // fprintf(logfile, "%f ",LC(i,j));
  256. // }
  257. // fprintf(logfile, "\n");
  258. // }
  259. //
  260. // fprintf(logfile, "\n\n Check Cholesky of T: \n");
  261. // fprintf(logfile, "Tmatrix:\n");
  262. // for (uint i = 0; i < Tmatrix.rows(); i++)
  263. // {
  264. // for (uint j = 0; j < Tmatrix.cols(); j++)
  265. // {
  266. // fprintf(logfile, "%f ",Tmatrix(i,j));
  267. // }
  268. // fprintf(logfile, "\n");
  269. // }
  270. //
  271. // fprintf(logfile, "\nDmatrix:\n");
  272. // for (uint i = 0; i < Dmatrix.rows(); i++)
  273. // {
  274. // for (uint j = 0; j < Dmatrix.cols(); j++)
  275. // {
  276. // fprintf(logfile, "%f ",Dmatrix(i,j));
  277. // }
  278. // fprintf(logfile, "\n");
  279. // }
  280. //
  281. // fprintf(logfile, "\nLmatrix:\n");
  282. // for (uint i = 0; i < Lmatrix.rows(); i++)
  283. // {
  284. // for (uint j = 0; j < Lmatrix.cols(); j++)
  285. // {
  286. // fprintf(logfile, "%f ",Lmatrix(i,j));
  287. // }
  288. // fprintf(logfile, "\n");
  289. // }
  290. //
  291. // Matrix LD(mySize,mySize,0.0);
  292. // Matrix LDL(mySize,mySize,0.0);
  293. // LD.multiply(Lmatrix,Dmatrix);
  294. // LDL.multiply(LD,Lmatrix.transpose());
  295. //
  296. // fprintf(logfile, "\nLDL:\n");
  297. // for (uint i = 0; i < LDL.rows(); i++)
  298. // {
  299. // for (uint j = 0; j < LDL.cols(); j++)
  300. // {
  301. // fprintf(logfile, "%f ",LDL(i,j));
  302. // }
  303. // fprintf(logfile, "\n");
  304. // }
  305. //
  306. // Vector LDp;
  307. // LDp.multiply(LD,p);
  308. //
  309. // fprintf(logfile, "\nL*D*p:\n");
  310. // for (uint i = 0; i < LDp.size(); i++)
  311. // {
  312. // fprintf(logfile, "%f ",LDp(i));
  313. // }
  314. //
  315. //
  316. // fclose(logfile);
  317. //
  318. // return 0;
  319. // }
  320. // CHOLESKY FACTORIZATION OF TRIDIAGONAL MATRIX T WORKS:
  321. // int main(int argc, char* argv[])
  322. // {
  323. //
  324. // FILE * logfile;
  325. // std::string logfilename;
  326. //
  327. // if ( argc < 2 )
  328. // logfilename = "/home/bodesheim/testILS-CGM-Lanczos.log";
  329. // else
  330. // logfilename = argv[1];
  331. //
  332. // logfile = fopen(logfilename.c_str(), "w");
  333. //
  334. // Matrix Tmatrix(5,5,0.0);
  335. // fprintf(logfile, "Tmatrix:\n");
  336. // for (uint i = 0; i < Tmatrix.rows(); i++)
  337. // {
  338. // for (uint j = 0; j < Tmatrix.cols(); j++)
  339. // {
  340. // if ( j == i ) Tmatrix(i,j) = (i+1)+(j+1);
  341. // else if ( j == (i+1) ) {
  342. //
  343. // Tmatrix(i,j) = sqrt((i+1)*(j+1));
  344. // Tmatrix(j,i) = Tmatrix(i,j);
  345. // }
  346. //
  347. // fprintf(logfile, "%f ",Tmatrix(i,j));
  348. // }
  349. // fprintf(logfile, "\n");
  350. // }
  351. //
  352. // Matrix Dmatrix(5,5,0.0);
  353. // Matrix Lmatrix(5,5,0.0);
  354. // Vector p(5,0.0);
  355. //
  356. // double beta = 2.4;
  357. // double d_new = 0;
  358. // double d_old = 0;
  359. // double l_new = 0;
  360. // double p_new = 0;
  361. // double p_old = 0;
  362. //
  363. // for (uint iter = 0; iter < Tmatrix.rows(); iter++)
  364. // {
  365. //
  366. // if ( iter == 0 ) {
  367. //
  368. // d_new = Tmatrix(iter,iter);
  369. // l_new = 1;
  370. // p_new = beta/d_new;
  371. //
  372. // } else {
  373. //
  374. // l_new = Tmatrix(iter,iter-1)/sqrt(d_old);
  375. // d_new = Tmatrix(iter,iter)-(l_new*l_new);
  376. //
  377. // l_new/=sqrt(d_old);
  378. // Lmatrix(iter,iter-1)=l_new;
  379. //
  380. // p_new = -p_old*l_new*d_old/d_new;
  381. //
  382. // }
  383. //
  384. // Dmatrix(iter,iter)=d_new;
  385. // Lmatrix(iter,iter)=1;
  386. // p(iter)=p_new;
  387. //
  388. // d_old = d_new;
  389. // p_old = p_new;
  390. // }
  391. //
  392. // fprintf(logfile, "Dmatrix:\n");
  393. // for (uint i = 0; i < Dmatrix.rows(); i++)
  394. // {
  395. // for (uint j = 0; j < Dmatrix.cols(); j++)
  396. // {
  397. // fprintf(logfile, "%f ",Dmatrix(i,j));
  398. // }
  399. // fprintf(logfile, "\n");
  400. // }
  401. //
  402. // fprintf(logfile, "Lmatrix:\n");
  403. // for (uint i = 0; i < Lmatrix.rows(); i++)
  404. // {
  405. // for (uint j = 0; j < Lmatrix.cols(); j++)
  406. // {
  407. // fprintf(logfile, "%f ",Lmatrix(i,j));
  408. // }
  409. // fprintf(logfile, "\n");
  410. // }
  411. //
  412. // Matrix LD(5,5,0.0);
  413. // Matrix LDL(5,5,0.0);
  414. // LD.multiply(Lmatrix,Dmatrix);
  415. // LDL.multiply(LD,Lmatrix.transpose());
  416. //
  417. // fprintf(logfile, "LDL:\n");
  418. // for (uint i = 0; i < LDL.rows(); i++)
  419. // {
  420. // for (uint j = 0; j < LDL.cols(); j++)
  421. // {
  422. // fprintf(logfile, "%f ",LDL(i,j));
  423. // }
  424. // fprintf(logfile, "\n");
  425. // }
  426. //
  427. // Vector result;
  428. // result.multiply(LD,p);
  429. //
  430. // fprintf(logfile, "result:\n");
  431. // for (uint i = 0; i < result.size(); i++)
  432. // {
  433. // fprintf(logfile, "%f ",result(i));
  434. // }
  435. //
  436. //
  437. // fclose(logfile);
  438. //
  439. // return 0;
  440. // }