Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

base/Vector

Go to the documentation of this file.
00001 /* **-*-c++-*-**************************************************************
00002   Copyright (C)2002 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: Vector 1029 2004-02-11 20:45:54Z jungd $
00019   $Revision: 1.15 $
00020   $Date: 2004-02-11 15:45:54 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #ifndef _BASE_VECTOR_
00026 #define _BASE_VECTOR_
00027 
00028 #include <base/base>
00029 #include <base/consts>
00030 
00031 //#define USE_BOOST_UBLAS
00032 
00033 #include <base/Vector3>
00034 
00035 
00036 #ifndef USE_BOOST_UBLAS
00037 
00038 //
00039 // array wrapper implementation
00040 //
00041 
00042 namespace base {
00043 
00044 #include <base/array>  
00045   
00046 
00047 template<typename T>
00048 class vector
00049 {
00050 public:
00051   typedef T        value_type;
00052   typedef T&       reference;
00053   typedef const T& const_reference;
00054   typedef SInt     difference_type;
00055   typedef Int      size_type;
00056 
00057   vector() {}
00058   vector(size_type n) : a(n) {}
00059   vector(const vector& v) : a(v.a) {}
00060   vector(size_type n, const_reference p) : a(n) { for(size_type i=0; i<n; i++) a[i]=p; }
00061   ~vector() {}
00062   
00063   
00064   bool operator==(const vector&v) const { return a == v.a; }
00065   bool operator!=(const vector&v) const { return a != v.a; }
00066   
00067   bool equals(const vector& v, value_type eps = value_type(consts::epsilon) ) const 
00068   { 
00069     Assert(a.size() == v.size());
00070     for(Int i=0; i<a.size(); i++)
00071       if (!base::equals(a[i],v[i],eps)) return false;
00072     return true;
00073   }
00074 
00075   
00076   reference operator[](size_type i) { return a[i]; }
00077   const_reference operator[](size_type i) const { return a[i]; }
00078 
00079   reference operator()(size_type i) { return a[i]; }
00080   const_reference operator()(size_type i) const { return a[i]; }
00081   
00082   size_type size() const { return a.size(); }
00083 
00084   void clear() { a.clear(); }
00085 
00086   void resize(size_type n) { a.resize(n); }  
00087   
00088   vector& operator=(const vector& v) { a = v.a; return *this; }
00089   void reset(const vector& v) { *this = v; }
00090   
00091   vector& operator+=(const vector& v) { Assert(size() == v.size()); for(size_type i=0; i<size(); i++) a[i]+=v.a[i]; return *this; }  
00092   vector& operator-=(const vector& v) { Assert(size() == v.size()); for(size_type i=0; i<size(); i++) a[i]-=v.a[i]; return *this; }  
00093   vector& operator*=(const value_type& t) { for(size_type i=0; i<size(); i++) a[i]*=t; return *this; }  
00094   vector& operator/=(const value_type& t) { for(size_type i=0; i<size(); i++) a[i]/=t; return *this; }  
00095   vector& negate() { for(size_type i=0; i<size(); i++) a[i]=-a[i]; return *this; }
00096   
00097   value_type norm() const 
00098   { value_type r(0); for(size_type i=0; i<a.size(); i++) r+=(a[i]*a[i]); return r; }
00099   
00100   value_type magnitude() const { return ::sqrt(norm()); }
00101   
00102   
00103   typedef typename array<T>::iterator               iterator;
00104   typedef typename array<T>::const_iterator         const_iterator;
00105   
00106   iterator begin() { return a.begin(); }
00107   const_iterator begin() const { return a.begin(); }
00108   
00109   iterator end() { return a.end(); }
00110   const_iterator end() const { a.end(); }
00111   
00112   friend class matrix;
00113   
00114 protected:
00115   array<T> a;
00116   
00117 };
00118 
00119 
00120 template<typename T>
00121 class range 
00122 {
00123 public:
00124   typedef T   value_type;
00125   typedef Int size_type;
00126 
00127   range(size_type b, size_type e) : b(b), e(e) { Assert(b<=e); }
00128   ~range() {}
00129   
00130   value_type b, e;
00131 };
00132 
00133 
00134 
00135 
00136 template<typename T>
00137 class const_vectorrange
00138 {
00139 public:
00140   typedef T                             value_type;
00141   typedef typename vector<T>::size_type size_type;
00142 
00143   const_vectorrange(const vector<T>& v, const range<size_type>& r) : v(v), r(r) {}
00144   const_vectorrange(const const_vectorrange& vr) : v(vr.v), r(vr.r) {}
00145   ~const_vectorrange() {}
00146   
00147   const value_type& operator[](size_type i) const { Assert(i<(r.e-r.b)); return _v[i]; }
00148   
00149   const_vectorrange& operator=(const const_vectorrange& vr) {
00150     v = vr.v;
00151     r = vr.r;
00152     return *this;
00153   }
00154   
00155 
00156   size_type size() const { return r.e-r.b; }
00157   
00158   operator vector<T>() {
00159     vector<T> rv(r.e-r.b);
00160     for(size_type i=r.b; i<r.e; i++) rv[i-r.b]=v[i];
00161     return rv;
00162   }
00163   
00164 protected:
00165   const_vectorrange() {}
00166 
00167   const value_type& _v(size_type i) const { return v[i-r.b]; }
00168   
00169   const vector<T>& v;
00170   range<size_type> r;
00171 };
00172 
00173 
00174 
00175 
00176 
00177 template<typename T>
00178 class vectorrange
00179 {
00180 public:
00181   typedef T                             value_type;
00182   typedef typename vector<T>::size_type size_type;
00183 
00184   vectorrange(vector<T>& v, const range<size_type>& r) : v(v), r(r) {}
00185   vectorrange(const vectorrange& vr) : v(vr.v), r(vr.r) {}
00186   ~vectorrange() {}
00187   
00188   value_type& operator[](size_type i) { Assert(i<(r.e-r.b)); return _v[i]; }
00189   const value_type& operator[](size_type i) const { Assert(i<(r.e-r.b)); return _v[i]; }
00190   
00191   vectorrange& operator=(const vectorrange& vr) {
00192     v = vr.v;
00193     r = vr.r;
00194     return *this;
00195   }
00196   
00197   vectorrange& operator=(const vector<T>& sv) {
00198     Assert(sv.size() == (r.e-r.b));
00199     for(size_type i=r.b; i<r.e; i++) v[i]=sv[i-r.b];
00200     return *this;
00201   }
00202 
00203   vectorrange& operator+=(const vector<T>& v2) { Assert(size() == v2.size()); for(size_type i=0; i<size(); i++) this->_v(i)+=v2[i]; return *this; }  
00204   vectorrange& operator-=(const vector<T>& v2) { Assert(size() == v2.size()); for(size_type i=0; i<size(); i++) this->_v(i)-=v2[i]; return *this; }  
00205   vectorrange& operator*=(const value_type& t) { for(size_type i=0; i<size(); i++) this->_v(i)*=t; return *this; }  
00206   vectorrange& operator/=(const value_type& t) { for(size_type i=0; i<size(); i++) this->_v(i)/=t; return *this; }  
00207   vectorrange& negate() { for(size_type i=0; i<size(); i++) this->_v(i)=-this->_v(i); return *this; }
00208 
00209   
00210   size_type size() const { return r.e-r.b; }
00211   
00212   operator const_vectorrange<T>() const { return const_vectorrange<T>(v,r); }
00213   
00214   operator vector<T>() {
00215     vector<T> rv(r.e-r.b);
00216     for(size_type i=r.b; i<r.e; i++) rv[i-r.b]=v[i];
00217     return rv;
00218   }
00219   
00220 protected:
00221   vectorrange() {}
00222 
00223   value_type& _v(size_type i) { return v[i-r.b]; }
00224   const value_type& _v(size_type i) const { return v[i-r.b]; }
00225   
00226   vector<T>& v;
00227   range<size_type> r;
00228 };
00229 
00230 
00231 
00232 
00233 
00234 typedef vector<Real>            Vector;
00235 typedef vector<Int>             IVector;
00236 typedef range<Int>              Range;
00237 typedef vectorrange<Real>       VectorRange;
00238 typedef const_vectorrange<Real> ConstVectorRange;
00239 typedef vectorrange<Int>        IVectorRange;
00240 typedef const_vectorrange<Int>  ConstIVectorRange;
00241 
00242 
00243 /// N-dim zero Vector
00244 inline Vector zeroVector(const Int N) { return Vector(N,0); }
00245 inline IVector zeroIVector(const Int N) { return IVector(N,0); }
00246 
00247 /// N-dim unit Vector, with element i=1, all others 0
00248 inline Vector unitVector(const Int N, const Int i) { Assert(i<N); Vector v(N,0); v[i]=1; return v; }
00249 inline IVector unitIVector(const Int N, const Int i) { Assert(i<N); IVector v(N,0); v[i]=1; return v; }
00250 
00251 /// select Vector range (subvector)
00252 inline VectorRange vectorRange(Vector& v, const Range& r)
00253 { 
00254   Assert((r.b < v.size()) && (r.e <= v.size())); 
00255   return VectorRange(v,r); 
00256 }
00257 
00258 inline ConstVectorRange vectorRange(const Vector& v, const Range& r)
00259 { 
00260   Assert((r.b < v.size()) && (r.e <= v.size())); 
00261   return ConstVectorRange(v,r); 
00262 }
00263 
00264 
00265 inline IVectorRange vectorRange(IVector& v, const Range& r)
00266 { 
00267   Assert((r.b < v.size()) && (r.e <= v.size())); 
00268   return IVectorRange(v,r);
00269 }
00270 
00271 inline ConstIVectorRange vectorRange(const IVector& v, const Range& r)
00272 { 
00273   Assert((r.b < v.size()) && (r.e <= v.size())); 
00274   return ConstIVectorRange(v,r);
00275 }
00276 
00277 
00278 
00279 inline Real dot(const Vector& v1, const Vector& v2)
00280 {
00281   Assert(v1.size()==v2.size()); 
00282   Real d=0;
00283   for(Int i=0; i<v1.size(); i++) d+=(v1[i]*v2[i]);
00284   return d; 
00285 }
00286 
00287 inline Real inner_prod(const Vector& v1, const Vector& v2)
00288 { return dot(v1,v2); }
00289 
00290 
00291 inline base::Vector3 toVector3(const Vector& v)
00292 {
00293   Assert(v.size()==3);
00294   return base::Vector3(v[0],v[1],v[2]);
00295 }
00296 
00297 inline Vector fromVector3(const base::Vector3& v)
00298 {
00299   Vector r(3);
00300   r[0]=v.x; r[1]=v.y; r[2]=v.z;
00301   return r;
00302 }
00303 
00304 
00305 inline Vector operator+(const Vector& v1, const Vector& v2)
00306 { Vector r(v1); r+=v2; return r; }
00307   
00308 inline Vector operator-(const Vector& v1, const Vector& v2)
00309 { Vector r(v1); r-=v2; return r; }
00310 
00311 inline Vector operator*(const Vector& v, const Real& s)
00312 { Vector r(v); r*=s; return r; }
00313 
00314 inline Vector operator*(const Real& s, const Vector& v)
00315 { Vector r(v); r*=s; return r; }
00316 
00317 inline Vector operator/(const Vector& v, const Real& s)
00318 { Vector r(v); r/=s; return r; }
00319 
00320 inline Vector operator-(const Vector& v)
00321 { Vector r(v); r.negate(); return r; }
00322 
00323 inline Real norm_2(const Vector& v) { return v.magnitude(); }
00324 
00325 
00326 inline bool equals(const Vector& v1, const Vector& v2, Real eps = consts::epsilon) 
00327 { return v1.equals(v2,eps); }
00328 
00329 
00330 inline Int max_index(const Vector& v)
00331 {
00332   Int mi=0;
00333   Real max=v[mi];
00334   for(Int i=0; i<v.size(); i++)
00335     if (v[i] > max) {
00336       mi=i;
00337       max=v[i];
00338     }
00339   return mi;
00340 }
00341 
00342 
00343 inline Int min_index(const Vector& v)
00344 {
00345   Int mi=0;
00346   Real min=v[mi];
00347   for(Int i=0; i<v.size(); i++)
00348     if (v[i] < min) {
00349       mi=i;
00350       min=v[i];
00351     }
00352   return mi;
00353 }
00354 
00355 
00356 inline std::ostream& operator<<(std::ostream& out, const Vector& v) // Output
00357 { 
00358   out << "[";
00359   for(Int i=0; i<v.size(); i++)
00360     out << v[i] << ((i!=v.size()-1)?",":"");
00361   out << "]";
00362   return out;
00363 }
00364 
00365 inline std::ostream& operator<<(std::ostream& out, const IVector& v) // Output
00366 { 
00367   out << "[";
00368   for(Int i=0; i<v.size(); i++)
00369     out << v[i] << ((i!=v.size()-1)?",":"");
00370   out << "]";
00371   return out;
00372 }
00373 
00374 
00375  
00376 } // base
00377 
00378 
00379 #else
00380 
00381 //
00382 // uBlas wrapper implementation
00383 //
00384 
00385 #include <boost/numeric/ublas/vector.hpp>
00386 
00387 namespace base {
00388 
00389 namespace ublas = boost::numeric::ublas; // alias
00390 
00391 /// Vector(size)
00392 typedef ublas::vector<base::Real> Vector;
00393 typedef ublas::vector<base::Int> IVector;
00394 
00395 typedef ublas::zero_vector<base::Real> ZeroVector;
00396 typedef ublas::zero_vector<base::Int>  ZeroIVector;
00397 typedef ublas::unit_vector<base::Real> UnitVector;
00398 typedef ublas::unit_vector<base::Int>  UnitIVector;
00399 
00400 /// N-dim zero Vector
00401 inline ZeroVector zeroVector(const Int N)
00402 { return ZeroVector(N); }
00403 
00404 inline ZeroIVector zeroIVector(const Int N)
00405 { return ZeroIVector(N); }
00406 
00407 /// N-dim unit Vector, with element i=1, all others 0
00408 inline UnitVector unitVector(const Int N, const Int i)
00409 { return UnitVector(N, i); }
00410 
00411 inline UnitIVector unitIVector(const Int N, const Int i)
00412 { return UnitIVector(N, i); }
00413 
00414 
00415 /// Range(start, stop) s.t. start <= index < stop
00416 /** i.e. index in [start,stop) */
00417 typedef ublas::range Range;
00418 
00419 typedef ublas::vector_range<Vector>        VectorRange;
00420 typedef ublas::vector_range<const Vector>  ConstVectorRange;
00421 typedef ublas::vector_range<IVector>       IVectorRange;
00422 typedef ublas::vector_range<const IVector> ConstIVectorRange;
00423 
00424 
00425 /// select Vector range (subvector)
00426 inline VectorRange vectorRange(Vector& v, const Range& r)
00427 { return VectorRange(v,r); }
00428 
00429 inline ConstVectorRange vectorRange(const Vector& v, const Range& r)
00430 { return ConstVectorRange(v,r); }
00431 
00432 inline IVectorRange vectorRange(IVector& v, const Range& r)
00433 { return IVectorRange(v,r); }
00434 
00435 inline ConstIVectorRange vectorRange(const IVector& v, const Range& r)
00436 { return ConstIVectorRange(v,r); }
00437 
00438 
00439 inline Real dot(const Vector& v1, const Vector& v2)
00440 { return ublas::inner_prod(v1,v2); }
00441 
00442 
00443 inline base::Vector3 toVector3(const Vector& v)
00444 {
00445   Assert(v.size()==3);
00446   return base::Vector3(v[0],v[1],v[2]);
00447 }
00448 
00449 inline Vector fromVector3(const base::Vector3& v)
00450 {
00451   Vector r(3);
00452   r[0]=v.x; r[1]=v.y; r[2]=v.z;
00453   return r;
00454 }
00455 
00456 
00457 
00458 template<class T>
00459 inline bool operator==(const ublas::vector<T>& v1, const ublas::vector<T>& v2)
00460 {
00461   Assert(v1.size() == v2.size());
00462   for(Int i=0; i<v1.size(); i++)
00463     if (!(v1[i] == v2[i])) return false;
00464   return true;
00465 }
00466 
00467 
00468 template<class T>
00469 inline bool equals(const ublas::vector<T>& v1, const ublas::vector<T>& v2, Real eps = consts::epsilon)
00470 {
00471   Assert(v1.size() == v2.size());
00472   for(Int i=0; i<v1.size(); i++)
00473     if (!equals(v1[i],v2[i],eps)) return false;
00474   return true;
00475 }
00476 
00477 template<class T>
00478 inline bool equals(const ublas::vector<T>& v1, const ublas::zero_vector<T>& v2, Real eps = consts::epsilon)
00479 {
00480   Assert(v1.size() == v2.size());
00481   for(Int i=0; i<v1.size(); i++)
00482     if (!equals(v1[i],v2[i],eps)) return false;
00483   return true;
00484 }
00485 
00486 
00487 template <class T>
00488 inline Int max_index(const ublas::vector<T>& v)
00489 {
00490   Int mi=0;
00491   Real max=v[mi];
00492   for(Int i=0; i<v.size(); i++)
00493     if (v[i] > max) {
00494       mi=i;
00495       max=v[i];
00496     }
00497   return mi;
00498 }
00499 
00500 
00501 template <class T>
00502 inline Int min_index(const ublas::vector<T>& v)
00503 {
00504   Int mi=0;
00505   Real min=v[mi];
00506   for(Int i=0; i<v.size(); i++)
00507     if (v[i] < min) {
00508       mi=i;
00509       min=v[i];
00510     }
00511   return mi;
00512 }
00513 
00514 
00515 template <class T>
00516 inline std::ostream& operator<<(std::ostream& out, const ublas::vector<T>& v) // Output
00517 { 
00518   out << "[";
00519   for(Int i=0; i<v.size(); i++)
00520     out << v[i] << ((i!=v.size()-1)?",":"");
00521   out << "]";
00522   return out;
00523 }
00524 
00525 
00526 } // base
00527 
00528 #endif
00529 
00530 #endif

Generated on Thu Jul 29 15:56:16 2004 for OpenSim by doxygen 1.3.6