My Project
Loading...
Searching...
No Matches
tnt_sparse_matrix.h
1#ifndef TNT_SPARSE_MATRIX_H
2#define TNT_SPARSE_MATRIX_H
3
4/*
5*
6* Template Numerical Toolkit (TNT)
7*
8* Mathematical and Computational Sciences Division
9* National Institute of Technology,
10* Gaithersburg, MD USA
11*
12*
13* This software was developed at the National Institute of Standards and
14* Technology (NIST) by employees of the Federal Government in the course
15* of their official duties. Pursuant to title 17 Section 105 of the
16* United States Code, this software is not subject to copyright protection
17* and is in the public domain. NIST assumes no responsibility whatsoever for
18* its use by other parties, and makes no guarantees, expressed or implied,
19* about its quality, reliability, or any other characteristic.
20*
21*/
22
23
24
25#include <vector>
26#include "tnt_vector.h"
27#include "tnt_sparse_vector.h"
28
29namespace TNT
30{
31
32//namespace Linear_Algebra
33//{
34
35
36#if 0
37template <class T, class Integer>
38class Sparse_Matrix_Coordinate_Element
39{
40 private:
41
42 T val_;
43 Integer row_index_;
44 Integer col_index_;
45
46 public:
47
48 Sparse_Matrix_Coordinate_Element(
49 const T& a, const Integer &i, const Integer &j) :
50 val_(a), row_index_(i), col_index(i) {}
51
52
53 const T& value() const { return val_; }
54 Integer row_index() const { return row_index_; }
55 Integer col_index() const { return col_index_; }
56
57 T& value() { return val_; }
58 Integer& row_index() { return row_index_; }
59 Integer& col_index() { return col_index_; }
60
61};
62#endif
63
81template <class T>
83{
84 private:
85
86 // compressed row storage M rows of <value, col_index> pairs
87 //
88 std::vector< Sparse_Vector<T> > S_;
89
90 int num_rows_; // number of rows
91 int num_cols_; // number of cols
92 int num_nonzeros_; // number of nonzeros
93
94 // Used only in multi-step constructions. This
95 // allows one to build a sparse matrix with
96 // multiple calls to insert().
97 //
98 int internal_state_; // 0 if closed (no more inserts) , 1 if open;
99
100
101
102public:
103
104
105 Sparse_Matrix(Subscript M, Subscript N):
106 S_(M),
107 num_rows_(M), num_cols_(N), num_nonzeros_(0),
108 internal_state_(1) {};
109
110
111
112 Sparse_Matrix(Subscript M, Subscript N, Subscript nz, const T* val,
113 const Subscript *r, const Subscript *c):
114 S_(M),
115 num_rows_(M),
116 num_cols_(N),
117 num_nonzeros_(0),
118 internal_state_(1)
119 {
120
121 insert(nz, val, r, c);
122 close();
123 };
124
125
126 int is_closed() { return internal_state_; }
127
128 void insert(const T& val, Subscript i, Subscript j)
129 {
130 if (internal_state_ == 0) return;
131
132
133 S_[i].insert(val, j);
134 num_nonzeros_++;
135 }
136
137 void insert(Subscript nz, const T* val, const Subscript *i,
138 const Subscript *j)
139 {
140 if (internal_state_ == 0) return;
141
142 for (int count=0; count<nz; count++)
143 {
144 insert(val[count], i[count], j[count]);
145 }
146 }
147
148 void insert_base_one(const T& val, Subscript i, Subscript j)
149 {
150 insert_one_base(val, i-1, j-1);
151 }
152
153 void insert_base_one(Subscript nz, const T* val, const Subscript *i,
154 const Subscript *j)
155 {
156 for (int count=0; count<nz; count++)
157 {
158 insert(val[count], i[count]-1, j[count]-1);
159 }
160 }
161
162
163 void close()
164 {
165 /*
166 After this, there are no more inserts. Now one could optimize
167 storage layout.
168
169 */
170
171 internal_state_ = 0;
172 }
173
174 inline int num_rows() const {return num_rows_;}
175 inline int num_cols() const {return num_cols_;}
176 inline int num_columns() const {return num_cols_;}
177 int num_nonzeros() const {return num_nonzeros_;}
178
179
180 Vector<T> diag() const
181 {
182 int minMN = num_rows() < num_columns() ? num_rows() : num_columns();
183 Vector<T> diag_(minMN, T(0));
184
185 for (int i=0; i<minMN; i++)
186 {
187 for (typename Sparse_Vector<T>::const_iterator p = S_[i].begin();
188 p < S_[i].end(); p++ )
189 {
190 if (p->index() == i)
191 {
192 diag_[i] += p->value();
193 break;
194 }
195 }
196 }
197 return diag_;
198 }
199
200 Vector<T> mult(const Vector<T> &x) const
201 {
202 int M = num_rows();
203 Vector<T> y(M);
204 for (int i=0; i<M; i++)
205 {
206 y[i] = dot_product(S_[i], x);
207 }
208 return y;
209 }
210
211
212 inline double norm() const
213 {
214 T sum(0.0);
215 for (int i=0; i<num_rows_; i++)
216 {
217 for (typename Sparse_Vector<T>::const_iterator p = S_[i].begin();
218 p < S_[i].end(); p++ )
219 {
220 sum += p->value() * p->value();
221 }
222 }
223 return sqrt(sum);
224 }
225
226 std::ostream & print(std::ostream &s) const
227 {
228 for (int i=0; i<num_rows_; i++)
229 {
230 for (typename Sparse_Vector<T>::const_iterator p = S_[i].begin();
231 p < S_[i].end(); p++ )
232 {
233 s << "( " << p->value() << " , " << i << ", " << p->index() << " )\n";
234 }
235 }
236
237 return s;
238 }
239
240 std::ostream & print_base_one(std::ostream &s) const
241 {
242 for (int i=0; i<num_rows_; i++)
243 {
244 for (typename Sparse_Vector<T>::const_iterator p = S_[i].begin();
245 p < S_[i].end(); p++ )
246 {
247 s <<"( "<<p->value()<<" , "<<i+1<<", "<< p->index()+1 << " )\n";
248 }
249 }
250
251 return s;
252 }
253
254
255};
256
257
258template <class T>
259inline Vector<T> operator*(const Sparse_Matrix<T> &S, const Vector<T> &x)
260{
261 return S.mult(x);
262}
263
264template <class T>
265inline double norm(const Sparse_Matrix<T> &S)
266{
267 return S.norm();
268}
269
270template <class T>
271inline std::ostream& operator<<(std::ostream &s, const Sparse_Matrix<T> &A)
272{
273 return A.print(s);
274}
275
276
277
278//} /* namspace TNT::Linear_Algebra */
279} /* namspace TNT */
280
281#endif
Definition tnt_sparse_matrix.h:83
Definition tnt_vector.h:49
Definition tnt_array1d.h:36
double norm(const Matrix< T > &A)
Definition tnt_matrix.h:996