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