matrix_NEWMAT.cpp
Go to the documentation of this file.
1 // $Id$
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_NEWMAT__
22 
23 #include <iostream>
24 #include "matrix_NEWMAT.h"
25 #include <assert.h>
26 
27 
28 // Passing the constructor arguments...
29 MyMatrix::Matrix() : NewMatMatrix() {}
30 MyMatrix::Matrix(int num_rows, int num_cols) : NewMatMatrix(num_rows,
31  num_cols){}
32 
33 // Destructor
34 MyMatrix::~Matrix(){}
35 
36 // Copy constructor
37 MyMatrix::Matrix(const MyMatrix& a) : NewMatMatrix(a){}
38 
39 // ill-designed
40 MyMatrix::Matrix(const NewMatMatrix & a) : NewMatMatrix(a){}
41 
42 // Number of Rows/Cols
43 unsigned int MyMatrix::rows() const { return this->Nrows();}
44 unsigned int MyMatrix::columns() const { return this->Ncols();}
45 
46 // MATRIX - SCALAR operators
47 MyMatrix& MyMatrix::operator+= (double a)
48 {
49  NewMatMatrix & op1 = (*this);
50  op1 += a;
51  return (MyMatrix&) op1;
52 }
53 
54 MyMatrix& MyMatrix::operator-= (double a)
55 {
56  NewMatMatrix & op1 = (*this);
57  op1 -= a;
58  return (MyMatrix&) op1;
59 }
60 
61 MyMatrix& MyMatrix::operator*= (double a)
62 {
63  NewMatMatrix & op1 = (*this);
64  op1 *= a;
65  return (MyMatrix&) op1;
66 }
67 
68 MyMatrix& MyMatrix::operator/= (double a)
69 {
70  NewMatMatrix & op1 = (*this);
71  op1 /= a;
72  return (MyMatrix&) op1;
73 }
74 
75 MyMatrix MyMatrix::operator+ (double a) const
76 {
77  // make copy
78  NewMatMatrix op1 = (*this);
79  op1 += a;
80  return (MyMatrix&) op1;
81 }
82 
83 MyMatrix MyMatrix::operator- (double a) const
84 {
85  // make copy
86  NewMatMatrix op1 = (*this);
87  op1 -= a;
88  return (MyMatrix&) op1;
89 }
90 
91 MyMatrix MyMatrix::operator* (double a) const
92 {
93  // make copy
94  NewMatMatrix op1 = (*this);
95  op1 *= a;
96  return (MyMatrix&) op1;
97 }
98 
99 MyMatrix MyMatrix::operator/ (double a) const
100 {
101  // make copy
102  NewMatMatrix op1 = (*this);
103  op1 /= a;
104  return (MyMatrix&) op1;
105 }
106 
107 MyMatrix&
108 MyMatrix::operator =(const MySymmetricMatrix& a)
109 {
110  *this =(MyMatrix) a;
111 
112  return *this;
113 }
114 
115 // MATRIX - MATRIX Operators
116 MyMatrix MyMatrix::operator- (const MyMatrix& a) const
117 {
118  const NewMatMatrix& op1 = (*this);
119  const NewMatMatrix& op2 = a;
120  NewMatMatrix result = (NewMatMatrix) (op1 - op2);
121  return (MyMatrix) result;
122 }
123 
124 MyMatrix MyMatrix::operator+ (const MyMatrix& a) const
125 {
126  const NewMatMatrix& op1 = (*this);
127  const NewMatMatrix& op2 = a;
128  NewMatMatrix result = (NewMatMatrix) (op1 + op2);
129  return (MyMatrix) result;
130 }
131 
132 MyMatrix MyMatrix::operator* (const MyMatrix& a) const
133 {
134  const NewMatMatrix& op1 = (*this);
135  const NewMatMatrix& op2 = a;
136  NewMatMatrix result = (NewMatMatrix) (op1 * op2);
137  return (MyMatrix) result;
138 }
139 
140 MyMatrix & MyMatrix::operator+= (const MyMatrix& a)
141 {
142  NewMatMatrix & op1 = (*this);
143  const NewMatMatrix & op2 = a;
144  op1 += op2;
145  return (MyMatrix &) op1;
146 }
147 
148 MyMatrix & MyMatrix::operator-= (const MyMatrix& a)
149 {
150  NewMatMatrix & op1 = (*this);
151  const NewMatMatrix & op2 = a;
152  op1 -= op2;
153  return (MyMatrix &) op1;
154 }
155 
156 
157 // MATRIX - VECTOR Operators
158 MyColumnVector MyMatrix::operator* (const MyColumnVector &b) const
159 {
160  const NewMatMatrix& op1 = (*this);
161  const NewMatColumnVector& op2 = b;
162  return (MyColumnVector) (op1 * op2);
163 }
164 
165 
166 // Set all elements equal to a
167 MyMatrix& MyMatrix::operator=(double a)
168 {
169  NewMatMatrix temp = (NewMatMatrix) *this;
170  temp = a;
171  *this = (MyMatrix) temp;
172 
173  return *this;
174 }
175 
176 
177 MyRowVector MyMatrix::rowCopy(unsigned int r) const
178 {
179  const NewMatMatrix& temp = (*this);
180  return (MyRowVector) temp.Row(r);
181 }
182 
183 MyColumnVector MyMatrix::columnCopy(unsigned int c) const
184 {
185  const NewMatMatrix& temp = (*this);
186  return (MyColumnVector) temp.Column(c);
187 }
188 
189 
190 
191 
192 MyMatrix MyMatrix::transpose() const
193 {
194  const NewMatMatrix & base = (*this);
195  NewMatMatrix transposedbase = base.t();
196  return (MyMatrix) transposedbase;
197 }
198 
199 double MyMatrix::determinant() const
200 {
201  const NewMatMatrix& base = (*this);
202  NEWMAT::LogAndSign temp = base.LogDeterminant();
203  double result = temp.Value();
204  return result;
205 }
206 
207 
208 MyMatrix MyMatrix::inverse() const
209 {
210  const NewMatMatrix & base = (*this);
211  NewMatMatrix inverted = base.i();
212  return (MyMatrix) inverted;
213 }
214 
215 
216 int
217 MyMatrix::convertToSymmetricMatrix(MySymmetricMatrix& sym)
218 {
219  // test if matrix is square matrix
220  assert( this->rows() == this->columns() );
221 
222  // if necessairy, resize sym
223  // only check cols or rows. Symmetric matrix is square.
224  if ( sym.rows() != this->rows() )
225  sym.ReSize(this->rows());
226 
227 
228  // copy elements
229  for ( unsigned int i=0; i<this->rows(); i++ )
230  for ( unsigned int j=0; j<=i; j++ )
231  sym(i+1,j+1) = (*this)(i+1,j+1);
232  return 0;
233 }
234 
235 void
236 MyMatrix::resize(unsigned int i, unsigned int j, bool copy, bool initialize)
237 {
238  NewMatMatrix & temp = (NewMatMatrix &) (*this);
239  temp.ReSize(i,j);
240 }
241 
242 // get sub matrix
243 MyMatrix MyMatrix::sub(int i_start, int i_end, int j_start , int j_end) const
244 {
245  assert(i_start >= 0 && i_start <= rows());
246  assert(j_start >= 0 && j_start <= columns());
247  assert(i_end >= 0 && i_end <= rows());
248  assert(j_end >= 0 && j_end <= columns());
249  assert(i_start <= i_end);
250  assert(j_start <= j_end);
251 
252  return (MyMatrix)(this->SubMatrix(i_start, i_end, j_start, j_end));
253 }
254 
255 
256 
258 // CLASS SYMMETRIC MATRIX //
260 
261 MySymmetricMatrix::SymmetricMatrix() : NewMatSymmetricMatrix() {}
262 MySymmetricMatrix::SymmetricMatrix(int n) : NewMatSymmetricMatrix(n) {}
263 
264 // Copy constructor
265 MySymmetricMatrix::SymmetricMatrix(const SymmetricMatrix& a) : NewMatSymmetricMatrix(a){}
266 MySymmetricMatrix::SymmetricMatrix(const NewMatSymmetricMatrix & a) : NewMatSymmetricMatrix(a){}
267 
268 // Destructor
269 MySymmetricMatrix::~SymmetricMatrix(){}
270 
271 // Ask Number of Rows and Columns
272 unsigned int MySymmetricMatrix::rows() const { return this->Nrows();}
273 unsigned int MySymmetricMatrix::columns() const { return this->Ncols();}
274 
275 
276 MySymmetricMatrix MySymmetricMatrix::transpose() const {return (*this);}
277 
278 MySymmetricMatrix MySymmetricMatrix::inverse() const
279 {
280  const NewMatSymmetricMatrix & base = (NewMatSymmetricMatrix &) *this;
281  NewMatSymmetricMatrix inverted = base.i();
282  return (MySymmetricMatrix) inverted;
283 }
284 
285 double MySymmetricMatrix::determinant() const
286 {
287  const NewMatSymmetricMatrix & base = (NewMatSymmetricMatrix &) *this;
288  NEWMAT::LogAndSign temp = base.LogDeterminant();
289  double result = temp.Value();
290  return result;
291 }
292 
293 
294 double& MyMatrix::operator()(unsigned int a, unsigned int b)
295 {
296  NewMatMatrix & op1(*this);
297  return op1(a,b);
298 }
299 
300 const double MyMatrix::operator()(unsigned int a, unsigned int b) const
301 {
302  const NewMatMatrix& op1(*this);
303  return op1(a,b);
304 }
305 
306 const bool MyMatrix::operator==(const MyMatrix& a) const
307 {
308  const NewMatMatrix& op1 = *this;
309  const NewMatMatrix& op2 = a;
310  return (op1 == op2);
311 }
312 
313 // Set all elements equal to a
314 MySymmetricMatrix& MySymmetricMatrix::operator=(const double a)
315 {
316  NewMatSymmetricMatrix temp = (NewMatSymmetricMatrix) *this;
317  temp = a;
318  *this = (MySymmetricMatrix) temp;
319 
320  return *this;
321 }
322 
323 
324 // SYMMETRICMATRIX - SCALAR operators
325 MySymmetricMatrix& MySymmetricMatrix::operator +=(double a)
326 {
327  NewMatSymmetricMatrix & op1 = (*this);
328  op1 += a;
329  return (MySymmetricMatrix&) op1;
330 }
331 
332 MySymmetricMatrix& MySymmetricMatrix::operator -=(double a)
333 {
334  NewMatSymmetricMatrix & op1 = (*this);
335  op1 -= a;
336  return (MySymmetricMatrix&) op1;
337 }
338 
339 MySymmetricMatrix& MySymmetricMatrix::operator *=(double b)
340 {
341  NewMatSymmetricMatrix & op1 = (*this);
342  op1 *= b;
343  return (MySymmetricMatrix&) op1;
344 }
345 
346 MySymmetricMatrix& MySymmetricMatrix::operator /=(double b)
347 {
348  NewMatSymmetricMatrix & op1 = (*this);
349  op1 /= b;
350  return (MySymmetricMatrix&) op1;
351 }
352 
353 MySymmetricMatrix MySymmetricMatrix::operator +(double a) const
354 {
355  // make copy
356  NewMatSymmetricMatrix op1 = (*this);
357  op1 += a;
358  return (MySymmetricMatrix) op1;
359 }
360 
361 MySymmetricMatrix MySymmetricMatrix::operator -(double a) const
362 {
363  // make copy
364  NewMatSymmetricMatrix op1 = (*this);
365  op1 -= a;
366  return (MySymmetricMatrix) op1;
367 }
368 
369 MySymmetricMatrix MySymmetricMatrix::operator *(double b) const
370 {
371  // make copy
372  NewMatSymmetricMatrix op1 = (*this);
373  op1 *= b;
374  return (MySymmetricMatrix) op1;
375 }
376 
377 MySymmetricMatrix MySymmetricMatrix::operator /(double b) const
378 {
379  // make copy
380  NewMatSymmetricMatrix op1 = (*this);
381  op1 /= b;
382  return (MySymmetricMatrix) op1;
383 }
384 
385 
386 
387 
388 // SYMMETRICMATRIX - MATRIX operators
389 MyMatrix& MySymmetricMatrix::operator +=(const MyMatrix& a)
390 {
391  NewMatSymmetricMatrix & op1 = (*this);
392  const NewMatMatrix & op2 = a;
393  op1 += op2;
394  return (MyMatrix &) op1;
395 }
396 
397 MyMatrix& MySymmetricMatrix::operator -=(const MyMatrix& a)
398 {
399  NewMatSymmetricMatrix & op1 = (*this);
400  const NewMatMatrix & op2 = a;
401  op1 -= op2;
402  return (MyMatrix &) op1;
403 }
404 
405 
406 MyMatrix MySymmetricMatrix::operator+ (const MyMatrix &a) const
407 {
408  const NewMatMatrix& op1 = (*this);
409  const NewMatMatrix& op2 = a;
410  NewMatMatrix result = (NewMatMatrix) (op1 + op2);
411  return (MyMatrix) result;
412 }
413 
414 MyMatrix MySymmetricMatrix::operator- (const MyMatrix &a) const
415 {
416  const NewMatMatrix& op1 = (*this);
417  const NewMatMatrix& op2 = a;
418  NewMatMatrix result = (NewMatMatrix) (op1 - op2);
419  return (MyMatrix) result;
420 }
421 
422 MyMatrix MySymmetricMatrix::operator* (const MyMatrix &a) const
423 {
424  const NewMatMatrix& op1 = (*this);
425  const NewMatMatrix& op2 = a;
426  NewMatMatrix result = (NewMatMatrix) (op1 * op2);
427  return (MyMatrix) result;
428 }
429 
430 
431 
432 // SYMMETRICMATRIX - SYMMETRICMATRIX operators
433 MySymmetricMatrix& MySymmetricMatrix::operator +=(const MySymmetricMatrix& a)
434 {
435  NewMatSymmetricMatrix & op1 = (*this);
436  const NewMatSymmetricMatrix & op2 = a;
437  op1 += op2;
438  return (MySymmetricMatrix &) op1;
439 }
440 
441 MySymmetricMatrix& MySymmetricMatrix::operator -=(const MySymmetricMatrix& a)
442 {
443  NewMatSymmetricMatrix & op1 = (*this);
444  const NewMatSymmetricMatrix & op2 = a;
445  op1 -= op2;
446  return (MySymmetricMatrix &) op1;
447 }
448 
449 MySymmetricMatrix MySymmetricMatrix::operator+ (const MySymmetricMatrix &a) const
450 {
451  const NewMatSymmetricMatrix& op1 = (*this);
452  const NewMatSymmetricMatrix& op2 = a;
453  NewMatSymmetricMatrix result = (NewMatSymmetricMatrix) (op1 + op2);
454  return (MySymmetricMatrix) result;
455 }
456 
457 MySymmetricMatrix MySymmetricMatrix::operator- (const MySymmetricMatrix &a) const
458 {
459  const NewMatSymmetricMatrix& op1 = (*this);
460  const NewMatSymmetricMatrix& op2 = a;
461  NewMatSymmetricMatrix result = (NewMatSymmetricMatrix) (op1 - op2);
462  return (MySymmetricMatrix) result;
463 }
464 
465 MyMatrix MySymmetricMatrix::operator* (const MySymmetricMatrix &a) const
466 {
467  const NewMatSymmetricMatrix& op1 = (*this);
468  const NewMatSymmetricMatrix& op2 = a;
469  return (MyMatrix) (op1 * op2);
470 }
471 
472 
473 
474 
475 MyColumnVector MySymmetricMatrix::operator* (const MyColumnVector &b) const
476 {
477  const NewMatSymmetricMatrix& op1 = *this;
478  const NewMatColumnVector& op2 = b;
479  return (MyColumnVector) (op1 * op2);
480 }
481 
482 void MySymmetricMatrix::multiply (const MyColumnVector &b, MyColumnVector &result) const
483 {
484  const NewMatSymmetricMatrix& op1 = *this;
485  const NewMatColumnVector& op2 = b;
486  result = (MyColumnVector) (op1 * op2);
487 }
488 
489 MyMatrix MySymmetricMatrix::sub(int i_start, int i_end, int j_start , int j_end) const
490 {
491  return (MyMatrix)(this->SubMatrix(i_start, i_end, j_start, j_end));
492 }
493 
494 
495 void
496 MySymmetricMatrix::resize(unsigned int i, bool copy, bool initialize)
497 {
498  NewMatSymmetricMatrix & temp = (NewMatSymmetricMatrix &) (*this);
499  temp.ReSize(i);
500 }
501 
502 
503 
504 double& MySymmetricMatrix::operator()(unsigned int a, unsigned int b)
505 {
506  NewMatSymmetricMatrix & op1 = (*this);
507  return op1(a,b);
508 }
509 
510 const double MySymmetricMatrix::operator()(unsigned int a, unsigned int b) const
511 {
512  const NewMatSymmetricMatrix op1(*this);
513  return op1(a,b);
514 }
515 
516 const bool MySymmetricMatrix::operator==(const MySymmetricMatrix& a) const
517 {
518  const NewMatSymmetricMatrix& op1 = *this;
519  const NewMatSymmetricMatrix& op2 = a;
520  return (op1 == op2);
521 }
522 
523 
524 #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