vector_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 "vector_NEWMAT.h"
24 
25 // Constructors
26 MyColumnVector::ColumnVector() : NewMatColumnVector() {}
27 MyColumnVector::ColumnVector(int num_rows) : NewMatColumnVector(num_rows){}
28 MyColumnVector::ColumnVector(const MyColumnVector& a, const MyColumnVector& b) : NewMatColumnVector(a.rows() + b.rows())
29 {
30  NewMatColumnVector& opl = (*this);
31 
32  unsigned int i;
33 
34  // copy elements of a to opl
35  for (i=0; i< a.rows(); i++)
36  opl(i+1) = a(i+1);
37 
38  // copy elements of b to opl
39  for (i=0; i< b.rows(); i++)
40  opl(a.rows() + i+1) = b(i+1);
41 }
42 
43 // Destructor
44 MyColumnVector::~ColumnVector(){}
45 
46 // Copy constructor
47 MyColumnVector::ColumnVector(const MyColumnVector& a) :
48  NewMatColumnVector(a){}
49 MyColumnVector::ColumnVector(const NewMatColumnVector & a) :
50  NewMatColumnVector(a){}
51 
52 // Resizing
53 void MyColumnVector::resize(int num_rows)
54 {
55  NewMatColumnVector & op1 = (*this);
56  op1.ReSize(num_rows);
57 }
58 
59 // Assign
60 void MyColumnVector::assign(int num_rows, double value)
61 {
62  NewMatColumnVector & op1 = (*this);
63  op1.resize(num_rows);
64  for (unsigned int i=0; i<num_rows; i++)
65  op1(i+1) = value;
66 }
67 
68 // Number of Rows / Cols
69 unsigned int MyColumnVector::rows() const { return this->Nrows();}
70 unsigned int MyColumnVector::columns() const { return this->Ncols();}
71 unsigned int MyColumnVector::capacity() const { return this->Nrows();}
72 
74 MyColumnVector::vectorAdd(const MyColumnVector& v2) const
75 {
76  const MyColumnVector& v1 = *this;
77  MyColumnVector res(v1.rows() + v2.rows());
78 
79  for (unsigned int i=0; i<v1.rows(); i++)
80  res(i+1) = v1(i+1);
81 
82  for (unsigned int i=0; i<v2.rows(); i++)
83  res(v1.rows()+i+1) = v2(i+1);
84 
85  return res;
86 }
87 
88 double& MyColumnVector::operator()(unsigned int i)
89 {
90  NewMatColumnVector& op1 = *(this);
91  return op1(i);
92 }
93 
94 const double MyColumnVector::operator()(unsigned int i) const
95 {
96  NewMatColumnVector op1 = (*this);
97  return op1(i);
98 }
99 
100 
101 // Operators
102 MyColumnVector & MyColumnVector::operator+= (const MyColumnVector& a)
103 {
104  NewMatColumnVector & op1 = (*this);
105  const NewMatColumnVector & op2 = a;
106  op1 += op2;
107  return (MyColumnVector &) op1 ;
108 }
109 
110 MyColumnVector & MyColumnVector::operator-= (const MyColumnVector& a)
111 {
112  NewMatColumnVector & op1 = (*this);
113  const NewMatColumnVector & op2 = a;
114  op1 -= op2;
115  return (MyColumnVector &) op1 ;
116 }
117 
118 MyColumnVector MyColumnVector::operator+ (const MyColumnVector &a) const
119 {
120  const NewMatColumnVector& op1 = (*this);
121  const NewMatColumnVector & op2 = a;
122  NewMatColumnVector result = (NewMatColumnVector) (op1 + op2);
123  return (MyColumnVector) result;
124 }
125 
126 MyColumnVector MyColumnVector::operator- (const MyColumnVector &a) const
127 {
128  const NewMatColumnVector & op1 = (*this);
129  const NewMatColumnVector & op2 = a;
130  NewMatColumnVector result = (NewMatColumnVector) (op1 - op2);
131  return (MyColumnVector) result;
132 }
133 
134 
135 
136 MyColumnVector& MyColumnVector::operator+= (double a)
137 {
138  NewMatColumnVector& op1 = *this;
139  op1 += a;
140  return (MyColumnVector&) op1;
141 }
142 
143 MyColumnVector& MyColumnVector::operator-= (double a)
144 {
145  NewMatColumnVector& op1 = *this;
146  op1 -= a;
147  return (MyColumnVector&) op1;
148 }
149 
150 MyColumnVector& MyColumnVector::operator*= (double a)
151 {
152  NewMatColumnVector& op1 = *this;
153  op1 *= a;
154  return (MyColumnVector&) op1;
155 }
156 
157 MyColumnVector& MyColumnVector::operator/= (double a)
158 {
159  NewMatColumnVector& op1 = *this;
160  op1 /= a;
161  return (MyColumnVector&) op1;
162 }
163 
164 
165 MyColumnVector MyColumnVector::operator+ (double a) const
166 {
167  const NewMatColumnVector & op1 = (*this);
168  return (MyColumnVector) (op1 + a);
169 }
170 
171 MyColumnVector MyColumnVector::operator- (double a) const
172 {
173  const NewMatColumnVector & op1 = (*this);
174  return (MyColumnVector) (op1 - a);
175 }
176 
177 MyColumnVector MyColumnVector::operator* (double a) const
178 {
179  const NewMatColumnVector & op1 = (*this);
180  return (MyColumnVector) (op1 * a);
181 }
182 
183 MyColumnVector MyColumnVector::operator/ (double a) const
184 {
185  const NewMatColumnVector & op1 = (*this);
186  return (MyColumnVector) (op1 / a);
187 }
188 
189 const bool MyColumnVector::operator==(const MyColumnVector& a) const
190 {
191  const NewMatColumnVector& op1 = *this;
192  const NewMatColumnVector& op2 = a;
193  return (op1 == op2);
194 }
195 
196 MyRowVector MyColumnVector::transpose() const
197 {
198  NewMatColumnVector & base = (NewMatColumnVector &) *this;
199  NewMatRowVector transposedbase = base.t();
200  return (MyRowVector) transposedbase;
201 }
202 
203 MyMatrix MyColumnVector::operator* (const MyRowVector &a) const
204 {
205  const NewMatColumnVector & op1 = (*this);
206  const NewMatRowVector & op2 = a;
207  NewMatMatrix result = (NewMatMatrix) (op1 * op2);
208  return (MyMatrix) result;
209 }
210 
212 MyColumnVector::operator=(const MyColumnVector &a)
213 {
214  // Both these implementations result in the same
215  NewMatColumnVector * op1; const NewMatColumnVector * op2;
216  op1 = this; op2 = &a;
217  *op1 = *op2;
218  return *(this);
219 }
220 
222 MyColumnVector::operator=(double a)
223 {
224  // Both these implementations result in the same
225  NewMatColumnVector * op1;
226  op1 = this;
227  *op1 = a;
228  return *this;
229 }
230 
231 // KG temporary: to read in files fast
232 istream& operator >> (istream& is, MyColumnVector& a)
233 {
234  int nr = a.rows();
235  int nc = 1;
236 
237  if (nr < 1 || nc < 1)
238  is.clear (ios::badbit);
239  else
240  {
241  double tmp;
242  for (int i = 0; i < nr; i++)
243  {
244  is >> tmp;
245  if (is)
246  a(i+1) = tmp; // check if this is ok
247  else
248  goto done;
249  }
250  }
251 done:
252  return is;
253 }
254 
255 
256 MyColumnVector MyColumnVector::sub(int j_start , int j_end) const
257 {
258  return (MyColumnVector)(this->SubMatrix(j_start, j_end, 1, 1));
259 }
260 
264 
265 // Constructors
266 MyRowVector::RowVector() : NewMatRowVector() {}
267 MyRowVector::RowVector(int num_rows) : NewMatRowVector(num_rows){}
268 
269 // Destructor
270 MyRowVector::~RowVector(){}
271 
272 // Copy constructor
273 MyRowVector::RowVector(const MyRowVector& a) : NewMatRowVector(a){}
274 MyRowVector::RowVector(const NewMatRowVector & a) : NewMatRowVector(a){}
275 
276 // Number of Rows / Cols
277 unsigned int MyRowVector::rows() const { return this->Nrows();}
278 unsigned int MyRowVector::columns() const { return this->Ncols();}
279 unsigned int MyRowVector::capacity() const { return this->Ncols();}
280 
281 // Operators
282 MyRowVector & MyRowVector::operator+= (const MyRowVector& a)
283 {
284  NewMatRowVector & op1 = (*this);
285  const NewMatRowVector & op2 = a;
286  op1 += op2;
287  return (MyRowVector &) op1 ;
288 }
289 
290 MyRowVector & MyRowVector::operator-= (const MyRowVector& a)
291 {
292  NewMatRowVector & op1 = (*this);
293  const NewMatRowVector & op2 = a;
294  op1 -= op2;
295  return (MyRowVector &) op1 ;
296 }
297 
298 MyRowVector MyRowVector::operator+ (const MyRowVector &a) const
299 {
300  const NewMatRowVector & op1 = (*this);
301  const NewMatRowVector & op2 = a;
302  NewMatRowVector result = (NewMatRowVector) (op1 + op2);
303  return (MyRowVector) result;
304 }
305 
306 MyRowVector MyRowVector::operator- (const MyRowVector &a) const
307 {
308  const NewMatRowVector & op1 = (*this);
309  const NewMatRowVector & op2 = a;
310  NewMatRowVector result = (NewMatRowVector) (op1 - op2);
311  return (MyRowVector) result;
312 }
313 
314 
315 
316 
317 double
318 MyRowVector::operator*(const MyColumnVector& a) const
319 {
320  assert (this->columns() == a.rows());
321 
322  const NewMatRowVector& op1 = (*this);
323  const NewMatColumnVector & op2 = a;
324  NewMatMatrix matrixresult = op1 * op2;
325  double result = matrixresult.AsScalar();
326  return result;
327 }
328 
329 
331 MyRowVector::operator=(const MyRowVector &a)
332 {
333  // Both these implementations result in the same
334  NewMatRowVector * op1; const NewMatRowVector * op2;
335  op1 = this; op2 = &a;
336  *op1 = *op2;
337 
338  return *this;
339 }
340 
341 
342 MyColumnVector MyRowVector::transpose() const
343 {
344  NewMatRowVector & base = (NewMatRowVector &) *this;
345  NewMatColumnVector transposedbase = base.t();
346  return (MyColumnVector) transposedbase;
347 }
348 
349 
350 MyRowVector MyRowVector::sub(int j_start , int j_end) const
351 {
352  return (MyRowVector)(this->SubMatrix(1, 1, j_start, j_end));
353 }
354 
355 
356 
357 MyRowVector& MyRowVector::operator+= (double a)
358 {
359  NewMatRowVector& op1 = *this;
360  op1 += a;
361  return (MyRowVector&) op1;
362 }
363 
364 MyRowVector& MyRowVector::operator-= (double a)
365 {
366  NewMatRowVector& op1 = *this;
367  op1 -= a;
368  return (MyRowVector&) op1;
369 }
370 
371 MyRowVector& MyRowVector::operator*= (double a)
372 {
373  NewMatRowVector& op1 = *this;
374  op1 *= a;
375  return (MyRowVector&) op1;
376 }
377 
378 MyRowVector& MyRowVector::operator/= (double a)
379 {
380  NewMatRowVector& op1 = *this;
381  op1 /= a;
382  return (MyRowVector&) op1;
383 }
384 
385 
386 MyRowVector MyRowVector::operator+ (double a) const
387 {
388  const NewMatRowVector & op1 = (*this);
389  return (MyRowVector) (op1 + a);
390 }
391 
392 MyRowVector MyRowVector::operator- (double a) const
393 {
394  const NewMatRowVector & op1 = (*this);
395  return (MyRowVector) (op1 - a);
396 }
397 
398 MyRowVector MyRowVector::operator* (double a) const
399 {
400  const NewMatRowVector & op1 = (*this);
401  return (MyRowVector) (op1 * a);
402 }
403 
404 MyRowVector MyRowVector::operator/ (double a) const
405 {
406  const NewMatRowVector & op1 = (*this);
407  return (MyRowVector) (op1 / a);
408 }
409 
410 
411 
413 MyRowVector::operator=(double a)
414 {
415  // Both these implementations result in the same
416  NewMatRowVector * op1;
417  op1 = this;
418  *op1 = a;
419  return *this;
420 }
421 
422 
423 double& MyRowVector::operator()(unsigned int i)
424 {
425  NewMatRowVector& op1 = *(this);
426  return op1(i);
427 }
428 
429 const double MyRowVector::operator()(unsigned int i) const
430 {
431  NewMatRowVector op1 = (*this);
432  return op1(i);
433 }
434 
435 const bool MyRowVector::operator==(const MyRowVector& a) const
436 {
437  const NewMatRowVector& op1 = *this;
438  const NewMatRowVector& op2 = a;
439  return (op1 == op2);
440 }
441 
442 
444 MyRowVector::vectorAdd(const MyRowVector& v2) const
445 {
446  const MyRowVector& v1 = *this;
447  MyRowVector res(v1.rows() + v2.rows());
448 
449  for (unsigned int i=0; i<v1.rows(); i++)
450  res(i+1) = v1(i+1);
451 
452  for (unsigned int i=0; i<v2.rows(); i++)
453  res(v1.rows()+i+1) = v2(i+1);
454 
455  return res;
456 }
457 
458 
459 // Resizing
460 void MyRowVector::resize(int num_cols)
461 {
462  NewMatRowVector & op1 = (*this);
463  op1.ReSize(num_cols);
464 }
465 
466 // Assign
467 void MyRowVector::assign(int num_columns, double value)
468 {
469  NewMatRowVector & op1 = (*this);
470  op1.resize(num_columns);
471  for (unsigned int i=0; i<num_columns; i++)
472  op1(i+1) = value;
473 }
474 
475 
476 #endif
#define MyMatrix
istream & operator>>(istream &stream, Probability &prob)
Definition: bfl_toolkit.cpp:68
#define MyRowVector
#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