00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "SawVector.h"
00021
00029 SawVector::SawVector( uint size, double a )
00030 : SawObject()
00031 {
00032 this->size = size ;
00033 vector = gsl_vector_alloc( size ) ;
00034 gsl_vector_set_all( vector, a ) ;
00035 }
00036
00044 SawVector::SawVector( double* array, uint size )
00045 : SawObject()
00046 {
00047 this->size = size ;
00048 vector = gsl_vector_alloc( size ) ;
00049
00050 for ( uint i = 0; i < size; i++ ) {
00051 gsl_vector_set( vector, i, array[ i ] ) ;
00052 }
00054 }
00055
00060 SawVector::SawVector( const SawVector& vector )
00061 : SawObject()
00062 {
00063 this->size = vector.getSize() ;
00064 this->vector = gsl_vector_alloc( size ) ;
00065 gsl_vector_memcpy( this->vector, vector.getGSLVector() ) ;
00067 }
00068
00074 void SawVector::operator = ( const SawVector& vector )
00075 {
00076 this->size = vector.getSize() ;
00077 this->vector = gsl_vector_alloc( size ) ;
00078 gsl_vector_memcpy( this->vector, vector.getGSLVector() ) ;
00080 }
00081
00085 SawVector::~SawVector()
00086 {
00087 gsl_vector_free( vector ) ;
00088 }
00089
00095 void SawVector::swapElements( uint i, uint j )
00096 {
00097 gsl_vector_swap_elements( vector, i, j ) ;
00099 }
00100
00104 void SawVector::reverse()
00105 {
00106 gsl_vector_reverse( vector ) ;
00107 }
00108
00112 gsl_vector* SawVector::getGSLVector() const
00113 {
00114 return vector ;
00115 }
00116
00120 void SawVector::setGSLVector( gsl_vector* vector )
00121 {
00122 this->vector = vector ;
00123 }
00124
00129 uint SawVector::getSize() const
00130 {
00131 return size ;
00132 }
00133
00139 double SawVector::get( uint i ) const
00140 {
00141 return gsl_vector_get( vector, i ) ;
00143 }
00144
00149 void SawVector::set( uint i, double value )
00150 {
00151 gsl_vector_set( vector, i, value ) ;
00153 }
00154
00160 bool SawVector::operator == ( SawVector vector )
00161 {
00162 for ( uint i = 0; i < this->size; i++ ) {
00163 if ( gsl_fcmp( get( i ), vector.get( i ), thresholdDoubleComparation ) != 0 )
00164 return false ;
00165 }
00166
00168 return true ;
00169 }
00170
00176 bool SawVector::operator != ( SawVector vector )
00177 {
00178 for ( uint i = 0; i < this->size; i++ ) {
00179 if ( gsl_fcmp( get( i ), vector.get( i ), thresholdDoubleComparation ) == 0 )
00180 return false ;
00181 }
00182
00184 return true ;
00185 }
00186
00191 double SawVector::getMax( uint* i ) const
00192 {
00193 if ( i ) {
00194 *i = gsl_vector_max_index( vector ) ;
00195 }
00196 return gsl_vector_max( vector ) ;
00197 }
00198
00203 double SawVector::getMin( uint* i ) const
00204 {
00205 if ( i ) {
00206 *i = gsl_vector_min_index( vector ) ;
00207 }
00208 return gsl_vector_min( vector ) ;
00209 }
00210
00216 bool SawVector::isNull() const
00217 {
00218 if ( gsl_vector_isnull( vector ) == 1 )
00219 return true ;
00220 else
00221 return false ;
00222 }
00223
00229 SawVector SawVector::operator + ( SawVector vector )
00230 {
00231 gsl_vector * tmp = gsl_vector_alloc( size ) ;
00232 gsl_vector_memcpy( tmp, this->vector ) ;
00233 gsl_vector_add( tmp, vector.getGSLVector() ) ;
00234
00235 SawVector output( size ) ;
00236 output.setGSLVector( tmp ) ;
00237
00238 return output ;
00239 }
00240
00246 SawVector SawVector::operator - ( SawVector vector )
00247 {
00248 gsl_vector * tmp = gsl_vector_alloc( size ) ;
00249 gsl_vector_memcpy( tmp, this->vector ) ;
00250 gsl_vector_sub( tmp, vector.getGSLVector() ) ;
00251
00252 SawVector output( size ) ;
00253 output.setGSLVector( tmp ) ;
00254
00255 return output ;
00256 }
00257
00263 double SawVector::operator * ( SawVector vector )
00264 {
00265 double output ;
00266 gsl_blas_ddot( this->vector, vector.getGSLVector(), &output ) ;
00267
00268 return output ;
00269 }
00270
00275 double SawVector::getEuclideanNorm()
00276 {
00277 return gsl_blas_dnrm2( this->vector ) ;
00278 }
00279
00286 SawVector operator * ( SawVector vector, double a )
00287 {
00288 gsl_vector * tmp = gsl_vector_alloc( vector.getSize() ) ;
00289 gsl_vector_memcpy( tmp, vector.getGSLVector() ) ;
00290 gsl_vector_scale( tmp, a ) ;
00291
00292 SawVector output( vector.getSize() ) ;
00293 output.setGSLVector( tmp ) ;
00294
00295 return output ;
00296 }
00297
00304 SawVector operator * ( double a, SawVector vector )
00305 {
00306 return vector * a ;
00307 }
00308
00315 SawVector operator + ( SawVector vector, double a )
00316 {
00317 gsl_vector * tmp = gsl_vector_alloc( vector.getSize() ) ;
00318 gsl_vector_memcpy( tmp, vector.getGSLVector() ) ;
00319 gsl_vector_add_constant( tmp, a ) ;
00320
00321 SawVector output( vector.getSize() ) ;
00322 output.setGSLVector( tmp ) ;
00323
00324 return output ;
00325 }
00326
00333 SawVector operator + ( double a, SawVector vector )
00334 {
00335 return vector + a ;
00336 }
00337
00344 ostream& operator << ( ostream& os, const SawVector& vector )
00345 {
00346 switch ( vector.getFormatNumberOutputStream() ) {
00347 case SawObject::FIXED:
00348 os.setf( ios::fixed , ios::floatfield ) ;
00349 break ;
00350
00351 case SawObject::SCIENTIFIC:
00352 os.setf( ios::scientific, ios::floatfield ) ;
00353 break ;
00354 }
00355
00356 os.precision( vector.precisionOutputStream ) ;
00357
00358 os << setw( vector.widthOutputStream - vector.precisionOutputStream ) << "[" ;
00359
00360 for ( uint i = 0; i < vector.size; i++ ) {
00361
00362
00363
00364 switch ( vector.getFormatNumberOutputStream() ) {
00365 case SawObject::FIXED:
00366 os << setw( vector.widthOutputStream ) << gsl_vector_get( vector.vector, i ) ;
00367 break ;
00368
00369 case SawObject::SCIENTIFIC:
00370 os << setw( vector.widthOutputStream + 3 ) << gsl_vector_get( vector.vector, i ) ;
00371 break ;
00372 }
00373 }
00374
00375 os << setw( vector.widthOutputStream - vector.precisionOutputStream - 1 ) << "]" ;
00376
00377 return os ;
00378 }
00379
00386 istream& operator >> ( istream& is, const SawVector& vector )
00387 {
00388 double tmp ;
00389 for ( uint i = 0; i < vector.size; i++ ) {
00390 is >> tmp ;
00391 gsl_vector_set( vector.vector, i, tmp ) ;
00392 }
00393 return is ;
00394 }
00395