00001
00002 #ifndef vnl_vector_fixed_h_
00003 #define vnl_vector_fixed_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <vcl_cstring.h>
00032 #include <vcl_cassert.h>
00033 #include <vcl_iosfwd.h>
00034 #include "vnl_vector.h"
00035 #include "vnl_vector_ref.h"
00036 #include <vnl/vnl_c_vector.h>
00037 #include <vnl/vnl_matrix.h>
00038 #include <vnl/vnl_config.h>
00039
00040 export template <class T, unsigned int n> class vnl_vector_fixed;
00041 export template <class T, unsigned int num_rows, unsigned int num_cols> class vnl_matrix_fixed;
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 template <class T, unsigned int n>
00085 class vnl_vector_fixed
00086 {
00087 protected:
00088 T data_[n];
00089
00090 public:
00091 typedef vnl_vector_fixed<T,n> self;
00092 typedef unsigned int size_type;
00093
00094 enum { SIZE = n };
00095
00096
00097
00098
00099
00100
00101 vnl_vector_fixed() {}
00102
00103
00104
00105 vnl_vector_fixed( const vnl_vector_fixed<T,n>& rhs )
00106 {
00107 vcl_memcpy( data_, rhs.data_, sizeof data_ );
00108 }
00109
00110
00111
00112 vnl_vector_fixed( const vnl_vector<T>& rhs )
00113 {
00114 assert( n == rhs.size() );
00115 vcl_memcpy( data_, rhs.data_block(), sizeof data_ );
00116 }
00117
00118
00119 explicit vnl_vector_fixed( const T& v ) { fill( v ); }
00120
00121
00122
00123 explicit vnl_vector_fixed( const T* datablck )
00124 {
00125 vcl_memcpy( data_, datablck, sizeof data_ );
00126 }
00127
00128
00129
00130
00131 vnl_vector_fixed( const T& x0, const T& x1 )
00132 {
00133 assert( n == 2 );
00134 data_[0] = x0; data_[1] = x1;
00135 }
00136
00137
00138
00139
00140 vnl_vector_fixed( const T& x0, const T& x1, const T& x2 )
00141 {
00142 assert( n == 3 );
00143 data_[0] = x0; data_[1] = x1; data_[2] = x2;
00144 }
00145
00146
00147 vnl_vector_fixed( const T& x0, const T& x1, const T& x2, const T& x3 )
00148 {
00149 assert( n == 4 );
00150 data_[0] = x0; data_[1] = x1; data_[2] = x2; data_[3] = x3;
00151 }
00152
00153
00154 vnl_vector_fixed<T,n>& operator=( const vnl_vector_fixed<T,n>& rhs ) {
00155 vcl_memcpy( data_, rhs.data_, sizeof data_ );
00156 return *this;
00157 }
00158
00159
00160
00161 vnl_vector_fixed<T,n>& operator=( const vnl_vector<T>& rhs) {
00162 assert( n == rhs.size() );
00163 vcl_memcpy( data_, rhs.data_block(), sizeof data_ );
00164 return *this;
00165 }
00166
00167
00168
00169 unsigned size() const { return n; }
00170
00171
00172 void put (unsigned int i, T const& v) { data_[i] = v; }
00173
00174
00175 T get (unsigned int i) const { return data_[i]; }
00176
00177
00178 vnl_vector_fixed& fill( T const& v )
00179 {
00180 for ( size_type i = 0; i < n; ++i )
00181 data_[i] = v;
00182 return *this;
00183 }
00184
00185
00186
00187 vnl_vector_fixed& copy_in( T const * ptr )
00188 {
00189 for ( size_type i = 0; i < n; ++i )
00190 data_[i] = ptr[i];
00191 return *this;
00192 }
00193
00194
00195
00196 void copy_out( T* ptr ) const
00197 {
00198 for ( size_type i = 0; i < n; ++i )
00199 ptr[i] = data_[i];
00200 }
00201
00202
00203
00204 vnl_vector_fixed& set( T const *ptr ) { return copy_in(ptr); }
00205
00206
00207
00208 T & operator() (unsigned int i)
00209 {
00210 #if VNL_CONFIG_CHECK_BOUNDS && (!defined NDEBUG)
00211 assert(i<n);
00212 #endif
00213 return data_[i];
00214 }
00215
00216
00217
00218 T const & operator() (unsigned int i) const
00219 {
00220 #if VNL_CONFIG_CHECK_BOUNDS && (!defined NDEBUG)
00221 assert(i<n);
00222 #endif
00223 return data_[i];
00224 }
00225
00226
00227 T& operator[] ( unsigned int i ) { return data_[i]; }
00228
00229
00230 const T& operator[] ( unsigned int i ) const { return data_[i]; }
00231
00232
00233
00234
00235 T const* data_block() const { return data_; }
00236
00237
00238
00239
00240 T * data_block() { return data_; }
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 vnl_vector_ref<T> as_ref() { return vnl_vector_ref<T>( n, data_ ); }
00256
00257
00258
00259
00260
00261
00262 const vnl_vector_ref<T> as_ref() const { return vnl_vector_ref<T>( n, const_cast<T*>(data_) ); }
00263
00264
00265
00266
00267
00268 operator const vnl_vector_ref<T>() const { return vnl_vector_ref<T>( n, const_cast<T*>(data_) ); }
00269
00270
00271
00272
00273 typedef T element_type;
00274
00275 typedef T *iterator;
00276
00277 iterator begin() { return data_; }
00278
00279
00280 iterator end() { return data_+n; }
00281
00282
00283 typedef T const *const_iterator;
00284
00285 const_iterator begin() const { return data_; }
00286
00287 const_iterator end() const { return data_+n; }
00288
00289
00290
00291
00292 vnl_vector_fixed<T,n> apply(T (*f)(T));
00293
00294
00295
00296 vnl_vector_fixed<T,n> apply(T (*f)(const T&));
00297
00298
00299 vnl_vector_fixed<T,n>& operator+=( T s ) { self::add( data_, s, data_ ); return *this; }
00300
00301
00302 vnl_vector_fixed<T,n>& operator-=( T s ) { self::sub( data_, s, data_ ); return *this; }
00303
00304
00305 vnl_vector_fixed<T,n>& operator*=( T s ) { self::mul( data_, s, data_ ); return *this; }
00306
00307
00308 vnl_vector_fixed<T,n>& operator/=( T s ) { self::div( data_, s, data_ ); return *this; }
00309
00310
00311 vnl_vector_fixed<T,n>& operator+=( const vnl_vector_fixed<T,n>& v ) { self::add( data_, v.data_block(), data_ ); return *this; }
00312
00313
00314 vnl_vector_fixed<T,n>& operator-=( const vnl_vector_fixed<T,n>& v ) { self::sub( data_, v.data_block(), data_ ); return *this; }
00315
00316
00317 vnl_vector_fixed<T,n>& operator+=( const vnl_vector<T>& v )
00318 {
00319 assert( v.size() == n );
00320 self::add( data_, v.data_block(), data_ ); return *this;
00321 }
00322
00323
00324 vnl_vector_fixed<T,n>& operator-=( const vnl_vector<T>& v )
00325 {
00326 assert( v.size() == n );
00327 self::sub( data_, v.data_block(), data_ ); return *this;
00328 }
00329
00330
00331 vnl_vector_fixed<T,n> operator-() const
00332 {
00333 vnl_vector_fixed<T,n> result;
00334 self::sub( (T)0, data_, result.data_ );
00335 return result;
00336 }
00337
00338
00339 vnl_vector<T> extract (unsigned int len, unsigned int start=0) const;
00340
00341
00342 vnl_vector<T> as_vector() const { return extract(n); }
00343
00344
00345 vnl_vector_fixed& update (vnl_vector<T> const&, unsigned int start=0);
00346
00347
00348 typedef typename vnl_c_vector<T>::abs_t abs_t;
00349
00350
00351 abs_t squared_magnitude() const { return vnl_c_vector<T>::two_nrm2(begin(), size()); }
00352
00353
00354 abs_t magnitude() const { return two_norm(); }
00355
00356
00357 abs_t one_norm() const { return vnl_c_vector<T>::one_norm(begin(), size()); }
00358
00359
00360 abs_t two_norm() const { return vnl_c_vector<T>::two_norm(begin(), size()); }
00361
00362
00363 abs_t inf_norm() const { return vnl_c_vector<T>::inf_norm(begin(), size()); }
00364
00365
00366 vnl_vector_fixed<T,n>& normalize() { vnl_c_vector<T>::normalize(begin(), size()); return *this; }
00367
00368
00369
00370
00371
00372 abs_t rms () const { return vnl_c_vector<T>::rms_norm(begin(), size()); }
00373
00374
00375 T min_value () const { return vnl_c_vector<T>::min_value(begin(), size()); }
00376
00377
00378 T max_value () const { return vnl_c_vector<T>::max_value(begin(), size()); }
00379
00380
00381 unsigned arg_min() const { return vnl_c_vector<T>::arg_min(begin(), size()); }
00382
00383
00384 unsigned arg_max() const { return vnl_c_vector<T>::arg_max(begin(), size()); }
00385
00386
00387 T mean() const { return vnl_c_vector<T>::mean(begin(), size()); }
00388
00389
00390 T sum() const { return vnl_c_vector<T>::sum(begin(), size()); }
00391
00392
00393
00394 vnl_vector_fixed& flip();
00395
00396
00397
00398 void assert_size( unsigned sz ) const { assert( sz == n ); }
00399
00400
00401
00402 void assert_finite() const
00403 {
00404 #ifndef NDEBUG
00405 assert_finite_internal();
00406 #endif
00407 }
00408
00409
00410 bool is_finite() const;
00411
00412
00413 bool is_zero() const;
00414
00415
00416 bool empty() const { return n==0; }
00417
00418
00419 bool operator_eq (vnl_vector_fixed<T,n> const& v) const
00420 {
00421 for ( size_type i = 0; i < n; ++i )
00422 if ( (*this)[i] != v[i] )
00423 return false;
00424 return true;
00425 }
00426
00427
00428 bool operator_eq (vnl_vector<T> const& v) const
00429 {
00430 assert( v.size() == n );
00431 for ( size_type i = 0; i < n; ++i )
00432 if ( (*this)[i] != v[i] )
00433 return false;
00434 return true;
00435 }
00436
00437
00438
00439 bool read_ascii(vcl_istream& s);
00440
00441
00442
00443 void print( vcl_ostream& s ) const;
00444
00445 public:
00446
00447
00448
00449 inline static void add( const T* a, const T* b, T* r )
00450 {
00451 for ( unsigned int i=0; i < n; ++i,++r,++a,++b )
00452 *r = *a + *b;
00453 }
00454
00455 inline static void add( const T* a, T b, T* r )
00456 {
00457 for ( unsigned int i=0; i < n; ++i,++r,++a )
00458 *r = *a + b;
00459 }
00460
00461 inline static void sub( const T* a, const T* b, T* r )
00462 {
00463 for ( unsigned int i=0; i < n; ++i,++r,++a,++b )
00464 *r = *a - *b;
00465 }
00466
00467 inline static void sub( const T* a, T b, T* r )
00468 {
00469 for ( unsigned int i=0; i < n; ++i,++r,++a )
00470 *r = *a - b;
00471 }
00472
00473 inline static void sub( T a, const T* b, T* r )
00474 {
00475 for ( unsigned int i=0; i < n; ++i,++r,++b )
00476 *r = a - *b;
00477 }
00478
00479 inline static void mul( const T* a, const T* b, T* r )
00480 {
00481 for ( unsigned int i=0; i < n; ++i,++r,++a,++b )
00482 *r = *a * *b;
00483 }
00484
00485 inline static void mul( const T* a, T b, T* r )
00486 {
00487 for ( unsigned int i=0; i < n; ++i,++r,++a )
00488 *r = *a * b;
00489 }
00490
00491 inline static void div( const T* a, const T* b, T* r )
00492 {
00493 for ( unsigned int i=0; i < n; ++i,++r,++a,++b )
00494 *r = *a / *b;
00495 }
00496
00497 inline static void div( const T* a, T b, T* r )
00498 {
00499 for ( unsigned int i=0; i < n; ++i,++r,++a )
00500 *r = *a / b;
00501 }
00502
00503 private:
00504
00505 void assert_finite_internal() const;
00506 };
00507
00508
00509
00510
00511
00512
00513 template<class T, unsigned int n>
00514 inline vnl_vector_fixed<T,n> operator+( const vnl_vector_fixed<T,n>& v, T s )
00515 {
00516 vnl_vector_fixed<T,n> r;
00517 vnl_vector_fixed<T,n>::add( v.data_block(), s, r.data_block() );
00518 return r;
00519 }
00520
00521
00522
00523 template<class T, unsigned int n>
00524 inline vnl_vector_fixed<T,n> operator+( const T& s,
00525 const vnl_vector_fixed<T,n>& v )
00526 {
00527 vnl_vector_fixed<T,n> r;
00528 vnl_vector_fixed<T,n>::add( v.data_block(), s, r.data_block() );
00529 return r;
00530 }
00531
00532
00533
00534 template<class T, unsigned int n>
00535 inline vnl_vector_fixed<T,n> operator-( const vnl_vector_fixed<T,n>& v, T s )
00536 {
00537 vnl_vector_fixed<T,n> r;
00538 vnl_vector_fixed<T,n>::sub( v.data_block(), s, r.data_block() );
00539 return r;
00540 }
00541
00542
00543
00544 template<class T, unsigned int n>
00545 inline vnl_vector_fixed<T,n> operator-( const T& s,
00546 const vnl_vector_fixed<T,n>& v )
00547 {
00548 vnl_vector_fixed<T,n> r;
00549 vnl_vector_fixed<T,n>::sub( s, v.data_block(), r.data_block() );
00550 return r;
00551 }
00552
00553
00554
00555 template<class T, unsigned int n>
00556 inline vnl_vector_fixed<T,n> operator*( const vnl_vector_fixed<T,n>& v, T s )
00557 {
00558 vnl_vector_fixed<T,n> r;
00559 vnl_vector_fixed<T,n>::mul( v.data_block(), s, r.data_block() );
00560 return r;
00561 }
00562
00563
00564
00565 template<class T, unsigned int n>
00566 inline vnl_vector_fixed<T,n> operator*( const T& s,
00567 const vnl_vector_fixed<T,n>& v )
00568 {
00569 vnl_vector_fixed<T,n> r;
00570 vnl_vector_fixed<T,n>::mul( v.data_block(), s, r.data_block() );
00571 return r;
00572 }
00573
00574
00575
00576 template<class T, unsigned int n>
00577 inline vnl_vector_fixed<T,n> operator/( const vnl_vector_fixed<T,n>& v, T s )
00578 {
00579 vnl_vector_fixed<T,n> r;
00580 vnl_vector_fixed<T,n>::div( v.data_block(), s, r.data_block() );
00581 return r;
00582 }
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593 template<class T, unsigned int n>
00594 inline vnl_vector_fixed<T,n> operator+( const vnl_vector_fixed<T,n>& a, const vnl_vector_fixed<T,n>& b )
00595 {
00596 vnl_vector_fixed<T,n> r;
00597 vnl_vector_fixed<T,n>::add( a.data_block(), b.data_block(), r.data_block() );
00598 return r;
00599 }
00600
00601
00602
00603
00604 template<class T, unsigned int n>
00605 inline vnl_vector<T> operator+( const vnl_vector_fixed<T,n>& a, const vnl_vector<T>& b )
00606 {
00607 return a.as_ref() + b;
00608 }
00609
00610
00611
00612
00613 template<class T, unsigned int n>
00614 inline vnl_vector<T> operator+( const vnl_vector<T>& a, const vnl_vector_fixed<T,n>& b )
00615 {
00616 return a + b.as_ref();
00617 }
00618
00619
00620
00621 template<class T, unsigned int n>
00622 inline vnl_vector_fixed<T,n> operator-( const vnl_vector_fixed<T,n>& a, const vnl_vector_fixed<T,n>& b )
00623 {
00624 vnl_vector_fixed<T,n> r;
00625 vnl_vector_fixed<T,n>::sub( a.data_block(), b.data_block(), r.data_block() );
00626 return r;
00627 }
00628
00629
00630
00631
00632 template<class T, unsigned int n>
00633 inline vnl_vector<T> operator-( const vnl_vector_fixed<T,n>& a, const vnl_vector<T>& b )
00634 {
00635 return a.as_ref() - b;
00636 }
00637
00638
00639
00640
00641 template<class T, unsigned int n>
00642 inline vnl_vector<T> operator-( const vnl_vector<T>& a, const vnl_vector_fixed<T,n>& b )
00643 {
00644 return a - b.as_ref();
00645 }
00646
00647
00648
00649 template<class T, unsigned int n>
00650 inline vnl_vector_fixed<T,n> element_product( const vnl_vector_fixed<T,n>& a, const vnl_vector_fixed<T,n>& b )
00651 {
00652 vnl_vector_fixed<T,n> r;
00653 vnl_vector_fixed<T,n>::mul( a.data_block(), b.data_block(), r.data_block() );
00654 return r;
00655 }
00656
00657
00658
00659
00660 template<class T, unsigned int n>
00661 inline vnl_vector<T> element_product( const vnl_vector_fixed<T,n>& a, const vnl_vector<T>& b )
00662 {
00663 assert( b.size() == n );
00664 vnl_vector<T> r(n);
00665 vnl_vector_fixed<T,n>::mul( a.data_block(), b.data_block(), r.data_block() );
00666 return r;
00667 }
00668
00669
00670
00671
00672 template<class T, unsigned int n>
00673 inline vnl_vector<T> element_product( const vnl_vector<T>& a, const vnl_vector_fixed<T,n>& b )
00674 {
00675 assert( a.size() == n );
00676 vnl_vector<T> r(n);
00677 vnl_vector_fixed<T,n>::mul( a.data_block(), b.data_block(), r.data_block() );
00678 return r;
00679 }
00680
00681
00682
00683 template<class T, unsigned int n>
00684 inline vnl_vector_fixed<T,n> element_quotient( const vnl_vector_fixed<T,n>& a, const vnl_vector_fixed<T,n>& b )
00685 {
00686 vnl_vector_fixed<T,n> r;
00687 vnl_vector_fixed<T,n>::div( a.data_block(), b.data_block(), r.data_block() );
00688 return r;
00689 }
00690
00691
00692
00693
00694 template<class T, unsigned int n>
00695 inline vnl_vector<T> element_quotient( const vnl_vector_fixed<T,n>& a, const vnl_vector<T>& b )
00696 {
00697 assert( b.size() == n );
00698 vnl_vector<T> r(n);
00699 vnl_vector_fixed<T,n>::div( a.data_block(), b.data_block(), r.data_block() );
00700 return r;
00701 }
00702
00703
00704
00705
00706 template<class T, unsigned int n>
00707 inline vnl_vector<T> element_quotient( const vnl_vector<T>& a, const vnl_vector_fixed<T,n>& b )
00708 {
00709 assert( a.size() == n );
00710 vnl_vector<T> r(n);
00711 vnl_vector_fixed<T,n>::div( a.data_block(), b.data_block(), r.data_block() );
00712 return r;
00713 }
00714
00715
00716
00717 template<class T, unsigned n>
00718 inline T dot_product( const vnl_vector_fixed<T,n>& a, const vnl_vector_fixed<T,n>& b )
00719 {
00720 return dot_product( a.as_ref(), b.as_ref() );
00721 }
00722
00723
00724
00725
00726 template<class T, unsigned n>
00727 inline T dot_product( const vnl_vector_fixed<T,n>& a, const vnl_vector<T>& b )
00728 {
00729 return dot_product( a.as_ref(), b );
00730 }
00731
00732
00733
00734
00735 template<class T, unsigned n>
00736 inline T dot_product( const vnl_vector<T>& a, const vnl_vector_fixed<T,n>& b )
00737 {
00738 return dot_product( a, b.as_ref() );
00739 }
00740
00741
00742
00743
00744 template<class T, unsigned int n>
00745 inline vnl_matrix<T> outer_product( const vnl_vector<T>& a, const vnl_vector_fixed<T,n>& b )
00746 {
00747 return outer_product( a, b.as_ref());
00748 }
00749
00750
00751
00752
00753 template<class T, unsigned int n>
00754 inline vnl_matrix<T> outer_product( const vnl_vector_fixed<T,n>& a, const vnl_vector<T>& b )
00755 {
00756 return outer_product( a.as_ref(), b);
00757 }
00758
00759
00760
00761 template<class T, unsigned n>
00762 inline T angle( const vnl_vector_fixed<T,n>& a, const vnl_vector_fixed<T,n>& b )
00763 {
00764 return angle( a.as_ref(), b.as_ref() );
00765 }
00766
00767
00768
00769
00770 template<class T, unsigned n>
00771 inline T angle( const vnl_vector_fixed<T,n>& a, const vnl_vector<T>& b )
00772 {
00773 return angle( a.as_ref(), b );
00774 }
00775
00776
00777
00778
00779 template<class T, unsigned n>
00780 inline T angle( const vnl_vector<T>& a, const vnl_vector_fixed<T,n>& b )
00781 {
00782 return angle( a, b.as_ref() );
00783 }
00784
00785
00786
00787
00788 template<class T, unsigned n>
00789 inline T vnl_vector_ssd( const vnl_vector_fixed<T,n>& a, const vnl_vector_fixed<T,n>& b )
00790 {
00791 return vnl_vector_ssd( a.as_ref(), b.as_ref() );
00792 }
00793
00794
00795
00796
00797 template<class T, unsigned n>
00798 inline T vnl_vector_ssd( const vnl_vector_fixed<T,n>& a, const vnl_vector<T>& b )
00799 {
00800 return vnl_vector_ssd( a.as_ref(), b );
00801 }
00802
00803
00804
00805
00806 template<class T, unsigned n>
00807 inline T vnl_vector_ssd( const vnl_vector<T>& a, const vnl_vector_fixed<T,n>& b )
00808 {
00809 return vnl_vector_ssd( a, b.as_ref() );
00810 }
00811
00812
00813
00814
00815 template<class T, unsigned int n>
00816 inline bool operator==( const vnl_vector_fixed<T,n>& a, const vnl_vector_fixed<T,n>& b )
00817 {
00818 return a.operator_eq(b);
00819 }
00820
00821
00822
00823
00824 template<class T, unsigned int n>
00825 inline bool operator==( vnl_vector_fixed<T,n> const& a, vnl_vector<T> const& b )
00826 {
00827 return a.operator_eq(b);
00828 }
00829
00830
00831
00832
00833 template<class T, unsigned int n>
00834 inline bool operator==( vnl_vector<T> const& a, vnl_vector_fixed<T,n> const& b )
00835 {
00836 return b.operator_eq(a);
00837 }
00838
00839
00840
00841 template<class T, unsigned int n>
00842 inline bool operator!=( const vnl_vector_fixed<T,n>& a, const vnl_vector_fixed<T,n>& b )
00843 {
00844 return ! a.operator_eq(b);
00845 }
00846
00847
00848
00849
00850 template<class T, unsigned int n>
00851 inline bool operator!=( vnl_vector_fixed<T,n> const& a, vnl_vector<T> const& b )
00852 {
00853 return ! a.operator_eq(b);
00854 }
00855
00856
00857
00858
00859 template<class T, unsigned int n>
00860 inline bool operator!=( vnl_vector<T> const& a, vnl_vector_fixed<T,n> const& b )
00861 {
00862 return ! b.operator_eq(a);
00863 }
00864
00865
00866
00867
00868
00869
00870
00871 template<class T, unsigned int n>
00872 inline
00873 vcl_ostream& operator<< ( vcl_ostream& ostr, const vnl_vector_fixed<T,n>& v )
00874 {
00875 v.print( ostr );
00876 return ostr;
00877 }
00878
00879
00880
00881 template<class T, unsigned int n>
00882 inline
00883 vcl_istream& operator>> ( vcl_istream& ostr, vnl_vector_fixed<T,n>& v )
00884 {
00885 v.read_ascii( ostr );
00886 return ostr;
00887 }
00888
00889 #endif // vnl_vector_fixed_h_