]> git.tdb.fi Git - libs/math.git/blob - source/linal/squarematrix.h
Support Matrix multiplication operators on SquareMatrix
[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         using Matrix<T, S, S>::operator*=;
28
29         SquareMatrix &invert();
30 };
31
32 template<typename T, unsigned S>
33 inline SquareMatrix<T, S> SquareMatrix<T, S>::identity()
34 {
35         SquareMatrix<T, S> m;
36         for(unsigned i=0; i<S; ++i)
37                 m(i, i) = T(1);
38         return m;
39 }
40
41 template<typename T, unsigned S>
42 SquareMatrix<T, S> &SquareMatrix<T, S>::operator*=(const SquareMatrix<T, S> &m)
43 {
44         return *this = *this*m;
45 }
46
47 template<typename T, unsigned S>
48 SquareMatrix<T, S> &SquareMatrix<T, S>::invert()
49 {
50         SquareMatrix<T, S> r = identity();
51         gauss_jordan(*this, r);
52         return *this = r;
53 }
54
55 template<typename T, unsigned S>
56 inline SquareMatrix<T, S> invert(const SquareMatrix<T, S> &m)
57 {
58         SquareMatrix<T, S> temp = m;
59         SquareMatrix<T, S> r = SquareMatrix<T, S>::identity();
60         return gauss_jordan(temp, r);
61 }
62
63 } // namespace LinAl
64 } // namespace Msp
65
66 #endif