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