63 #ifndef GMM_PRECOND_ILDLT_H
64 #define GMM_PRECOND_ILDLT_H
91 template <
typename Matrix>
95 typedef typename linalg_traits<Matrix>::value_type value_type;
96 typedef typename number_traits<value_type>::magnitude_type magnitude_type;
97 typedef csr_matrix_ref<value_type *, size_type *, size_type *, 0> tm_type;
102 std::vector<value_type> Tri_val;
103 std::vector<size_type> Tri_ind, Tri_ptr;
105 template<
typename M>
void do_ildlt(
const M& A, row_major);
106 void do_ildlt(
const Matrix& A, col_major);
110 size_type nrows(
void)
const {
return mat_nrows(U); }
111 size_type ncols(
void)
const {
return mat_ncols(U); }
112 value_type &D(size_type i) {
return Tri_val[Tri_ptr[i]]; }
113 const value_type &D(size_type i)
const {
return Tri_val[Tri_ptr[i]]; }
115 void build_with(
const Matrix& A) {
116 Tri_ptr.resize(mat_nrows(A)+1);
117 do_ildlt(A,
typename principal_orientation_type<
typename
118 linalg_traits<Matrix>::sub_orientation>::potype());
121 size_type memsize()
const {
122 return sizeof(*this) +
123 Tri_val.size() *
sizeof(value_type) +
124 (Tri_ind.size()+Tri_ptr.size()) *
sizeof(size_type);
128 template <
typename Matrix>
template<
typename M>
130 typedef typename linalg_traits<Matrix>::storage_type store_type;
131 typedef value_type T;
132 typedef typename number_traits<T>::magnitude_type R;
134 size_type Tri_loc = 0, n = mat_nrows(A), d, g, h, i, j, k;
138 R prec = default_tol(R());
139 R max_pivot = gmm::abs(A(0,0)) * prec;
141 for (
int count = 0; count < 2; ++count) {
142 if (count) { Tri_val.resize(Tri_loc); Tri_ind.resize(Tri_loc); }
143 for (Tri_loc = 0, i = 0; i < n; ++i) {
144 typedef typename linalg_traits<M>::const_sub_row_type row_type;
145 row_type row = mat_const_row(A, i);
146 typename linalg_traits<typename org_type<row_type>::t>::const_iterator
147 it = vect_const_begin(row), ite = vect_const_end(row);
149 if (count) { Tri_val[Tri_loc] = T(0); Tri_ind[Tri_loc] = i; }
152 for (k = 0; it != ite; ++it, ++k) {
153 j = index_of_it(it, k, store_type());
155 if (count) Tri_val[Tri_loc-1] = *it;
158 if (count) { Tri_val[Tri_loc] = *it; Tri_ind[Tri_loc]=j; }
162 Tri_ptr[i+1] = Tri_loc;
166 if (A(0,0) == T(0)) {
167 Tri_val[Tri_ptr[0]] = T(1);
168 GMM_WARNING2(
"pivot 0 is too small");
171 for (k = 0; k < n; k++) {
173 z = T(gmm::real(Tri_val[d])); Tri_val[d] = z;
174 if (gmm::abs(z) <= max_pivot) {
175 Tri_val[d] = z = T(1);
176 GMM_WARNING2(
"pivot " << k <<
" is too small [" << gmm::abs(z) <<
"]");
178 max_pivot = std::max(max_pivot, std::min(gmm::abs(z) * prec, R(1)));
180 for (i = d + 1; i < Tri_ptr[k+1]; ++i) Tri_val[i] /= z;
181 for (i = d + 1; i < Tri_ptr[k+1]; ++i) {
182 zz = gmm::conj(Tri_val[i] * z);
186 for (j = Tri_ptr[h] ; j < Tri_ptr[h+1]; ++j)
187 for ( ; g < Tri_ptr[k+1] && Tri_ind[g] <= Tri_ind[j]; ++g)
188 if (Tri_ind[g] == Tri_ind[j])
189 Tri_val[j] -= zz * Tri_val[g];
192 U = tm_type(&(Tri_val[0]), &(Tri_ind[0]), &(Tri_ptr[0]),
196 template <
typename Matrix>
197 void ildlt_precond<Matrix>::do_ildlt(
const Matrix& A, col_major)
198 { do_ildlt(gmm::conjugated(A), row_major()); }
200 template <
typename Matrix,
typename V1,
typename V2>
inline
201 void mult(
const ildlt_precond<Matrix>& P,
const V1 &v1, V2 &v2) {
203 gmm::lower_tri_solve(gmm::conjugated(P.U), v2,
true);
204 for (
size_type i = 0; i < mat_nrows(P.U); ++i) v2[i] /= P.D(i);
205 gmm::upper_tri_solve(P.U, v2,
true);
208 template <
typename Matrix,
typename V1,
typename V2>
inline
209 void transposed_mult(
const ildlt_precond<Matrix>& P,
const V1 &v1,V2 &v2)
212 template <
typename Matrix,
typename V1,
typename V2>
inline
213 void left_mult(
const ildlt_precond<Matrix>& P,
const V1 &v1, V2 &v2) {
215 gmm::lower_tri_solve(gmm::conjugated(P.U), v2,
true);
216 for (
size_type i = 0; i < mat_nrows(P.U); ++i) v2[i] /= P.D(i);
219 template <
typename Matrix,
typename V1,
typename V2>
inline
220 void right_mult(
const ildlt_precond<Matrix>& P,
const V1 &v1, V2 &v2)
221 {
copy(v1, v2); gmm::upper_tri_solve(P.U, v2,
true); }
223 template <
typename Matrix,
typename V1,
typename V2>
inline
224 void transposed_left_mult(
const ildlt_precond<Matrix>& P,
const V1 &v1,
227 gmm::upper_tri_solve(P.U, v2,
true);
228 for (
size_type i = 0; i < mat_nrows(P.U); ++i) v2[i] /= P.D(i);
231 template <
typename Matrix,
typename V1,
typename V2>
inline
232 void transposed_right_mult(
const ildlt_precond<Matrix>& P,
const V1 &v1,
234 {
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