forked from lantian2012/CS207
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtl_test.cpp
executable file
·105 lines (90 loc) · 2.59 KB
/
mtl_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* @file mtl_test.cpp
* Test script for interfacing with MTL4 and it's linear solvers.
*/
#include <boost/numeric/mtl/mtl.hpp>
#include <boost/numeric/itl/itl.hpp>
// Define a IdentityMatrix that interfaces with MTL
struct IdentityMatrix
{
IdentityMatrix(unsigned int col):num_rows_(col), num_cols_(col){
size_ = num_rows_*num_cols_;
}
/** Helper function to perform multiplication. Allows for delayed
* evaluation of results.
* Assign::apply(a, b) resolves to an assignment operation such as * a += b, a -= b, or a = b.
* @pre @a size(v) == size(w) */
template <typename VectorIn, typename VectorOut, typename Assign>
void mult(const VectorIn& v, VectorOut& w, Assign) const{
for (unsigned int i = 0; i < num_rows_; ++i)
{
Assign::apply(w[i], v[i]);
}
}
/** Matrix - vector multiplication forwards to MTL ’s lazy mat_cvec_multiplier operator */
template <typename Vector>
mtl::vec::mat_cvec_multiplier<IdentityMatrix, Vector> operator*(const Vector& v) const {
return mtl::vec::mat_cvec_multiplier<IdentityMatrix, Vector>(*this, v);
}
unsigned int size() const {
return size_;
}
unsigned int num_rows() const{
return num_rows_;
}
private:
unsigned int size_;
unsigned int num_rows_;
unsigned int num_cols_;
};
/** The number of elements in the matrix*/
inline std::size_t size(const IdentityMatrix& A){
return A.size();
}
/** The number of rows in the matrix*/
inline std::size_t num_rows(const IdentityMatrix& A){
return A.num_rows();
}
/** The number of columns in the matrix*/
inline std::size_t num_cols(const IdentityMatrix& A){
return A.num_rows();
}
/** Traits that MTL uses to determine properties of our IdentityMatrix . */
namespace mtl{
namespace ashape{
/**Define IdentityMatrix to be a non-svalar type*/
template<>
struct ashape_aux<IdentityMatrix>
{
typedef nonscal type;
};
} //end namespace ashape
/** IdentityMatrix implements the Collection concept
* with value_type and size_type*/
template<>
struct Collection<IdentityMatrix>
{
typedef double value_type;
typedef unsigned size_type;
};
} //end namesapce mtl
int main()
{
// HW3: YOUR CODE HERE
// Construct an IdentityMatrix and "solve" Ix = b
// using MTL's conjugate gradient solver
const unsigned int N=15;
IdentityMatrix I(N);
// Create a preconditioner
itl::pc::identity<IdentityMatrix> P(I);
// Set the b such that x==2.7 is the solution
mtl::dense_vector<double> x(N, 2.7), b(N);
b = I * x;
// start with x=0
x = 0;
itl::cyclic_iteration<double> iter(b, 100, 1.e-11, 0.0, 5);
cg(I, x, b, P, iter);
//output the solve
std::cout<<x<<std::endl;
return 0;
}