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

numvector.h

00001 /***************************************************************************
00002                    numvector.h  -  vector for numeric types
00003                              -------------------
00004     begin                : Tue Apr 25 2000
00005     copyright            : (C) 2000 by Michael Peeters
00006     email                : Michael.Peeters@vub.ac.be
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #ifndef NUMVECTOR_H
00019 #define NUMVECTOR_H
00020 
00021 #include <vector>
00022 #include <stdexcept>
00023 #include "numerictypes.h"
00024 #include "numerictraits.h"
00025 
00026 namespace MODEL {
00027 
00031   // Previously: template<integer dims, class NT = NumericTraits<number, dims> >
00032   template<integer dims, typename nelem=number, class NT = NumericTraits<nelem,dims> >
00033         class NumVector : public std::vector<typename NT::number>
00034   {
00035         public:
00036         typedef typename        NT::number                              number; 
00037         typedef typename        NT::number                              numT;   // old code
00038         typedef typename        NT::vect                                vect;   // make life easier
00039         typedef std::vector<number>     base;   // base type
00040         
00042         NumVector(const numT& init=0.)
00043         {this->resize(dims,init);}
00044         virtual ~NumVector() {}
00045 
00049         NumVector(const numT Tarray[])
00050         {       this->resize(dims);     
00051                 for(integer i=0;i<dims;i++) (*this)[i]=Tarray[i];}
00052         
00054         NumVector*      clone(void){return new NumVector(*this);}
00055                 
00057         NumVector(const NumVector& vcopy) : base(vcopy) {}
00058                 
00060         const NumVector&        operator=(const NumVector& vassign)
00061         { if (this!=&vassign) base::operator=(vassign); return *this; }         
00062         
00064         const numT&     operator[](integer i) const
00065         {
00066           // removed bounds checking for speed
00067           return std::vector<typename NT::number>::operator[](i);
00068           // if (i>=0&&i<dims) return std::vector<typename NT::number>::operator[](i);
00069           // else throw std::logic_error("array value out of bounds");
00070           return (*this)[0]; // this should _never_ happen
00071         }       
00072 
00074         numT&   operator[](integer i)
00075         {
00076           if (i>=0&&i<dims) return std::vector<typename NT::number>::operator[](i);
00077           else throw std::logic_error("array value out of bounds");
00078           return (*this)[0]; // this should _never_ happen
00079         }       
00080                                                 
00081         // Arithmetic Operators
00082         // This will be taken over by PETE, undoubtedly 
00083                 
00084         NumVector& operator+=(const NumVector& nvplus)
00085         { for(integer i=0;i<dims;i++) (*this)[i]+=nvplus[i]; return *this; }
00086 
00087         NumVector& operator-=(const NumVector& nvmin)
00088         { for(integer i=0;i<dims;i++) (*this)[i]-=nvmin[i]; return *this; }
00089                 
00090         NumVector& operator/=(const numT& nvdiv)
00091         { for(integer i=0;i<dims;i++) (*this)[i]/=nvdiv; return *this; }
00092 
00093         NumVector& operator*=(const numT& nvmul)
00094         { for(integer i=0;i<dims;i++) (*this)[i] *= nvmul; return *this; }
00095 
00096         friend NumVector operator*<dims, nelem, NT>(const numT& nvmul, const NumVector& nv);
00097         friend NumVector operator/<dims, nelem, NT>(const NumVector& nvmul, const numT& nv);
00098 
00099                 
00100         friend NumVector operator+<dims, nelem, NT>(const NumVector& nvmul, const NumVector& nv);
00101 
00102         friend NumVector operator-<dims, nelem, NT>(const NumVector& nvmul, const NumVector& nv);
00103                                 
00105         /*              
00106                         friend numT operator*<dims,NT>(const NumVector& nv,
00107                         const NumVector& nvmul);
00108         */      
00109         //              /** Compute the scalar product correct for complex types */
00110         //              friend real operator*(const NumVector< std::complex<numT> ,dims>& nv,
00111         //                                                                                      const NumVector< std::complex<numT>,dims>& nvmul);
00112                                                                                                                 
00115         friend numT norm<dims,nelem,NT>(const NumVector& nv) ;
00116 
00118         friend numT length<dims,nelem,NT>(const NumVector& nv);
00119                 
00120         friend NumVector operator-<dims,nelem,NT>(const NumVector& m);                                                  
00121   };
00122         
00123   // Previously: template<integer dims, class NT>
00124   template <integer dims, typename nelem, class NT >
00125         inline
00126         NumVector<dims,nelem,NT>
00127         operator-(const NumVector<dims,nelem,NT>& m)
00128         {
00129           NumVector<dims,nelem,NT>      v(m);
00130           const typename NT::number     minus(-1.);
00131           v*=minus;
00132           return v;
00133         }       
00134 
00135 
00136   // Previously: template<integer dims, class NT>
00137   template <integer dims, typename nelem, class NT >
00138         inline
00139         NumVector<dims, nelem, NT>
00140         operator*(const typename NT::number& nvmul, const NumVector<dims,nelem,NT>& nv)
00141         {
00142           NumVector<dims,nelem,NT> res(nv);
00143           return res*=nvmul;
00144         }
00145 
00146   template <integer dims, typename nelem, class NT >
00147         inline
00148         NumVector<dims, nelem, NT>
00149         operator/(const NumVector<dims,nelem,NT>& nvmul, const typename NT::number&  nv)
00150         {
00151           NumVector<dims,nelem,NT> res(nvmul);
00152           return res/=nv;
00153         }
00154 
00155   // Previously: template<integer dims, class NT>
00156   template <integer dims, typename nelem, class NT >
00157         inline
00158         NumVector<dims,nelem,NT>
00159         operator+(const  NumVector<dims,nelem,NT>& nvmul, const NumVector<dims,nelem,NT>& nv)
00160         {
00161           NumVector<dims,nelem,NT> res(nv);
00162           return res+=nvmul;
00163         }
00164 
00165   template <integer dims, typename nelem, class NT >
00166         inline
00167         NumVector<dims,nelem,NT>
00168         operator-(const  NumVector<dims,nelem,NT>& nvmul, const NumVector<dims,nelem,NT>& nv)
00169         {
00170           NumVector<dims,nelem,NT> res(nvmul);
00171           return res-=nv;
00172         }
00173 
00177   // Previously: template<integer dims, class NT>
00178   template <integer dims, typename nelem, class NT >
00179         inline
00180         typename NT::number
00181         operator*(const NumVector<dims,nelem,NT>& nv,const NumVector<dims,nelem,NT>& nvmul)
00182         {
00183           typename NT::number r=0.0;
00184           for(integer i=0;i<dims;i++) r += nv[i]*nvmul[i]; return r;
00185         }
00186 
00188   //    */
00189   //template<typename numT, integer dims>
00190   //inline
00191   //real operator*(const NumVector< std::complex<numT> ,dims>& nv,const NumVector< std::complex<numT>,dims>& nvmul)
00192   //{
00193   //    real r=0.0;
00194   //    for(integer i=0;i<dims;i++) r += nv[i].real()*nvmul[i].real()+nv[i].imag()*nvmul[i].imag(); return r;
00195   //}
00196 
00197   // Previously: template<integer dims, class NT>
00198   template <integer dims, typename nelem, class NT >
00199         typename NT::number
00200         norm(const NumVector<dims,nelem,NT>& nv)
00201         {
00202           return nv*nv;
00203         }
00204 
00205   // Previously: template<integer dims, class NT>
00206   template <integer dims, typename nelem, class NT >
00207         typename NT::number
00208         length(const NumVector<dims,nelem,NT>& nv)
00209         {
00210           return sqrt(norm(nv));
00211         }
00212 
00213 } // end namespace MODEL
00214 #endif
00215 
00216 /*********************************************************************
00217 $Id: numvector.h,v 1.1 2001/05/22 10:54:55 mpeeters Exp $
00218 **********************************************************************
00219 
00220 $Log: numvector.h,v $
00221 Revision 1.1  2001/05/22 10:54:55  mpeeters
00222 Moved sources and headers for libModel to model/ subdirectory, in an attempt to rationalize the source tree. This should make things "netter".
00223 
00224 Revision 1.3  2001/05/21 11:53:16  mpeeters
00225 Removed Makefile.in, which is automatically generated anyway.
00226 
00227 Revision 1.2  2000/09/15 10:26:31  mpeeters
00228 Cleaned out header and added CVS tails to files, removed superfuous
00229 @author comments, inserted dates
00230 
00231 *********************************************************************/

To get the sources or tarballs, please go to SourceForge or you can use the CVS repository.

More Info? Michael Peeters. Also, check our research website: www.alna.vub.ac.be

Last update: June 2002.


Looking for Open Source? Check out SourceForge Logo !