00001
00002 #ifndef vnl_matrix_fixed_h_
00003 #define vnl_matrix_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 #include <vcl_cstring.h>
00030 #include <vcl_cassert.h>
00031 #include <vcl_iosfwd.h>
00032
00033 #include "vnl_matrix.h"
00034 #include "vnl_matrix_ref.h"
00035 #include <vnl/vnl_vector.h>
00036 #include <vnl/vnl_vector_fixed.h>
00037 #include <vnl/vnl_c_vector.h>
00038 #include <vnl/vnl_config.h>
00039
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
00084 template <class T, unsigned int num_rows, unsigned int num_cols>
00085 class vnl_matrix_fixed;
00086 template <class T, unsigned M, unsigned N>
00087 inline
00088 vnl_vector_fixed<T, M> vnl_matrix_fixed_mat_vec_mult(const vnl_matrix_fixed<T, M, N>& a, const vnl_vector_fixed<T, N>& b);
00089 template <class T, unsigned M, unsigned N, unsigned O>
00090 inline
00091 vnl_matrix_fixed<T, M, O> vnl_matrix_fixed_mat_mat_mult(const vnl_matrix_fixed<T, M, N>& a, const vnl_matrix_fixed<T, N, O>& b);
00092 #ifdef VCL_VC_6
00093 template <unsigned num_cols, unsigned num_rows, class T>
00094 class vnl_matrix_fixed_fake_base
00095 {
00096 };
00097
00098 #define VNL_MATRIX_FIXED_VCL60_WORKAROUND : public vnl_matrix_fixed_fake_base<num_cols,num_rows,T>
00099 #else
00100 #define VNL_MATRIX_FIXED_VCL60_WORKAROUND
00101 #endif
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 template <class T, unsigned int num_rows, unsigned int num_cols>
00112 class vnl_matrix_fixed VNL_MATRIX_FIXED_VCL60_WORKAROUND
00113 {
00114 T data_[num_rows][num_cols];
00115
00116 public:
00117 typedef vnl_matrix_fixed<T,num_rows,num_cols> self;
00118 typedef unsigned int size_type;
00119
00120
00121 vnl_matrix_fixed() {}
00122
00123
00124
00125
00126
00127
00128
00129 vnl_matrix_fixed( unsigned n, unsigned m )
00130 {
00131 assert( n == num_rows && m == num_cols );
00132 }
00133
00134
00135 explicit vnl_matrix_fixed(T value)
00136 {
00137 T* p = data_[0];
00138 unsigned int n = num_rows * num_cols;
00139 while (n--)
00140 *p++ = value;
00141 }
00142
00143
00144 explicit vnl_matrix_fixed(const T* datablck)
00145 {
00146 vcl_memcpy(data_[0], datablck, num_rows*num_cols*sizeof(T));
00147 }
00148
00149
00150
00151 vnl_matrix_fixed(const vnl_matrix_fixed& rhs)
00152 {
00153 vcl_memcpy(data_[0], rhs.data_block(), num_rows*num_cols*sizeof(T));
00154 }
00155
00156
00157
00158 vnl_matrix_fixed(const vnl_matrix<T>& rhs)
00159 {
00160 assert(rhs.rows() == num_rows && rhs.columns() == num_cols);
00161 vcl_memcpy(data_[0], rhs.data_block(), num_rows*num_cols*sizeof(T));
00162 }
00163
00164
00165
00166
00167
00168 ~vnl_matrix_fixed() {}
00169
00170
00171
00172 vnl_matrix_fixed& operator= (T const&v) { fill(v); return *this; }
00173
00174
00175
00176 vnl_matrix_fixed& operator=(const vnl_matrix<T>& rhs)
00177 {
00178 assert(rhs.rows() == num_rows && rhs.columns() == num_cols);
00179 vcl_memcpy(data_[0], rhs.data_block(), num_rows*num_cols*sizeof(T));
00180 return *this;
00181 }
00182
00183
00184 vnl_matrix_fixed& operator=(const vnl_matrix_fixed& rhs)
00185 {
00186 vcl_memcpy(data_[0], rhs.data_block(), num_rows*num_cols*sizeof(T));
00187 return *this;
00188 }
00189
00190
00191
00192
00193 unsigned rows() const { return num_rows; }
00194
00195
00196
00197 unsigned columns() const { return num_cols; }
00198
00199
00200
00201 unsigned cols() const { return num_cols; }
00202
00203
00204
00205 unsigned size() const { return num_rows*num_cols; }
00206
00207
00208 void put (unsigned r, unsigned c, T const& v) { (*this)(r,c) = v; }
00209
00210
00211 vnl_matrix_fixed& set (unsigned r, unsigned c, T const& v) { (*this)(r,c) = v; return *this; }
00212
00213
00214 T get (unsigned r, unsigned c) const { return (*this)(r,c); }
00215
00216
00217
00218 T * operator[] (unsigned r) { return data_[r]; }
00219
00220
00221
00222 T const * operator[] (unsigned r) const { return data_[r]; }
00223
00224
00225
00226 T & operator() (unsigned r, unsigned c)
00227 {
00228 #if VNL_CONFIG_CHECK_BOUNDS && (!defined NDEBUG)
00229 assert(r<rows());
00230 assert(c<cols());
00231 #endif
00232 return this->data_[r][c];
00233 }
00234
00235
00236
00237 T const & operator() (unsigned r, unsigned c) const
00238 {
00239 #if VNL_CONFIG_CHECK_BOUNDS && (!defined NDEBUG)
00240 assert(r<rows());
00241 assert(c<cols());
00242 #endif
00243 return this->data_[r][c];
00244 }
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 vnl_matrix_fixed& fill(T);
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 vnl_matrix_fixed& fill_diagonal(T);
00275
00276
00277
00278
00279
00280 vnl_matrix_fixed& set_diagonal(vnl_vector<T> const&);
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 vnl_matrix_fixed& copy_in(T const *);
00296
00297
00298
00299 vnl_matrix_fixed& set(T const *d) { return copy_in(d); }
00300
00301
00302
00303
00304 void copy_out(T *) const;
00305
00306
00307
00308
00309
00310
00311
00312 vnl_matrix_fixed& inplace_transpose();
00313
00314
00315
00316
00317
00318
00319 vnl_matrix_fixed& operator+= (T s)
00320 {
00321 self::add( data_block(), s, data_block() ); return *this;
00322 }
00323
00324
00325 vnl_matrix_fixed& operator-= (T s)
00326 {
00327 self::sub( data_block(), s, data_block() ); return *this;
00328 }
00329
00330
00331 vnl_matrix_fixed& operator*= (T s)
00332 {
00333 self::mul( data_block(), s, data_block() ); return *this;
00334 }
00335
00336
00337 vnl_matrix_fixed& operator/= (T s)
00338 {
00339 self::div( data_block(), s, data_block() ); return *this;
00340 }
00341
00342
00343 vnl_matrix_fixed& operator+= (vnl_matrix_fixed const& m)
00344 {
00345 self::add( data_block(), m.data_block(), data_block() ); return *this;
00346 }
00347
00348
00349 vnl_matrix_fixed& operator+= (vnl_matrix<T> const& m)
00350 {
00351 assert( m.rows() == rows() && m.cols() == cols() );
00352 self::add( data_block(), m.data_block(), data_block() ); return *this;
00353 }
00354
00355
00356 vnl_matrix_fixed& operator-= (vnl_matrix_fixed const& m)
00357 {
00358 self::sub( data_block(), m.data_block(), data_block() ); return *this;
00359 }
00360
00361
00362 vnl_matrix_fixed& operator-= (vnl_matrix<T> const& m)
00363 {
00364 assert( m.rows() == rows() && m.cols() == cols() );
00365 self::sub( data_block(), m.data_block(), data_block() );
00366 return *this;
00367 }
00368
00369
00370 vnl_matrix_fixed operator- () const
00371 {
00372 vnl_matrix_fixed r;
00373 self::sub( T(0), data_block(), r.data_block() );
00374 return r;
00375 }
00376
00377
00378 vnl_matrix_fixed& operator*= (vnl_matrix_fixed<T,num_cols,num_cols> const& s)
00379 {
00380 vnl_matrix_fixed<T, num_rows, num_cols> out;
00381 for (unsigned i = 0; i < num_rows; ++i)
00382 for (unsigned j = 0; j < num_cols; ++j)
00383 {
00384 T accum = this->data_[i][0] * s(0,j);
00385 for (unsigned k = 1; k < num_cols; ++k)
00386 accum += this->data_[i][k] * s(k,j);
00387 out(i,j) = accum;
00388 }
00389 return *this = out;
00390 }
00391
00392 #ifdef VCL_VC_6
00393 template <unsigned o>
00394 vnl_matrix_fixed<T,num_rows,o> operator*( vnl_matrix_fixed_fake_base<o,num_cols,T> const& mat ) const
00395 {
00396 vnl_matrix_fixed<T,num_cols,o> const& b = static_cast<vnl_matrix_fixed<T,num_cols,o> const&>(mat);
00397 return vnl_matrix_fixed_mat_mat_mult<T,num_rows,num_cols,o>( *this, b );
00398 }
00399 vnl_vector_fixed<T, num_rows> operator*( vnl_vector_fixed<T, num_cols> const& b) const
00400 {
00401 return vnl_matrix_fixed_mat_vec_mult<T,num_rows,num_cols>(*this,b);
00402 }
00403 #endif
00404
00405
00406
00407
00408 vnl_matrix_fixed apply(T (*f)(T)) const;
00409
00410
00411 vnl_matrix_fixed apply(T (*f)(T const&)) const;
00412
00413
00414 vnl_matrix_fixed<T,num_cols,num_rows> transpose() const;
00415
00416
00417 vnl_matrix_fixed<T,num_cols,num_rows> conjugate_transpose() const;
00418
00419
00420 vnl_matrix_fixed& update(vnl_matrix<T> const&, unsigned top=0, unsigned left=0);
00421
00422
00423 vnl_matrix_fixed& set_column(unsigned i, T const * v);
00424
00425
00426 vnl_matrix_fixed& set_column(unsigned i, T value );
00427
00428
00429 vnl_matrix_fixed& set_column(unsigned j, vnl_vector<T> const& v);
00430
00431
00432 vnl_matrix_fixed& set_column(unsigned j, vnl_vector_fixed<T,num_rows> const& v);
00433
00434
00435 vnl_matrix_fixed& set_columns(unsigned starting_column, vnl_matrix<T> const& M);
00436
00437
00438 vnl_matrix_fixed& set_row (unsigned i, T const * v);
00439
00440
00441 vnl_matrix_fixed& set_row (unsigned i, T value );
00442
00443
00444 vnl_matrix_fixed& set_row (unsigned i, vnl_vector<T> const&);
00445
00446
00447 vnl_matrix_fixed& set_row (unsigned i, vnl_vector_fixed<T,num_cols> const&);
00448
00449
00450
00451 vnl_matrix<T> extract (unsigned r, unsigned c,
00452 unsigned top=0, unsigned left=0) const;
00453
00454
00455
00456
00457
00458
00459 void extract ( vnl_matrix<T>& sub_matrix,
00460 unsigned top=0, unsigned left=0) const;
00461
00462
00463 vnl_vector_fixed<T,num_cols> get_row (unsigned row) const;
00464
00465
00466 vnl_vector_fixed<T,num_rows> get_column(unsigned col) const;
00467
00468
00469 vnl_matrix<T> get_n_rows (unsigned rowstart, unsigned n) const;
00470
00471
00472 vnl_matrix<T> get_n_columns(unsigned colstart, unsigned n) const;
00473
00474
00475 vnl_vector<T> get_diagonal() const;
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491 vnl_matrix_fixed& set_identity();
00492
00493
00494
00495
00496
00497
00498
00499 vnl_matrix_fixed& flipud();
00500
00501
00502
00503
00504
00505
00506
00507 vnl_matrix_fixed& fliplr();
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521 vnl_matrix_fixed& normalize_rows();
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535 vnl_matrix_fixed& normalize_columns();
00536
00537
00538
00539
00540
00541
00542
00543 vnl_matrix_fixed& scale_row (unsigned row, T value);
00544
00545
00546
00547
00548
00549
00550
00551 vnl_matrix_fixed& scale_column(unsigned col, T value);
00552
00553
00554 typedef typename vnl_c_vector<T>::abs_t abs_t;
00555
00556
00557 abs_t array_one_norm() const { return vnl_c_vector<T>::one_norm(begin(), size()); }
00558
00559
00560 abs_t array_two_norm() const { return vnl_c_vector<T>::two_norm(begin(), size()); }
00561
00562
00563 abs_t array_inf_norm() const { return vnl_c_vector<T>::inf_norm(begin(), size()); }
00564
00565
00566 abs_t absolute_value_sum() const { return array_one_norm(); }
00567
00568
00569 abs_t absolute_value_max() const { return array_inf_norm(); }
00570
00571
00572 abs_t operator_one_norm() const;
00573
00574
00575 abs_t operator_inf_norm() const;
00576
00577
00578 abs_t frobenius_norm() const { return vnl_c_vector<T>::two_norm(begin(), size()); }
00579
00580
00581 abs_t fro_norm() const { return frobenius_norm(); }
00582
00583
00584 abs_t rms() const { return vnl_c_vector<T>::rms_norm(begin(), size()); }
00585
00586
00587 T min_value() const { return vnl_c_vector<T>::min_value(begin(), size()); }
00588
00589
00590 T max_value() const { return vnl_c_vector<T>::max_value(begin(), size()); }
00591
00592
00593 unsigned arg_min() const { return vnl_c_vector<T>::arg_min(begin(), size()); }
00594
00595
00596 unsigned arg_max() const { return vnl_c_vector<T>::arg_max(begin(), size()); }
00597
00598
00599 T mean() const { return vnl_c_vector<T>::mean(begin(), size()); }
00600
00601
00602
00603
00604 bool empty() const { return num_rows==0 && num_cols==0; }
00605
00606
00607 bool is_identity() const;
00608
00609
00610 bool is_identity(double tol) const;
00611
00612
00613 bool is_zero() const;
00614
00615
00616 bool is_zero(double tol) const;
00617
00618
00619 bool is_finite() const;
00620
00621
00622 bool has_nans() const;
00623
00624
00625
00626 void assert_size(unsigned nr_rows, unsigned nr_cols) const
00627 {
00628 #ifndef NDEBUG
00629 assert_size_internal(nr_rows, nr_cols);
00630 #endif
00631 }
00632
00633
00634
00635 void assert_finite() const
00636 {
00637 #ifndef NDEBUG
00638 assert_finite_internal();
00639 #endif
00640 }
00641
00642
00643
00644
00645 bool read_ascii(vcl_istream& s);
00646
00647
00648
00649
00650
00651 T const* data_block () const { return data_[0]; }
00652
00653
00654
00655 T * data_block () { return data_[0]; }
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671 vnl_matrix_ref<T> as_ref() { return vnl_matrix_ref<T>( num_rows, num_cols, data_block() ); }
00672
00673
00674
00675
00676
00677
00678 const vnl_matrix_ref<T> as_ref() const { return vnl_matrix_ref<T>( num_rows, num_cols, const_cast<T*>(data_block()) ); }
00679
00680
00681
00682
00683
00684 operator const vnl_matrix_ref<T>() const { return vnl_matrix_ref<T>( num_rows, num_cols, const_cast<T*>(data_block()) ); }
00685
00686
00687 const vnl_matrix<T> as_matrix() const { return vnl_matrix<T>(const_cast<T*>(data_block()),num_rows,num_cols); }
00688
00689
00690
00691 typedef T element_type;
00692
00693
00694 typedef T *iterator;
00695
00696 iterator begin() { return data_[0]; }
00697
00698 iterator end() { return begin() + size(); }
00699
00700
00701 typedef T const *const_iterator;
00702
00703 const_iterator begin() const { return data_[0]; }
00704
00705 const_iterator end() const { return begin() + size(); }
00706
00707
00708
00709
00710 bool operator_eq (vnl_matrix_fixed const & rhs) const
00711 {
00712 return equal( this->data_block(), rhs.data_block() );
00713 }
00714
00715
00716 bool operator==(vnl_matrix_fixed const &that) const { return this->operator_eq(that); }
00717
00718
00719 bool operator!=(vnl_matrix_fixed const &that) const { return !this->operator_eq(that); }
00720
00721
00722 bool operator==(vnl_matrix<T> const &that) const { return this->operator_eq(that); }
00723
00724
00725 bool operator!=(vnl_matrix<T> const &that) const { return !this->operator_eq(that); }
00726
00727
00728 void print(vcl_ostream& os) const;
00729
00730
00731
00732
00733
00734
00735
00736
00737 static void add( const T* a, const T* b, T* r );
00738 static void add( const T* a, T b, T* r );
00739 static void sub( const T* a, const T* b, T* r );
00740 static void sub( const T* a, T b, T* r );
00741 static void sub( T a, const T* b, T* r );
00742 static void mul( const T* a, const T* b, T* r );
00743 static void mul( const T* a, T b, T* r );
00744 static void div( const T* a, const T* b, T* r );
00745 static void div( const T* a, T b, T* r );
00746
00747 static bool equal( const T* a, const T* b );
00748
00749 private:
00750 void assert_finite_internal() const;
00751
00752 void assert_size_internal(unsigned, unsigned) const;
00753 };
00754
00755 #undef VNL_MATRIX_FIXED_VCL60_WORKAROUND
00756
00757
00758
00759
00760
00761
00762
00763
00764 template <class T, unsigned m, unsigned n>
00765 inline
00766 vnl_matrix_fixed<T,m,n> operator+( const vnl_matrix_fixed<T,m,n>& mat1, const vnl_matrix_fixed<T,m,n>& mat2 )
00767 {
00768 vnl_matrix_fixed<T,m,n> r;
00769 vnl_matrix_fixed<T,m,n>::add( mat1.data_block(), mat2.data_block(), r.data_block() );
00770 return r;
00771 }
00772
00773 template <class T, unsigned m, unsigned n>
00774 inline
00775 vnl_matrix_fixed<T,m,n> operator+( const vnl_matrix_fixed<T,m,n>& mat, T s )
00776 {
00777 vnl_matrix_fixed<T,m,n> r;
00778 vnl_matrix_fixed<T,m,n>::add( mat.data_block(), s, r.data_block() );
00779 return r;
00780 }
00781
00782 template <class T, unsigned m, unsigned n>
00783 inline
00784 vnl_matrix_fixed<T,m,n> operator+( const T& s,
00785 const vnl_matrix_fixed<T,m,n>& mat )
00786 {
00787 vnl_matrix_fixed<T,m,n> r;
00788 vnl_matrix_fixed<T,m,n>::add( mat.data_block(), s, r.data_block() );
00789 return r;
00790 }
00791
00792 template <class T, unsigned m, unsigned n>
00793 inline
00794 vnl_matrix_fixed<T,m,n> operator-( const vnl_matrix_fixed<T,m,n>& mat1, const vnl_matrix_fixed<T,m,n>& mat2 )
00795 {
00796 vnl_matrix_fixed<T,m,n> r;
00797 vnl_matrix_fixed<T,m,n>::sub( mat1.data_block(), mat2.data_block(), r.data_block() );
00798 return r;
00799 }
00800
00801 template <class T, unsigned m, unsigned n>
00802 inline
00803 vnl_matrix_fixed<T,m,n> operator-( const vnl_matrix_fixed<T,m,n>& mat, T s )
00804 {
00805 vnl_matrix_fixed<T,m,n> r;
00806 vnl_matrix_fixed<T,m,n>::sub( mat.data_block(), s, r.data_block() );
00807 return r;
00808 }
00809
00810 template <class T, unsigned m, unsigned n>
00811 inline
00812 vnl_matrix_fixed<T,m,n> operator-( const T& s,
00813 const vnl_matrix_fixed<T,m,n>& mat )
00814 {
00815 vnl_matrix_fixed<T,m,n> r;
00816 vnl_matrix_fixed<T,m,n>::sub( s, mat.data_block(), r.data_block() );
00817 return r;
00818 }
00819
00820 template <class T, unsigned m, unsigned n>
00821 inline
00822 vnl_matrix_fixed<T,m,n> operator*( const vnl_matrix_fixed<T,m,n>& mat, T s )
00823 {
00824 vnl_matrix_fixed<T,m,n> r;
00825 vnl_matrix_fixed<T,m,n>::mul( mat.data_block(), s, r.data_block() );
00826 return r;
00827 }
00828
00829 template <class T, unsigned m, unsigned n>
00830 inline
00831 vnl_matrix_fixed<T,m,n> operator*( const T& s,
00832 const vnl_matrix_fixed<T,m,n>& mat )
00833 {
00834 vnl_matrix_fixed<T,m,n> r;
00835 vnl_matrix_fixed<T,m,n>::mul( mat.data_block(), s, r.data_block() );
00836 return r;
00837 }
00838
00839 template <class T, unsigned m, unsigned n>
00840 inline
00841 vnl_matrix_fixed<T,m,n> operator/( const vnl_matrix_fixed<T,m,n>& mat, T s )
00842 {
00843 vnl_matrix_fixed<T,m,n> r;
00844 vnl_matrix_fixed<T,m,n>::div( mat.data_block(), s, r.data_block() );
00845 return r;
00846 }
00847
00848
00849 template <class T, unsigned m, unsigned n>
00850 inline
00851 vnl_matrix_fixed<T,m,n> element_product( const vnl_matrix_fixed<T,m,n>& mat1,
00852 const vnl_matrix_fixed<T,m,n>& mat2 )
00853 {
00854 vnl_matrix_fixed<T,m,n> r;
00855 vnl_matrix_fixed<T,m,n>::mul( mat1.data_block(), mat2.data_block(), r.data_block() );
00856 return r;
00857 }
00858
00859
00860 template <class T, unsigned m, unsigned n>
00861 inline
00862 vnl_matrix_fixed<T,m,n> element_quotient( const vnl_matrix_fixed<T,m,n>& mat1,
00863 const vnl_matrix_fixed<T,m,n>& mat2)
00864 {
00865 vnl_matrix_fixed<T,m,n> r;
00866 vnl_matrix_fixed<T,m,n>::div( mat1.data_block(), mat2.data_block(), r.data_block() );
00867 return r;
00868 }
00869
00870
00871
00872
00873
00874 template <class T, unsigned M, unsigned N>
00875 inline
00876 vnl_vector_fixed<T, M>
00877 vnl_matrix_fixed_mat_vec_mult(const vnl_matrix_fixed<T, M, N>& a,
00878 const vnl_vector_fixed<T, N>& b)
00879 {
00880 vnl_vector_fixed<T, M> out;
00881 for (unsigned i = 0; i < M; ++i)
00882 {
00883 T accum = a(i,0) * b(0);
00884 for (unsigned k = 1; k < N; ++k)
00885 accum += a(i,k) * b(k);
00886 out(i) = accum;
00887 }
00888 return out;
00889 }
00890
00891 template <class T, unsigned M, unsigned N>
00892 inline
00893 vnl_vector_fixed<T, N>
00894 vnl_matrix_fixed_vec_mat_mult(const vnl_vector_fixed<T, M>& a,
00895 const vnl_matrix_fixed<T, M, N>& b)
00896 {
00897 vnl_vector_fixed<T, N> out;
00898 for (unsigned i = 0; i < N; ++i)
00899 {
00900 T accum = a(0) * b(0,i);
00901 for (unsigned k = 1; k < M; ++k)
00902 accum += a(k) * b(k,i);
00903 out(i) = accum;
00904 }
00905 return out;
00906 }
00907
00908
00909 template <class T, unsigned M, unsigned N, unsigned O>
00910 inline
00911 vnl_matrix_fixed<T, M, O>
00912 vnl_matrix_fixed_mat_mat_mult(const vnl_matrix_fixed<T, M, N>& a,
00913 const vnl_matrix_fixed<T, N, O>& b)
00914 {
00915 vnl_matrix_fixed<T, M, O> out;
00916 for (unsigned i = 0; i < M; ++i)
00917 for (unsigned j = 0; j < O; ++j)
00918 {
00919 T accum = a(i,0) * b(0,j);
00920 for (unsigned k = 1; k < N; ++k)
00921 accum += a(i,k) * b(k,j);
00922 out(i,j) = accum;
00923 }
00924 return out;
00925 }
00926
00927 #ifndef VCL_VC_6
00928
00929
00930
00931
00932
00933 template <class T, unsigned M, unsigned N>
00934 inline
00935 vnl_vector_fixed<T, M> operator*(const vnl_matrix_fixed<T, M, N>& a, const vnl_vector_fixed<T, N>& b)
00936 {
00937 return vnl_matrix_fixed_mat_vec_mult(a, b);
00938 }
00939
00940
00941
00942
00943 template <class T, unsigned M, unsigned N>
00944 inline
00945 vnl_vector_fixed<T, N> operator*(const vnl_vector_fixed<T, M>& a, const vnl_matrix_fixed<T, M, N>& b)
00946 {
00947 return vnl_matrix_fixed_vec_mat_mult(a, b);
00948 }
00949
00950
00951
00952 template <class T, unsigned M, unsigned N, unsigned O>
00953 inline
00954 vnl_matrix_fixed<T, M, O> operator*(const vnl_matrix_fixed<T, M, N>& a, const vnl_matrix_fixed<T, N, O>& b)
00955 {
00956 return vnl_matrix_fixed_mat_mat_mult(a, b);
00957 }
00958 #endif // VCL_VC_6
00959
00960
00961
00962
00963
00964
00965 template <class T, unsigned m, unsigned n>
00966 inline vnl_matrix<T> operator+( const vnl_matrix_fixed<T,m,n>& a, const vnl_matrix<T>& b )
00967 {
00968 return a.as_ref() + b;
00969 }
00970
00971 template <class T, unsigned m, unsigned n>
00972 inline vnl_matrix<T> operator+( const vnl_matrix<T>& a, const vnl_matrix_fixed<T,m,n>& b )
00973 {
00974 return a + b.as_ref();
00975 }
00976
00977 template <class T, unsigned m, unsigned n>
00978 inline vnl_matrix<T> operator-( const vnl_matrix_fixed<T,m,n>& a, const vnl_matrix<T>& b )
00979 {
00980 return a.as_ref() - b;
00981 }
00982
00983 template <class T, unsigned m, unsigned n>
00984 inline vnl_matrix<T> operator-( const vnl_matrix<T>& a, const vnl_matrix_fixed<T,m,n>& b )
00985 {
00986 return a - b.as_ref();
00987 }
00988
00989 template <class T, unsigned m, unsigned n>
00990 inline vnl_matrix<T> operator*( const vnl_matrix_fixed<T,m,n>& a, const vnl_matrix<T>& b )
00991 {
00992 return a.as_ref() * b;
00993 }
00994
00995 template <class T, unsigned m, unsigned n>
00996 inline vnl_matrix<T> operator*( const vnl_matrix<T>& a, const vnl_matrix_fixed<T,m,n>& b )
00997 {
00998 return a * b.as_ref();
00999 }
01000
01001 template <class T, unsigned m, unsigned n>
01002 inline vnl_vector<T> operator*( const vnl_matrix_fixed<T,m,n>& a, const vnl_vector<T>& b )
01003 {
01004 return a.as_ref() * b;
01005 }
01006
01007 template <class T, unsigned n>
01008 inline vnl_vector<T> operator*( const vnl_matrix<T>& a, const vnl_vector_fixed<T,n>& b )
01009 {
01010 return a * b.as_ref();
01011 }
01012
01013
01014
01015
01016 template <class T, unsigned m, unsigned n>
01017 inline
01018 vcl_ostream& operator<< (vcl_ostream& os, vnl_matrix_fixed<T,m,n> const& mat)
01019 {
01020 mat.print(os);
01021 return os;
01022 }
01023
01024 template <class T, unsigned m, unsigned n>
01025 inline
01026 vcl_istream& operator>> (vcl_istream& is, vnl_matrix_fixed<T,m,n>& mat)
01027 {
01028 mat.read_ascii(is);
01029 return is;
01030 }
01031
01032
01033
01034
01035
01036
01037
01038 #if defined(VCL_VC_6) && !defined(__GCCXML__)
01039
01040 template<class T, unsigned m, class FixedVector>
01041 struct outer_product_fixed_type_helper
01042 {
01043 typedef vnl_matrix_fixed<T,m,FixedVector::SIZE> result_matrix;
01044 };
01045
01046 template<class V1, class V2, class RM>
01047 struct outer_product_fixed_calc_helper
01048 {
01049 static RM calc( V1 const& a, V2 const& b );
01050 };
01051
01052 template <class T, unsigned m, class SecondFixedVector>
01053 outer_product_fixed_type_helper<T,m,SecondFixedVector>::result_matrix
01054 outer_product(vnl_vector_fixed<T,m> const& a, SecondFixedVector const& b)
01055 {
01056 typedef vnl_vector_fixed<T,m> VecA;
01057 typedef vnl_vector_fixed<T,SecondFixedVector::SIZE> VecB;
01058 typedef outer_product_fixed_type_helper<T,m,SecondFixedVector>::result_matrix ResultMat;
01059 return outer_product_fixed_calc_helper<VecA,VecB,ResultMat>::calc(a,b);
01060 }
01061
01062 #else // no need for VC6 workaround for outer_product
01063
01064
01065
01066 template <class T, unsigned m, unsigned n>
01067 vnl_matrix_fixed<T,m,n> outer_product(vnl_vector_fixed<T,m> const& a, vnl_vector_fixed<T,n> const& b);
01068
01069 #endif // VC6 workaround for outer_product
01070
01071 #define VNL_MATRIX_FIXED_INSTANTIATE(T, M, N) \
01072 extern "please include vnl/vnl_matrix_fixed.txx instead"
01073
01074 #endif // vnl_matrix_fixed_h_