00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef WVALUE_H
00026 #define WVALUE_H
00027
00028 #include <algorithm>
00029 #include <cmath>
00030 #include <vector>
00031
00032 #include "../WAssert.h"
00033 #include "../WStringUtils.h"
00034 #include "linearAlgebra/WLinearAlgebra.h"
00035
00036
00037
00038
00039 template< typename T > class WValue
00040 {
00041 template< typename S > friend class WValue;
00042
00043
00044
00045 template< typename U > friend std::ostream& operator<<( std::ostream& os, const WValue< U > &rhs );
00046 template< typename U > friend std::istream& operator>>( std::istream& in, WValue< U >& rhs );
00047
00048 public:
00049
00050
00051
00052
00053
00054 explicit WValue( size_t nbComponents )
00055 : m_components( nbComponents )
00056 {
00057 }
00058
00059
00060
00061
00062
00063 WValue( const WValue& newValue )
00064 : m_components( newValue.m_components )
00065 {
00066 }
00067
00068
00069
00070
00071
00072 template< typename S > explicit WValue( const WValue< S >& newValue )
00073 {
00074 m_components.resize( newValue.m_components.size() );
00075 for( size_t i = 0; i < m_components.size(); ++i )
00076 {
00077 m_components[i] = newValue.m_components[i];
00078 }
00079 }
00080
00081
00082
00083
00084
00085 explicit WValue( const WVector_2& newValues )
00086 : m_components( static_cast< std::size_t >( newValues.size() ) )
00087 {
00088 for( std::size_t i = 0; i < m_components.size(); ++i )
00089 {
00090 m_components[ i ] = static_cast< T >( newValues( i ) );
00091 }
00092 }
00093
00094
00095
00096
00097
00098 size_t size() const
00099 {
00100 return m_components.size();
00101 }
00102
00103
00104
00105
00106
00107
00108
00109 T& operator[]( size_t i )
00110 {
00111 WAssert( i < m_components.size(), "Index out of bounds." );
00112 return m_components[i];
00113 }
00114
00115
00116
00117
00118
00119
00120
00121 const T& operator[]( size_t i ) const
00122 {
00123 WAssert( i < m_components.size(), "Index out of bounds." );
00124 return m_components[i];
00125 }
00126
00127
00128
00129
00130
00131
00132 bool operator==( const WValue& rhs ) const
00133 {
00134 return ( m_components == rhs.m_components );
00135 }
00136
00137
00138
00139
00140
00141
00142 bool operator!=( const WValue& rhs ) const
00143 {
00144 return ( m_components != rhs.m_components );
00145 }
00146
00147
00148
00149
00150
00151
00152 WValue& operator=( const WValue& rhs )
00153 {
00154 m_components = rhs.m_components;
00155 return *this;
00156 }
00157
00158
00159
00160
00161
00162
00163 WValue& operator+=( const WValue& rhs )
00164 {
00165 WAssert( m_components.size() == rhs.m_components.size(), "Incompatible sizes of lhs and rhs of operator." );
00166 for( unsigned int i = 0; i < m_components.size(); ++i )
00167 m_components[i] += rhs.m_components[i];
00168 return *this;
00169 }
00170
00171
00172
00173
00174
00175
00176 WValue& operator-=( const WValue& rhs )
00177 {
00178 WAssert( m_components.size() == rhs.m_components.size(), "Incompatible sizes of lhs and rhs of operator." );
00179 for( unsigned int i = 0; i < m_components.size(); ++i )
00180 m_components[i] -= rhs.m_components[i];
00181 return *this;
00182 }
00183
00184
00185
00186
00187
00188
00189 WValue& operator*=( double rhs )
00190 {
00191 for( unsigned int i = 0; i < m_components.size(); ++i )
00192 m_components[i] *= rhs;
00193 return *this;
00194 }
00195
00196
00197
00198
00199
00200
00201
00202 WValue& operator*=( const WValue& rhs )
00203 {
00204 WAssert( m_components.size() == rhs.m_components.size(), "Incompatible sizes of lhs and rhs of operator." );
00205 for( unsigned int i = 0; i < m_components.size(); ++i )
00206 m_components[i] *= rhs.m_components[i];
00207 return *this;
00208 }
00209
00210
00211
00212
00213
00214
00215 WValue& operator/=( const double rhs )
00216 {
00217 for( unsigned int i = 0; i < m_components.size(); ++i )
00218 m_components[i] /= rhs;
00219 return *this;
00220 }
00221
00222
00223
00224
00225
00226
00227
00228 const WValue operator+( const WValue& summand2 ) const
00229 {
00230 WAssert( m_components.size() == summand2.m_components.size(), "Incompatible sizes of summands." );
00231 WValue result( *this );
00232 result += summand2;
00233 return result;
00234 }
00235
00236
00237
00238
00239
00240
00241 const WValue operator-( const WValue& subtrahend ) const
00242 {
00243 WAssert( m_components.size() == subtrahend.m_components.size(), "Incompatible sizes of subtrahend and minuend." );
00244 WValue result( *this );
00245 result -= subtrahend;
00246 return result;
00247 }
00248
00249
00250
00251
00252
00253
00254 const WValue operator*( const WValue& factor2 ) const
00255 {
00256 WAssert( m_components.size() == factor2.m_components.size(), "Incompatible sizes of factors." );
00257 WValue result( *this );
00258 result *= factor2;
00259 return result;
00260 }
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 double norm() const
00271 {
00272 return sqrt( this->normSquare() );
00273 }
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284 double normSquare() const
00285 {
00286 double normSquare = 0.0;
00287
00288 for( unsigned int i = 0; i < m_components.size(); ++i )
00289 {
00290 normSquare += m_components[i] * m_components[i];
00291 }
00292
00293 return normSquare;
00294 }
00295
00296
00297
00298
00299 void normalize()
00300 {
00301 double currentNorm = norm();
00302 for( unsigned int i = 0; i < m_components.size(); ++i )
00303 {
00304 WAssert( currentNorm > 0.0, "Norm is non-positive!" );
00305 m_components[i] /= currentNorm;
00306 }
00307 }
00308
00309
00310
00311
00312
00313 WValue normalized() const
00314 {
00315 WValue result = *this;
00316 result.normalize();
00317 return result;
00318 }
00319
00320
00321
00322
00323
00324 T mean() const
00325 {
00326 WAssert( !m_components.empty(), "WValue has no entries." );
00327 T sum = 0;
00328 for( typename std::vector< T >::const_iterator it = m_components.begin(); it != m_components.end(); it++ )
00329 {
00330 sum += ( *it );
00331 }
00332 return ( sum / static_cast< T >( m_components.size() ) );
00333 }
00334
00335
00336
00337
00338
00339 T median() const
00340 {
00341 WAssert( !m_components.empty(), "WValue has no entries. " );
00342 std::vector< T > components( m_components );
00343 std::sort( components.begin(), components.end() );
00344 return components[ components.size() / 2 ];
00345 }
00346
00347
00348
00349
00350
00351 void resize( size_t size )
00352 {
00353 m_components.resize( size );
00354 }
00355
00356
00357
00358
00359
00360 WVector_2 toWVector()
00361 {
00362 WVector_2 result( m_components.size() );
00363 for( size_t i = 0; i < m_components.size(); ++i )
00364 {
00365 result( i ) = static_cast<double>( m_components[ i ] );
00366 }
00367 return result;
00368 }
00369
00370 protected:
00371 private:
00372
00373
00374
00375 std::vector< T > m_components;
00376 };
00377
00378
00379
00380
00381
00382
00383
00384 template< typename T > inline const WValue< T > operator*( const WValue< T >& lhs, double rhs )
00385 {
00386 WValue< T > result( lhs );
00387 result *= rhs;
00388 return result;
00389 }
00390
00391
00392
00393
00394
00395
00396
00397 template< typename T > inline const WValue< T > operator*( double lhs, const WValue< T >& rhs )
00398 {
00399 WValue< T > result( rhs );
00400 result *= lhs;
00401 return result;
00402 }
00403
00404
00405
00406
00407
00408
00409
00410 template< typename T > inline const WValue< T > operator/( const WValue< T >& lhs, double rhs )
00411 {
00412 WValue< T > result( lhs );
00413 result /= rhs;
00414 return result;
00415 }
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425 template< typename U > inline std::ostream& operator<<( std::ostream& os, const WValue< U > &rhs )
00426 {
00427 return string_utils::operator<<( os, rhs.m_components );
00428 }
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438 template< typename U > inline std::istream& operator>>( std::istream& in, WValue< U >& rhs )
00439 {
00440 return string_utils::operator>>( in, rhs.m_components );
00441 }
00442
00443 #endif // WVALUE_H