casacore
Loading...
Searching...
No Matches
SquareMatrix.h
Go to the documentation of this file.
1// # SquareMatrix.h: Fast Square Matrix class with fixed (templated) size
2// # Copyright (C) 1996,1997,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_SQUAREMATRIX_H
27#define SCIMATH_SQUAREMATRIX_H
28
29#include <casacore/casa/aips.h>
30#include <casacore/casa/BasicSL/Complex.h>
31#include <casacore/casa/Arrays/Matrix.h>
32#include <casacore/casa/iosfwd.h>
33
34namespace casacore { // # NAMESPACE CASACORE - BEGIN
35
36// # forward declarations
37template <class T, Int n>
38class RigidVector;
39
40// <summary>
41// Fast Square Matrix class with fixed (templated) size
42// </summary>
43// <use visibility=export>
44// <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
45// </reviewed>
46// <prerequisite>
47// <li> Complex
48// <li> Matrix
49// </prerequisite>
50//
51// <etymology>
52// SquareMatrix is a specialized class for small (<5x5) square matrices.
53// </etymology>
54//
55// <synopsis>
56// SquareMatrix provides operations similar to the Matrix class, but it is
57// much faster for small arrays. One important difference is that operators *=
58// and * do matrix products for SquareMatrices instead of element by element
59// multiplication. SquareMatrices also optimize operations internally for
60// scalar identity matrices (diagonal matrix with all elements equal) and
61// diagonal matrices. The different types of SquareMatrix are created by
62// constructors and operator= taking either a scalar, a vector or a full
63// matrix.
64// </synopsis>
65//
66// <example>
67// <srcblock>
68// // create two SquareMatrices
69// SquareMatrix<Float,2> sm1(3.0); // a scalar identity matrix
70// Vector<Float> vec(2); vec(0)=2.0; vec(1)=3.0;
71// SquareMatrix<Float,2> sm2(vec); // a diagonal matrix
72// // multiply the matrices
73// // Note: A*=B is equivalent to A=A*B where '*' is matrix multiplication
74// sm1*=sm2; // sm1 now diagonal
75// </srcblock>
76// </example>
77//
78// <motivation>
79// The basic Matrix classes are rather inefficient for small sizes,
80// new and delete tend to dominate the execution time for computationally
81// intensive code. The SquareMatrix classes circumvent this by having a
82// compile-time fixed size c-array internally. The SquareMatrix class have
83// fixed zero origin and no increments, this allows fast indexing,
84// copying and math operations. As mentioned in the synopsis, the SquareMatrix
85// classes also avoid unnecessary operations for simple matrices
86// (scalar-identity and diagonal).
87// </motivation>
88//
89// <templating arg=T>
90// <li> real() is called for T=Complex/DComplex
91// </templating>
92//
93// <thrown>
94// <li> No exceptions
95// </thrown>
96//
97// <todo asof="1996/11/06">
98// <li> when the Sun native compiler improves, some explicit instantiations
99// can be replaced by their templated equivalent and two constructor
100// calls with for loops can be moved out of line.
101// <li> not all operators and math functions available for Matrix are
102// implemented yet, add on as-needed basis.
103// </todo>
104
105template <class T, Int n>
107 // Friends currently need to be explicit (non templated) type to work.
108 friend class RigidVector<T, n>;
109 // # friend class SquareMatrix<Complex,n>; // for real()
110 // friend class SquareMatrix<T,n*n>;// Sun native does not accept this
111 // friend class SquareMatrix<Complex,4>; // for directProduct of 2x2
112 // Global friend function for product of Complex matrix and Float 4-vector
114 const RigidVector<Float, 4>& v);
115 // Global friend function to calculate direct product
117 const SquareMatrix<Complex, 2>& left,
118 const SquareMatrix<Complex, 2>& right);
119
120 public:
121 // Enum used internally to optimize operations.
123 // Destructor
125 // Default constructor - creates a unity matrix at present, this may not
126 // be what we want (non-intuitive?)
127 SquareMatrix() : type_p(ScalarId) { a_p[0][0] = T(1); }
128 // Create a matrix of a given type, no initialization
129 SquareMatrix(int itype) : type_p(itype) {}
130 // Copy construct a SquareMatrix, a true copy is made.
132 // Construct from c-style matrix (by copying elements).
133 SquareMatrix(const T a[n][n]) { operator=(a); }
134 // Construct from Matrix.
135 SquareMatrix(const Matrix<T>& mat) { operator=(mat); }
136 // Construct from c-style vector, creates a diagonal matrix.
137 SquareMatrix(const T vec[n]) { operator=(vec); }
138 // Construct from Vector, creates a diagonal matrix.
139 SquareMatrix(const Vector<T>& vec) { operator=(vec); }
140 // Construct from scalar, creates a scalar-identity matrix
141 SquareMatrix(const T& scalar) : type_p(ScalarId) { a_p[0][0] = scalar; }
142 // Assignment, uses copy semantics.
144 // Assign a c-style matrix, creates a general matrix.
145 SquareMatrix<T, n>& operator=(const T a[n][n]) {
146 type_p = General;
147 const T* pa = &a[0][0];
148 T* pa_p = &a_p[0][0];
149 for (Int i = 0; i < n * n; i++) *pa_p++ = *pa++;
150 return *this;
151 }
152 // Assign a Matrix, creates a general matrix.
154 // Assign a c-style vector, creates a diagonal matrix
155 SquareMatrix<T, n>& operator=(const T vec[n]) {
157 for (Int i = 0; i < n; i++) a_p[i][i] = vec[i];
158 return *this;
159 }
160 // Assign a Vector, creates a diagonal matrix
162 // Assign a scalar, creates a scalar-identity matrix
165 a_p[0][0] = val;
166 return *this;
167 }
168 // Add two SquareMatrices, element by element.
170 // Matrix product of 'this' SquareMatrix with other,
171 // i.e., A*=B; is equivalent with A=A*B where '*' is matrix multiplication.
173 // Scalar multiplication
175 // Indexing, only const indexing is allowed. You cannot change the
176 // matrix via indexing. No bounds checking.
177 T operator()(Int i, Int j) const {
178 switch (type_p) {
179 case ScalarId:
180 return (i == j) ? a_p[0][0] : T();
181 break;
182 case Diagonal:
183 return (i == j) ? a_p[i][i] : T();
184 break;
185 }
186 return a_p[i][j];
187 }
188 // Non const indexing, throws exception if you try to change an element
189 // which would require a type change of the matrix
190 T& operator()(Int i, Int j) {
191 switch (type_p) {
192 case ScalarId:
193 return (i == j) ? a_p[0][0] : throwInvAccess();
194 break;
195 case Diagonal:
196 return (i == j) ? a_p[i][i] : throwInvAccess();
197 break;
198 }
199 return a_p[i][j];
200 }
201
202 // # The following does not compile with Sun native, replaced by explicit
203 // # global function.
204 // # direct product : dp= this (directproduct) other
205 // # SquareMatrix<T,n*n>&
206 // # directProduct(SquareMatrix<T,n*n>& dp,
207 // # const SquareMatrix<T,n>& other) const;
208 // For a <src>SquareMatrix<Complex,n></src>:
209 // set the argument result to the real part of the matrix
210 // (and return result by reference to allow use in
211 // expressions without creating temporary).
212 // # SquareMatrix<Float,n>& real(SquareMatrix<Float,n>& result) const;
213 // For a <src>SquareMatrix<Complex,n></src>:
214 // return the real part of the matrix.
215 // # SquareMatrix<Float,n> real() const {
216 // # SquareMatrix<Float,n> result;
217 // # return real(result);
218 // # }
219 // Conjugate the matrix in place(!).
221 // Tranpose and conjugate the matrix in place(!).
223 // Conjugate the matrix, return it in result (and by ref)
225 // Tranpose and conjugate the matrix, return it in result (and by ref)
227 // Compute the inverse of the matrix and return it in result (also
228 // returns result by reference).
230 // Return the inverse of the matrix by value.
232 SquareMatrix<T, n> result;
233 return inverse(result);
234 }
235 // Assign 'this' to the Matrix result, also return result by reference.
236 Matrix<T>& matrix(Matrix<T>& result) const;
237 // Convert the SquareMatrix to a Matrix.
239 Matrix<T> result(n, n);
240 return matrix(result);
241 }
242
243 private:
245 T a_p[n][n];
247};
248
249// # the following does not compile with Sun native but should...
250// # expanded by hand for types and sizes needed
251// #template<class T, Int n>
252// #ostream& operator<<(ostream& os, const SquareMatrix<T,n>& m) {
253// # return os<<m.matrix();
254// #}
255// #
256// #template<class T, Int n> inline SquareMatrix<T,n> operator+(const SquareMatrix<T,n>& left,
257// # const SquareMatrix<T,n>& right) {
258// # SquareMatrix<T,n> result(left);
259// # return result+=right;
260// #}
261// #template<class T, Int n> inline SquareMatrix<T,n> operator*(const SquareMatrix<T,n>& left,
262// # const SquareMatrix<T,n>& right)
263// #{
264// # SquareMatrix<T,n> result(left);
265// # return result*=right;
266// #}
267// #
268// #template<class T, Int n> inline SquareMatrix<T,n*n> directProduct(const SquareMatrix<T,n>& left,
269// # const SquareMatrix<T,n>& right)
270// #{
271// # SquareMatrix<T,n*n> result;
272// # return left.directProduct(result,right);
273// #}
274// #
275// #template<class T, Int n> inline SquareMatrix<T,n*n>&
276// #directProduct(SquareMatrix<T,n*n>& result,
277// # const SquareMatrix<T,n>& left,
278// # const SquareMatrix<T,n>& right)
279// #{
280// # return left.directProduct(result,right);
281// #}
282// #template<class T, Int n> inline SquareMatrix<T,n> conj(
283// # const SquareMatrix<T,n>& m) {
284// # SquareMatrix<T,n> result(m);
285// # return result.conj();
286// #}
287// #
288// #template<class T, Int n> inline SquareMatrix<T,n> adjoint(
289// # const SquareMatrix<T,n>& m) {
290// # SquareMatrix<T,n> result(m);
291// # return result.adjoint();
292// #}
293// #
294
295// <summary>
296// Various global math and IO functions.
297// </summary>
298// <group name=SqM_global_functions>
300// Calculate direct product of two SquareMatrices.
302 const SquareMatrix<Complex, 2>& right);
303
304// Return conjugate of SquareMatrix.
306
307// Return conjugate of SquareMatrix.
309
310// Return adjoint of SquareMatrix.
312
313// Return adjoint of SquareMatrix.
315
316// Write SquareMatrix to output, uses Matrix to do the work.
317ostream& operator<<(ostream& os, const SquareMatrix<Complex, 2>& m);
318ostream& operator<<(ostream& os, const SquareMatrix<Complex, 4>& m);
319ostream& operator<<(ostream& os, const SquareMatrix<Float, 2>& m);
320ostream& operator<<(ostream& os, const SquareMatrix<Float, 4>& m);
321// </group>
322
323} // namespace casacore
324
325#ifndef CASACORE_NO_AUTO_TEMPLATES
326#include <casacore/scimath/Mathematics/SquareMatrix.tcc>
327#endif // # CASACORE_NO_AUTO_TEMPLATES
328#endif
T & operator()(Int i, Int j)
Non const indexing, throws exception if you try to change an element which would require a type chang...
Matrix< T > matrix() const
Convert the SquareMatrix to a Matrix.
SquareMatrix< T, n > & operator=(const Vector< T > &v)
Assign a Vector, creates a diagonal matrix.
SquareMatrix< T, n > & operator+=(const SquareMatrix< T, n > &other)
Add two SquareMatrices, element by element.
SquareMatrix< T, n > inverse() const
Return the inverse of the matrix by value.
friend SquareMatrix< Complex, 4 > & directProduct(SquareMatrix< Complex, 4 > &result, const SquareMatrix< Complex, 2 > &left, const SquareMatrix< Complex, 2 > &right)
Global friend function to calculate direct product.
SquareMatrix< T, n > & conj(SquareMatrix< T, n > &result)
Conjugate the matrix, return it in result (and by ref).
SquareMatrix< T, n > & operator=(T val)
Assign a scalar, creates a scalar-identity matrix.
SquareMatrix< T, n > & operator*=(const SquareMatrix< T, n > &other)
Matrix product of 'this' SquareMatrix with other, i.e., A*=B; is equivalent with A=A*B where '*' is m...
SquareMatrix< T, n > & adjoint(SquareMatrix< T, n > &result)
Tranpose and conjugate the matrix, return it in result (and by ref).
SquareMatrix(const Vector< T > &vec)
Construct from Vector, creates a diagonal matrix.
T operator()(Int i, Int j) const
Indexing, only const indexing is allowed.
SquareMatrix< T, n > & operator=(const T vec[n])
Assign a c-style vector, creates a diagonal matrix.
SquareMatrix< T, n > & operator=(const Matrix< T > &m)
Assign a Matrix, creates a general matrix.
friend RigidVector< Complex, 4 > operator*(const SquareMatrix< Complex, 4 > &m, const RigidVector< Float, 4 > &v)
friend class SquareMatrix<T,n*n>;// Sun native does not accept this friend class SquareMatrix<Complex...
SquareMatrix< T, n > & operator*=(Float f)
Scalar multiplication.
SquareMatrix< T, n > & adjoint()
Tranpose and conjugate the matrix in place(!).
Matrix< T > & matrix(Matrix< T > &result) const
Assign 'this' to the Matrix result, also return result by reference.
SquareMatrix(const T vec[n])
Construct from c-style vector, creates a diagonal matrix.
~SquareMatrix()
Destructor.
SquareMatrix< T, n > & conj()
For a SquareMatrix<Complex,n>: set the argument result to the real part of the matrix (and return res...
SquareMatrix(const SquareMatrix< T, n > &m)
Copy construct a SquareMatrix, a true copy is made.
SquareMatrix(const T a[n][n])
Construct from c-style matrix (by copying elements).
SquareMatrix(const Matrix< T > &mat)
Construct from Matrix.
SquareMatrix(const T &scalar)
Construct from scalar, creates a scalar-identity matrix.
SquareMatrix()
Default constructor - creates a unity matrix at present, this may not be what we want (non-intuitive?...
SquareMatrix(int itype)
Create a matrix of a given type, no initialization.
SquareMatrix< T, n > & inverse(SquareMatrix< T, n > &result) const
Compute the inverse of the matrix and return it in result (also returns result by reference).
SquareMatrix< T, n > & operator=(const T a[n][n])
Assign a c-style matrix, creates a general matrix.
SquareMatrix< T, n > & operator=(const SquareMatrix< T, n > &m)
Assignment, uses copy semantics.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
LatticeExprNode pa(const LatticeExprNode &left, const LatticeExprNode &right)
This function finds 180/pi*atan2(left,right)/2.
float Float
Definition aipstype.h:52
int Int
Definition aipstype.h:48
SquareMatrix< Complex, 2 > conj(const SquareMatrix< Complex, 2 > &m)
Return conjugate of SquareMatrix.
SquareMatrix< Complex, 4 > directProduct(const SquareMatrix< Complex, 2 > &left, const SquareMatrix< Complex, 2 > &right)
Calculate direct product of two SquareMatrices.
SquareMatrix< Complex, 2 > adjoint(const SquareMatrix< Complex, 2 > &m)
Return adjoint of SquareMatrix.
ostream & operator<<(ostream &os, const SquareMatrix< Complex, 4 > &m)
ostream & operator<<(ostream &os, const SquareMatrix< Complex, 2 > &m)
Write SquareMatrix to output, uses Matrix to do the work.
ostream & operator<<(ostream &os, const SquareMatrix< Float, 2 > &m)
SquareMatrix< Complex, 4 > conj(const SquareMatrix< Complex, 4 > &m)
Return conjugate of SquareMatrix.
ostream & operator<<(ostream &os, const SquareMatrix< Float, 4 > &m)
SquareMatrix< Complex, 4 > adjoint(const SquareMatrix< Complex, 4 > &m)
Return adjoint of SquareMatrix.