Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
NumMatrix.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2023 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-2023 */
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/*---------------------------------------------------------------------------*/
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
56 using DataType = T;
57
58 public:
59
61 NumMatrix() = default;
62
64 template <int S = RowSize, typename = std::enable_if_t<S == 2, void>>
65 constexpr ARCCORE_HOST_DEVICE NumMatrix(const VectorType& ax, const VectorType& ay)
66 {
67 m_values[0] = ax;
68 m_values[1] = ay;
69 }
70
72 template <int S = RowSize, typename = std::enable_if_t<S == 3, void>>
73 constexpr ARCCORE_HOST_DEVICE NumMatrix(const VectorType& ax, const VectorType& ay, const VectorType& az)
74 {
75 m_values[0] = ax;
76 m_values[1] = ay;
77 m_values[2] = az;
78 }
79
81 template <int S = RowSize, typename = std::enable_if_t<S == 4, void>>
82 constexpr ARCCORE_HOST_DEVICE NumMatrix(const VectorType& a1, const VectorType& a2,
83 const VectorType& a3, const VectorType& a4)
84 {
85 m_values[0] = a1;
86 m_values[1] = a2;
87 m_values[2] = a3;
88 m_values[3] = a4;
89 }
90
92 template <int S = RowSize, typename = std::enable_if_t<S == 4, void>>
93 constexpr ARCCORE_HOST_DEVICE NumMatrix(const VectorType& a1, const VectorType& a2,
94 const VectorType& a3, const VectorType& a4,
95 const VectorType& a5)
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
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 template <typename X = ThatClass, typename = std::enable_if_t<X::isSquare2(), void>>
112 explicit constexpr ARCCORE_HOST_DEVICE NumMatrix(Real2x2 v)
113 : NumMatrix(VectorType(v.x), VectorType(v.y))
114 {}
115
116 template <typename X = ThatClass, typename = std::enable_if_t<X::isSquare3(), void>>
117 explicit constexpr ARCCORE_HOST_DEVICE NumMatrix(Real3x3 v)
118 : NumMatrix(VectorType(v.x), VectorType(v.y), VectorType(v.z))
119 {}
120
122 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(T v)
123 {
124 for (int i = 0; i < Size; ++i)
125 m_values[i] = v;
126 return (*this);
127 }
128
129 template <typename X = ThatClass, typename = std::enable_if_t<X::isSquare2(), void>>
130 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(const Real2x2& v)
131 {
132 *this = ThatClass(v);
133 return (*this);
134 }
135
136 template <typename X = ThatClass, typename = std::enable_if_t<X::isSquare3(), void>>
137 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(const Real3x3& v)
138 {
139 *this = ThatClass(v);
140 return (*this);
141 }
142
143 template <typename X = ThatClass, typename = std::enable_if_t<X::isSquare2(), void>>
144 operator Real2x2() const
145 {
146 return Real2x2(m_values[0], m_values[1]);
147 }
148
149 template <typename X = ThatClass, typename = std::enable_if_t<X::isSquare3(), void>>
150 operator Real3x3() const
151 {
152 return Real3x3(m_values[0], m_values[1], m_values[2]);
153 }
154
155 public:
156
158 constexpr ARCCORE_HOST_DEVICE static ThatClass zero()
159 {
160 return ThatClass();
161 }
162
164 template <typename X = ThatClass, typename = std::enable_if_t<X::isSquare3(), void>>
165 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)
166 {
168 }
169
171 template <typename X = ThatClass, typename = std::enable_if_t<X::isSquare3(), void>>
172 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)
173 {
175 }
176
177 public:
178
190 constexpr ARCCORE_HOST_DEVICE bool isNearlyZero() const
191 {
192 bool is_nearly_zero = true;
193 for (int i = 0; i < Size; ++i)
194 is_nearly_zero = is_nearly_zero && math::isNearlyZero(m_values[i]);
195 return is_nearly_zero;
196 }
197
199 constexpr ARCCORE_HOST_DEVICE ThatClass& operator+=(const ThatClass& b)
200 {
201 for (int i = 0; i < Size; ++i)
202 m_values[i] += b.m_values[i];
203 return (*this);
204 }
206 constexpr ARCCORE_HOST_DEVICE ThatClass& operator-=(const ThatClass& b)
207 {
208 for (int i = 0; i < Size; ++i)
209 m_values[i] -= b.m_values[i];
210 return (*this);
211 }
213 constexpr ARCCORE_HOST_DEVICE ThatClass& operator*=(T b)
214 {
215 for (int i = 0; i < Size; ++i)
216 m_values[i] *= b;
217 return (*this);
218 }
220 constexpr ARCCORE_HOST_DEVICE ThatClass& operator/=(T b)
221 {
222 for (int i = 0; i < Size; ++i)
223 m_values[i] *= b;
224 return (*this);
225 }
227 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator+(const ThatClass& a, const ThatClass& b)
228 {
229 ThatClass v;
230 for (int i = 0; i < Size; ++i)
231 v.m_values[i] = a.m_values[i] + b.m_values[i];
232 return v;
233 }
235 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator-(const ThatClass& a, const ThatClass& b)
236 {
237 ThatClass v;
238 for (int i = 0; i < Size; ++i)
239 v.m_values[i] = a.m_values[i] - b.m_values[i];
240 return v;
241 }
243 constexpr ARCCORE_HOST_DEVICE ThatClass operator-() const
244 {
245 ThatClass v;
246 for (int i = 0; i < Size; ++i)
247 v.m_values[i] = -m_values[i];
248 return v;
249 }
250
252 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator*(DataType a, const ThatClass& mat)
253 {
254 ThatClass v;
255 for (int i = 0; i < Size; ++i)
256 v.m_values[i] = a * mat.m_values[i];
257 return v;
258 }
260 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator*(const ThatClass& mat, DataType b)
261 {
262 ThatClass v;
263 for (int i = 0; i < Size; ++i)
264 v.m_values[i] = mat.m_values[i] * b;
265 return v;
266 }
268 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator/(const ThatClass& mat, DataType b)
269 {
270 ThatClass v;
271 for (int i = 0; i < Size; ++i)
272 v.m_values[i] = mat.m_values[i] / b;
273 return v;
274 }
275
282 friend constexpr ARCCORE_HOST_DEVICE bool operator==(const ThatClass& a, const ThatClass& b)
283 {
284 for (int i = 0; i < Size; ++i)
285 if (a.m_values[i] != b.m_values[i])
286 return false;
287 return true;
288 }
289
296 friend constexpr ARCCORE_HOST_DEVICE bool operator!=(const ThatClass& a, const ThatClass& b)
297 {
298 return !(a == b);
299 }
300
301 public:
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 la \a i-ème ligne
310 constexpr ARCCORE_HOST_DEVICE VectorType operator[](Int32 i) const
311 {
312 ARCCORE_CHECK_AT(i, RowSize);
313 return m_values[i];
314 }
315 // Récupère une référence sur la valeur de \a i-ème ligne et \a j-ème colonne
316 constexpr ARCCORE_HOST_DEVICE T& operator()(Int32 i, Int32 j)
317 {
318 ARCCORE_CHECK_AT(i, RowSize);
319 ARCCORE_CHECK_AT(j, ColumnSize);
320 return m_values[i](j);
321 }
322 // Récupère la valeur de \a i-ème ligne et \a j-ème colonne
323 constexpr ARCCORE_HOST_DEVICE T operator()(Int32 i, Int32 j) const
324 {
325 ARCCORE_CHECK_AT(i, RowSize);
326 ARCCORE_CHECK_AT(j, ColumnSize);
327 return m_values[i](j);
328 }
329
331 constexpr ARCCORE_HOST_DEVICE void setLine(Int32 i, const VectorType& v)
332 {
333 ARCCORE_CHECK_AT(i, RowSize);
334 m_values[i] = v;
335 }
336
337 public:
338
339 template <int S = RowSize, typename = std::enable_if_t<S >= 1, void>>
340 VectorType& vx()
341 {
342 return m_values[0];
343 }
344 template <int S = RowSize, typename = std::enable_if_t<S >= 1, void>>
345 VectorType vx() const
346 {
347 return m_values[0];
348 }
349
350 template <int S = RowSize, typename = std::enable_if_t<S >= 2, void>>
351 VectorType& vy()
352 {
353 return m_values[1];
354 }
355 template <int S = RowSize, typename = std::enable_if_t<S >= 2, void>>
356 VectorType vy() const
357 {
358 return m_values[1];
359 }
360
361 template <int S = RowSize, typename = std::enable_if_t<S >= 3, void>>
362 VectorType& vz()
363 {
364 return m_values[2];
365 }
366 template <int S = RowSize, typename = std::enable_if_t<S >= 3, void>>
367 VectorType vz() const
368 {
369 return m_values[2];
370 }
371
372 private:
373
374 VectorType m_values[RowSize] = {};
375
376 private:
377
383 constexpr ARCCORE_HOST_DEVICE static bool _eq(T a, T b)
384 {
385 return TypeEqualT<T>::isEqual(a, b);
386 }
387};
388
389/*---------------------------------------------------------------------------*/
390/*---------------------------------------------------------------------------*/
391
392} // End namespace Arcane
393
394/*---------------------------------------------------------------------------*/
395/*---------------------------------------------------------------------------*/
396
397#endif
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Petite matrice de taille fixe contenant RowSize lignes et ColumnSize colonnes.
Definition NumMatrix.h:42
constexpr static ARCCORE_HOST_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:165
constexpr static ARCCORE_HOST_DEVICE ThatClass zero()
Construit la matrice nulle.
Definition NumMatrix.h:158
constexpr ARCCORE_HOST_DEVICE NumMatrix(T v)
Construit l'instance avec le triplet (v,v,v).
Definition NumMatrix.h:105
constexpr ARCCORE_HOST_DEVICE ThatClass & operator+=(const ThatClass &b)
Ajoute b au triplet.
Definition NumMatrix.h:199
friend constexpr ARCCORE_HOST_DEVICE ThatClass operator+(const ThatClass &a, const ThatClass &b)
Créé un triplet qui vaut ce triplet ajouté à b.
Definition NumMatrix.h:227
friend constexpr ARCCORE_HOST_DEVICE bool operator==(const ThatClass &a, const ThatClass &b)
Compare composant pas composante l'instance courante à b.
Definition NumMatrix.h:282
friend constexpr ARCCORE_HOST_DEVICE ThatClass operator*(DataType a, const ThatClass &mat)
Multiplication par un scalaire.
Definition NumMatrix.h:252
constexpr ARCCORE_HOST_DEVICE bool isNearlyZero() const
Compare la matrice avec la matrice nulle.
Definition NumMatrix.h:190
constexpr ARCCORE_HOST_DEVICE void setLine(Int32 i, const VectorType &v)
Positionne à v la valeur de la i-ème ligne.
Definition NumMatrix.h:331
NumMatrix()=default
Construit la matrice avec tous les coefficiants nuls.
constexpr ARCCORE_HOST_DEVICE NumMatrix(const VectorType &ax, const VectorType &ay)
Construit la matrice avec les lignes (ax,ay)
Definition NumMatrix.h:65
constexpr static ARCCORE_HOST_DEVICE bool _eq(T a, T b)
Compare les valeurs de a et b avec le comparateur TypeEqualT.
Definition NumMatrix.h:383
constexpr ARCCORE_HOST_DEVICE NumMatrix(const VectorType &ax, const VectorType &ay, const VectorType &az)
Construit la matrice avec les lignes (ax,ay,az)
Definition NumMatrix.h:73
constexpr ARCCORE_HOST_DEVICE ThatClass & operator*=(T b)
Multiple chaque composante de la matrice par le réel b.
Definition NumMatrix.h:213
constexpr ARCCORE_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:93
constexpr ARCCORE_HOST_DEVICE ThatClass & operator-=(const ThatClass &b)
Soustrait b au triplet.
Definition NumMatrix.h:206
friend constexpr ARCCORE_HOST_DEVICE ThatClass operator*(const ThatClass &mat, DataType b)
Multiplication par un scalaire.
Definition NumMatrix.h:260
constexpr static ARCCORE_HOST_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:172
constexpr ARCCORE_HOST_DEVICE ThatClass & operator/=(T b)
Divise chaque composante de la matrice par le réel b.
Definition NumMatrix.h:220
constexpr ARCCORE_HOST_DEVICE ThatClass operator-() const
Créé un tenseur opposé au tenseur actuel.
Definition NumMatrix.h:243
constexpr ARCCORE_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:82
friend constexpr ARCCORE_HOST_DEVICE bool operator!=(const ThatClass &a, const ThatClass &b)
Compare deux triplets. Pour la notion d'égalité, voir operator==()
Definition NumMatrix.h:296
constexpr ARCCORE_HOST_DEVICE ThatClass & operator=(T v)
Affecte à l'instance le triplet (v,v,v).
Definition NumMatrix.h:122
friend constexpr ARCCORE_HOST_DEVICE ThatClass operator-(const ThatClass &a, const ThatClass &b)
Créé un triplet qui vaut b soustrait de ce triplet.
Definition NumMatrix.h:235
friend constexpr ARCCORE_HOST_DEVICE ThatClass operator/(const ThatClass &mat, DataType b)
Division par un scalaire.
Definition NumMatrix.h:268
Classe gérant une matrice de réel de dimension 2x2.
Definition Real2x2.h:53
constexpr static ARCCORE_HOST_DEVICE bool isEqual(const T &a, const T &b)
Compare a à b.
Definition Numeric.h:92
Espace de nom pour l'utilisation des accélérateurs.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-