Go to the documentation of this file.00001
00002 #ifndef vnl_sparse_matrix_h_
00003 #define vnl_sparse_matrix_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
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 #include <vcl_vector.h>
00059 #include <vnl/vnl_vector.h>
00060 #include <vcl_functional.h>
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 template <class T>
00073 class vnl_sparse_matrix_pair
00074 {
00075 public:
00076 unsigned int first;
00077 T second;
00078
00079
00080 vnl_sparse_matrix_pair() : first(0), second(T(0)) {}
00081
00082
00083 vnl_sparse_matrix_pair(unsigned int const& a, T const& b) : first(a), second(b) {}
00084
00085 vnl_sparse_matrix_pair(const vnl_sparse_matrix_pair<T>& o) : first(o.first), second(o.second) {}
00086
00087 vnl_sparse_matrix_pair<T>& operator=(vnl_sparse_matrix_pair const &o) {
00088 if (&o != this) {
00089 first = o.first;
00090 second = o.second;
00091 }
00092 return *this;
00093 }
00094
00095 struct less : public vcl_binary_function<vnl_sparse_matrix_pair, vnl_sparse_matrix_pair, bool>
00096 {
00097 bool operator() (vnl_sparse_matrix_pair const& p1, vnl_sparse_matrix_pair const& p2) {
00098 return p1.first < p2.first;
00099 }
00100 };
00101 };
00102
00103
00104
00105
00106 template <class T>
00107 class vnl_sparse_matrix
00108 {
00109 public:
00110 typedef vnl_sparse_matrix_pair<T> pair_t;
00111 #if defined(VCL_SUNPRO_CC)
00112
00113 typedef vcl_vector < typename pair_t > row;
00114 typedef vcl_vector < typename row > vnl_sparse_matrix_elements;
00115 #else
00116 typedef vcl_vector < pair_t > row;
00117 typedef vcl_vector < row > vnl_sparse_matrix_elements;
00118 #endif
00119
00120
00121 vnl_sparse_matrix();
00122
00123
00124 vnl_sparse_matrix(unsigned int m, unsigned int n);
00125
00126
00127 vnl_sparse_matrix(vnl_sparse_matrix<T> const& rhs);
00128
00129
00130 vnl_sparse_matrix<T>& operator=(vnl_sparse_matrix<T> const& rhs);
00131
00132
00133 void mult(vnl_vector<T> const& rhs, vnl_vector<T>& result) const;
00134
00135
00136 void mult(unsigned int n, unsigned int m, T const* p, T* q) const;
00137
00138
00139 void pre_mult(const vnl_vector<T>& lhs, vnl_vector<T>& result) const;
00140
00141
00142 T& operator()(unsigned int row, unsigned int column);
00143
00144
00145 T operator()(unsigned int row, unsigned int column) const;
00146
00147
00148
00149 T get(unsigned int row, unsigned int column) const;
00150
00151
00152 void put(unsigned int row, unsigned int column, T value);
00153
00154
00155
00156 void diag_AtA(vnl_vector<T>& result) const;
00157
00158
00159 vnl_sparse_matrix& set_row(unsigned int r,
00160 vcl_vector<int> const& cols,
00161 vcl_vector<T> const& vals);
00162
00163
00164
00165 row& get_row(unsigned int r) {return elements[r];}
00166
00167
00168 vnl_sparse_matrix<T>& vcat(vnl_sparse_matrix<T> const& A);
00169
00170
00171 unsigned int rows() const { return rs_; }
00172
00173
00174 unsigned int columns() const { return cs_; }
00175
00176
00177 unsigned int cols() const { return cs_; }
00178
00179
00180 bool empty_row(unsigned int r) const { return elements[r].empty(); }
00181
00182
00183 T sum_row(unsigned int r);
00184
00185
00186 vnl_sparse_matrix& scale_row(unsigned int r, T scale);
00187
00188
00189 void clear() { elements.clear(); }
00190
00191
00192 void set_size( int r, int c );
00193
00194
00195 void resize( int r, int c );
00196
00197
00198 void reset() const;
00199
00200
00201
00202
00203 bool next() const;
00204
00205
00206 int getrow() const;
00207
00208
00209 int getcolumn() const;
00210
00211
00212 T value() const;
00213
00214
00215 bool operator==(vnl_sparse_matrix<T> const& rhs) const;
00216
00217
00218 bool operator!=(vnl_sparse_matrix<T> const& rhs) const
00219 { return !operator==(rhs); }
00220
00221
00222 vnl_sparse_matrix<T> operator-() const;
00223
00224
00225 vnl_sparse_matrix<T> operator+(vnl_sparse_matrix<T> const& rhs) const;
00226
00227
00228 vnl_sparse_matrix<T> operator-(vnl_sparse_matrix<T> const& rhs) const;
00229
00230
00231 vnl_sparse_matrix<T> operator*(vnl_sparse_matrix<T> const& rhs) const;
00232
00233
00234 vnl_sparse_matrix<T>& operator+=(vnl_sparse_matrix<T> const& rhs);
00235
00236
00237 vnl_sparse_matrix<T>& operator-=(vnl_sparse_matrix<T> const& rhs);
00238
00239
00240 vnl_sparse_matrix<T>& operator*=(vnl_sparse_matrix<T> const& rhs);
00241
00242
00243 vnl_sparse_matrix<T> operator*(T const& rhs) const;
00244
00245
00246 vnl_sparse_matrix<T>& operator*=(T const& rhs);
00247
00248
00249 vnl_sparse_matrix<T> operator/(T const& rhs) const;
00250
00251
00252 vnl_sparse_matrix<T>& operator/=(T const& rhs);
00253
00254
00255 vnl_sparse_matrix<T> transpose() const;
00256
00257
00258 vnl_sparse_matrix<T> conjugate_transpose() const;
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 vnl_sparse_matrix& set_identity();
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 vnl_sparse_matrix& normalize_rows();
00283
00284
00285
00286
00287
00288
00289
00290 void add(const vnl_sparse_matrix<T>& rhs, vnl_sparse_matrix<T>& result) const;
00291
00292
00293
00294 void subtract(const vnl_sparse_matrix<T>& rhs, vnl_sparse_matrix<T>& result) const;
00295
00296
00297
00298 void mult(vnl_sparse_matrix<T> const& rhs, vnl_sparse_matrix<T>& result) const;
00299
00300 protected:
00301 vnl_sparse_matrix_elements elements;
00302 unsigned int rs_, cs_;
00303
00304
00305 mutable unsigned int itr_row;
00306 mutable typename row::const_iterator itr_cur;
00307 mutable bool itr_isreset;
00308 };
00309
00310
00311
00312
00313
00314 template<class T>
00315 inline vnl_sparse_matrix<T> operator*(T const& value, vnl_sparse_matrix<T> const& m)
00316 {
00317 return m * value;
00318 }
00319
00320
00321 #endif // vnl_sparse_matrix_h_