00001 #ifndef vnl_matrix_h_
00002 #define vnl_matrix_h_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <vcl_iosfwd.h>
00014 #include <vcl_string.h>
00015 #include <vnl/vnl_tag.h>
00016 #include <vnl/vnl_error.h>
00017 #include <vnl/vnl_c_vector.h>
00018
00019 export template <class T> class vnl_vector;
00020 export template <class T> class vnl_matrix;
00021
00022
00023
00024 #define v vnl_vector<T>
00025 #define m vnl_matrix<T>
00026 template <class T> m operator+ (T const&, m const&);
00027 template <class T> m operator- (T const&, m const&);
00028 template <class T> m operator* (T const&, m const&);
00029 template <class T> m element_product(m const&, m const&);
00030 template <class T> m element_quotient(m const&, m const&);
00031 template <class T> T dot_product (m const&, m const&);
00032 template <class T> T inner_product (m const&, m const&);
00033 template <class T> T cos_angle(m const&, m const& );
00034 template <class T> vcl_ostream& operator<< (vcl_ostream&, m const&);
00035 template <class T> vcl_istream& operator>> (vcl_istream&, m&);
00036 #undef v
00037 #undef m
00038
00039
00040
00041 enum vnl_matrix_type {
00042 vnl_matrix_null,
00043 vnl_matrix_identity
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 template<class T>
00083 class vnl_matrix {
00084 public:
00085
00086 vnl_matrix () :
00087 num_rows(0),
00088 num_cols(0),
00089 data(0)
00090 {
00091 }
00092
00093
00094
00095
00096 vnl_matrix(unsigned r, unsigned c);
00097
00098
00099
00100 vnl_matrix(unsigned r, unsigned c, T const& v0);
00101
00102
00103
00104
00105 vnl_matrix(unsigned r, unsigned c, vnl_matrix_type t);
00106
00107
00108
00109
00110 vnl_matrix(unsigned r, unsigned c, unsigned n, T const values[]);
00111
00112
00113
00114
00115 vnl_matrix(T const* data_block, unsigned r, unsigned c);
00116
00117
00118
00119 vnl_matrix(vnl_matrix<T> const&);
00120
00121
00122
00123 vnl_matrix(vnl_matrix<T> &that, const vnl_tag_grab &)
00124 : num_rows(that.num_rows), num_cols(that.num_cols), data(that.data)
00125 { that.num_cols=that.num_rows=0; that.data=0; }
00126
00127
00128
00129 #ifndef VXL_DOXYGEN_SHOULD_SKIP_THIS
00130
00131
00132
00133 vnl_matrix (vnl_matrix<T> const &, vnl_matrix<T> const &, const vnl_tag_add &);
00134 vnl_matrix (vnl_matrix<T> const &, vnl_matrix<T> const &, const vnl_tag_sub &);
00135 vnl_matrix (vnl_matrix<T> const &, T, const vnl_tag_mul &);
00136 vnl_matrix (vnl_matrix<T> const &, T, const vnl_tag_div &);
00137 vnl_matrix (vnl_matrix<T> const &, T, const vnl_tag_add &);
00138 vnl_matrix (vnl_matrix<T> const &, T, const vnl_tag_sub &);
00139 vnl_matrix (vnl_matrix<T> const &, vnl_matrix<T> const &, const vnl_tag_mul &);
00140
00141 #endif
00142
00143
00144 ~vnl_matrix() {
00145
00146 if (data) destroy();
00147 }
00148
00149
00150
00151
00152 unsigned rows () const { return num_rows; }
00153
00154
00155 unsigned columns () const { return num_cols; }
00156
00157
00158
00159
00160 unsigned cols () const { return num_cols; }
00161
00162
00163
00164 unsigned size () const { return rows()*cols(); }
00165
00166
00167 void put (unsigned r, unsigned c, T const&);
00168
00169
00170 T get (unsigned r, unsigned c) const;
00171
00172
00173
00174 T * operator[] (unsigned r) { return data[r]; }
00175
00176
00177
00178 T const * operator[] (unsigned r) const { return data[r]; }
00179
00180
00181
00182 T & operator() (unsigned r, unsigned c) { return this->data[r][c]; }
00183
00184
00185
00186 T const & operator() (unsigned r, unsigned c) const { return this->data[r][c]; }
00187
00188
00189
00190
00191
00192
00193 void fill (T const&);
00194
00195
00196
00197 void fill_diagonal (T const&);
00198
00199
00200
00201 void copy_in(T const *);
00202
00203
00204
00205 void set(T const *d) { copy_in(d); }
00206
00207
00208
00209
00210
00211 void copy_out(T *) const;
00212
00213
00214
00215
00216 vnl_matrix<T>& operator= (T const&v) { fill(v); return *this; }
00217
00218
00219
00220 vnl_matrix<T>& operator= (vnl_matrix<T> const&);
00221
00222
00223
00224
00225
00226
00227 vnl_matrix<T>& operator+= (T value);
00228
00229
00230 vnl_matrix<T>& operator-= (T value);
00231
00232
00233 vnl_matrix<T>& operator*= (T value);
00234
00235
00236 vnl_matrix<T>& operator/= (T value);
00237
00238
00239 vnl_matrix<T>& operator+= (vnl_matrix<T> const&);
00240
00241 vnl_matrix<T>& operator-= (vnl_matrix<T> const&);
00242
00243 vnl_matrix<T>& operator*= (vnl_matrix<T> const&rhs) { *this = (*this) * rhs; return *this; }
00244
00245
00246 vnl_matrix<T> operator- () const;
00247
00248
00249
00250 vnl_matrix<T> operator+ (T const& v) const { return vnl_matrix<T>(*this, v, vnl_tag_add()); }
00251
00252
00253 vnl_matrix<T> operator- (T const& v) const { return vnl_matrix<T>(*this, v, vnl_tag_sub()); }
00254
00255
00256 vnl_matrix<T> operator* (T const& v) const { return vnl_matrix<T>(*this, v, vnl_tag_mul()); }
00257
00258
00259 vnl_matrix<T> operator/ (T const& v) const { return vnl_matrix<T>(*this, v, vnl_tag_div()); }
00260
00261
00262 vnl_matrix<T> operator+ (vnl_matrix<T> const& rhs) const { return vnl_matrix<T>(*this, rhs, vnl_tag_add()); }
00263
00264 vnl_matrix<T> operator- (vnl_matrix<T> const& rhs) const { return vnl_matrix<T>(*this, rhs, vnl_tag_sub()); }
00265
00266 vnl_matrix<T> operator* (vnl_matrix<T> const& rhs) const { return vnl_matrix<T>(*this, rhs, vnl_tag_mul()); }
00267
00269
00270
00271 vnl_matrix<T> apply(T (*f)(T)) const;
00272
00273
00274 vnl_matrix<T> apply(T (*f)(T const&)) const;
00275
00276
00277 vnl_matrix<T> transpose () const;
00278
00279
00280 vnl_matrix<T> conjugate_transpose () const;
00281
00282
00283 vnl_matrix<T>& update (vnl_matrix<T> const&, unsigned top=0, unsigned left=0);
00284
00285
00286 void set_column(unsigned i, T const * v);
00287
00288
00289 void set_column(unsigned i, T value );
00290
00291
00292 void set_column(unsigned j, vnl_vector<T> const& v);
00293
00294
00295 void set_columns(unsigned starting_column, vnl_matrix<T> const& M);
00296
00297
00298 void set_row (unsigned i, T const * v);
00299
00300
00301 void set_row (unsigned i, T value );
00302
00303
00304 void set_row (unsigned i, vnl_vector<T> const&);
00305
00306
00307
00308 vnl_matrix<T> extract (unsigned rows, unsigned cols,
00309 unsigned top=0, unsigned left=0) const;
00310
00311
00312 vnl_vector<T> get_row (unsigned row) const;
00313
00314
00315 vnl_vector<T> get_column(unsigned col) const;
00316
00317
00318 vnl_matrix<T> get_n_rows (unsigned rowstart, unsigned n) const;
00319
00320
00321 vnl_matrix<T> get_n_columns(unsigned colstart, unsigned n) const;
00322
00323
00324
00325
00326
00327
00328 void set_identity();
00329
00330
00331 void inplace_transpose();
00332
00333
00334 void flipud();
00335
00336 void fliplr();
00337
00338
00339
00340 void normalize_rows();
00341
00342
00343
00344 void normalize_columns();
00345
00346
00347 void scale_row (unsigned row, T value);
00348
00349
00350 void scale_column(unsigned col, T value);
00351
00352
00353 void swap(vnl_matrix<T> & that);
00354
00355
00356 typedef typename vnl_c_vector<T>::abs_t abs_t;
00357
00358
00359 abs_t array_one_norm() const { return vnl_c_vector<T>::one_norm(begin(), size()); }
00360
00361
00362 abs_t array_two_norm() const { return vnl_c_vector<T>::two_norm(begin(), size()); }
00363
00364
00365 abs_t array_inf_norm() const { return vnl_c_vector<T>::inf_norm(begin(), size()); }
00366
00367
00368 abs_t absolute_value_sum() const { return array_one_norm(); }
00369
00370
00371 abs_t absolute_value_max() const { return array_inf_norm(); }
00372
00373 abs_t operator_one_norm() const;
00374
00375
00376
00377 abs_t operator_inf_norm() const;
00378
00379
00380 abs_t frobenius_norm() const { return vnl_c_vector<T>::two_norm(begin(), size()); }
00381
00382
00383 abs_t fro_norm() const { return frobenius_norm(); }
00384
00385
00386 abs_t rms() const { return vnl_c_vector<T>::rms_norm(begin(), size()); }
00387
00388
00389 T min_value() const { return vnl_c_vector<T>::min_value(begin(), size()); }
00390
00391
00392 T max_value() const { return vnl_c_vector<T>::max_value(begin(), size()); }
00393
00394
00395 T mean() const { return vnl_c_vector<T>::mean(begin(), size()); }
00396
00397 #ifndef VXL_DOXYGEN_SHOULD_SKIP_THIS
00398
00399
00400
00401
00402
00403 abs_t one_norm(void *) const { return vnl_c_vector<T>::one_norm(begin(), size()); }
00404 abs_t inf_norm(void *) const { return vnl_c_vector<T>::inf_norm(begin(), size()); }
00405
00406 #endif
00407
00408
00409
00410
00411 bool is_identity(double tol = 0) const;
00412
00413
00414 bool is_zero(double tol = 0) const;
00415
00416
00417 bool is_finite() const;
00418
00419
00420 bool has_nans() const;
00421
00422
00423 void assert_size(unsigned rows, unsigned cols) const;
00424 void assert_finite() const;
00425
00427
00428
00429
00430 static vnl_matrix<T> read(vcl_istream& s);
00431
00432
00433
00434 bool read_ascii(vcl_istream& s);
00435
00436
00437
00438
00439
00440 T const* data_block () const { return data[0]; }
00441
00442
00443
00444 T * data_block () { return data[0]; }
00445
00446
00447
00448 T const* const* data_array () const { return data; }
00449
00450
00451
00452 T * * data_array () { return data; }
00453
00454 typedef T element_type;
00455
00456
00457 typedef T *iterator;
00458
00459 iterator begin() { return data[0]; }
00460
00461 iterator end() { return data[0]+num_rows*num_cols; }
00462
00463
00464 typedef T const *const_iterator;
00465
00466 const_iterator begin() const { return data[0]; }
00467
00468 const_iterator end() const { return data[0]+num_rows*num_cols; }
00469
00470
00471
00472
00473 bool operator_eq (vnl_matrix<T> const & rhs) const;
00474
00475
00476 bool operator==(vnl_matrix<T> const &that) const { return this->operator_eq(that); }
00477
00478
00479 bool operator!=(vnl_matrix<T> const &that) const { return !this->operator_eq(that); }
00480
00481
00482 void print(vcl_ostream& os) const;
00483
00484
00485 void clear();
00486
00487
00488
00489 bool resize (unsigned r, unsigned c);
00490
00491
00492
00493 protected:
00494 unsigned num_rows;
00495 unsigned num_cols;
00496 T** data;
00497
00498
00499 void destroy();
00500
00501 #if VCL_NEED_FRIEND_FOR_TEMPLATE_OVERLOAD
00502 # define v vnl_vector<T>
00503 # define m vnl_matrix<T>
00504 friend m operator+ VCL_NULL_TMPL_ARGS (T const&, m const&);
00505 friend m operator- VCL_NULL_TMPL_ARGS (T const&, m const&);
00506 friend m operator* VCL_NULL_TMPL_ARGS (T const&, m const&);
00507 friend m element_product VCL_NULL_TMPL_ARGS (m const&, m const&);
00508 friend m element_quotient VCL_NULL_TMPL_ARGS (m const&, m const&);
00509 friend T dot_product VCL_NULL_TMPL_ARGS (m const&, m const&);
00510 friend T inner_product VCL_NULL_TMPL_ARGS (m const&, m const&);
00511 friend T cos_angle VCL_NULL_TMPL_ARGS (m const&, m const&);
00512 friend vcl_ostream& operator<< VCL_NULL_TMPL_ARGS (vcl_ostream&, m const&);
00513 friend vcl_istream& operator>> VCL_NULL_TMPL_ARGS (vcl_istream&, m&);
00514 # undef v
00515 # undef m
00516 #endif
00517
00518
00519 static void inline_function_tickler();
00520 };
00521
00522
00523
00524
00525
00526
00527
00528
00529 template<class T>
00530 inline T vnl_matrix<T>::get (unsigned row, unsigned column) const {
00531 #if ERROR_CHECKING
00532 if (row >= this->num_rows)
00533 vnl_error_matrix_row_index ("get", row);
00534 if (column >= this->num_cols)
00535 vnl_error_matrix_col_index ("get", column);
00536 #endif
00537 return this->data[row][column];
00538 }
00539
00540
00541
00542
00543 template<class T>
00544 inline void vnl_matrix<T>::put (unsigned row, unsigned column, T const& value) {
00545 #if ERROR_CHECKING
00546 if (row >= this->num_rows)
00547 vnl_error_matrix_row_index ("put", row);
00548 if (column >= this->num_cols)
00549 vnl_error_matrix_col_index ("put", column);
00550 #endif
00551 this->data[row][column] = value;
00552 }
00553
00554
00555
00556
00557 template<class T>
00558 inline vnl_matrix<T> operator* (T const& value, vnl_matrix<T> const& m) {
00559 return vnl_matrix<T>(m, value, vnl_tag_mul());
00560 }
00561
00562 template<class T>
00563 inline vnl_matrix<T> operator+ (T const& value, vnl_matrix<T> const& m) {
00564 return vnl_matrix<T>(m, value, vnl_tag_add());
00565 }
00566
00567 template<class T>
00568 inline void swap(vnl_matrix<T> &A, vnl_matrix<T> &B) { A.swap(B); }
00569
00570
00571 #endif // vnl_matrix_h_