7 * Template Numerical Toolkit (TNT): Linear Algebra Module
9 * Mathematical and Computational Sciences Division
10 * National Institute of Technology,
11 * Gaithersburg, MD USA
14 * This software was developed at the National Institute of Standards and
15 * Technology (NIST) by employees of the Federal Government in the course
16 * of their official duties. Pursuant to title 17 Section 105 of the
17 * United States Code, this software is not subject to copyright protection
18 * and is in the public domain. The Template Numerical Toolkit (TNT) is
19 * an experimental system. NIST assumes no responsibility whatsoever for
20 * its use by other parties, and makes no guarantees, expressed or implied,
21 * about its quality, reliability, or any other characteristic.
23 * BETA VERSION INCOMPLETE AND SUBJECT TO CHANGE
24 * see http://math.nist.gov/tnt for latest updates.
29 // Basic TNT numerical vector (0-based [i] AND 1-based (i) indexing )
35 #include "subscript.h"
50 typedef Subscript size_type;
52 typedef T element_type;
56 typedef const T* const_iterator;
57 typedef const T& const_reference;
59 Subscript lbound() const { return 1;}
63 T* vm1_; // pointer adjustment for optimzied 1-offset indexing
66 // internal helper function to create the array
69 void initialize(Subscript N)
71 // adjust pointers so that they are 1-offset:
72 // v_[] is the internal contiguous array, it is still 0-offset
86 #ifdef TNT_UNROLL_LOOPS
87 Subscript Nmod4 = N & 3;
88 Subscript N4 = N - Nmod4;
107 void set(const T& val)
112 #ifdef TNT_UNROLL_LOOPS
113 Subscript Nmod4 = N & 3;
114 Subscript N4 = N - Nmod4;
116 for (i=0; i<N4; i+=4)
124 for (i=N4; i< N; i++)
138 /* do nothing, if no memory has been previously allocated */
139 if (v_ == NULL) return ;
141 /* if we are here, then matrix was previously allocated */
153 iterator begin() { return v_;}
154 iterator end() { return v_ + n_; }
155 const iterator begin() const { return v_;}
156 const iterator end() const { return v_ + n_; }
167 Vector() : v_(0), vm1_(0), n_(0) {}
169 Vector(const Vector<T> &A) : v_(0), vm1_(0), n_(0)
175 Vector(Subscript N, const T& value = T()) : v_(0), vm1_(0), n_(0)
181 Vector(Subscript N, const T* v) : v_(0), vm1_(0), n_(0)
190 Vector<T>& newsize(Subscript N)
192 if (n_ == N) return *this;
203 Vector<T>& operator=(const Vector<T> &A)
208 if (n_ == A.n_) // no need to re-alloc
221 Vector<T>& operator=(const T& scalar)
227 inline Subscript dim() const
232 inline Subscript size() const
238 inline reference operator()(Subscript i)
240 #ifdef TNT_BOUNDS_CHECK
247 inline const_reference operator() (Subscript i) const
249 #ifdef TNT_BOUNDS_CHECK
256 inline reference operator[](Subscript i)
258 #ifdef TNT_BOUNDS_CHECK
265 inline const_reference operator[](Subscript i) const
267 #ifdef TNT_BOUNDS_CHECK
279 /* *************************** I/O ********************************/
282 std::ostream& operator<<(std::ostream &s, const Vector<T> &A)
288 for (Subscript i=0; i<N; i++)
289 s << A[i] << " " << std::endl;
296 std::istream & operator>>(std::istream &s, Vector<T> &A)
303 if ( !(N == A.size() ))
309 for (Subscript i=0; i<N; i++)
316 // *******************[ basic matrix algorithms ]***************************
320 Vector<T> operator+(const Vector<T> &A,
323 Subscript N = A.dim();
331 tmp[i] = A[i] + B[i];
337 Vector<T> operator-(const Vector<T> &A,
340 Subscript N = A.dim();
348 tmp[i] = A[i] - B[i];
354 Vector<T> operator*(const Vector<T> &A,
357 Subscript N = A.dim();
365 tmp[i] = A[i] * B[i];
371 Vector<T> operator*(const Vector<T> &A,
374 Subscript N = A.dim();
387 T dot_prod(const Vector<T> &A, const Vector<T> &B)
389 Subscript N = A.dim();
390 assert(N == B.dim());
401 // inplace versions of the above template functions
410 const Subscript N = A.dim();
418 // same with separate output vector
426 const Subscript N = A.dim();
441 const Subscript N = A.dim();
455 const Subscript N = A.dim();
469 const Subscript N = A.dim();
481 const Subscript N = A.dim();
488 } /* namespace TNT */