File: tnt_sparse_vector.h

package info (click to toggle)
pymol 2.5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 42,288 kB
  • sloc: cpp: 476,472; python: 76,538; ansic: 29,510; javascript: 6,792; sh: 47; makefile: 24
file content (248 lines) | stat: -rw-r--r-- 4,926 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#ifndef TNT_SPARSE_VECTOR_H
#define TNT_SPARSE_VECTOR_H

/*
*
* Template Numerical Toolkit (TNT)
*
* Mathematical and Computational Sciences Division
* National Institute of Technology,
* Gaithersburg, MD USA
*
*
* This software was developed at the National Institute of Standards and
* Technology (NIST) by employees of the Federal Government in the course
* of their official duties. Pursuant to title 17 Section 105 of the
* United States Code, this software is not subject to copyright protection
* and is in the public domain. NIST assumes no responsibility whatsoever for
* its use by other parties, and makes no guarantees, expressed or implied,
* about its quality, reliability, or any other characteristic.
*
*/



#include <vector>
#include "tnt_vector.h"

namespace TNT
{
//namespace Linear_Algebra
//{



template <class T, class Integer>
class Sparse_Vector_Element
{
		private:

				T val_;
				Integer index_;

		public:

				Sparse_Vector_Element(const T& a, const Integer &i) : 
						val_(a), index_(i) {}
				

				const T& value() const { return val_; } 
				Integer	index() const { return index_; } 

				T& value() { return val_; }
				Integer& index() { return index_; }

					

};


/**

		Sparse Vector.

	<p>
	Index values begin at 0.  Thus S[3] = 6.5 means that the fourth, not
	the third element, is set to 6.5.

	<p>
	S.value(0) is the first nonzero values in S, and S.index(0) is
	the index (0-based) of the first in S.

	

*/
template <class T>
class Sparse_Vector
{
	/* sparse vector indices are 0-based internally. */

  private:

		// (value, index) pairs
		//
		std::vector< Sparse_Vector_Element<T, Subscript> > s_;    

  	int dim_;        			// dimension
		int num_nonzeros_; 		// number of nonzeros




public:
		typedef typename 
				std::vector< Sparse_Vector_Element<T, Subscript> >::const_iterator 
																															const_iterator;


		const_iterator begin() const { return s_.begin(); }
		const_iterator end() const { return s_.end(); }

		inline const T& value(Subscript i) const { return s_[i].value(); }
		inline Subscript index(Subscript i) const {return  s_[i].index(); }
	  
    inline T dot_product(const Vector<T>& x) const
    {
        T sum(0);

        for ( const_iterator p = begin(); p < end(); p++)
        {
						sum += p->value() * x[p->index()];
        }
       return sum;
   	}

	Sparse_Vector() : s_(), dim_(0), num_nonzeros_(0) {}
	Sparse_Vector(Subscript N): dim_(N), num_nonzeros_(0) {}

			

	Sparse_Vector(Subscript N, Subscript nz, const T* Val, const Subscript *I):
					dim_(N),
					num_nonzeros_(0)
					{
						insert(nz, Val, I);	
					}
   


	void   insert(const T& val, Subscript i)
	{

			s_.push_back( Sparse_Vector_Element<T, Subscript>(val,i) );
			num_nonzeros_++;
	}

	void   insert(Subscript nz, const T* Val, const Subscript *I)
	{

			for (int count=0; count<nz; count++)
			{
				insert(Val[count], I[count]);	
			}
	}


	void insert_base_one(const T& val, Subscript i)
	{
		insert(val, i-1);
	}

	void   insert_base_one(Subscript nz, const T* Val, const Subscript *I)
	{
			for (int count=0; count<nz; count++)
			{
				insert(Val[count], I[count]-1);	
			}
	}
		


  inline   int    dim() const 	{return dim_;}
  int          num_nonzeros() const {return num_nonzeros_;}




	inline double norm() const
	{
		T sum(0.0);

		for (const_iterator p = begin(); p < end(); p++)
		{
			sum += p->value() * p->value();
		}

		return sqrt(sum);
	}

	std::ostream & print(std::ostream &s) const
	{
		for (typename Sparse_Vector<T>::const_iterator p = begin(); 
						p < end(); p++)
		{
				s << "( " << p->value() << ", " << p->index() << " ) \n";
		}
		return s;
	}

	std::ostream & print_base_one(std::ostream &s) const
	{
		for (typename Sparse_Vector<T>::const_iterator p = begin(); 
						p < end(); p++)
		{
				s << "( " << p->value() << ", " << p->index()+1 << " ) \n";
		}
		return s;
	}

			
};



template <class T>
inline T dot_product(const Sparse_Vector<T> &s, const Vector<T> &x)
{
	return s.dot_product(x);
}

template <class T>
inline T dot_product(const Vector<T> &x, const Sparse_Vector<T> &s)
{
	return s.dot_product(x);
}

template <class T>
inline T operator*(const Vector<T> &x, const Sparse_Vector<T> &s)
{
	return dot_product(x,s);
}

template <class T>
inline T operator*(const Sparse_Vector<T> &s, const Vector<T> &x)
{
	return dot_product(x,s);
}


template <class T>
inline double norm( const Sparse_Vector<T> & s)
{
	return s.norm();
}


template <class T>
std::ostream& operator<<(std::ostream &s, const Sparse_Vector<T> &A)
{

		return	A.print(s);
}


//}  /* namspace TNT::Linear_Algebra */
}	 /* namspace TNT */

#endif
// TNT_SPARSE_VECTOR_H