Algorithms.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #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. if (G.rows() != G.cols())
  92. fthrow(Exception, "Matrix G is not quadratic !");
  93. if ( G.rows() != B.rows() )
  94. fthrow(Exception, "Matrices sizes do not fit together.");
  95. if (B.rows() == B.cols())
  96. choleskyInvert ( G, B );
  97. else
  98. {
  99. Vector b(B.rows(),0.0);
  100. for (size_t i=0;i<B.cols();i++)
  101. {
  102. choleskySolveLargeScale (G, B.getColumn(i), b);
  103. B.getColumnRef(i) = b;
  104. }
  105. }
  106. #endif
  107. }
  108. void triangleSolveMatrix ( const Matrix & G, Matrix & B, bool transposedMatrix )
  109. {
  110. #ifdef NICE_USELIB_LINAL
  111. if ( (G.rows() != G.cols()) || (B.rows() != B.cols()) )
  112. fthrow(Exception, "Matrix is not quadratic !");
  113. if ( G.rows() != B.rows() )
  114. fthrow(Exception, "Matrices sizes do not fit together.");
  115. int size = G.rows();
  116. int ldb = size;
  117. char trans = (transposedMatrix) ? 'T' : 'N';
  118. char diag = 'N';
  119. char uplo='L';
  120. int lda=size;
  121. int info;
  122. dtrtrs_ ( &uplo, &trans, &diag, &size, &size, G.getDataPointer(), &lda,
  123. B.getDataPointer(), &ldb, &info );
  124. #else
  125. fthrow(Exception, "LinAl is not installed: triangleSolveMatrix is not available.");
  126. #endif
  127. }
  128. void triangleSolve ( const Matrix & G, const Vector & b, Vector & x, bool transposedMatrix )
  129. {
  130. #ifdef NICE_USELIB_LINAL
  131. if ( (G.rows() != G.cols()) )
  132. fthrow(Exception, "Matrix is not quadratic !");
  133. if ( G.rows() != b.size() )
  134. fthrow(Exception, "Matrix and vector sizes do not fit together.");
  135. int size = G.rows();
  136. int ldb = size;
  137. char trans = (transposedMatrix) ? 'T' : 'N';
  138. char diag = 'N';
  139. char uplo='L';
  140. int lda=size;
  141. int info;
  142. int sizeB = 1;
  143. x.resize ( b.size() );
  144. x = b;
  145. dtrtrs_ ( &uplo, &trans, &diag, &size, &sizeB, G.getDataPointer(), &lda,
  146. x.getDataPointer(), &ldb, &info );
  147. #else
  148. fthrow(Exception, "LinAl is not installed: triangleSolveMatrix is not available.");
  149. #endif
  150. }
  151. void choleskySolveLargeScale ( const Matrix & G, const Vector & b, Vector & x )
  152. {
  153. if ( G.rows() != G.cols() )
  154. fthrow(Exception, "Matrix is not quadratic !");
  155. if ( b.size() != G.cols() )
  156. fthrow(Exception, "Matrix or right hand side of the linear system has wrong dimensions !");
  157. x.resize ( b.size() );
  158. x = b;
  159. #ifdef NICE_USELIB_LINAL
  160. int size = G.rows();
  161. int ldb = size;
  162. char trans = 'N';
  163. char diag = 'N';
  164. char uplo='L';
  165. int lda=size;
  166. int info;
  167. int sizeSingle = 1;
  168. dtrtrs_ ( &uplo, &trans, &diag, &size, &sizeSingle, G.getDataPointer(), &lda,
  169. x.getDataPointer(), &ldb, &info );
  170. if ( info != 0 )
  171. fthrow(Exception, "dtrtrs_ returned with error code (invalid cholesky matrix?)" << info );
  172. trans = 'T';
  173. dtrtrs_ ( &uplo, &trans, &diag, &size, &sizeSingle, G.getDataPointer(), &lda,
  174. x.getDataPointer(), &ldb, &info );
  175. if ( info != 0 )
  176. fthrow(Exception, "dtrtrs_ returned with error code (invalid cholesky matrix?)" << info );
  177. #else
  178. #ifdef NICE_USELIB_IPP
  179. int stride = sizeof(double);
  180. int stride_m = G.rows() * sizeof(double);
  181. // this is nasty
  182. Matrix Gcorrected ( G );
  183. Gcorrected.transposeInplace();
  184. for ( uint i = 0 ; i < G.rows() ; i++ )
  185. Gcorrected(i,i) = 1.0 / Gcorrected(i,i);
  186. IppStatus ippStatus = ippmCholeskyBackSubst_mv( Gcorrected.getDataPointer(), stride_m,
  187. stride, b.getDataPointer(), stride, x.getDataPointer(), stride, G.rows() );
  188. if ( ippStatus != ippStsOk )
  189. fthrow(Exception, ippGetStatusString(ippStatus));
  190. #else
  191. #ifndef CHOLESKYLINAL_WARNING
  192. #warning "LinAl is not installed: choleskyInvertLargeScale will not be optimized."
  193. #define CHOLESKYLINAL_WARNING
  194. #endif
  195. choleskySolve ( G, b, x );
  196. #endif
  197. #endif
  198. }
  199. }