Tulip  4.6.0
Better Visualization Through Research
library/tulip-core/include/tulip/Matrix.h
00001 /*
00002  *
00003  * This file is part of Tulip (www.tulip-software.org)
00004  *
00005  * Authors: David Auber and the Tulip development Team
00006  * from LaBRI, University of Bordeaux
00007  *
00008  * Tulip is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation, either version 3
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Tulip is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  *
00018  */
00019 ///@cond DOXYGEN_HIDDEN
00020 
00021 //@TLPGEOLICENCE#
00022 
00023 #ifndef _TLP_GEO_MATRIX_H
00024 #define _TLP_GEO_MATRIX_H
00025 
00026 #include <cassert>
00027 #include <iostream>
00028 #include <vector>
00029 #include <tulip/Vector.h>
00030 
00031 
00032 namespace tlp {
00033 
00034 #define MATRIX tlp::Matrix<Obj,SIZE>
00035 
00036 
00037 /**
00038  * @ingroup Structures
00039  * \brief class for mathematical square matrix
00040  *
00041  * Enables to create a Square Matrix of Obj with a
00042  * limited size and provides Mathematical operation. Mathematical
00043  * operators must be defined for Obj. Out of bound accesses
00044  * are only checked in debug mode.
00045  *
00046  * \author : David Auber auber@tulip-software.org
00047  *
00048  * \author Contributor : Maxime Delorme
00049  * \version 0.0.2 27/04/2005
00050  */
00051 template<typename Obj,unsigned int SIZE>
00052 class Matrix:public Array< Vector<Obj,SIZE> , SIZE > {
00053 public:
00054   Matrix() {}
00055   Matrix(const Array< Vector<Obj,SIZE> , SIZE > &a) :
00056     Array< Vector<Obj,SIZE> , SIZE >(a) {
00057   }
00058 
00059   // Builds a correlation matrix from a covariance matrix !
00060   Matrix(const std::vector<std::vector<Obj> > &covarianceMatrix);
00061 
00062   /**
00063    * Fill the matrix with the value of obj
00064    */
00065   inline MATRIX& fill(Obj obj);
00066   /**
00067    * Compute the determinant of the matrix,
00068    */
00069   Obj determinant() const;
00070   /**
00071    * Transpose the matrix and return "&(*this)".
00072    */
00073   MATRIX& transpose();
00074   /**
00075    * Inverse the matrix and return "&(*this)"
00076    */
00077   MATRIX& inverse();
00078   /**
00079    * add another matrix to the current one and return "&(*this)"
00080    */
00081   inline MATRIX & operator+=(const MATRIX &mat);
00082   /**
00083    * substract another matrix from the current and return "&(*this)"
00084    */
00085   inline MATRIX & operator-=(const MATRIX &mat);
00086   /**
00087    * Check equality of two Matrices
00088    */
00089   inline bool operator==(const MATRIX &) const;
00090   /**
00091    * Check non equality of two Matrices
00092    */
00093   inline bool operator!=(const MATRIX &) const;
00094   /**
00095    * Multiply the matrix by another matrix and return "&(*this)"
00096    */
00097   inline MATRIX & operator*=(const MATRIX &mat);
00098   /**
00099    * Multiply all elements of the matrix by obj, return "&(*this)"
00100    */
00101   inline MATRIX & operator*=(const Obj &obj);
00102   /**
00103    *  Divide the matrix by another one return "&(*this)"
00104    */
00105   inline MATRIX & operator/=(const MATRIX &mat);
00106   /**
00107    * Divide all elements of the matrix by obj, return "&(*this)"
00108    */
00109   inline MATRIX & operator/=(const Obj &obj);
00110   /**
00111    * Returns the cofactor Matrix of this
00112    */
00113   MATRIX cofactor() const;
00114   /**
00115    * Returns a new matrix equal to the division of the matrix by
00116    * another matrix"
00117    */
00118   MATRIX operator/(const MATRIX &mat2) const;
00119 
00120   /**
00121    * Returns a new matrix equal to the division of the matrix by
00122    * obj"
00123    */
00124   MATRIX operator/(const Obj &obj) const;
00125 
00126 
00127   /**
00128    * Returns a new vector equal to the most influent eigenvector of the
00129    * matrix
00130    */
00131   inline Vector<Obj,SIZE> powerIteration(const unsigned int nIterations) const;
00132 
00133 #ifndef DOXYGEN_NOTFOR_DEVEL
00134   /**
00135    * Simplifies a 3x3 matrix in 2x2 matrix to be used with computeEigenVector
00136    */
00137   inline bool simplify(Matrix<Obj, 2> &simplifiedMatrix) const;
00138 
00139   /**
00140    * Returns the EigenVector of the matrix corresponding to the EigenValue passed, with a base x
00141    *           /!\ This can only be used with a 2x2 matrix !!! /!\
00142    */
00143   inline bool computeEigenVector(const float x, Vector<Obj, 3> &eigenVector) const;
00144 #endif // DOXYGEN_NOTFOR_DEVEL
00145 
00146 };
00147 
00148 typedef Matrix<float,  3> Mat3f;
00149 typedef Matrix<double, 3> Mat3d;
00150 typedef Matrix<float,  4> Mat4f;
00151 typedef Matrix<double, 4> Mat4d;
00152 
00153 /**
00154  * Returns a new matrix equal to the sum of 2 matrices
00155  */
00156 template<typename Obj, unsigned int SIZE>
00157 inline MATRIX operator+(const MATRIX &mat1, const MATRIX &mat2);
00158 /**
00159  * Returns a new matrix equal to the difference of 2 matrices
00160  */
00161 template<typename Obj, unsigned int SIZE>
00162 inline MATRIX operator-(const MATRIX &mat1, const MATRIX &mat2);
00163 /**
00164  * Returns a new matrix equal to the multiplication of the matrix by
00165  * obj
00166  */
00167 template<typename Obj, unsigned int SIZE>
00168 inline MATRIX operator*(const MATRIX &mat, const Obj &obj);
00169 /**
00170  * Returns a new matrix equal to the multiplication of the matrix by
00171  * another matrix
00172  */
00173 template<typename Obj, unsigned int SIZE>
00174 inline MATRIX operator*(const MATRIX &mat1, const MATRIX &mat2);
00175 /**
00176  * Returns a new vector equal to the multiplication of the vector by
00177  * a matrix,(the vector is automatically transposed to do the multiplication)
00178  */
00179 template<typename Obj, unsigned int SIZE>
00180 inline Vector<Obj,SIZE> operator*(const Vector<Obj,SIZE> &vec, const tlp::Matrix<Obj, SIZE> &);
00181 /**
00182  * Returns a new vector equal to the multiplication of the matrix by
00183  * a vector
00184  */
00185 template<typename Obj, unsigned int SIZE>
00186 inline Vector<Obj,SIZE> operator*( const Matrix<Obj, SIZE> &, const Vector<Obj,SIZE> &vec);
00187 
00188 
00189 
00190 }
00191 
00192 #include "cxx/Matrix.cxx"
00193 #endif
00194 ///@endcond
 All Classes Files Functions Variables Enumerations Enumerator Properties