00001 #ifndef TNT_SPARSE_MATRIX_H
00002 #define TNT_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 #include <vector>
00026 #include "tnt_vector.h"
00027 #include "tnt_sparse_vector.h"
00028
00029 namespace TNT
00030 {
00031
00032
00033
00034
00035
00036 #if 0
00037 template <class T, class Integer>
00038 class Sparse_Matrix_Coordinate_Element
00039 {
00040 private:
00041
00042 T val_;
00043 Integer row_index_;
00044 Integer col_index_;
00045
00046 public:
00047
00048 Sparse_Matrix_Coordinate_Element(
00049 const T& a, const Integer &i, const Integer &j) :
00050 val_(a), row_index_(i), col_index(i) {}
00051
00052
00053 const T& value() const { return val_; }
00054 Integer row_index() const { return row_index_; }
00055 Integer col_index() const { return col_index_; }
00056
00057 T& value() { return val_; }
00058 Integer& row_index() { return row_index_; }
00059 Integer& col_index() { return col_index_; }
00060
00061 };
00062 #endif
00063
00081 template <class T>
00082 class Sparse_Matrix
00083 {
00084 private:
00085
00086
00087
00088 std::vector< Sparse_Vector<T> > S_;
00089
00090 int num_rows_;
00091 int num_cols_;
00092 int num_nonzeros_;
00093
00094
00095
00096
00097
00098 int internal_state_;
00099
00100
00101
00102 public:
00103
00104
00105 Sparse_Matrix(Subscript M, Subscript N):
00106 S_(M),
00107 num_rows_(M), num_cols_(N), num_nonzeros_(0),
00108 internal_state_(1){};
00109
00110
00111
00112 Sparse_Matrix(Subscript M, Subscript N, Subscript nz, const T* val,
00113 const Subscript *r, const Subscript *c):
00114 S_(M),
00115 num_rows_(M),
00116 num_cols_(N),
00117 num_nonzeros_(0),
00118 internal_state_(1)
00119 {
00120 insert(nz, val, r, c);
00121 close();
00122 };
00123
00124
00125 int is_closed() { return internal_state_; }
00126
00127 void insert(const T& val, Subscript i, Subscript j)
00128 {
00129 if (internal_state_ == 0) return;
00130
00131
00132 S_[i].insert(val, j);
00133 num_nonzeros_++;
00134 }
00135
00136 void insert(Subscript nz, const T* val, const Subscript *i,
00137 const Subscript *j)
00138 {
00139 if (internal_state_ == 0) return;
00140
00141 for (int count=0; count<nz; count++)
00142 {
00143 insert(val[count], i[count], j[count]);
00144 }
00145 }
00146
00147 void insert_base_one(const T& val, Subscript i, Subscript j)
00148 {
00149 insert_one_base(val, i-1, j-1);
00150 }
00151
00152 void insert_base_one(Subscript nz, const T* val, const Subscript *i,
00153 const Subscript *j)
00154 {
00155 for (int count=0; count<nz; count++)
00156 {
00157 insert(val[count], i[count]-1, j[count]-1);
00158 }
00159 }
00160
00161
00162 void close()
00163 {
00164
00165
00166
00167
00168
00169
00170 internal_state_ = 0;
00171 }
00172
00173 inline int num_rows() const {return num_rows_;}
00174 inline int num_cols() const {return num_cols_;}
00175 inline int num_columns() const {return num_cols_;}
00176 int num_nonzeros() const {return num_nonzeros_;}
00177
00178
00179 Vector<T> diag() const
00180 {
00181 int minMN = num_rows() < num_columns() ? num_rows() : num_columns();
00182 Vector<T> diag_(minMN, T(0));
00183
00184 for (int i=0; i<minMN; i++)
00185 {
00186 for (typename Sparse_Vector<T>::const_iterator p = S_[i].begin();
00187 p < S_[i].end(); p++ )
00188 {
00189 if (p->index() == i)
00190 {
00191 diag_[i] += p->value();
00192 break;
00193 }
00194 }
00195 }
00196 return diag_;
00197 }
00198
00199 Vector<T> mult(const Vector<T> &x) const
00200 {
00201 int M = num_rows();
00202 Vector<T> y(M);
00203 for (int i=0; i<M; i++)
00204 {
00205 y[i] = dot_product(S_[i], x);
00206 }
00207 return y;
00208 }
00209
00210
00211 inline double norm() const
00212 {
00213 T sum(0.0);
00214 for (int i=0; i<num_rows_; i++)
00215 {
00216 for (typename Sparse_Vector<T>::const_iterator p = S_[i].begin();
00217 p < S_[i].end(); p++ )
00218 {
00219 sum += p->value() * p->value();
00220 }
00221 }
00222 return sqrt(sum);
00223 }
00224
00225 std::ostream & print(std::ostream &s) const
00226 {
00227 for (int i=0; i<num_rows_; i++)
00228 {
00229 for (typename Sparse_Vector<T>::const_iterator p = S_[i].begin();
00230 p < S_[i].end(); p++ )
00231 {
00232 s << "( " << p->value() << " , " << i << ", " << p->index() << " )\n";
00233 }
00234 }
00235
00236 return s;
00237 }
00238
00239 std::ostream & print_base_one(std::ostream &s) const
00240 {
00241 for (int i=0; i<num_rows_; i++)
00242 {
00243 for (typename Sparse_Vector<T>::const_iterator p = S_[i].begin();
00244 p < S_[i].end(); p++ )
00245 {
00246 s <<"( "<<p->value()<<" , "<<i+1<<", "<< p->index()+1 << " )\n";
00247 }
00248 }
00249
00250 return s;
00251 }
00252
00253
00254 };
00255
00256
00257 template <class T>
00258 inline Vector<T> operator*(const Sparse_Matrix<T> &S, const Vector<T> &x)
00259 {
00260 return S.mult(x);
00261 }
00262
00263 template <class T>
00264 inline double norm(const Sparse_Matrix<T> &S)
00265 {
00266 return S.norm();
00267 }
00268
00269 template <class T>
00270 inline std::ostream& operator<<(std::ostream &s, const Sparse_Matrix<T> &A)
00271 {
00272 return A.print(s);
00273 }
00274
00275
00276
00277
00278 }
00279
00280 #endif