]> git.tdb.fi Git - libs/math.git/blobdiff - source/linal/matrix.h
Remove SquareMatrix and instead use static_assert to check squareness
[libs/math.git] / source / linal / matrix.h
index 929c48247e517aa47d837df1438c94465e25064a..2ad03447a9b10454f73ab982cf49f26dd25f983f 100644 (file)
@@ -2,6 +2,8 @@
 #define MSP_LINAL_MATRIX_H_
 
 #include <algorithm>
+#include <ostream>
+#include "matrixops.h"
 #include "vector.h"
 
 namespace Msp {
@@ -13,6 +15,9 @@ A general mathematical matrix with M rows and N columns.
 template<typename T, unsigned M, unsigned N>
 class Matrix
 {
+public:
+       typedef T ElementType;
+
 private:
        T data[M*N];
 
@@ -22,25 +27,38 @@ public:
        template<typename U>
        Matrix(const Matrix<U, M, N> &);
 
+       static Matrix identity();
        static Matrix from_columns(const Vector<T, M> *);
        static Matrix from_rows(const Vector<T, N> *);
 
+       unsigned rows() const { return M; }
+       unsigned columns() const { return N; }
+
        T &element(unsigned i, unsigned j) { return data[i+M*j]; }
        const T &element(unsigned i, unsigned j) const { return data[i+M*j]; }
        T &operator()(unsigned i, unsigned j) { return element(i, j); }
        const T &operator()(unsigned i, unsigned j) const { return element(i, j); }
 
+       Vector<T, M> column(unsigned i) const { return Vector<T, M>(data+M*i); }
+       Vector<T, N> row(unsigned i) const { return Vector<T, N>(data+i, M); }
+
+       template<unsigned P, unsigned Q>
+       Matrix<T, P, Q> select(const Vector<unsigned, P> &, const Vector<unsigned, Q> &) const;
+
        template<unsigned P, unsigned Q>
        Matrix<T, P, Q> block(unsigned, unsigned) const;
 
        Matrix &operator*=(T);
+       Matrix &operator*=(const Matrix &);
        Matrix &operator/=(T);
        Matrix &operator+=(const Matrix &);
        Matrix &operator-=(const Matrix &);
 
-       Matrix &exchange_rows(unsigned, unsigned);
-       Matrix &multiply_row(unsigned, T);
-       Matrix &add_row(unsigned, unsigned, T);
+       Matrix &invert();
+
+       Matrix &exchange_columns(unsigned, unsigned);
+       Matrix &multiply_column(unsigned, T);
+       Matrix &add_column(unsigned, unsigned, T);
 };
 
 template<typename T, unsigned M, unsigned N>
@@ -64,6 +82,16 @@ inline Matrix<T, M, N>::Matrix(const Matrix<U, M, N> &other)
                        element(i, j) = other(i, j);
 }
 
+template<typename T, unsigned M, unsigned N>
+inline Matrix<T, M, N> Matrix<T, M, N>::identity()
+{
+       static_assert(M==N, "An identity matrix must be square");
+       Matrix<T, M, N> m;
+       for(unsigned i=0; i<M; ++i)
+               m(i, i) = T(1);
+       return m;
+}
+
 template<typename T, unsigned M, unsigned N>
 inline Matrix<T, M, N> Matrix<T, M, N>::from_columns(const Vector<T, M> *v)
 {
@@ -84,6 +112,17 @@ inline Matrix<T, M, N> Matrix<T, M, N>::from_rows(const Vector<T, N> *v)
        return m;
 }
 
+template<typename T, unsigned M, unsigned N>
+template<unsigned P, unsigned Q>
+inline Matrix<T, P, Q> Matrix<T, M, N>::select(const Vector<unsigned, P> &row_indices, const Vector<unsigned, Q> &col_indices) const
+{
+       Matrix<T, P, Q> r;
+       for(unsigned j=0; j<P; ++j)
+               for(unsigned i=0; i<Q; ++i)
+                       r(j, i) = element(row_indices[j], col_indices[i]);
+       return r;
+}
+
 template<typename T, unsigned M, unsigned N>
 template<unsigned P, unsigned Q>
 inline Matrix<T, P, Q> Matrix<T, M, N>::block(unsigned y, unsigned x) const
@@ -103,6 +142,13 @@ inline Matrix<T, M, N> &Matrix<T, M, N>::operator*=(T s)
        return *this;
 }
 
+template<typename T, unsigned M, unsigned N>
+inline Matrix<T, M, N> &Matrix<T, M, N>::operator*=(const Matrix<T, M, N> &m)
+{
+       static_assert(M==N, "Multiplication-assignment is only possible on square matrices");
+       return *this = *this*m;
+}
+
 template<typename T, unsigned M, unsigned N>
 inline Matrix<T, M, N> operator*(const Matrix<T, M, N> &m, T s)
 {
@@ -192,6 +238,23 @@ inline Matrix<T, M, N> operator-(const Matrix<T, M, N> &m1, const Matrix<T, M, N
        return r -= m2;
 }
 
+template<typename T, unsigned M, unsigned N>
+inline Matrix<T, M, N>& Matrix<T, M, N>::invert()
+{
+       static_assert(M==N, "Inversion is only possible on square matrices");
+       Matrix<T, M, N> r = identity();
+       gauss_jordan(*this, r);
+       return *this = r;
+}
+
+template<typename T, unsigned S>
+inline Matrix<T, S, S> invert(const Matrix<T, S, S> &m)
+{
+       Matrix<T, S, S> temp = m;
+       Matrix<T, S, S> r = Matrix<T, S, S>::identity();
+       return gauss_jordan(temp, r);
+}
+
 template<typename T, unsigned M, unsigned N>
 inline bool operator==(const Matrix<T, M, N> &a, const Matrix<T, M, N> &b)
 {
@@ -203,27 +266,27 @@ inline bool operator==(const Matrix<T, M, N> &a, const Matrix<T, M, N> &b)
 }
 
 template<typename T, unsigned M, unsigned N>
-inline Matrix<T, M, N> &Matrix<T, M, N>::exchange_rows(unsigned i, unsigned j)
+inline Matrix<T, M, N> &Matrix<T, M, N>::exchange_columns(unsigned i, unsigned j)
 {
        using std::swap;
-       for(unsigned k=0; k<N; ++k)
-               swap(element(i, k), element(j, k));
+       for(unsigned k=0; k<M; ++k)
+               swap(element(k, i), element(k, j));
        return *this;
 }
 
 template<typename T, unsigned M, unsigned N>
-inline Matrix<T, M, N> &Matrix<T, M, N>::multiply_row(unsigned i, T s)
+inline Matrix<T, M, N> &Matrix<T, M, N>::multiply_column(unsigned i, T s)
 {
-       for(unsigned k=0; k<N; ++k)
-               element(i, k) *= s;
+       for(unsigned k=0; k<M; ++k)
+               element(k, i) *= s;
        return *this;
 }
 
 template<typename T, unsigned M, unsigned N>
-inline Matrix<T, M, N> &Matrix<T, M, N>::add_row(unsigned i, unsigned j, T s)
+inline Matrix<T, M, N> &Matrix<T, M, N>::add_column(unsigned i, unsigned j, T s)
 {
-       for(unsigned k=0; k<N; ++k)
-               element(j, k) += element(i, k)*s;
+       for(unsigned k=0; k<M; ++k)
+               element(k, j) += element(k, i)*s;
        return *this;
 }
 
@@ -237,6 +300,27 @@ inline Matrix<T, N, M> transpose(const Matrix<T, M, N> &m)
        return r;
 }
 
+template<typename T, unsigned M, unsigned N>
+inline std::ostream &operator<<(std::ostream &s, const Matrix<T, M, N> &m)
+{
+       s << "Matrix" << M << 'x' << N << '(';
+       for(unsigned i=0; i<N; ++i)
+       {
+               if(i)
+                       s << ", ";
+               s << '[';
+               for(unsigned j=0; j<M; ++j)
+               {
+                       if(j)
+                               s << ", ";
+                       s << m(j, i);
+               }
+               s << ']';
+       }
+       s << ')';
+       return s;
+}
+
 } // namespace LinAl
 } // namespace Msp