matrix_BOOST.cpp
Go to the documentation of this file.
1 // $Id: matrix_BOOST.cpp 27906 2007-04-27 11:50:53Z wmeeusse $
2 // Copyright (C) 2002 Klaas Gadeyne <first dot last at gmail dot com>
3 
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 
20 #include "../config.h"
21 #ifdef __MATRIXWRAPPER_BOOST__
22 
23 #include "matrix_BOOST.h"
24 #include "vector_BOOST.h"
25 
26 #include <boost/numeric/ublas/matrix_proxy.hpp>
27 
28 using namespace std;
29 
30 // Passing the constructor arguments...
31 MyMatrix::Matrix() : BoostMatrix() {}
32 MyMatrix::Matrix(int num_rows, int num_cols) : BoostMatrix(num_rows,
33  num_cols){}
34 
35 // Destructor
36 MyMatrix::~Matrix(){}
37 
38 // Copy constructor
39 MyMatrix::Matrix(const MyMatrix& a) : BoostMatrix(a){}
40 
41 // ill-designed
42 MyMatrix::Matrix(const BoostMatrix & a) : BoostMatrix(a){}
43 
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++)
47  row(m,i).assign(v);
48 }
49 
50 MyRowVector MyMatrix::operator[](unsigned int i) const{
51  return this->rowCopy(i);
52 }
53 
54 // Size/Capacity
55 unsigned int MyMatrix::size() const { return this->size1();}
56 unsigned int MyMatrix::capacity() const { return this->size1();}
57 
58 // Number of Rows/Cols
59 unsigned int MyMatrix::rows() const { return this->size1();}
60 unsigned int MyMatrix::columns() const { return this->size2();}
61 
62 // MATRIX - SCALAR operators
63 MyMatrix& MyMatrix::operator+= (double a)
64 {
65  BoostMatrix & op1 = *this;
66  op1 += boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a);
67  return (MyMatrix&)op1;
68 }
69 
70 MyMatrix& MyMatrix::operator-= (double a)
71 {
72  BoostMatrix & op1 = (*this);
73  op1 -= boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a);
74  return (MyMatrix&) op1;
75 }
76 
77 MyMatrix& MyMatrix::operator*= (double a)
78 {
79  BoostMatrix & op1 = (*this);
80  op1 *= a;
81  return *this;
82 }
83 
84 MyMatrix& MyMatrix::operator/= (double a)
85 {
86  BoostMatrix & op1 = (*this);
87  op1 /= a;
88  return (MyMatrix&) op1;
89 }
90 
91 MyMatrix MyMatrix::operator+ (double a) const
92 {
93  return (MyMatrix)(((BoostMatrix)(*this)) + boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a));
94 }
95 
96 MyMatrix MyMatrix::operator- (double a) const
97 {
98  return (MyMatrix)(((BoostMatrix)(*this)) - boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a));
99 }
100 
101 MyMatrix MyMatrix::operator* (double a) const
102 {
103  const BoostMatrix& op1 = (*this);
104  return (MyMatrix) (op1 * a);
105 }
106 
107 MyMatrix MyMatrix::operator/ (double a) const
108 {
109  const BoostMatrix& op1 = (*this);
110  return (MyMatrix) (op1 / a);
111 }
112 
113 MyMatrix&
114 MyMatrix::operator =(const MySymmetricMatrix& a)
115 {
116  *this =(MyMatrix) a;
117 
118  return *this;
119 }
120 
121 // MATRIX - MATRIX Operators
122 MyMatrix MyMatrix::operator- (const MyMatrix& a) const
123 {
124  const BoostMatrix& op1 = *this;
125  const BoostMatrix& op2 = a;
126 
127  return (MyMatrix)(op1 - op2);
128 }
129 
130 MyMatrix MyMatrix::operator+ (const MyMatrix& a) const
131 {
132  const BoostMatrix& op1 = *this;
133  const BoostMatrix& op2 = a;
134 
135  return (MyMatrix)(op1 + op2);
136 }
137 
138 MyMatrix MyMatrix::operator* (const MyMatrix& a) const
139 {
140  const BoostMatrix& op1 = *this;
141  const BoostMatrix& op2 = a;
142 
143  return (MyMatrix) prod(op1,op2);
144 }
145 
146 MyMatrix & MyMatrix::operator+= (const MyMatrix& a)
147 {
148  BoostMatrix & op1 = (*this);
149  const BoostMatrix & op2 = a;
150  op1 += op2;
151  return (MyMatrix &) op1;
152 }
153 
154 MyMatrix & MyMatrix::operator-= (const MyMatrix& a)
155 {
156  BoostMatrix & op1 = (*this);
157  const BoostMatrix & op2 = a;
158  op1 -= op2;
159  return (MyMatrix &) op1;
160 }
161 
162 
163 // MATRIX - VECTOR Operators
164 MyColumnVector MyMatrix::operator* (const MyColumnVector &b) const
165 {
166  const BoostMatrix& op1 = (*this);
167  return (MyColumnVector) prod(op1, ((const BoostColumnVector&)b));
168 }
169 
170 
171 
172 double& MyMatrix::operator()(unsigned int a, unsigned int b)
173 {
174  BoostMatrix & op1 = (*this);
175  return op1(a-1,b-1);
176 }
177 
178 double MyMatrix::operator()(unsigned int a, unsigned int b) const
179 {
180  BoostMatrix op1(*this);
181  return op1(a-1,b-1);
182 }
183 
184 bool MyMatrix::operator==(const MyMatrix& a) const
185 {
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);
189 }
190 
191 
192 // Set all elements equal to a
193 MyMatrix&
194  MyMatrix::operator=(double a)
195 {
196  *this = (MyMatrix)boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a);
197 
198  return *this;
199 }
200 
201 
202 MyRowVector MyMatrix::rowCopy(unsigned int r) const
203 {
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);
208  return (MyRowVector) temp;
209 }
210 
211 MyColumnVector MyMatrix::columnCopy(unsigned int c) const
212 {
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);
217  return (MyColumnVector) temp;
218 }
219 
220 
221 
222 
223 MyMatrix MyMatrix::transpose() const
224 {
225  const BoostMatrix &op1 = (*this);
226  return (MyMatrix) trans(op1);
227 }
228 
229 double MyMatrix::determinant() const
230 {
231  unsigned int r = this->rows();
232  assert(r == this->columns());
233  double result = 1.0;
234  const BoostMatrix& A = (*this);
235  switch (r)
236  {
237  case 1:
238  return A(0,0);
239  case 2:
240  return ( ( A(0,0) * A(1,1)) - ( A(1,0) * A(0,1)) );
241  default:
242  BoostMatrix LU(r,r);
243  boost::numeric::ublas::permutation_matrix<> ndx(r);
244  noalias(LU) = A;
245  int res = lu_factorize(LU,ndx);
246  assert(res == 0);
247 
248  int s = 1;
249  for (boost::numeric::ublas::matrix<double>::size_type i=0;i<LU.size1();++i) {
250  result *= LU(i,i);
251  if (ndx(i)!=i) s = -s;
252  }
253  return result*s;
254  }
255 }
256 
257 
258 MyMatrix MyMatrix::inverse() const
259 {
260  unsigned int r = this->rows();
261  assert(r == this->columns());
262  const BoostMatrix& A = (*this);
263  BoostMatrix Ai(r,r);
264  switch (r)
265  {
266  case 1:
267  {
268  Ai(0,0) = 1/A(0,0);
269  break;
270  }
271  case 2:
272  {
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;
278  break;
279  }
280  default:
281  {
282  BoostMatrix LU(r,r);
283  boost::numeric::ublas::permutation_matrix<> ndx(r);
284  noalias(LU) = A;
285  int res = lu_factorize(LU,ndx);
286  assert(res == 0);
287  noalias(Ai) = boost::numeric::ublas::identity_matrix<double>(r);
288  lu_substitute(LU,ndx,Ai);
289  break;
290  }
291  }
292  return Ai;
293 }
294 
295 
296 int
297 MyMatrix::convertToSymmetricMatrix(MySymmetricMatrix& sym)
298 {
299  // test if matrix is square matrix
300  assert(this->rows() == this->columns());
301 
302  // if necessairy, resize sym
303  // only check cols or rows. Symmetric matrix is square.
304  if ( sym.rows() != this->rows() )
305  sym = MySymmetricMatrix(this->rows());
306 
307  // copy elements
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);
311  return 0;
312 }
313 
314 void
315 MyMatrix::resize(unsigned int i, unsigned int j, bool copy, bool initialize)
316 {
317  BoostMatrix & temp = (BoostMatrix &) (*this);
318  temp.resize(i,j,copy);
319 }
320 
321 // get sub matrix
322 MyMatrix MyMatrix::sub(int i_start, int i_end, int j_start , int j_end) const
323 {
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);
328 
329  return submatrix;
330 }
331 
333 // CLASS SYMMETRIC MATRIX //
335 
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++)
341  row(m,i).assign(v);
342 }
343 
344 MyRowVector MySymmetricMatrix::operator[](unsigned int i) const{
345  return this->rowCopy(i);
346 }
347 
348 
349 
350 // Copy constructor
351 MySymmetricMatrix::SymmetricMatrix(const SymmetricMatrix& a) : BoostSymmetricMatrix(a){}
352 MySymmetricMatrix::SymmetricMatrix(const BoostSymmetricMatrix & a) : BoostSymmetricMatrix(a){}
353 
354 // Destructor
355 MySymmetricMatrix::~SymmetricMatrix(){}
356 
357 // Size/Capacity
358 unsigned int MySymmetricMatrix::size() const { return this->size1();}
359 unsigned int MySymmetricMatrix::capacity() const { return this->size1();}
360 
361 // Ask Number of Rows and Columns
362 unsigned int MySymmetricMatrix::rows() const { return this->size1();}
363 unsigned int MySymmetricMatrix::columns() const { return this->size2();}
364 
365 
366 MyRowVector MySymmetricMatrix::rowCopy(unsigned int r) const
367 {
368 
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);
373  return (MyRowVector) temp;
374 }
375 
376 MySymmetricMatrix MySymmetricMatrix::transpose() const {return (*this);}
377 
378 MySymmetricMatrix MySymmetricMatrix::inverse() const
379 {
380  unsigned int r = this->rows();
381  assert(r == this->columns());
382  const BoostMatrix& A = (*this);
383  BoostSymmetricMatrix Ai(r,r);
384  switch (r)
385  {
386  case 1:
387  {
388  Ai(0,0) = 1/A(0,0);
389  break;
390  }
391  case 2:
392  {
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;
398  break;
399  }
400  default:
401  {
402  BoostSymmetricMatrix LU(r,r);
403  boost::numeric::ublas::permutation_matrix<> ndx(r);
404  noalias(LU) = A;
405  int res = lu_factorize(LU,ndx);
406  assert(res == 0);
407  noalias(Ai) = boost::numeric::ublas::identity_matrix<double>(r);
408  lu_substitute(LU,ndx,Ai);
409  break;
410  }
411  }
412  return Ai;
413 }
414 
415 double MySymmetricMatrix::determinant() const
416 {
417  unsigned int r = this->rows();
418  assert(r == this->columns());
419  const BoostMatrix& A = (*this);
420  switch (r)
421  {
422  case 1:
423  {
424  return A(0,0);
425  }
426  case 2:
427  {
428  return A(0,0)*A(1,1)-A(0,1)*A(1,0);
429  }
430  default:
431  {
432  BoostSymmetricMatrix LU(r,r);
433  boost::numeric::ublas::permutation_matrix<> ndx(r);
434  noalias(LU) = A;
435  int res = lu_factorize(LU,ndx);
436  assert(res == 0);
437 
438  double result = 1.0;
439  int s = 1;
440  for (boost::numeric::ublas::matrix<double>::size_type i=0;i<LU.size1();++i) {
441  result *= LU(i,i);
442  if (ndx(i)!=i) s = -s;
443  }
444  return result*s;
445  }
446  }
447 }
448 
449 
450 // Set all elements equal to a
451 MySymmetricMatrix& MySymmetricMatrix::operator=(const double a)
452 {
453  *this = (MySymmetricMatrix)boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a);
454 
455  return *this;
456 }
457 
458 
459 // SYMMETRICMATRIX - SCALAR operators
460 MySymmetricMatrix& MySymmetricMatrix::operator +=(double a)
461 {
462  BoostSymmetricMatrix & op1 = *this;
463  op1 += boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a);
464  return (MySymmetricMatrix&)op1;
465 }
466 
467 MySymmetricMatrix& MySymmetricMatrix::operator -=(double a)
468 {
469  BoostSymmetricMatrix & op1 = *this;
470  op1 -= boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a);
471  return (MySymmetricMatrix&)op1;
472 }
473 
474 MySymmetricMatrix& MySymmetricMatrix::operator *=(double b)
475 {
476  BoostSymmetricMatrix & op1 = (*this);
477  op1 *= b;
478  return (MySymmetricMatrix&) op1;
479 }
480 
481 MySymmetricMatrix& MySymmetricMatrix::operator /=(double b)
482 {
483  BoostSymmetricMatrix & op1 = (*this);
484  op1 /= b;
485  return (MySymmetricMatrix&) op1;
486 }
487 
488 MySymmetricMatrix MySymmetricMatrix::operator +(double a) const
489 {
490  return (MySymmetricMatrix)(((BoostSymmetricMatrix)(*this)) + boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a));
491 }
492 
493 MySymmetricMatrix MySymmetricMatrix::operator -(double a) const
494 {
495  return (MySymmetricMatrix)(((BoostSymmetricMatrix)(*this)) - boost::numeric::ublas::scalar_matrix<double>(rows(),columns(),a));
496 }
497 
498 MySymmetricMatrix MySymmetricMatrix::operator *(double b) const
499 {
500  const BoostSymmetricMatrix& op1 = (*this);
501  return (MySymmetricMatrix) (op1 * b);
502 }
503 
504 MySymmetricMatrix MySymmetricMatrix::operator /(double b) const
505 {
506  const BoostSymmetricMatrix& op1 = (*this);
507  return (MySymmetricMatrix) (op1 / b);
508 }
509 
510 
511 
512 
513 // SYMMETRICMATRIX - MATRIX operators
514 MyMatrix& MySymmetricMatrix::operator +=(const MyMatrix& a)
515 {
516  BoostSymmetricMatrix & op1 = (*this);
517  op1 += a;
518  return (MyMatrix &) op1;
519 }
520 
521 MyMatrix& MySymmetricMatrix::operator -=(const MyMatrix& a)
522 {
523  BoostSymmetricMatrix & op1 = (*this);
524  op1 -= a;
525  return (MyMatrix &) op1;
526 }
527 
528 
529 MyMatrix MySymmetricMatrix::operator+ (const MyMatrix &a) const
530 {
531  const BoostSymmetricMatrix& op1 = *this;
532  const BoostMatrix& op2 = a;
533 
534  return (MyMatrix) (op1 + op2);
535 }
536 
537 MyMatrix MySymmetricMatrix::operator- (const MyMatrix &a) const
538 {
539  const BoostSymmetricMatrix& op1 = *this;
540  const BoostMatrix& op2 = a;
541 
542  return (MyMatrix) (op1 - op2);
543 }
544 
545 MyMatrix MySymmetricMatrix::operator* (const MyMatrix &a) const
546 {
547  const BoostSymmetricMatrix& op1 = *this;
548  const BoostMatrix& op2 = a;
549 
550  return (MyMatrix) prod(op1, op2);
551 }
552 
553 
554 
555 // SYMMETRICMATRIX - SYMMETRICMATRIX operators
556 MySymmetricMatrix& MySymmetricMatrix::operator +=(const MySymmetricMatrix& a)
557 {
558  BoostSymmetricMatrix & op1 = (*this);
559  const BoostSymmetricMatrix & op2 = a;
560  op1 += op2;
561  return (MySymmetricMatrix &) op1;
562 }
563 
564 MySymmetricMatrix& MySymmetricMatrix::operator -=(const MySymmetricMatrix& a)
565 {
566  BoostSymmetricMatrix & op1 = (*this);
567  const BoostSymmetricMatrix & op2 = a;
568  op1 -= op2;
569  return (MySymmetricMatrix &) op1;
570 }
571 
572 MySymmetricMatrix MySymmetricMatrix::operator+ (const MySymmetricMatrix &a) const
573 {
574  const BoostSymmetricMatrix& op1 = *this;
575  const BoostSymmetricMatrix& op2 = a;
576 
577  return (MySymmetricMatrix) (op1 + op2);
578 }
579 
580 MySymmetricMatrix MySymmetricMatrix::operator- (const MySymmetricMatrix &a) const
581 {
582  const BoostSymmetricMatrix& op1 = *this;
583  const BoostSymmetricMatrix& op2 = a;
584 
585  return (MySymmetricMatrix) (op1 - op2);
586 }
587 
588 MyMatrix MySymmetricMatrix::operator* (const MySymmetricMatrix &a) const
589 {
590  const BoostSymmetricMatrix& op1 = *this;
591  const BoostSymmetricMatrix& op2 = a;
592 
593  return (MyMatrix) prod(op1, op2);
594 }
595 
596 
597 
598 
599 MyColumnVector MySymmetricMatrix::operator* (const MyColumnVector &b) const
600 {
601  const BoostSymmetricMatrix& op1 = (BoostSymmetricMatrix) *this;
602  return (MyColumnVector) prod(op1, ((const BoostColumnVector&)b));
603 }
604 
605 void MySymmetricMatrix::multiply (const MyColumnVector &b, MyColumnVector &result) const
606 {
607  const BoostSymmetricMatrix& op1 = (BoostSymmetricMatrix) *this;
608  result = (MyColumnVector) prod(op1, ((const BoostColumnVector&)b));
609 }
610 
611 MyMatrix MySymmetricMatrix::sub(int i_start, int i_end, int j_start , int j_end) const
612 {
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);
617 
618  return submatrix;
619 }
620 
621 
622 
623 double& MySymmetricMatrix::operator()(unsigned int a, unsigned int b)
624 {
625  BoostSymmetricMatrix & op1 = (*this);
626  return op1(a-1,b-1);
627 }
628 
629 double MySymmetricMatrix::operator()(unsigned int a, unsigned int b) const
630 {
631  BoostSymmetricMatrix op1(*this);
632  return op1(a-1,b-1);
633 }
634 
635 bool MySymmetricMatrix::operator==(const MySymmetricMatrix& a) const
636 {
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);
640 }
641 
642 void
643 MySymmetricMatrix::resize(unsigned int i, bool copy, bool initialize)
644 {
645  BoostSymmetricMatrix & temp = (BoostSymmetricMatrix &) (*this);
646  temp.resize(i, copy);
647 }
648 
649 
650 #endif
#define MyMatrix
#define MyRowVector
#define MySymmetricMatrix
#define MyColumnVector


bfl
Author(s): Klaas Gadeyne, Wim Meeussen, Tinne Delaet and many others. See web page for a full contributor list. ROS package maintained by Wim Meeussen.
autogenerated on Mon Feb 28 2022 21:56:33