MoReFEM
Loading...
Searching...
No Matches
Functions
MoReFEM::NumericNS Namespace Reference

Placeholder namespace description needed for it to appear in Doxygen namespace list. More...

Functions

template<class T >
constexpr T UninitializedIndex () noexcept
 When an index is not known at object constructor, better assign a controlled dumb value than not knowing what the compiler decided.
 
template<class T >
constexpr T Square (T value) noexcept
 Computes the square of a value.
 
template<class T >
constexpr T Cube (T value) noexcept
 Computes the cube of a value.
 
template<class T >
constexpr T PowerFour (T value) noexcept
 Computes the 4th power of a value.
 
template<class T >
constexpr T AbsPlus (T value) noexcept
 Returns value if it is positive or 0 otherwise.
 
template<class T >
constexpr T Sign (T value) noexcept
 Returns -1, 0 or 1 depending on the sign of tge value.
 
template<class T >
constexpr T TrueSign (T value) noexcept
 A special version of Sign which considers 0 as positive.
 
template<class T >
constexpr T Heaviside (T value) noexcept
 Heaviside function.
 
template<class T >
constexpr std::enable_if_t< std::is_floating_point< T >::value, T > DefaultEpsilon () noexcept
 Returns a floating-point value that is small enough for most purposes.
 
template<class T >
std::enable_if_t< std::is_floating_point< T >::value, bool > IsZero (T value, T epsilon=DefaultEpsilon< T >()) noexcept
 Check whether a value is close enough to zero to be able to be considered as 0.
 
template<class T >
std::enable_if_t< std::is_integral< T >::value, bool > IsZero (T value) noexcept
 Check whether a value is 0.
 
template<class T >
std::enable_if_t< std::is_same< T, LocalMatrix >()||std::is_same< T, LocalVector >(), bool > IsZero (T value, double epsilon=DefaultEpsilon< double >()) noexcept
 Check whether a LocalVector or a LocalMatrix is filled only by values close enough to zero.
 
bool IsZero (const LocalVector &vector, double epsilon=DefaultEpsilon< double >()) noexcept
 Check whether a LocalVector is filled with all its values close enough to zero to be able to be considered as 0.
 
bool IsZero (const LocalMatrix &matrix, double epsilon=DefaultEpsilon< double >()) noexcept
 Check whether a LocalMatrix is filled with all its values close enough to zero to be able to be considered as 0.
 
template<class T >
std::enable_if_t< std::is_floating_point< T >::value, bool > AreEqual (T lhs, T rhs, T epsilon=DefaultEpsilon< T >()) noexcept
 Check whether a value is close enough to another.
 
bool AreEqual (const LocalVector &lhs, const LocalVector &rhs, double epsilon=DefaultEpsilon< double >())
 Check whether two LocalVector are filled with all their values close enough to one another.
 
bool AreEqual (const LocalMatrix &lhs, const LocalMatrix &rhs, double epsilon=DefaultEpsilon< double >())
 Check whether two LocalMatrix are filled with all their values close enough to one another.
 
template<class T >
std::enable_if_t< std::is_same< T, LocalMatrix >()||std::is_same< T, LocalVector >(), bool > AreEqual (const T &lhs, const T &rhs, double epsilon=DefaultEpsilon< double >())
 Check whether two LocalVector or LocalMatrix are close enough.
 
template<class T >
Pow (T base, T exponent, const std::source_location location=std::source_location::current())
 A wrapper over std::pow which throws whenever an invalid value is computed.
 

Detailed Description

Placeholder namespace description needed for it to appear in Doxygen namespace list.

Namespace that enclose numeric utilities (such as IsZero() or AreEqual() used to compared floating-points).

Feel free to replace this text by explanation if namespace name is not self-explaining enough.

Function Documentation

◆ UninitializedIndex()

template<class T >
constexpr T MoReFEM::NumericNS::UninitializedIndex ( )
constexprnoexcept

When an index is not known at object constructor, better assign a controlled dumb value than not knowing what the compiler decided.

In most cases (typically for instance the initialization of a class attribute) it is a good idea to use it along with decltype:

MyClass::MyClass()
: index_(MoReFEM::NumericNS::UninitializedIndex<decltype(index_)>())
{ }
constexpr T UninitializedIndex() noexcept
When an index is not known at object constructor, better assign a controlled dumb value than not know...
Main MoReFEM namespace.
Definition CommandLineFlags.hpp:31

Doing so makes the code robust to a change of type: if some day index_ becomes a std::size_t rather than an int the default value will keep being the greatest value it can reach.

Returns
Known value when an index is not properly initialized (currently it is the highest possible value for T).

◆ Square()

template<class T >
constexpr T MoReFEM::NumericNS::Square ( T value)
constexprnoexcept

Computes the square of a value.

Note
std::pow() is more versatile but far less efficient than a simple call to operator*. Defining a function is not just laziness: if value is for instance computed on the spot it is done only once; consider for instance Operator(f(5)): f(5) is computed only once!
Template Parameters
TType of the value; it is assumed operator* is defined for this type.
Parameters
[in]valueValue upon which the operation is performed.
Returns
value * value.

◆ Cube()

template<class T >
constexpr T MoReFEM::NumericNS::Cube ( T value)
constexprnoexcept

Computes the cube of a value.

Note
std::pow() is more versatile but far less efficient than a simple call to operator*. Defining a function is not just laziness: if value is for instance computed on the spot it is done only once; consider for instance Operator(f(5)): f(5) is computed only once!
Template Parameters
TType of the value; it is assumed operator* is defined for this type.
Parameters
[in]valueValue upon which the operation is performed.
Returns
value * value * value.

◆ PowerFour()

template<class T >
constexpr T MoReFEM::NumericNS::PowerFour ( T value)
constexprnoexcept

Computes the 4th power of a value.

Note
std::pow() is more versatile but far less efficient than a simple call to operator*. Defining a function is not just laziness: if value is for instance computed on the spot it is done only once; consider for instance Operator(f(5)): f(5) is computed only once!
Template Parameters
TType of the value; it is assumed operator* is defined for this type.
Parameters
[in]valueValue upon which the operation is performed.
Returns
value * value * value * value.

◆ AbsPlus()

template<class T >
constexpr T MoReFEM::NumericNS::AbsPlus ( T value)
constexprnoexcept

Returns value if it is positive or 0 otherwise.

Template Parameters
TType of the value considered; it must both be initialized with 0 and comes with an operator<.
Parameters
[in]valueValue upon which the operation is performed.
Returns
value if value > 0, 0 otherwise.

◆ Sign()

template<class T >
constexpr T MoReFEM::NumericNS::Sign ( T value)
constexprnoexcept

Returns -1, 0 or 1 depending on the sign of tge value.

Parameters
[in]valueValue which sign is evaluated.
Template Parameters
TType of the value; 0, 1 and -1 must be convertible to this type and operator< must be defined for it.
Returns
-1 if negative, 1 if positive, 0 if null (as determined by the IsZero() function).

◆ TrueSign()

template<class T >
constexpr T MoReFEM::NumericNS::TrueSign ( T value)
constexprnoexcept

A special version of Sign which considers 0 as positive.

Parameters
[in]valueValue which sign is evaluated.
Template Parameters
TType of the value; 0, 1 and -1 must be convertible to this type and operator< must be defined for it.

This is directly lifted from HeartLab code; TrueSign is like the sign except zero is counted as positive.

Returns
-1 if negative, 1 if positive or zero (as determined by the IsZero function).

◆ Heaviside()

template<class T >
constexpr T MoReFEM::NumericNS::Heaviside ( T value)
constexprnoexcept

Heaviside function.

Parameters
[in]valueValue which sign is evaluated.
Template Parameters
TA floating-point type.
Returns
0 if negative, 1 if positive, 0.5 if null (as determined by the IsZero function).

◆ DefaultEpsilon()

template<class T >
constexpr std::enable_if_t< std::is_floating_point< T >::value, T > MoReFEM::NumericNS::DefaultEpsilon ( )
constexprnoexcept

Returns a floating-point value that is small enough for most purposes.

Currently set to 1.e-15 is used here.

Note
We use a function for this so that there are no magic number in the code and the same value is used everywhere.
Template Parameters
TType for which the value is required; a double must be convertible into this type.
Returns
1.e-15.

◆ IsZero() [1/5]

template<class T >
std::enable_if_t< std::is_floating_point< T >::value, bool > MoReFEM::NumericNS::IsZero ( T value,
T epsilon = DefaultEpsilon< T >() )
noexcept

Check whether a value is close enough to zero to be able to be considered as 0.

Template Parameters
TFloating point type considered (float, double or long double).
Parameters
[in]valueValue that is tested.
[in]epsilonEpsilon used for the comparison. A default value is provided; but the parameter is there if you want to play with it.
Returns
True if value might be considered as null or not.

◆ IsZero() [2/5]

template<class T >
std::enable_if_t< std::is_integral< T >::value, bool > MoReFEM::NumericNS::IsZero ( T value)
noexcept

Check whether a value is 0.

Template Parameters
TIntegral type considered
Parameters
[in]valueValue that is tested.

For an integral type such a function is trivial but it might be useful from a metaprogramming standpoint.

Returns
True if value might be considered as null or not.

◆ IsZero() [3/5]

template<class T >
std::enable_if_t< std::is_same< T, LocalMatrix >()||std::is_same< T, LocalVector >(), bool > MoReFEM::NumericNS::IsZero ( T value,
double epsilon = DefaultEpsilon< double >() )
noexcept

Check whether a LocalVector or a LocalMatrix is filled only by values close enough to zero.

There are in fact dedicated functions to handle the separate cases of Localvector and LocalMatrix, but we wanted to enable the users to make a call such as:

NumericNS::IsZero<LocalVector>(vector);

that might be useful in metaprogramming context. Of course current function just calls directly under the hood the other one.

Parameters
[in]valueVector or matrix that is tested.
[in]epsilonEpsilon used for the comparison. A default value is provided; but the parameter is there if you want to play with it.

◆ IsZero() [4/5]

bool MoReFEM::NumericNS::IsZero ( const LocalVector & vector,
double epsilon = DefaultEpsilon< double >() )
noexcept

Check whether a LocalVector is filled with all its values close enough to zero to be able to be considered as 0.

Parameters
[in]vectorVector that is tested.
[in]epsilonEpsilon used for the comparison. A default value is provided; but the parameter is there if you want to play with it.
Returns
True if vector is filled only with values close enough to zero.

◆ IsZero() [5/5]

bool MoReFEM::NumericNS::IsZero ( const LocalMatrix & matrix,
double epsilon = DefaultEpsilon< double >() )
noexcept

Check whether a LocalMatrix is filled with all its values close enough to zero to be able to be considered as 0.

Parameters
[in]matrixMatrix that is tested.
[in]epsilonEpsilon used for the comparison. A default value is provided; but the parameter is there if you want to play with it.
Returns
True if matrix is filled only with values close enough to zero.

◆ AreEqual() [1/4]

template<class T >
std::enable_if_t< std::is_floating_point< T >::value, bool > MoReFEM::NumericNS::AreEqual ( T lhs,
T rhs,
T epsilon = DefaultEpsilon< T >() )
noexcept

Check whether a value is close enough to another.

Template Parameters
TFloating point type considered (float, double or long double).
Parameters
[in]lhsLhs value.
[in]rhsRhs value.
[in]epsilonEpsilon used for the comparison. A default value is provided; but the parameter is there if you want to play with it.
Returns
True if lhs and rhs are deemed to be close enough to be considered equal.

◆ AreEqual() [2/4]

bool MoReFEM::NumericNS::AreEqual ( const LocalVector & lhs,
const LocalVector & rhs,
double epsilon = DefaultEpsilon< double >() )

Check whether two LocalVector are filled with all their values close enough to one another.

If the vectors aren't the same size, an exception is thrown.

Parameters
[in]lhsFirst vector to be tested.
[in]rhsSecond vector to be tested.
[in]epsilonEpsilon used for the comparison. A default value is provided; but the parameter is there if you want to play with it.
Returns
True if both lhs and rhs vectors are the same size and filled only with values close enough to one another.

◆ AreEqual() [3/4]

bool MoReFEM::NumericNS::AreEqual ( const LocalMatrix & lhs,
const LocalMatrix & rhs,
double epsilon = DefaultEpsilon< double >() )

Check whether two LocalMatrix are filled with all their values close enough to one another.

If the matrices aren't the same dimension, an exception is thrown.

Parameters
[in]lhsFirst matrix to be tested.
[in]rhsSecond matrix to be tested.
[in]epsilonEpsilon used for the comparison. A default value is provided; but the parameter is there if you want to play with it.
Returns
True if both lhs and rhs matrices are the same size and filled only with values close enough to one another.

◆ AreEqual() [4/4]

template<class T >
std::enable_if_t< std::is_same< T, LocalMatrix >()||std::is_same< T, LocalVector >(), bool > MoReFEM::NumericNS::AreEqual ( const T & lhs,
const T & rhs,
double epsilon = DefaultEpsilon< double >() )

Check whether two LocalVector or LocalMatrix are close enough.

Parameters
[in]lhsFirst linear algebra to be tested.
[in]rhsSecond linear algebra to be tested. Must be the same shape as the first one (or an exception is thrown).
[in]epsilonEpsilon used for the comparison (absolute tolerance). A default value is provided; but the parameter is there if you want to play with it.

There are in fact dedicated functions to handle the separate cases of LocalVector and LocalMatrix, but we wanted to enable the users to make a call such as:

NumericNS::AreEqual<LocalVector>(vector1, vector2);

that might be useful in metaprogramming context. Of course current function just calls directly under the hood the other one.

◆ Pow()

template<class T >
T MoReFEM::NumericNS::Pow ( T base,
T exponent,
const std::source_location location = std::source_location::current() )

A wrapper over std::pow which throws whenever an invalid value is computed.

std::pow handles its errors by:

  • setting the return value as nan
  • setting a global variable with the information about the state
Template Parameters
TAny floating-point type (less powerful than std::pow on this regard but at the moment no need to consider other types).
Parameters
[in]baseBase value
[in]exponentExponent
Parameters
[in]locationSTL object with relevant information about the calling site (usually to help when an exception is thrown.
Returns
The computed value (that shouldn't be nan: if the computation is invalid an exception should be thrown.