testLogDetApproximation.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @file testLogDetApproximation.cpp
  3. * @brief
  4. * @author Alexander Freytag
  5. * @date 05-01-2012 (dd-mm-yyyy)
  6. */
  7. #include <iostream>
  8. #include <cstdlib>
  9. #include <ctime>
  10. #include "core/vector/MatrixT.h"
  11. #include "core/vector/VectorT.h"
  12. #include "core/algebra/GenericMatrix.h"
  13. #include "core/algebra/EigValuesTRLAN.h"
  14. #include "core/algebra/GMStandard.h"
  15. #include "gp-hik-core/tools.h"
  16. #include "gp-hik-core/algebra/LogDetApproxBaiAndGolub.h"
  17. using namespace std;
  18. using namespace NICE;
  19. /**
  20. * @brief Printing main menu.
  21. * @author Alexander Freytag
  22. * @date 12/06/2011
  23. *
  24. * @return void
  25. **/
  26. void print_main_menu()
  27. {
  28. std::cerr << std::endl << " - test program for logDet Approximation" << std::endl;
  29. std::cerr << "Input options:" << std::endl;
  30. std::cerr << " -n <number> dimension of K"<< std::endl;
  31. std::cerr << " -v 1/0 verbose mode"<< std::endl;
  32. return;
  33. }
  34. int main (int argc, char* argv[])
  35. {
  36. int n (5);
  37. bool verbose(false);
  38. int rc;
  39. if (argc<2)
  40. {
  41. print_main_menu();
  42. return -1;
  43. }
  44. while ((rc=getopt(argc,argv,"n:v:h"))>=0)
  45. {
  46. switch(rc)
  47. {
  48. case 'n': n = atoi(optarg); break;
  49. case 'v': verbose = atoi(optarg); break;
  50. default: print_main_menu();
  51. }
  52. }
  53. if (verbose)
  54. {
  55. std::cerr << "Testing logDet Approximation for n = " << n << std::endl;
  56. }
  57. srand ( time(NULL) );
  58. NICE::Matrix ARand(generateRandomMatrix(n,n));
  59. NICE::Matrix A;
  60. // A shall be positive definite
  61. A.multiply(ARand, ARand, true);
  62. NICE::GMStandard genericA(A);
  63. //compute GT LogDet based on eigenvalues of A
  64. NICE::Vector eigenvalues;
  65. NICE::Matrix eigenvectors;
  66. try
  67. {
  68. NICE::EigValuesTRLAN eigValuesComputation;
  69. eigValuesComputation.getEigenvalues(genericA, eigenvalues,eigenvectors, n );
  70. }
  71. catch (...)
  72. {
  73. NICE::EVArnoldi eigValuesComputation;
  74. eigValuesComputation.getEigenvalues(genericA, eigenvalues,eigenvectors, n );
  75. }
  76. double logDetGT(0.0);
  77. for (int i = 0; i < n; i++)
  78. {
  79. logDetGT += log(eigenvalues[i]);
  80. }
  81. if (verbose)
  82. {
  83. std::cerr << "GT logDet: " << logDetGT << std::endl;
  84. }
  85. //replace this later on using only the k largest eigenvalues
  86. double frobNorm(A.squaredFrobeniusNorm());
  87. NICE::LogDetApproxBaiAndGolub logDetApproximator;
  88. double logDetApprox(logDetApproximator.getLogDetApproximation(A.trace(), frobNorm, eigenvalues.Max(), eigenvalues.Min(), n ) );
  89. if (verbose)
  90. {
  91. std::cerr << "logDetApprox: " << logDetApprox << std::endl;
  92. }
  93. double logDetApproxUpperBound(logDetApproximator.getLogDetApproximationUpperBound(A.trace(), frobNorm, eigenvalues.Max(), n ) );
  94. if (verbose)
  95. {
  96. std::cerr << "logDetApproxUpperBound: " << logDetApproxUpperBound << std::endl;
  97. }
  98. double logDetApproxLowerBound(logDetApproximator.getLogDetApproximationUpperBound(A.trace(), frobNorm, eigenvalues.Min(), n ) );
  99. if (verbose)
  100. {
  101. std::cerr << "logDetApproxLowerBound: " << logDetApproxLowerBound << std::endl;
  102. }
  103. return 0;
  104. }