From 6b974b64e447610f8b5340817ffd4a3513d4f3d1 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Wed, 14 Jun 2017 11:02:54 +0100 Subject: Improving the matrix class --- include/YAGE/Math/matrix.hpp | 73 +++++++++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 18 deletions(-) (limited to 'include') diff --git a/include/YAGE/Math/matrix.hpp b/include/YAGE/Math/matrix.hpp index 5a9e80f2..47bb6d60 100644 --- a/include/YAGE/Math/matrix.hpp +++ b/include/YAGE/Math/matrix.hpp @@ -49,7 +49,7 @@ protected: public: Matrix() : data_(Rows*Cols) {} - + int rowSize() const { return Rows; @@ -80,6 +80,16 @@ public: return colMatrix; } + typename std::vector::iterator begin() + { + return data_.begin(); + } + + typename std::vector::iterator end() + { + return data_.end(); + } + virtual std::string toString() const { std::stringstream ss; @@ -134,13 +144,6 @@ public: return *this; } - friend Matrix operator+(Matrix lhs, - const Matrix &rhs) - { - lhs+=rhs; - return lhs; - } - Matrix& operator-=(const Matrix &rhs) { std::vector out; @@ -153,18 +156,48 @@ public: return *this; } - friend Matrix operator-(Matrix lhs, - const Matrix &rhs) + +}; + +template +Matrix operator+(Matrix lhs, const Matrix &rhs) +{ + lhs+=rhs; + return lhs; +} + +template +Matrix operator-(Matrix lhs, const Matrix &rhs) +{ + lhs-=rhs; + return lhs; +} + +template +Matrix operator+(Matrix lhs, const T &rhs) +{ + for(auto &data : lhs) { - lhs-=rhs; - return lhs; + data+=rhs; } + return lhs; +} - friend std::ostream& operator<<(std::ostream &os, const Matrix &mat) +template +Matrix operator+(const T &lhs, Matrix rhs) +{ + for(auto &data : rhs) { - return os< +std::ostream& operator<<(std::ostream &os, const Matrix &mat) +{ + return os< class Vector : public Matrix { @@ -231,7 +264,12 @@ template Matrix transpose(const Matrix template T dot(const Vector &v1, const Vector &v2) { - + T sum=0; + for(int i=0; i @@ -248,8 +286,7 @@ Matrix multiply(const Matrix &m1, const Matrix &m2) { for(int j=0; j