64 #ifndef GMM_PRECOND_ILDLT_H
65 #define GMM_PRECOND_ILDLT_H
92 template <
typename Matrix>
96 typedef typename linalg_traits<Matrix>::value_type value_type;
97 typedef typename number_traits<value_type>::magnitude_type magnitude_type;
98 typedef csr_matrix_ref<value_type *, size_type *, size_type *, 0> tm_type;
103 std::vector<value_type> Tri_val;
104 std::vector<size_type> Tri_ind, Tri_ptr;
106 template<
typename M>
void do_ildlt(
const M& A, row_major);
107 void do_ildlt(
const Matrix& A, col_major);
111 size_type nrows(
void)
const {
return mat_nrows(U); }
112 size_type ncols(
void)
const {
return mat_ncols(U); }
113 value_type &D(size_type i) {
return Tri_val[Tri_ptr[i]]; }
114 const value_type &D(size_type i)
const {
return Tri_val[Tri_ptr[i]]; }
116 void build_with(
const Matrix& A) {
117 Tri_ptr.resize(mat_nrows(A)+1);
118 do_ildlt(A,
typename principal_orientation_type<
typename
119 linalg_traits<Matrix>::sub_orientation>::potype());
122 size_type memsize()
const {
123 return sizeof(*this) +
124 Tri_val.size() *
sizeof(value_type) +
125 (Tri_ind.size()+Tri_ptr.size()) *
sizeof(size_type);
129 template <
typename Matrix>
template<
typename M>
131 typedef typename linalg_traits<Matrix>::storage_type store_type;
132 typedef value_type T;
133 typedef typename number_traits<T>::magnitude_type R;
135 size_type Tri_loc = 0, n = mat_nrows(A), d, g, h, i, j, k;
139 R prec = default_tol(R());
140 R max_pivot = gmm::abs(A(0,0)) * prec;
142 for (
int count = 0; count < 2; ++count) {
143 if (count) { Tri_val.resize(Tri_loc); Tri_ind.resize(Tri_loc); }
144 for (Tri_loc = 0, i = 0; i < n; ++i) {
145 typedef typename linalg_traits<M>::const_sub_row_type row_type;
146 row_type row = mat_const_row(A, i);
147 typename linalg_traits<typename org_type<row_type>::t>::const_iterator
148 it = vect_const_begin(row), ite = vect_const_end(row);
150 if (count) { Tri_val[Tri_loc] = T(0); Tri_ind[Tri_loc] = i; }
153 for (k = 0; it != ite; ++it, ++k) {
154 j = index_of_it(it, k, store_type());
156 if (count) Tri_val[Tri_loc-1] = *it;
159 if (count) { Tri_val[Tri_loc] = *it; Tri_ind[Tri_loc]=j; }
163 Tri_ptr[i+1] = Tri_loc;
167 if (A(0,0) == T(0)) {
168 Tri_val[Tri_ptr[0]] = T(1);
169 GMM_WARNING2(
"pivot 0 is too small");
172 for (k = 0; k < n; k++) {
174 z = T(gmm::real(Tri_val[d])); Tri_val[d] = z;
175 if (gmm::abs(z) <= max_pivot) {
176 Tri_val[d] = z = T(1);
177 GMM_WARNING2(
"pivot " << k <<
" is too small [" << gmm::abs(z) <<
"]");
179 max_pivot = std::max(max_pivot, std::min(gmm::abs(z) * prec, R(1)));
181 for (i = d + 1; i < Tri_ptr[k+1]; ++i) Tri_val[i] /= z;
182 for (i = d + 1; i < Tri_ptr[k+1]; ++i) {
183 zz = gmm::conj(Tri_val[i] * z);
187 for (j = Tri_ptr[h] ; j < Tri_ptr[h+1]; ++j)
188 for ( ; g < Tri_ptr[k+1] && Tri_ind[g] <= Tri_ind[j]; ++g)
189 if (Tri_ind[g] == Tri_ind[j])
190 Tri_val[j] -= zz * Tri_val[g];
193 U = tm_type(&(Tri_val[0]), &(Tri_ind[0]), &(Tri_ptr[0]),
197 template <
typename Matrix>
198 void ildlt_precond<Matrix>::do_ildlt(
const Matrix& A, col_major)
199 { do_ildlt(gmm::conjugated(A), row_major()); }
201 template <
typename Matrix,
typename V1,
typename V2>
inline
202 void mult(
const ildlt_precond<Matrix>& P,
const V1 &v1, V2 &v2) {
204 gmm::lower_tri_solve(gmm::conjugated(P.U), v2,
true);
205 for (
size_type i = 0; i < mat_nrows(P.U); ++i) v2[i] /= P.D(i);
206 gmm::upper_tri_solve(P.U, v2,
true);
209 template <
typename Matrix,
typename V1,
typename V2>
inline
210 void transposed_mult(
const ildlt_precond<Matrix>& P,
const V1 &v1,V2 &v2)
213 template <
typename Matrix,
typename V1,
typename V2>
inline
214 void left_mult(
const ildlt_precond<Matrix>& P,
const V1 &v1, V2 &v2) {
216 gmm::lower_tri_solve(gmm::conjugated(P.U), v2,
true);
217 for (
size_type i = 0; i < mat_nrows(P.U); ++i) v2[i] /= P.D(i);
220 template <
typename Matrix,
typename V1,
typename V2>
inline
221 void right_mult(
const ildlt_precond<Matrix>& P,
const V1 &v1, V2 &v2)
222 {
copy(v1, v2); gmm::upper_tri_solve(P.U, v2,
true); }
224 template <
typename Matrix,
typename V1,
typename V2>
inline
225 void transposed_left_mult(
const ildlt_precond<Matrix>& P,
const V1 &v1,
228 gmm::upper_tri_solve(P.U, v2,
true);
229 for (
size_type i = 0; i < mat_nrows(P.U); ++i) v2[i] /= P.D(i);
232 template <
typename Matrix,
typename V1,
typename V2>
inline
233 void transposed_right_mult(
const ildlt_precond<Matrix>& P,
const V1 &v1,
235 {
copy(v1, v2); gmm::lower_tri_solve(gmm::conjugated(P.U), v2,
true); }
Incomplete Level 0 LDLT Preconditioner.
void copy(const L1 &l1, L2 &l2)
*/
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*/
size_t size_type
used as the common size type in the library