00001
00015 #ifndef TBCI_SOLVER_DIAGPRECOND_H
00016 #define TBCI_SOLVER_DIAGPRECOND_H
00017
00018 #include "precond.h"
00019
00020 NAMESPACE_TBCI
00021
00026 template <typename T, typename MatrixType = BdMatrix<T> >
00027 class DiagPreconditioner : public Preconditioner_Sig<T, MatrixType>
00028 {
00029 private:
00030 BVector<T> invDiag;
00031 bool checkdiag;
00032 public:
00033 friend class SystemMatrix;
00034
00035 DiagPreconditioner()
00036 {}
00037 DiagPreconditioner(const MatrixType &A)
00038 { update (A); }
00039 ~DiagPreconditioner() {}
00040
00041
00042 void update(const MatrixType &A)
00043 {
00044 const unsigned int rw = A.rows();
00045 if (invDiag.size() != A.rows())
00046 invDiag.resize(A.rows());
00047 BCHK(invDiag.size() == 0, VecErr, "DiagPrecond::update: NULL ptr", 0,);
00048 #ifdef ERRCHECK
00049
00050 for (unsigned int j = 0; j < rw; ++j)
00051 BCHK(A(j,j) == T(0.0), VecErr, "DiagPrecond::update: Diagonal ZERO", j,);
00052 #endif
00053 for (unsigned int i = 0; i < rw; ++i)
00054 invDiag(i) = T(1.0) / A(i,i);
00055 }
00056
00057
00058
00059
00060 inline TVector<T> solve(const Vector<T> &v) const
00061 {
00062 TVector<T> tv(v);
00063 return (solve(tv));
00064 }
00065
00066 inline TVector<T> solve(TVector<T> tv) const
00067 {
00068 const unsigned long sz = invDiag.size();
00069
00070 for (unsigned int i = 0; i < sz; ++i)
00071 tv.set(tv.get(i) * invDiag(i), i);
00072 return tv;
00073 }
00074
00075 inline TVector<T> transSolve(const Vector<T> &v) const
00076 { return solve(v); }
00077
00078 inline TVector<T> transSolve(TVector<T> tv) const
00079 { return solve(tv); }
00080
00081 };
00082
00083 NAMESPACE_END
00084
00085 #endif