X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flinal%2Fsquarematrix.h;h=5117bb3fa189ee4339d0ce52cdbb4d2ff141b83b;hb=e4b75401bd773201deb00eff672ee34794479671;hp=4cdc9e88f7288654d4dcbae9d2ec006517bfb734;hpb=a3d9772a9fd483278b6248a811dc6f4ce968892b;p=libs%2Fmath.git diff --git a/source/linal/squarematrix.h b/source/linal/squarematrix.h index 4cdc9e8..5117bb3 100644 --- a/source/linal/squarematrix.h +++ b/source/linal/squarematrix.h @@ -1,6 +1,7 @@ #ifndef MSP_LINAL_SQUAREMATRIX_H_ #define MSP_LINAL_SQUAREMATRIX_H_ +#include #include #include "matrix.h" @@ -14,14 +15,18 @@ public: virtual ~not_invertible() throw() { } }; +/** +A mathematical matrix with S rows and columns. Some operations are provided +here that are only possible for square matrices. +*/ template class SquareMatrix: public Matrix { public: SquareMatrix() { } - SquareMatrix(const T *); + SquareMatrix(const T *d): Matrix(d) { } template - SquareMatrix(const Matrix &); + SquareMatrix(const Matrix &m): Matrix(m) { } static SquareMatrix identity(); @@ -30,17 +35,6 @@ public: SquareMatrix &invert(); }; -template -SquareMatrix::SquareMatrix(const T *d): - Matrix(d) -{ } - -template -template -SquareMatrix::SquareMatrix(const Matrix &m): - Matrix(m) -{ } - template inline SquareMatrix SquareMatrix::identity() { @@ -53,26 +47,27 @@ inline SquareMatrix SquareMatrix::identity() template SquareMatrix &SquareMatrix::operator*=(const SquareMatrix &m) { - Matrix::operator*=(m); - return *this; + return *this = *this*m; } template SquareMatrix &SquareMatrix::invert() { + using std::abs; + SquareMatrix r = identity(); for(unsigned i=0; ielement(i, i)==T(0)) - { - unsigned pivot = i; - for(unsigned j=i+1; jelement(j, i))>abs(this->element(pivot, i))) - pivot = j; + unsigned pivot = i; + for(unsigned j=i+1; jelement(j, i))>abs(this->element(pivot, i))) + pivot = j; - if(pivot==i) - throw not_invertible(); + if(this->element(pivot, i)==T(0)) + throw not_invertible(); + if(pivot!=i) + { this->exchange_rows(i, pivot); r.exchange_rows(i, pivot); }