diff options
-rw-r--r-- | include/YAGE/Math/matrix.hpp | 97 | ||||
-rw-r--r-- | test/matrix_test.cpp | 7 |
2 files changed, 70 insertions, 34 deletions
diff --git a/include/YAGE/Math/matrix.hpp b/include/YAGE/Math/matrix.hpp index 3b18be0b..5a9e80f2 100644 --- a/include/YAGE/Math/matrix.hpp +++ b/include/YAGE/Math/matrix.hpp @@ -4,12 +4,15 @@ #include <algorithm> #include <exception> #include <iostream> +#include <sstream> +#include <string> #include <vector> namespace yage { template<int Rows, int Cols, class Type> class Matrix; +template<int Rows, class Type> class Vector; namespace detail { @@ -47,21 +50,69 @@ protected: public: Matrix<Rows, Cols, Type>() : data_(Rows*Cols) {} - int rowSize() + int rowSize() const { return Rows; } - int colSize() + int colSize() const { return Cols; } + Matrix<1, Cols, Type> getRow(int row) const + { + Matrix<1, Cols, Type> rowMatrix; + for(int i=0; i<Cols; ++i) + { + rowMatrix[0][i]=data_[row][i]; + } + return rowMatrix; + } + + Matrix<Rows, 1, Type> getCol(int col) const + { + Matrix<Rows, 1, Type> colMatrix; + for(int i=0; i<Rows; ++i) + { + colMatrix[i][0]=data_[i][col]; + } + return colMatrix; + } + + virtual std::string toString() const + { + std::stringstream ss; + ss<<'['; + for(int i=0; i<Rows-1; ++i) + { + ss<<'['; + for(int j=0; j<Cols-1; ++j) + { + ss<<data_[i*Cols+j]<<' '; + } + ss<<data_[(Rows-1)*Cols+Cols-1]<<"],"; + } + ss<<'['; + for(int j=0; j<Cols-1; ++j) + { + ss<<data_[(Rows-1)*Cols+j]<<' '; + } + ss<<data_[(Rows-1)*Cols+Cols-1]<<"]]"; + return ss.str(); + } + detail::Row<Rows, Cols, Type> operator[](int row) { return detail::Row<Rows, Cols, Type>(this, row); } + detail::Row<Rows, Cols, Type> operator[](int row) const + { + // TODO got to fix this + return detail::Row<Rows, Cols, Type>((Matrix<Rows, Cols, Type>*)this, row); + } + Matrix<Rows, Cols, Type>& operator=(const Matrix<Rows, Cols, Type> &other) { if(this!=&other) @@ -111,25 +162,7 @@ public: friend std::ostream& operator<<(std::ostream &os, const Matrix<Rows, Cols, Type> &mat) { - os<<'['; - for(std::size_t i=0; i<mat.data_.size()-1; ++i) - { - if(i%Cols == 0 && i!=0) - { - os<<"]\n["; - } - - if((i+1)%Cols == 0) - { - os<<mat.data_[i]; - } - else - { - os<<mat.data_[i]<<" "; - } - } - os<<mat.data_[mat.data_.size()-1]<<"]"; - return os; + return os<<mat.toString(); } }; @@ -137,10 +170,7 @@ template<int Rows=2, class Type=double> class Vector : public Matrix<Rows, 1, Ty { public: Vector<Rows, Type>() : Matrix<Rows, 1, Type>() {} - explicit Vector<Rows, Type>(const Matrix<Rows, 1, Type> &other) - { - this->data_=other.data_; - } + explicit Vector<Rows, Type>(const Matrix<Rows, 1, Type> &other) : Matrix<Rows, 1, Type>(other) {} Type& operator[](int col) { @@ -188,12 +218,15 @@ namespace matrix template<int M, int N, class T> Matrix<N, M, T> transpose(const Matrix<M, N, T> &m) { - static_assert(M!=1, "Called wrong transpose"); -} - -template<int N, class T> Vector<N, T> transpose(const Matrix<1, N, T> &m) -{ - + Matrix<N, M, T> trans; + for(int i=0; i<M; ++i) + { + for(int j=0; j<N; ++j) + { + trans[j][i]=m[i][j]; + } + } + return trans; } template<int R, class T> T dot(const Vector<R, T> &v1, const Vector<R, T> &v2) @@ -223,7 +256,7 @@ Matrix<M, Q, T> multiply(const Matrix<M, N, T> &m1, const Matrix<P, Q, T> &m2) return res; } -} // vector +} // matrix } // yage diff --git a/test/matrix_test.cpp b/test/matrix_test.cpp index a6974c7f..bf85ab35 100644 --- a/test/matrix_test.cpp +++ b/test/matrix_test.cpp @@ -9,7 +9,7 @@ int main() yage::Vector<2, int> v1, v2; yage::Vector2d v3; - + m1[0][1]=1; m2[1][1]=2; @@ -23,10 +23,13 @@ int main() v3.x() = 2; v3.y() = 1; - yage::matrix::multiply(m1, m2); + auto m3=yage::matrix::transpose(v3); + auto vec4=yage::Vector<2, double>(yage::matrix::transpose(m3)); std::cout<<m2<<'\n'; std::cout<<v3<<'\n'; + std::cout<<m3<<'\n'; + std::cout<<vec4<<'\n'; return 0; } |