/******************************************************************************* * CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps * * version 0.1 * * Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg * * * * This library is free software; you can redistribute it and/or modify it * * under the terms of the GNU Lesser General Public License as published by the * * Free Software Foundation; either version 2.1 of the License, or (at your * * option) any later version. * * * * This library is distributed in the hope that it will be useful, but WITHOUT * * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * * for more details. * * * * You should have received a copy of the GNU Lesser General Public License * * along with this library; if not, write to the Free Software Foundation, * * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * * * Web site: http://cgogn.unistra.fr/ * * Contact information: cgogn@unistra.fr * * * *******************************************************************************/ #ifndef __MATRIX__ #define __MATRIX__ #include "Geometry/vector_gen.h" namespace CGoGN { namespace Geom { /* * Class for the representation of rectangular matrices */ template class Matrix { public: typedef T DATA_TYPE ; static std::string CGoGNnameOfType() ; /**********************************************/ /* CONSTRUCTORS */ /**********************************************/ Matrix(); Matrix(const Matrix& m); Matrix(T v); void set(T a); void zero(); void identity(); template bool setSubMatrix(unsigned int ii, unsigned int jj, const Matrix& m); template bool setSubVectorV(unsigned int ii, unsigned int jj, const Vector& v) ; template bool setSubVectorH(unsigned int ii, unsigned int jj, const Vector& v) ; /**********************************************/ /* ACCESSORS */ /**********************************************/ T& operator()(unsigned int i, unsigned int j); const T& operator()(unsigned int i, unsigned int j) const; template bool getSubMatrix(unsigned int ii, unsigned int jj, Matrix& m) const; template bool getSubVectorV(unsigned int ii, unsigned int jj, Vector& v) const; template bool getSubVectorH(unsigned int ii, unsigned int jj, Vector& v) const; unsigned int m() const; unsigned int n() const; /**********************************************/ /* ARITHMETIC SELF-OPERATORS */ /**********************************************/ Matrix& operator+=(const Matrix& m); Matrix& operator-=(const Matrix& m); Matrix& operator*=(T a); Matrix& operator/=(T a); /**********************************************/ /* ARITHMETIC OPERATORS */ /**********************************************/ Matrix operator+(const Matrix& m) const; Matrix operator-(const Matrix& m) const; // Matrix / Matrix multiplication template Matrix operator*(const Matrix& m) const; // Matrix / Vector multiplication Vector operator*(const Vector& v) const; // Matrix / Scalar multiplication Matrix operator*(T s) const; // Matrix / Scalar division Matrix operator/(T s) const; /**********************************************/ /* UTILITY FUNCTIONS */ /**********************************************/ // transpose the matrix // ** Works only for square matrices ** void transpose(); // return a new matrix which is the transpose of the matrix Matrix transposed() const; // Invert the matrix using Gauss-Jordan elimination // The determinant of the matrix is returned // (in case of singular matrix (determinant=0), // trash values are leaved in the result) // ** Works only for square matrices ** T invert(Matrix& result) const; // Equal bool operator==(const Matrix& m) const ; /**********************************************/ /* STREAM OPERATORS */ /**********************************************/ template friend std::ostream& operator<<(std::ostream& out, const Matrix& m); template friend std::istream& operator>>(std::istream& in, Matrix& m); private: T m_data[M][N] ; } ; /**********************************************/ /* EXTERNAL OPERATORS */ /**********************************************/ // Vector / Matrix multiplication template Vector operator*(const Vector& v, const Matrix& m) ; // Matrix / Vector multiplication template Vector operator*(const Matrix& m,const Vector& v) ; // Scalar / Matrix multiplication template Matrix operator*(T s, const Matrix& m) ; // Vector / Transposed vector multiplication template Matrix transposed_vectors_mult(const Vector& v1, const Vector& v2) ; /**********************************************/ /* SOME USEFUL TYPEDEFS */ /**********************************************/ typedef Matrix<2,2,float> Matrix22f ; typedef Matrix<2,2,double> Matrix22d ; typedef Matrix<2,2,unsigned int> Matrix22ui ; typedef Matrix<2,2,int> Matrix22i ; typedef Matrix<2,2,unsigned char> Matrix22uc ; typedef Matrix<3,3,float> Matrix33f ; typedef Matrix<3,3,double> Matrix33d ; typedef Matrix<3,3,unsigned int> Matrix33ui ; typedef Matrix<3,3,int> Matrix33i ; typedef Matrix<3,3,unsigned char> Matrix33uc ; typedef Matrix<4,4,float> Matrix44f ; typedef Matrix<4,4,double> Matrix44d ; typedef Matrix<4,4,unsigned int> Matrix44ui ; typedef Matrix<4,4,int> Matrix44i ; typedef Matrix<4,4,unsigned char> Matrix44uc ; } } #include "matrix.hpp" #endif