X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flinal%2Fmatrix.h;h=2ad03447a9b10454f73ab982cf49f26dd25f983f;hb=60caa19c84877c89d750140a90e5d891bce30ee7;hp=2711d40a637489c1bfa3cc9676c2fb8043f978d4;hpb=9ab061034720c1c0db4e8bff5dae4191ccd2a26f;p=libs%2Fmath.git diff --git a/source/linal/matrix.h b/source/linal/matrix.h index 2711d40..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,13 +49,16 @@ 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 &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 @@ -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) { @@ -227,27 +266,27 @@ inline bool operator==(const Matrix &a, const Matrix &b) } template -inline Matrix &Matrix::exchange_rows(unsigned i, unsigned j) +inline Matrix &Matrix::exchange_columns(unsigned i, unsigned j) { using std::swap; - for(unsigned k=0; k -inline Matrix &Matrix::multiply_row(unsigned i, T s) +inline Matrix &Matrix::multiply_column(unsigned i, T s) { - for(unsigned k=0; k -inline Matrix &Matrix::add_row(unsigned i, unsigned j, T s) +inline Matrix &Matrix::add_column(unsigned i, unsigned j, T s) { - for(unsigned k=0; k