12#ifndef ARCANE_CORE_MATHUTILS_H
13#define ARCANE_CORE_MATHUTILS_H
17#include "arcane/utils/Math.h"
18#include "arcane/utils/ArrayView.h"
19#include "arcane/utils/NumericTypes.h"
20#include "arcane/utils/Real2x2.h"
21#include "arcane/utils/Real3.h"
22#include "arcane/utils/Real2.h"
24#include "arcane/core/Algorithm.h"
47ARCCORE_HOST_DEVICE
inline Real3
51 u.
y * v.
z - u.
z * v.
y,
52 u.
z * v.
x - u.
x * v.
z,
53 u.
x * v.
y - u.
y * v.
x);
65ARCCORE_HOST_DEVICE
inline Real
78ARCCORE_HOST_DEVICE
inline Real
93ARCCORE_HOST_DEVICE
inline Real
96 return (u.
x * v.
x + u.
y * v.
y);
111ARCCORE_HOST_DEVICE
inline Real
114 return (u.
x * v.
x + u.
y * v.
y);
127ARCCORE_HOST_DEVICE
inline Real
130 return (u.
x * v.
x + u.
y * v.
y + u.
z * v.
z);
145ARCCORE_HOST_DEVICE
inline Real
148 return (u.
x * v.
x + u.
y * v.
y + u.
z * v.
z);
160ARCCORE_HOST_DEVICE
inline Real
172ARCCORE_HOST_DEVICE
inline Real
176 (u.
x * (v.
y * w.
z - v.
z * w.
y)) +
177 (u.
y * (v.
z * w.
x - v.
x * w.
z)) +
178 (u.
z * (v.
x * w.
y - v.
y * w.
x)));
210ARCCORE_HOST_DEVICE
inline Real3
215ARCCORE_HOST_DEVICE
inline Real2
230ARCCORE_HOST_DEVICE
inline Real3
233 return Real3(
dot(v,
Real3(t.
x.
x, t.
y.
x, t.
z.
x)),
dot(v,
Real3(t.
x.
y, t.
y.
y, t.
z.
y)),
dot(v,
Real3(t.
x.
z, t.
y.
z, t.
z.
z)));
243ARCCORE_HOST_DEVICE
inline Real3x3
264ARCCORE_HOST_DEVICE
inline Real3x3
279ARCCORE_HOST_DEVICE
inline Real3x3
296ARCCORE_HOST_DEVICE
inline Real
311 return x1 + x2 + x3 + y1 + y2 + y3 + z1 + z2 + z3;
323ARCCORE_HOST_DEVICE
inline Real
332 return x1 + x2 + y1 + y2;
345ARCCORE_HOST_DEVICE
inline Real2
355ARCCORE_HOST_DEVICE
inline Real3
365ARCCORE_HOST_DEVICE
inline Real2x2
375ARCCORE_HOST_DEVICE
inline Real3x3
391template <
class T>
inline T
392min(
const T& a,
const T& b,
const T& c)
394 return ((a < b) ? ((a < c) ? a : ((b < c) ? b : c)) : ((b < c) ? b : c));
406template <
class T>
inline T
407max(
const T& a,
const T& b,
const T& c)
409 return ((a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c));
419ARCCORE_HOST_DEVICE
inline Real2
429ARCCORE_HOST_DEVICE
inline Real3
439ARCCORE_HOST_DEVICE
inline Real2x2
449ARCCORE_HOST_DEVICE
inline Real3x3
464ARCCORE_HOST_DEVICE
inline Real
476ARCCORE_HOST_DEVICE
inline Real
488ARCCORE_HOST_DEVICE
inline Real
512ARCCORE_HOST_DEVICE
inline Real
516 return min4Real(
max(a, zero),
max(b, zero),
max(c, zero),
max(d, zero)) +
max4Real(
min(a, zero),
min(b, zero),
min(c, zero),
min(d, zero));
525ARCCORE_HOST_DEVICE
inline Real
540ARCCORE_HOST_DEVICE
inline Real
560 Real sum = math::abs(a) + math::abs(b);
561 return (
isZero(sum)) ? (a - b) : (a - b) / sum;
604 Real sum = math::abs(b);
605 return (
isZero(sum)) ? (a - b) : (a - b) / sum;
621 Real sum = math::abs(a);
622 return (
isZero(sum)) ? (a - b) : (a - b) / sum;
633 Real& ymin,
Real& ymax,
bool need_init)
645 xmin = xmax = array[0].x;
646 ymin = ymax = array[0].y;
649 for (
Integer i = 1; i < size; ++i) {
650 if (array[i].x < xmin)
652 if (array[i].x > xmax)
655 if (array[i].y < ymin)
657 if (array[i].y > ymax)
669ARCCORE_HOST_DEVICE
inline Real
695ARCCORE_HOST_DEVICE
inline Real3x3
699 Real3(0.0, 1.0, 0.0),
700 Real3(0.0, 0.0, 1.0));
709ARCCORE_HOST_DEVICE
inline Real3x3
725ARCCORE_HOST_DEVICE
inline Real3x3
744 v.
x = v1.
y * v2.
z - v1.
z * v2.
y;
745 v.
y = v2.
x * v1.
z - v2.
z * v1.
x;
746 v.
z = v1.
x * v2.
y - v1.
y * v2.
x;
758ARCCORE_HOST_DEVICE
inline Real3
762 v.
x = v1.
y * v2.
z - v1.
z * v2.
y;
763 v.
y = v2.
x * v1.
z - v2.
z * v1.
x;
764 v.
z = v1.
x * v2.
y - v1.
y * v2.
x;
777ARCCORE_HOST_DEVICE
inline Real3
782 return Real3(v.
x / norme, v.
y / norme, v.
z / norme);
847ARCCORE_HOST_DEVICE
inline Real3
852 m.
z.
x * v.
x + m.
z.
y * v.
y + m.
z.
z * v.
z);
868 return (m0.
x.
x < epsilon) && (m0.
x.
y < epsilon) && (m0.
x.
z < epsilon) &&
869 (m0.
y.
x < epsilon) && (m0.
y.
y < epsilon) && (m0.
y.
z < epsilon) &&
870 (m0.
z.
x < epsilon) && (m0.
z.
y < epsilon) && (m0.
z.
z < epsilon);
890 u_tilde = u - 2.0 *
dot(norm, u) * norm;
911 u_tilde = 2.0 *
dot(u, norm) * norm - u;
926template <
typename T>
inline void
930 ARCANE_ASSERT((copy_array.
size() >= size), (
"Bad size %d %d", copy_array.
size(), size));
931 const T* copy_begin = copy_array.
data();
932 T* to_ptr = lhs.
data();
933 for (
Integer i = 0; i < size; ++i)
934 to_ptr[i] += copy_begin[i];
945template <
typename T>
inline void
949 ARCANE_ASSERT((copy_array.
size() >= size), (
"Bad size %d %d", copy_array.
size(), size));
950 const T* copy_begin = copy_array.
data();
951 T* to_ptr = lhs.
data();
952 for (
Integer i = 0; i < size; ++i)
953 to_ptr[i] += copy_begin[i];
964template <
typename T>
inline void
968 ARCANE_ASSERT((copy_array.
size() >= size), (
"Bad size %d %d", copy_array.
size(), size));
969 const T* copy_begin = copy_array.
data();
970 T* to_ptr = lhs.
data();
971 for (
Integer i = 0; i < size; ++i)
972 to_ptr[i] -= copy_begin[i];
983template <
typename T>
inline void
987 ARCANE_ASSERT((copy_array.
size() >= size), (
"Bad size %d %d", copy_array.
size(), size));
988 const T* copy_begin = copy_array.
data();
989 T* to_ptr = lhs.
data();
990 for (
Integer i = 0; i < size; ++i)
991 to_ptr[i] -= copy_begin[i];
1004template <
typename T>
inline void
1008 ARCANE_ASSERT((copy_array.
size() >= size), (
"Bad size %d %d", copy_array.
size(), size));
1009 const T* copy_begin = copy_array.
data();
1010 T* to_ptr = lhs.
data();
1011 for (
Integer i = 0; i < size; ++i)
1012 to_ptr[i] *= copy_begin[i];
1025template <
typename T>
inline void
1034template <
typename T>
inline void
1037 T* ptr = lhs.
data();
1038 for (
Integer i = 0, size = lhs.
size(); i < size; ++i)
1045template <
typename T>
inline void
1048 T* ptr = lhs.
data();
1049 for (
Integer i = 0, size = lhs.
size(); i < size; ++i)
Modifiable view of an array of type T.
constexpr const_pointer data() const noexcept
Pointer to the start of the view.
constexpr ConstArrayView< T > constView() const noexcept
Constant view of this view.
constexpr Integer size() const noexcept
Returns the size of the array.
Constant view of an array of type T.
constexpr const_pointer data() const noexcept
Pointer to the allocated memory.
constexpr Integer size() const noexcept
Number of elements in the array.
Class managing a 2-dimensional real vector.
Class managing a 2x2 matrix of reals.
Class managing a 3-dimensional real vector.
__host__ __device__ Real normL2() const
Returns the L2 norm of the triplet $ .
Class managing a 3x3 real matrix.
Real3 z
first element of the triplet
constexpr __host__ static __device__ Real3x3 fromLines(Real ax, Real bx, Real cx, Real ay, Real by, Real cy, Real az, Real bz, Real cz)
Constructs the matrix ((ax,bx,cx),(ay,by,cy),(az,bz,cz)).
Real3 y
first element of the triplet
constexpr __host__ __device__ Real determinant() const
Determinant of the matrix.
constexpr __host__ static __device__ Real3x3 fromColumns(Real ax, Real ay, Real az, Real bx, Real by, Real bz, Real cx, Real cy, Real cz)
Constructs the matrix ((ax,bx,cx),(ay,by,cy),(az,bz,cz)).
Real3 x
first element of the triplet
__host__ __device__ Real vecMul2D(Real3 u, Real3 v)
Vector cross product of u by v in .
__host__ __device__ Real dot(Real2 u, Real2 v)
Dot product of u by v in .
__host__ __device__ Real2 min(Real2 a, Real2 b)
Returns the minimum of two Real2.
T max(const T &a, const T &b, const T &c)
Returns the maximum of three elements.
__host__ __device__ Real3 vecMul(Real3 u, Real3 v)
Vector cross product of u by v in .
Real3 normalizedCrossProduct3(Real3 v1, Real3 v2)
Normalized cross product.
__host__ __device__ Real minMod(Real a, Real b, Real c, Real d)
Returns the Min mod of four Reals.
__host__ __device__ Real minMod2(Real a, Real b)
Returns the Min mod of two Reals.
__host__ __device__ Real cross2D(Real3 u, Real3 v)
Vector cross product of u by v in .
__host__ __device__ Real scaMul(Real2 u, Real2 v)
Dot product of u by v in .
__host__ __device__ Real3 prodVecTens(Real3 v, Real3x3 t)
Transposed(vector) matrix product between the transpose of a vector and a matrix.
__host__ __device__ Real3 cross(Real3 v1, Real3 v2)
Cross product of two 3-component vectors.
__host__ __device__ Real3 normalizeReal3(Real3 v)
Normalization of a Real3.
Real3x3 prodTens(Real3 u, Real3 v)
Tensor product of two Real3 vectors.
__host__ __device__ Real maxMod2(Real a, Real b)
Returns the Max mod of two Reals.
__host__ __device__ Real3x3 matrixProduct(const Real3x3 &t, const Real3x3 &v)
Matrix-matrix product between two tensors.
Real3x3 matrix3x3Transp(Real3x3 m)
Real3 planarSymmetric(Real3 u, Real3 n)
Symmetry of a vector u with respect to a normal plane n.
__host__ __device__ Real3 prodTensVec(Real3x3 t, Real3 v)
Matrix-vector product between a tensor and a vector.
Real3 axisSymmetric(Real3 u, Real3 a)
Symmetry of a vector u with respect to an axis defined by direction vector a.
__host__ __device__ Real mixteMul(Real3 u, Real3 v, Real3 w)
Mixed product of u, v and w.
__host__ __device__ Real3x3 matrixTranspose(const Real3x3 &t)
Transpose the matrix.
double toDouble(Real r)
Converts a Real to double.
Namespace for mathematical functions.
__host__ __device__ double pow(double x, double y)
Power function.
Real relativeError(Real a, Real b)
Returns the relative error between two scalars a and b.
__host__ __device__ Real min8Real(const Real a[8])
Returns the minimum of eight Reals.
Real relativeError2(Real a, Real b)
Returns the relative error between two scalars a and b relative to b.
__host__ __device__ double sqrt(double v)
Square root of v.
void power(ArrayView< T > lhs, T o)
Raises all elements of the array to the power o.
Real normeR3(Real3 v1)
Norm of a vector.
Real relativeError1(Real a, Real b)
Returns the relative error between two scalars a and b relative to a.
bool isNearlyId(Real3x3 m, Real epsilon=1.e-10)
Checks if a matrix is the identity matrix.
bool searchExtrema(ConstArrayView< Real2 > array, Real &xmin, Real &xmax, Real &ymin, Real &ymax, bool need_init)
Searches for the extreme values of an array of pairs (x,y).
__host__ __device__ Real doubleContraction(const Real3x3 &u, const Real3x3 &v)
__host__ __device__ Real3 multiply(const Real3x3 &m, Real3 v)
3x3 matrix multiplied by a vector.
bool isZero(const BuiltInProxy< _Type > &a)
Tests if a value is exactly equal to zero.
__host__ __device__ Real matDet(Real3 u, Real3 v, Real3 w)
Determinant of the matrix u,v,w.
Real max8Real(const Real a[8])
Returns the maximum of eight Reals.
__host__ __device__ Real3x3 matrix3x3Id()
Identity matrix.
__host__ __device__ Real matrixDeterminant(Real3x3 m)
Calculates the determinant of a 3x3 matrix.
void mult(ArrayView< T > lhs, ConstArrayView< T > copy_array)
Multiplies the elements of the instance term-by-term by the elements of the array copy_array.
__host__ __device__ Real min4Real(Real a, Real b, Real c, Real d)
Returns the minimum of four Reals.
Real3 crossProduct3(Real3 v1, Real3 v2)
Cross product of two 3-component vectors.
__host__ __device__ Real max4Real(Real a, Real b, Real c, Real d)
Returns the maximum of four Reals.
Real3x3 matrix3x3Prod(Real3x3 m1, Real3x3 m2)
Multiplication of 2 3x3 matrices.
__host__ __device__ Real3x3 inverseMatrix(const Real3x3 &m, Real d)
Calculates the inverse of a matrix m assuming its determinant d is known.
void sub(ArrayView< T > lhs, ConstArrayView< T > copy_array)
Subtracts the array copy_array from the instance.
__host__ __device__ Real3x3 transpose(const Real3x3 &t)
Transpose the matrix.
void add(ArrayView< T > lhs, ConstArrayView< T > copy_array)
Adds the array copy_array to the instance.
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.
__host__ __device__ void arcaneMathError(long double arg_value, const char *func_name)
Signals an invalid argument in a mathematical function.
Real y
second component of the pair
Real x
first component of the pair
Real y
second component of the triplet
Real z
third component of the triplet
Real x
first component of the triplet