My Project
Loading...
Searching...
No Matches
tnt_vector.h
1/*
2*
3* Template Numerical Toolkit (TNT)
4*
5* Mathematical and Computational Sciences Division
6* National Institute of Technology,
7* Gaithersburg, MD USA
8*
9*
10* This software was developed at the National Institute of Standards and
11* Technology (NIST) by employees of the Federal Government in the course
12* of their official duties. Pursuant to title 17 Section 105 of the
13* United States Code, this software is not subject to copyright protection
14* and is in the public domain. NIST assumes no responsibility whatsoever for
15* its use by other parties, and makes no guarantees, expressed or implied,
16* about its quality, reliability, or any other characteristic.
17*
18*/
19
20
21
22#ifndef TNT_VECTOR_H
23#define TNT_VECTOR_H
24
25#include "tnt_subscript.h"
26#include <cstdlib>
27#include <cassert>
28#include <iostream>
29#include <sstream>
30#include <cmath>
31
32using namespace std;
33
34namespace TNT
35{
36
37//namespace Linear_Algebra
38// {
39
40
47template <class T>
48class Vector
49{
50
51
52 public:
53
54 typedef Subscript size_type;
55 typedef T value_type;
56 typedef T element_type;
57 typedef T* pointer;
58 typedef T* iterator;
59 typedef T& reference;
60 typedef const T* const_iterator;
61 typedef const T& const_reference;
62
63 Subscript lbound() const { return 1;}
64
65 private:
66 T* v_;
67 T* vm1_; // pointer adjustment for optimzied 1-offset indexing
68 Subscript n_;
69
70 // internal helper function to create the array
71 // of row pointers
72
73 void initialize(Subscript N)
74 {
75 // adjust pointers so that they are 1-offset:
76 // v_[] is the internal contiguous array, it is still 0-offset
77 //
78 assert(v_ == NULL);
79 v_ = new T[N];
80 assert(v_ != NULL);
81 vm1_ = v_-1;
82 n_ = N;
83 }
84
85 void copy(const T* v)
86 {
87 Subscript N = n_;
88 Subscript i;
89
90#ifdef TNT_UNROLL_LOOPS
91 Subscript Nmod4 = N & 3;
92 Subscript N4 = N - Nmod4;
93
94 for (i=0; i<N4; i+=4)
95 {
96 v_[i] = v[i];
97 v_[i+1] = v[i+1];
98 v_[i+2] = v[i+2];
99 v_[i+3] = v[i+3];
100 }
101
102 for (i=N4; i< N; i++)
103 v_[i] = v[i];
104#else
105
106 for (i=0; i< N; i++)
107 v_[i] = v[i];
108#endif
109 }
110
111 void set(const T& val)
112 {
113 Subscript N = n_;
114 Subscript i;
115
116#ifdef TNT_UNROLL_LOOPS
117 Subscript Nmod4 = N & 3;
118 Subscript N4 = N - Nmod4;
119
120 for (i=0; i<N4; i+=4)
121 {
122 v_[i] = val;
123 v_[i+1] = val;
124 v_[i+2] = val;
125 v_[i+3] = val;
126 }
127
128 for (i=N4; i< N; i++)
129 v_[i] = val;
130#else
131
132 for (i=0; i< N; i++)
133 v_[i] = val;
134
135#endif
136 }
137
138
139
140 void destroy()
141 {
142 /* do nothing, if no memory has been previously allocated */
143 if (v_ == NULL) return ;
144
145 /* if we are here, then matrix was previously allocated */
146 delete [] (v_);
147
148 v_ = NULL;
149 vm1_ = NULL;
150 }
151
152
153 public:
154
155 // access
156
157 iterator begin() { return v_;}
158 iterator end() { return v_ + n_; }
159 const iterator begin() const { return v_;}
160 const iterator end() const { return v_ + n_; }
161
162 operator const T* const() { return v_; }
163 operator T*() { return v_; }
164
165 // destructor
166
167 ~Vector()
168 {
169 destroy();
170 }
171
172 // constructors
173
174 Vector() : v_(0), vm1_(0), n_(0) {};
175
176 Vector(const Vector<T> &A) : v_(0), vm1_(0), n_(0)
177 {
178 initialize(A.n_);
179 copy(A.v_);
180 }
181
182 Vector(Subscript N, const T& value = T()) : v_(0), vm1_(0), n_(0)
183 {
184 initialize(N);
185 set(value);
186 }
187
188 Vector(Subscript N, const T* v) : v_(0), vm1_(0), n_(0)
189 {
190 initialize(N);
191 copy(v);
192 }
193
194 Vector(Subscript N, char *s) : v_(0), vm1_(0), n_(0)
195 {
196 initialize(N);
197 std::istringstream ins(s);
198
199 Subscript i;
200
201 for (i=0; i<N; i++)
202 ins >> v_[i];
203 }
204
205
206 // methods
207 //
208 Vector<T>& newsize(Subscript N)
209 {
210 if (n_ == N) return *this;
211
212 destroy();
213 initialize(N);
214
215 return *this;
216 }
217
218
219 // assignments
220 //
221 Vector<T>& operator=(const Vector<T> &A)
222 {
223 if (v_ == A.v_)
224 return *this;
225
226 if (n_ == A.n_) // no need to re-alloc
227 copy(A.v_);
228
229 else
230 {
231 destroy();
232 initialize(A.n_);
233 copy(A.v_);
234 }
235
236 return *this;
237 }
238
239 Vector<T>& operator=(const T& scalar)
240 {
241 set(scalar);
242 return *this;
243 }
244
245 inline Subscript dim() const
246 {
247 return n_;
248 }
249
250 inline Subscript size() const
251 {
252 return n_;
253 }
254
255
256 inline reference operator()(Subscript i)
257 {
258#ifdef TNT_BOUNDS_CHECK
259 assert(1<=i);
260 assert(i <= n_) ;
261#endif
262 return vm1_[i];
263 }
264
265 inline const_reference operator() (Subscript i) const
266 {
267#ifdef TNT_BOUNDS_CHECK
268 assert(1<=i);
269 assert(i <= n_) ;
270#endif
271 return vm1_[i];
272 }
273
274 inline reference operator[](Subscript i)
275 {
276#ifdef TNT_BOUNDS_CHECK
277 assert(0<=i);
278 assert(i < n_) ;
279#endif
280 return v_[i];
281 }
282
283 inline const_reference operator[](Subscript i) const
284 {
285#ifdef TNT_BOUNDS_CHECK
286 assert(0<=i);
287
288
289
290
291
292
293 assert(i < n_) ;
294#endif
295 return v_[i];
296 }
297
298
299
300};
301
302
303/* *************************** I/O ********************************/
304
305template <class T>
306std::ostream& operator<<(std::ostream &s, const Vector<T> &A)
307{
308 Subscript N=A.dim();
309
310 s << N << "\n";
311
312 for (Subscript i=0; i<N; i++)
313 s << A[i] << " " << "\n";
314 s << "\n";
315
316 return s;
317}
318
319template <class T>
320std::istream & operator>>(std::istream &s, Vector<T> &A)
321{
322
323 Subscript N;
324
325 s >> N;
326
327 if ( !(N == A.size() ))
328 {
329 A.newsize(N);
330 }
331
332
333 for (Subscript i=0; i<N; i++)
334 s >> A[i];
335
336
337 return s;
338}
339
340// *******************[ basic matrix algorithms ]***************************
341
342
343template <class T>
344Vector<T> operator+(const Vector<T> &A,
345 const Vector<T> &B)
346{
347 Subscript N = A.dim();
348
349 assert(N==B.dim());
350
351 Vector<T> tmp(N);
352 Subscript i;
353
354 for (i=0; i<N; i++)
355 tmp[i] = A[i] + B[i];
356
357 return tmp;
358}
359
360template <class T>
361Vector<T> operator+=(Vector<T> &A,
362 const Vector<T> &B)
363{
364 Subscript N = A.dim();
365
366 assert(N==B.dim());
367
368 Subscript i;
369
370 for (i=0; i<N; i++)
371 A[i] += B[i];
372
373 return A;
374}
375
376template <class T>
377Vector<T> operator-(const Vector<T> &A,
378 const Vector<T> &B)
379{
380 Subscript N = A.dim();
381
382 assert(N==B.dim());
383
384 Vector<T> tmp(N);
385 Subscript i;
386
387 for (i=0; i<N; i++)
388 tmp[i] = A[i] - B[i];
389
390 return tmp;
391}
392
393template <class T>
394Vector<T> operator-=(Vector<T> &A,
395 const Vector<T> &B)
396{
397 Subscript N = A.dim();
398
399 assert(N==B.dim());
400
401 Subscript i;
402
403 for (i=0; i<N; i++)
404 A[i] -= B[i];
405
406 return A;
407}
408
409
410
411
412template <class T>
413Vector<T> elementwise_mult(const Vector<T> &A, const Vector<T> &B)
414{
415 Subscript N = A.dim();
416
417 assert(N==B.dim());
418
419 Vector<T> tmp(N);
420 Subscript i;
421
422 for (i=0; i<N; i++)
423 tmp[i] = A[i] * B[i];
424
425 return tmp;
426}
427
428
429template <class T>
430double norm(const Vector<T> &A)
431{
432 Subscript N = A.dim();
433
434 double sum = 0.0;
435 for (int i=0; i<N; i++)
436 sum += abs(A[i])*abs(A[i]);
437 return sqrt(sum);
438}
439
440
441
442template <class T>
443T dot_prod(const Vector<T> &A, const Vector<T> &B)
444{
445 Subscript N = A.dim();
446 assert(N == B.dim());
447
448 Subscript i;
449 T sum = 0;
450
451 for (i=0; i<N; i++)
452 sum += A[i] * B[i];
453
454 return sum;
455}
456
457template <class T>
458inline T dot_product(const Vector<T> &A, const Vector<T> &B)
459{
460 return dot_prod(A, B);
461}
462
463
464template <class T>
465inline T operator*(const Vector<T> &A,
466 const Vector<T> &B)
467{
468 return dot_prod(A,B);
469}
470
471
472template <class T>
473Vector<T> operator*(const T &a, const Vector<T> &A)
474{
475 Subscript N = A.dim();
476 Vector<T> r(N);
477
478 for (int i=0; i<N; i++)
479 r[i] = A[i] * a;
480
481 return r;
482}
483
484template <class T>
485inline Vector<T> operator*(const Vector<T> &A, const T& a)
486{
487 return a * A;
488}
489
490//} /* namspace TNT::Linear_Algebra */
491} /* namespace TNT */
492
493
494#endif
495// TNT_VECTOR_H
Definition tnt_vector.h:49
Definition tnt_array1d.h:36
double norm(const Matrix< T > &A)
Definition tnt_matrix.h:996