casacore
Loading...
Searching...
No Matches
MatrixMathLA.h
Go to the documentation of this file.
1// # MatrixMath.h: The Casacore linear algebra functions
2// # Copyright (C) 1994,1995,1996,1999,2000,2002
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_MATRIXMATHLA_H
27#define SCIMATH_MATRIXMATHLA_H
28
29#include <casacore/casa/aips.h>
30#include <casacore/casa/Arrays/Vector.h>
31#include <casacore/casa/Arrays/Matrix.h>
32#include <casacore/casa/BasicSL/Complex.h>
33
34namespace casacore { // # NAMESPACE CASACORE - BEGIN
35
36//<summary>
37// Linear algebra functions on Vectors and Matrices.
38// </summary>
39//
40// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tLinAlgebra">
41//
42// <linkfrom anchor="Linear Algebra" classes="Vector Matrix">
43// <here>Linear Algebra</here> -- Linear algebra functions
44// on Vectors and Matrices.
45// </linkfrom>
46//
47//<group name="Linear Algebra">
49// Routines which calculate the inverse of a matrix. The inverse is very
50// often the worst way to do a calculation. Nevertheless it is often
51// convenient. The present implementation uses LU decomposition implemented
52// by LAPACK. The determinate can be calculated "for free" as it is the
53// product of the diagonal terms after decomposition. If the input matrix is
54// singular, a matrix with no rows or columns is returned. <src>in</src>
55// must be a square matrix.
56// <note role="warning">This function will only work for complex types if
57// Complex and DComplex map onto their FORTRAN counterparts.</note>
58// # We could special case small matrices for efficiency.
59//<group>
60template <class T>
61void invert(Matrix<T> &out, T &determinate, const Matrix<T> &in);
62template <class T>
64template <class T>
66//</group>
67
68// This function inverts a symmetric positive definite matrix. It is
69// written in C++, so it should work with any data type for which
70// operators +, -, *, /, =, and sqrt are defined. The function uses
71// the Cholesky decomposition method to invert the matrix. Cholesky
72// decomposition is about a factor of 2 better than LU decomposition
73// where symmetry is ignored.
74template <class T>
76template <class T>
78
79//</group>
80
81// # These are actually used by invertSymPosDef. They will not
82// # normally be called by the end user.
83
84// # This function performs Cholesky decomposition.
85// # A is a positive-definite symmetric matrix. Only the upper triangle of
86// # A is needed on input. On output, the lower triangle of A contains the
87// # Cholesky factor L. The diagonal elements of L are returned in vector
88// # diag.
89template <class T>
91
92// # Solve linear equation A*x = b, where A positive-definite symmetric.
93// # On input, A contains Cholesky factor L in its low triangle except the
94// # diagonal elements which are in vector diag. On return x contains the
95// # solution. b and x can be the same vector to save memory space.
96template <class T>
98
99// # These are the LAPACK routines actually used by invert. They will not
100// # normally be called by the end user.
101
102#if !defined(NEED_FORTRAN_UNDERSCORES)
103#define NEED_FORTRAN_UNDERSCORES 1
104#endif
105
106#if NEED_FORTRAN_UNDERSCORES
107#define sgetrf sgetrf_
108#define dgetrf dgetrf_
109#define cgetrf cgetrf_
110#define zgetrf zgetrf_
111#define sgetri sgetri_
112#define dgetri dgetri_
113#define cgetri cgetri_
114#define zgetri zgetri_
115#define sposv sposv_
116#define dposv dposv_
117#define cposv cposv_
118#define zposv zposv_
119#define spotri spotri_
120#define dpotri dpotri_
121#define cpotri cpotri_
122#define zpotri zpotri_
123#endif
124
125extern "C" {
126void sgetrf(const int *m, const int *n, float *a, const int *lda, int *ipiv, int *info);
127void dgetrf(const int *m, const int *n, double *a, const int *lda, int *ipiv, int *info);
128void cgetrf(const int *m, const int *n, Complex *a, const int *lda, int *ipiv, int *info);
129void zgetrf(const int *m, const int *n, DComplex *a, const int *lda, int *ipiv, int *info);
130void sgetri(const int *m, float *a, const int *lda, const int *ipiv, float *work, const int *lwork,
131 int *info);
132void dgetri(const int *m, double *a, const int *lda, const int *ipiv, double *work,
133 const int *lwork, int *info);
134void cgetri(const int *m, Complex *a, const int *lda, const int *ipiv, Complex *work,
135 const int *lwork, int *info);
136void zgetri(const int *m, DComplex *a, const int *lda, const int *ipiv, DComplex *work,
137 const int *lwork, int *info);
138
139void sposv(const char *uplo, const int *n, const int *nrhs, float *a, const int *lda, float *b,
140 const int *ldb, int *info);
141void dposv(const char *uplo, const int *n, const int *nrhs, double *a, const int *lda, double *b,
142 const int *ldb, int *info);
143void cposv(const char *uplo, const int *n, const int *nrhs, Complex *a, const int *lda, Complex *b,
144 const int *ldb, int *info);
145void zposv(const char *uplo, const int *n, const int *nrhs, DComplex *a, const int *lda,
146 DComplex *b, const int *ldb, int *info);
147
148void spotri(const char *uplo, const int *n, float *a, const int *lda, int *info);
149void dpotri(const char *uplo, const int *n, double *a, const int *lda, int *info);
150void cpotri(const char *uplo, const int *n, Complex *a, const int *lda, int *info);
151void zpotri(const char *uplo, const int *n, DComplex *a, const int *lda, int *info);
152}
153
154// # Overloaded versions of the above to make templating work more easily
155inline void getrf(const int *m, const int *n, float *a, const int *lda, int *ipiv, int *info) {
156 sgetrf(m, n, a, lda, ipiv, info);
157}
158inline void getrf(const int *m, const int *n, double *a, const int *lda, int *ipiv, int *info) {
159 dgetrf(m, n, a, lda, ipiv, info);
160}
161inline void getrf(const int *m, const int *n, Complex *a, const int *lda, int *ipiv, int *info) {
162 cgetrf(m, n, a, lda, ipiv, info);
163}
164inline void getrf(const int *m, const int *n, DComplex *a, const int *lda, int *ipiv, int *info) {
165 zgetrf(m, n, a, lda, ipiv, info);
166}
167inline void getri(const int *m, float *a, const int *lda, const int *ipiv, float *work,
168 const int *lwork, int *info) {
169 sgetri(m, a, lda, ipiv, work, lwork, info);
170}
171inline void getri(const int *m, double *a, const int *lda, const int *ipiv, double *work,
172 const int *lwork, int *info) {
173 dgetri(m, a, lda, ipiv, work, lwork, info);
174}
175inline void getri(const int *m, Complex *a, const int *lda, const int *ipiv, Complex *work,
176 const int *lwork, int *info) {
177 cgetri(m, a, lda, ipiv, work, lwork, info);
178}
179inline void getri(const int *m, DComplex *a, const int *lda, const int *ipiv, DComplex *work,
180 const int *lwork, int *info) {
181 zgetri(m, a, lda, ipiv, work, lwork, info);
182}
183
184inline void posv(const char *uplo, const int *n, const int *nrhs, float *a, const int *lda,
185 float *b, const int *ldb, int *info) {
186 sposv(uplo, n, nrhs, a, lda, b, ldb, info);
187}
188inline void posv(const char *uplo, const int *n, const int *nrhs, double *a, const int *lda,
189 double *b, const int *ldb, int *info) {
190 dposv(uplo, n, nrhs, a, lda, b, ldb, info);
191}
192inline void posv(const char *uplo, const int *n, const int *nrhs, Complex *a, const int *lda,
193 Complex *b, const int *ldb, int *info) {
194 cposv(uplo, n, nrhs, a, lda, b, ldb, info);
195}
196inline void posv(const char *uplo, const int *n, const int *nrhs, DComplex *a, const int *lda,
197 DComplex *b, const int *ldb, int *info) {
198 zposv(uplo, n, nrhs, a, lda, b, ldb, info);
199}
200
201inline void potri(const char *uplo, const int *n, float *a, const int *lda, int *info) {
202 spotri(uplo, n, a, lda, info);
203}
204inline void potri(const char *uplo, const int *n, double *a, const int *lda, int *info) {
205 dpotri(uplo, n, a, lda, info);
206}
207inline void potri(const char *uplo, const int *n, Complex *a, const int *lda, int *info) {
208 cpotri(uplo, n, a, lda, info);
209}
210inline void potri(const char *uplo, const int *n, DComplex *a, const int *lda, int *info) {
211 zpotri(uplo, n, a, lda, info);
212}
213
214} // namespace casacore
215
216#ifndef CASACORE_NO_AUTO_TEMPLATES
217#include <casacore/scimath/Mathematics/MatrixMathLA.tcc>
218#endif // # CASACORE_NO_AUTO_TEMPLATES
219#endif
#define zpotri
#define sgetrf
#define zgetri
#define sgetri
#define dgetri
#define cposv
#define spotri
#define cgetrf
#define dposv
#define zgetrf
#define cpotri
#define sposv
#define dgetrf
#define zposv
#define cgetri
#define dpotri
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
void CholeskySolve(Matrix< T > &A, Vector< T > &diag, Vector< T > &b, Vector< T > &x)
void CholeskyDecomp(Matrix< T > &A, Vector< T > &diag)
void getri(const int *m, float *a, const int *lda, const int *ipiv, float *work, const int *lwork, int *info)
void potri(const char *uplo, const int *n, float *a, const int *lda, int *info)
void posv(const char *uplo, const int *n, const int *nrhs, float *a, const int *lda, float *b, const int *ldb, int *info)
void getrf(const int *m, const int *n, float *a, const int *lda, int *ipiv, int *info)
void invert(Matrix< T > &out, T &determinate, const Matrix< T > &in)
Routines which calculate the inverse of a matrix.
void invertSymPosDef(Matrix< T > &out, T &determinate, const Matrix< T > &in)
This function inverts a symmetric positive definite matrix.
Matrix< T > invertSymPosDef(const Matrix< T > &in)