/* * ===================================================================================== * * Filename: conjugate_gradient.cc * * Description: * * Version: 1.0 * Created: 06/21/2007 01:13:30 PM EDT * Revision: none * Compiler: gcc * * Author: Nikolaos Vasiloglou (NV), nvasil@ieee.org * Company: Georgia Tech Fastlab-ESP Lab * * ===================================================================================== */ #ifndef CONJUGATE_MATRIX_IMPL_H_ #define CONJUGATE_MATRIX_IMPL_H_ #include #include #include "u/nvasil/sparse/sparse_matrix.h" namespace sparse { // solves the problem A*x=b // where A is sparse semipositive definite template void ConjugateGradient(Matrix &A, T *b, T* x, T tolerance) { index_t dimension=A.get_dimension(); T* r = NewVector(dimension); T* p = NewVector(dimension); A.Multiply(x,r); // r=A*x VectorMinus(b, r, dimension, r); // r=b-r; memcpy(p, r, dimension*sizeof(T)); T *temp1=NewVector(dimension); while(true) { A.Multiply(p, temp1); // temp1=A*p; T rr=VectorDotProduct(r, r, dimension); T alpha=rr/VectorDotProduct(temp1, p, dimension); // a=(r,r)/(A*p,p) VectorPlusTimes(x, alpha, p, dimension, x); // x=x+a*p T error=alpha*alpha*VectorDotProduct(p, p, dimension)/ VectorDotProduct(x, x, dimension); printf("Error %lg...\n", (double)error); if (error