00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef ITL_CYCLIC_ITERATION_INCLUDE
00013 #define ITL_CYCLIC_ITERATION_INCLUDE
00014
00015 #include <iostream>
00016 #include <boost/numeric/itl/iteration/basic_iteration.hpp>
00017
00018 namespace itl {
00019
00020 template <class Real, class OStream = std::ostream>
00021 class cyclic_iteration : public basic_iteration<Real>
00022 {
00023 typedef basic_iteration<Real> super;
00024
00025 void print_resid()
00026 {
00027 if (!this->my_quite && this->i % cycle == 0)
00028 if (this->i != last_print) {
00029 out << "iteration " << this->i << ": resid " << this->resid() << std::endl;
00030 last_print= this->i;
00031 }
00032 }
00033
00034 public:
00035
00036 template <class Vector>
00037 cyclic_iteration(const Vector& b, int max_iter_, Real tol_, Real atol_ = Real(0), int cycle_ = 100,
00038 OStream& out = std::cout)
00039 : super(b, max_iter_, tol_, atol_), cycle(cycle_), last_print(-1), out(out)
00040 {}
00041
00042 bool finished() { return super::finished(); };
00043
00044 template <typename T>
00045 bool finished(const T& r)
00046 {
00047 bool ret= super::finished(r);
00048 print_resid();
00049 return ret;
00050 }
00051
00052 operator int() { return error_code(); }
00053
00054 int error_code()
00055 {
00056 if (!this->my_quite)
00057 out << "finished! error code = " << this->error << '\n'
00058 << this->iterations() << " iterations\n"
00059 << this->resid() << " is actual final residual. \n"
00060 << this->resid()/this->normb() << " is actual relative tolerance achieved. \n"
00061 << "Relative tol: " << this->rtol_ << " Absolute tol: " << this->atol_ << '\n'
00062 << "Convergence: " << pow(this->resid()/this->normb(), 1.0 / double(this->iterations())) << std::endl;
00063 return this->error;
00064 }
00065 protected:
00066 int cycle, last_print;
00067 OStream& out;
00068 };
00069
00070
00071
00072 }
00073
00074 #endif // ITL_CYCLIC_ITERATION_INCLUDE