37 #ifndef GMM_SOLVER_CCG_H__
38 #define GMM_SOLVER_CCG_H__
45 template <
typename CMatrix,
typename CINVMatrix,
typename Matps,
47 void pseudo_inverse(
const CMatrix &C, CINVMatrix &CINV,
48 const Matps& , VectorX&) {
55 typedef VectorX TmpVec;
56 typedef typename linalg_traits<VectorX>::value_type value_type;
58 size_type nr = mat_nrows(C), nc = mat_ncols(C);
60 TmpVec d(nr), e(nr), l(nc), p(nr), q(nr), r(nr);
61 value_type rho, rho_1,
alpha;
66 d[i] = 1.0; rho = 1.0;
71 while (rho >= 1E-38) {
73 mult(gmm::transposed(C), p, l);
76 add(scaled(p, alpha), e);
77 add(scaled(q, -alpha), r);
80 add(r, scaled(p, rho / rho_1), p);
83 mult(transposed(C), e, l);
86 copy(l, mat_row(CINV, i));
93 template <
typename Matrix,
typename CMatrix,
typename Matps,
94 typename VectorX,
typename VectorB,
typename VectorF,
95 typename Preconditioner >
97 const VectorB& b,
const VectorF& f,
const Matps& PS,
98 const Preconditioner& M,
iteration &iter) {
99 typedef typename temporary_dense_vector<VectorX>::vector_type TmpVec;
100 typedef typename temporary_vector<CMatrix>::vector_type TmpCVec;
101 typedef row_matrix<TmpCVec> TmpCmat;
103 typedef typename linalg_traits<VectorX>::value_type value_type;
104 value_type rho = 1.0, rho_1, lambda, gamma;
105 TmpVec p(vect_size(x)), q(vect_size(x)), q2(vect_size(x)),
106 r(vect_size(x)), old_z(vect_size(x)), z(vect_size(x)),
108 std::vector<bool> satured(mat_nrows(C));
110 iter.set_rhsnorm(sqrt(
vect_sp(PS, b, b)));
111 if (iter.get_rhsnorm() == 0.0) iter.set_rhsnorm(1.0);
113 TmpCmat CINV(mat_nrows(C), mat_ncols(C));
114 pseudo_inverse(C, CINV, PS, x);
120 mult(A, scaled(x, -1.0), b, r);
122 bool transition =
false;
123 for (size_type i = 0; i < mat_nrows(C); ++i) {
124 value_type al =
vect_sp(mat_row(C, i), x) - f[i];
125 if (al >= -1.0E-15) {
126 if (!satured[i]) { satured[i] =
true; transition =
true; }
127 value_type bb =
vect_sp(mat_row(CINV, i), z);
128 if (bb > 0.0)
add(scaled(mat_row(C, i), -bb), z);
135 rho_1 = rho; rho =
vect_sp(PS, r, z);
137 if (iter.finished(rho))
break;
139 if (iter.get_noisy() > 0 && transition) std::cout <<
"transition\n";
140 if (transition || iter.first()) gamma = 0.0;
141 else gamma = std::max(0.0, (rho -
vect_sp(PS, old_z, z) ) / rho_1);
144 add(z, scaled(p, gamma), p);
149 lambda = rho /
vect_sp(PS, q, p);
150 for (size_type i = 0; i < mat_nrows(C); ++i)
152 value_type bb =
vect_sp(mat_row(C, i), p) - f[i];
154 lambda = std::min(lambda, (f[i]-
vect_sp(mat_row(C, i), x)) / bb);
156 add(x, scaled(p, lambda), x);
157 add(memox, scaled(x, -1.0), memox);
The Iteration object calculates whether the solution has reached the desired accuracy,...
void copy(const L1 &l1, L2 &l2)
*/
void clear(L &l)
clear (fill with zeros) a vector or matrix.
void clean(L &l, double threshold)
Clean a vector or matrix (replace near-zero entries with zeroes).
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*/
strongest_value_type< V1, V2 >::value_type vect_sp(const V1 &v1, const V2 &v2)
*/
void add(const L1 &l1, L2 &l2)
*/
Include the base gmm files.
void constrained_cg(const Matrix &A, const CMatrix &C, VectorX &x, const VectorB &b, const VectorF &f, const Matps &PS, const Preconditioner &M, iteration &iter)
Compute the minimum of under the contraint .
size_t size_type
used as the common size type in the library
size_type alpha(short_type n, short_type d)
Return the value of which is the number of monomials of a polynomial of variables and degree .