Arcane  4.1.12.0
User 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/*---------------------------------------------------------------------------*/
30
31/*!
32 * \brief Small fixed-size vector of N numerical data points.
33 *
34 * \note Currently only implemented for the Real type.
35 *
36 * \warning API is currently under definition. Do not use outside of Arcane.
37 *
38 * It is possible to access each component of the vector using 'operator[]'
39 * or 'operator()' or via the methods vx(), vy(), vz() if the dimension is
40 * sufficient (for example, vz() is only accessible if Size>=3.
41 */
42template <typename T, int Size>
44{
45 static_assert(Size > 1, "Size has to be strictly greater than 1");
46 static_assert(std::is_same_v<T, Real>, "Only type 'Real' is allowed");
47
48 public:
49
50 using ThatClass = NumVector<T, Size>;
51 using DataType = T;
52
53 public:
54
55 //! Constructs the zero vector.
56 NumVector() = default;
57
58 //! Constructs with the pair (ax,ay)
59 constexpr ARCCORE_HOST_DEVICE NumVector(T ax, T ay) requires(Size == 2)
60
61 {
62 m_values[0] = ax;
63 m_values[1] = ay;
64 }
65
66 //! Constructs with the triplet (ax,ay,az)
67 constexpr ARCCORE_HOST_DEVICE NumVector(T ax, T ay, T az) requires(Size == 3)
68
69 {
70 m_values[0] = ax;
71 m_values[1] = ay;
72 m_values[2] = az;
73 }
74
75 //! Constructs with the quadruplet (a1,a2,a3,a4)
76 constexpr ARCCORE_HOST_DEVICE NumVector(T a1, T a2, T a3, T a4) requires(Size == 4)
77
78 {
79 m_values[0] = a1;
80 m_values[1] = a2;
81 m_values[2] = a3;
82 m_values[3] = a4;
83 }
84
85 //! Constructs with the quintuplet (a1,a2,a3,a4,a5)
86 constexpr ARCCORE_HOST_DEVICE NumVector(T a1, T a2, T a3, T a4, T a5) requires(Size == 5)
87 {
88 m_values[0] = a1;
89 m_values[1] = a2;
90 m_values[2] = a3;
91 m_values[3] = a4;
92 m_values[4] = a5;
93 }
94
95 //! Constructs the instance with the value \a v for each component
96 template <bool = true>
97 explicit constexpr ARCCORE_HOST_DEVICE NumVector(const T (&v)[Size])
98 {
99 for (int i = 0; i < Size; ++i)
100 m_values[i] = v[i];
101 }
102
103 //! Constructs the instance with the value \a v for each component
104 explicit constexpr ARCCORE_HOST_DEVICE NumVector(std::array<T, Size> v)
105 {
106 for (int i = 0; i < Size; ++i)
107 m_values[i] = v[i];
108 }
109
110 //! Constructs the instance with the value \a v for each component
111 explicit constexpr ARCCORE_HOST_DEVICE NumVector(T v)
112 {
113 for (int i = 0; i < Size; ++i)
114 m_values[i] = v;
115 }
116
117 explicit constexpr ARCCORE_HOST_DEVICE NumVector(Real2 v) requires(Size == 2)
118 : NumVector(v.x, v.y)
119 {}
120
121 explicit constexpr ARCCORE_HOST_DEVICE NumVector(Real3 v) requires(Size == 3)
122 : NumVector(v.x, v.y, v.z)
123 {}
124
125 //! Assigns the triplet (v,v,v) to the instance.
126 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(Real v)
127 {
128 for (int i = 0; i < Size; ++i)
129 m_values[i] = v;
130 return (*this);
131 }
132
133 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(const Real2& v) requires(Size == 2)
134 {
135 *this = ThatClass(v);
136 return (*this);
137 }
138
139 constexpr ARCCORE_HOST_DEVICE ThatClass& operator=(const Real3& v) requires(Size == 3)
140 {
141 *this = ThatClass(v);
142 return (*this);
143 }
144
145 operator Real2() const requires(Size == 2) { return Real2(m_values[0], m_values[1]); }
146
147 operator Real3() const requires(Size == 3) { return Real3(m_values[0], m_values[1], m_values[2]); }
148
149 public:
150
151 constexpr ARCCORE_HOST_DEVICE static ThatClass zero() { return ThatClass(); }
152
153 public:
154
155 constexpr ARCCORE_HOST_DEVICE bool isNearlyZero() const
156 {
157 bool is_nearly_zero = true;
158 for (int i = 0; i < Size; ++i)
159 is_nearly_zero = is_nearly_zero && math::isNearlyZero(m_values[i]);
160 return is_nearly_zero;
161 }
162
163 //! Returns the square of the L2 norm of the triplet \f$x^2+y^2+z^2\f$
164 constexpr ARCCORE_HOST_DEVICE Real squareNormL2() const
165 {
166 T v = T();
167 for (int i = 0; i < Size; ++i)
168 v += m_values[i] * m_values[i];
169 return v;
170 }
171
172 //! Returns the L2 norm of the triplet \f$\sqrt{x^2+y^2+z^2}\f$
173 ARCCORE_HOST_DEVICE Real normL2() const { return _sqrt(squareNormL2()); }
174
175 //! Absolute value component by component.
176 ARCCORE_HOST_DEVICE ThatClass absolute() const
177 {
178 ThatClass v;
179 for (int i = 0; i < Size; ++i)
180 v.m_values[i] = math::abs(m_values[i]);
181 return v;
182 }
183
184 //! Adds \a b to each component of the instance
185 constexpr ARCCORE_HOST_DEVICE ThatClass& operator+=(T b)
186 {
187 for (int i = 0; i < Size; ++i)
188 m_values[i] += b;
189 return (*this);
190 }
191
192 //! Adds \a b to the instance
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
200 //! Subtracts \a b from each component of the instance
201 constexpr ARCCORE_HOST_DEVICE ThatClass& operator-=(T b)
202 {
203 for (int i = 0; i < Size; ++i)
204 m_values[i] -= b;
205 return (*this);
206 }
207
208 //! Subtracts \a b from the instance
209 constexpr ARCCORE_HOST_DEVICE ThatClass& operator-=(const ThatClass& b)
210 {
211 for (int i = 0; i < Size; ++i)
212 m_values[i] -= b.m_values[i];
213 return (*this);
214 }
215
216 //! Multiplies each component by \a b
217 constexpr ARCCORE_HOST_DEVICE ThatClass& operator*=(T b)
218 {
219 for (int i = 0; i < Size; ++i)
220 m_values[i] *= b;
221 return (*this);
222 }
223
224 //! Divides each component by \a b
225 constexpr ARCCORE_HOST_DEVICE ThatClass& operator/=(T b)
226 {
227 for (int i = 0; i < Size; ++i)
228 m_values[i] /= b;
229 return (*this);
230 }
231
232 //! Creates a triplet that equals this triplet added to \a b
233 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator+(const ThatClass& a, const ThatClass& b)
234 {
235 ThatClass v;
236 for (int i = 0; i < Size; ++i)
237 v.m_values[i] = a.m_values[i] + b.m_values[i];
238 return v;
239 }
240
241 //! Creates a triplet that equals \a b subtracted from this triplet
242 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator-(const ThatClass& a, const ThatClass& b)
243 {
244 ThatClass v;
245 for (int i = 0; i < Size; ++i)
246 v.m_values[i] = a.m_values[i] - b.m_values[i];
247 return v;
248 }
249
250 //! Creates a triplet opposite to the current triplet
251 constexpr ARCCORE_HOST_DEVICE ThatClass operator-() const
252 {
253 ThatClass v;
254 for (int i = 0; i < Size; ++i)
255 v.m_values[i] = -m_values[i];
256 return v;
257 }
258
259 //! Multiplication by a scalar.
260 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator*(T a, const ThatClass& vec)
261 {
262 ThatClass v;
263 for (int i = 0; i < Size; ++i)
264 v.m_values[i] = a * vec.m_values[i];
265 return v;
266 }
267
268 //! Multiplication by a scalar.
269 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator*(const ThatClass& vec, T b)
270 {
271 ThatClass v;
272 for (int i = 0; i < Size; ++i)
273 v.m_values[i] = vec.m_values[i] * b;
274 return v;
275 }
276
277 //! Division by a scalar.
278 friend constexpr ARCCORE_HOST_DEVICE ThatClass operator/(const ThatClass& vec, T b)
279 {
280 ThatClass v;
281 for (int i = 0; i < Size; ++i)
282 v.m_values[i] = vec.m_values[i] / b;
283 return v;
284 }
285
286 /*!
287 * \brief Compares the current instance component by component to \a b.
288 *
289 * \retval true if this.x==b.x and this.y==b.y and this.z==b.z.
290 * \retval false otherwise.
291 */
292 friend constexpr ARCCORE_HOST_DEVICE bool operator==(const ThatClass& a, const ThatClass& b)
293 {
294 for (int i = 0; i < Size; ++i)
295 if (!_eq(a.m_values[i], b.m_values[i]))
296 return false;
297 return true;
298 }
299
300 /*!
301 * \brief Compares two vectors
302 * For the notion of equality, see operator==()
303 */
304 friend constexpr ARCCORE_HOST_DEVICE bool operator!=(const ThatClass& a, const ThatClass& b)
305 {
306 return !(a == b);
307 }
308
309 constexpr ARCCORE_HOST_DEVICE T& operator()(Int32 i)
310 {
311 ARCCORE_CHECK_AT(i, Size);
312 return m_values[i];
313 }
314 constexpr ARCCORE_HOST_DEVICE T operator()(Int32 i) const
315 {
316 ARCCORE_CHECK_AT(i, Size);
317 return m_values[i];
318 }
319 constexpr ARCCORE_HOST_DEVICE T& operator[](Int32 i)
320 {
321 ARCCORE_CHECK_AT(i, Size);
322 return m_values[i];
323 }
324 constexpr ARCCORE_HOST_DEVICE T operator[](Int32 i) const
325 {
326 ARCCORE_CHECK_AT(i, Size);
327 return m_values[i];
328 }
329
330 //! Value of the first component
331 T& vx() requires(Size >= 1)
332 {
333 return m_values[0];
334 }
335 //! Value of the first component
336 T vx() const requires(Size >= 1)
337 {
338 return m_values[0];
339 }
340
341 //! Value of the second component
342 T& vy() requires(Size >= 2)
343 {
344 return m_values[1];
345 }
346 //! Value of the second component
347 T vy() const requires(Size >= 2)
348 {
349 return m_values[1];
350 }
351
352 //! Value of the third component
353 T& vz() requires(Size >= 3)
354 {
355 return m_values[2];
356 }
357 //! Value of the third component
358 T vz() const requires(Size >= 3)
359 {
360 return m_values[2];
361 }
362
363 private:
364
365 //! Vector values
366 T m_values[Size] = {};
367
368 private:
369
370 /*!
371 * \brief Compares the values of \a a and \a b using the TypeEqualT comparator
372 * \retval true if \a a and \a b are equal,
373 * \retval false otherwise.
374 */
375 constexpr ARCCORE_HOST_DEVICE static bool
376 _eq(T a, T b)
377 {
378 return math::isEqual(a, b);
379 }
380
381 //! Returns the square root of \a a
382 ARCCORE_HOST_DEVICE static T _sqrt(T a)
383 {
384 return math::sqrt(a);
385 }
386};
387
388/*---------------------------------------------------------------------------*/
389/*---------------------------------------------------------------------------*/
390
391} // End namespace Arcane
392
393/*---------------------------------------------------------------------------*/
394/*---------------------------------------------------------------------------*/
395
396#endif
Small fixed-size vector of N numerical data points.
Definition NumVector.h:44
T & vy()
Value of the second component.
Definition NumVector.h:342
constexpr __host__ __device__ NumVector(T ax, T ay, T az)
Constructs with the triplet (ax,ay,az).
Definition NumVector.h:67
T vz() const
Value of the third component.
Definition NumVector.h:358
constexpr __host__ __device__ ThatClass & operator=(Real v)
Assigns the triplet (v,v,v) to the instance.
Definition NumVector.h:126
constexpr __host__ __device__ ThatClass & operator*=(T b)
Multiplies each component by b.
Definition NumVector.h:217
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:304
T vy() const
Value of the second component.
Definition NumVector.h:347
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:86
__host__ __device__ ThatClass absolute() const
Absolute value component by component.
Definition NumVector.h:176
NumVector()=default
Constructs the zero vector.
constexpr __host__ __device__ ThatClass & operator/=(T b)
Divides each component by b.
Definition NumVector.h:225
T & vx()
Value of the first component.
Definition NumVector.h:331
friend constexpr __host__ __device__ bool operator==(const ThatClass &a, const ThatClass &b)
Compares the current instance component by component to b.
Definition NumVector.h:292
constexpr __host__ __device__ ThatClass & operator-=(const ThatClass &b)
Subtracts b from the instance.
Definition NumVector.h:209
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:242
friend constexpr __host__ __device__ ThatClass operator*(const ThatClass &vec, T b)
Multiplication by a scalar.
Definition NumVector.h:269
constexpr __host__ __device__ NumVector(T a1, T a2, T a3, T a4)
Constructs with the quadruplet (a1,a2,a3,a4).
Definition NumVector.h:76
constexpr __host__ __device__ NumVector(T v)
Constructs the instance with the value v for each component.
Definition NumVector.h:111
constexpr __host__ __device__ ThatClass & operator-=(T b)
Subtracts b from each component of the instance.
Definition NumVector.h:201
constexpr __host__ __device__ NumVector(std::array< T, Size > v)
Constructs the instance with the value v for each component.
Definition NumVector.h:104
friend constexpr __host__ __device__ ThatClass operator/(const ThatClass &vec, T b)
Division by a scalar.
Definition NumVector.h:278
T vx() const
Value of the first component.
Definition NumVector.h:336
constexpr __host__ __device__ NumVector(T ax, T ay)
Constructs with the pair (ax,ay).
Definition NumVector.h:59
constexpr __host__ __device__ ThatClass & operator+=(T b)
Adds b to each component of the instance.
Definition NumVector.h:185
constexpr __host__ __device__ Real squareNormL2() const
Returns the square of the L2 norm of the triplet .
Definition NumVector.h:164
constexpr __host__ __device__ NumVector(const T(&v)[Size])
Constructs the instance with the value v for each component.
Definition NumVector.h:97
friend constexpr __host__ __device__ ThatClass operator*(T a, const ThatClass &vec)
Multiplication by a scalar.
Definition NumVector.h:260
T & vz()
Value of the third component.
Definition NumVector.h:353
constexpr __host__ __device__ ThatClass & operator+=(const ThatClass &b)
Adds b to the instance.
Definition NumVector.h:193
__host__ __device__ Real normL2() const
Returns the L2 norm of the triplet .
Definition NumVector.h:173
constexpr __host__ __device__ ThatClass operator-() const
Creates a triplet opposite to the current triplet.
Definition NumVector.h:251
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:233
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.