Protium
Math and Design Features
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
Vector.h
Go to the documentation of this file.
1 #ifndef Protium_Vector_h_
2 #define Protium_Vector_h_
3 
6 
7 #include <vector>
8 #include <cmath>
9 
10 namespace Protium{
11 
12  namespace LinearAlgebra{
13 
15  template<typename T, int n>
16  class Vector;
17 
19  template<typename T, int n>
20  struct SubVector{
21  Vector<T, n-1> Of(const Vector<T,n>& other, const int& i=0){
22  Vector<T,n-1> temp;
23  int index=0;
24  for(int j=0; j<n;j++ ){
25  if(j!=i)
26  temp[index++] = T(other.At(j) );
27  }
28  return temp;
29  }
30  };
31 
33  template<typename T>
34  struct SubVector<T,2>{
35  Vector<T, 1> Of(const Vector<T,2>& other, const int& i=0){
36  Vector<T,1> temp;
37  if(i==0)
38  temp[0] = T(other.At(1) );
39  else
40  temp[0] = T(other.At(0) );
41  return temp;
42  }
43  };
44 
46  template<typename T>
47  struct SubVector<T,1>{
48  Vector<T, 0> Of(const Vector<T,1>& other, const int& i=0){
49  PROTIUM_STATIC_ASSERT(false, ERROR_NO_SUBVECTOR_OF_LENGTH_0() );
50  }
51  };
52 
53  template <typename T, int n=3>
54  class Vector : public Protium::Allocation::DefaultSmallObject {
55 
57  std::vector<T> fComponents;
58 
59  public:
60 
62  void Init(){
63  PROTIUM_STATIC_ASSERT(n>0, ERROR_NO_SUBVECTOR_OF_LENGTH_0 );
64  fComponents.clear();
65  for(int i=0; i<n;i++)fComponents.push_back( T(1) );
66  }
67 
72 
73  Init();
74 
75  this->Normalize( amplitude );
76  }
77 
82  Init();
83  for(int i=0; i<n; i++)
84  fComponents[i] = T(input[i]);
85  }
86 
89  Vector<T,n>(const std::vector<T>& vec) : Protium::Allocation::DefaultSmallObject(){
90  Init();
91  for(int i=0; i<n;i++)
92  fComponents[i] = T(vec[i] );
93  }
94 
98  Init();
99  for(int i=0; i<n; i++){
100  fComponents[i] = T( other.At(i) );
101  }
102  }
103 
106  virtual ~Vector(){
107  fComponents.clear();
108  }
109 
113  static int NDimensions() {
114  return n;
115  }
116 
120  virtual T Norm() const {
121  return sqrt( (*this)*(*this) );
122  }
123 
128  virtual T Normalize(const T& norm){
129  T old_norm = this->Norm();
130  if(old_norm == norm) return norm;
131  if(old_norm ==0) return 0;
132  for(int i=0 ;i<n; i++ )
133  fComponents[i] *= (norm/old_norm);
134  return this->Norm();
135  }
136 
142  Vector<T,n-1> GetSubVector(const int& i){
143  SubVector<T,n> helper;
144  return helper.Of( (*this), i );
145  }
146 
149  T& operator[](const int& index){
150  return fComponents[index];
151  }
152 
155  const T& At(const int& index) const{
156  return fComponents.at(index);
157  }
158 
163  //PROTIUM_STATIC_ASSERT(n == m,"Vector Dimensions Must Match");
164  for(int i=0; i<n;i++)
165  (*this)[i] += rhs.At(i);
166  return *this;
167  }
168 
171  Vector<T,n>& operator*=(const T& rhs) {
172  //PROTIUM_STATIC_ASSERT(n == m,"Vector Dimensions Must Match");
173  for(int i=0; i<n;i++)
174  (*this)[i] *=rhs;
175  return *this;
176  }
177 
181  //PROTIUM_STATIC_ASSERT(n == m,"Vector Dimensions Must Match");
182  for(int i=0; i<n;i++)
183  (*this)[i] -=rhs.At(i);
184  return *this;
185  }
186 
189  const Vector<T,n> operator+(const Vector<T,n>& rhs) const {
190  return Vector<T,n>(*this) += rhs;
191  }
192 
195  const Vector<T,n> operator-(const Vector<T,n>& rhs) const {
196  return Vector<T,n>(*this) -= rhs;
197  }
198 
203  const T operator*(const Vector<T,n>& rhs) const {
204  T ret=0;
205  //PROTIUM_STATIC_ASSERT(n == m,"Vector Dimensions Must Match");
206  for(int i=0; i<n;i++){
207  ret+= (this->At(i)) * (rhs.At(i) );
208  }
209 
210  return ret;
211  }
212 
217  const Vector<T,n> operator*(const T& rhs) const {
218  return Vector<T,n>(*this) *=rhs;
219  }
220 
224  for(int i=0; i<n;i++) fComponents.at(i) = T(rhs.At(i) );
225  return *this;
226  }
227 
230  bool operator==( const Vector<T,n> &rhs) const {
231  bool ret=true;
232  //PROTIUM_STATIC_ASSERT(n == m,"Vector Dimensions Must Match");
233  for(int i=0; i<n;i++)
234  ret &= ( this->At(i) == rhs.At(i) );
235  return ret;
236  }
237 
240  bool operator!=( const Vector<T,n> &rhs) const {
241  return !(*this == rhs);
242  }
243 
245  static Vector<T,n> UnitVector(int dim=0){
246  Vector<T,n> vec;
247  for(int i=0;i<n;i++)
248  if(i==dim)
249  vec[i]=T(1);
250  else
251  vec[i]=T(0);
252  return vec;
253  }
254 
255  };
256 
274  }
275 }
276 
277 
278 #endif //File Guardian
279 
virtual T Normalize(const T &norm)
Definition: Vector.h:128
Vector< double, 10 > TenVector
Definition: Vector.h:268
Vector< double, 13 > ThirteenVector
Definition: Vector.h:271
Vector< double, 5 > FiveVector
Definition: Vector.h:263
Vector< double, 12 > TwelveVector
Definition: Vector.h:270
Vector< double, 6 > SixVector
Definition: Vector.h:264
const T operator*(const Vector< T, n > &rhs) const
Definition: Vector.h:203
Vector< T, 0 > Of(const Vector< T, 1 > &other, const int &i=0)
Definition: Vector.h:48
void Init()
Initialization of vector components to.
Definition: Vector.h:62
bool operator!=(const Vector< T, n > &rhs) const
Definition: Vector.h:240
bool operator==(const Vector< T, n > &rhs) const
Definition: Vector.h:230
#define PROTIUM_STATIC_ASSERT(expr, msg)
Use for performing static assertion.
Definition: Assert.h:16
T & operator[](const int &index)
Definition: Vector.h:149
Vector< double, 11 > ElevenVector
Definition: Vector.h:269
Vector< T, n > & operator-=(const Vector< T, n > &rhs)
Definition: Vector.h:180
const Vector< T, n > operator*(const T &rhs) const
Definition: Vector.h:217
const Vector< T, n > operator+(const Vector< T, n > &rhs) const
Definition: Vector.h:189
Vector< T, n > & operator+=(const Vector< T, n > &rhs)
Definition: Vector.h:162
Vector< double, 14 > FourteenVector
Definition: Vector.h:272
std::vector< T > fComponents
Holds the components of the vector.
Definition: Vector.h:57
Vector< double, 7 > SevenVector
Definition: Vector.h:265
Vector< double, 8 > EightVector
Definition: Vector.h:266
Vector< T, n > & operator*=(const T &rhs)
Definition: Vector.h:171
const Vector< T, n > operator-(const Vector< T, n > &rhs) const
Definition: Vector.h:195
virtual T Norm() const
Definition: Vector.h:120
Vector< double, 15 > FifteenVector
Definition: Vector.h:273
Vector< double, 9 > NineVector
Definition: Vector.h:267
static Vector< T, n > UnitVector(int dim=0)
Constructs a unit vector in the given dimension.
Definition: Vector.h:245
Vector< T, n > & operator=(const Vector< T, n > &rhs)
Definition: Vector.h:223
Inherit from this in order to benefit from small object allocation.
Definition: SmallObject.h:131
Implementation of vector to be used in Linear Algebra.
Definition: Vector.h:16
Vector< T, 1 > Of(const Vector< T, 2 > &other, const int &i=0)
Definition: Vector.h:35
const T & At(const int &index) const
Definition: Vector.h:155
Vector< double, 3 > ThreeVector
Convenience typedef of a double three vector.
Definition: Vector.h:260
Vector< T, n-1 > Of(const Vector< T, n > &other, const int &i=0)
Definition: Vector.h:21
Helper template for generating subvectors of vectors.
Definition: Vector.h:20
Vector< double, 4 > FourVector
Convenience typedef of a double four vector.
Definition: Vector.h:262
Vector< double, 2 > TwoVector
Convenience Typedef of a double twovector.
Definition: Vector.h:258
Vector< T, n-1 > GetSubVector(const int &i)
Definition: Vector.h:142