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