CholeskyRobust.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * @file CholeskyRobust.cpp
  3. * @brief robust cholesky decomposition by adding some noise
  4. * @author Erik Rodner
  5. * @date 01/06/2010
  6. */
  7. #include <iostream>
  8. #include "CholeskyRobust.h"
  9. #include "core/vector/Algorithms.h"
  10. #ifdef NICE_USELIB_CUDACHOLESKY
  11. #include "cholesky-gpu/niceinterface/CudaCholeskyNICE.h"
  12. #endif
  13. using namespace std;
  14. using namespace NICE;
  15. using namespace OBJREC;
  16. CholeskyRobust::CholeskyRobust ( const CholeskyRobust & src )
  17. : m_verbose(src.m_verbose), m_noiseStep(src.m_noiseStep),
  18. m_minMatrixLogDeterminant(src.m_minMatrixLogDeterminant),
  19. m_useCuda(src.m_useCuda), m_logDetMatrix(src.m_logDetMatrix)
  20. {
  21. }
  22. double CholeskyRobust::robustChol ( const Matrix & A, Matrix & cholA )
  23. {
  24. // this additional memory requirement could be skipped
  25. // with a modified cholesky routine
  26. Matrix ARegularized (A);
  27. if ( m_verbose )
  28. cerr << "CholeskyRobust::robustChol: Adding noise " << m_noiseStep << endl;
  29. ARegularized.addIdentity ( m_noiseStep );
  30. #ifdef NICE_USELIB_CUDACHOLESKY
  31. if ( m_useCuda )
  32. CudaCholeskyNICE::choleskyDecomp ( ARegularized, cholA );
  33. else
  34. #endif
  35. choleskyDecompLargeScale ( ARegularized, cholA );
  36. m_logDetMatrix = 2*triangleMatrixLogDet ( cholA );
  37. if ( m_verbose )
  38. cerr << "CholeskyRobust::robustChol: Cholesky condition: " << m_logDetMatrix << endl;
  39. if ( !finite(m_logDetMatrix) )
  40. {
  41. fthrow(Exception, "The matrix has infinite or not defined log determinant !" );
  42. }
  43. return m_noiseStep;
  44. }
  45. double CholeskyRobust::robustCholInv ( const Matrix & A, Matrix & invA )
  46. {
  47. Matrix G;
  48. double noise = robustChol ( A, G );
  49. if ( m_verbose )
  50. cerr << "CholeskyRobust::robustChol: Cholesky inversion" << endl;
  51. choleskyInvertLargeScale ( G, invA );
  52. return noise;
  53. }
  54. CholeskyRobust::~CholeskyRobust ()
  55. {
  56. }
  57. CholeskyRobust::CholeskyRobust ( bool verbose, double noiseStep, double minMatrixLogDeterminant, bool useCuda ) :
  58. m_verbose(verbose), m_noiseStep(noiseStep), m_minMatrixLogDeterminant(minMatrixLogDeterminant), m_useCuda ( useCuda )
  59. {
  60. m_logDetMatrix = m_minMatrixLogDeterminant;
  61. }
  62. CholeskyRobust *CholeskyRobust::clone(void) const
  63. {
  64. CholeskyRobust *dst = new CholeskyRobust ( *this );
  65. return dst;
  66. }