casacore
Loading...
Searching...
No Matches
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: casa-feedback@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#ifndef SCIMATH_RIGIDVECTOR_H
27#define SCIMATH_RIGIDVECTOR_H
28
29#include <casacore/casa/aips.h>
30#include <casacore/casa/Arrays/Vector.h>
31#include <casacore/casa/BasicSL/Complex.h>
32#include <casacore/casa/iosfwd.h>
33
34namespace casacore { //# NAMESPACE CASACORE - BEGIN
35
36//# forward
37template <class T, Int n> class SquareMatrix;
38// <summary> Fast Vector classes with fixed (templated) length </summary>
39
40// <use visibility=export>
41
42// <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
43// </reviewed>
44
45// <prerequisite>
46// <li> Vector
47// <li> Complex
48// </prerequisite>
49//
50// <etymology>
51// RigidVector is a vector with a size fixed at compile time, i.e. Rigid
52// as compared to the normal Vector class.
53// </etymology>
54//
55// <synopsis>
56// RigidVector is a specialized Vector class for short (<25 elements) vectors.
57// It has a size fixed at compile time, avoids new and delete and uses
58// copy semantics throughout.
59// Unlike Vectors, RigidVectors have fixed zero origin and no strides,
60// allowing fast indexing.
61// The more common mathematical operations are defined for RigidVector,
62// allowing element by element arithmetic, innerproduct and matrix
63// multiplication (by a SquareMatrix). Conversion to and from normal
64// vectors is provided.
65// </synopsis>
66//
67// <example>
68// <srcblock>
69// // Create two RigidVectors
70// RigidVector<Float,3> rv1(1.0,2.0,3.0),rv2(3.0,2.0,1.0);
71// // Compute sum
72// RigidVector<Float,3> rv3=rv1+rv2;
73// // Compute innerproduct
74// Float inprod=rv1*rv2;
75// // Write out results
76// cout << "rv1+rv2="<< rv3 <<", rv1*rv2="<< inprod<<endl;
77// </srcblock>
78// </example>
79//
80// <motivation>
81// The standard Vector class is rather inefficient for short vectors, this
82// class is designed for speed and simplicity.
83// </motivation>
84//
85// <templating arg=T>
86// <li> this class is meant for computation and assumes operators
87// +,-,* to be defined.
88// </templating>
89//
90// <thrown>
91// <li> no exceptions
92// </thrown>
93//
94// <todo asof="1996/11/07">
95// <li> not all operations defined for Vectors are defined
96// <li> default implementation of innerProduct is wrong for Complex vectors
97// </todo>
98
99template <class T, Int n> class RigidVector {
100
101 //# friends (could be out of line if compiler accepted that)
102 // Add two RigidVectors.
104 const RigidVector<T,n>& r) {
105 RigidVector<T,n> result=l;
106 return result+=r;
107 }
108 // Subtract two RigidVectors.
110 const RigidVector<T,n>& r) {
111 RigidVector<T,n> result=l;
112 return result-=r;
113 }
114 // The innerproduct of 2 RigidVectors.
115 friend T operator*(const RigidVector<T,n>& l,
116 const RigidVector<T,n>& r) {
117 T sum=T(0);
118 for (Int i=0; i<n; i++) sum+=l.v_p[i]*r.v_p[i];
119 return sum;
120 }
121 // Multiply a RigidVector by a scalar.
122 friend RigidVector<T,n> operator*(const T& f, const RigidVector<T,n>& v) {
123 RigidVector<T,n> r(v);
124 return r*=f;
125 }
126 // Multiply a RigidVector by a scalar.
127 friend RigidVector<T,n> operator*(const RigidVector<T,n>& v, const T& f) {
128 RigidVector<T,n> r(v);
129 return r*=f;
130 }
131 // Write out a RigidVector using the Vector output method.
132 friend ostream& operator<<(ostream& os, const RigidVector<T,n>& v) {
133 os << v.vector();
134 return os;
135 }
136 // Special matrix multiply of Complex matrix * Float vector.
138 const RigidVector<Float,4>& v);
139public:
140 // RigidVector(Int dummy) {
141 // for (Int i=0; i<n; i++) v_p[i]=T(0);
142 // }
143 // Default constructor
145 for (Int i=0; i<n; i++) v_p[i]=T(0);
146 }
147 // Construct from scalar, sets all elements to c
148 RigidVector(const T& c) {
149 for (Int i=0; i<n; i++) v_p[i]=c;
150 }
151 // Construct a 2-element vector, fails for wrong size vectors.
152 RigidVector(const T& v0, const T& v1) {
153 if (n!=2) exit(1);
154 v_p[0]=v0; v_p[1]=v1;
155 }
156 // Construct a 3-element vector, fails for wrong size vectors.
157 RigidVector(const T& v0, const T& v1, const T& v2) {
158 if (n!=3) exit(1);
159 v_p[0]=v0; v_p[1]=v1; v_p[2]=v2;
160 }
161 // Construct a 4-element vector, fails for wrong size vectors.
162 RigidVector(const T& v0, const T& v1, const T& v2, const T& v3) {
163 if (n!=4) exit(1);
164 v_p[0]=v0; v_p[1]=v1; v_p[2]=v2; v_p[3]=v3;
165 }
166 // Construct a 5-element vector, fails for wrong size vectors.
167 RigidVector(const T& v0, const T& v1, const T& v2, const T& v3,
168 const T& v4) {
169 if (n!=5) exit(1);
170 v_p[0]=v0; v_p[1]=v1; v_p[2]=v2; v_p[3]=v3; v_p[4]=v4;
171 }
172 // Construct a 6-element vector, fails for wrong size vectors.
173 RigidVector(const T& v0, const T& v1, const T& v2, const T& v3,
174 const T& v4, const T& v5) {
175 if (n!=6) exit(1);
176 v_p[0]=v0; v_p[1]=v1; v_p[2]=v2; v_p[3]=v3; v_p[4]=v4; v_p[5]=v5;
177 }
178 // Construct from a c-array (copy semantics)
179 RigidVector(const T v[n]) {
180 for (Int i=0; i<n; i++) v_p[i]=v[i];
181 }
182 // Construct from a Vector.
184 for (Int i=0; i<n; i++) v_p[i]=v(i);
185 }
186 // Copy constructor, copy semantics.
188 for (Int i=0; i<n; i++) v_p[i]=v.v_p[i];
189 }
190 // Assign from a RigidVector.
192 for (Int i=0; i<n; i++) v_p[i]=v.v_p[i];
193 return *this;
194 }
195 // Assign from a Vector.
197 for (Int i=0; i<n; i++) v_p[i]=v(i);
198 return *this;
199 }
200 // Assign a scalar, sets all elements to c.
202 for (Int i=0; i<n; i++) v_p[i]=c;
203 return *this;
204 }
205 // Negation
207 for (Int i=0; i<n ;i++) v_p[i]=-v_p[i];
208 return *this;
209 }
210 // Addition
212 for (Int i=0; i<n; i++) v_p[i]+=v.v_p[i];
213 return *this;
214 }
216 for (Int i=0; i<n; i++) v_p[i]*=v.v_p[i];
217 return *this;
218 }
219 // Subtraction
221 for (Int i=0; i<n; i++) v_p[i]-=v.v_p[i];
222 return *this;
223 }
224 // Multiplication by scalar.
226 for (Int i=0; i<n; i++) v_p[i]*=val;
227 return *this;
228 }
229 // Multiply vector by matrix: v*=M is equivalent to v=M*v;
231
232 // Indexing by reference
233 T& operator()(Int i) { return v_p[i];}
234 // Indexing by const reference
235 const T& operator()(Int i) const { return v_p[i];}
236 //# Get const access to the underlying c-array
237 //#const T*& cArray() const { return v_p;}
238 // Convert to a regular Vector
240 Vector<T> v(n);
241 for (Int i=0; i<n; i++) v(i)=v_p[i];
242 return v;
243 }
244 // Square Root
246
247// // The following are needed for Image<RigidVector>
248
249// static IPosition shape() {return IPosition(1,n);}
250
251// static void* newCopyInfo (const TableRecord& record,
252// const IPosition& sourceElementShape);
253
254// static void deleteCopyInfo (void*);
255
256// static void set (void* copyInfo, void* out,
257// const Array<T>& in,
258// const IPosition& shape);
259// static void get (void* copyInfo, Array<T>& out,
260// const void* in,
261// const IPosition& shape);
262
263protected:
264 T v_p[n];
265};
266
267// <summary> Mathematical operations involving RigidVectors </summary>
268
269// <group name=math>
270//#Fails to compile
271//#// Multiply vector by matrix.
272//#template <class T, Int n>
273//#inline RigidVector<T,n> operator*(const SquareMatrix<T,n>& m,
274//# const RigidVector<T,n>& v) {
275//# RigidVector<T,n> result(v);
276//# return result*=m;
277//#}
278// Multiply vector by matrix.
280 const RigidVector<Float,4>& v) {
281 RigidVector<Float,4> result(v);
282 return result*=m;
283}
284// Multiply vector by matrix.
286 const RigidVector<Complex,4>& v) {
287 RigidVector<Complex,4> result(v);
288 return result*=m;
289}
290// </group>
291
292} //# NAMESPACE CASACORE - END
293
294#ifndef CASACORE_NO_AUTO_TEMPLATES
295#include <casacore/scimath/Mathematics/RigidVector.tcc>
296#endif //# CASACORE_NO_AUTO_TEMPLATES
297#endif
RigidVector(const RigidVector< T, n > &v)
Copy constructor, copy semantics.
RigidVector< T, n > & operator-=(const RigidVector< T, n > &v)
Subtraction.
RigidVector(const T v[n])
Construct from a c-array (copy semantics)
RigidVector(const T &v0, const T &v1)
Construct a 2-element vector, fails for wrong size vectors.
RigidVector(const Vector< T > &v)
Construct from a Vector.
RigidVector< T, n > & operator=(const T &c)
Assign a scalar, sets all elements to c.
Vector< T > vector() const
Convert to a regular Vector.
RigidVector(const T &v0, const T &v1, const T &v2, const T &v3)
Construct a 4-element vector, fails for wrong size vectors.
friend ostream & operator<<(ostream &os, const RigidVector< T, n > &v)
Write out a RigidVector using the Vector output method.
RigidVector< T, n > & operator-()
Negation.
RigidVector< T, n > & operator*=(const T &val)
Multiplication by scalar.
RigidVector()
RigidVector(Int dummy) { for (Int i=0; i<n; i++) v_p[i]=T(0); } Default constructor.
RigidVector< T, n > & operator=(const RigidVector< T, n > &v)
Assign from a RigidVector.
friend RigidVector< T, n > operator-(const RigidVector< T, n > &l, const RigidVector< T, n > &r)
Subtract two RigidVectors.
RigidVector< T, n > sqrt(const RigidVector< T, n > &v)
Square Root.
friend RigidVector< Complex, 4 > operator*(const SquareMatrix< Complex, 4 > &m, const RigidVector< Float, 4 > &v)
Special matrix multiply of Complex matrix * Float vector.
friend RigidVector< T, n > operator*(const T &f, const RigidVector< T, n > &v)
Multiply a RigidVector by a scalar.
friend RigidVector< T, n > operator*(const RigidVector< T, n > &v, const T &f)
Multiply a RigidVector by a scalar.
RigidVector< T, n > & operator+=(const RigidVector< T, n > &v)
Addition
T & operator()(Int i)
Indexing by reference.
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.
const T & operator()(Int i) const
Indexing by const reference.
RigidVector(const T &c)
Construct from scalar, sets all elements to c.
RigidVector(const T &v0, const T &v1, const T &v2)
Construct a 3-element vector, fails for wrong size vectors.
RigidVector< T, n > & operator=(const Vector< T > &v)
Assign from a Vector.
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.
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 > &l, const RigidVector< T, n > &r)
Add two RigidVectors.
friend T operator*(const RigidVector< T, n > &l, const RigidVector< T, n > &r)
The innerproduct of 2 RigidVectors.
RigidVector< T, n > & operator*=(const RigidVector< T, n > &v)
T v_p[n]
// The following are needed for Image<RigidVector>
this file contains all the compiler specific defines
Definition mainpage.dox:28
LatticeExprNode sum(const LatticeExprNode &expr)
int Int
Definition aipstype.h:48
Mathematical operations involving RigidVectors
RigidVector< Complex, 4 > operator*(const SquareMatrix< Complex, 4 > &m, const RigidVector< Complex, 4 > &v)
Multiply vector by matrix.
RigidVector< Float, 4 > operator*(const SquareMatrix< Float, 4 > &m, const RigidVector< Float, 4 > &v)
Multiply vector by matrix.