4 #include <boost/numeric/ublas/vector.hpp> 5 #include <boost/numeric/ublas/matrix.hpp> 6 #include <boost/qvm/mat_operations.hpp> 7 #include <boost/numeric/ublas/vector_proxy.hpp> 8 #include <boost/numeric/ublas/triangular.hpp> 9 #include <boost/numeric/ublas/lu.hpp> 10 #include <boost/numeric/ublas/io.hpp> 21 boost::numeric::ublas::matrix<double> value;
22 boost::numeric::ublas::range r1;
23 boost::numeric::ublas::range r2;
24 boost::numeric::ublas::matrix<double>valBack;
25 bool subMatrixCopy =
false;
28 bool InvertMatrix(
const boost::numeric::ublas::matrix<double>& input, boost::numeric::ublas::matrix<double>& inverse) {
29 using namespace boost::numeric::ublas;
30 typedef permutation_matrix<std::size_t> pmatrix;
32 matrix<double> A(input);
34 pmatrix pm(A.size1());
36 int res = lu_factorize(A,pm);
37 if( res != 0 )
return false;
39 inverse.assign(boost::numeric::ublas::identity_matrix<double>(A.size1()));
41 lu_substitute(A, pm, inverse);
45 void setSystemValue(boost::numeric::ublas::matrix<double> v){
53 value = boost::numeric::ublas::zero_matrix<double>(rowCount,colCount);
56 matrixBoost(boost::numeric::ublas::matrix<double> value):value(value){}
58 static matrixBoost Identity(
int dimension,
int dimension2){
59 return matrixBoost(boost::numeric::ublas::identity_matrix<double>(dimension,dimension2));
62 matrixBoost transpose(){
63 matrixBoost t(boost::numeric::ublas::trans(value));
67 matrixBoost inverse(){
68 boost::numeric::ublas::matrix<double> inverse(value.size1(),value.size2());
69 InvertMatrix(value,inverse);
70 matrixBoost t(inverse);
74 boost::numeric::ublas::matrix<double> getSystemValue()
const{
78 matrixBoost & block(
int i1,
int i2,
int l1,
int l2){
80 std::swap(valBack,value);
83 r1 = boost::numeric::ublas::range(i1,i1+l1);
84 r2 = boost::numeric::ublas::range(i2,i2+l2);
85 valBack = boost::numeric::ublas::project(value,r1,r2);
86 std::swap(valBack,value);
90 matrixBoost &operator=(
const matrixBoost other){
92 subMatrixCopy =
false;
93 boost::numeric::ublas::project(valBack,r1,r2) = other.getSystemValue();
94 std::swap(valBack,value);
96 value = other.getSystemValue();
101 static void lowerTriangulate(matrixBoost & val){
102 boost::numeric::ublas::matrix<double> in= val.getSystemValue();
103 int rows = in.size1();
104 int cols = in.size2();
105 for(
int i = 0; i < rows; i++){
106 int length = cols - i;
107 boost::numeric::ublas::vector<double> v = (row(in,i));
108 v = boost::numeric::ublas::subrange(v,i,cols);
109 double mew = boost::numeric::ublas::norm_2(v);
111 double beta = v(0) + (-2*(v(0) < 0)+1)*mew;
115 boost::numeric::ublas::range t1(i,rows);
116 boost::numeric::ublas::range t2(i,cols);
117 boost::numeric::ublas::vector<double> w = -2.0/
118 (boost::numeric::ublas::norm_2(v)*boost::numeric::ublas::norm_2(v))*
119 (prod(project(in,t1,t2),v));
121 boost::numeric::ublas::matrix<double> vM(v.size(),1);
122 for(
int i = 0; i < v.size(); i++){
125 boost::numeric::ublas::matrix<double> wM(w.size(),1);
126 for(
int i = 0; i < w.size(); i++){
129 project(in,t1,t2) = project(in,t1,t2) + prod(wM,boost::numeric::ublas::trans(vM));
131 val.setSystemValue(in);
137 boost::numeric::ublas::vector<double> value;
142 vectorBoost(boost::numeric::ublas::vector<double> value):value(value){}
144 boost::numeric::ublas::vector<double> getSystemValue()
const{
148 static vectorBoost randomVector(
int dimension,uint32_t seed){
149 std::mt19937 generator(seed);
150 std::normal_distribution<double> distribution(0.0,1.0);
151 boost::numeric::ublas::vector<double> tmp(dimension);
152 for(
int i = 0; i < dimension; i++){
153 tmp(i) = distribution(generator);
159 boost::numeric::ublas::matrix<double> Alocal = A.getSystemValue();
160 boost::numeric::ublas::vector<double> blocal = b.getSystemValue();
161 boost::numeric::ublas::permutation_matrix<size_t> pm(Alocal.size1());
163 boost::numeric::ublas::lu_factorize(Alocal,pm);
164 boost::numeric::ublas::lu_substitute(Alocal, pm, blocal);
171 vectorBoost res(lhs.getSystemValue() - rhs.getSystemValue());
176 vectorBoost res(lhs.getSystemValue() + rhs.getSystemValue());
180 std::ostream& operator<<(std::ostream& stream,
const vectorBoost & vec) {
181 boost::numeric::ublas::vector<double> v = vec.getSystemValue();
182 for(
int i = 0; i < v.size()-1; i++){
183 stream<<double(v(i))<<
'\n';
185 stream<<v(v.size()-1);
193 matrixBoost res(boost::numeric::ublas::prod(lhs.getSystemValue(),rhs.getSystemValue()));
198 matrixBoost res(lhs.getSystemValue() + rhs.getSystemValue());
203 matrixBoost res(lhs.getSystemValue() - rhs.getSystemValue());
207 std::ostream& operator<<(std::ostream& stream,
const matrixBoost & mat) {
208 boost::numeric::ublas::matrix<double> m = mat.getSystemValue();
209 for(
int i = 0; i < m.size1(); i++){
210 for(
int j = 0; j < m.size2(); j++){
223 vectorBoost res(boost::numeric::ublas::prod(lhs.getSystemValue(),rhs.getSystemValue()));
228 vectorBoost res(boost::numeric::ublas::prod(lhs.getSystemValue(),rhs.getSystemValue()));