Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
Real2x2.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/* Real2x2.h (C) 2000-2025 */
9/* */
10/* 2x2 Matrix of 'Real'. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_REAL2X2_H
13#define ARCANE_UTILS_REAL2X2_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/Real2.h"
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Arcane
23{
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28/*!
29 * \brief POD structure for a Real2x2.
30 */
32{
33 public:
34
35 Real2POD x;
36 Real2POD y;
37};
38
39/*---------------------------------------------------------------------------*/
40/*---------------------------------------------------------------------------*/
41
42/*!
43 * \brief Class managing a 2x2 matrix of reals.
44 *
45 * The matrix comprises two components \a x and \a y which are of the
46 * type \b Real2. For example:
47 *
48 * \code
49 * Real2x2 matrix;
50 * matrix.x.y = 2.;
51 * matrix.y.y = 3.;
52 * \endcode
53 */
54class ARCANE_UTILS_EXPORT Real2x2
55{
56 public:
57
58 //! Constructs the zero matrix
59 constexpr ARCCORE_HOST_DEVICE Real2x2()
60 : x(Real2::null())
61 , y(Real2::null())
62 {}
63
64 //! Constructs the pair (ax,ay)
65 constexpr ARCCORE_HOST_DEVICE Real2x2(Real2 ax, Real2 ay)
66 : x(ax)
67 , y(ay)
68 {}
69
70 /*!
71 * \brief Constructs the pair ((ax,bx),(ay,by)).
72 * \deprecated Use Real2x2(Real2 a,Real2 b) instead.
73 */
74 ARCANE_DEPRECATED_116 Real2x2(Real ax, Real ay, Real bx, Real by)
75 : x(ax, bx)
76 , y(ay, by)
77 {}
78
79 //! Constructs a copy identical to \a f
80 Real2x2(const Real2x2& f) = default;
81
82 //! Constructs a copy identical to \a f
83 constexpr ARCCORE_HOST_DEVICE explicit Real2x2(const Real2x2POD& f)
84 : x(f.x)
85 , y(f.y)
86 {}
87
88 //! Constructs the instance with the triplet (v,v,v).
89 constexpr ARCCORE_HOST_DEVICE explicit Real2x2(Real v)
90 {
91 x = y = v;
92 }
93
94 //! Constructs the pair ((av[0], av[1]), (av[2], av[3]))
95 constexpr ARCCORE_HOST_DEVICE explicit Real2x2(ConstArrayView<Real> av)
96 : x(av[0], av[1])
97 , y(av[2], av[3])
98 {}
99
100 //! Copy assignment operator
101 Real2x2& operator=(const Real2x2& f) = default;
102
103 //! Assigns the pair (v,v,v) to the instance.
104 constexpr ARCCORE_HOST_DEVICE Real2x2& operator=(Real v)
105 {
106 x = y = v;
107 return (*this);
108 }
109
110 public:
111
112 Real2 x; //!< First component
113 Real2 y; //!< Second component
114
115 public:
116
117 //! Constructs the zero matrix
118 constexpr ARCCORE_HOST_DEVICE static Real2x2 null() { return Real2x2(); }
119
120 //! Constructs the pair ((ax,bx),(ay,by)).
121 constexpr ARCCORE_HOST_DEVICE static Real2x2 fromColumns(Real ax, Real ay, Real bx, Real by)
122 {
123 return Real2x2(Real2(ax, bx), Real2(ay, by));
124 }
125
126 //! Constructs the pair ((ax,bx),(ay,by)).
127 constexpr ARCCORE_HOST_DEVICE static Real2x2 fromLines(Real ax, Real bx, Real ay, Real by)
128 {
129 return Real2x2(Real2(ax, bx), Real2(ay, by));
130 }
131
132 public:
133
134 //! Returns a copy of the pair.
135 constexpr ARCCORE_HOST_DEVICE Real2x2 copy() const { return (*this); }
136
137 //! Resets the pair with default constructors.
138 constexpr ARCCORE_HOST_DEVICE Real2x2& reset()
139 {
140 *this = null();
141 return (*this);
142 }
143
144 //! Assigns the pair (ax,ay) to the instance
145 constexpr ARCCORE_HOST_DEVICE Real2x2& assign(Real2 ax, Real2 ay)
146 {
147 x = ax;
148 y = ay;
149 return (*this);
150 }
151
152 //! Copies the pair \a f
153 constexpr ARCCORE_HOST_DEVICE Real2x2& assign(Real2x2 f)
154 {
155 x = f.x;
156 y = f.y;
157 return (*this);
158 }
159
160 //! Returns a view of the four elements of the matrix.
161 //! [x.x, x.y, y.x, y.y]
162 constexpr ARCCORE_HOST_DEVICE ArrayView<Real> view()
163 {
164 return { 4, &x.x };
165 }
166
167 //! Returns a constant view of the four elements of the matrix.
168 //! [x.x, x.y, y.x, y.y]
169 constexpr ARCCORE_HOST_DEVICE ConstArrayView<Real> constView() const
170 {
171 return { 4, &x.x };
172 }
173
174 /*!
175 * \brief Reads the matrix from the stream \a i
176 * The matrix is read in the form of three Real2.
177 */
178 std::istream& assign(std::istream& i);
179
180 //! Writes the pair to the stream \a o readable by an assign()
181 std::ostream& print(std::ostream& o) const;
182
183 //! Writes the pair to the stream \a o in the form (x,y,z)
184 std::ostream& printXy(std::ostream& o) const;
185
186 //! Adds \a b to the pair
187 constexpr ARCCORE_HOST_DEVICE Real2x2& add(Real2x2 b)
188 {
189 x += b.x;
190 y += b.y;
191 return (*this);
192 }
193
194 //! Subtracts \a b from the pair
195 constexpr ARCCORE_HOST_DEVICE Real2x2& sub(Real2x2 b)
196 {
197 x -= b.x;
198 y -= b.y;
199 return (*this);
200 }
201
202 //! Multiplies each component of the pair by the corresponding component of \a b
203 //Real2x2& mul(Real2x2 b) { x*=b.x; y*=b.y; return (*this); }
204
205 //! Divides each component of the pair by the corresponding component of \a b
206 constexpr ARCCORE_HOST_DEVICE Real2x2& div(Real2x2 b)
207 {
208 x /= b.x;
209 y /= b.y;
210 return (*this);
211 }
212
213 //! Adds \a b to each component of the pair
214 constexpr ARCCORE_HOST_DEVICE Real2x2& addSame(Real2 b)
215 {
216 x += b;
217 y += b;
218 return (*this);
219 }
220
221 //! Subtracts \a b from each component of the pair
222 constexpr ARCCORE_HOST_DEVICE Real2x2& subSame(Real2 b)
223 {
224 x -= b;
225 y -= b;
226 return (*this);
227 }
228
229 //! Multiplies each component of the pair by \a b
230 constexpr ARCCORE_HOST_DEVICE Real2x2& mulSame(Real2 b)
231 {
232 x *= b;
233 y *= b;
234 return (*this);
235 }
236
237 //! Divides each component of the pair by \a b
238 constexpr ARCCORE_HOST_DEVICE Real2x2& divSame(Real2 b)
239 {
240 x /= b;
241 y /= b;
242 return (*this);
243 }
244
245 //! Adds \a b to the pair.
246 constexpr ARCCORE_HOST_DEVICE Real2x2& operator+=(Real2x2 b) { return add(b); }
247
248 //! Subtracts \a b from the pair
249 constexpr ARCCORE_HOST_DEVICE Real2x2& operator-=(Real2x2 b) { return sub(b); }
250
251 //! Multiplies each component of the pair by the corresponding component of \a b
252 //Real2x2& operator*=(Real2x2 b) { return mul(b); }
253
254 //! Multiplies each component of the matrix by the real \a b
255 constexpr ARCCORE_HOST_DEVICE void operator*=(Real b)
256 {
257 x *= b;
258 y *= b;
259 }
260
261 //! Divides each component of the pair by the corresponding component of \a b
262 //Real2x2& operator/= (Real2x2 b) { return div(b); }
263
264 //! Divides each component of the matrix by the real \a b
265 constexpr ARCCORE_HOST_DEVICE void operator/=(Real b)
266 {
267 x /= b;
268 y /= b;
269 }
270
271 //! Creates a pair that equals this pair added to \a b
272 constexpr ARCCORE_HOST_DEVICE Real2x2 operator+(Real2x2 b) const { return Real2x2(x + b.x, y + b.y); }
273
274 //! Creates a pair that equals \a b subtracted from this pair
275 constexpr ARCCORE_HOST_DEVICE Real2x2 operator-(Real2x2 b) const { return Real2x2(x - b.x, y - b.y); }
276
277 //! Creates an inverse tensor of the current tensor
278 constexpr ARCCORE_HOST_DEVICE Real2x2 operator-() const { return Real2x2(-x, -y); }
279
280 /*!
281 * \brief Compares component by component the current instance to \a b.
282 *
283 * \retval true if this.x==b.x and this.y==b.y.
284 * \retval false otherwise.
285 */
286 constexpr ARCCORE_HOST_DEVICE bool operator==(Real2x2 b) const
287 {
288 return (x == b.x) && (y == b.y);
289 }
290
291 /*!
292 * \brief Compares two pairs.
293 * For the notion of equality, see operator==()
294 * \retval true if the two pairs are different,
295 * \retval false otherwise.
296 */
297 constexpr ARCCORE_HOST_DEVICE bool operator!=(Real2x2 b) const
298 {
299 return !operator==(b);
300 }
301
302 /*!
303 * \brief Read-only access to the \a i-th (between 0 and 1 inclusive) row of the instance.
304 * \param i row number to return
305 */
306 ARCCORE_HOST_DEVICE Real2 operator[](Integer i) const
307 {
308 ARCCORE_CHECK_AT(i, 2);
309 return (&x)[i];
310 }
311
312 /*!
313 * \brief Read-only access to the \a i-th (between 0 and 1 inclusive) row of the instance.
314 * \param i row number to return
315 */
316 ARCCORE_HOST_DEVICE Real2 operator()(Integer i) const
317 {
318 ARCCORE_CHECK_AT(i, 2);
319 return (&x)[i];
320 }
321
322 /*!
323 * \brief Read-only access to the \a i-th row and \a j-th column.
324 * \param i row number to return
325 * \param j column number to return
326 */
327 ARCCORE_HOST_DEVICE Real operator()(Integer i, Integer j) const
328 {
329 ARCCORE_CHECK_AT(i, 2);
330 ARCCORE_CHECK_AT(j, 2);
331 return (&x)[i][j];
332 }
333
334 /*!
335 * \brief Access to the \a i-th row (between 0 and 1 inclusive) of the instance.
336 * \param i row number to return
337 */
338 ARCCORE_HOST_DEVICE Real2& operator[](Integer i)
339 {
340 ARCCORE_CHECK_AT(i, 2);
341 return (&x)[i];
342 }
343
344 /*!
345 * \brief Access to the \a i-th row (between 0 and 1 inclusive) of the instance.
346 * \param i row number to return
347 */
348 ARCCORE_HOST_DEVICE Real2& operator()(Integer i)
349 {
350 ARCCORE_CHECK_AT(i, 2);
351 return (&x)[i];
352 }
353
354 /*!
355 * \brief Access to the \a i-th row and \a j-th column.
356 * \param i row number to return
357 * \param j column number to return
358 */
359 ARCCORE_HOST_DEVICE Real& operator()(Integer i, Integer j)
360 {
361 ARCCORE_CHECK_AT(i, 2);
362 ARCCORE_CHECK_AT(j, 2);
363 return (&x)[i][j];
364 }
365
366 public:
367
368 //! Writes the pair \a t to the stream \a o
369 friend std::ostream& operator<<(std::ostream& o, Real2x2 t)
370 {
371 return t.printXy(o);
372 }
373
374 //! Reads the pair \a t from the stream \a o.
375 friend std::istream& operator>>(std::istream& i, Real2x2& t)
376 {
377 return t.assign(i);
378 }
379
380 //! Multiplication by a scalar.
381 friend constexpr ARCCORE_HOST_DEVICE Real2x2 operator*(Real sca, Real2x2 vec)
382 {
383 return Real2x2(vec.x * sca, vec.y * sca);
384 }
385
386 //! Multiplication by a scalar.
387 friend constexpr ARCCORE_HOST_DEVICE Real2x2 operator*(Real2x2 vec, Real sca)
388 {
389 return Real2x2(vec.x * sca, vec.y * sca);
390 }
391
392 //! Division by a scalar.
393 friend constexpr ARCCORE_HOST_DEVICE Real2x2 operator/(Real2x2 vec, Real sca)
394 {
395 return Real2x2(vec.x / sca, vec.y / sca);
396 }
397
398 /*!
399 * \brief Comparison operator.
400 *
401 * This operator allows sorting Real2x2 for example
402 * in std::set
403 */
404 friend constexpr ARCCORE_HOST_DEVICE bool operator<(Real2x2 v1, Real2x2 v2)
405 {
406 if (v1.x == v2.x) {
407 return v1.y < v2.y;
408 }
409 return (v1.x < v2.x);
410 }
411
412 public:
413
414 /*!
415 * \brief Compares the matrix with the zero matrix.
416 *
417 * The matrix is zero if and only if each of its components
418 * is less than a given epsilon. The epsilon value used is that
419 * of float_info<value_type>::nearlyEpsilon():
420 * \f[A=0 \Leftrightarrow |A.x|<\epsilon,|A.y|<\epsilon\f]
421 *
422 * \retval true if the matrix is equal to the zero matrix,
423 * \retval false otherwise.
424 */
425 // TODO: make obsolete mid-2025: ARCANE_DEPRECATED_REASON("Y2024: Use math::isNearlyZero(const Real2x2&) instead")
426 inline constexpr ARCCORE_HOST_DEVICE bool isNearlyZero() const;
427
428 private:
429
430 /*!
431 * \brief Compares the values of \a a and \a b with the TypeEqualT comparator
432 * \retval true if \a a and \a b are equal,
433 * \retval false otherwise.
434 */
435 constexpr ARCCORE_HOST_DEVICE static bool _eq(Real a, Real b)
436 {
437 return TypeEqualT<Real>::isEqual(a, b);
438 }
439};
440
441/*---------------------------------------------------------------------------*/
442/*---------------------------------------------------------------------------*/
443
444namespace math
445{
446 /*!
447 * \brief Compares the matrix with the zero matrix.
448 *
449 * The matrix is zero if and only if each of its components
450 * is less than a given epsilon. The epsilon value used is that
451 * of float_info<value_type>::nearlyEpsilon():
452 * \f[A=0 \Leftrightarrow |A.x|<\epsilon,|A.y|<\epsilon\f]
453 *
454 * \retval true if the matrix is equal to the zero matrix,
455 * \retval false otherwise.
456 */
457 constexpr ARCCORE_HOST_DEVICE bool isNearlyZero(const Real2x2& v)
458 {
459 return math::isNearlyZero(v.x) && math::isNearlyZero(v.y);
460 }
461} // namespace math
462
463/*---------------------------------------------------------------------------*/
464/*---------------------------------------------------------------------------*/
465
466inline constexpr ARCCORE_HOST_DEVICE bool Real2x2::
467isNearlyZero() const
468{
469 return math::isNearlyZero(*this);
470}
471
472/*---------------------------------------------------------------------------*/
473/*---------------------------------------------------------------------------*/
474
475} // End namespace Arcane
476
477/*---------------------------------------------------------------------------*/
478/*---------------------------------------------------------------------------*/
479
480#endif
Modifiable view of an array of type T.
Constant view of an array of type T.
Class managing a 2-dimensional real vector.
Definition Real2.h:122
Class managing a 2x2 matrix of reals.
Definition Real2x2.h:55
friend constexpr __host__ __device__ Real2x2 operator/(Real2x2 vec, Real sca)
Division by a scalar.
Definition Real2x2.h:393
constexpr __host__ __device__ Real2x2 & sub(Real2x2 b)
Subtracts b from the pair.
Definition Real2x2.h:195
constexpr __host__ __device__ Real2x2 operator+(Real2x2 b) const
Creates a pair that equals this pair added to b.
Definition Real2x2.h:272
constexpr __host__ __device__ Real2x2 & addSame(Real2 b)
Adds b to each component of the pair.
Definition Real2x2.h:214
friend constexpr __host__ __device__ Real2x2 operator*(Real sca, Real2x2 vec)
Multiplication by a scalar.
Definition Real2x2.h:381
constexpr __host__ __device__ Real2x2 & div(Real2x2 b)
Multiplies each component of the pair by the corresponding component of b.
Definition Real2x2.h:206
__host__ __device__ Real & operator()(Integer i, Integer j)
Access to the i-th row and j-th column.
Definition Real2x2.h:359
Real2 x
First component.
Definition Real2x2.h:112
std::ostream & printXy(std::ostream &o) const
Writes the pair to the stream o in the form (x,y,z).
Definition Real2x2.cc:48
ARCANE_DEPRECATED_116 Real2x2(Real ax, Real ay, Real bx, Real by)
Constructs the pair ((ax,bx),(ay,by)).
Definition Real2x2.h:74
Real2 y
Second component.
Definition Real2x2.h:113
constexpr __host__ __device__ Real2x2 & operator-=(Real2x2 b)
Subtracts b from the pair.
Definition Real2x2.h:249
constexpr __host__ __device__ Real2x2 & assign(Real2 ax, Real2 ay)
Assigns the pair (ax,ay) to the instance.
Definition Real2x2.h:145
constexpr __host__ static __device__ Real2x2 null()
Constructs the zero matrix.
Definition Real2x2.h:118
constexpr __host__ __device__ bool operator!=(Real2x2 b) const
Compares two pairs. For the notion of equality, see operator==().
Definition Real2x2.h:297
constexpr __host__ static __device__ Real2x2 fromColumns(Real ax, Real ay, Real bx, Real by)
Constructs the pair ((ax,bx),(ay,by)).
Definition Real2x2.h:121
constexpr __host__ __device__ void operator/=(Real b)
Divides each component of the pair by the corresponding component of b.
Definition Real2x2.h:265
constexpr __host__ __device__ Real2x2(ConstArrayView< Real > av)
Constructs the pair ((av[0], av[1]), (av[2], av[3])).
Definition Real2x2.h:95
__host__ __device__ Real2 & operator()(Integer i)
Access to the i-th row (between 0 and 1 inclusive) of the instance.
Definition Real2x2.h:348
constexpr __host__ __device__ ArrayView< Real > view()
Definition Real2x2.h:162
constexpr __host__ __device__ Real2x2(Real2 ax, Real2 ay)
Constructs the pair (ax,ay).
Definition Real2x2.h:65
friend std::istream & operator>>(std::istream &i, Real2x2 &t)
Reads the pair t from the stream o.
Definition Real2x2.h:375
constexpr __host__ static __device__ Real2x2 fromLines(Real ax, Real bx, Real ay, Real by)
Constructs the pair ((ax,bx),(ay,by)).
Definition Real2x2.h:127
constexpr __host__ __device__ Real2x2 & assign(Real2x2 f)
Copies the pair f.
Definition Real2x2.h:153
constexpr __host__ __device__ Real2x2 operator-(Real2x2 b) const
Creates a pair that equals b subtracted from this pair.
Definition Real2x2.h:275
Real2x2(const Real2x2 &f)=default
Constructs a copy identical to f.
__host__ __device__ Real2 & operator[](Integer i)
Access to the i-th row (between 0 and 1 inclusive) of the instance.
Definition Real2x2.h:338
constexpr __host__ __device__ ConstArrayView< Real > constView() const
Definition Real2x2.h:169
constexpr __host__ __device__ bool operator==(Real2x2 b) const
Compares component by component the current instance to b.
Definition Real2x2.h:286
constexpr __host__ __device__ Real2x2 & mulSame(Real2 b)
Multiplies each component of the pair by b.
Definition Real2x2.h:230
Real2x2 & operator=(const Real2x2 &f)=default
Copy assignment operator.
__host__ __device__ Real operator()(Integer i, Integer j) const
Read-only access to the i-th row and j-th column.
Definition Real2x2.h:327
friend constexpr __host__ __device__ bool operator<(Real2x2 v1, Real2x2 v2)
Comparison operator.
Definition Real2x2.h:404
friend constexpr __host__ __device__ Real2x2 operator*(Real2x2 vec, Real sca)
Multiplication by a scalar.
Definition Real2x2.h:387
__host__ __device__ Real2 operator[](Integer i) const
Read-only access to the i-th (between 0 and 1 inclusive) row of the instance.
Definition Real2x2.h:306
constexpr __host__ __device__ Real2x2()
Constructs the zero matrix.
Definition Real2x2.h:59
__host__ __device__ Real2 operator()(Integer i) const
Read-only access to the i-th (between 0 and 1 inclusive) row of the instance.
Definition Real2x2.h:316
constexpr __host__ __device__ bool isNearlyZero() const
Compares the matrix with the zero matrix.
Definition Real2x2.h:467
constexpr __host__ __device__ Real2x2 copy() const
Returns a copy of the pair.
Definition Real2x2.h:135
constexpr __host__ __device__ Real2x2 & reset()
Resets the pair with default constructors.
Definition Real2x2.h:138
friend std::ostream & operator<<(std::ostream &o, Real2x2 t)
Writes the pair t to the stream o.
Definition Real2x2.h:369
constexpr __host__ __device__ Real2x2 & operator+=(Real2x2 b)
Adds b to the pair.
Definition Real2x2.h:246
constexpr __host__ __device__ Real2x2 & divSame(Real2 b)
Divides each component of the pair by b.
Definition Real2x2.h:238
constexpr __host__ __device__ Real2x2 & subSame(Real2 b)
Subtracts b from each component of the pair.
Definition Real2x2.h:222
constexpr __host__ __device__ void operator*=(Real b)
Multiplies each component of the pair by the corresponding component of b.
Definition Real2x2.h:255
constexpr __host__ __device__ Real2x2 & add(Real2x2 b)
Adds b to the pair.
Definition Real2x2.h:187
constexpr __host__ __device__ Real2x2 & operator=(Real v)
Assigns the pair (v,v,v) to the instance.
Definition Real2x2.h:104
constexpr __host__ __device__ Real2x2(Real v)
Constructs the instance with the triplet (v,v,v).
Definition Real2x2.h:89
constexpr __host__ __device__ Real2x2(const Real2x2POD &f)
Constructs a copy identical to f.
Definition Real2x2.h:83
constexpr __host__ __device__ Real2x2 operator-() const
Creates an inverse tensor of the current tensor.
Definition Real2x2.h:278
constexpr __host__ static __device__ bool isEqual(const T &a, const T &b)
Compares a to b.
Definition Numeric.h:94
Namespace for mathematical functions.
Definition MathUtils.h:36
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.
POD structure for a Real2x2.
Definition Real2x2.h:32