Arcane  4.1.15.0
Developer documentation
Loading...
Searching...
No Matches
NumVector.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/* NumVector.h (C) 2000-2026 */
9/* */
10/* Fixed-size vector of numerical types. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_NUMVECTOR_H
13#define ARCANE_UTILS_NUMVECTOR_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/Real2.h"
18#include "arcane/utils/Real3.h"
19
20#include <type_traits>
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
41template <typename T, int Size>
43{
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");
46
47 public:
48
49 using ThatClass = NumVector<T, Size>;
50 using DataType = T;
51
52 public:
53
55 NumVector() = default;
56
58 constexpr ARCCORE_HOST_DEVICE NumVector(T ax, T ay) requires(Size == 2)
59
60 {
61 m_values[0] = ax;
62 m_values[1] = ay;
63 }
64
66 constexpr ARCCORE_HOST_DEVICE NumVector(T ax, T ay, T az) requires(Size == 3)
67
68 {
69 m_values[0] = ax;
70 m_values[1] = ay;
71 m_values[2] = az;
72 }
73
75 constexpr ARCCORE_HOST_DEVICE NumVector(T a1, T a2, T a3, T a4) requires(Size == 4)
76
77 {
78 m_values[0] = a1;
79 m_values[1] = a2;
80 m_values[2] = a3;
81 m_values[3] = a4;
82 }
83
85 constexpr ARCCORE_HOST_DEVICE NumVector(T a1, T a2, T a3, T a4, T a5) requires(Size == 5)
86 {
87 m_values[0] = a1;
88 m_values[1] = a2;
89 m_values[2] = a3;
90 m_values[3] = a4;
91 m_values[4] = a5;
92 }
93
95 template <bool = true>
96 explicit constexpr ARCCORE_HOST_DEVICE NumVector(const T (&v)[Size])
97 {
98 for (int i = 0; i < Size; ++i)
99 m_values[i] = v[i];
100 }
101
103 explicit constexpr ARCCORE_HOST_DEVICE NumVector(std::array<T, Size> v)
104 {
105 for (int i = 0; i < Size; ++i)
106 m_values[i] = v[i];
107 }
108
110 explicit constexpr ARCCORE_HOST_DEVICE NumVector(T v)
111 {
112 for (int i = 0; i < Size; ++i)
113 m_values[i] = v;
114 }
115
116 explicit constexpr ARCCORE_HOST_DEVICE NumVector(Real2 v) requires(Size == 2)
117 : NumVector(v.x, v.y)
118 {}
119
120 explicit constexpr ARCCORE_HOST_DEVICE NumVector(Real3 v) requires(Size == 3)
121 : NumVector(v.x, v.y, v.z)
122 {}
123
125 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(Real v)
126 {
127 for (int i = 0; i < Size; ++i)
128 m_values[i] = v;
129 return (*this);
130 }
131
132 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(const Real2& v) requires(Size == 2)
133 {
134 *this = ThatClass(v);
135 return (*this);
136 }
137
138 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(const Real3& v) requires(Size == 3)
139 {
140 *this = ThatClass(v);
141 return (*this);
142 }
143
144 operator Real2() const requires(Size == 2) { return Real2(m_values[0], m_values[1]); }
145
146 operator Real3() const requires(Size == 3) { return Real3(m_values[0], m_values[1], m_values[2]); }
147
148 public:
149
150 constexpr ARCCORE_HOST_DEVICE static ThatClass zero() { return ThatClass(); }
151
152 public:
153
154 constexpr ARCCORE_HOST_DEVICE bool isNearlyZero() const
155 {
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;
160 }
161
163 constexpr ARCCORE_HOST_DEVICE Real squareNormL2() const
164 {
165 T v = T();
166 for (int i = 0; i < Size; ++i)
167 v += m_values[i] * m_values[i];
168 return v;
169 }
170
172 ARCCORE_HOST_DEVICE Real normL2() const { return _sqrt(squareNormL2()); }
173
175 ARCCORE_HOST_DEVICE ThatClass absolute() const
176 {
177 ThatClass v;
178 for (int i = 0; i < Size; ++i)
179 v.m_values[i] = math::abs(m_values[i]);
180 return v;
181 }
182
184 ARCCORE_HOST_DEVICE void fill(const T& v)
185 {
186 for (int i = 0; i < Size; ++i) {
187 m_values[i] = v;
188 }
189 }
190
192 constexpr ARCCORE_HOST_DEVICE ThatClass& operator+=(T b)
193 {
194 for (int i = 0; i < Size; ++i)
195 m_values[i] += b;
196 return (*this);
197 }
198
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
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
216 constexpr ARCCORE_HOST_DEVICE ThatClass& operator-=(const ThatClass& b)
217 {
218 for (int i = 0; i < Size; ++i)
219 m_values[i] -= b.m_values[i];
220 return (*this);
221 }
222
224 constexpr ARCCORE_HOST_DEVICE ThatClass& operator*=(T b)
225 {
226 for (int i = 0; i < Size; ++i)
227 m_values[i] *= b;
228 return (*this);
229 }
230
232 constexpr ARCCORE_HOST_DEVICE ThatClass& operator/=(T b)
233 {
234 for (int i = 0; i < Size; ++i)
235 m_values[i] /= b;
236 return (*this);
237 }
238
240 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator+(const ThatClass& a, const ThatClass& b)
241 {
242 ThatClass v;
243 for (int i = 0; i < Size; ++i)
244 v.m_values[i] = a.m_values[i] + b.m_values[i];
245 return v;
246 }
247
249 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator-(const ThatClass& a, const ThatClass& b)
250 {
251 ThatClass v;
252 for (int i = 0; i < Size; ++i)
253 v.m_values[i] = a.m_values[i] - b.m_values[i];
254 return v;
255 }
256
258 constexpr ARCCORE_HOST_DEVICE ThatClass operator-() const
259 {
260 ThatClass v;
261 for (int i = 0; i < Size; ++i)
262 v.m_values[i] = -m_values[i];
263 return v;
264 }
265
267 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator*(T a, const ThatClass& vec)
268 {
269 ThatClass v;
270 for (int i = 0; i < Size; ++i)
271 v.m_values[i] = a * vec.m_values[i];
272 return v;
273 }
274
276 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator*(const ThatClass& vec, T b)
277 {
278 ThatClass v;
279 for (int i = 0; i < Size; ++i)
280 v.m_values[i] = vec.m_values[i] * b;
281 return v;
282 }
283
285 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator/(const ThatClass& vec, T b)
286 {
287 ThatClass v;
288 for (int i = 0; i < Size; ++i)
289 v.m_values[i] = vec.m_values[i] / b;
290 return v;
291 }
292
299 friend constexpr ARCCORE_HOST_DEVICE bool operator==(const ThatClass& a, const ThatClass& b)
300 {
301 for (int i = 0; i < Size; ++i)
302 if (!_eq(a.m_values[i], b.m_values[i]))
303 return false;
304 return true;
305 }
306
307 friend std::ostream& operator<<(std::ostream& o, const NumVector& t)
308 {
309 for (int i = 0; i < Size; ++i) {
310 if (i != 0)
311 o << ' ';
312 o << t.m_values[i];
313 }
314 return o;
315 }
316
321 friend constexpr ARCCORE_HOST_DEVICE bool operator!=(const ThatClass& a, const ThatClass& b)
322 {
323 return !(a == b);
324 }
325
326 constexpr ARCCORE_HOST_DEVICE T& operator()(Int32 i)
327 {
328 ARCCORE_CHECK_AT(i, Size);
329 return m_values[i];
330 }
331 constexpr ARCCORE_HOST_DEVICE T operator()(Int32 i) const
332 {
333 ARCCORE_CHECK_AT(i, Size);
334 return m_values[i];
335 }
336 constexpr ARCCORE_HOST_DEVICE T& operator[](Int32 i)
337 {
338 ARCCORE_CHECK_AT(i, Size);
339 return m_values[i];
340 }
341 constexpr ARCCORE_HOST_DEVICE T operator[](Int32 i) const
342 {
343 ARCCORE_CHECK_AT(i, Size);
344 return m_values[i];
345 }
346
348 T& vx() requires(Size >= 1)
349 {
350 return m_values[0];
351 }
352
353 T vx() const requires(Size >= 1)
354 {
355 return m_values[0];
356 }
357
359 T& vy() requires(Size >= 2)
360 {
361 return m_values[1];
362 }
363
364 T vy() const requires(Size >= 2)
365 {
366 return m_values[1];
367 }
368
370 T& vz() requires(Size >= 3)
371 {
372 return m_values[2];
373 }
374
375 T vz() const requires(Size >= 3)
376 {
377 return m_values[2];
378 }
379
380 private:
381
383 T m_values[Size] = {};
384
385 private:
386
392 constexpr ARCCORE_HOST_DEVICE static bool
393 _eq(T a, T b)
394 {
395 return math::isEqual(a, b);
396 }
397
399 ARCCORE_HOST_DEVICE static T _sqrt(T a)
400 {
401 return math::sqrt(a);
402 }
403};
404
405/*---------------------------------------------------------------------------*/
406/*---------------------------------------------------------------------------*/
407
408} // End namespace Arcane
409
410/*---------------------------------------------------------------------------*/
411/*---------------------------------------------------------------------------*/
412
413#endif
Small fixed-size vector of N numerical data points.
Definition NumVector.h:43
T & vy()
Value of the second component.
Definition NumVector.h:359
constexpr __host__ __device__ NumVector(T ax, T ay, T az)
Constructs with the triplet (ax,ay,az).
Definition NumVector.h:66
T vz() const
Value of the third component.
Definition NumVector.h:375
constexpr __host__ __device__ ThatClass & operator=(Real v)
Assigns the triplet (v,v,v) to the instance.
Definition NumVector.h:125
constexpr __host__ __device__ ThatClass & operator*=(T b)
Multiplies each component by b.
Definition NumVector.h:224
friend constexpr __host__ __device__ bool operator!=(const ThatClass &a, const ThatClass &b)
Compares two vectors For the notion of equality, see operator==().
Definition NumVector.h:321
T vy() const
Value of the second component.
Definition NumVector.h:364
constexpr __host__ __device__ NumVector(T a1, T a2, T a3, T a4, T a5)
Constructs with the quintuplet (a1,a2,a3,a4,a5).
Definition NumVector.h:85
__host__ __device__ ThatClass absolute() const
Absolute value component by component.
Definition NumVector.h:175
NumVector()=default
Constructs the zero vector.
constexpr __host__ __device__ ThatClass & operator/=(T b)
Divides each component by b.
Definition NumVector.h:232
T & vx()
Value of the first component.
Definition NumVector.h:348
friend constexpr __host__ __device__ bool operator==(const ThatClass &a, const ThatClass &b)
Compares the current instance component by component to b.
Definition NumVector.h:299
constexpr __host__ static __device__ bool _eq(T a, T b)
Compares the values of a and b using the TypeEqualT comparator.
Definition NumVector.h:393
constexpr __host__ __device__ ThatClass & operator-=(const ThatClass &b)
Subtracts b from the instance.
Definition NumVector.h:216
friend constexpr __host__ __device__ ThatClass operator-(const ThatClass &a, const ThatClass &b)
Creates a triplet that equals b subtracted from this triplet.
Definition NumVector.h:249
friend constexpr __host__ __device__ ThatClass operator*(const ThatClass &vec, T b)
Multiplication by a scalar.
Definition NumVector.h:276
constexpr __host__ __device__ NumVector(T a1, T a2, T a3, T a4)
Constructs with the quadruplet (a1,a2,a3,a4).
Definition NumVector.h:75
constexpr __host__ __device__ NumVector(T v)
Constructs the instance with the value v for each component.
Definition NumVector.h:110
constexpr __host__ __device__ ThatClass & operator-=(T b)
Subtracts b from each component of the instance.
Definition NumVector.h:208
constexpr __host__ __device__ NumVector(std::array< T, Size > v)
Constructs the instance with the value v for each component.
Definition NumVector.h:103
friend constexpr __host__ __device__ ThatClass operator/(const ThatClass &vec, T b)
Division by a scalar.
Definition NumVector.h:285
T vx() const
Value of the first component.
Definition NumVector.h:353
__host__ __device__ void fill(const T &v)
Fill the vector with the value v.
Definition NumVector.h:184
constexpr __host__ __device__ NumVector(T ax, T ay)
Constructs with the pair (ax,ay).
Definition NumVector.h:58
constexpr __host__ __device__ ThatClass & operator+=(T b)
Adds b to each component of the instance.
Definition NumVector.h:192
__host__ static __device__ T _sqrt(T a)
Returns the square root of a.
Definition NumVector.h:399
constexpr __host__ __device__ Real squareNormL2() const
Returns the square of the L2 norm of the triplet .
Definition NumVector.h:163
constexpr __host__ __device__ NumVector(const T(&v)[Size])
Constructs the instance with the value v for each component.
Definition NumVector.h:96
friend constexpr __host__ __device__ ThatClass operator*(T a, const ThatClass &vec)
Multiplication by a scalar.
Definition NumVector.h:267
T & vz()
Value of the third component.
Definition NumVector.h:370
constexpr __host__ __device__ ThatClass & operator+=(const ThatClass &b)
Adds b to the instance.
Definition NumVector.h:200
__host__ __device__ Real normL2() const
Returns the L2 norm of the triplet .
Definition NumVector.h:172
constexpr __host__ __device__ ThatClass operator-() const
Creates a triplet opposite to the current triplet.
Definition NumVector.h:258
friend constexpr __host__ __device__ ThatClass operator+(const ThatClass &a, const ThatClass &b)
Creates a triplet that equals this triplet added to b.
Definition NumVector.h:240
Class managing a 2-dimensional real vector.
Definition Real2.h:122
__host__ __device__ double sqrt(double v)
Square root of v.
Definition Math.h:142
constexpr __host__ __device__ bool isEqual(const _Type &a, const _Type &b)
Tests the bit-by-bit equality between two values.
Definition Numeric.h:260
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
double Real
Type representing a real number.
std::int32_t Int32
Signed integer type of 32 bits.