00001 #ifndef vnl_sparse_matrix_h_
00002 #define vnl_sparse_matrix_h_
00003
00004
00005
00006
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 #include <vcl_vector.h>
00033 #include <vnl/vnl_vector.h>
00034 #include <vcl_functional.h>
00035
00036
00037
00038
00039 template <class T>
00040 class vnl_sparse_matrix_pair {
00041 public:
00042 unsigned int first;
00043 T second;
00044
00045
00046 vnl_sparse_matrix_pair() : first(0), second(T(0)) {}
00047
00048
00049 vnl_sparse_matrix_pair(unsigned int const& a, T const& b) : first(a), second(b) {}
00050
00051 vnl_sparse_matrix_pair(const vnl_sparse_matrix_pair<T>& o) : first(o.first), second(o.second) {}
00052
00053 vnl_sparse_matrix_pair<T>& operator=(vnl_sparse_matrix_pair const &o) {
00054 if (&o != this) {
00055 first = o.first;
00056 second = o.second;
00057 }
00058 return *this;
00059 }
00060
00061 struct less : public vcl_binary_function<vnl_sparse_matrix_pair, vnl_sparse_matrix_pair, bool> {
00062 bool operator() (vnl_sparse_matrix_pair const& p1, vnl_sparse_matrix_pair const& p2) {
00063 return p1.first < p2.first;
00064 }
00065 };
00066
00067 };
00068
00069
00070
00071
00072 template <class T>
00073 class vnl_sparse_matrix {
00074 public:
00075 typedef vnl_sparse_matrix_pair<T> pair_t;
00076 #if defined(VCL_SUNPRO_CC)
00077
00078 typedef vcl_vector < typename pair_t > row ;
00079 typedef vcl_vector < typename row > vnl_sparse_matrix_elements;
00080 #else
00081 typedef vcl_vector < pair_t > row ;
00082 typedef vcl_vector < row > vnl_sparse_matrix_elements;
00083 #endif
00084
00085
00086
00087
00088 vnl_sparse_matrix();
00089
00090
00091 vnl_sparse_matrix(unsigned int m, unsigned int n);
00092
00093
00094 vnl_sparse_matrix(const vnl_sparse_matrix<T>& rhs);
00095
00096
00097 vnl_sparse_matrix<T>& operator=(const vnl_sparse_matrix<T>& rhs);
00098
00099
00100 void mult(const vnl_sparse_matrix<T>& rhs, vnl_sparse_matrix<T>& result) const;
00101
00102
00103 void mult(const vnl_vector<T>& rhs, vnl_vector<T>& result) const;
00104
00105
00106 void mult(unsigned int n, unsigned int m, const T* p, T* q) const;
00107
00108
00109 void pre_mult(const vnl_vector<T>& lhs, vnl_vector<T>& result) const;
00110
00111
00112 void add(const vnl_sparse_matrix<T>& rhs, vnl_sparse_matrix<T>& result) const;
00113
00114
00115 void subtract(const vnl_sparse_matrix<T>& rhs, vnl_sparse_matrix<T>& result) const;
00116
00117
00118 T& operator()(unsigned int row, unsigned int column);
00119
00120
00121
00122 void diag_AtA(vnl_vector<T>& result) const;
00123
00124
00125 void set_row(unsigned int r,
00126 vcl_vector<int> const& cols,
00127 vcl_vector<T> const& vals);
00128
00129
00130
00131 row& get_row(unsigned int r) {return elements[r];}
00132
00133
00134 vnl_sparse_matrix<T>& vcat(vnl_sparse_matrix<T> const& A);
00135
00136
00137 unsigned int rows() const { return rs_; }
00138
00139
00140 unsigned int columns() const { return cs_; }
00141
00142
00143 bool empty_row(unsigned int r) const { return elements[r].empty(); }
00144
00145
00146 double sum_row(unsigned int r);
00147
00148
00149 void scale_row(unsigned int r, T scale);
00150
00151
00152
00153 void resize( int r, int c );
00154
00155
00156 void reset();
00157
00158
00159
00160
00161 bool next();
00162
00163
00164 int getrow();
00165
00166
00167 int getcolumn();
00168
00169
00170 T value();
00171
00172
00173 protected:
00174 vnl_sparse_matrix_elements elements;
00175 unsigned int rs_, cs_;
00176
00177
00178 unsigned int itr_row;
00179 typename row::iterator itr_cur;
00180 bool itr_isreset;
00181 };
00182
00183
00184 #endif // vnl_sparse_matrix_h_