12#ifndef ARCANE_UTILS_NUMMATRIX_H
13#define ARCANE_UTILS_NUMMATRIX_H
17#include "arcane/utils/NumVector.h"
18#include "arcane/utils/Real2x2.h"
19#include "arcane/utils/Real3x3.h"
41template <
typename T,
int RowSize,
int ColumnSize>
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");
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; }
65 constexpr ARCCORE_HOST_DEVICE
NumMatrix(
const VectorType&
ax,
const VectorType& ay)
66 requires(RowSize == 2)
73 constexpr ARCCORE_HOST_DEVICE
NumMatrix(
const VectorType&
ax,
const VectorType& ay,
const VectorType& az)
74 requires(RowSize == 3)
82 constexpr ARCCORE_HOST_DEVICE
NumMatrix(
const VectorType& a1,
const VectorType& a2,
83 const VectorType& a3,
const VectorType& a4)
84 requires(RowSize == 4)
93 constexpr ARCCORE_HOST_DEVICE
NumMatrix(
const VectorType& a1,
const VectorType& a2,
94 const VectorType& a3,
const VectorType& a4,
96 requires(RowSize == 5)
108 for (
int i = 0; i < Size; ++i)
112 explicit constexpr ARCCORE_HOST_DEVICE
NumMatrix(
Real2x2 v)
requires(isSquare2())
113 :
NumMatrix(VectorType(v.x), VectorType(v.y))
116 explicit constexpr ARCCORE_HOST_DEVICE
NumMatrix(Real3x3 v)
requires(isSquare3())
117 :
NumMatrix(VectorType(v.x), VectorType(v.y), VectorType(v.z))
123 for (
int i = 0; i < Size; ++i)
128 constexpr ARCCORE_HOST_DEVICE ThatClass&
operator=(
const Real2x2& v)
requires(isSquare2())
130 *
this = ThatClass(v);
134 constexpr ARCCORE_HOST_DEVICE ThatClass&
operator=(
const Real3x3& v)
requires(isSquare3())
136 *
this = ThatClass(v);
140 operator Real2x2() const requires(isSquare2())
142 return Real2x2(m_values[0], m_values[1]);
145 operator Real3x3() const requires(isSquare3())
147 return Real3x3(m_values[0], m_values[1], m_values[2]);
153 constexpr ARCCORE_HOST_DEVICE
static ThatClass
zero()
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())
162 return ThatClass(VectorType(
ax, bx, cx), VectorType(ay, by, cy), VectorType(az, bz, cz));
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())
169 return ThatClass(VectorType(
ax, bx, cx), VectorType(ay, by, cy), VectorType(az, bz, cz));
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;
194 constexpr ARCCORE_HOST_DEVICE ThatClass&
operator+=(
const ThatClass& b)
196 for (
int i = 0; i < Size; ++i)
197 m_values[i] += b.m_values[i];
201 constexpr ARCCORE_HOST_DEVICE ThatClass&
operator-=(
const ThatClass& b)
203 for (
int i = 0; i < Size; ++i)
204 m_values[i] -= b.m_values[i];
210 for (
int i = 0; i < Size; ++i)
217 for (
int i = 0; i < Size; ++i)
222 friend constexpr ARCCORE_HOST_DEVICE ThatClass
operator+(
const ThatClass& a,
const ThatClass& b)
225 for (
int i = 0; i < Size; ++i)
226 v.m_values[i] = a.
m_values[i] + b.m_values[i];
230 friend constexpr ARCCORE_HOST_DEVICE ThatClass
operator-(
const ThatClass& a,
const ThatClass& b)
233 for (
int i = 0; i < Size; ++i)
234 v.m_values[i] = a.
m_values[i] - b.m_values[i];
238 constexpr ARCCORE_HOST_DEVICE ThatClass
operator-()
const
241 for (
int i = 0; i < Size; ++i)
242 v.m_values[i] = -m_values[i];
247 friend constexpr ARCCORE_HOST_DEVICE ThatClass
operator*(DataType a,
const ThatClass& mat)
250 for (
int i = 0; i < Size; ++i)
251 v.m_values[i] = a * mat.m_values[i];
255 friend constexpr ARCCORE_HOST_DEVICE ThatClass
operator*(
const ThatClass& mat, DataType b)
258 for (
int i = 0; i < Size; ++i)
259 v.m_values[i] = mat.m_values[i] * b;
263 friend constexpr ARCCORE_HOST_DEVICE ThatClass
operator/(
const ThatClass& mat, DataType b)
266 for (
int i = 0; i < Size; ++i)
267 v.m_values[i] = mat.m_values[i] / b;
277 friend constexpr ARCCORE_HOST_DEVICE
bool operator==(
const ThatClass& a,
const ThatClass& b)
279 for (
int i = 0; i < Size; ++i)
280 if (a.m_values[i] != b.m_values[i])
291 friend constexpr ARCCORE_HOST_DEVICE
bool operator!=(
const ThatClass& a,
const ThatClass& b)
299 constexpr ARCCORE_HOST_DEVICE VectorType operator()(
Int32 i)
const
301 ARCCORE_CHECK_AT(i, RowSize);
306 constexpr ARCCORE_HOST_DEVICE VectorType operator[](Int32 i)
const
308 ARCCORE_CHECK_AT(i, RowSize);
313 constexpr ARCCORE_HOST_DEVICE T& operator()(
Int32 i,
Int32 j)
315 ARCCORE_CHECK_AT(i, RowSize);
316 ARCCORE_CHECK_AT(j, ColumnSize);
317 return m_values[i](j);
321 constexpr ARCCORE_HOST_DEVICE T operator()(
Int32 i,
Int32 j)
const
323 ARCCORE_CHECK_AT(i, RowSize);
324 ARCCORE_CHECK_AT(j, ColumnSize);
325 return m_values[i](j);
329 constexpr ARCCORE_HOST_DEVICE
void setLine(
Int32 i,
const VectorType& v)
331 ARCCORE_CHECK_AT(i, RowSize);
337 VectorType& vx()
requires(RowSize >= 1)
342 VectorType vx() const requires(RowSize >= 1)
347 VectorType& vy()
requires(RowSize >= 2)
352 VectorType vy() const requires(RowSize >= 2)
357 VectorType& vz()
requires(RowSize >= 3)
362 VectorType vz() const requires(RowSize >= 3)
369 VectorType m_values[RowSize] = {};
378 constexpr ARCCORE_HOST_DEVICE
static bool _eq(T a, T b)
Small fixed-size matrix containing RowSize rows and ColumnSize columns.
constexpr __host__ static __device__ bool _eq(T a, T b)
Compares the values of a and b using the TypeEqualT comparator.
friend constexpr __host__ __device__ ThatClass operator/(const ThatClass &mat, DataType b)
Division by a scalar.
friend constexpr __host__ __device__ bool operator!=(const ThatClass &a, const ThatClass &b)
Compares two triplets. For the notion of equality, see operator==().
constexpr __host__ __device__ ThatClass & operator+=(const ThatClass &b)
Adds b to the triplet.
constexpr __host__ static __device__ ThatClass zero()
Constructs the zero matrix.
constexpr __host__ __device__ bool isNearlyZero() const
Compares the matrix with the zero matrix.
constexpr __host__ __device__ ThatClass operator-() const
Creates a tensor opposite to the current tensor.
constexpr __host__ __device__ ThatClass & operator/=(T b)
Divides each component of the matrix by the real number b.
constexpr __host__ __device__ ThatClass & operator-=(const ThatClass &b)
Subtracts b from the triplet.
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)).
friend constexpr __host__ __device__ bool operator==(const ThatClass &a, const ThatClass &b)
Compares the current instance component by component to b.
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.
constexpr __host__ __device__ NumMatrix(const VectorType &ax, const VectorType &ay)
Constructs the matrix with rows (ax, ay).
constexpr __host__ __device__ ThatClass & operator*=(T b)
Multiplies each component of the matrix by the real number b.
constexpr __host__ __device__ NumMatrix(const VectorType &ax, const VectorType &ay, const VectorType &az)
Constructs the matrix with rows (ax, ay, az).
constexpr __host__ __device__ void setLine(Int32 i, const VectorType &v)
Sets the value of the i-th row to v.
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).
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)).
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).
friend constexpr __host__ __device__ ThatClass operator*(const ThatClass &mat, DataType b)
Multiplication by a scalar.
constexpr __host__ __device__ ThatClass & operator=(T v)
Assigns the triplet (v, v, v) to the instance.
friend constexpr __host__ __device__ ThatClass operator*(DataType a, const ThatClass &mat)
Multiplication by a scalar.
constexpr __host__ __device__ NumMatrix(T v)
Constructs the instance with the triplet (v, v, v).
friend constexpr __host__ __device__ ThatClass operator+(const ThatClass &a, const ThatClass &b)
Creates a triplet that equals this triplet added to b.
Small fixed-size vector of N numerical data points.
T m_values[Size]
Vector values.
Class managing a 2x2 matrix of reals.
constexpr __host__ static __device__ bool isEqual(const T &a, const T &b)
Compares a to b.
Namespace for accelerator usage.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int32_t Int32
Signed integer type of 32 bits.