]> git.tdb.fi Git - libs/math.git/blob - source/linal/squarematrix.h
0a97e61172b6489c58e91296421e946cfddfb9d8
[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         return invert_matrix(*this, r);
51 }
52
53 template<typename T, unsigned S>
54 inline SquareMatrix<T, S> invert(const SquareMatrix<T, S> &m)
55 {
56         SquareMatrix<T, S> r = m;
57         return r.invert();
58 }
59
60 } // namespace LinAl
61 } // namespace Msp
62
63 #endif