casacore
RigidVector.h
Go to the documentation of this file.
1 //# RigidVector.h: Fast Vector classes with fixed (templated) length
2 //# Copyright (C) 1996,1999,2001
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //#
27 //# $Id$
28 
29 #ifndef SCIMATH_RIGIDVECTOR_H
30 #define SCIMATH_RIGIDVECTOR_H
31 
32 #include <casacore/casa/aips.h>
33 #include <casacore/casa/Arrays/Vector.h>
34 #include <casacore/casa/BasicSL/Complex.h>
35 #include <casacore/casa/iosfwd.h>
36 
37 namespace casacore { //# NAMESPACE CASACORE - BEGIN
38 
39 //# forward
40 template <class T, Int n> class SquareMatrix;
41 // <summary> Fast Vector classes with fixed (templated) length </summary>
42 
43 // <use visibility=export>
44 
45 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
46 // </reviewed>
47 
48 // <prerequisite>
49 // <li> Vector
50 // <li> Complex
51 // </prerequisite>
52 //
53 // <etymology>
54 // RigidVector is a vector with a size fixed at compile time, i.e. Rigid
55 // as compared to the normal Vector class.
56 // </etymology>
57 //
58 // <synopsis>
59 // RigidVector is a specialized Vector class for short (<25 elements) vectors.
60 // It has a size fixed at compile time, avoids new and delete and uses
61 // copy semantics throughout.
62 // Unlike Vectors, RigidVectors have fixed zero origin and no strides,
63 // allowing fast indexing.
64 // The more common mathematical operations are defined for RigidVector,
65 // allowing element by element arithmetic, innerproduct and matrix
66 // multiplication (by a SquareMatrix). Conversion to and from normal
67 // vectors is provided.
68 // </synopsis>
69 //
70 // <example>
71 // <srcblock>
72 // // Create two RigidVectors
73 // RigidVector<Float,3> rv1(1.0,2.0,3.0),rv2(3.0,2.0,1.0);
74 // // Compute sum
75 // RigidVector<Float,3> rv3=rv1+rv2;
76 // // Compute innerproduct
77 // Float inprod=rv1*rv2;
78 // // Write out results
79 // cout << "rv1+rv2="<< rv3 <<", rv1*rv2="<< inprod<<endl;
80 // </srcblock>
81 // </example>
82 //
83 // <motivation>
84 // The standard Vector class is rather inefficient for short vectors, this
85 // class is designed for speed and simplicity.
86 // </motivation>
87 //
88 // <templating arg=T>
89 // <li> this class is meant for computation and assumes operators
90 // +,-,* to be defined.
91 // </templating>
92 //
93 // <thrown>
94 // <li> no exceptions
95 // </thrown>
96 //
97 // <todo asof="1996/11/07">
98 // <li> not all operations defined for Vectors are defined
99 // <li> default implementation of innerProduct is wrong for Complex vectors
100 // </todo>
101 
102 template <class T, Int n> class RigidVector {
103 
104  //# friends (could be out of line if compiler accepted that)
105  // Add two RigidVectors.
107  const RigidVector<T,n>& r) {
108  RigidVector<T,n> result=l;
109  return result+=r;
110  }
111  // Subtract two RigidVectors.
113  const RigidVector<T,n>& r) {
114  RigidVector<T,n> result=l;
115  return result-=r;
116  }
117  // The innerproduct of 2 RigidVectors.
118  friend T operator*(const RigidVector<T,n>& l,
119  const RigidVector<T,n>& r) {
120  T sum=T(0);
121  for (Int i=0; i<n; i++) sum+=l.v_p[i]*r.v_p[i];
122  return sum;
123  }
124  // Multiply a RigidVector by a scalar.
125  friend RigidVector<T,n> operator*(const T& f, const RigidVector<T,n>& v) {
126  RigidVector<T,n> r(v);
127  return r*=f;
128  }
129  // Multiply a RigidVector by a scalar.
130  friend RigidVector<T,n> operator*(const RigidVector<T,n>& v, const T& f) {
131  RigidVector<T,n> r(v);
132  return r*=f;
133  }
134  // Write out a RigidVector using the Vector output method.
135  friend ostream& operator<<(ostream& os, const RigidVector<T,n>& v) {
136  os << v.vector();
137  return os;
138  }
139  // Special matrix multiply of Complex matrix * Float vector.
141  const RigidVector<Float,4>& v);
142 public:
143  // RigidVector(Int dummy) {
144  // for (Int i=0; i<n; i++) v_p[i]=T(0);
145  // }
146  // Default constructor
148  for (Int i=0; i<n; i++) v_p[i]=T(0);
149  }
150  // Construct from scalar, sets all elements to c
151  RigidVector(const T& c) {
152  for (Int i=0; i<n; i++) v_p[i]=c;
153  }
154  // Construct a 2-element vector, fails for wrong size vectors.
155  RigidVector(const T& v0, const T& v1) {
156  if (n!=2) exit(1);
157  v_p[0]=v0; v_p[1]=v1;
158  }
159  // Construct a 3-element vector, fails for wrong size vectors.
160  RigidVector(const T& v0, const T& v1, const T& v2) {
161  if (n!=3) exit(1);
162  v_p[0]=v0; v_p[1]=v1; v_p[2]=v2;
163  }
164  // Construct a 4-element vector, fails for wrong size vectors.
165  RigidVector(const T& v0, const T& v1, const T& v2, const T& v3) {
166  if (n!=4) exit(1);
167  v_p[0]=v0; v_p[1]=v1; v_p[2]=v2; v_p[3]=v3;
168  }
169  // Construct a 5-element vector, fails for wrong size vectors.
170  RigidVector(const T& v0, const T& v1, const T& v2, const T& v3,
171  const T& v4) {
172  if (n!=5) exit(1);
173  v_p[0]=v0; v_p[1]=v1; v_p[2]=v2; v_p[3]=v3; v_p[4]=v4;
174  }
175  // Construct a 6-element vector, fails for wrong size vectors.
176  RigidVector(const T& v0, const T& v1, const T& v2, const T& v3,
177  const T& v4, const T& v5) {
178  if (n!=6) exit(1);
179  v_p[0]=v0; v_p[1]=v1; v_p[2]=v2; v_p[3]=v3; v_p[4]=v4; v_p[5]=v5;
180  }
181  // Construct from a c-array (copy semantics)
182  RigidVector(const T v[n]) {
183  for (Int i=0; i<n; i++) v_p[i]=v[i];
184  }
185  // Construct from a Vector.
186  RigidVector(const Vector<T> & v) {
187  for (Int i=0; i<n; i++) v_p[i]=v(i);
188  }
189  // Copy constructor, copy semantics.
191  for (Int i=0; i<n; i++) v_p[i]=v.v_p[i];
192  }
193  // Assign from a RigidVector.
195  for (Int i=0; i<n; i++) v_p[i]=v.v_p[i];
196  return *this;
197  }
198  // Assign from a Vector.
200  for (Int i=0; i<n; i++) v_p[i]=v(i);
201  return *this;
202  }
203  // Assign a scalar, sets all elements to c.
205  for (Int i=0; i<n; i++) v_p[i]=c;
206  return *this;
207  }
208  // Negation
210  for (Int i=0; i<n ;i++) v_p[i]=-v_p[i];
211  return *this;
212  }
213  // Addition
215  for (Int i=0; i<n; i++) v_p[i]+=v.v_p[i];
216  return *this;
217  }
219  for (Int i=0; i<n; i++) v_p[i]*=v.v_p[i];
220  return *this;
221  }
222  // Subtraction
224  for (Int i=0; i<n; i++) v_p[i]-=v.v_p[i];
225  return *this;
226  }
227  // Multiplication by scalar.
228  RigidVector<T,n>& operator*=(const T& val) {
229  for (Int i=0; i<n; i++) v_p[i]*=val;
230  return *this;
231  }
232  // Multiply vector by matrix: v*=M is equivalent to v=M*v;
234 
235  // Indexing by reference
236  T& operator()(Int i) { return v_p[i];}
237  // Indexing by const reference
238  const T& operator()(Int i) const { return v_p[i];}
239  //# Get const access to the underlying c-array
240  //#const T*& cArray() const { return v_p;}
241  // Convert to a regular Vector
242  Vector<T> vector() const {
243  Vector<T> v(n);
244  for (Int i=0; i<n; i++) v(i)=v_p[i];
245  return v;
246  }
247  // Square Root
249 
250 // // The following are needed for Image<RigidVector>
251 
252 // static IPosition shape() {return IPosition(1,n);}
253 
254 // static void* newCopyInfo (const TableRecord& record,
255 // const IPosition& sourceElementShape);
256 
257 // static void deleteCopyInfo (void*);
258 
259 // static void set (void* copyInfo, void* out,
260 // const Array<T>& in,
261 // const IPosition& shape);
262 // static void get (void* copyInfo, Array<T>& out,
263 // const void* in,
264 // const IPosition& shape);
265 
266 protected:
267  T v_p[n];
268 };
269 
270 // <summary> Mathematical operations involving RigidVectors </summary>
271 
272 // <group name=math>
273 //#Fails to compile
274 //#// Multiply vector by matrix.
275 //#template <class T, Int n>
276 //#inline RigidVector<T,n> operator*(const SquareMatrix<T,n>& m,
277 //# const RigidVector<T,n>& v) {
278 //# RigidVector<T,n> result(v);
279 //# return result*=m;
280 //#}
281 // Multiply vector by matrix.
283  const RigidVector<Float,4>& v) {
284  RigidVector<Float,4> result(v);
285  return result*=m;
286 }
287 // Multiply vector by matrix.
289  const RigidVector<Complex,4>& v) {
290  RigidVector<Complex,4> result(v);
291  return result*=m;
292 }
293 // </group>
294 
295 } //# NAMESPACE CASACORE - END
296 
297 #ifndef CASACORE_NO_AUTO_TEMPLATES
298 #include <casacore/scimath/Mathematics/RigidVector.tcc>
299 #endif //# CASACORE_NO_AUTO_TEMPLATES
300 #endif
RigidVector(const RigidVector< T, n > &v)
Copy constructor, copy semantics.
Definition: RigidVector.h:190
RigidVector< T, n > & operator=(const Vector< T > &v)
Assign from a Vector.
Definition: RigidVector.h:199
RigidVector(const T v[n])
Construct from a c-array (copy semantics)
Definition: RigidVector.h:182
RigidVector(const T &v0, const T &v1)
Construct a 2-element vector, fails for wrong size vectors.
Definition: RigidVector.h:155
friend ostream & operator<<(ostream &os, const RigidVector< T, n > &v)
Write out a RigidVector using the Vector output method.
Definition: RigidVector.h:135
RigidVector(const Vector< T > &v)
Construct from a Vector.
Definition: RigidVector.h:186
friend RigidVector< T, n > operator-(const RigidVector< T, n > &l, const RigidVector< T, n > &r)
Subtract two RigidVectors.
Definition: RigidVector.h:112
RigidVector< T, n > & operator*=(const SquareMatrix< T, n > &m)
Multiply vector by matrix: v*=M is equivalent to v=M*v;.
friend RigidVector< T, n > operator*(const RigidVector< T, n > &v, const T &f)
Multiply a RigidVector by a scalar.
Definition: RigidVector.h:130
RigidVector< T, n > & operator+=(const RigidVector< T, n > &v)
Addition
Definition: RigidVector.h:214
RigidVector(const T &v0, const T &v1, const T &v2, const T &v3)
Construct a 4-element vector, fails for wrong size vectors.
Definition: RigidVector.h:165
RigidVector()
RigidVector(Int dummy) { for (Int i=0; i<n; i++) v_p[i]=T(0); } Default constructor.
Definition: RigidVector.h:147
RigidVector< T, n > & operator=(const RigidVector< T, n > &v)
Assign from a RigidVector.
Definition: RigidVector.h:194
Vector< T > vector() const
Convert to a regular Vector.
Definition: RigidVector.h:242
RigidVector< T, n > & operator-=(const RigidVector< T, n > &v)
Subtraction.
Definition: RigidVector.h:223
friend RigidVector< Complex, 4 > operator*(const SquareMatrix< Complex, 4 > &m, const RigidVector< Float, 4 > &v)
Special matrix multiply of Complex matrix * Float vector.
const T & operator()(Int i) const
Indexing by const reference.
Definition: RigidVector.h:238
RigidVector< T, n > & operator*=(const T &val)
Multiplication by scalar.
Definition: RigidVector.h:228
RigidVector(const T &v0, const T &v1, const T &v2, const T &v3, const T &v4)
Construct a 5-element vector, fails for wrong size vectors.
Definition: RigidVector.h:170
RigidVector(const T &c)
Construct from scalar, sets all elements to c.
Definition: RigidVector.h:151
RigidVector(const T &v0, const T &v1, const T &v2)
Construct a 3-element vector, fails for wrong size vectors.
Definition: RigidVector.h:160
RigidVector(const T &v0, const T &v1, const T &v2, const T &v3, const T &v4, const T &v5)
Construct a 6-element vector, fails for wrong size vectors.
Definition: RigidVector.h:176
friend RigidVector< T, n > operator+(const RigidVector< T, n > &l, const RigidVector< T, n > &r)
Add two RigidVectors.
Definition: RigidVector.h:106
friend RigidVector< T, n > operator*(const T &f, const RigidVector< T, n > &v)
Multiply a RigidVector by a scalar.
Definition: RigidVector.h:125
friend T operator*(const RigidVector< T, n > &l, const RigidVector< T, n > &r)
The innerproduct of 2 RigidVectors.
Definition: RigidVector.h:118
RigidVector< T, n > & operator=(const T &c)
Assign a scalar, sets all elements to c.
Definition: RigidVector.h:204
T & operator()(Int i)
Indexing by reference.
Definition: RigidVector.h:236
RigidVector< T, n > & operator*=(const RigidVector< T, n > &v)
Definition: RigidVector.h:218
T v_p[n]
// The following are needed for Image<RigidVector>
Definition: RigidVector.h:267
RigidVector< T, n > & operator-()
Negation.
Definition: RigidVector.h:209
RigidVector< T, n > sqrt(const RigidVector< T, n > &v)
Square Root.
const Double c
Fundamental physical constants (SI units):
this file contains all the compiler specific defines
Definition: mainpage.dox:28
LatticeExprNode sum(const LatticeExprNode &expr)
int Int
Definition: aipstype.h:50
RigidVector< Float, 4 > operator*(const SquareMatrix< Float, 4 > &m, const RigidVector< Float, 4 > &v)
Multiply vector by matrix.
Definition: RigidVector.h:282
RigidVector< Complex, 4 > operator*(const SquareMatrix< Complex, 4 > &m, const RigidVector< Complex, 4 > &v)
Multiply vector by matrix.
Definition: RigidVector.h:288