MGIS 3.1.0
Loading...
Searching...
No Matches
Algorithms.ixx File Reference

Implementation of the algorithms which can be applied to the functions and the evaluators of the MGIS/Function library. More...

#include <ranges>
#include <numeric>
#include <iterator>
#include <algorithm>

Namespaces

namespace  mgis
 the namespace mgis encloses all the MGIS project.

Functions

template<size_type N, typename InputIterator, typename OutputIterator>
requires (N > 0)
constexpr void mgis::function::algorithm::copy (const InputIterator p, const InputIterator pe, const OutputIterator po)
 copies N values from a range of input iterators to a range of output iterators
constexpr void mgis::function::internals::assign_value (std::span< real >, const auto &)
 assigns to a span of dynamic size the values of any container which can be iterated over
constexpr void mgis::function::internals::assign_value (std::span< real >, const std::span< const real >)
 assigns to a span of dynamic size the values of a constant span
constexpr void mgis::function::internals::assign_value (std::span< real >, const std::span< real >)
 assigns to a span of dynamic size the values of a mutable span
constexpr void mgis::function::internals::assign_value (real &, const std::span< real > &)
 assigns to a scalar value the first value of a mutable span
constexpr void mgis::function::internals::assign_value (real &, const std::span< const real > &)
 assigns to a scalar value the first value of a constant span
constexpr void mgis::function::internals::assign_value (real &, const std::span< real, 1u > &)
 assigns to a scalar value the value of a mutable span of one element
constexpr void mgis::function::internals::assign_value (real &, const std::span< const real, 1u > &)
 assigns to a scalar value the value of a constant span of one element
constexpr void mgis::function::internals::assign_value (real &, const std::array< real, 1u > &)
 assigns to a scalar value the value of an array of one element
constexpr void mgis::function::internals::assign_value (std::array< real, 1u > &, const real)
 assigns a scalar value to the unique element of an array
constexpr void mgis::function::internals::assign_value (std::array< real, 1u > &, const std::array< real, 1u >)
 assigns the value of an array of one element to an array of one element
constexpr void mgis::function::internals::assign_value (std::array< real, 1u > &, const std::span< real >)
 assigns the first value of a mutable span to an array of one element
constexpr void mgis::function::internals::assign_value (std::array< real, 1u > &, const std::span< const real >)
 assigns the first value of a constant span to an array of one element
constexpr void mgis::function::internals::assign_value (std::array< real, 1u > &, const std::span< real, 1u >)
 assigns the value of a mutable one element span to an array of one element
constexpr void mgis::function::internals::assign_value (std::array< real, 1u > &, const std::span< const real, 1u >)
 assigns the value of a constant span of one element to an array of one element
constexpr void mgis::function::internals::assign_value (std::span< real, 1u >, const real)
 assigns a scalar value to the unique element of a span
constexpr void mgis::function::internals::assign_value (std::span< real >, const real)
 assigns a scalar value to the first element of a span of dynamic size
constexpr void mgis::function::internals::assign_value (std::span< real >, const std::span< real, 1u >)
 assigns the value of a mutable span of one element to the first element of a span of dynamic size
constexpr void mgis::function::internals::assign_value (std::span< real >, const std::span< const real, 1u >)
 assigns the value of a constant span of one element to the first element of a span of dynamic size
constexpr void mgis::function::internals::assign_value (std::span< real, 1u >, const std::span< real >)
 assigns the first value of a mutable span to a span of one element
constexpr void mgis::function::internals::assign_value (std::span< real, 1u >, const std::span< const real >)
 assigns the first value of a constant span to a span of one element
constexpr void mgis::function::internals::assign_value (std::span< real, 1u >, const std::span< real, 1u >)
 assigns the value of a mutable span of one element to a span of one element
constexpr void mgis::function::internals::assign_value (std::span< real, 1u >, const std::span< const real, 1u >)
 assigns the value of a constant span of one element to a span of one element
constexpr void mgis::function::internals::assign_value (std::span< real, 1u >, const std::array< real, 1u > &)
 assigns the value of an array of one element to a span of one element
template<typename FunctionType, EvaluatorConcept EvaluatorType>
requires ( (LinearElementSpaceConcept<std::decay_t<decltype(getSpace(f))>>)&&( !hasElementWorkspace<std::decay_t<decltype(getSpace(f))>>))
constexpr void mgis::function::internals::assign_sequential_scalar_impl (FunctionType &f, const EvaluatorType &e)
 assigns to a function the scalar values computed by an evaluator, the evaluation being sequential
template<typename FunctionType, EvaluatorConcept EvaluatorType>
requires ( (LinearElementSpaceConcept<std::decay_t<decltype(getSpace(f))>>)&&( hasElementWorkspace<std::decay_t<decltype(getSpace(f))>>))
constexpr void mgis::function::internals::assign_sequential_scalar_impl (FunctionType &f, const EvaluatorType &e)
 assigns to a function the scalar values computed by an evaluator, the evaluation being sequential
template<typename FunctionType, EvaluatorConcept EvaluatorType>
requires ( (LinearElementSpaceConcept<std::decay_t<decltype(getSpace(f))>>)&&( !hasElementWorkspace<std::decay_t<decltype(getSpace(f))>>))
constexpr void mgis::function::internals::assign_sequential_impl (FunctionType &f, const EvaluatorType &e)
 assigns to a function the values computed by an evaluator, the evaluation being sequential
template<typename FunctionType, EvaluatorConcept EvaluatorType>
requires ( (LinearElementSpaceConcept<std::decay_t<decltype(getSpace(f))>>)&&( hasElementWorkspace<std::decay_t<decltype(getSpace(f))>>))
constexpr void mgis::function::internals::assign_sequential_impl (FunctionType &f, const EvaluatorType &e)
 assigns to a function the values computed by an evaluator, the evaluation being sequential
template<typename FunctionType, EvaluatorConcept EvaluatorType>
requires ( (LinearElementSpaceConcept<evaluator_space<EvaluatorType>>) && same_decay_type<function_space<FunctionType>, evaluator_space<EvaluatorType>>)
constexpr void mgis::function::internals::assign_sequential (FunctionType &f, const EvaluatorType e)
 assigns to a function the values computed by an evaluator, the evaluation being sequential
template<EvaluatorConcept EvaluatorType, typename OperatorType>
requires (LinearElementSpaceConcept<evaluator_space<EvaluatorType>>)
constexpr real mgis::function::internals::sequential_scalar_reduce (const EvaluatorType e, const OperatorType op, const real initial_value)
 reduces the values computed by an evaluator to a single scalar value, the evaluation being sequential
template<ViewableFunctionArgumentConcept FunctionType, EvaluatorConcept EvaluatorType>
requires ( (internals::isEvaluatorAssignableToFunction< EvaluatorType, std::decay_t<FunctionType>>)&& // (LinearElementSpaceConcept<evaluator_space<EvaluatorType>>))
constexpr bool mgis::function::assign (AbstractErrorHandler &, FunctionType &&, const EvaluatorType)
 assigns the values computed by an evaluator to a function, the evaluation being sequential
template<EvaluatorConcept EvaluatorType, typename OperatorType>
requires (LinearElementSpaceConcept<evaluator_space<EvaluatorType>>)
constexpr std::optional< real > mgis::function::scalar_reduce (AbstractErrorHandler &, const EvaluatorType, const OperatorType, const real)
 reduces the values computed by a scalar evaluator to a single value using the given reduction operator, the evaluation being sequential

Detailed Description

Implementation of the algorithms which can be applied to the functions and the evaluators of the MGIS/Function library.

Author
Thomas Helfer
Date
29/04/2025

Function Documentation

◆ assign()

template<ViewableFunctionArgumentConcept FunctionType, EvaluatorConcept EvaluatorType>
requires ( (internals::isEvaluatorAssignableToFunction< EvaluatorType, std::decay_t<FunctionType>>)&& // (LinearElementSpaceConcept<evaluator_space<EvaluatorType>>))
bool mgis::function::assign ( AbstractErrorHandler & ctx,
FunctionType && f,
const EvaluatorType e )
nodiscardconstexpr

assigns the values computed by an evaluator to a function, the evaluation being sequential

implementation of the sequential assign algorithm

The spaces of the function and the evaluator are compared and the number of components of the values returned by both objects is checked to be the same before the values computed by the evaluator are assigned.

Template Parameters
FunctionTypetype of the function argument, which may be a reference type
EvaluatorTypeevaluator type
Parameters
[in]ctxerror handler used to report the errors
[in,out]ffunction assigned
[in]eevaluator providing the values assigned
Returns
true if the assignment succeeded, false otherwise
Note
the values computed by the evaluator are checked with the check function before being assigned. As no execution policy is used, this function can be called in a constexpr context

The error messages "unmatched spaces" and "unmatched number of components" are registered in the error handler when the spaces of the function and the evaluator are not equivalent or when the number of components of the values they return differs. The evaluator is then checked before the values are assigned.

◆ assign_sequential()

template<typename FunctionType, EvaluatorConcept EvaluatorType>
requires ( (LinearElementSpaceConcept<evaluator_space<EvaluatorType>>) && same_decay_type<function_space<FunctionType>, evaluator_space<EvaluatorType>>)
void mgis::function::internals::assign_sequential ( FunctionType & f,
const EvaluatorType e )
constexpr

assigns to a function the values computed by an evaluator, the evaluation being sequential

Template Parameters
FunctionTypefunction type
EvaluatorTypeevaluator type
Parameters
[in,out]ffunction assigned
[in]eevaluator providing the values assigned
Note
the scalar implementation is used when both the function and the evaluator return scalar values, the general implementation otherwise

◆ assign_sequential_impl() [1/2]

template<typename FunctionType, EvaluatorConcept EvaluatorType>
requires ( (LinearElementSpaceConcept<std::decay_t<decltype(getSpace(f))>>)&&( hasElementWorkspace<std::decay_t<decltype(getSpace(f))>>))
void mgis::function::internals::assign_sequential_impl ( FunctionType & f,
const EvaluatorType & e )
constexpr

assigns to a function the values computed by an evaluator, the evaluation being sequential

This overload is used when the values returned by the function or the evaluator are not scalar values and when the space requires a workspace to access its elements. The function assigned and the evaluator providing the values assigned are the same arguments as for the other overload.

◆ assign_sequential_impl() [2/2]

template<typename FunctionType, EvaluatorConcept EvaluatorType>
requires ( (LinearElementSpaceConcept<std::decay_t<decltype(getSpace(f))>>)&&( !hasElementWorkspace<std::decay_t<decltype(getSpace(f))>>))
void mgis::function::internals::assign_sequential_impl ( FunctionType & f,
const EvaluatorType & e )
constexpr

assigns to a function the values computed by an evaluator, the evaluation being sequential

This overload is used when the values returned by the function or the evaluator are not scalar values and when the space does not require any workspace to access its elements.

Template Parameters
FunctionTypefunction type
EvaluatorTypeevaluator type
Parameters
[in,out]ffunction assigned
[in]eevaluator providing the values assigned
Note
the values returned by the evaluator are assigned directly to those returned by the function if the corresponding types are compatible, using one of the assign_value helpers otherwise

◆ assign_sequential_scalar_impl() [1/2]

template<typename FunctionType, EvaluatorConcept EvaluatorType>
requires ( (LinearElementSpaceConcept<std::decay_t<decltype(getSpace(f))>>)&&( hasElementWorkspace<std::decay_t<decltype(getSpace(f))>>))
void mgis::function::internals::assign_sequential_scalar_impl ( FunctionType & f,
const EvaluatorType & e )
constexpr

assigns to a function the scalar values computed by an evaluator, the evaluation being sequential

This overload is used when the values returned by both objects are scalar values and when the space requires a workspace to access its elements: the workspace of an element is retrieved before calling the function and the evaluator with this workspace and the index of the element. The function assigned and the evaluator providing the values assigned are the same arguments as for the other overload.

◆ assign_sequential_scalar_impl() [2/2]

template<typename FunctionType, EvaluatorConcept EvaluatorType>
requires ( (LinearElementSpaceConcept<std::decay_t<decltype(getSpace(f))>>)&&( !hasElementWorkspace<std::decay_t<decltype(getSpace(f))>>))
void mgis::function::internals::assign_sequential_scalar_impl ( FunctionType & f,
const EvaluatorType & e )
constexpr

assigns to a function the scalar values computed by an evaluator, the evaluation being sequential

This overload is used when the values returned by both objects are scalar values and when the space does not require any workspace to access its elements: the function and the evaluator are called with the index of each element of the space.

Template Parameters
FunctionTypefunction type
EvaluatorTypeevaluator type
Parameters
[in,out]ffunction assigned
[in]eevaluator providing the values assigned

◆ assign_value() [1/2]

void mgis::function::internals::assign_value ( std::span< real > lhs,
const auto & rhs )
constexpr

assigns to a span of dynamic size the values of any container which can be iterated over

implementation of the most generic assign_value function, which iterates over the values of the right hand side

Parameters
[out]lhsspan receiving the values
[in]rhscontainer providing the values
Note
the number of values of lhs must be greater than or equal to the number of values of rhs

The other overloads, declared in MGIS/Function/Algorithms.hxx, copy the values one by one, those connecting a scalar value and a container of a single value using only the first value of this container.

◆ assign_value() [2/2]

void mgis::function::internals::assign_value ( std::span< real > lhs,
const real rhs )
constexpr

assigns a scalar value to the first element of a span of dynamic size

Note
only the first element of the span is modified

◆ copy()

template<size_type N, typename InputIterator, typename OutputIterator>
requires (N > 0)
void mgis::function::algorithm::copy ( const InputIterator p,
const InputIterator pe,
const OutputIterator po )
constexpr

copies N values from a range of input iterators to a range of output iterators

When both iterators are random access iterators and the number of values is small, the copy is unrolled at compile time, which generates a more efficient code than the call to std::copy used otherwise.

Template Parameters
Nnumber of values to be copied, which must be greater than zero
InputIteratorinput iterator type
OutputIteratoroutput iterator type
Parameters
[in]piterator to the first value to be copied
[in]peiterator past the last value to be copied
[in]poiterator to the first value assigned
Note
std::copy is used if more than nine values are copied or if one of the iterators is not a random access iterator

◆ scalar_reduce()

template<EvaluatorConcept EvaluatorType, typename OperatorType>
requires (LinearElementSpaceConcept<evaluator_space<EvaluatorType>>)
std::optional< real > mgis::function::scalar_reduce ( AbstractErrorHandler & ctx,
const EvaluatorType e,
const OperatorType op,
const real initial_value )
nodiscardconstexpr

reduces the values computed by a scalar evaluator to a single value using the given reduction operator, the evaluation being sequential

implementation of the sequential scalar_reduce algorithm

The values computed by the evaluator are checked before being reduced.

Template Parameters
EvaluatorTypeevaluator type
OperatorTypetype of the reduction operator, a binary operation taking two scalar values and returning a scalar value
Parameters
[in]ctxerror handler used to report the errors
[in]eevaluator reduced
[in]opreduction operator
[in]initial_valuevalue the reduction starts from
Returns
the reduced value or an empty std::optional if an error is reported
Note
this function requires the values returned by the evaluator to have a single component

The error message "non scalar evaluator" is registered in the error handler when the values returned by the evaluator have more than one component. The evaluator is then checked before being reduced.

◆ sequential_scalar_reduce()

template<EvaluatorConcept EvaluatorType, typename OperatorType>
requires (LinearElementSpaceConcept<evaluator_space<EvaluatorType>>)
real mgis::function::internals::sequential_scalar_reduce ( const EvaluatorType e,
const OperatorType op,
const real initial_value )
constexpr

reduces the values computed by an evaluator to a single scalar value, the evaluation being sequential

The reduction is performed by std::transform_reduce: the values computed by the evaluator are combined two by two with the reduction operator, starting from the initial value.

Template Parameters
EvaluatorTypeevaluator type
OperatorTypereduction operator type
Parameters
[in]eevaluator reduced
[in]opreduction operator
[in]initial_valuevalue the reduction starts from
Returns
the reduced value
Note
the first component of the values returned by the evaluator is used when those values are not scalar values