Tulip  5.2.0
Large graphs analysis and drawing
Matrix.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 ///@cond DOXYGEN_HIDDEN
20 
21 //@TLPGEOLICENCE#
22 
23 #ifndef _TLP_GEO_MATRIX_H
24 #define _TLP_GEO_MATRIX_H
25 
26 #include <cassert>
27 #include <iostream>
28 #include <vector>
29 #include <tulip/Vector.h>
30 
31 namespace tlp {
32 
33 #define MATRIX tlp::Matrix<Obj, SIZE>
34 
35 /**
36  * @ingroup Structures
37  * \brief class for mathematical square matrix
38  *
39  * Enables to create a Square Matrix of Obj with a
40  * limited size and provides Mathematical operation. Mathematical
41  * operators must be defined for Obj. Out of bound accesses
42  * are only checked in debug mode.
43  *
44  * \author : David Auber auber@labri.fr
45  *
46  * \author Contributor : Maxime Delorme
47  * \version 0.0.2 27/04/2005
48  */
49 template <typename Obj, size_t SIZE>
50 class Matrix : public Array<Vector<Obj, SIZE>, SIZE> {
51 public:
52  Matrix() {}
53  Matrix(const Array<Vector<Obj, SIZE>, SIZE> &a) : Array<Vector<Obj, SIZE>, SIZE>(a) {}
54 
55  // Builds a correlation matrix from a covariance matrix !
56  Matrix(const std::vector<std::vector<Obj>> &covarianceMatrix);
57 
58  /**
59  * Fill the matrix with the value of obj
60  */
61  inline MATRIX &fill(Obj obj);
62  /**
63  * Compute the determinant of the matrix,
64  */
65  Obj determinant() const;
66  /**
67  * Transpose the matrix and return "&(*this)".
68  */
69  MATRIX &transpose();
70  /**
71  * Inverse the matrix and return "&(*this)"
72  */
73  MATRIX &inverse();
74  /**
75  * add another matrix to the current one and return "&(*this)"
76  */
77  inline MATRIX &operator+=(const MATRIX &mat);
78  /**
79  * substract another matrix from the current and return "&(*this)"
80  */
81  inline MATRIX &operator-=(const MATRIX &mat);
82  /**
83  * Check equality of two Matrices
84  */
85  inline bool operator==(const MATRIX &) const;
86  /**
87  * Check non equality of two Matrices
88  */
89  inline bool operator!=(const MATRIX &) const;
90  /**
91  * Multiply the matrix by another matrix and return "&(*this)"
92  */
93  inline MATRIX &operator*=(const MATRIX &mat);
94  /**
95  * Multiply all elements of the matrix by obj, return "&(*this)"
96  */
97  inline MATRIX &operator*=(const Obj &obj);
98  /**
99  * Divide the matrix by another one return "&(*this)"
100  */
101  inline MATRIX &operator/=(const MATRIX &mat);
102  /**
103  * Divide all elements of the matrix by obj, return "&(*this)"
104  */
105  inline MATRIX &operator/=(const Obj &obj);
106  /**
107  * Returns the cofactor Matrix of this
108  */
109  MATRIX cofactor() const;
110  /**
111  * Returns a new matrix equal to the division of the matrix by
112  * another matrix"
113  */
114  MATRIX operator/(const MATRIX &mat2) const;
115 
116  /**
117  * Returns a new matrix equal to the division of the matrix by
118  * obj"
119  */
120  MATRIX operator/(const Obj &obj) const;
121 
122  /**
123  * Returns a new vector equal to the most influent eigenvector of the
124  * matrix
125  */
126  inline Vector<Obj, SIZE> powerIteration(const unsigned int nIterations) const;
127 
128  ///@cond DOXYGEN_HIDDEN
129  /**
130  * Simplifies a 3x3 matrix in 2x2 matrix to be used with computeEigenVector
131  */
132  inline bool simplify(Matrix<Obj, 2> &simplifiedMatrix) const;
133 
134  /**
135  * Returns the EigenVector of the matrix corresponding to the EigenValue passed, with a base x
136  * /!\ This can only be used with a 2x2 matrix !!! /!\
137  */
138  inline bool computeEigenVector(const float x, Vector<Obj, 3> &eigenVector) const;
139  ///@endcond
140 };
141 
142 typedef Matrix<float, 3> Mat3f;
143 typedef Matrix<double, 3> Mat3d;
144 typedef Matrix<float, 4> Mat4f;
145 typedef Matrix<double, 4> Mat4d;
146 
147 /**
148  * Returns a new matrix equal to the sum of 2 matrices
149  */
150 template <typename Obj, size_t SIZE>
151 inline MATRIX operator+(const MATRIX &mat1, const MATRIX &mat2);
152 /**
153  * Returns a new matrix equal to the difference of 2 matrices
154  */
155 template <typename Obj, size_t SIZE>
156 inline MATRIX operator-(const MATRIX &mat1, const MATRIX &mat2);
157 /**
158  * Returns a new matrix equal to the multiplication of the matrix by
159  * obj
160  */
161 template <typename Obj, size_t SIZE>
162 inline MATRIX operator*(const MATRIX &mat, const Obj &obj);
163 /**
164  * Returns a new matrix equal to the multiplication of the matrix by
165  * another matrix
166  */
167 template <typename Obj, size_t SIZE>
168 inline MATRIX operator*(const MATRIX &mat1, const MATRIX &mat2);
169 /**
170  * Returns a new vector equal to the multiplication of the vector by
171  * a matrix,(the vector is automatically transposed to do the multiplication)
172  */
173 template <typename Obj, size_t SIZE>
174 inline Vector<Obj, SIZE> operator*(const Vector<Obj, SIZE> &vec, const tlp::Matrix<Obj, SIZE> &);
175 /**
176  * Returns a new vector equal to the multiplication of the matrix by
177  * a vector
178  */
179 template <typename Obj, size_t SIZE>
180 inline Vector<Obj, SIZE> operator*(const Matrix<Obj, SIZE> &, const Vector<Obj, SIZE> &vec);
181 }
182 
183 #include "cxx/Matrix.cxx"
184 #endif
185 ///@endcond