20 #include "../config.h" 21 #ifdef __MATRIXWRAPPER_BOOST__ 26 #include <boost/numeric/ublas/matrix_proxy.hpp> 31 MyMatrix::Matrix() : BoostMatrix() {}
32 MyMatrix::Matrix(
int num_rows,
int num_cols) : BoostMatrix(num_rows,
39 MyMatrix::Matrix(
const MyMatrix& a) : BoostMatrix(a){}
42 MyMatrix::Matrix(
const BoostMatrix & a) : BoostMatrix(a){}
44 MyMatrix::Matrix(
int num_rows,
const RowVector& v):BoostMatrix(num_rows,v.size()){
45 BoostMatrix & m = *
this;
46 for(
unsigned int i=0;i<num_rows;i++)
50 MyRowVector MyMatrix::operator[](
unsigned int i)
const{
51 return this->rowCopy(i);
55 unsigned int MyMatrix::size()
const {
return this->size1();}
56 unsigned int MyMatrix::capacity()
const {
return this->size1();}
59 unsigned int MyMatrix::rows()
const {
return this->size1();}
60 unsigned int MyMatrix::columns()
const {
return this->size2();}
63 MyMatrix& MyMatrix::operator+= (
double a)
65 BoostMatrix & op1 = *
this;
66 op1 += boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a);
70 MyMatrix& MyMatrix::operator-= (
double a)
72 BoostMatrix & op1 = (*this);
73 op1 -= boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a);
77 MyMatrix& MyMatrix::operator*= (
double a)
79 BoostMatrix & op1 = (*this);
84 MyMatrix& MyMatrix::operator/= (
double a)
86 BoostMatrix & op1 = (*this);
91 MyMatrix MyMatrix::operator+ (
double a)
const 93 return (
MyMatrix)(((BoostMatrix)(*
this)) + boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a));
96 MyMatrix MyMatrix::operator- (
double a)
const 98 return (
MyMatrix)(((BoostMatrix)(*
this)) - boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a));
101 MyMatrix MyMatrix::operator* (
double a)
const 103 const BoostMatrix& op1 = (*this);
107 MyMatrix MyMatrix::operator/ (
double a)
const 109 const BoostMatrix& op1 = (*this);
124 const BoostMatrix& op1 = *
this;
125 const BoostMatrix& op2 = a;
132 const BoostMatrix& op1 = *
this;
133 const BoostMatrix& op2 = a;
140 const BoostMatrix& op1 = *
this;
141 const BoostMatrix& op2 = a;
148 BoostMatrix & op1 = (*this);
149 const BoostMatrix & op2 = a;
156 BoostMatrix & op1 = (*this);
157 const BoostMatrix & op2 = a;
166 const BoostMatrix& op1 = (*this);
172 double& MyMatrix::operator()(
unsigned int a,
unsigned int b)
174 BoostMatrix & op1 = (*this);
178 double MyMatrix::operator()(
unsigned int a,
unsigned int b)
const 180 BoostMatrix op1(*
this);
184 bool MyMatrix::operator==(
const MyMatrix& a)
const 186 if (this->rows() != a.rows())
return false;
187 if (this->columns() != a.columns())
return false;
188 return(norm_inf((BoostMatrix)(*
this)-(BoostMatrix)a) == 0);
194 MyMatrix::operator=(
double a)
196 *
this = (
MyMatrix)boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a);
202 MyRowVector MyMatrix::rowCopy(
unsigned int r)
const 204 unsigned int cols = columns();
205 BoostRowVector temp(cols);
206 for (
unsigned int i=0; i<cols; i++)
207 temp(i) = (*this)(r,i+1);
213 unsigned int ro = rows();
214 BoostColumnVector temp(ro);
215 for (
unsigned int i=0; i<ro; i++)
216 temp(i) = (*this)(i+1,c);
223 MyMatrix MyMatrix::transpose()
const 225 const BoostMatrix &op1 = (*this);
229 double MyMatrix::determinant()
const 231 unsigned int r = this->rows();
232 assert(r == this->columns());
234 const BoostMatrix& A = (*this);
240 return ( ( A(0,0) * A(1,1)) - ( A(1,0) * A(0,1)) );
243 boost::numeric::ublas::permutation_matrix<> ndx(r);
245 int res = lu_factorize(LU,ndx);
249 for (boost::numeric::ublas::matrix<double>::size_type i=0;i<LU.size1();++i) {
251 if (ndx(i)!=i) s = -s;
260 unsigned int r = this->rows();
261 assert(r == this->columns());
262 const BoostMatrix& A = (*this);
273 double det = A(0,0)*A(1,1)-A(0,1)*A(1,0);
274 Ai(0,0) = A(1,1)/det;
275 Ai(1,1) = A(0,0)/det;
276 Ai(0,1) = -A(0,1)/det;
277 Ai(1,0) = -A(1,0)/det;
283 boost::numeric::ublas::permutation_matrix<> ndx(r);
285 int res = lu_factorize(LU,ndx);
287 noalias(Ai) = boost::numeric::ublas::identity_matrix<double>(r);
288 lu_substitute(LU,ndx,Ai);
300 assert(this->rows() == this->columns());
304 if ( sym.rows() != this->rows() )
308 for (
unsigned int i=0; i<this->rows(); i++ )
309 for (
unsigned int j=0; j<=i; j++ )
310 sym(i+1,j+1) = (*this)(i+1,j+1);
315 MyMatrix::resize(
unsigned int i,
unsigned int j,
bool copy,
bool initialize)
317 BoostMatrix & temp = (BoostMatrix &) (*
this);
318 temp.resize(i,j,copy);
322 MyMatrix MyMatrix::sub(
int i_start,
int i_end,
int j_start ,
int j_end)
const 324 MyMatrix submatrix(i_end-i_start+1, j_end-j_start+1);
325 for (
int i=i_start; i<=i_end; i++)
326 for (
int j=j_start; j<=j_end; j++)
327 submatrix(i-i_start+1,j-j_start+1) = (*this)(i,j);
336 MySymmetricMatrix::SymmetricMatrix() : BoostSymmetricMatrix() {}
337 MySymmetricMatrix::SymmetricMatrix(
int n) : BoostSymmetricMatrix(n) {}
338 MySymmetricMatrix::SymmetricMatrix(
int num_rows,
const RowVector& v):BoostSymmetricMatrix(num_rows,v.size()){
339 BoostSymmetricMatrix & m = *
this;
340 for(
unsigned int i=0;i<num_rows;i++)
344 MyRowVector MySymmetricMatrix::operator[](
unsigned int i)
const{
345 return this->rowCopy(i);
351 MySymmetricMatrix::SymmetricMatrix(
const SymmetricMatrix& a) : BoostSymmetricMatrix(a){}
352 MySymmetricMatrix::SymmetricMatrix(
const BoostSymmetricMatrix & a) : BoostSymmetricMatrix(a){}
355 MySymmetricMatrix::~SymmetricMatrix(){}
358 unsigned int MySymmetricMatrix::size()
const {
return this->size1();}
359 unsigned int MySymmetricMatrix::capacity()
const {
return this->size1();}
362 unsigned int MySymmetricMatrix::rows()
const {
return this->size1();}
363 unsigned int MySymmetricMatrix::columns()
const {
return this->size2();}
366 MyRowVector MySymmetricMatrix::rowCopy(
unsigned int r)
const 369 unsigned int cols = columns();
370 BoostRowVector temp(cols);
371 for (
unsigned int i=0; i<cols; i++)
372 temp(i) = (*this)(r,i+1);
380 unsigned int r = this->rows();
381 assert(r == this->columns());
382 const BoostMatrix& A = (*this);
383 BoostSymmetricMatrix Ai(r,r);
393 double det = A(0,0)*A(1,1)-A(0,1)*A(1,0);
394 Ai(0,0) = A(1,1)/det;
395 Ai(1,1) = A(0,0)/det;
396 Ai(0,1) = -A(0,1)/det;
397 Ai(1,0) = -A(1,0)/det;
402 BoostSymmetricMatrix LU(r,r);
403 boost::numeric::ublas::permutation_matrix<> ndx(r);
405 int res = lu_factorize(LU,ndx);
407 noalias(Ai) = boost::numeric::ublas::identity_matrix<double>(r);
408 lu_substitute(LU,ndx,Ai);
415 double MySymmetricMatrix::determinant()
const 417 unsigned int r = this->rows();
418 assert(r == this->columns());
419 const BoostMatrix& A = (*this);
428 return A(0,0)*A(1,1)-A(0,1)*A(1,0);
432 BoostSymmetricMatrix LU(r,r);
433 boost::numeric::ublas::permutation_matrix<> ndx(r);
435 int res = lu_factorize(LU,ndx);
440 for (boost::numeric::ublas::matrix<double>::size_type i=0;i<LU.size1();++i) {
442 if (ndx(i)!=i) s = -s;
453 *
this = (
MySymmetricMatrix)boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a);
462 BoostSymmetricMatrix & op1 = *
this;
463 op1 += boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a);
469 BoostSymmetricMatrix & op1 = *
this;
470 op1 -= boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a);
476 BoostSymmetricMatrix & op1 = (*this);
483 BoostSymmetricMatrix & op1 = (*this);
490 return (
MySymmetricMatrix)(((BoostSymmetricMatrix)(*
this)) + boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a));
495 return (
MySymmetricMatrix)(((BoostSymmetricMatrix)(*
this)) - boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a));
500 const BoostSymmetricMatrix& op1 = (*this);
506 const BoostSymmetricMatrix& op1 = (*this);
516 BoostSymmetricMatrix & op1 = (*this);
523 BoostSymmetricMatrix & op1 = (*this);
531 const BoostSymmetricMatrix& op1 = *
this;
532 const BoostMatrix& op2 = a;
539 const BoostSymmetricMatrix& op1 = *
this;
540 const BoostMatrix& op2 = a;
547 const BoostSymmetricMatrix& op1 = *
this;
548 const BoostMatrix& op2 = a;
558 BoostSymmetricMatrix & op1 = (*this);
559 const BoostSymmetricMatrix & op2 = a;
566 BoostSymmetricMatrix & op1 = (*this);
567 const BoostSymmetricMatrix & op2 = a;
574 const BoostSymmetricMatrix& op1 = *
this;
575 const BoostSymmetricMatrix& op2 = a;
582 const BoostSymmetricMatrix& op1 = *
this;
583 const BoostSymmetricMatrix& op2 = a;
590 const BoostSymmetricMatrix& op1 = *
this;
591 const BoostSymmetricMatrix& op2 = a;
601 const BoostSymmetricMatrix& op1 = (BoostSymmetricMatrix) *
this;
607 const BoostSymmetricMatrix& op1 = (BoostSymmetricMatrix) *
this;
608 result = (
MyColumnVector) prod(op1, ((
const BoostColumnVector&)b));
611 MyMatrix MySymmetricMatrix::sub(
int i_start,
int i_end,
int j_start ,
int j_end)
const 613 MyMatrix submatrix(i_end-i_start+1, j_end-j_start+1);
614 for (
int i=i_start; i<=i_end; i++)
615 for (
int j=j_start; j<=j_end; j++)
616 submatrix(i-i_start+1,j-j_start+1) = (*this)(i,j);
623 double& MySymmetricMatrix::operator()(
unsigned int a,
unsigned int b)
625 BoostSymmetricMatrix & op1 = (*this);
629 double MySymmetricMatrix::operator()(
unsigned int a,
unsigned int b)
const 631 BoostSymmetricMatrix op1(*
this);
637 if (this->rows() != a.rows())
return false;
638 if (this->columns() != a.columns())
return false;
639 return(norm_inf((BoostSymmetricMatrix)(*
this)-(BoostSymmetricMatrix)a) == 0);
643 MySymmetricMatrix::resize(
unsigned int i,
bool copy,
bool initialize)
645 BoostSymmetricMatrix & temp = (BoostSymmetricMatrix &) (*
this);
646 temp.resize(i, copy);
#define MySymmetricMatrix