Arcane  4.1.15.0
User 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/*!
30 * \brief Small fixed-size matrix containing RowSize rows and ColumnSize columns.
31 *
32 * \note Currently only implemented for the Real type.
33 *
34 * \warning API is under definition. Do not use outside of Arcane
35 *
36 * It is possible to access each vector component using 'operator[]'
37 * or 'operator()' or via the methods vx(), vy(), vz() if the dimension is
38 * sufficient (for example, vz() is only accessible if 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 //! Constructs the matrix with all coefficients zero.
61 NumMatrix() = default;
62
63 //! Constructs the matrix with rows (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 //! Constructs the matrix with rows (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 //! Constructs the matrix with rows (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 //! Constructs the matrix with rows (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 //! Constructs the instance with the 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 //! Assigns the triplet (v, v, v) to the instance.
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 //! Constructs the zero matrix
152 constexpr ARCCORE_HOST_DEVICE static ThatClass zero()
153 {
154 return ThatClass();
155 }
156
157 //! Constructs the matrix ((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 //! Constructs the matrix ((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 Compares the matrix with the zero matrix.
175 *
176 * The matrix is zero if and only if each of its components
177 * is less than a given epsilon. The epsilon value used is that
178 * of float_info<value_type>::nearlyEpsilon():
179 * \f[A=0 \Leftrightarrow |A.x|<\epsilon,|A.y|<\epsilon,|A.z|<\epsilon \f]
180 *
181 * \retval true if the matrix is equal to the zero matrix,
182 * \retval false otherwise.
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 //! Adds b to the 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 //! Subtracts b from the 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 //! Multiplies each component of the matrix by the real number 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 //! Divides each component of the matrix by the real number 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 //! Creates a triplet that equals this triplet added to 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 //! Creates a triplet that equals a subtracted from this 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 //! Creates a tensor opposite to the current tensor
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 by a scalar.
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 by a scalar.
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 by a scalar.
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 Compares the current instance component by component to b.
272 *
273 * \retval true if this.x==b.x and this.y==b.y and this.z==b.z.
274 * \retval false otherwise.
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 Compares two triplets.
286 * For the notion of equality, see operator==()
287 * \retval true if the two triplets are different,
288 * \retval false otherwise.
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 // Retrieves the i-th row
298 constexpr ARCCORE_HOST_DEVICE VectorType operator()(Int32 i) const
299 {
300 ARCCORE_CHECK_AT(i, RowSize);
301 return m_values[i];
302 }
303
304 // Retrieves the i-th row
305 constexpr ARCCORE_HOST_DEVICE VectorType operator[](Int32 i) const
306 {
307 ARCCORE_CHECK_AT(i, RowSize);
308 return m_values[i];
309 }
310
311 // Retrieves a reference to the value of the i-th row and j-th column
312 constexpr ARCCORE_HOST_DEVICE T& operator()(Int32 i, Int32 j)
313 {
314 ARCCORE_CHECK_AT(i, RowSize);
315 ARCCORE_CHECK_AT(j, ColumnSize);
316 return m_values[i](j);
317 }
318
319 // Retrieves the value of the i-th row and j-th column
320 constexpr ARCCORE_HOST_DEVICE T operator()(Int32 i, Int32 j) const
321 {
322 ARCCORE_CHECK_AT(i, RowSize);
323 ARCCORE_CHECK_AT(j, ColumnSize);
324 return m_values[i](j);
325 }
326
327 //! Sets the value of the i-th row to v
328 constexpr ARCCORE_HOST_DEVICE void setLine(Int32 i, const VectorType& v)
329 {
330 ARCCORE_CHECK_AT(i, RowSize);
331 m_values[i] = v;
332 }
333
334 //! Fill the matrix with the value \a v
335 ARCCORE_HOST_DEVICE void fill(const T& v)
336 {
337 for (int i = 0; i < Size; ++i) {
338 m_values[i].fill(v);
339 }
340 }
341
342 friend std::ostream& operator<<(std::ostream& o, const NumMatrix& t)
343 {
344 for (int i = 0; i < Size; ++i) {
345 if (i != 0)
346 o << ' ';
347 o << t.m_values[i];
348 }
349 return o;
350 }
351
352 public:
353
354 VectorType& vx() requires(RowSize >= 1)
355 {
356 return m_values[0];
357 }
358
359 VectorType vx() const requires(RowSize >= 1)
360 {
361 return m_values[0];
362 }
363
364 VectorType& vy() requires(RowSize >= 2)
365 {
366 return m_values[1];
367 }
368
369 VectorType vy() const requires(RowSize >= 2)
370 {
371 return m_values[1];
372 }
373
374 VectorType& vz() requires(RowSize >= 3)
375 {
376 return m_values[2];
377 }
378
379 VectorType vz() const requires(RowSize >= 3)
380 {
381 return m_values[2];
382 }
383
384 private:
385
386 VectorType m_values[RowSize] = {};
387
388 private:
389
390 /*!
391 * \brief Compares the values of a and b using the TypeEqualT comparator
392 * \retval true if a and b are equal,
393 * \retval false otherwise.
394 */
395 constexpr ARCCORE_HOST_DEVICE static bool _eq(T a, T b)
396 {
397 return TypeEqualT<T>::isEqual(a, b);
398 }
399};
400
401/*---------------------------------------------------------------------------*/
402/*---------------------------------------------------------------------------*/
403
404} // End namespace Arcane
405
406/*---------------------------------------------------------------------------*/
407/*---------------------------------------------------------------------------*/
408
409#endif
Small fixed-size matrix containing RowSize rows and ColumnSize columns.
Definition NumMatrix.h:42
friend constexpr __host__ __device__ ThatClass operator/(const ThatClass &mat, DataType b)
Division by a scalar.
Definition NumMatrix.h:262
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:290
constexpr __host__ __device__ ThatClass & operator+=(const ThatClass &b)
Adds b to the triplet.
Definition NumMatrix.h:193
constexpr __host__ static __device__ ThatClass zero()
Constructs the zero matrix.
Definition NumMatrix.h:152
constexpr __host__ __device__ bool isNearlyZero() const
Compares the matrix with the zero matrix.
Definition NumMatrix.h:184
constexpr __host__ __device__ ThatClass operator-() const
Creates a tensor opposite to the current tensor.
Definition NumMatrix.h:237
constexpr __host__ __device__ ThatClass & operator/=(T b)
Divides each component of the matrix by the real number b.
Definition NumMatrix.h:214
constexpr __host__ __device__ ThatClass & operator-=(const ThatClass &b)
Subtracts b from the 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)
Constructs the matrix ((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)
Compares the current instance component by component to b.
Definition NumMatrix.h:276
NumMatrix()=default
Constructs the matrix with all coefficients zero.
__host__ __device__ void fill(const T &v)
Fill the matrix with the value v.
Definition NumMatrix.h:335
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:229
constexpr __host__ __device__ NumMatrix(const VectorType &ax, const VectorType &ay)
Constructs the matrix with rows (ax, ay).
Definition NumMatrix.h:64
constexpr __host__ __device__ ThatClass & operator*=(T b)
Multiplies each component of the matrix by the real number b.
Definition NumMatrix.h:207
constexpr __host__ __device__ NumMatrix(const VectorType &ax, const VectorType &ay, const VectorType &az)
Constructs the matrix with rows (ax, ay, az).
Definition NumMatrix.h:72
constexpr __host__ __device__ void setLine(Int32 i, const VectorType &v)
Sets the value of the i-th row to v.
Definition NumMatrix.h:328
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: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)
Constructs the matrix ((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)
Constructs the matrix with rows (a1, a2, a3, a4).
Definition NumMatrix.h:81
friend constexpr __host__ __device__ ThatClass operator*(const ThatClass &mat, DataType b)
Multiplication by a scalar.
Definition NumMatrix.h:254
constexpr __host__ __device__ ThatClass & operator=(T v)
Assigns the triplet (v, v, v) to the instance.
Definition NumMatrix.h:120
friend constexpr __host__ __device__ ThatClass operator*(DataType a, const ThatClass &mat)
Multiplication by a scalar.
Definition NumMatrix.h:246
constexpr __host__ __device__ NumMatrix(T v)
Constructs the instance with the triplet (v, v, v).
Definition NumMatrix.h:105
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:221
Small fixed-size vector of N numerical data points.
Definition NumVector.h:43
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::ostream & operator<<(std::ostream &ostr, eItemKind item_kind)
Output operator for a stream.
std::int32_t Int32
Signed integer type of 32 bits.