00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #pragma warning(disable : 4786)
00026
00027 #include "itl_bicgstab.h"
00028 #include "itl/interface/mtl.h"
00029 #include "itl/krylov/bicgstab.h"
00030 #include "itl/preconditioner/diagonal.h"
00031 #include "itl/preconditioner/ssor.h"
00032
00033 #include "mtl/matrix.h"
00034 #include "mtl/mtl.h"
00035 #include "mtl/utils.h"
00036
00037 #include "real.h"
00038
00039 #include <cassert>
00040 #include <iostream>
00041
00042 using namespace itl;
00043 using namespace mtl;
00044
00045 using sheep::Real;
00046
00047 typedef matrix<Real, rectangle<>, array<compressed<> >, row_major>::type mtl_matrix;
00048
00049 int sheep::itl_bicgstab(const sheep::Matrix &a, const sheep::Vector &b, sheep::Vector *x) {
00050 assert(b.GetDimension() == a.GetColumns());
00051 assert(x);
00052 assert(x->GetDimension() == a.GetColumns());
00053
00054
00055
00056 mtl_matrix mtl_a(a.GetRows(), a.GetColumns());
00057
00058 for(int i = 0; i < mtl_a.nrows(); i++) {
00059 for(int j = 0; j < mtl_a.ncols(); j++) {
00060 mtl_a(i, j) = a(i, j);
00061 }
00062 }
00063
00064 #ifdef VERBOSE
00065 std::cout << "A:" << std::endl;
00066 for(int i = 0; i < mtl_a.nrows(); i++) {
00067 for(int j = 0; j < mtl_a.ncols(); j++) {
00068 std::cout << mtl_a(i, j) << "\t";
00069 }
00070 std::cout << std::endl;
00071 }
00072 std::cout << std::endl;
00073 #endif // VERBOSE
00074
00075
00076
00077 dense1D<Real> mtl_b(mtl_a.ncols());
00078
00079 int idx = 0;
00080
00081 for(dense1D<Real>::iterator i = mtl_b.begin(); i != mtl_b.end(); i++) {
00082 *i = b[idx++];
00083 }
00084
00085 #ifdef VERBOSE
00086 std::cout << "B:" << std::endl;
00087 for(dense1D<Real>::iterator i = mtl_b.begin(); i != mtl_b.end(); i++) {
00088 std::cout << *i << "\t";
00089 }
00090 std::cout << std::endl;
00091 #endif // VERBOSE
00092
00093
00094
00095 dense1D<Real> mtl_x(mtl_a.nrows(), Real(0));
00096
00097
00098 diagonal_precond<mtl_matrix> precond(mtl_a);
00099
00100
00101 basic_iteration<Real> iter(mtl_b, 50, 1e-6);
00102
00103 bicgstab(mtl_a, mtl_x, mtl_b, precond(), iter);
00104
00105 #ifdef VERBOSE
00106 std::cout << "X:" << std::endl;
00107 for(dense1D<Real>::iterator i = mtl_x.begin(); i != mtl_x.end(); i++) {
00108 std::cout << *i << "\t";
00109 }
00110 std::cout << std::endl;
00111 #endif // VERBOSE
00112
00113
00114
00115 idx = 0;
00116
00117 for(dense1D<Real>::iterator i = mtl_x.begin(); i != mtl_x.end(); i++) {
00118 (*x)[idx++] = *i;
00119 }
00120
00121 return 0;
00122 }