X-Git-Url: http://git.tdb.fi/?p=libs%2Fmath.git;a=blobdiff_plain;f=source%2Flinal%2Fmatrix.h;h=2ad03447a9b10454f73ab982cf49f26dd25f983f;hp=bf21184092046c71bc1973ae1575deff51406fb7;hb=60caa19c84877c89d750140a90e5d891bce30ee7;hpb=ff8619c1c440fd1a7c8deaca763a33b8e1d53b5e diff --git a/source/linal/matrix.h b/source/linal/matrix.h index bf21184..2ad0344 100644 --- a/source/linal/matrix.h +++ b/source/linal/matrix.h @@ -3,6 +3,7 @@ #include #include +#include "matrixops.h" #include "vector.h" namespace Msp { @@ -26,6 +27,7 @@ public: template Matrix(const Matrix &); + static Matrix identity(); static Matrix from_columns(const Vector *); static Matrix from_rows(const Vector *); @@ -47,10 +49,13 @@ public: Matrix block(unsigned, unsigned) const; Matrix &operator*=(T); + Matrix &operator*=(const Matrix &); Matrix &operator/=(T); Matrix &operator+=(const Matrix &); Matrix &operator-=(const Matrix &); + Matrix &invert(); + Matrix &exchange_columns(unsigned, unsigned); Matrix &multiply_column(unsigned, T); Matrix &add_column(unsigned, unsigned, T); @@ -77,6 +82,16 @@ inline Matrix::Matrix(const Matrix &other) element(i, j) = other(i, j); } +template +inline Matrix Matrix::identity() +{ + static_assert(M==N, "An identity matrix must be square"); + Matrix m; + for(unsigned i=0; i inline Matrix Matrix::from_columns(const Vector *v) { @@ -127,6 +142,13 @@ inline Matrix &Matrix::operator*=(T s) return *this; } +template +inline Matrix &Matrix::operator*=(const Matrix &m) +{ + static_assert(M==N, "Multiplication-assignment is only possible on square matrices"); + return *this = *this*m; +} + template inline Matrix operator*(const Matrix &m, T s) { @@ -216,6 +238,23 @@ inline Matrix operator-(const Matrix &m1, const Matrix +inline Matrix& Matrix::invert() +{ + static_assert(M==N, "Inversion is only possible on square matrices"); + Matrix r = identity(); + gauss_jordan(*this, r); + return *this = r; +} + +template +inline Matrix invert(const Matrix &m) +{ + Matrix temp = m; + Matrix r = Matrix::identity(); + return gauss_jordan(temp, r); +} + template inline bool operator==(const Matrix &a, const Matrix &b) {