Algorithms.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "Algorithms.h"
  2. #ifdef NICE_USELIB_LINAL
  3. // direct access to lapack function (not forwarded in LinAl)
  4. extern "C" {
  5. void dtrtrs_ ( char *uplo, char *trans, char *diag, int *n, int *nrhs,
  6. const double *a, int *lda, double *b, int *ldb, int *info );
  7. }
  8. #endif
  9. namespace NICE {
  10. void choleskyDecompLargeScale ( const Matrix & A, Matrix & G, bool resetUpperTriangle )
  11. {
  12. if ( A.rows() != A.cols() )
  13. fthrow(Exception, "Matrix is not quadratic !");
  14. int size = A.rows();
  15. G.resize ( size, size );
  16. // IPP is faster for cholesky decomposition, but lapack
  17. // is faster for back substitution in combination with choleskyInvert
  18. // For a comparision: cf. testCholeskySpeed
  19. #ifdef NICE_USELIB_IPP
  20. int stride = sizeof(double);
  21. int stride_m = size * sizeof(double);
  22. // the following function is undocumented in 5.3 but documented in 6.1
  23. IppStatus ippStatus = ippmCholeskyDecomp_m( A.getDataPointer(), stride_m, stride,
  24. G.getDataPointer(), stride_m, stride, size);
  25. if ( ippStatus != ippStsOk )
  26. fthrow(Exception, ippGetStatusString(ippStatus));
  27. // we have now to correct this matrix -> transpose and invert diagonal elements
  28. // funny definition in IPP
  29. G.transposeInplace();
  30. for ( int i = 0 ; i < size ; i++ )
  31. G(i,i) = 1.0 / G(i,i);
  32. if ( resetUpperTriangle )
  33. for ( uint i = 0 ; i < (uint)size; i++ )
  34. for ( uint j = i+1 ; j < (uint)size ; j++ )
  35. G(i,j) = 0;
  36. #else
  37. #ifdef NICE_USELIB_LINAL
  38. // Lapack routines
  39. G = A;
  40. char uplo='L';
  41. int lda=size;
  42. int info;
  43. dpotrf_ ( &uplo, &size, G.getDataPointer(), &lda, &info );
  44. if ( info != 0 )
  45. fthrow(Exception, "Lapack cholesky decomposition failed: error=" << info << "." );
  46. if ( resetUpperTriangle )
  47. for ( uint i = 0 ; i < (uint)size; i++ )
  48. for ( uint j = i+1 ; j < (uint)size ; j++ )
  49. G(i,j) = 0;
  50. #else
  51. #ifndef CHOLESKYLINAL_WARNING
  52. #pragma message WARNING("LinAl is not installed: choleskyDecompLargeScale will not be optimized.")
  53. #define CHOLESKYLINAL_WARNING
  54. #endif
  55. choleskyDecomp ( A, G, resetUpperTriangle );
  56. #endif
  57. #endif
  58. }
  59. void choleskyInvertLargeScale ( const Matrix & G, Matrix & Ainv )
  60. {
  61. int size = G.rows();
  62. Ainv.resize ( size, size );
  63. Ainv.setIdentity();
  64. choleskySolveMatrixLargeScale ( G, Ainv );
  65. }
  66. void choleskySolveMatrixLargeScale ( const Matrix & G, Matrix & B )
  67. {
  68. #ifdef NICE_USELIB_LINAL
  69. if (G.rows() != G.cols())// || (B.rows() != B.cols()) )
  70. fthrow(Exception, "Matrix is not quadratic !");
  71. if ( G.rows() != B.rows() )
  72. fthrow(Exception, "Matrices sizes do not fit together.");
  73. int sizeG = G.rows();
  74. int sizeB = B.cols();
  75. int ldb = sizeG;
  76. char trans = 'N';
  77. char diag = 'N';
  78. char uplo='L';
  79. int lda=sizeG;
  80. int info;
  81. dtrtrs_ ( &uplo, &trans, &diag, &sizeG, &sizeB, G.getDataPointer(), &lda,
  82. B.getDataPointer(), &ldb, &info );
  83. trans = 'T';
  84. dtrtrs_ ( &uplo, &trans, &diag, &sizeG, &sizeB, G.getDataPointer(), &lda,
  85. B.getDataPointer(), &ldb, &info );
  86. #else
  87. #ifndef CHOLESKYLINAL_WARNING
  88. #warning "LinAl is not installed: choleskyInvertLargeScale will not be optimized."
  89. #define CHOLESKYLINAL_WARNING
  90. #endif
  91. choleskyInvert ( G, B );
  92. #endif
  93. }
  94. void triangleSolveMatrix ( const Matrix & G, Matrix & B, bool transposedMatrix )
  95. {
  96. #ifdef NICE_USELIB_LINAL
  97. if ( (G.rows() != G.cols()) || (B.rows() != B.cols()) )
  98. fthrow(Exception, "Matrix is not quadratic !");
  99. if ( G.rows() != B.rows() )
  100. fthrow(Exception, "Matrices sizes do not fit together.");
  101. int size = G.rows();
  102. int ldb = size;
  103. char trans = (transposedMatrix) ? 'T' : 'N';
  104. char diag = 'N';
  105. char uplo='L';
  106. int lda=size;
  107. int info;
  108. dtrtrs_ ( &uplo, &trans, &diag, &size, &size, G.getDataPointer(), &lda,
  109. B.getDataPointer(), &ldb, &info );
  110. #else
  111. fthrow(Exception, "LinAl is not installed: triangleSolveMatrix is not available.");
  112. #endif
  113. }
  114. void triangleSolve ( const Matrix & G, const Vector & b, Vector & x, bool transposedMatrix )
  115. {
  116. #ifdef NICE_USELIB_LINAL
  117. if ( (G.rows() != G.cols()) )
  118. fthrow(Exception, "Matrix is not quadratic !");
  119. if ( G.rows() != b.size() )
  120. fthrow(Exception, "Matrix and vector sizes do not fit together.");
  121. int size = G.rows();
  122. int ldb = size;
  123. char trans = (transposedMatrix) ? 'T' : 'N';
  124. char diag = 'N';
  125. char uplo='L';
  126. int lda=size;
  127. int info;
  128. int sizeB = 1;
  129. x.resize ( b.size() );
  130. x = b;
  131. dtrtrs_ ( &uplo, &trans, &diag, &size, &sizeB, G.getDataPointer(), &lda,
  132. x.getDataPointer(), &ldb, &info );
  133. #else
  134. fthrow(Exception, "LinAl is not installed: triangleSolveMatrix is not available.");
  135. #endif
  136. }
  137. void choleskySolveLargeScale ( const Matrix & G, const Vector & b, Vector & x )
  138. {
  139. if ( G.rows() != G.cols() )
  140. fthrow(Exception, "Matrix is not quadratic !");
  141. if ( b.size() != G.cols() )
  142. fthrow(Exception, "Matrix or right hand side of the linear system has wrong dimensions !");
  143. x.resize ( b.size() );
  144. x = b;
  145. #ifdef NICE_USELIB_LINAL
  146. int size = G.rows();
  147. int ldb = size;
  148. char trans = 'N';
  149. char diag = 'N';
  150. char uplo='L';
  151. int lda=size;
  152. int info;
  153. int sizeSingle = 1;
  154. dtrtrs_ ( &uplo, &trans, &diag, &size, &sizeSingle, G.getDataPointer(), &lda,
  155. x.getDataPointer(), &ldb, &info );
  156. if ( info != 0 )
  157. fthrow(Exception, "dtrtrs_ returned with error code (invalid cholesky matrix?)" << info );
  158. trans = 'T';
  159. dtrtrs_ ( &uplo, &trans, &diag, &size, &sizeSingle, G.getDataPointer(), &lda,
  160. x.getDataPointer(), &ldb, &info );
  161. if ( info != 0 )
  162. fthrow(Exception, "dtrtrs_ returned with error code (invalid cholesky matrix?)" << info );
  163. #else
  164. #ifdef NICE_USELIB_IPP
  165. int stride = sizeof(double);
  166. int stride_m = G.rows() * sizeof(double);
  167. // this is nasty
  168. Matrix Gcorrected ( G );
  169. Gcorrected.transposeInplace();
  170. for ( uint i = 0 ; i < G.rows() ; i++ )
  171. Gcorrected(i,i) = 1.0 / Gcorrected(i,i);
  172. IppStatus ippStatus = ippmCholeskyBackSubst_mv( Gcorrected.getDataPointer(), stride_m,
  173. stride, b.getDataPointer(), stride, x.getDataPointer(), stride, G.rows() );
  174. if ( ippStatus != ippStsOk )
  175. fthrow(Exception, ippGetStatusString(ippStatus));
  176. #else
  177. #ifndef CHOLESKYLINAL_WARNING
  178. #warning "LinAl is not installed: choleskyInvertLargeScale will not be optimized."
  179. #define CHOLESKYLINAL_WARNING
  180. #endif
  181. choleskySolve ( G, b, x );
  182. #endif
  183. #endif
  184. }
  185. }