vector_NEWMAT.cpp
Go to the documentation of this file.
00001 // $Id$
00002 // Copyright (C) 2002 Klaas Gadeyne <first dot last at gmail dot com>
00003 
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published by
00007 // the Free Software Foundation; either version 2.1 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 //
00019 
00020 #include "../config.h"
00021 #ifdef __MATRIXWRAPPER_NEWMAT__
00022 
00023 #include "vector_NEWMAT.h"
00024 
00025 // Constructors
00026 MyColumnVector::ColumnVector() : NewMatColumnVector() {}
00027 MyColumnVector::ColumnVector(int num_rows) : NewMatColumnVector(num_rows){}
00028 MyColumnVector::ColumnVector(const MyColumnVector& a, const MyColumnVector& b) : NewMatColumnVector(a.rows() + b.rows())
00029 {
00030   NewMatColumnVector& opl = (*this);
00031 
00032   unsigned int i;
00033 
00034   // copy elements of a to opl
00035   for (i=0; i< a.rows(); i++)
00036     opl(i+1) = a(i+1);
00037 
00038   // copy elements of b to opl
00039   for (i=0; i< b.rows(); i++)
00040     opl(a.rows() + i+1) = b(i+1);
00041 }
00042 
00043 // Destructor
00044 MyColumnVector::~ColumnVector(){}
00045 
00046 // Copy constructor
00047 MyColumnVector::ColumnVector(const MyColumnVector& a) :
00048   NewMatColumnVector(a){}
00049 MyColumnVector::ColumnVector(const NewMatColumnVector & a) :
00050   NewMatColumnVector(a){}
00051 
00052 // Resizing
00053 void MyColumnVector::resize(int num_rows)
00054 {
00055   NewMatColumnVector & op1 = (*this);
00056   op1.ReSize(num_rows);
00057 }
00058 
00059 // Assign
00060 void MyColumnVector::assign(int num_rows, double value)
00061 {
00062   NewMatColumnVector & op1 = (*this);
00063   op1.resize(num_rows);
00064   for (unsigned int i=0; i<num_rows; i++)
00065     op1(i+1) = value;
00066 }
00067 
00068 // Number of Rows / Cols
00069 unsigned int MyColumnVector::rows() const { return this->Nrows();}
00070 unsigned int MyColumnVector::columns() const { return this->Ncols();}
00071 unsigned int MyColumnVector::capacity() const { return this->Nrows();}
00072 
00073 MyColumnVector
00074 MyColumnVector::vectorAdd(const MyColumnVector& v2) const
00075 {
00076   const MyColumnVector& v1 = *this;
00077   MyColumnVector res(v1.rows() + v2.rows());
00078 
00079   for (unsigned int i=0; i<v1.rows(); i++)
00080     res(i+1) = v1(i+1);
00081 
00082   for (unsigned int i=0; i<v2.rows(); i++)
00083     res(v1.rows()+i+1) = v2(i+1);
00084 
00085   return res;
00086 }
00087 
00088 double& MyColumnVector::operator()(unsigned int i)
00089 {
00090   NewMatColumnVector& op1 = *(this);
00091   return op1(i);
00092 }
00093 
00094 const double MyColumnVector::operator()(unsigned int i) const
00095 {
00096   NewMatColumnVector op1 = (*this);
00097   return op1(i);
00098 }
00099 
00100 
00101 // Operators
00102 MyColumnVector & MyColumnVector::operator+= (const MyColumnVector& a)
00103 {
00104   NewMatColumnVector & op1 = (*this);
00105   const NewMatColumnVector & op2 = a;
00106   op1 += op2;
00107   return (MyColumnVector &) op1 ;
00108 }
00109 
00110 MyColumnVector & MyColumnVector::operator-= (const MyColumnVector& a)
00111 {
00112   NewMatColumnVector & op1 = (*this);
00113   const NewMatColumnVector & op2 = a;
00114   op1 -= op2;
00115   return (MyColumnVector &) op1 ;
00116 }
00117 
00118 MyColumnVector MyColumnVector::operator+ (const MyColumnVector &a) const
00119 {
00120   const NewMatColumnVector& op1 = (*this);
00121   const NewMatColumnVector & op2 = a;
00122   NewMatColumnVector result = (NewMatColumnVector) (op1 + op2);
00123   return (MyColumnVector) result;
00124 }
00125 
00126 MyColumnVector MyColumnVector::operator- (const MyColumnVector &a) const
00127 {
00128   const NewMatColumnVector & op1 = (*this);
00129   const NewMatColumnVector & op2 = a;
00130   NewMatColumnVector result = (NewMatColumnVector) (op1 - op2);
00131   return (MyColumnVector) result;
00132 }
00133 
00134 
00135 
00136 MyColumnVector& MyColumnVector::operator+= (double a)
00137 {
00138   NewMatColumnVector& op1 = *this;
00139   op1 += a;
00140   return (MyColumnVector&) op1;
00141 }
00142 
00143 MyColumnVector& MyColumnVector::operator-= (double a)
00144 {
00145   NewMatColumnVector& op1 = *this;
00146   op1 -= a;
00147   return (MyColumnVector&) op1;
00148 }
00149 
00150 MyColumnVector& MyColumnVector::operator*= (double a)
00151 {
00152   NewMatColumnVector& op1 = *this;
00153   op1 *= a;
00154   return (MyColumnVector&) op1;
00155 }
00156 
00157 MyColumnVector& MyColumnVector::operator/= (double a)
00158 {
00159   NewMatColumnVector& op1 = *this;
00160   op1 /= a;
00161   return (MyColumnVector&) op1;
00162 }
00163 
00164 
00165 MyColumnVector MyColumnVector::operator+ (double a) const
00166 {
00167   const NewMatColumnVector & op1 = (*this);
00168   return (MyColumnVector) (op1 + a);
00169 }
00170 
00171 MyColumnVector MyColumnVector::operator- (double a) const
00172 {
00173   const NewMatColumnVector & op1 = (*this);
00174   return (MyColumnVector) (op1 - a);
00175 }
00176 
00177 MyColumnVector MyColumnVector::operator* (double a) const
00178 {
00179   const NewMatColumnVector & op1 = (*this);
00180   return (MyColumnVector) (op1 * a);
00181 }
00182 
00183 MyColumnVector MyColumnVector::operator/ (double a) const
00184 {
00185   const NewMatColumnVector & op1 = (*this);
00186   return (MyColumnVector) (op1 / a);
00187 }
00188 
00189 const bool MyColumnVector::operator==(const MyColumnVector& a) const
00190 {
00191   const NewMatColumnVector& op1 = *this;
00192   const NewMatColumnVector& op2 = a;
00193   return (op1 == op2);
00194 }
00195 
00196 MyRowVector MyColumnVector::transpose() const
00197 {
00198   NewMatColumnVector & base = (NewMatColumnVector &) *this;
00199   NewMatRowVector transposedbase = base.t();
00200   return (MyRowVector) transposedbase;
00201 }
00202 
00203 MyMatrix MyColumnVector::operator* (const MyRowVector &a) const
00204 {
00205   const NewMatColumnVector & op1 = (*this);
00206   const NewMatRowVector & op2 = a;
00207   NewMatMatrix result = (NewMatMatrix) (op1 * op2);
00208   return (MyMatrix) result;
00209 }
00210 
00211 MyColumnVector&
00212 MyColumnVector::operator=(const MyColumnVector &a)
00213 {
00214   // Both these implementations result in the same
00215   NewMatColumnVector * op1; const NewMatColumnVector * op2;
00216   op1 = this; op2 = &a;
00217   *op1 = *op2;
00218   return *(this);
00219 }
00220 
00221 MyColumnVector&
00222 MyColumnVector::operator=(double a)
00223 {
00224   // Both these implementations result in the same
00225   NewMatColumnVector * op1;
00226   op1 = this;
00227   *op1 = a;
00228   return *this;
00229 }
00230 
00231 // KG temporary: to read in files fast
00232 istream& operator >> (istream& is, MyColumnVector& a)
00233 {
00234   int nr = a.rows();
00235   int nc = 1;
00236 
00237   if (nr < 1 || nc < 1)
00238     is.clear (ios::badbit);
00239   else
00240     {
00241       double tmp;
00242       for (int i = 0; i < nr; i++)
00243         {
00244           is >> tmp;
00245           if (is)
00246             a(i+1) = tmp; // check if this is ok
00247           else
00248             goto done;
00249         }
00250     }
00251 done:
00252   return is;
00253 }
00254 
00255 
00256 MyColumnVector MyColumnVector::sub(int j_start , int j_end) const
00257 {
00258   return (MyColumnVector)(this->SubMatrix(j_start, j_end, 1, 1));
00259 }
00260 
00264 
00265 // Constructors
00266 MyRowVector::RowVector() : NewMatRowVector() {}
00267 MyRowVector::RowVector(int num_rows) : NewMatRowVector(num_rows){}
00268 
00269 // Destructor
00270 MyRowVector::~RowVector(){}
00271 
00272 // Copy constructor
00273 MyRowVector::RowVector(const MyRowVector& a) : NewMatRowVector(a){}
00274 MyRowVector::RowVector(const NewMatRowVector & a) : NewMatRowVector(a){}
00275 
00276 // Number of Rows / Cols
00277 unsigned int MyRowVector::rows() const { return this->Nrows();}
00278 unsigned int MyRowVector::columns() const { return this->Ncols();}
00279 unsigned int MyRowVector::capacity() const { return this->Ncols();}
00280 
00281 // Operators
00282 MyRowVector & MyRowVector::operator+= (const MyRowVector& a)
00283 {
00284   NewMatRowVector & op1 = (*this);
00285   const NewMatRowVector & op2 = a;
00286   op1 += op2;
00287   return (MyRowVector &) op1 ;
00288 }
00289 
00290 MyRowVector & MyRowVector::operator-= (const MyRowVector& a)
00291 {
00292   NewMatRowVector & op1 = (*this);
00293   const NewMatRowVector & op2 = a;
00294   op1 -= op2;
00295   return (MyRowVector &) op1 ;
00296 }
00297 
00298 MyRowVector MyRowVector::operator+ (const MyRowVector &a) const
00299 {
00300   const NewMatRowVector & op1 = (*this);
00301   const NewMatRowVector & op2 = a;
00302   NewMatRowVector result = (NewMatRowVector) (op1 + op2);
00303   return (MyRowVector) result;
00304 }
00305 
00306 MyRowVector MyRowVector::operator- (const MyRowVector &a) const
00307 {
00308   const NewMatRowVector & op1 = (*this);
00309   const NewMatRowVector & op2 = a;
00310   NewMatRowVector result = (NewMatRowVector) (op1 - op2);
00311   return (MyRowVector) result;
00312 }
00313 
00314 
00315 
00316 
00317 double
00318 MyRowVector::operator*(const MyColumnVector& a) const
00319 {
00320   assert (this->columns() == a.rows());
00321 
00322   const NewMatRowVector& op1 = (*this);
00323   const NewMatColumnVector & op2 = a;
00324   NewMatMatrix matrixresult = op1 * op2;
00325   double result = matrixresult.AsScalar();
00326   return result;
00327 }
00328 
00329 
00330 MyRowVector&
00331 MyRowVector::operator=(const MyRowVector &a)
00332 {
00333   // Both these implementations result in the same
00334   NewMatRowVector * op1; const NewMatRowVector * op2;
00335   op1 = this; op2 = &a;
00336   *op1 = *op2;
00337 
00338   return *this;
00339 }
00340 
00341 
00342 MyColumnVector MyRowVector::transpose() const
00343 {
00344   NewMatRowVector & base = (NewMatRowVector &) *this;
00345   NewMatColumnVector transposedbase = base.t();
00346   return (MyColumnVector) transposedbase;
00347 }
00348 
00349 
00350 MyRowVector MyRowVector::sub(int j_start , int j_end) const
00351 {
00352   return (MyRowVector)(this->SubMatrix(1, 1, j_start, j_end));
00353 }
00354 
00355 
00356 
00357 MyRowVector& MyRowVector::operator+= (double a)
00358 {
00359   NewMatRowVector& op1 = *this;
00360   op1 += a;
00361   return (MyRowVector&) op1;
00362 }
00363 
00364 MyRowVector& MyRowVector::operator-= (double a)
00365 {
00366   NewMatRowVector& op1 = *this;
00367   op1 -= a;
00368   return (MyRowVector&) op1;
00369 }
00370 
00371 MyRowVector& MyRowVector::operator*= (double a)
00372 {
00373   NewMatRowVector& op1 = *this;
00374   op1 *= a;
00375   return (MyRowVector&) op1;
00376 }
00377 
00378 MyRowVector& MyRowVector::operator/= (double a)
00379 {
00380   NewMatRowVector& op1 = *this;
00381   op1 /= a;
00382   return (MyRowVector&) op1;
00383 }
00384 
00385 
00386 MyRowVector MyRowVector::operator+ (double a) const
00387 {
00388   const NewMatRowVector & op1 = (*this);
00389   return (MyRowVector) (op1 + a);
00390 }
00391 
00392 MyRowVector MyRowVector::operator- (double a) const
00393 {
00394   const NewMatRowVector & op1 = (*this);
00395   return (MyRowVector) (op1 - a);
00396 }
00397 
00398 MyRowVector MyRowVector::operator* (double a) const
00399 {
00400   const NewMatRowVector & op1 = (*this);
00401   return (MyRowVector) (op1 * a);
00402 }
00403 
00404 MyRowVector MyRowVector::operator/ (double a) const
00405 {
00406   const NewMatRowVector & op1 = (*this);
00407   return (MyRowVector) (op1 / a);
00408 }
00409 
00410 
00411 
00412 MyRowVector&
00413 MyRowVector::operator=(double a)
00414 {
00415   // Both these implementations result in the same
00416   NewMatRowVector * op1;
00417   op1 = this;
00418   *op1 = a;
00419   return *this;
00420 }
00421 
00422 
00423 double& MyRowVector::operator()(unsigned int i)
00424 {
00425   NewMatRowVector& op1 = *(this);
00426   return  op1(i);
00427 }
00428 
00429 const double MyRowVector::operator()(unsigned int i) const
00430 {
00431   NewMatRowVector op1 = (*this);
00432   return  op1(i);
00433 }
00434 
00435 const bool MyRowVector::operator==(const MyRowVector& a) const
00436 {
00437   const NewMatRowVector& op1 = *this;
00438   const NewMatRowVector& op2 = a;
00439   return (op1 == op2);
00440 }
00441 
00442 
00443 MyRowVector
00444 MyRowVector::vectorAdd(const MyRowVector& v2) const
00445 {
00446   const MyRowVector& v1 = *this;
00447   MyRowVector res(v1.rows() + v2.rows());
00448 
00449   for (unsigned int i=0; i<v1.rows(); i++)
00450     res(i+1) = v1(i+1);
00451 
00452   for (unsigned int i=0; i<v2.rows(); i++)
00453     res(v1.rows()+i+1) = v2(i+1);
00454 
00455   return res;
00456 }
00457 
00458 
00459 // Resizing
00460 void MyRowVector::resize(int num_cols)
00461 {
00462   NewMatRowVector & op1 = (*this);
00463   op1.ReSize(num_cols);
00464 }
00465 
00466 // Assign
00467 void MyRowVector::assign(int num_columns, double value)
00468 {
00469   NewMatRowVector & op1 = (*this);
00470   op1.resize(num_columns);
00471   for (unsigned int i=0; i<num_columns; i++)
00472     op1(i+1) = value;
00473 }
00474 
00475 
00476 #endif


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 11 2019 03:45:12