Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
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/* Fixed-size square matrix of numeric types. */
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
41template <typename T, int RowSize, int ColumnSize>
43{
44 static_assert(RowSize > 1, "RowSize has to be strictly greater than 1");
45 static_assert(ColumnSize > 1, "RowSize has to be strictly greater than 1");
46 //static_assert(RowSize == ColumnSize, "Only square matrix are allowed (ColumnSize==RowSize)");
47 static_assert(std::is_same_v<T, Real>, "Only type 'Real' is allowed");
48 static constexpr int Size = RowSize;
49 static constexpr bool isSquare() { return RowSize == ColumnSize; }
50 static constexpr bool isSquare2() { return RowSize == 2 && ColumnSize == 2; }
51 static constexpr bool isSquare3() { return RowSize == 3 && ColumnSize == 3; }
52
53 public:
54
55 using VectorType = NumVector<T, ColumnSize>;
56 using ThatClass = NumMatrix<T, RowSize, ColumnSize>;
57 using DataType = T;
58
59 public:
60
62 NumMatrix() = default;
63
65 constexpr ARCCORE_HOST_DEVICE NumMatrix(const VectorType& ax, const VectorType& ay)
66 requires(RowSize == 2)
67 {
68 m_values[0] = ax;
69 m_values[1] = ay;
70 }
71
73 constexpr ARCCORE_HOST_DEVICE NumMatrix(const VectorType& ax, const VectorType& ay, const VectorType& az)
74 requires(RowSize == 3)
75 {
76 m_values[0] = ax;
77 m_values[1] = ay;
78 m_values[2] = az;
79 }
80
82 constexpr ARCCORE_HOST_DEVICE NumMatrix(const VectorType& a1, const VectorType& a2,
83 const VectorType& a3, const VectorType& a4)
84 requires(RowSize == 4)
85 {
86 m_values[0] = a1;
87 m_values[1] = a2;
88 m_values[2] = a3;
89 m_values[3] = a4;
90 }
91
93 constexpr ARCCORE_HOST_DEVICE NumMatrix(const VectorType& a1, const VectorType& a2,
94 const VectorType& a3, const VectorType& a4,
95 const VectorType& a5)
96 requires(RowSize == 5)
97 {
98 m_values[0] = a1;
99 m_values[1] = a2;
100 m_values[2] = a3;
101 m_values[3] = a4;
102 m_values[4] = a5;
103 }
104
106 constexpr ARCCORE_HOST_DEVICE explicit NumMatrix(T v)
107 {
108 for (int i = 0; i < Size; ++i)
109 m_values[i] = v;
110 }
111
112 explicit constexpr ARCCORE_HOST_DEVICE NumMatrix(Real2x2 v) requires(isSquare2())
113 : NumMatrix(VectorType(v.x), VectorType(v.y))
114 {}
115
116 explicit constexpr ARCCORE_HOST_DEVICE NumMatrix(Real3x3 v) requires(isSquare3())
117 : NumMatrix(VectorType(v.x), VectorType(v.y), VectorType(v.z))
118 {}
119
121 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(T v)
122 {
123 for (int i = 0; i < Size; ++i)
124 m_values[i] = v;
125 return (*this);
126 }
127
128 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(const Real2x2& v) requires(isSquare2())
129 {
130 *this = ThatClass(v);
131 return (*this);
132 }
133
134 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(const Real3x3& v) requires(isSquare3())
135 {
136 *this = ThatClass(v);
137 return (*this);
138 }
139
140 operator Real2x2() const requires(isSquare2())
141 {
142 return Real2x2(m_values[0], m_values[1]);
143 }
144
145 operator Real3x3() const requires(isSquare3())
146 {
147 return Real3x3(m_values[0], m_values[1], m_values[2]);
148 }
149
150 public:
151
153 constexpr ARCCORE_HOST_DEVICE static ThatClass zero()
154 {
155 return ThatClass();
156 }
157
159 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)
160 requires(isSquare3())
161 {
162 return ThatClass(VectorType(ax, bx, cx), VectorType(ay, by, cy), VectorType(az, bz, cz));
163 }
164
166 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)
167 requires(isSquare3())
168 {
169 return ThatClass(VectorType(ax, bx, cx), VectorType(ay, by, cy), VectorType(az, bz, cz));
170 }
171
172 public:
173
185 constexpr ARCCORE_HOST_DEVICE bool isNearlyZero() const
186 {
187 bool is_nearly_zero = true;
188 for (int i = 0; i < Size; ++i)
189 is_nearly_zero = is_nearly_zero && math::isNearlyZero(m_values[i]);
190 return is_nearly_zero;
191 }
192
194 constexpr ARCCORE_HOST_DEVICE ThatClass& operator+=(const ThatClass& b)
195 {
196 for (int i = 0; i < Size; ++i)
197 m_values[i] += b.m_values[i];
198 return (*this);
199 }
200
201 constexpr ARCCORE_HOST_DEVICE ThatClass& operator-=(const ThatClass& b)
202 {
203 for (int i = 0; i < Size; ++i)
204 m_values[i] -= b.m_values[i];
205 return (*this);
206 }
207
208 constexpr ARCCORE_HOST_DEVICE ThatClass& operator*=(T b)
209 {
210 for (int i = 0; i < Size; ++i)
211 m_values[i] *= b;
212 return (*this);
213 }
214
215 constexpr ARCCORE_HOST_DEVICE ThatClass& operator/=(T b)
216 {
217 for (int i = 0; i < Size; ++i)
218 m_values[i] *= b;
219 return (*this);
220 }
221
222 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator+(const ThatClass& a, const ThatClass& b)
223 {
224 ThatClass v;
225 for (int i = 0; i < Size; ++i)
226 v.m_values[i] = a.m_values[i] + b.m_values[i];
227 return v;
228 }
229
230 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator-(const ThatClass& a, const ThatClass& b)
231 {
232 ThatClass v;
233 for (int i = 0; i < Size; ++i)
234 v.m_values[i] = a.m_values[i] - b.m_values[i];
235 return v;
236 }
237
238 constexpr ARCCORE_HOST_DEVICE ThatClass operator-() const
239 {
240 ThatClass v;
241 for (int i = 0; i < Size; ++i)
242 v.m_values[i] = -m_values[i];
243 return v;
244 }
245
247 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator*(DataType a, const ThatClass& mat)
248 {
249 ThatClass v;
250 for (int i = 0; i < Size; ++i)
251 v.m_values[i] = a * mat.m_values[i];
252 return v;
253 }
254
255 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator*(const ThatClass& mat, DataType b)
256 {
257 ThatClass v;
258 for (int i = 0; i < Size; ++i)
259 v.m_values[i] = mat.m_values[i] * b;
260 return v;
261 }
262
263 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator/(const ThatClass& mat, DataType b)
264 {
265 ThatClass v;
266 for (int i = 0; i < Size; ++i)
267 v.m_values[i] = mat.m_values[i] / b;
268 return v;
269 }
270
277 friend constexpr ARCCORE_HOST_DEVICE bool operator==(const ThatClass& a, const ThatClass& b)
278 {
279 for (int i = 0; i < Size; ++i)
280 if (a.m_values[i] != b.m_values[i])
281 return false;
282 return true;
283 }
284
291 friend constexpr ARCCORE_HOST_DEVICE bool operator!=(const ThatClass& a, const ThatClass& b)
292 {
293 return !(a == b);
294 }
295
296 public:
297
298 // Retrieves the i-th row
299 constexpr ARCCORE_HOST_DEVICE VectorType operator()(Int32 i) const
300 {
301 ARCCORE_CHECK_AT(i, RowSize);
302 return m_values[i];
303 }
304
305 // Retrieves the i-th row
306 constexpr ARCCORE_HOST_DEVICE VectorType operator[](Int32 i) const
307 {
308 ARCCORE_CHECK_AT(i, RowSize);
309 return m_values[i];
310 }
311
312 // Retrieves a reference to the value of the i-th row and j-th column
313 constexpr ARCCORE_HOST_DEVICE T& operator()(Int32 i, Int32 j)
314 {
315 ARCCORE_CHECK_AT(i, RowSize);
316 ARCCORE_CHECK_AT(j, ColumnSize);
317 return m_values[i](j);
318 }
319
320 // Retrieves the value of the i-th row and j-th column
321 constexpr ARCCORE_HOST_DEVICE T operator()(Int32 i, Int32 j) const
322 {
323 ARCCORE_CHECK_AT(i, RowSize);
324 ARCCORE_CHECK_AT(j, ColumnSize);
325 return m_values[i](j);
326 }
327
329 constexpr ARCCORE_HOST_DEVICE void setLine(Int32 i, const VectorType& v)
330 {
331 ARCCORE_CHECK_AT(i, RowSize);
332 m_values[i] = v;
333 }
334
335 public:
336
337 VectorType& vx() requires(RowSize >= 1)
338 {
339 return m_values[0];
340 }
341
342 VectorType vx() const requires(RowSize >= 1)
343 {
344 return m_values[0];
345 }
346
347 VectorType& vy() requires(RowSize >= 2)
348 {
349 return m_values[1];
350 }
351
352 VectorType vy() const requires(RowSize >= 2)
353 {
354 return m_values[1];
355 }
356
357 VectorType& vz() requires(RowSize >= 3)
358 {
359 return m_values[2];
360 }
361
362 VectorType vz() const requires(RowSize >= 3)
363 {
364 return m_values[2];
365 }
366
367 private:
368
369 VectorType m_values[RowSize] = {};
370
371 private:
372
378 constexpr ARCCORE_HOST_DEVICE static bool _eq(T a, T b)
379 {
380 return TypeEqualT<T>::isEqual(a, b);
381 }
382};
383
384/*---------------------------------------------------------------------------*/
385/*---------------------------------------------------------------------------*/
386
387} // End namespace Arcane
388
389/*---------------------------------------------------------------------------*/
390/*---------------------------------------------------------------------------*/
391
392#endif
Small fixed-size matrix containing RowSize rows and ColumnSize columns.
Definition NumMatrix.h:43
constexpr __host__ static __device__ bool _eq(T a, T b)
Compares the values of a and b using the TypeEqualT comparator.
Definition NumMatrix.h:378
friend constexpr __host__ __device__ ThatClass operator/(const ThatClass &mat, DataType b)
Division by a scalar.
Definition NumMatrix.h:263
friend constexpr __host__ __device__ bool operator!=(const ThatClass &a, const ThatClass &b)
Compares two triplets. For the notion of equality, see operator==().
Definition NumMatrix.h:291
constexpr __host__ __device__ ThatClass & operator+=(const ThatClass &b)
Adds b to the triplet.
Definition NumMatrix.h:194
constexpr __host__ static __device__ ThatClass zero()
Constructs the zero matrix.
Definition NumMatrix.h:153
constexpr __host__ __device__ bool isNearlyZero() const
Compares the matrix with the zero matrix.
Definition NumMatrix.h:185
constexpr __host__ __device__ ThatClass operator-() const
Creates a tensor opposite to the current tensor.
Definition NumMatrix.h:238
constexpr __host__ __device__ ThatClass & operator/=(T b)
Divides each component of the matrix by the real number b.
Definition NumMatrix.h:215
constexpr __host__ __device__ ThatClass & operator-=(const ThatClass &b)
Subtracts b from the triplet.
Definition NumMatrix.h:201
constexpr __host__ static __device__ ThatClass fromLines(T ax, T bx, T cx, T ay, T by, T cy, T az, T bz, T cz)
Constructs the matrix ((ax,bx,cx), (ay,by,cy), (az,bz,cz)).
Definition NumMatrix.h:166
friend constexpr __host__ __device__ bool operator==(const ThatClass &a, const ThatClass &b)
Compares the current instance component by component to b.
Definition NumMatrix.h:277
NumMatrix()=default
Constructs the matrix with all coefficients zero.
friend constexpr __host__ __device__ ThatClass operator-(const ThatClass &a, const ThatClass &b)
Creates a triplet that equals a subtracted from this triplet.
Definition NumMatrix.h:230
constexpr __host__ __device__ NumMatrix(const VectorType &ax, const VectorType &ay)
Constructs the matrix with rows (ax, ay).
Definition NumMatrix.h:65
constexpr __host__ __device__ ThatClass & operator*=(T b)
Multiplies each component of the matrix by the real number b.
Definition NumMatrix.h:208
constexpr __host__ __device__ NumMatrix(const VectorType &ax, const VectorType &ay, const VectorType &az)
Constructs the matrix with rows (ax, ay, az).
Definition NumMatrix.h:73
constexpr __host__ __device__ void setLine(Int32 i, const VectorType &v)
Sets the value of the i-th row to v.
Definition NumMatrix.h:329
constexpr __host__ __device__ NumMatrix(const VectorType &a1, const VectorType &a2, const VectorType &a3, const VectorType &a4, const VectorType &a5)
Constructs the matrix with rows (a1, a2, a3, a4, a5).
Definition NumMatrix.h:93
constexpr __host__ static __device__ ThatClass fromColumns(T ax, T ay, T az, T bx, T by, T bz, T cx, T cy, T cz)
Constructs the matrix ((ax,bx,cx), (ay,by,cy), (az,bz,cz)).
Definition NumMatrix.h:159
constexpr __host__ __device__ NumMatrix(const VectorType &a1, const VectorType &a2, const VectorType &a3, const VectorType &a4)
Constructs the matrix with rows (a1, a2, a3, a4).
Definition NumMatrix.h:82
friend constexpr __host__ __device__ ThatClass operator*(const ThatClass &mat, DataType b)
Multiplication by a scalar.
Definition NumMatrix.h:255
constexpr __host__ __device__ ThatClass & operator=(T v)
Assigns the triplet (v, v, v) to the instance.
Definition NumMatrix.h:121
friend constexpr __host__ __device__ ThatClass operator*(DataType a, const ThatClass &mat)
Multiplication by a scalar.
Definition NumMatrix.h:247
constexpr __host__ __device__ NumMatrix(T v)
Constructs the instance with the triplet (v, v, v).
Definition NumMatrix.h:106
friend constexpr __host__ __device__ ThatClass operator+(const ThatClass &a, const ThatClass &b)
Creates a triplet that equals this triplet added to b.
Definition NumMatrix.h:222
Small fixed-size vector of N numerical data points.
Definition NumVector.h:44
T m_values[Size]
Vector values.
Definition NumVector.h:366
Class managing a 2x2 matrix of reals.
Definition Real2x2.h:55
constexpr __host__ static __device__ bool isEqual(const T &a, const T &b)
Compares a to b.
Definition Numeric.h:94
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int32_t Int32
Signed integer type of 32 bits.