00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "../config.h"
00021 #ifdef __MATRIXWRAPPER_BOOST__
00022
00023 #include "matrix_BOOST.h"
00024 #include "vector_BOOST.h"
00025
00026 #include <boost/numeric/ublas/matrix_proxy.hpp>
00027
00028 using namespace std;
00029
00030
00031 MyMatrix::Matrix() : BoostMatrix() {}
00032 MyMatrix::Matrix(int num_rows, int num_cols) : BoostMatrix(num_rows,
00033 num_cols){}
00034
00035
00036 MyMatrix::~Matrix(){}
00037
00038
00039 MyMatrix::Matrix(const MyMatrix& a) : BoostMatrix(a){}
00040
00041
00042 MyMatrix::Matrix(const BoostMatrix & a) : BoostMatrix(a){}
00043
00044 MyMatrix::Matrix(int num_rows,const RowVector& v):BoostMatrix(num_rows,v.size()){
00045 BoostMatrix & m = *this;
00046 for(unsigned int i=0;i<num_rows;i++)
00047 row(m,i).assign(v);
00048 }
00049
00050 MyRowVector MyMatrix::operator[](unsigned int i) const{
00051 return this->rowCopy(i);
00052 }
00053
00054
00055 unsigned int MyMatrix::size() const { return this->size1();}
00056 unsigned int MyMatrix::capacity() const { return this->size1();}
00057
00058
00059 unsigned int MyMatrix::rows() const { return this->size1();}
00060 unsigned int MyMatrix::columns() const { return this->size2();}
00061
00062
00063 MyMatrix& MyMatrix::operator+= (double a)
00064 {
00065 BoostMatrix & op1 = *this;
00066 op1 += boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a);
00067 return (MyMatrix&)op1;
00068 }
00069
00070 MyMatrix& MyMatrix::operator-= (double a)
00071 {
00072 BoostMatrix & op1 = (*this);
00073 op1 -= boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a);
00074 return (MyMatrix&) op1;
00075 }
00076
00077 MyMatrix& MyMatrix::operator*= (double a)
00078 {
00079 BoostMatrix & op1 = (*this);
00080 op1 *= a;
00081 return *this;
00082 }
00083
00084 MyMatrix& MyMatrix::operator/= (double a)
00085 {
00086 BoostMatrix & op1 = (*this);
00087 op1 /= a;
00088 return (MyMatrix&) op1;
00089 }
00090
00091 MyMatrix MyMatrix::operator+ (double a) const
00092 {
00093 return (MyMatrix)(((BoostMatrix)(*this)) + boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a));
00094 }
00095
00096 MyMatrix MyMatrix::operator- (double a) const
00097 {
00098 return (MyMatrix)(((BoostMatrix)(*this)) - boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a));
00099 }
00100
00101 MyMatrix MyMatrix::operator* (double a) const
00102 {
00103 const BoostMatrix& op1 = (*this);
00104 return (MyMatrix) (op1 * a);
00105 }
00106
00107 MyMatrix MyMatrix::operator/ (double a) const
00108 {
00109 const BoostMatrix& op1 = (*this);
00110 return (MyMatrix) (op1 / a);
00111 }
00112
00113 MyMatrix&
00114 MyMatrix::operator =(const MySymmetricMatrix& a)
00115 {
00116 *this =(MyMatrix) a;
00117
00118 return *this;
00119 }
00120
00121
00122 MyMatrix MyMatrix::operator- (const MyMatrix& a) const
00123 {
00124 const BoostMatrix& op1 = *this;
00125 const BoostMatrix& op2 = a;
00126
00127 return (MyMatrix)(op1 - op2);
00128 }
00129
00130 MyMatrix MyMatrix::operator+ (const MyMatrix& a) const
00131 {
00132 const BoostMatrix& op1 = *this;
00133 const BoostMatrix& op2 = a;
00134
00135 return (MyMatrix)(op1 + op2);
00136 }
00137
00138 MyMatrix MyMatrix::operator* (const MyMatrix& a) const
00139 {
00140 const BoostMatrix& op1 = *this;
00141 const BoostMatrix& op2 = a;
00142
00143 return (MyMatrix) prod(op1,op2);
00144 }
00145
00146 MyMatrix & MyMatrix::operator+= (const MyMatrix& a)
00147 {
00148 BoostMatrix & op1 = (*this);
00149 const BoostMatrix & op2 = a;
00150 op1 += op2;
00151 return (MyMatrix &) op1;
00152 }
00153
00154 MyMatrix & MyMatrix::operator-= (const MyMatrix& a)
00155 {
00156 BoostMatrix & op1 = (*this);
00157 const BoostMatrix & op2 = a;
00158 op1 -= op2;
00159 return (MyMatrix &) op1;
00160 }
00161
00162
00163
00164 MyColumnVector MyMatrix::operator* (const MyColumnVector &b) const
00165 {
00166 const BoostMatrix& op1 = (*this);
00167 return (MyColumnVector) prod(op1, ((const BoostColumnVector&)b));
00168 }
00169
00170
00171
00172 double& MyMatrix::operator()(unsigned int a, unsigned int b)
00173 {
00174 BoostMatrix & op1 = (*this);
00175 return op1(a-1,b-1);
00176 }
00177
00178 double MyMatrix::operator()(unsigned int a, unsigned int b) const
00179 {
00180 BoostMatrix op1(*this);
00181 return op1(a-1,b-1);
00182 }
00183
00184 bool MyMatrix::operator==(const MyMatrix& a) const
00185 {
00186 if (this->rows() != a.rows()) return false;
00187 if (this->columns() != a.columns()) return false;
00188 return(norm_inf((BoostMatrix)(*this)-(BoostMatrix)a) == 0);
00189 }
00190
00191
00192
00193 MyMatrix&
00194 MyMatrix::operator=(double a)
00195 {
00196 *this = (MyMatrix)boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a);
00197
00198 return *this;
00199 }
00200
00201
00202 MyRowVector MyMatrix::rowCopy(unsigned int r) const
00203 {
00204 unsigned int cols = columns();
00205 BoostRowVector temp(cols);
00206 for (unsigned int i=0; i<cols; i++)
00207 temp(i) = (*this)(r,i+1);
00208 return (MyRowVector) temp;
00209 }
00210
00211 MyColumnVector MyMatrix::columnCopy(unsigned int c) const
00212 {
00213 unsigned int ro = rows();
00214 BoostColumnVector temp(ro);
00215 for (unsigned int i=0; i<ro; i++)
00216 temp(i) = (*this)(i+1,c);
00217 return (MyColumnVector) temp;
00218 }
00219
00220
00221
00222
00223 MyMatrix MyMatrix::transpose() const
00224 {
00225 const BoostMatrix &op1 = (*this);
00226 return (MyMatrix) trans(op1);
00227 }
00228
00229 double MyMatrix::determinant() const
00230 {
00231 unsigned int r = this->rows();
00232 assert(r == this->columns());
00233 double result = 1.0;
00234 const BoostMatrix& A = (*this);
00235 switch (r)
00236 {
00237 case 1:
00238 return A(0,0);
00239 case 2:
00240 return ( ( A(0,0) * A(1,1)) - ( A(1,0) * A(0,1)) );
00241 default:
00242 BoostMatrix LU(r,r);
00243 boost::numeric::ublas::permutation_matrix<> ndx(r);
00244 noalias(LU) = A;
00245 int res = lu_factorize(LU,ndx);
00246 assert(res == 0);
00247
00248 int s = 1;
00249 for (boost::numeric::ublas::matrix<double>::size_type i=0;i<LU.size1();++i) {
00250 result *= LU(i,i);
00251 if (ndx(i)!=i) s = -s;
00252 }
00253 return result*s;
00254 }
00255 }
00256
00257
00258 MyMatrix MyMatrix::inverse() const
00259 {
00260 unsigned int r = this->rows();
00261 assert(r == this->columns());
00262 const BoostMatrix& A = (*this);
00263 BoostMatrix Ai(r,r);
00264 switch (r)
00265 {
00266 case 1:
00267 {
00268 Ai(0,0) = 1/A(0,0);
00269 break;
00270 }
00271 case 2:
00272 {
00273 double det = A(0,0)*A(1,1)-A(0,1)*A(1,0);
00274 Ai(0,0) = A(1,1)/det;
00275 Ai(1,1) = A(0,0)/det;
00276 Ai(0,1) = -A(0,1)/det;
00277 Ai(1,0) = -A(1,0)/det;
00278 break;
00279 }
00280 default:
00281 {
00282 BoostMatrix LU(r,r);
00283 boost::numeric::ublas::permutation_matrix<> ndx(r);
00284 noalias(LU) = A;
00285 int res = lu_factorize(LU,ndx);
00286 assert(res == 0);
00287 noalias(Ai) = boost::numeric::ublas::identity_matrix<double>(r);
00288 lu_substitute(LU,ndx,Ai);
00289 break;
00290 }
00291 }
00292 return Ai;
00293 }
00294
00295
00296 int
00297 MyMatrix::convertToSymmetricMatrix(MySymmetricMatrix& sym)
00298 {
00299
00300 assert(this->rows() == this->columns());
00301
00302
00303
00304 if ( sym.rows() != this->rows() )
00305 sym = MySymmetricMatrix(this->rows());
00306
00307
00308 for ( unsigned int i=0; i<this->rows(); i++ )
00309 for ( unsigned int j=0; j<=i; j++ )
00310 sym(i+1,j+1) = (*this)(i+1,j+1);
00311 return 0;
00312 }
00313
00314 void
00315 MyMatrix::resize(unsigned int i, unsigned int j, bool copy, bool initialize)
00316 {
00317 BoostMatrix & temp = (BoostMatrix &) (*this);
00318 temp.resize(i,j,copy);
00319 }
00320
00321
00322 MyMatrix MyMatrix::sub(int i_start, int i_end, int j_start , int j_end) const
00323 {
00324 MyMatrix submatrix(i_end-i_start+1, j_end-j_start+1);
00325 for (int i=i_start; i<=i_end; i++)
00326 for (int j=j_start; j<=j_end; j++)
00327 submatrix(i-i_start+1,j-j_start+1) = (*this)(i,j);
00328
00329 return submatrix;
00330 }
00331
00333
00335
00336 MySymmetricMatrix::SymmetricMatrix() : BoostSymmetricMatrix() {}
00337 MySymmetricMatrix::SymmetricMatrix(int n) : BoostSymmetricMatrix(n) {}
00338 MySymmetricMatrix::SymmetricMatrix(int num_rows,const RowVector& v):BoostSymmetricMatrix(num_rows,v.size()){
00339 BoostSymmetricMatrix & m = *this;
00340 for(unsigned int i=0;i<num_rows;i++)
00341 row(m,i).assign(v);
00342 }
00343
00344 MyRowVector MySymmetricMatrix::operator[](unsigned int i) const{
00345 return this->rowCopy(i);
00346 }
00347
00348
00349
00350
00351 MySymmetricMatrix::SymmetricMatrix(const SymmetricMatrix& a) : BoostSymmetricMatrix(a){}
00352 MySymmetricMatrix::SymmetricMatrix(const BoostSymmetricMatrix & a) : BoostSymmetricMatrix(a){}
00353
00354
00355 MySymmetricMatrix::~SymmetricMatrix(){}
00356
00357
00358 unsigned int MySymmetricMatrix::size() const { return this->size1();}
00359 unsigned int MySymmetricMatrix::capacity() const { return this->size1();}
00360
00361
00362 unsigned int MySymmetricMatrix::rows() const { return this->size1();}
00363 unsigned int MySymmetricMatrix::columns() const { return this->size2();}
00364
00365
00366 MyRowVector MySymmetricMatrix::rowCopy(unsigned int r) const
00367 {
00368
00369 unsigned int cols = columns();
00370 BoostRowVector temp(cols);
00371 for (unsigned int i=0; i<cols; i++)
00372 temp(i) = (*this)(r,i+1);
00373 return (MyRowVector) temp;
00374 }
00375
00376 MySymmetricMatrix MySymmetricMatrix::transpose() const {return (*this);}
00377
00378 MySymmetricMatrix MySymmetricMatrix::inverse() const
00379 {
00380 unsigned int r = this->rows();
00381 assert(r == this->columns());
00382 const BoostMatrix& A = (*this);
00383 BoostSymmetricMatrix Ai(r,r);
00384 switch (r)
00385 {
00386 case 1:
00387 {
00388 Ai(0,0) = 1/A(0,0);
00389 break;
00390 }
00391 case 2:
00392 {
00393 double det = A(0,0)*A(1,1)-A(0,1)*A(1,0);
00394 Ai(0,0) = A(1,1)/det;
00395 Ai(1,1) = A(0,0)/det;
00396 Ai(0,1) = -A(0,1)/det;
00397 Ai(1,0) = -A(1,0)/det;
00398 break;
00399 }
00400 default:
00401 {
00402 BoostSymmetricMatrix LU(r,r);
00403 boost::numeric::ublas::permutation_matrix<> ndx(r);
00404 noalias(LU) = A;
00405 int res = lu_factorize(LU,ndx);
00406 assert(res == 0);
00407 noalias(Ai) = boost::numeric::ublas::identity_matrix<double>(r);
00408 lu_substitute(LU,ndx,Ai);
00409 break;
00410 }
00411 }
00412 return Ai;
00413 }
00414
00415 double MySymmetricMatrix::determinant() const
00416 {
00417 unsigned int r = this->rows();
00418 assert(r == this->columns());
00419 const BoostMatrix& A = (*this);
00420 switch (r)
00421 {
00422 case 1:
00423 {
00424 return A(0,0);
00425 }
00426 case 2:
00427 {
00428 return A(0,0)*A(1,1)-A(0,1)*A(1,0);
00429 }
00430 default:
00431 {
00432 BoostSymmetricMatrix LU(r,r);
00433 boost::numeric::ublas::permutation_matrix<> ndx(r);
00434 noalias(LU) = A;
00435 int res = lu_factorize(LU,ndx);
00436 assert(res == 0);
00437
00438 double result = 1.0;
00439 int s = 1;
00440 for (boost::numeric::ublas::matrix<double>::size_type i=0;i<LU.size1();++i) {
00441 result *= LU(i,i);
00442 if (ndx(i)!=i) s = -s;
00443 }
00444 return result*s;
00445 }
00446 }
00447 }
00448
00449
00450
00451 MySymmetricMatrix& MySymmetricMatrix::operator=(const double a)
00452 {
00453 *this = (MySymmetricMatrix)boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a);
00454
00455 return *this;
00456 }
00457
00458
00459
00460 MySymmetricMatrix& MySymmetricMatrix::operator +=(double a)
00461 {
00462 BoostSymmetricMatrix & op1 = *this;
00463 op1 += boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a);
00464 return (MySymmetricMatrix&)op1;
00465 }
00466
00467 MySymmetricMatrix& MySymmetricMatrix::operator -=(double a)
00468 {
00469 BoostSymmetricMatrix & op1 = *this;
00470 op1 -= boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a);
00471 return (MySymmetricMatrix&)op1;
00472 }
00473
00474 MySymmetricMatrix& MySymmetricMatrix::operator *=(double b)
00475 {
00476 BoostSymmetricMatrix & op1 = (*this);
00477 op1 *= b;
00478 return (MySymmetricMatrix&) op1;
00479 }
00480
00481 MySymmetricMatrix& MySymmetricMatrix::operator /=(double b)
00482 {
00483 BoostSymmetricMatrix & op1 = (*this);
00484 op1 /= b;
00485 return (MySymmetricMatrix&) op1;
00486 }
00487
00488 MySymmetricMatrix MySymmetricMatrix::operator +(double a) const
00489 {
00490 return (MySymmetricMatrix)(((BoostSymmetricMatrix)(*this)) + boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a));
00491 }
00492
00493 MySymmetricMatrix MySymmetricMatrix::operator -(double a) const
00494 {
00495 return (MySymmetricMatrix)(((BoostSymmetricMatrix)(*this)) - boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a));
00496 }
00497
00498 MySymmetricMatrix MySymmetricMatrix::operator *(double b) const
00499 {
00500 const BoostSymmetricMatrix& op1 = (*this);
00501 return (MySymmetricMatrix) (op1 * b);
00502 }
00503
00504 MySymmetricMatrix MySymmetricMatrix::operator /(double b) const
00505 {
00506 const BoostSymmetricMatrix& op1 = (*this);
00507 return (MySymmetricMatrix) (op1 / b);
00508 }
00509
00510
00511
00512
00513
00514 MyMatrix& MySymmetricMatrix::operator +=(const MyMatrix& a)
00515 {
00516 BoostSymmetricMatrix & op1 = (*this);
00517 op1 += a;
00518 return (MyMatrix &) op1;
00519 }
00520
00521 MyMatrix& MySymmetricMatrix::operator -=(const MyMatrix& a)
00522 {
00523 BoostSymmetricMatrix & op1 = (*this);
00524 op1 -= a;
00525 return (MyMatrix &) op1;
00526 }
00527
00528
00529 MyMatrix MySymmetricMatrix::operator+ (const MyMatrix &a) const
00530 {
00531 const BoostSymmetricMatrix& op1 = *this;
00532 const BoostMatrix& op2 = a;
00533
00534 return (MyMatrix) (op1 + op2);
00535 }
00536
00537 MyMatrix MySymmetricMatrix::operator- (const MyMatrix &a) const
00538 {
00539 const BoostSymmetricMatrix& op1 = *this;
00540 const BoostMatrix& op2 = a;
00541
00542 return (MyMatrix) (op1 - op2);
00543 }
00544
00545 MyMatrix MySymmetricMatrix::operator* (const MyMatrix &a) const
00546 {
00547 const BoostSymmetricMatrix& op1 = *this;
00548 const BoostMatrix& op2 = a;
00549
00550 return (MyMatrix) prod(op1, op2);
00551 }
00552
00553
00554
00555
00556 MySymmetricMatrix& MySymmetricMatrix::operator +=(const MySymmetricMatrix& a)
00557 {
00558 BoostSymmetricMatrix & op1 = (*this);
00559 const BoostSymmetricMatrix & op2 = a;
00560 op1 += op2;
00561 return (MySymmetricMatrix &) op1;
00562 }
00563
00564 MySymmetricMatrix& MySymmetricMatrix::operator -=(const MySymmetricMatrix& a)
00565 {
00566 BoostSymmetricMatrix & op1 = (*this);
00567 const BoostSymmetricMatrix & op2 = a;
00568 op1 -= op2;
00569 return (MySymmetricMatrix &) op1;
00570 }
00571
00572 MySymmetricMatrix MySymmetricMatrix::operator+ (const MySymmetricMatrix &a) const
00573 {
00574 const BoostSymmetricMatrix& op1 = *this;
00575 const BoostSymmetricMatrix& op2 = a;
00576
00577 return (MySymmetricMatrix) (op1 + op2);
00578 }
00579
00580 MySymmetricMatrix MySymmetricMatrix::operator- (const MySymmetricMatrix &a) const
00581 {
00582 const BoostSymmetricMatrix& op1 = *this;
00583 const BoostSymmetricMatrix& op2 = a;
00584
00585 return (MySymmetricMatrix) (op1 - op2);
00586 }
00587
00588 MyMatrix MySymmetricMatrix::operator* (const MySymmetricMatrix &a) const
00589 {
00590 const BoostSymmetricMatrix& op1 = *this;
00591 const BoostSymmetricMatrix& op2 = a;
00592
00593 return (MyMatrix) prod(op1, op2);
00594 }
00595
00596
00597
00598
00599 MyColumnVector MySymmetricMatrix::operator* (const MyColumnVector &b) const
00600 {
00601 const BoostSymmetricMatrix& op1 = (BoostSymmetricMatrix) *this;
00602 return (MyColumnVector) prod(op1, ((const BoostColumnVector&)b));
00603 }
00604
00605 void MySymmetricMatrix::multiply (const MyColumnVector &b, MyColumnVector &result) const
00606 {
00607 const BoostSymmetricMatrix& op1 = (BoostSymmetricMatrix) *this;
00608 result = (MyColumnVector) prod(op1, ((const BoostColumnVector&)b));
00609 }
00610
00611 MyMatrix MySymmetricMatrix::sub(int i_start, int i_end, int j_start , int j_end) const
00612 {
00613 MyMatrix submatrix(i_end-i_start+1, j_end-j_start+1);
00614 for (int i=i_start; i<=i_end; i++)
00615 for (int j=j_start; j<=j_end; j++)
00616 submatrix(i-i_start+1,j-j_start+1) = (*this)(i,j);
00617
00618 return submatrix;
00619 }
00620
00621
00622
00623 double& MySymmetricMatrix::operator()(unsigned int a, unsigned int b)
00624 {
00625 BoostSymmetricMatrix & op1 = (*this);
00626 return op1(a-1,b-1);
00627 }
00628
00629 double MySymmetricMatrix::operator()(unsigned int a, unsigned int b) const
00630 {
00631 BoostSymmetricMatrix op1(*this);
00632 return op1(a-1,b-1);
00633 }
00634
00635 bool MySymmetricMatrix::operator==(const MySymmetricMatrix& a) const
00636 {
00637 if (this->rows() != a.rows()) return false;
00638 if (this->columns() != a.columns()) return false;
00639 return(norm_inf((BoostSymmetricMatrix)(*this)-(BoostSymmetricMatrix)a) == 0);
00640 }
00641
00642 void
00643 MySymmetricMatrix::resize(unsigned int i, bool copy, bool initialize)
00644 {
00645 BoostSymmetricMatrix & temp = (BoostSymmetricMatrix &) (*this);
00646 temp.resize(i, copy);
00647 }
00648
00649
00650 #endif