Main Page | Namespace List | Class List | File List | Class Members | File Members

DVector.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Claudio Attaccalite                             *
00003  *   claudio@freescience.info                                              *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 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 General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #ifndef DVector_H
00022 #define DVector_H
00023 
00024 #include <iostream>
00025 #include "lapack_wrapper.h"
00026 #include "blas_wrapper.h"
00027 
00028 using namespace std;
00029 
00030 
00036 class DVector
00037 {
00038  public:
00039           friend class DMatrix;
00040 
00041  private:
00046   int n_1;
00047 
00048 
00053   double* pArray;
00054 
00055 
00060   int lda;
00061 
00062 public:
00068   int dim1(){return n_1;}
00069 
00075   int size(){return n_1;}
00076 
00081   double* array(){return pArray;}
00082 
00089   void allocate(int i)
00090     {
00091       if( n_1 != i )
00092         {
00093           deallocate();
00094           
00095           n_1 = i;
00096           
00097           if(n_1 >= 1) 
00098             {
00099               pArray = new double[n_1];
00100             }
00101           else 
00102             {
00103               n_1 = 0;
00104               pArray = 0;
00105             }
00106         }
00107       lda=1;
00108     }
00109 
00110 
00115   void deallocate()
00116     {
00117       if( n_1 > 0 )
00118         {
00119           delete [] pArray;
00120           pArray = 0;
00121 
00122           n_1 = 0;
00123         }
00124     }
00125 
00126    
00131     double Norm()
00132     {
00133       return F77_FUNC(dnrm2,DNRM2)(&n_1,pArray,&lda);
00134     }
00135  
00140     double AbsoluteSum()
00141     {
00142       return F77_FUNC(dasum,DASUM)(&n_1,pArray,&lda);
00143     }
00144    
00149     double Sum()
00150     {
00151       double sum=0;
00152       for(int i=0;i<n_1;i++) sum+=pArray[i];
00153       return sum;
00154     }
00155 
00156 
00161     int iMax()
00162     {
00163       return F77_FUNC(idamax,IDAMAX)(&n_1,pArray,&lda);
00164     }
00165 
00166 
00171   void operator=(const DVector & rhs)
00172   {
00173    if(n_1 != rhs.n_1) allocate(rhs.n_1);
00174    F77_FUNC(dcopy,DCOPY)(&n_1,rhs.pArray,&lda,pArray,&lda);
00175   }
00176   
00181   void operator=(const double C)
00182   {
00183     for(int i=0; i<n_1; i++)
00184       {
00185         pArray[i] = C;
00186       }
00187   }
00188   
00189 
00194   double operator*( const DVector & rhs)
00195     {
00196 #ifdef DEBUG
00197       if( n_1 != rhs.n_1 )
00198         {
00199           cerr << "ERROR: incorrect sizes for two DVector's in DVector*"
00200                << "DVector" << endl;
00201           exit(1);
00202         }
00203       if( n_1 <= 0 )
00204         {
00205           cerr << "ERROR: DVector of size 0 used in DVector*DVector" << endl;
00206           exit(1);
00207         }
00208 #endif
00209       return  F77_FUNC(ddot,DDOT)(&n_1,pArray,&lda,rhs.pArray,&lda);
00210     }
00211 
00212 
00217   DVector operator*(double rhs)
00218     {
00219       DVector  A(n_1);
00220       double zero=0.0;
00221       F77_FUNC(dscal,DSCAL)(&n_1,&zero,A.pArray,&lda);
00222       F77_FUNC(daxpy,DAXPY)(&n_1,&rhs,pArray,&lda,A.pArray,&lda);
00223       return A;
00224     }
00225 
00226 
00231   DVector operator+( const DVector & rhs)
00232     {
00233 #ifdef DEBUG
00234       if( n_1 != rhs.n_1 )
00235         {
00236           cerr << "ERROR: incorrect sizes for two DVector's in DVector+"
00237                << "DVector" << endl;
00238           exit(1);
00239         }
00240       if( n_1 <= 0 )
00241         {
00242           cerr << "ERROR: DVector of size 0 used in DVector+DVector" << endl;
00243           exit(1);
00244         }
00245 #endif
00246       DVector  A(n_1);
00247       double d_one=1.0;
00248       F77_FUNC(dcopy,DCOPY)(&n_1,pArray,&lda,A.pArray,&lda);
00249       F77_FUNC(daxpy,DAXPY)(&n_1,&d_one,rhs.pArray,&lda,A.pArray,&lda); 
00250       return A;
00251     }
00252 
00253 
00258   DVector operator-( const DVector & rhs)
00259     {
00260 #ifdef DEBUG
00261       if( n_1 != rhs.n_1 )
00262         {
00263           cerr << "ERROR: incorrect sizes for two DVector's in DVector-"
00264                << "DVector" << endl;
00265           exit(1);
00266         }
00267       if( n_1 <= 0 )
00268         {
00269           cerr << "ERROR: DVector of size 0 used in DVector-DVector" << endl;
00270           exit(1);
00271         }
00272 #endif
00273       DVector  A(n_1);
00274       double d_m_one=-1.0;
00275       F77_FUNC(dcopy,DCOPY)(&n_1,pArray,&lda,A.pArray,&lda);
00276       F77_FUNC(daxpy,DAXPY)(&n_1,&d_m_one,rhs.pArray,&lda,A.pArray,&lda); 
00277       return A;
00278     }
00279 
00280 
00285   void operator*=(double C)
00286     {
00287     F77_FUNC(dscal,DSCAL)(&n_1,&C,pArray,&lda);
00288     }
00289 
00290 
00295   void operator/=(double C)
00296     {
00297     double inv=1.0/C;
00298     F77_FUNC(dscal,DSCAL)(&n_1,&inv,pArray,&lda);
00299     }
00300 
00301 
00306   DVector(){pArray = 0; n_1 = 0; lda=1;}
00307 
00308 
00315   DVector(int i){pArray = 0; n_1 = 0; allocate(i); lda=1; }
00316 
00317 
00324   DVector(const DVector & rhs)
00325     {
00326       n_1 = 0;
00327       pArray = 0;
00328       lda = 1;
00329       allocate(rhs.n_1);
00330       F77_FUNC(dcopy,DCOPY)(&n_1,rhs.pArray,&lda,pArray,&lda);
00331     }
00332 
00339   DVector(int dim,double *rhs)
00340     {
00341       n_1 = 0;
00342       pArray = 0;
00343       lda = 1;
00344       allocate(dim);
00345       n_1=dim;
00346       F77_FUNC(dcopy,DCOPY)(&n_1,rhs,&lda,pArray,&lda);
00347     }
00348 
00349    
00354   void set(int ndim,double *rhs)
00355     {
00356 #ifdef DEBUG
00357        if( n_1 != ndim )
00358         {
00359           cerr << "ERROR: incorrect sizes for two Vector's in "
00360                << " DVector.set(n_1,c_array) " << endl;
00361           exit(1);
00362         }
00363 #endif
00364       F77_FUNC(dcopy,DCOPY)(&n_1,rhs,&lda,pArray,&lda);
00365     }
00366   
00371   void get(int dim,double *rhs)
00372     {
00373 #ifdef DEBUG
00374        if( n_1 != dim )
00375         {
00376           cerr << "ERROR: incorrect sizes for two Vector's in "
00377                << " Vector.get(n_1,c_array) " << endl;
00378           exit(1);
00379         }
00380 #endif
00381       F77_FUNC(dcopy,DCOPY)(&n_1,pArray,&lda,rhs,&lda);
00382     }
00383   
00388   ~DVector(){deallocate();}
00389 
00393   double& operator()(int i){return pArray[i];}
00394 
00399   friend ostream& operator<<(ostream & strm, const DVector & rhs)
00400     {
00401       for( int i=0; i<rhs.n_1; i++ )  
00402         {
00403           strm << rhs.pArray[i] << "\t";
00404         }
00405       strm << endl;
00406       return strm;
00407     }
00408 };
00409 
00410 #endif
00411 
00412 
00413 
00414 
00415 
00416 

Generated on Wed Aug 16 19:03:50 2006 for MyLapack by  doxygen 1.4.4