First public contribution.
2 // Copyright (c) 2000-2002
3 // Joerg Walter, Mathias Koch
5 // Permission to use, copy, modify, distribute and sell this software
6 // and its documentation for any purpose is hereby granted without fee,
7 // provided that the above copyright notice appear in all copies and
8 // that both that copyright notice and this permission notice appear
9 // in supporting documentation. The authors make no representations
10 // about the suitability of this software for any purpose.
11 // It is provided "as is" without express or implied warranty.
13 // The authors gratefully acknowledge the support of
14 // GeNeSys mbH & Co. KG in producing this work.
17 #ifndef _BOOST_UBLAS_LU_
18 #define _BOOST_UBLAS_LU_
20 #include <boost/numeric/ublas/operation.hpp>
21 #include <boost/numeric/ublas/vector_proxy.hpp>
22 #include <boost/numeric/ublas/matrix_proxy.hpp>
23 #include <boost/numeric/ublas/vector.hpp>
24 #include <boost/numeric/ublas/triangular.hpp>
26 // LU factorizations in the spirit of LAPACK and Golub & van Loan
28 namespace boost { namespace numeric { namespace ublas {
30 template<class T = std::size_t, class A = unbounded_array<T> >
31 class permutation_matrix:
34 typedef vector<T, A> vector_type;
35 typedef typename vector_type::size_type size_type;
37 // Construction and destruction
39 permutation_matrix (size_type size):
41 for (size_type i = 0; i < size; ++ i)
45 ~permutation_matrix () {}
49 permutation_matrix &operator = (const permutation_matrix &m) {
50 vector_type::operator = (m);
55 template<class PM, class MV>
57 void swap_rows (const PM &pm, MV &mv, vector_tag) {
58 typedef typename PM::size_type size_type;
59 typedef typename MV::value_type value_type;
61 size_type size = pm.size ();
62 for (size_type i = 0; i < size; ++ i) {
64 std::swap (mv (i), mv (pm (i)));
67 template<class PM, class MV>
69 void swap_rows (const PM &pm, MV &mv, matrix_tag) {
70 typedef typename PM::size_type size_type;
71 typedef typename MV::value_type value_type;
73 size_type size = pm.size ();
74 for (size_type i = 0; i < size; ++ i) {
76 row (mv, i).swap (row (mv, pm (i)));
80 template<class PM, class MV>
82 void swap_rows (const PM &pm, MV &mv) {
83 swap_rows (pm, mv, typename MV::type_category ());
86 // LU factorization without pivoting
88 typename M::size_type lu_factorize (M &m) {
89 typedef M matrix_type;
90 typedef typename M::size_type size_type;
91 typedef typename M::value_type value_type;
93 #if BOOST_UBLAS_TYPE_CHECK
97 size_type size1 = m.size1 ();
98 size_type size2 = m.size2 ();
99 size_type size = (std::min) (size1, size2);
100 for (size_type i = 0; i < size; ++ i) {
101 matrix_column<M> mci (column (m, i));
102 matrix_row<M> mri (row (m, i));
103 if (m (i, i) != value_type/*zero*/()) {
104 project (mci, range (i + 1, size1)) *= value_type (1) / m (i, i);
105 } else if (singular == 0) {
108 project (m, range (i + 1, size1), range (i + 1, size2)).minus_assign (
109 outer_prod (project (mci, range (i + 1, size1)),
110 project (mri, range (i + 1, size2))));
112 #if BOOST_UBLAS_TYPE_CHECK
113 BOOST_UBLAS_CHECK (singular != 0 ||
114 detail::expression_type_check (prod (triangular_adaptor<matrix_type, unit_lower> (m),
115 triangular_adaptor<matrix_type, upper> (m)),
116 cm), internal_logic ());
121 // LU factorization with partial pivoting
122 template<class M, class PM>
123 typename M::size_type lu_factorize (M &m, PM &pm) {
124 typedef M matrix_type;
125 typedef typename M::size_type size_type;
126 typedef typename M::value_type value_type;
128 #if BOOST_UBLAS_TYPE_CHECK
132 size_type size1 = m.size1 ();
133 size_type size2 = m.size2 ();
134 size_type size = (std::min) (size1, size2);
135 for (size_type i = 0; i < size; ++ i) {
136 matrix_column<M> mci (column (m, i));
137 matrix_row<M> mri (row (m, i));
138 size_type i_norm_inf = i + index_norm_inf (project (mci, range (i, size1)));
139 BOOST_UBLAS_CHECK (i_norm_inf < size1, external_logic ());
140 if (m (i_norm_inf, i) != value_type/*zero*/()) {
141 if (i_norm_inf != i) {
143 row (m, i_norm_inf).swap (mri);
145 BOOST_UBLAS_CHECK (pm (i) == i_norm_inf, external_logic ());
147 project (mci, range (i + 1, size1)) *= value_type (1) / m (i, i);
148 } else if (singular == 0) {
151 project (m, range (i + 1, size1), range (i + 1, size2)).minus_assign (
152 outer_prod (project (mci, range (i + 1, size1)),
153 project (mri, range (i + 1, size2))));
155 #if BOOST_UBLAS_TYPE_CHECK
157 BOOST_UBLAS_CHECK (singular != 0 ||
158 detail::expression_type_check (prod (triangular_adaptor<matrix_type, unit_lower> (m),
159 triangular_adaptor<matrix_type, upper> (m)), cm), internal_logic ());
164 template<class M, class PM>
165 typename M::size_type axpy_lu_factorize (M &m, PM &pm) {
166 typedef M matrix_type;
167 typedef typename M::size_type size_type;
168 typedef typename M::value_type value_type;
169 typedef vector<value_type> vector_type;
171 #if BOOST_UBLAS_TYPE_CHECK
175 size_type size1 = m.size1 ();
176 size_type size2 = m.size2 ();
177 size_type size = (std::min) (size1, size2);
178 #ifndef BOOST_UBLAS_LU_WITH_INPLACE_SOLVE
180 mr.assign (zero_matrix<value_type> (size1, size2));
181 vector_type v (size1);
182 for (size_type i = 0; i < size; ++ i) {
183 matrix_range<matrix_type> lrr (project (mr, range (0, i), range (0, i)));
184 vector_range<matrix_column<matrix_type> > urr (project (column (mr, i), range (0, i)));
185 urr.assign (solve (lrr, project (column (m, i), range (0, i)), unit_lower_tag ()));
186 project (v, range (i, size1)).assign (
187 project (column (m, i), range (i, size1)) -
188 axpy_prod<vector_type> (project (mr, range (i, size1), range (0, i)), urr));
189 size_type i_norm_inf = i + index_norm_inf (project (v, range (i, size1)));
190 BOOST_UBLAS_CHECK (i_norm_inf < size1, external_logic ());
191 if (v (i_norm_inf) != value_type/*zero*/()) {
192 if (i_norm_inf != i) {
194 std::swap (v (i_norm_inf), v (i));
195 project (row (m, i_norm_inf), range (i + 1, size2)).swap (project (row (m, i), range (i + 1, size2)));
197 BOOST_UBLAS_CHECK (pm (i) == i_norm_inf, external_logic ());
199 project (column (mr, i), range (i + 1, size1)).assign (
200 project (v, range (i + 1, size1)) / v (i));
201 if (i_norm_inf != i) {
202 project (row (mr, i_norm_inf), range (0, i)).swap (project (row (mr, i), range (0, i)));
204 } else if (singular == 0) {
213 lr.assign (identity_matrix<value_type> (size1, size2));
214 ur.assign (zero_matrix<value_type> (size1, size2));
215 vector_type v (size1);
216 for (size_type i = 0; i < size; ++ i) {
217 matrix_range<matrix_type> lrr (project (lr, range (0, i), range (0, i)));
218 vector_range<matrix_column<matrix_type> > urr (project (column (ur, i), range (0, i)));
219 urr.assign (project (column (m, i), range (0, i)));
220 inplace_solve (lrr, urr, unit_lower_tag ());
221 project (v, range (i, size1)).assign (
222 project (column (m, i), range (i, size1)) -
223 axpy_prod<vector_type> (project (lr, range (i, size1), range (0, i)), urr));
224 size_type i_norm_inf = i + index_norm_inf (project (v, range (i, size1)));
225 BOOST_UBLAS_CHECK (i_norm_inf < size1, external_logic ());
226 if (v (i_norm_inf) != value_type/*zero*/()) {
227 if (i_norm_inf != i) {
229 std::swap (v (i_norm_inf), v (i));
230 project (row (m, i_norm_inf), range (i + 1, size2)).swap (project (row (m, i), range (i + 1, size2)));
232 BOOST_UBLAS_CHECK (pm (i) == i_norm_inf, external_logic ());
234 project (column (lr, i), range (i + 1, size1)).assign (
235 project (v, range (i + 1, size1)) / v (i));
236 if (i_norm_inf != i) {
237 project (row (lr, i_norm_inf), range (0, i)).swap (project (row (lr, i), range (0, i)));
239 } else if (singular == 0) {
244 m.assign (triangular_adaptor<matrix_type, strict_lower> (lr) +
245 triangular_adaptor<matrix_type, upper> (ur));
247 #if BOOST_UBLAS_TYPE_CHECK
249 BOOST_UBLAS_CHECK (singular != 0 ||
250 detail::expression_type_check (prod (triangular_adaptor<matrix_type, unit_lower> (m),
251 triangular_adaptor<matrix_type, upper> (m)), cm), internal_logic ());
257 template<class M, class E>
258 void lu_substitute (const M &m, vector_expression<E> &e) {
259 typedef const M const_matrix_type;
260 typedef vector<typename E::value_type> vector_type;
262 #if BOOST_UBLAS_TYPE_CHECK
265 inplace_solve (m, e, unit_lower_tag ());
266 #if BOOST_UBLAS_TYPE_CHECK
267 BOOST_UBLAS_CHECK (detail::expression_type_check (prod (triangular_adaptor<const_matrix_type, unit_lower> (m), e), cv1), internal_logic ());
270 inplace_solve (m, e, upper_tag ());
271 #if BOOST_UBLAS_TYPE_CHECK
272 BOOST_UBLAS_CHECK (detail::expression_type_check (prod (triangular_adaptor<const_matrix_type, upper> (m), e), cv2), internal_logic ());
275 template<class M, class E>
276 void lu_substitute (const M &m, matrix_expression<E> &e) {
277 typedef const M const_matrix_type;
278 typedef matrix<typename E::value_type> matrix_type;
280 #if BOOST_UBLAS_TYPE_CHECK
283 inplace_solve (m, e, unit_lower_tag ());
284 #if BOOST_UBLAS_TYPE_CHECK
285 BOOST_UBLAS_CHECK (detail::expression_type_check (prod (triangular_adaptor<const_matrix_type, unit_lower> (m), e), cm1), internal_logic ());
288 inplace_solve (m, e, upper_tag ());
289 #if BOOST_UBLAS_TYPE_CHECK
290 BOOST_UBLAS_CHECK (detail::expression_type_check (prod (triangular_adaptor<const_matrix_type, upper> (m), e), cm2), internal_logic ());
293 template<class M, class PMT, class PMA, class MV>
294 void lu_substitute (const M &m, const permutation_matrix<PMT, PMA> &pm, MV &mv) {
296 lu_substitute (m, mv);
298 template<class E, class M>
299 void lu_substitute (vector_expression<E> &e, const M &m) {
300 typedef const M const_matrix_type;
301 typedef vector<typename E::value_type> vector_type;
303 #if BOOST_UBLAS_TYPE_CHECK
306 inplace_solve (e, m, upper_tag ());
307 #if BOOST_UBLAS_TYPE_CHECK
308 BOOST_UBLAS_CHECK (detail::expression_type_check (prod (e, triangular_adaptor<const_matrix_type, upper> (m)), cv1), internal_logic ());
311 inplace_solve (e, m, unit_lower_tag ());
312 #if BOOST_UBLAS_TYPE_CHECK
313 BOOST_UBLAS_CHECK (detail::expression_type_check (prod (e, triangular_adaptor<const_matrix_type, unit_lower> (m)), cv2), internal_logic ());
316 template<class E, class M>
317 void lu_substitute (matrix_expression<E> &e, const M &m) {
318 typedef const M const_matrix_type;
319 typedef matrix<typename E::value_type> matrix_type;
321 #if BOOST_UBLAS_TYPE_CHECK
324 inplace_solve (e, m, upper_tag ());
325 #if BOOST_UBLAS_TYPE_CHECK
326 BOOST_UBLAS_CHECK (detail::expression_type_check (prod (e, triangular_adaptor<const_matrix_type, upper> (m)), cm1), internal_logic ());
329 inplace_solve (e, m, unit_lower_tag ());
330 #if BOOST_UBLAS_TYPE_CHECK
331 BOOST_UBLAS_CHECK (detail::expression_type_check (prod (e, triangular_adaptor<const_matrix_type, unit_lower> (m)), cm2), internal_logic ());
334 template<class MV, class M, class PMT, class PMA>
335 void lu_substitute (MV &mv, const M &m, const permutation_matrix<PMT, PMA> &pm) {
337 lu_substitute (mv, m);