12#ifndef ARCANE_UTILS_NUMVECTOR_H
13#define ARCANE_UTILS_NUMVECTOR_H
17#include "arcane/utils/Real2.h"
18#include "arcane/utils/Real3.h"
41template <
typename T,
int Size>
44 static_assert(Size > 1,
"Size has to be strictly greater than 1");
45 static_assert(std::is_same_v<T, Real>,
"Only type 'Real' is allowed");
58 constexpr ARCCORE_HOST_DEVICE
NumVector(T ax, T ay)
requires(Size == 2)
66 constexpr ARCCORE_HOST_DEVICE
NumVector(T ax, T ay, T az)
requires(Size == 3)
75 constexpr ARCCORE_HOST_DEVICE
NumVector(T a1, T a2, T a3, T a4)
requires(Size == 4)
85 constexpr ARCCORE_HOST_DEVICE
NumVector(T a1, T a2, T a3, T a4, T a5)
requires(Size == 5)
95 template <
bool = true>
96 explicit constexpr ARCCORE_HOST_DEVICE
NumVector(
const T (&v)[Size])
98 for (
int i = 0; i < Size; ++i)
103 explicit constexpr ARCCORE_HOST_DEVICE
NumVector(std::array<T, Size> v)
105 for (
int i = 0; i < Size; ++i)
112 for (
int i = 0; i < Size; ++i)
116 explicit constexpr ARCCORE_HOST_DEVICE
NumVector(
Real2 v)
requires(Size == 2)
120 explicit constexpr ARCCORE_HOST_DEVICE
NumVector(Real3 v)
requires(Size == 3)
127 for (
int i = 0; i < Size; ++i)
132 constexpr ARCCORE_HOST_DEVICE ThatClass&
operator=(
const Real2& v)
requires(Size == 2)
134 *
this = ThatClass(v);
138 constexpr ARCCORE_HOST_DEVICE ThatClass&
operator=(
const Real3& v)
requires(Size == 3)
140 *
this = ThatClass(v);
144 operator Real2() const requires(Size == 2) {
return Real2(m_values[0], m_values[1]); }
146 operator Real3() const requires(Size == 3) {
return Real3(m_values[0], m_values[1], m_values[2]); }
150 constexpr ARCCORE_HOST_DEVICE
static ThatClass zero() {
return ThatClass(); }
154 constexpr ARCCORE_HOST_DEVICE
bool isNearlyZero()
const
156 bool is_nearly_zero =
true;
157 for (
int i = 0; i < Size; ++i)
158 is_nearly_zero = is_nearly_zero && math::isNearlyZero(m_values[i]);
159 return is_nearly_zero;
166 for (
int i = 0; i < Size; ++i)
167 v += m_values[i] * m_values[i];
178 for (
int i = 0; i < Size; ++i)
179 v.m_values[i] = math::abs(m_values[i]);
184 ARCCORE_HOST_DEVICE
void fill(
const T& v)
186 for (
int i = 0; i < Size; ++i) {
194 for (
int i = 0; i < Size; ++i)
200 constexpr ARCCORE_HOST_DEVICE ThatClass&
operator+=(
const ThatClass& b)
202 for (
int i = 0; i < Size; ++i)
203 m_values[i] += b.m_values[i];
210 for (
int i = 0; i < Size; ++i)
216 constexpr ARCCORE_HOST_DEVICE ThatClass&
operator-=(
const ThatClass& b)
218 for (
int i = 0; i < Size; ++i)
219 m_values[i] -= b.m_values[i];
226 for (
int i = 0; i < Size; ++i)
234 for (
int i = 0; i < Size; ++i)
240 friend constexpr ARCCORE_HOST_DEVICE ThatClass
operator+(
const ThatClass& a,
const ThatClass& b)
243 for (
int i = 0; i < Size; ++i)
244 v.m_values[i] = a.m_values[i] + b.m_values[i];
249 friend constexpr ARCCORE_HOST_DEVICE ThatClass
operator-(
const ThatClass& a,
const ThatClass& b)
252 for (
int i = 0; i < Size; ++i)
253 v.m_values[i] = a.m_values[i] - b.m_values[i];
258 constexpr ARCCORE_HOST_DEVICE ThatClass
operator-()
const
261 for (
int i = 0; i < Size; ++i)
262 v.m_values[i] = -m_values[i];
267 friend constexpr ARCCORE_HOST_DEVICE ThatClass
operator*(T a,
const ThatClass& vec)
270 for (
int i = 0; i < Size; ++i)
271 v.m_values[i] = a * vec.m_values[i];
276 friend constexpr ARCCORE_HOST_DEVICE ThatClass
operator*(
const ThatClass& vec, T b)
279 for (
int i = 0; i < Size; ++i)
280 v.m_values[i] = vec.m_values[i] * b;
285 friend constexpr ARCCORE_HOST_DEVICE ThatClass
operator/(
const ThatClass& vec, T b)
288 for (
int i = 0; i < Size; ++i)
289 v.m_values[i] = vec.m_values[i] / b;
299 friend constexpr ARCCORE_HOST_DEVICE
bool operator==(
const ThatClass& a,
const ThatClass& b)
301 for (
int i = 0; i < Size; ++i)
302 if (!_eq(a.m_values[i], b.m_values[i]))
309 for (
int i = 0; i < Size; ++i) {
321 friend constexpr ARCCORE_HOST_DEVICE
bool operator!=(
const ThatClass& a,
const ThatClass& b)
326 constexpr ARCCORE_HOST_DEVICE T& operator()(
Int32 i)
328 ARCCORE_CHECK_AT(i, Size);
331 constexpr ARCCORE_HOST_DEVICE T operator()(Int32 i)
const
333 ARCCORE_CHECK_AT(i, Size);
336 constexpr ARCCORE_HOST_DEVICE T& operator[](
Int32 i)
338 ARCCORE_CHECK_AT(i, Size);
341 constexpr ARCCORE_HOST_DEVICE T operator[](
Int32 i)
const
343 ARCCORE_CHECK_AT(i, Size);
348 T&
vx()
requires(Size >= 1)
353 T
vx() const requires(Size >= 1)
359 T&
vy()
requires(Size >= 2)
364 T
vy() const requires(Size >= 2)
370 T&
vz()
requires(Size >= 3)
375 T
vz() const requires(Size >= 3)
383 T m_values[Size] = {};
392 constexpr ARCCORE_HOST_DEVICE
static bool
399 ARCCORE_HOST_DEVICE
static T _sqrt(T a)
Small fixed-size vector of N numerical data points.
T & vy()
Value of the second component.
constexpr __host__ __device__ NumVector(T ax, T ay, T az)
Constructs with the triplet (ax,ay,az).
T vz() const
Value of the third component.
constexpr __host__ __device__ ThatClass & operator=(Real v)
Assigns the triplet (v,v,v) to the instance.
constexpr __host__ __device__ ThatClass & operator*=(T b)
Multiplies each component by b.
friend constexpr __host__ __device__ bool operator!=(const ThatClass &a, const ThatClass &b)
Compares two vectors For the notion of equality, see operator==().
T vy() const
Value of the second component.
constexpr __host__ __device__ NumVector(T a1, T a2, T a3, T a4, T a5)
Constructs with the quintuplet (a1,a2,a3,a4,a5).
__host__ __device__ ThatClass absolute() const
Absolute value component by component.
NumVector()=default
Constructs the zero vector.
constexpr __host__ __device__ ThatClass & operator/=(T b)
Divides each component by b.
T & vx()
Value of the first component.
friend constexpr __host__ __device__ bool operator==(const ThatClass &a, const ThatClass &b)
Compares the current instance component by component to b.
constexpr __host__ __device__ ThatClass & operator-=(const ThatClass &b)
Subtracts b from the instance.
friend constexpr __host__ __device__ ThatClass operator-(const ThatClass &a, const ThatClass &b)
Creates a triplet that equals b subtracted from this triplet.
friend constexpr __host__ __device__ ThatClass operator*(const ThatClass &vec, T b)
Multiplication by a scalar.
constexpr __host__ __device__ NumVector(T a1, T a2, T a3, T a4)
Constructs with the quadruplet (a1,a2,a3,a4).
constexpr __host__ __device__ NumVector(T v)
Constructs the instance with the value v for each component.
constexpr __host__ __device__ ThatClass & operator-=(T b)
Subtracts b from each component of the instance.
constexpr __host__ __device__ NumVector(std::array< T, Size > v)
Constructs the instance with the value v for each component.
friend constexpr __host__ __device__ ThatClass operator/(const ThatClass &vec, T b)
Division by a scalar.
T vx() const
Value of the first component.
__host__ __device__ void fill(const T &v)
Fill the vector with the value v.
constexpr __host__ __device__ NumVector(T ax, T ay)
Constructs with the pair (ax,ay).
constexpr __host__ __device__ ThatClass & operator+=(T b)
Adds b to each component of the instance.
constexpr __host__ __device__ Real squareNormL2() const
Returns the square of the L2 norm of the triplet .
constexpr __host__ __device__ NumVector(const T(&v)[Size])
Constructs the instance with the value v for each component.
friend constexpr __host__ __device__ ThatClass operator*(T a, const ThatClass &vec)
Multiplication by a scalar.
T & vz()
Value of the third component.
constexpr __host__ __device__ ThatClass & operator+=(const ThatClass &b)
Adds b to the instance.
__host__ __device__ Real normL2() const
Returns the L2 norm of the triplet .
constexpr __host__ __device__ ThatClass operator-() const
Creates a triplet opposite to the current triplet.
friend constexpr __host__ __device__ ThatClass operator+(const ThatClass &a, const ThatClass &b)
Creates a triplet that equals this triplet added to b.
Class managing a 2-dimensional real vector.
__host__ __device__ double sqrt(double v)
Square root of v.
constexpr __host__ __device__ bool isEqual(const _Type &a, const _Type &b)
Tests the bit-by-bit equality between two values.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
double Real
Type representing a real number.
std::ostream & operator<<(std::ostream &ostr, eItemKind item_kind)
Output operator for a stream.
std::int32_t Int32
Signed integer type of 32 bits.