]> git.tdb.fi Git - libs/math.git/blob - source/linal/squarematrix.h
Rename the low-level matrix inversion function to gauss_jordan
[libs/math.git] / source / linal / squarematrix.h
1 #ifndef MSP_LINAL_SQUAREMATRIX_H_
2 #define MSP_LINAL_SQUAREMATRIX_H_
3
4 #include <cmath>
5 #include "matrix.h"
6 #include "matrixops.h"
7
8 namespace Msp {
9 namespace LinAl {
10
11 /**
12 A mathematical matrix with S rows and columns.  Some operations are provided
13 here that are only possible for square matrices.
14 */
15 template<typename T, unsigned S>
16 class SquareMatrix: public Matrix<T, S, S>
17 {
18 public:
19         SquareMatrix() { }
20         SquareMatrix(const T *d): Matrix<T, S, S>(d) { }
21         template<typename U>
22         SquareMatrix(const Matrix<U, S, S> &m): Matrix<T, S, S>(m) { }
23
24         static SquareMatrix identity();
25
26         SquareMatrix &operator*=(const SquareMatrix &);
27
28         SquareMatrix &invert();
29 };
30
31 template<typename T, unsigned S>
32 inline SquareMatrix<T, S> SquareMatrix<T, S>::identity()
33 {
34         SquareMatrix<T, S> m;
35         for(unsigned i=0; i<S; ++i)
36                 m(i, i) = T(1);
37         return m;
38 }
39
40 template<typename T, unsigned S>
41 SquareMatrix<T, S> &SquareMatrix<T, S>::operator*=(const SquareMatrix<T, S> &m)
42 {
43         return *this = *this*m;
44 }
45
46 template<typename T, unsigned S>
47 SquareMatrix<T, S> &SquareMatrix<T, S>::invert()
48 {
49         SquareMatrix<T, S> r = identity();
50         gauss_jordan(*this, r);
51         return *this = r;
52 }
53
54 template<typename T, unsigned S>
55 inline SquareMatrix<T, S> invert(const SquareMatrix<T, S> &m)
56 {
57         SquareMatrix<T, S> temp = m;
58         SquareMatrix<T, S> r = SquareMatrix<T, S>::identity();
59         return gauss_jordan(temp, r);
60 }
61
62 } // namespace LinAl
63 } // namespace Msp
64
65 #endif