Vector.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2003 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // espic/src/Vector.h
10 
11 #ifndef __Vector_H
12 #define __Vector_H
13 
14 #include "Traits.h"
15 #include "Estimate.h"
16 
17 #include <iostream>
18 #include <complex>
19 
21 template <unsigned N, typename T>
22 class Vector {
23 
24  template<typename U>
25  static void zero (Vector<N, U>& v)
26  { for (unsigned i=0; i<N; i++) v.x[i] = T(0.0); }
27 
29  template<unsigned M, typename U>
30  static void zero (Vector<N, Vector<M, U> >& v)
31  { /* do nothing */ }
32 
33 public:
34  T x[N];
35 
37  Vector () { zero (*this); }
38 
39  Vector (T x0)
40  { x[0] = x0; }
41 
42  Vector (T x0, T x1)
43  { x[0] = x0; x[1] = x1; }
44 
45  Vector (T x0, T x1, T x2)
46  { x[0] = x0; x[1] = x1; x[2] = x2; }
47 
48  Vector (T x0, T x1, T x2, T x3)
49  { x[0] = x0; x[1] = x1; x[2] = x2; x[3] = x3; }
50 
52  template<typename U> Vector (const Vector<N, U>& s)
53  { operator=(s); }
54 
56  template<typename U> Vector& operator = (const Vector<N, U>& s)
57  { for (unsigned i=0; i<N; i++) x[i] = T(s.x[i]); return *this; }
58 
60  Vector& operator = (const T& scalar)
61  { x[0] = scalar; for (unsigned i=1; i<N; i++) x[i] = 0.0; return *this; }
62 
64  Vector& operator += (const Vector& s)
65  { for (unsigned i=0; i<N; i++) x[i] += s.x[i]; return *this; }
66 
68  Vector& operator -= (const Vector& s)
69  { for (unsigned i=0; i<N; i++) x[i] -= s.x[i]; return *this; }
70 
76  template<typename U>
78  Vector& operator *= (const U& a)
79  { for (unsigned i=0; i<N; i++) x[i] *= a; return *this; }
80 
82  template<typename U>
83  Vector& operator /= (const U& a)
84  { for (unsigned i=0; i<N; i++) x[i] /= a; return *this; }
85 
87  bool operator == (const Vector& b) const
88  { for(unsigned i=0; i<N; i++) if(x[i]!=b.x[i]) return false; return true; }
89 
91  bool operator != (const Vector& b) const
92  { return ! operator==(b); }
93 
95  template<typename U>
96  const friend Vector operator + (Vector a, const Vector<N,U>& b)
97  { a+=b; return a; }
98 
100  template<typename U>
101  const friend Vector operator - (Vector a, const Vector<N,U>& b)
102  { a-=b; return a; }
103 
105  const friend Vector operator * (Vector a, T c)
106  { a*=c; return a; }
107 
109  const friend Vector operator * (T c, Vector a)
110  { a*=c; return a; }
111 
113  const friend T operator * (const Vector& a, const Vector& b)
114  { T r=0; for (unsigned i=0; i<N; i++) r += a[i]*b[i]; return r; }
115 
117  const friend Vector operator / (Vector a, T c)
118  { a/=c; return a; }
119 
121  const friend Vector operator - (Vector s)
122  { for (unsigned i=0; i<N; i++) s.x[i] = -s.x[i]; return s; }
123 
125  T& operator [] (unsigned n)
126  { return x[n]; }
127 
129  const T operator [] (unsigned n) const
130  { return x[n]; }
131 
133  unsigned size () const { return N; }
134 
136  static const Vector basis (unsigned i)
137  { Vector v; v[i] = 1.0; return v; }
138 
139 };
140 
142 template <typename T>
143 const Vector<3,T> cross (const Vector<3,T>& a, const Vector<3,T>& b)
144 {
145  Vector<3,T> result;
146  unsigned j, k;
147  for (unsigned i=0; i<3; i++) {
148  j = (i+1)%3; k = (i+2)%3;
149  result[i] = a[j]*b[k] - a[k]*b[j];
150  }
151 
152  return result;
153 }
154 
155 template <typename T>
156 T normsq (const T& v)
157 {
158  return v*v;
159 }
160 
161 template <typename T>
162 T normsq (const std::complex<T>& v)
163 {
164  return std::norm(v);
165 }
166 
167 template <typename T, typename U=T>
168 T normsq (const class Estimate<T,U>& estimate)
169 {
170  return normsq(estimate.get_value());
171 }
172 
174 template <unsigned N, typename T>
175 T normsq (const Vector< N, T>& v)
176 {
177  T sum = normsq(v[0]);
178  for (unsigned i=1; i < N; i++)
179  sum += normsq(v[i]);
180  return sum;
181 }
182 
184 template <unsigned N, template<typename T> class V, typename U >
185 U normsq (const Vector< N, V<U> >& v)
186 {
187  U sum = normsq(v[0]);
188  for (unsigned i=1; i < N; i++)
189  sum += normsq(v[i]);
190  return sum;
191 }
192 
194 template <unsigned N, typename T>
195 T norm(const Vector<N, T> &v)
196 {
197  return sqrt(normsq(v));
198 }
199 
200 
201 template<unsigned N, typename T>
202 Vector<N,T> real (const Vector< N, std::complex<T> >& input)
203 {
204  Vector<N,T> result;
205  for (unsigned i=0; i < N; i++)
206  result[i] = input[i].real();
207  return result;
208 }
209 
210 template<unsigned N, typename T>
211 Vector<N,T> imag (const Vector< N, std::complex<T> >& input)
212 {
213  Vector<N,T> result;
214  for (unsigned i=0; i < N; i++)
215  result[i] = input[i].imag();
216  return result;
217 }
218 
219 template<unsigned N, typename T>
220 Vector<N, std::complex<T> > conj (const Vector< N, std::complex<T> >& input)
221 {
223  for (unsigned i=0; i < N; i++)
224  result[i] = std::conj(input[i]);
225  return result;
226 }
227 
228 template<unsigned N, typename T>
229 bool myfinite (const Vector< N, T>& input)
230 {
231  for (unsigned i=0; i < N; i++)
232  if (!myfinite( input[i] ))
233  return false;
234  return true;
235 }
236 
238 template<unsigned N, typename T> struct DatumTraits< Vector<N,T> >
239 {
240  typedef T element_type;
241 
242  static inline unsigned ndim () { return N; }
243  static inline T& element (Vector<N,T>& t, unsigned i)
244  { return t[i]; }
245  static inline const T element (const Vector<N,T>& t, unsigned i)
246  { return t[i]; }
247 };
248 
250 template<unsigned N, typename T>
251 std::ostream& operator<< (std::ostream& ostr, const Vector<N,T>& v)
252 {
253  ostr << "(" << v[0];
254  for (unsigned i=1; i<N; i++)
255  ostr << "," << v[i];
256  return ostr << ")";
257 }
258 
259 template<unsigned N, typename T>
260 std::istream& operator >> (std::istream& is, Vector<N,T>& v)
261 {
262  char c;
263  is >> c;
264 
265  if (c != '(') {
266  is.setstate(std::istream::failbit);
267  return is;
268  }
269 
270  is >> v[0];
271 
272  for (unsigned i=1; i<N; i++)
273  {
274  is >> c >> v[i];
275  if (c != ',') {
276  is.setstate(std::istream::failbit);
277  return is;
278  }
279  }
280 
281  is >> c;
282 
283  if (c != ')')
284  is.setstate(std::istream::failbit);
285 
286  return is;
287 }
288 
289 #endif /* not __Vector_H defined */
290 
Vector & operator-=(const Vector &s)
friend const friend Vector operator*(Vector a, T c)
friend const friend Vector operator+(Vector a, const Vector< N, U > &b)
Estimates with a value, , and a variance, .
Definition: Estimate.h:33
Vector & operator+=(const Vector &s)
Vector.
Definition: Vector.h:22
Vector & operator*=(const U &a)
unsigned size() const
bool operator==(const Vector &b) const
bool operator!=(const Vector &b) const
static const Vector basis(unsigned i)
T get_value() const
friend const friend Vector operator-(Vector a, const Vector< N, U > &b)
Vector & operator/=(const U &a)
Vector & operator=(const Vector< N, U > &s)
T & operator[](unsigned n)
Traits of the data type.
Definition: Traits.h:70
friend const friend Vector operator/(Vector a, T c)

Generated using doxygen 1.8.17