Arcane  v4.1.7.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
NumMatrix.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* NumMatrix.h (C) 2000-2026 */
9/* */
10/* Matrice carrée de taille fixe de types numériques. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_NUMMATRIX_H
13#define ARCANE_UTILS_NUMMATRIX_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/NumVector.h"
18#include "arcane/utils/Real2x2.h"
19#include "arcane/utils/Real3x3.h"
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Arcane
25{
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29/*!
30 * \brief Petite matrice de taille fixe contenant RowSize lignes et ColumnSize colonnes.
31 *
32 * \note Actuellement uniquement implémenté pour le type Real.
33 *
34 * \warning API en cours de définition. Ne pas utiliser en dehors de Arcane
35 *
36 * Il est possible d'accéder à chaque composante du vecteur par 'operator[]'
37 * ou 'operator()' ou par les méthodes vx(), vy(), vz() si la dimension est
38 * suffisante (par exemple vz() est uniquement accessible si la Size>=3.
39 */
40template <typename T, int RowSize, int ColumnSize>
42{
43 static_assert(RowSize > 1, "RowSize has to be strictly greater than 1");
44 static_assert(ColumnSize > 1, "RowSize has to be strictly greater than 1");
45 //static_assert(RowSize == ColumnSize, "Only square matrix are allowed (ColumnSize==RowSize)");
46 static_assert(std::is_same_v<T, Real>, "Only type 'Real' is allowed");
47 static constexpr int Size = RowSize;
48 static constexpr bool isSquare() { return RowSize == ColumnSize; }
49 static constexpr bool isSquare2() { return RowSize == 2 && ColumnSize == 2; }
50 static constexpr bool isSquare3() { return RowSize == 3 && ColumnSize == 3; }
51
52 public:
53
54 using VectorType = NumVector<T, ColumnSize>;
55 using ThatClass = NumMatrix<T, RowSize, ColumnSize>;
56 using DataType = T;
57
58 public:
59
60 //! Construit la matrice avec tous les coefficiants nuls.
61 NumMatrix() = default;
62
63 //! Construit la matrice avec les lignes (ax,ay)
64 constexpr ARCCORE_HOST_DEVICE NumMatrix(const VectorType& ax, const VectorType& ay)
65 requires(RowSize == 2)
66 {
67 m_values[0] = ax;
68 m_values[1] = ay;
69 }
70
71 //! Construit la matrice avec les lignes (ax,ay,az)
72 constexpr ARCCORE_HOST_DEVICE NumMatrix(const VectorType& ax, const VectorType& ay, const VectorType& az)
73 requires(RowSize == 3)
74 {
75 m_values[0] = ax;
76 m_values[1] = ay;
77 m_values[2] = az;
78 }
79
80 //! Construit la matrice avec les lignes (a1,a2,a3,a4)
81 constexpr ARCCORE_HOST_DEVICE NumMatrix(const VectorType& a1, const VectorType& a2,
82 const VectorType& a3, const VectorType& a4)
83 requires(RowSize == 4)
84 {
85 m_values[0] = a1;
86 m_values[1] = a2;
87 m_values[2] = a3;
88 m_values[3] = a4;
89 }
90
91 //! Construit la matrice avec les lignes (a1,a2,a3,a4,a5)
92 constexpr ARCCORE_HOST_DEVICE NumMatrix(const VectorType& a1, const VectorType& a2,
93 const VectorType& a3, const VectorType& a4,
94 const VectorType& a5)
95 requires(RowSize == 5)
96 {
97 m_values[0] = a1;
98 m_values[1] = a2;
99 m_values[2] = a3;
100 m_values[3] = a4;
101 m_values[4] = a5;
102 }
103
104 //! Construit l'instance avec le triplet (v,v,v).
105 constexpr ARCCORE_HOST_DEVICE explicit NumMatrix(T v)
106 {
107 for (int i = 0; i < Size; ++i)
108 m_values[i] = v;
109 }
110
111 explicit constexpr ARCCORE_HOST_DEVICE NumMatrix(Real2x2 v) requires(isSquare2())
112 : NumMatrix(VectorType(v.x), VectorType(v.y))
113 {}
114
115 explicit constexpr ARCCORE_HOST_DEVICE NumMatrix(Real3x3 v) requires(isSquare3())
116 : NumMatrix(VectorType(v.x), VectorType(v.y), VectorType(v.z))
117 {}
118
119 //! Affecte à l'instance le triplet (v,v,v).
120 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(T v)
121 {
122 for (int i = 0; i < Size; ++i)
123 m_values[i] = v;
124 return (*this);
125 }
126
127 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(const Real2x2& v) requires(isSquare2())
128 {
129 *this = ThatClass(v);
130 return (*this);
131 }
132
133 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(const Real3x3& v) requires(isSquare3())
134 {
135 *this = ThatClass(v);
136 return (*this);
137 }
138
139 operator Real2x2() const requires(isSquare2())
140 {
141 return Real2x2(m_values[0], m_values[1]);
142 }
143
144 operator Real3x3() const requires(isSquare3())
145 {
146 return Real3x3(m_values[0], m_values[1], m_values[2]);
147 }
148
149 public:
150
151 //! Construit la matrice nulle
152 constexpr ARCCORE_HOST_DEVICE static ThatClass zero()
153 {
154 return ThatClass();
155 }
156
157 //! Construit la matrice ((ax,bx,cx),(ay,by,cy),(az,bz,cz)).
158 constexpr ARCCORE_HOST_DEVICE static ThatClass fromColumns(T ax, T ay, T az, T bx, T by, T bz, T cx, T cy, T cz)
159 requires(isSquare3())
160 {
161 return ThatClass(VectorType(ax, bx, cx), VectorType(ay, by, cy), VectorType(az, bz, cz));
162 }
163
164 //! Construit la matrice ((ax,bx,cx),(ay,by,cy),(az,bz,cz)).
165 constexpr ARCCORE_HOST_DEVICE static ThatClass fromLines(T ax, T bx, T cx, T ay, T by, T cy, T az, T bz, T cz)
166 requires(isSquare3())
167 {
168 return ThatClass(VectorType(ax, bx, cx), VectorType(ay, by, cy), VectorType(az, bz, cz));
169 }
170
171 public:
172
173 /*!
174 * \brief Compare la matrice avec la matrice nulle.
175 *
176 * La matrice est nulle si et seulement si chacune de ses composantes
177 * est inférieure à un espilon donné. La valeur de l'epsilon utilisée est celle
178 * de float_info<value_type>::nearlyEpsilon():
179 * \f[A=0 \Leftrightarrow |A.x|<\epsilon,|A.y|<\epsilon,|A.z|<\epsilon \f]
180 *
181 * \retval true si la matrice est égale à la matrice nulle,
182 * \retval false sinon.
183 */
184 constexpr ARCCORE_HOST_DEVICE bool isNearlyZero() const
185 {
186 bool is_nearly_zero = true;
187 for (int i = 0; i < Size; ++i)
188 is_nearly_zero = is_nearly_zero && math::isNearlyZero(m_values[i]);
189 return is_nearly_zero;
190 }
191
192 //! Ajoute \a b au triplet.
193 constexpr ARCCORE_HOST_DEVICE ThatClass& operator+=(const ThatClass& b)
194 {
195 for (int i = 0; i < Size; ++i)
196 m_values[i] += b.m_values[i];
197 return (*this);
198 }
199 //! Soustrait \a b au triplet
200 constexpr ARCCORE_HOST_DEVICE ThatClass& operator-=(const ThatClass& b)
201 {
202 for (int i = 0; i < Size; ++i)
203 m_values[i] -= b.m_values[i];
204 return (*this);
205 }
206 //! Multiple chaque composante de la matrice par le réel \a b
207 constexpr ARCCORE_HOST_DEVICE ThatClass& operator*=(T b)
208 {
209 for (int i = 0; i < Size; ++i)
210 m_values[i] *= b;
211 return (*this);
212 }
213 //! Divise chaque composante de la matrice par le réel \a b
214 constexpr ARCCORE_HOST_DEVICE ThatClass& operator/=(T b)
215 {
216 for (int i = 0; i < Size; ++i)
217 m_values[i] *= b;
218 return (*this);
219 }
220 //! Créé un triplet qui vaut ce triplet ajouté à \a b
221 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator+(const ThatClass& a, const ThatClass& b)
222 {
223 ThatClass v;
224 for (int i = 0; i < Size; ++i)
225 v.m_values[i] = a.m_values[i] + b.m_values[i];
226 return v;
227 }
228 //! Créé un triplet qui vaut \a b soustrait de ce triplet
229 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator-(const ThatClass& a, const ThatClass& b)
230 {
231 ThatClass v;
232 for (int i = 0; i < Size; ++i)
233 v.m_values[i] = a.m_values[i] - b.m_values[i];
234 return v;
235 }
236 //! Créé un tenseur opposé au tenseur actuel
237 constexpr ARCCORE_HOST_DEVICE ThatClass operator-() const
238 {
239 ThatClass v;
240 for (int i = 0; i < Size; ++i)
241 v.m_values[i] = -m_values[i];
242 return v;
243 }
244
245 //! Multiplication par un scalaire.
246 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator*(DataType a, const ThatClass& mat)
247 {
248 ThatClass v;
249 for (int i = 0; i < Size; ++i)
250 v.m_values[i] = a * mat.m_values[i];
251 return v;
252 }
253 //! Multiplication par un scalaire.
254 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator*(const ThatClass& mat, DataType b)
255 {
256 ThatClass v;
257 for (int i = 0; i < Size; ++i)
258 v.m_values[i] = mat.m_values[i] * b;
259 return v;
260 }
261 //! Division par un scalaire.
262 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator/(const ThatClass& mat, DataType b)
263 {
264 ThatClass v;
265 for (int i = 0; i < Size; ++i)
266 v.m_values[i] = mat.m_values[i] / b;
267 return v;
268 }
269
270 /*!
271 * \brief Compare composant pas composante l'instance courante à \a b.
272 *
273 * \retval true si this.x==b.x et this.y==b.y et this.z==b.z.
274 * \retval false sinon.
275 */
276 friend constexpr ARCCORE_HOST_DEVICE bool operator==(const ThatClass& a, const ThatClass& b)
277 {
278 for (int i = 0; i < Size; ++i)
279 if (a.m_values[i] != b.m_values[i])
280 return false;
281 return true;
282 }
283
284 /*!
285 * \brief Compare deux triplets.
286 * Pour la notion d'égalité, voir operator==()
287 * \retval true si les deux triplets sont différents,
288 * \retval false sinon.
289 */
290 friend constexpr ARCCORE_HOST_DEVICE bool operator!=(const ThatClass& a, const ThatClass& b)
291 {
292 return !(a == b);
293 }
294
295 public:
296
297 // Récupère la \a i-ème ligne
298 constexpr ARCCORE_HOST_DEVICE VectorType operator()(Int32 i) const
299 {
300 ARCCORE_CHECK_AT(i, RowSize);
301 return m_values[i];
302 }
303 // Récupère la \a i-ème ligne
304 constexpr ARCCORE_HOST_DEVICE VectorType operator[](Int32 i) const
305 {
306 ARCCORE_CHECK_AT(i, RowSize);
307 return m_values[i];
308 }
309 // Récupère une référence sur la valeur de \a i-ème ligne et \a j-ème colonne
310 constexpr ARCCORE_HOST_DEVICE T& operator()(Int32 i, Int32 j)
311 {
312 ARCCORE_CHECK_AT(i, RowSize);
313 ARCCORE_CHECK_AT(j, ColumnSize);
314 return m_values[i](j);
315 }
316 // Récupère la valeur de \a i-ème ligne et \a j-ème colonne
317 constexpr ARCCORE_HOST_DEVICE T operator()(Int32 i, Int32 j) const
318 {
319 ARCCORE_CHECK_AT(i, RowSize);
320 ARCCORE_CHECK_AT(j, ColumnSize);
321 return m_values[i](j);
322 }
323
324 //! Positionne à \a v la valeur de la \a i-ème ligne
325 constexpr ARCCORE_HOST_DEVICE void setLine(Int32 i, const VectorType& v)
326 {
327 ARCCORE_CHECK_AT(i, RowSize);
328 m_values[i] = v;
329 }
330
331 public:
332
333 VectorType& vx() requires(RowSize >= 1)
334 {
335 return m_values[0];
336 }
337
338 VectorType vx() const requires(RowSize >= 1)
339 {
340 return m_values[0];
341 }
342
343 VectorType& vy() requires(RowSize >= 2)
344 {
345 return m_values[1];
346 }
347
348 VectorType vy() const requires(RowSize >= 2)
349 {
350 return m_values[1];
351 }
352
353 VectorType& vz() requires(RowSize >= 3)
354 {
355 return m_values[2];
356 }
357
358 VectorType vz() const requires(RowSize >= 3)
359 {
360 return m_values[2];
361 }
362
363 private:
364
365 VectorType m_values[RowSize] = {};
366
367 private:
368
369 /*!
370 * \brief Compare les valeurs de \a a et \a b avec le comparateur TypeEqualT
371 * \retval true si \a a et \a b sont égaux,
372 * \retval false sinon.
373 */
374 constexpr ARCCORE_HOST_DEVICE static bool _eq(T a, T b)
375 {
376 return TypeEqualT<T>::isEqual(a, b);
377 }
378};
379
380/*---------------------------------------------------------------------------*/
381/*---------------------------------------------------------------------------*/
382
383} // End namespace Arcane
384
385/*---------------------------------------------------------------------------*/
386/*---------------------------------------------------------------------------*/
387
388#endif
Petite matrice de taille fixe contenant RowSize lignes et ColumnSize colonnes.
Definition NumMatrix.h:42
friend constexpr __host__ __device__ ThatClass operator/(const ThatClass &mat, DataType b)
Division par un scalaire.
Definition NumMatrix.h:262
friend constexpr __host__ __device__ bool operator!=(const ThatClass &a, const ThatClass &b)
Compare deux triplets. Pour la notion d'égalité, voir operator==()
Definition NumMatrix.h:290
constexpr __host__ __device__ ThatClass & operator+=(const ThatClass &b)
Ajoute b au triplet.
Definition NumMatrix.h:193
constexpr __host__ static __device__ ThatClass zero()
Construit la matrice nulle.
Definition NumMatrix.h:152
constexpr __host__ __device__ bool isNearlyZero() const
Compare la matrice avec la matrice nulle.
Definition NumMatrix.h:184
constexpr __host__ __device__ ThatClass operator-() const
Créé un tenseur opposé au tenseur actuel.
Definition NumMatrix.h:237
constexpr __host__ __device__ ThatClass & operator/=(T b)
Divise chaque composante de la matrice par le réel b.
Definition NumMatrix.h:214
constexpr __host__ __device__ ThatClass & operator-=(const ThatClass &b)
Soustrait b au triplet.
Definition NumMatrix.h:200
constexpr __host__ static __device__ ThatClass fromLines(T ax, T bx, T cx, T ay, T by, T cy, T az, T bz, T cz)
Construit la matrice ((ax,bx,cx),(ay,by,cy),(az,bz,cz)).
Definition NumMatrix.h:165
friend constexpr __host__ __device__ bool operator==(const ThatClass &a, const ThatClass &b)
Compare composant pas composante l'instance courante à b.
Definition NumMatrix.h:276
NumMatrix()=default
Construit la matrice avec tous les coefficiants nuls.
friend constexpr __host__ __device__ ThatClass operator-(const ThatClass &a, const ThatClass &b)
Créé un triplet qui vaut b soustrait de ce triplet.
Definition NumMatrix.h:229
constexpr __host__ __device__ NumMatrix(const VectorType &ax, const VectorType &ay)
Construit la matrice avec les lignes (ax,ay)
Definition NumMatrix.h:64
constexpr __host__ __device__ ThatClass & operator*=(T b)
Multiple chaque composante de la matrice par le réel b.
Definition NumMatrix.h:207
constexpr __host__ __device__ NumMatrix(const VectorType &ax, const VectorType &ay, const VectorType &az)
Construit la matrice avec les lignes (ax,ay,az)
Definition NumMatrix.h:72
constexpr __host__ __device__ void setLine(Int32 i, const VectorType &v)
Positionne à v la valeur de la i-ème ligne.
Definition NumMatrix.h:325
constexpr __host__ __device__ NumMatrix(const VectorType &a1, const VectorType &a2, const VectorType &a3, const VectorType &a4, const VectorType &a5)
Construit la matrice avec les lignes (a1,a2,a3,a4,a5)
Definition NumMatrix.h:92
constexpr __host__ static __device__ ThatClass fromColumns(T ax, T ay, T az, T bx, T by, T bz, T cx, T cy, T cz)
Construit la matrice ((ax,bx,cx),(ay,by,cy),(az,bz,cz)).
Definition NumMatrix.h:158
constexpr __host__ __device__ NumMatrix(const VectorType &a1, const VectorType &a2, const VectorType &a3, const VectorType &a4)
Construit la matrice avec les lignes (a1,a2,a3,a4)
Definition NumMatrix.h:81
friend constexpr __host__ __device__ ThatClass operator*(const ThatClass &mat, DataType b)
Multiplication par un scalaire.
Definition NumMatrix.h:254
constexpr __host__ __device__ ThatClass & operator=(T v)
Affecte à l'instance le triplet (v,v,v).
Definition NumMatrix.h:120
friend constexpr __host__ __device__ ThatClass operator*(DataType a, const ThatClass &mat)
Multiplication par un scalaire.
Definition NumMatrix.h:246
constexpr __host__ __device__ NumMatrix(T v)
Construit l'instance avec le triplet (v,v,v).
Definition NumMatrix.h:105
friend constexpr __host__ __device__ ThatClass operator+(const ThatClass &a, const ThatClass &b)
Créé un triplet qui vaut ce triplet ajouté à b.
Definition NumMatrix.h:221
Petit vecteur de taille fixe de N données numériques.
Definition NumVector.h:43
Classe gérant une matrice de réel de dimension 2x2.
Definition Real2x2.h:53
constexpr __host__ static __device__ bool isEqual(const T &a, const T &b)
Compare a à b.
Definition Numeric.h:92
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
std::int32_t Int32
Type entier signé sur 32 bits.