Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

matrix.h 6.94 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*******************************************************************************
 * CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
 * version 0.1                                                                  *
 * Copyright (C) 2009, 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: https://iggservis.u-strasbg.fr/CGoGN/                              *
 * 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 <unsigned int M, unsigned int N, typename T>
class Matrix
{
	public:
		typedef T DATA_TYPE ;

		static std::string CGoGNnameOfType() ;

		/**********************************************/
		/*                CONSTRUCTORS                */
		/**********************************************/

		Matrix();

		Matrix(const Matrix<M,N,T>& m);

		Matrix(T v);

		void set(T a);

		void zero();

		void identity();

		template <unsigned int K, unsigned int L>
		bool setSubMatrix(unsigned int ii, unsigned int jj, const Matrix<K,L,T>& m);

		template <unsigned int K>
		bool setSubVectorV(unsigned int ii, unsigned int jj, const Vector<K,T>& v) ;

		template <unsigned int L>
		bool setSubVectorH(unsigned int ii, unsigned int jj, const Vector<L,T>& v) ;

		/**********************************************/
		/*                 ACCESSORS                  */
		/**********************************************/

		T& operator()(unsigned int i, unsigned int j);

		const T& operator()(unsigned int i, unsigned int j) const;

		template <unsigned int K, unsigned int L>
		bool getSubMatrix(unsigned int ii, unsigned int jj, Matrix<K,L,T>& m) const;

		template <unsigned int K>
		bool getSubVectorV(unsigned int ii, unsigned int jj, Vector<K,T>& v) const;

		template <unsigned int L>
		bool getSubVectorH(unsigned int ii, unsigned int jj, Vector<L,T>& v) const;

		unsigned int m() const;

		unsigned int n() const;

		/**********************************************/
		/*         ARITHMETIC SELF-OPERATORS          */
		/**********************************************/

		Matrix<M,N,T>& operator+=(const Matrix<M,N,T>& m);

		Matrix<M,N,T>& operator-=(const Matrix<M,N,T>& m);

		Matrix<M,N,T>& operator*=(T a);

		Matrix<M,N,T>& operator/=(T a);

		/**********************************************/
		/*            ARITHMETIC OPERATORS            */
		/**********************************************/

		Matrix<M,N,T> operator+(const Matrix<M,N,T>& m) const;

		Matrix<M,N,T> operator-(const Matrix<M,N,T>& m) const;

		// Matrix / Matrix multiplication
		template <unsigned int P>
		Matrix<M,P,T> operator*(const Matrix<N,P,T>& m) const;

		// Matrix / Vector multiplication
		Vector<M,T> operator*(const Vector<N,T>& v) const;

		// Matrix / Scalar multiplication
		Matrix<M,N,T> 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<N,M,T> 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<M,M,T>& result) const;

		// Equal
		bool operator==(const Matrix<M,N,T>& m) const ;

		/**********************************************/
		/*             STREAM OPERATORS               */
		/**********************************************/

		template <unsigned int MM, unsigned int NN, typename TT>
		friend std::ostream& operator<<(std::ostream& out, const Matrix<MM,NN,TT>& m);

		template <unsigned int MM, unsigned int NN, typename TT>
		friend std::istream& operator>>(std::istream& in, Matrix<MM,NN,TT>& m);

	private:
		T m_data[M][N] ;
} ;

/**********************************************/
/*           EXTERNAL OPERATORS               */
/**********************************************/

// Vector / Matrix multiplication
template <unsigned int M, unsigned int N, typename T>
164
165
166
167
168
Vector<N,T> operator*(const Vector<M,T>& v, const Matrix<M,N,T>& m) ;

// Matrix / Vector multiplication
template <unsigned int M, unsigned int N, typename T>
Vector<M,T> operator*(const Matrix<M,N,T>& m,const Vector<N,T>& v) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
169
170
171

// Scalar / Matrix multiplication
template <unsigned int M, unsigned int N, typename T>
172
173
174
175
176
Matrix<M,N,T> operator*(T s, const Matrix<M,N,T>& m) ;

// Vector / Transposed vector multiplication
template <unsigned int M, unsigned int N, typename T>
Matrix<M,N,T> transposed_vectors_mult(const Vector<M,T>& v1, const Vector<N,T>& v2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207


/**********************************************/
/*           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