From f26f97b06aa071009dcb11f618d5f9d03c201156 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Thu, 8 Jun 2017 10:12:20 +0100 Subject: Continuing the matrix class --- include/YAGE/Math/matrix.hpp | 156 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 149 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/include/YAGE/Math/matrix.hpp b/include/YAGE/Math/matrix.hpp index 2ada55eb..889db336 100644 --- a/include/YAGE/Math/matrix.hpp +++ b/include/YAGE/Math/matrix.hpp @@ -1,7 +1,9 @@ #ifndef YAGE_MATH_MATRIX_HPP #define YAGE_MATH_MATRIX_HPP -#include +#include +#include +#include #include namespace yage @@ -15,15 +17,20 @@ namespace detail template class Row { private: - std::shared_ptr> parent_; + Matrix *parent_; int index_; public: - Row(std::shared_ptr> parent, int index) : + Row(Matrix *parent, int index) : parent_(parent), index_(index) {} - Type &operator[](int col) + Type& operator[](int col) + { + return parent_->data_[index_*Cols+col]; + } + + const Type& operator[](int col) const { return parent_->data_[index_*Cols+col]; } @@ -34,19 +41,154 @@ public: template class Matrix { friend class detail::Row; -private: +protected: std::vector data_; public: Matrix() : data_(Rows*Cols) {} - Matrix(int rows, int cols) : data_(rows*cols) {} detail::Row operator[](int row) { - return detail::Row(std::make_shared>(*this), row); + return detail::Row(this, row); + } + + Matrix& operator=(const Matrix &other) + { + if(this!=&other) + { + data_=other.data_; + } + return *this; + } + + Matrix& operator+=(const Matrix &rhs) + { + std::vector out; + out.reserve(data_.size()); + std::transform(data_.begin(), data_.end(), rhs.data_.begin(), std::back_inserter(out), + [] (Type a, Type b) { + return a+b; + }); + data_=std::move(out); + return *this; + } + + friend Matrix operator+(Matrix lhs, + const Matrix &rhs) + { + lhs+=rhs; + return lhs; + } + + Matrix& operator-=(const Matrix &rhs) + { + std::vector out; + out.reserve(data_.size()); + std::transform(data_.begin(), data_.end(), rhs.begin(), std::back_inserter(out), + [] (Type a, Type b) { + return a-b; + }); + data_=std::move(out); + return *this; + } + + friend Matrix operator-(Matrix lhs, + const Matrix &rhs) + { + lhs-=rhs; + return lhs; + } + + friend std::ostream& operator<<(std::ostream &os, const Matrix &mat) + { + os<<'['; + for(std::size_t i=0; i class Vector : public Matrix +{ +public: + Vector() : Matrix() {} + + Type& operator[](int col) + { + return this->data_[col]; + } + + const Type& operator[](int col) const + { + return this->data_[col]; } + + }; +template class Vector2 : public Vector<2, Type> +{ +public: + Vector2() : Vector<2, Type>() {} + + Type& x() + { + return this->data_[0]; + } + + const Type& x() const + { + return this->data_[0]; + } + + Type& y() + { + return this->data_[1]; + } + + const Type& y() const + { + return this->data_[1]; + } +}; + +typedef Vector2 Vector2d; + +namespace matrix +{ + +template +Matrix multiply(const Matrix &m1, const Matrix &m2) +{ + if(N!=P) + { + throw std::runtime_error("Matrices don't have the right dimensions for multiplication"); + } + + Matrix res; + + + + return res; +} + +} // vector + } // yage #endif -- cgit