My Project
Loading...
Searching...
No Matches
tnt_sparse_vector.h
1#ifndef TNT_SPARSE_VECTOR_H
2#define TNT_SPARSE_VECTOR_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
28namespace TNT
29{
30//namespace Linear_Algebra
31//{
32
33
34
35template <class T, class Integer>
37{
38 private:
39
40 T val_;
41 Integer index_;
42
43 public:
44
45 Sparse_Vector_Element(const T& a, const Integer &i) :
46 val_(a), index_(i) {}
47
48
49 const T& value() const { return val_; }
50 Integer index() const { return index_; }
51
52 T& value() { return val_; }
53 Integer& index() { return index_; }
54
55
56
57};
58
59
75template <class T>
77{
78 /* sparse vector indices are 0-based internally. */
79
80 private:
81
82 // (value, index) pairs
83 //
84 std::vector< Sparse_Vector_Element<T, Subscript> > s_;
85
86 int dim_; // dimension
87 int num_nonzeros_; // number of nonzeros
88
89
90
91
92public:
93 typedef typename
94 std::vector< Sparse_Vector_Element<T, Subscript> >::const_iterator
95 const_iterator;
96
97
98 const_iterator begin() const { return s_.begin(); }
99 const_iterator end() const { return s_.end(); }
100
101 inline const T& value(Subscript i) const { return s_[i].value(); }
102 inline Subscript index(Subscript i) const {return s_[i].index(); }
103
104 inline T dot_product(const Vector<T>& x) const
105 {
106 T sum(0);
107
108 for ( const_iterator p = begin(); p < end(); p++)
109 {
110 sum += p->value() * x[p->index()];
111 }
112 return sum;
113 }
114
115 Sparse_Vector() : s_(), dim_(0), num_nonzeros_(0) {}
116 Sparse_Vector(Subscript N): dim_(N), num_nonzeros_(0) {}
117
118
119
120 Sparse_Vector(Subscript N, Subscript nz, const T* Val, const Subscript *I):
121 dim_(N),
122 num_nonzeros_(0)
123 {
124 insert(nz, Val, I);
125 }
126
127
128
129 void insert(const T& val, Subscript i)
130 {
131
132 s_.push_back( Sparse_Vector_Element<T, Subscript>(val,i) );
133 num_nonzeros_++;
134 }
135
136 void insert(Subscript nz, const T* Val, const Subscript *I)
137 {
138
139 for (int count=0; count<nz; count++)
140 {
141 insert(Val[count], I[count]);
142 }
143 }
144
145
146 void insert_base_one(const T& val, Subscript i)
147 {
148 insert(val, i-1);
149 }
150
151 void insert_base_one(Subscript nz, const T* Val, const Subscript *I)
152 {
153 for (int count=0; count<nz; count++)
154 {
155 insert(Val[count], I[count]-1);
156 }
157 }
158
159
160
161 inline int dim() const {return dim_;}
162 int num_nonzeros() const {return num_nonzeros_;}
163
164
165
166
167 inline double norm() const
168 {
169 T sum(0.0);
170
171 for (const_iterator p = begin(); p < end(); p++)
172 {
173 sum += p->value() * p->value();
174 }
175
176 return sqrt(sum);
177 }
178
179 std::ostream & print(std::ostream &s) const
180 {
181 for (typename Sparse_Vector<T>::const_iterator p = begin();
182 p < end(); p++)
183 {
184 s << "( " << p->value() << ", " << p->index() << " ) \n";
185 }
186 return s;
187 }
188
189 std::ostream & print_base_one(std::ostream &s) const
190 {
191 for (typename Sparse_Vector<T>::const_iterator p = begin();
192 p < end(); p++)
193 {
194 s << "( " << p->value() << ", " << p->index()+1 << " ) \n";
195 }
196 return s;
197 }
198
199
200};
201
202
203
204template <class T>
205inline T dot_product(const Sparse_Vector<T> &s, const Vector<T> &x)
206{
207 return s.dot_product(x);
208}
209
210template <class T>
211inline T dot_product(const Vector<T> &x, const Sparse_Vector<T> &s)
212{
213 return s.dot_product(x);
214}
215
216template <class T>
217inline T operator*(const Vector<T> &x, const Sparse_Vector<T> &s)
218{
219 return dot_product(x,s);
220}
221
222template <class T>
223inline T operator*(const Sparse_Vector<T> &s, const Vector<T> &x)
224{
225 return dot_product(x,s);
226}
227
228
229template <class T>
230inline double norm( const Sparse_Vector<T> & s)
231{
232 return s.norm();
233}
234
235
236template <class T>
237std::ostream& operator<<(std::ostream &s, const Sparse_Vector<T> &A)
238{
239
240 return A.print(s);
241}
242
243
244//} /* namspace TNT::Linear_Algebra */
245} /* namspace TNT */
246
247#endif
248// TNT_SPARSE_VECTOR_H
Definition tnt_sparse_vector.h:37
Definition tnt_sparse_vector.h:77
Definition tnt_vector.h:49
Definition tnt_array1d.h:36
double norm(const Matrix< T > &A)
Definition tnt_matrix.h:996