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