lu.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. //
  2. // Copyright (c) 2000-2002
  3. // Joerg Walter, Mathias Koch
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // The authors gratefully acknowledge the support of
  10. // GeNeSys mbH & Co. KG in producing this work.
  11. //
  12. #ifndef _BOOST_UBLAS_LU_
  13. #define _BOOST_UBLAS_LU_
  14. #include <boost/numeric/ublas/operation.hpp>
  15. #include <boost/numeric/ublas/vector_proxy.hpp>
  16. #include <boost/numeric/ublas/matrix_proxy.hpp>
  17. #include <boost/numeric/ublas/vector.hpp>
  18. #include <boost/numeric/ublas/triangular.hpp>
  19. // LU factorizations in the spirit of LAPACK and Golub & van Loan
  20. namespace boost { namespace numeric { namespace ublas {
  21. /** \brief
  22. *
  23. * \tparam T
  24. * \tparam A
  25. */
  26. template<class T = std::size_t, class A = unbounded_array<T> >
  27. class permutation_matrix:
  28. public vector<T, A> {
  29. public:
  30. typedef vector<T, A> vector_type;
  31. typedef typename vector_type::size_type size_type;
  32. // Construction and destruction
  33. BOOST_UBLAS_INLINE
  34. explicit
  35. permutation_matrix (size_type size):
  36. vector<T, A> (size) {
  37. for (size_type i = 0; i < size; ++ i)
  38. (*this) (i) = i;
  39. }
  40. BOOST_UBLAS_INLINE
  41. explicit
  42. permutation_matrix (const vector_type & init)
  43. : vector_type(init)
  44. { }
  45. BOOST_UBLAS_INLINE
  46. ~permutation_matrix () {}
  47. // Assignment
  48. BOOST_UBLAS_INLINE
  49. permutation_matrix &operator = (const permutation_matrix &m) {
  50. vector_type::operator = (m);
  51. return *this;
  52. }
  53. };
  54. template<class PM, class MV>
  55. BOOST_UBLAS_INLINE
  56. void swap_rows (const PM &pm, MV &mv, vector_tag) {
  57. typedef typename PM::size_type size_type;
  58. size_type size = pm.size ();
  59. for (size_type i = 0; i < size; ++ i) {
  60. if (i != pm (i))
  61. std::swap (mv (i), mv (pm (i)));
  62. }
  63. }
  64. template<class PM, class MV>
  65. BOOST_UBLAS_INLINE
  66. void swap_rows (const PM &pm, MV &mv, matrix_tag) {
  67. typedef typename PM::size_type size_type;
  68. size_type size = pm.size ();
  69. for (size_type i = 0; i < size; ++ i) {
  70. if (i != pm (i))
  71. row (mv, i).swap (row (mv, pm (i)));
  72. }
  73. }
  74. // Dispatcher
  75. template<class PM, class MV>
  76. BOOST_UBLAS_INLINE
  77. void swap_rows (const PM &pm, MV &mv) {
  78. swap_rows (pm, mv, typename MV::type_category ());
  79. }
  80. // LU factorization without pivoting
  81. template<class M>
  82. typename M::size_type lu_factorize (M &m) {
  83. typedef typename M::size_type size_type;
  84. typedef typename M::value_type value_type;
  85. #if BOOST_UBLAS_TYPE_CHECK
  86. typedef M matrix_type;
  87. matrix_type cm (m);
  88. #endif
  89. size_type singular = 0;
  90. size_type size1 = m.size1 ();
  91. size_type size2 = m.size2 ();
  92. size_type size = (std::min) (size1, size2);
  93. for (size_type i = 0; i < size; ++ i) {
  94. matrix_column<M> mci (column (m, i));
  95. matrix_row<M> mri (row (m, i));
  96. if (m (i, i) != value_type/*zero*/()) {
  97. value_type m_inv = value_type (1) / m (i, i);
  98. project (mci, range (i + 1, size1)) *= m_inv;
  99. } else if (singular == 0) {
  100. singular = i + 1;
  101. }
  102. project (m, range (i + 1, size1), range (i + 1, size2)).minus_assign (
  103. outer_prod (project (mci, range (i + 1, size1)),
  104. project (mri, range (i + 1, size2))));
  105. }
  106. #if BOOST_UBLAS_TYPE_CHECK
  107. BOOST_UBLAS_CHECK (singular != 0 ||
  108. detail::expression_type_check (prod (triangular_adaptor<matrix_type, unit_lower> (m),
  109. triangular_adaptor<matrix_type, upper> (m)),
  110. cm), internal_logic ());
  111. #endif
  112. return singular;
  113. }
  114. // LU factorization with partial pivoting
  115. template<class M, class PM>
  116. typename M::size_type lu_factorize (M &m, PM &pm) {
  117. typedef typename M::size_type size_type;
  118. typedef typename M::value_type value_type;
  119. #if BOOST_UBLAS_TYPE_CHECK
  120. typedef M matrix_type;
  121. matrix_type cm (m);
  122. #endif
  123. size_type singular = 0;
  124. size_type size1 = m.size1 ();
  125. size_type size2 = m.size2 ();
  126. size_type size = (std::min) (size1, size2);
  127. for (size_type i = 0; i < size; ++ i) {
  128. matrix_column<M> mci (column (m, i));
  129. matrix_row<M> mri (row (m, i));
  130. size_type i_norm_inf = i + index_norm_inf (project (mci, range (i, size1)));
  131. BOOST_UBLAS_CHECK (i_norm_inf < size1, external_logic ());
  132. if (m (i_norm_inf, i) != value_type/*zero*/()) {
  133. if (i_norm_inf != i) {
  134. pm (i) = i_norm_inf;
  135. row (m, i_norm_inf).swap (mri);
  136. } else {
  137. BOOST_UBLAS_CHECK (pm (i) == i_norm_inf, external_logic ());
  138. }
  139. value_type m_inv = value_type (1) / m (i, i);
  140. project (mci, range (i + 1, size1)) *= m_inv;
  141. } else if (singular == 0) {
  142. singular = i + 1;
  143. }
  144. project (m, range (i + 1, size1), range (i + 1, size2)).minus_assign (
  145. outer_prod (project (mci, range (i + 1, size1)),
  146. project (mri, range (i + 1, size2))));
  147. }
  148. #if BOOST_UBLAS_TYPE_CHECK
  149. swap_rows (pm, cm);
  150. BOOST_UBLAS_CHECK (singular != 0 ||
  151. detail::expression_type_check (prod (triangular_adaptor<matrix_type, unit_lower> (m),
  152. triangular_adaptor<matrix_type, upper> (m)), cm), internal_logic ());
  153. #endif
  154. return singular;
  155. }
  156. template<class M, class PM>
  157. typename M::size_type axpy_lu_factorize (M &m, PM &pm) {
  158. typedef M matrix_type;
  159. typedef typename M::size_type size_type;
  160. typedef typename M::value_type value_type;
  161. typedef vector<value_type> vector_type;
  162. #if BOOST_UBLAS_TYPE_CHECK
  163. matrix_type cm (m);
  164. #endif
  165. size_type singular = 0;
  166. size_type size1 = m.size1 ();
  167. size_type size2 = m.size2 ();
  168. size_type size = (std::min) (size1, size2);
  169. #ifndef BOOST_UBLAS_LU_WITH_INPLACE_SOLVE
  170. matrix_type mr (m);
  171. mr.assign (zero_matrix<value_type> (size1, size2));
  172. vector_type v (size1);
  173. for (size_type i = 0; i < size; ++ i) {
  174. matrix_range<matrix_type> lrr (project (mr, range (0, i), range (0, i)));
  175. vector_range<matrix_column<matrix_type> > urr (project (column (mr, i), range (0, i)));
  176. urr.assign (solve (lrr, project (column (m, i), range (0, i)), unit_lower_tag ()));
  177. project (v, range (i, size1)).assign (
  178. project (column (m, i), range (i, size1)) -
  179. axpy_prod<vector_type> (project (mr, range (i, size1), range (0, i)), urr));
  180. size_type i_norm_inf = i + index_norm_inf (project (v, range (i, size1)));
  181. BOOST_UBLAS_CHECK (i_norm_inf < size1, external_logic ());
  182. if (v (i_norm_inf) != value_type/*zero*/()) {
  183. if (i_norm_inf != i) {
  184. pm (i) = i_norm_inf;
  185. std::swap (v (i_norm_inf), v (i));
  186. project (row (m, i_norm_inf), range (i + 1, size2)).swap (project (row (m, i), range (i + 1, size2)));
  187. } else {
  188. BOOST_UBLAS_CHECK (pm (i) == i_norm_inf, external_logic ());
  189. }
  190. project (column (mr, i), range (i + 1, size1)).assign (
  191. project (v, range (i + 1, size1)) / v (i));
  192. if (i_norm_inf != i) {
  193. project (row (mr, i_norm_inf), range (0, i)).swap (project (row (mr, i), range (0, i)));
  194. }
  195. } else if (singular == 0) {
  196. singular = i + 1;
  197. }
  198. mr (i, i) = v (i);
  199. }
  200. m.assign (mr);
  201. #else
  202. matrix_type lr (m);
  203. matrix_type ur (m);
  204. lr.assign (identity_matrix<value_type> (size1, size2));
  205. ur.assign (zero_matrix<value_type> (size1, size2));
  206. vector_type v (size1);
  207. for (size_type i = 0; i < size; ++ i) {
  208. matrix_range<matrix_type> lrr (project (lr, range (0, i), range (0, i)));
  209. vector_range<matrix_column<matrix_type> > urr (project (column (ur, i), range (0, i)));
  210. urr.assign (project (column (m, i), range (0, i)));
  211. inplace_solve (lrr, urr, unit_lower_tag ());
  212. project (v, range (i, size1)).assign (
  213. project (column (m, i), range (i, size1)) -
  214. axpy_prod<vector_type> (project (lr, range (i, size1), range (0, i)), urr));
  215. size_type i_norm_inf = i + index_norm_inf (project (v, range (i, size1)));
  216. BOOST_UBLAS_CHECK (i_norm_inf < size1, external_logic ());
  217. if (v (i_norm_inf) != value_type/*zero*/()) {
  218. if (i_norm_inf != i) {
  219. pm (i) = i_norm_inf;
  220. std::swap (v (i_norm_inf), v (i));
  221. project (row (m, i_norm_inf), range (i + 1, size2)).swap (project (row (m, i), range (i + 1, size2)));
  222. } else {
  223. BOOST_UBLAS_CHECK (pm (i) == i_norm_inf, external_logic ());
  224. }
  225. project (column (lr, i), range (i + 1, size1)).assign (
  226. project (v, range (i + 1, size1)) / v (i));
  227. if (i_norm_inf != i) {
  228. project (row (lr, i_norm_inf), range (0, i)).swap (project (row (lr, i), range (0, i)));
  229. }
  230. } else if (singular == 0) {
  231. singular = i + 1;
  232. }
  233. ur (i, i) = v (i);
  234. }
  235. m.assign (triangular_adaptor<matrix_type, strict_lower> (lr) +
  236. triangular_adaptor<matrix_type, upper> (ur));
  237. #endif
  238. #if BOOST_UBLAS_TYPE_CHECK
  239. swap_rows (pm, cm);
  240. BOOST_UBLAS_CHECK (singular != 0 ||
  241. detail::expression_type_check (prod (triangular_adaptor<matrix_type, unit_lower> (m),
  242. triangular_adaptor<matrix_type, upper> (m)), cm), internal_logic ());
  243. #endif
  244. return singular;
  245. }
  246. // LU substitution
  247. template<class M, class E>
  248. void lu_substitute (const M &m, vector_expression<E> &e) {
  249. #if BOOST_UBLAS_TYPE_CHECK
  250. typedef const M const_matrix_type;
  251. typedef vector<typename E::value_type> vector_type;
  252. vector_type cv1 (e);
  253. #endif
  254. inplace_solve (m, e, unit_lower_tag ());
  255. #if BOOST_UBLAS_TYPE_CHECK
  256. BOOST_UBLAS_CHECK (detail::expression_type_check (prod (triangular_adaptor<const_matrix_type, unit_lower> (m), e), cv1), internal_logic ());
  257. vector_type cv2 (e);
  258. #endif
  259. inplace_solve (m, e, upper_tag ());
  260. #if BOOST_UBLAS_TYPE_CHECK
  261. BOOST_UBLAS_CHECK (detail::expression_type_check (prod (triangular_adaptor<const_matrix_type, upper> (m), e), cv2), internal_logic ());
  262. #endif
  263. }
  264. template<class M, class E>
  265. void lu_substitute (const M &m, matrix_expression<E> &e) {
  266. #if BOOST_UBLAS_TYPE_CHECK
  267. typedef const M const_matrix_type;
  268. typedef matrix<typename E::value_type> matrix_type;
  269. matrix_type cm1 (e);
  270. #endif
  271. inplace_solve (m, e, unit_lower_tag ());
  272. #if BOOST_UBLAS_TYPE_CHECK
  273. BOOST_UBLAS_CHECK (detail::expression_type_check (prod (triangular_adaptor<const_matrix_type, unit_lower> (m), e), cm1), internal_logic ());
  274. matrix_type cm2 (e);
  275. #endif
  276. inplace_solve (m, e, upper_tag ());
  277. #if BOOST_UBLAS_TYPE_CHECK
  278. BOOST_UBLAS_CHECK (detail::expression_type_check (prod (triangular_adaptor<const_matrix_type, upper> (m), e), cm2), internal_logic ());
  279. #endif
  280. }
  281. template<class M, class PMT, class PMA, class MV>
  282. void lu_substitute (const M &m, const permutation_matrix<PMT, PMA> &pm, MV &mv) {
  283. swap_rows (pm, mv);
  284. lu_substitute (m, mv);
  285. }
  286. template<class E, class M>
  287. void lu_substitute (vector_expression<E> &e, const M &m) {
  288. #if BOOST_UBLAS_TYPE_CHECK
  289. typedef const M const_matrix_type;
  290. typedef vector<typename E::value_type> vector_type;
  291. vector_type cv1 (e);
  292. #endif
  293. inplace_solve (e, m, upper_tag ());
  294. #if BOOST_UBLAS_TYPE_CHECK
  295. BOOST_UBLAS_CHECK (detail::expression_type_check (prod (e, triangular_adaptor<const_matrix_type, upper> (m)), cv1), internal_logic ());
  296. vector_type cv2 (e);
  297. #endif
  298. inplace_solve (e, m, unit_lower_tag ());
  299. #if BOOST_UBLAS_TYPE_CHECK
  300. BOOST_UBLAS_CHECK (detail::expression_type_check (prod (e, triangular_adaptor<const_matrix_type, unit_lower> (m)), cv2), internal_logic ());
  301. #endif
  302. }
  303. template<class E, class M>
  304. void lu_substitute (matrix_expression<E> &e, const M &m) {
  305. #if BOOST_UBLAS_TYPE_CHECK
  306. typedef const M const_matrix_type;
  307. typedef matrix<typename E::value_type> matrix_type;
  308. matrix_type cm1 (e);
  309. #endif
  310. inplace_solve (e, m, upper_tag ());
  311. #if BOOST_UBLAS_TYPE_CHECK
  312. BOOST_UBLAS_CHECK (detail::expression_type_check (prod (e, triangular_adaptor<const_matrix_type, upper> (m)), cm1), internal_logic ());
  313. matrix_type cm2 (e);
  314. #endif
  315. inplace_solve (e, m, unit_lower_tag ());
  316. #if BOOST_UBLAS_TYPE_CHECK
  317. BOOST_UBLAS_CHECK (detail::expression_type_check (prod (e, triangular_adaptor<const_matrix_type, unit_lower> (m)), cm2), internal_logic ());
  318. #endif
  319. }
  320. template<class MV, class M, class PMT, class PMA>
  321. void lu_substitute (MV &mv, const M &m, const permutation_matrix<PMT, PMA> &pm) {
  322. swap_rows (pm, mv);
  323. lu_substitute (mv, m);
  324. }
  325. }}}
  326. #endif