matrix_LTI.cpp
Go to the documentation of this file.
1 // Copyright (C) 2002 Klaas Gadeyne <first dot last at gmail dot com>
2 
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as published by
6 // the Free Software Foundation; either version 2.1 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
19 #include "../config.h"
20 #ifdef __MATRIXWRAPPER_LTI__
21 
22 #include <iostream>
23 #include "matrix_LTI.h"
24 
25 #include <ltilib/ltiMatrixInversion.h>
26 #include <ltilib/ltiMatrixDecomposition.h>
27 #include <ltilib/ltiSymmetricMatrixInversion.h>
28 
29 
30 
32 // CLASS MATRIX //
34 // Passing the constructor arguments...
35 MyMatrix::Matrix() : ltiMatrix() {}
36 MyMatrix::Matrix(int num_rows, int num_cols) : ltiMatrix(num_rows,
37  num_cols,
38  0.0){}
39 // Destructor
40 MyMatrix::~Matrix(){}
41 
42 // Copy constructor
43 MyMatrix::Matrix(const MyMatrix& a) : ltiMatrix(a){}
44 
45 // This is a bad solution, but necessary if the base class is
46 // ill-designed
47 MyMatrix::Matrix(const ltiMatrix & a) : ltiMatrix(a){}
48 
49 // Number of Rows/Cols
50 unsigned int MyMatrix::rows() const { return ((ltiMatrix)(*this)).rows();}
51 unsigned int MyMatrix::columns() const { return ((ltiMatrix)(*this)).columns();}
52 
53 
54 MyRowVector MyMatrix::rowCopy(unsigned int r) const
55 {
56  ltiMatrix temp = (ltiMatrix) *this;
57  return (MyRowVector) temp.getRowCopy(r-1);
58 }
59 
60 MyColumnVector MyMatrix::columnCopy(unsigned int c) const
61 {
62  ltiMatrix temp = (ltiMatrix) *this;
63  return (MyColumnVector) temp.getColumnCopy(c-1);
64 }
65 
66 double& MyMatrix::operator()(unsigned int a, unsigned int b)
67 {
68  //ltiMatrix & op1 = (*this);
69  return this->at(a-1,b-1);
70 }
71 
72 const double MyMatrix::operator()(unsigned int a, unsigned int b) const
73 {
74  //ltiMatrix op1(*this);
75  return this->at(a-1,b-1);
76 }
77 
78 const bool MyMatrix::operator==(const MyMatrix& a) const
79 {
80  const ltiMatrix& op1 = *this;
81  const ltiMatrix& op2 = a;
82  return (op1 == op2);
83 }
84 
85 
86 // MATRIX - SCALAR operators
87 MyMatrix& MyMatrix::operator+= (double a)
88 {
89  ltiMatrix & op1 = (*this);
90  op1 += a;
91  return (MyMatrix&) op1;
92 }
93 
94 MyMatrix& MyMatrix::operator-= (double a)
95 {
96  ltiMatrix & op1 = (*this);
97  op1 -= a;
98  return (MyMatrix&) op1;
99 }
100 
101 MyMatrix& MyMatrix::operator*= (double a)
102 {
103  ltiMatrix & op1 = (*this);
104  op1 *= a;
105  return (MyMatrix&) op1;
106 }
107 
108 MyMatrix MyMatrix::operator+ (double a) const
109 {
110  ltiMatrix op1(*this);
111  op1 += a;
112  return (MyMatrix) op1;
113 }
114 
115 MyMatrix MyMatrix::operator- (double a) const
116 {
117  ltiMatrix op1(*this);
118  op1 -= a;
119  return (MyMatrix) op1;
120 }
121 
122 MyMatrix MyMatrix::operator* (double a) const
123 {
124  ltiMatrix op1(*this);
125  op1 *= a;
126  return (MyMatrix) op1;
127 }
128 
129 
130 MyMatrix& MyMatrix::operator/= (double a)
131 {
132  ltiMatrix & op1 = (*this);
133  op1 /= a;
134  return (MyMatrix&) op1;
135 }
136 
137 
138 // MATRIX - MATRIX Operators
139 MyMatrix MyMatrix::operator- (const MyMatrix& a) const
140 {
141  ltiMatrix op1 = (*this);
142  return (MyMatrix) (op1.subtract(a));
143 }
144 
145 MyMatrix MyMatrix::operator+ (const MyMatrix& a) const
146 {
147  ltiMatrix op1 = (*this);
148  return (MyMatrix) (op1.add(a));
149 }
150 
151 MyMatrix MyMatrix::operator* (const MyMatrix& a) const
152 {
153  ltiMatrix op1 = (*this);
154  return (MyMatrix) (op1.multiply(a));
155 }
156 
157 MyMatrix MyMatrix::operator/ (double b) const
158 {
159  ltiMatrix op1(*this);
160  op1 /= b;
161  return (MyMatrix) op1;
162 }
163 
164 
165 MyMatrix & MyMatrix::operator+= (const MyMatrix& a)
166 {
167  ltiMatrix & op1 = (*this);
168  op1 += a;
169  return (MyMatrix &) op1;
170 }
171 
172 MyMatrix & MyMatrix::operator-= (const MyMatrix& a)
173 {
174  ltiMatrix & op1 = (*this);
175  op1 -= a;
176  return (MyMatrix &) op1;
177 }
178 
179 
180 // MATRIX - VECTOR Operators
181 MyColumnVector MyMatrix::operator* (const MyColumnVector &b) const
182 {
183  const ltiMatrix& op1 = *this;
184  ltiColumnVector op2(b);
185  return (MyColumnVector) op1.multiply(op2);
186 }
187 
188 // Set all elements equal to a
189 MyMatrix &MyMatrix::operator=(const double a)
190 {
191  ltiMatrix & op1 = (*this);
192  op1.fill(a,0,0,(*this).rows(),(*this).columns());
193  return (MyMatrix&) op1;
194 }
195 
196 
197 
198 MyMatrix MyMatrix::transpose() const
199 {
200  ltiMatrix base(*this);
201  base.transpose();
202  return (MyMatrix) base;
203 }
204 
205 double MyMatrix::determinant() const
206 {
207  ltiMatrix & base = (ltiMatrix &) *this;
208  lti::matrix<double> tmp;
209  tmp.resize(base.size());
210  for (int i=0;i<tmp.rows();i++)
211  for (int j=0;j<tmp.columns();j++) {
212  tmp.at(i,j)=(double)(base.at(i,j));
213  }
214  lti::luDecomposition<double> lu;
215  return lu.det(tmp);
216 }
217 
218 
219 MyMatrix MyMatrix::inverse() const
220 {
221  lti::matrixInversion<double> inv;
222  ltiMatrix base(*this);
223  inv.apply(base);
224  return (MyMatrix) base;
225 }
226 
227 
228 int
229 MyMatrix::convertToSymmetricMatrix(MySymmetricMatrix& sym)
230 {
231  // test if matrix is square matrix
232  assert(this->rows() == this->columns() );
233 
234  // if necessairy, resize sym
235  // only check cols or rows. Symmetric matrix is square.
236  if ( sym.rows() != this->rows() )
237  sym.resize(this->rows());
238 
239  // copy elements
240  for ( unsigned int i=0; i<this->rows(); i++ )
241  for ( unsigned int j=0; j<=i; j++ )
242  {
243  sym[i][j] = (*this)[i][j];
244  sym[j][i] = (*this)[i][j];
245  }
246  return 0;
247 }
248 
249 // get sub matrix
250 MyMatrix MyMatrix::sub(int i_start, int i_end, int j_start , int j_end) const
251 {
252  ltiMatrix m(*this,i_start-1,i_end-1, j_start-1,j_end-1);
253  return (MyMatrix) m;
254 }
255 
256 
257 void
258 MyMatrix::resize(unsigned int i, unsigned int j, bool copy, bool initialize)
259 {
260  ltiMatrix& base = (ltiMatrix &) *this;
261  base.resize(i,j, copy, initialize);
262 }
263 
265 // CLASS SYMMETRIC MATRIX //
267 
268 MySymmetricMatrix::SymmetricMatrix() : ltiSymmetricMatrix() {}
269 MySymmetricMatrix::SymmetricMatrix(int n) : ltiSymmetricMatrix(n,n) {}
270 
271 // Copy constructor
272 MySymmetricMatrix::SymmetricMatrix(const SymmetricMatrix& a) : ltiSymmetricMatrix(a){}
273 MySymmetricMatrix::SymmetricMatrix(const ltiSymmetricMatrix & a) : ltiSymmetricMatrix(a){}
274 
275 // Destructor
276 MySymmetricMatrix::~SymmetricMatrix(){}
277 
278 // Ask Number of Rows and Columns
279 unsigned int MySymmetricMatrix::rows() const { return (((ltiSymmetricMatrix)(*this)).rows());}
280 unsigned int MySymmetricMatrix::columns() const { return (((ltiSymmetricMatrix)(*this)).rows());}
281 
282 MySymmetricMatrix MySymmetricMatrix::transpose() const
283 {return (*this);}
284 
285 MySymmetricMatrix MySymmetricMatrix::inverse() const
286 {
287  lti::matrixInversion<double> inv;
288  ltiSymmetricMatrix base(*this);
289  inv.apply(base);
290  return (MySymmetricMatrix) base;
291 }
292 
293 double MySymmetricMatrix::determinant() const
294 {
295  ltiSymmetricMatrix & base = (ltiSymmetricMatrix &) *this;
296  lti::matrix<double> tmp;
297  tmp.resize(base.size());
298  for (int i=0;i<tmp.rows();i++)
299  for (int j=0;j<tmp.columns();j++) {
300  tmp.at(i,j)=(double)(base.at(i,j));
301  }
302  lti::luDecomposition<double> lu;
303  return lu.det(tmp);
304 }
305 
306 
307 double& MySymmetricMatrix::operator()(unsigned int a, unsigned int b)
308 {
309  ltiSymmetricMatrix & op1 = (*this);
310  // only fill in lower triangle
311  if (a < b)
312  return op1.at(b-1,a-1);
313  else
314  return op1.at(a-1,b-1);
315 }
316 const double MySymmetricMatrix::operator()(unsigned int a, unsigned int b) const
317 {
318  ltiSymmetricMatrix op1(*this);
319  // only fill in lower triangle
320  if (a < b)
321  return op1.at(b-1,a-1);
322  else
323  return op1.at(a-1,b-1);
324 }
325 
326 
327 const bool MySymmetricMatrix::operator==(const MySymmetricMatrix& a) const
328 {
329  const ltiSymmetricMatrix& op1 = *this;
330  const ltiSymmetricMatrix& op2 = a;
331  return (op1 == op2);
332 }
333 
334 // MATRIX - SCALAR operators
335 MySymmetricMatrix& MySymmetricMatrix::operator=(const double a)
336 {
337  ltiSymmetricMatrix temp = (ltiSymmetricMatrix) *this;
338  temp.fill(a,0,0,temp.rows(),temp.columns());
339  *this = (MySymmetricMatrix) temp;
340 
341  return *this;
342 }
343 
344 // MATRIX - SCALAR operators
346 MySymmetricMatrix::operator +=(double a)
347 {
348  ltiSymmetricMatrix & op1 = (*this);
349  op1 += a;
350  return (MySymmetricMatrix&) op1;
351 }
352 
354 MySymmetricMatrix::operator -=(double a)
355 {
356  ltiSymmetricMatrix & op1 = (*this);
357  op1 -= a;
358  return (MySymmetricMatrix&) op1;
359 }
360 
362 MySymmetricMatrix::operator *=(double b)
363 {
364  ltiSymmetricMatrix & op1 = (*this);
365  op1 *= b;
366  return (MySymmetricMatrix&) op1;
367 }
368 
370 MySymmetricMatrix::operator /=(double b)
371 {
372  ltiSymmetricMatrix & op1 = (*this);
373  op1 /= b;
374  return (MySymmetricMatrix&) op1;
375 }
376 
378 MySymmetricMatrix::operator+ (double a) const
379 {
380  ltiSymmetricMatrix op1(*this);
381  op1 += a;
382  return (MySymmetricMatrix) op1;
383 }
384 
386 MySymmetricMatrix::operator- (double a) const
387 {
388  ltiSymmetricMatrix op1(*this);
389  op1 -= a;
390  return (MySymmetricMatrix) op1;
391 }
392 
393 MySymmetricMatrix MySymmetricMatrix::operator* (double a) const
394 {
395  ltiSymmetricMatrix op1(*this);
396  op1 *= a;
397  return (MySymmetricMatrix) op1;
398 }
399 
400 MySymmetricMatrix MySymmetricMatrix::operator/ (double b) const
401 {
402  ltiSymmetricMatrix op1(*this);
403  op1 /= b;
404  return (MySymmetricMatrix) op1;
405 }
406 
407 
408 
409 
410 
411 // SYMMETRICMATRIX - MATRIX operators
412 MyMatrix& MySymmetricMatrix::operator +=(const MyMatrix& a)
413 {
414  ltiSymmetricMatrix & op1 = (*this);
415  const ltiMatrix & op2 = a;
416  op1 += op2;
417  return (MyMatrix &) op1;
418 }
419 
420 MyMatrix&
421 MySymmetricMatrix::operator -=(const MyMatrix& a)
422 {
423  ltiSymmetricMatrix & op1 = (*this);
424  const ltiMatrix & op2 = a;
425  op1 -= op2;
426  return (MyMatrix &) op1;
427 }
428 
429 
430 MyMatrix
431 MySymmetricMatrix::operator+ (const MyMatrix &a) const
432 {
433  ltiMatrix op1(*this);
434  return (MyMatrix) (op1.add(a));
435 }
436 
437 MyMatrix
438 MySymmetricMatrix::operator- (const MyMatrix &a) const
439 {
440 
441  ltiMatrix op1(*this);
442  return (MyMatrix) (op1.subtract(a));
443 }
444 
445 MyMatrix
446 MySymmetricMatrix::operator* (const MyMatrix &a) const
447 {
448  ltiMatrix op1(*this);
449  return (MyMatrix) (op1.multiply(a));
450 }
451 
452 MyMatrix&
453 MyMatrix::operator =(const MySymmetricMatrix& a)
454 {
455  *this =(MyMatrix) a;
456 
457  return *this;
458 }
459 
461 MySymmetricMatrix::operator +=(const MySymmetricMatrix& a)
462 {
463  ltiSymmetricMatrix & op1 = (*this);
464  const ltiSymmetricMatrix & op2 = a;
465  op1 += op2;
466  return (MySymmetricMatrix &) op1;
467 }
468 
470 MySymmetricMatrix::operator -=(const MySymmetricMatrix& a)
471 {
472  ltiSymmetricMatrix & op1 = (*this);
473  const ltiSymmetricMatrix & op2 = a;
474  op1 -= op2;
475  return (MySymmetricMatrix &) op1;
476 }
477 
478 
480 MySymmetricMatrix::operator+ (const MySymmetricMatrix &a) const
481 {
482  ltiSymmetricMatrix op1 = (*this);
483  op1 += a;
484  return (MySymmetricMatrix &) op1;
485 }
486 
488 MySymmetricMatrix::operator- (const MySymmetricMatrix &a) const
489 {
490  ltiSymmetricMatrix op1 = (*this);
491  op1 -= a;
492  return (MySymmetricMatrix &) op1;
493 }
494 
495 MyMatrix
496 MySymmetricMatrix::operator* (const MySymmetricMatrix &a) const
497 {
498  ltiSymmetricMatrix op1 = (*this);
499  return (MyMatrix) (op1.multiply(a));
500 }
501 
502 
503 
504 
505 
506 
507 MyColumnVector MySymmetricMatrix::operator* (const MyColumnVector &b) const
508 {
509  const ltiSymmetricMatrix& op1 = (const ltiSymmetricMatrix&) *this;
510  ltiColumnVector op2 = b;
511  return (MyColumnVector) op1.multiply(op2);
512 }
513 
514 void MySymmetricMatrix::multiply (const MyColumnVector &b, MyColumnVector &result) const
515 {
516  const ltiSymmetricMatrix& op1 = (const ltiSymmetricMatrix&) *this;
517  ltiColumnVector op2 = b;
518  result = (MyColumnVector) op1.multiply(op2);
519 }
520 
521 MyMatrix MySymmetricMatrix::sub(int i_start, int i_end, int j_start , int j_end) const
522 {
523  // first copy all elements from lower triangle to upper triangle
524  unsigned int r = this->rows();
525  unsigned int c = this->columns();
526  ltiMatrix copy = *this;
527  for (unsigned int i=0; i<r; i++)
528  for (unsigned int j=0; j<=i; j++)
529  copy.at(j,i) = copy.at(i,j);
530  ltiMatrix m(copy,i_start-1,i_end-1, j_start-1,j_end-1);
531  return (MyMatrix) m;
532 }
533 
534 
535 void
536 MySymmetricMatrix::resize(unsigned int i, bool copy, bool initialize)
537 {
538  ltiSymmetricMatrix& base = (ltiSymmetricMatrix &) *this;
539  base.resize(i, i, copy, initialize);
540 }
541 
542 
543 #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 Jun 10 2019 12:47:59