X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flinal%2Fmatrix.h;fp=source%2Flinal%2Fmatrix.h;h=00e960d9ce8111de4d88bd0bf664d470183f442c;hb=839a88cd2b9a2f054323627c4e76cdcefc652c63;hp=0000000000000000000000000000000000000000;hpb=4d9e2d03503ba3d90633d2acdaadf64869bf2ab4;p=libs%2Fmath.git diff --git a/source/linal/matrix.h b/source/linal/matrix.h new file mode 100644 index 0000000..00e960d --- /dev/null +++ b/source/linal/matrix.h @@ -0,0 +1,149 @@ +#ifndef MSP_LIEAL_MATMIX_H_ +#define MSP_LIEAL_MATMIX_H_ + +#include "vector.h" + +namespace Msp { +namespace LinAl { + +/** +A general mathematical matrix. +*/ +template +class Matrix +{ +private: + T data[M*N]; + +public: + Matrix(); + Matrix(const T *); + template + Matrix(const Matrix &); + static Matrix from_columns(const Vector *); + static Matrix from_rows(const Vector *); + + T &operator()(unsigned, unsigned); + const T &operator()(unsigned, unsigned) const; + + Matrix &operator*=(T); + Matrix &operator/=(T); + Matrix &operator+=(const Matrix &); + Matrix &operator-=(const Matrix &); + + Matrix transpose() const; +}; + +template +inline T &Matrix::operator()(unsigned i, unsigned j) +{ + return data[i+M*j]; +} + +template +inline const T &Matrix::operator()(unsigned i, unsigned j) const +{ + return data[i+M*j]; +} + +template +inline Matrix &Matrix::operator*=(T s) +{ + for(unsigned i=0; i +inline Matrix operator*(const Matrix &m, T s) +{ + Matrix r(m); + return r *= s; +} + +template +inline Matrix operator*(T s, const Matrix &m) +{ + return m*s; +} + +template +inline Matrix &Matrix::operator/=(T s) +{ + for(unsigned i=0; i +inline Matrix operator/(const Matrix &m, T s) +{ + Matrix r(m); + return r /= s; +} + +template +inline Matrix &Matrix::operator+=(const Matrix &m) +{ + for(unsigned i=0; i +inline Matrix operator+(const Matrix &m1, const Matrix &m2) +{ + Matrix r(m1); + return r += m2; +} + +template +inline Matrix &Matrix::operator-=(const Matrix &m) +{ + for(unsigned i=0; i +inline Matrix operator-(const Matrix &m1, const Matrix &m2) +{ + Matrix r(m1); + return r -= m2; +} + +template +Matrix operator*(const Matrix &m1, const Matrix &m2) +{ + Matrix r; + for(unsigned i=0; i +Vector operator*(const Matrix &m, const Vector &v) +{ + Vector r; + for(unsigned i=0; i +Vector operator*(const Vector &v, const Matrix &m) +{ + Vector r; + for(unsigned j=0; j