From: Mikko Rasa Date: Sun, 13 Mar 2022 20:08:58 +0000 (+0200) Subject: Remove SquareMatrix and instead use static_assert to check squareness X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=60caa19c84877c89d750140a90e5d891bce30ee7;p=libs%2Fmath.git Remove SquareMatrix and instead use static_assert to check squareness --- diff --git a/source/geometry/affinetransformation.h b/source/geometry/affinetransformation.h index f01ac3a..e311b88 100644 --- a/source/geometry/affinetransformation.h +++ b/source/geometry/affinetransformation.h @@ -1,7 +1,7 @@ #ifndef MSP_GEOMETRY_AFFINETRANSFORMATION_H_ #define MSP_GEOMETRY_AFFINETRANSFORMATION_H_ -#include +#include #include "angle.h" #include "boundingbox.h" #include "ray.h" @@ -55,7 +55,7 @@ class AffineTransformation: public AffineTransformationOps friend class AffineTransformationOps; private: - LinAl::SquareMatrix matrix; + LinAl::Matrix matrix; public: AffineTransformation(); @@ -67,8 +67,8 @@ public: AffineTransformation &operator*=(const AffineTransformation &); AffineTransformation &invert(); - const LinAl::SquareMatrix &get_matrix() const { return matrix; } - operator const LinAl::SquareMatrix &() const { return matrix; } + const LinAl::Matrix &get_matrix() const { return matrix; } + operator const LinAl::Matrix &() const { return matrix; } LinAl::Vector transform(const LinAl::Vector &) const; LinAl::Vector transform_linear(const LinAl::Vector &) const; @@ -79,7 +79,7 @@ public: template inline AffineTransformation::AffineTransformation() { - this->matrix = LinAl::SquareMatrix::identity(); + this->matrix = LinAl::Matrix::identity(); } diff --git a/source/linal/dummy.cpp b/source/linal/dummy.cpp index 84d7395..74a8e44 100644 --- a/source/linal/dummy.cpp +++ b/source/linal/dummy.cpp @@ -1,6 +1,5 @@ #include "vector.h" #include "matrix.h" -#include "squarematrix.h" #include "dynamicvector.h" #include "dynamicmatrix.h" 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) { diff --git a/source/linal/squarematrix.h b/source/linal/squarematrix.h deleted file mode 100644 index 5b9415a..0000000 --- a/source/linal/squarematrix.h +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef MSP_LINAL_SQUAREMATRIX_H_ -#define MSP_LINAL_SQUAREMATRIX_H_ - -#include -#include "matrix.h" -#include "matrixops.h" - -namespace Msp { -namespace LinAl { - -/** -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 *d): Matrix(d) { } - template - SquareMatrix(const Matrix &m): Matrix(m) { } - - static SquareMatrix identity(); - - SquareMatrix &operator*=(const SquareMatrix &); - using Matrix::operator*=; - - SquareMatrix &invert(); -}; - -template -inline SquareMatrix SquareMatrix::identity() -{ - SquareMatrix m; - for(unsigned i=0; i -SquareMatrix &SquareMatrix::operator*=(const SquareMatrix &m) -{ - return *this = *this*m; -} - -template -SquareMatrix &SquareMatrix::invert() -{ - SquareMatrix r = identity(); - gauss_jordan(*this, r); - return *this = r; -} - -template -inline SquareMatrix invert(const SquareMatrix &m) -{ - SquareMatrix temp = m; - SquareMatrix r = SquareMatrix::identity(); - return gauss_jordan(temp, r); -} - -} // namespace LinAl -} // namespace Msp - -#endif