Arcane  4.2.1.0
User documentation
Loading...
Searching...
No Matches
MathReal2.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/* MathReal2.h (C) 2000-2026 */
9/* */
10/* Mathematical operations on Real2. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_BASE_MATHREAL2_H
13#define ARCCORE_BASE_MATHREAL2_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/base/Real2.h"
18#include "arccore/base/MathBase.h"
19
20/*---------------------------------------------------------------------------*/
21/*---------------------------------------------------------------------------*/
22
23namespace Arcane::math
24{
25 /*!
26 * \brief Indicates if the instance is close to the zero instance.
27 *
28 * \retval true if math::isNearlyZero() is true for every component.
29 * \retval false otherwise.
30 */
31 inline constexpr ARCCORE_HOST_DEVICE bool isNearlyZero(const Real2& v)
32 {
33 return math::isNearlyZero(v.x) && math::isNearlyZero(v.y);
34 }
35
36 //! Returns the squared norm of the pair $\f$x^2+y^2+z^2$\f$
37 inline constexpr ARCCORE_HOST_DEVICE Real squareNormL2(const Real2& v)
38 {
39 return v.x * v.x + v.y * v.y;
40 }
41
42 //! Returns the norm of the pair $\f$\sqrt{x^2+y^2+z^2}$\f$
43 inline ARCCORE_HOST_DEVICE Real normL2(const Real2& v)
44 {
46 }
47
48 /*!
49 * \brief Normalizes the pair.
50 *
51 * If the pair is non-zero, divides each component by the norm of the pair
52 * (abs()), such that after calling this method, abs() equals 1.
53 * If the pair is zero, does nothing.
54 */
56 {
57 Real d = math::normL2(v);
58 if (!math::isZero(d))
59 v.divSame(d);
60 return v;
61 }
62
63 /*!
64 * \brief Returns the pair v normalized by the L2 norm.
65 *
66 * If `math::normL2(v)` is non-zero, returns the pair v divided by `math::normL2(v)`.
67 * Otherwise, returns v.
68 */
69 inline Real2 normalizeL2(const Real2& v)
70 {
71 Real d = math::normL2(v);
72 if (!math::isZero(d))
73 return v / d;
74 return v;
75 }
76} // namespace math
77
78/*---------------------------------------------------------------------------*/
79/*---------------------------------------------------------------------------*/
80
81namespace Arcane
82{
83
84/*---------------------------------------------------------------------------*/
85/*---------------------------------------------------------------------------*/
86
87inline constexpr ARCCORE_HOST_DEVICE bool Real2::
88isNearlyZero() const
89{
90 return math::isNearlyZero(*this);
91}
92
93inline ARCCORE_HOST_DEVICE Real Real2::
94normL2() const
95{
96 return math::normL2(*this);
97}
98
100normalize()
101{
102 return math::mutableNormalize(*this);
103}
104
105inline ARCCORE_HOST_DEVICE Real Real2::
106abs() const
107{
108 return math::normL2(*this);
109}
110
111/*---------------------------------------------------------------------------*/
112/*---------------------------------------------------------------------------*/
113
114} // namespace Arcane
115
116/*---------------------------------------------------------------------------*/
117/*---------------------------------------------------------------------------*/
118
119#endif
Class managing a 2-dimensional real vector.
__host__ __device__ Real normL2() const
Returns the norm of the pair $ .
Definition MathReal2.h:94
constexpr __host__ __device__ Real2()
Constructs the zero vector.
constexpr __host__ __device__ bool isNearlyZero() const
Indicates if the instance is close to the zero instance.
Definition MathReal2.h:88
__host__ __device__ Real abs() const
Returns the norm of the pair $ .
Definition MathReal2.h:106
Real2 & normalize()
Normalizes the pair.
Definition MathReal2.h:100
constexpr __host__ __device__ Real2 & divSame(Real b)
Divides each component of the pair by b.
Namespace for mathematical functions.
Definition MathUtils.h:36
Real2 normalizeL2(const Real2 &v)
Returns the pair v normalized by the L2 norm.
Definition MathReal2.h:69
constexpr __host__ __device__ DataType squareNormL2(const NumVector< DataType, Size > &v)
Returns the square of the L2 norm of the vector.
Definition MathNumeric.h:77
bool isZero(const BuiltInProxy< _Type > &a)
Tests if a value is exactly equal to zero.
Real2 & mutableNormalize(Real2 &v)
Normalizes the pair.
Definition MathReal2.h:55
apfloat sqrt(apfloat v)
Square root of v.
Definition MathApfloat.h:69
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
double Real
Type representing a real number.
Real y
second component of the pair
Real x
first component of the pair