00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef SVector_H
00022 #define SVector_H
00023
00024 #include <iostream>
00025 #include "lapack_wrapper.h"
00026 #include "blas_wrapper.h"
00027
00028 using namespace std;
00029
00030
00036 class SVector
00037 {
00038 public:
00039 friend class SMatrix;
00040
00041 private:
00046 int n_1;
00047
00048
00053 float* pArray;
00054
00055
00060 int lda;
00061
00062 public:
00068 int dim1(){return n_1;}
00069
00075 int size(){return n_1;}
00076
00081 float* 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 float[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 float Norm()
00132 {
00133 return F77_FUNC(snrm2,SNRM2)(&n_1,pArray,&lda);
00134 }
00135
00140 float AbsoluteSum()
00141 {
00142 return F77_FUNC(sasum,SASUM)(&n_1,pArray,&lda);
00143 }
00144
00149 float Sum()
00150 {
00151 float 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(isamax,ISAMAX)(&n_1,pArray,&lda);
00164 }
00165
00166
00171 void operator=(const SVector & rhs)
00172 {
00173 if(n_1 != rhs.n_1) allocate(rhs.n_1);
00174 F77_FUNC(scopy,SCOPY)(&n_1,rhs.pArray,&lda,pArray,&lda);
00175 }
00176
00181 void operator=(const float C)
00182 {
00183 for(int i=0; i<n_1; i++)
00184 {
00185 pArray[i] = C;
00186 }
00187 }
00188
00189
00194 float operator*( const SVector & rhs)
00195 {
00196 #ifdef DEBUG
00197 if( n_1 != rhs.n_1 )
00198 {
00199 cerr << "ERROR: incorrect sizes for two SVector's in SVector*"
00200 << "SVector" << endl;
00201 exit(1);
00202 }
00203 if( n_1 <= 0 )
00204 {
00205 cerr << "ERROR: SVector of size 0 used in SVector*SVector" << endl;
00206 exit(1);
00207 }
00208 #endif
00209 return F77_FUNC(sdot,SDOT)(&n_1,pArray,&lda,rhs.pArray,&lda);
00210 }
00211
00212
00217 SVector operator*(float rhs)
00218 {
00219 SVector A(n_1);
00220 float zero=0.0;
00221 F77_FUNC(sscal,SSCAL)(&n_1,&zero,A.pArray,&lda);
00222 F77_FUNC(saxpy,SAXPY)(&n_1,&rhs,pArray,&lda,A.pArray,&lda);
00223 return A;
00224 }
00225
00226
00231 SVector operator+( const SVector & rhs)
00232 {
00233 #ifdef DEBUG
00234 if( n_1 != rhs.n_1 )
00235 {
00236 cerr << "ERROR: incorrect sizes for two SVector's in SVector+"
00237 << "SVector" << endl;
00238 exit(1);
00239 }
00240 if( n_1 <= 0 )
00241 {
00242 cerr << "ERROR: SVector of size 0 used in SVector+SVector" << endl;
00243 exit(1);
00244 }
00245 #endif
00246 SVector A(n_1);
00247 float d_one=1.0;
00248 F77_FUNC(scopy,SCOPY)(&n_1,pArray,&lda,A.pArray,&lda);
00249 F77_FUNC(saxpy,SAXPY)(&n_1,&d_one,rhs.pArray,&lda,A.pArray,&lda);
00250 return A;
00251 }
00252
00253
00258 SVector operator-( const SVector & rhs)
00259 {
00260 #ifdef DEBUG
00261 if( n_1 != rhs.n_1 )
00262 {
00263 cerr << "ERROR: incorrect sizes for two SVector's in SVector-"
00264 << "SVector" << endl;
00265 exit(1);
00266 }
00267 if( n_1 <= 0 )
00268 {
00269 cerr << "ERROR: SVector of size 0 used in SVector-SVector" << endl;
00270 exit(1);
00271 }
00272 #endif
00273 SVector A(n_1);
00274 float d_m_one=-1.0;
00275 F77_FUNC(scopy,SCOPY)(&n_1,pArray,&lda,A.pArray,&lda);
00276 F77_FUNC(saxpy,SAXPY)(&n_1,&d_m_one,rhs.pArray,&lda,A.pArray,&lda);
00277 return A;
00278 }
00279
00280
00285 void operator*=(float C)
00286 {
00287 F77_FUNC(sscal,SSCAL)(&n_1,&C,pArray,&lda);
00288 }
00289
00290
00295 void operator/=(float C)
00296 {
00297 float inv=1.0/C;
00298 F77_FUNC(sscal,SSCAL)(&n_1,&inv,pArray,&lda);
00299 }
00300
00301
00306 SVector(){pArray = 0; n_1 = 0; lda=1;}
00307
00308
00315 SVector(int i){pArray = 0; n_1 = 0; allocate(i); lda=1; }
00316
00317
00324 SVector(const SVector & rhs)
00325 {
00326 n_1 = 0;
00327 pArray = 0;
00328 lda = 1;
00329 allocate(rhs.n_1);
00330 F77_FUNC(scopy,SCOPY)(&n_1,rhs.pArray,&lda,pArray,&lda);
00331 }
00332
00339 SVector(int dim,float *rhs)
00340 {
00341 n_1 = 0;
00342 pArray = 0;
00343 lda = 1;
00344 allocate(dim);
00345 n_1=dim;
00346 F77_FUNC(scopy,SCOPY)(&n_1,rhs,&lda,pArray,&lda);
00347 }
00348
00349
00354 void set(int ndim,float *rhs)
00355 {
00356 #ifdef DEBUG
00357 if( n_1 != ndim )
00358 {
00359 cerr << "ERROR: incorrect sizes for two Vector's in "
00360 << " SVector.set(n_1,c_array) " << endl;
00361 exit(1);
00362 }
00363 #endif
00364 F77_FUNC(scopy,SCOPY)(&n_1,rhs,&lda,pArray,&lda);
00365 }
00366
00371 void get(int dim,float *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(scopy,SCOPY)(&n_1,pArray,&lda,rhs,&lda);
00382 }
00383
00388 ~SVector(){deallocate();}
00389
00393 float& operator()(int i){return pArray[i];}
00394
00399 friend ostream& operator<<(ostream & strm, const SVector & 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