gemseo / utils / derivatives

gradient_approximator module

Gradient approximation.

class gemseo.utils.derivatives.gradient_approximator.GradientApproximationFactory[source]

Bases: object

A factory to create gradient approximators.

Return type

None

create(name, f_pointer, step=None, parallel=False, **parallel_args)[source]

Create a gradient approximator.

Parameters
  • name (str) – Either the name of the class implementing the gradient approximator or its alias.

  • f_pointer (Callable) – The pointer to the function to differentiate.

  • step (float | None) –

    The differentiation step. If None, use a default differentiation step.

    By default it is set to None.

  • parallel (bool) –

    Whether to differentiate the function in parallel.

    By default it is set to False.

  • **parallel_args – The parallel execution options, see gemseo.core.parallel_execution.

Returns

The gradient approximator.

Return type

GradientApproximator

is_available(class_name)[source]

Whether a gradient approximator is available.

Parameters

class_name – The name of the class implementing the gradient approximator.

Returns

Whether the gradient approximator is available.

Return type

bool

property gradient_approximators: list[str]

The gradient approximators.

class gemseo.utils.derivatives.gradient_approximator.GradientApproximator(f_pointer, step=1e-06, parallel=False, design_space=None, normalize=True, **parallel_args)[source]

Bases: object

A gradient approximator.

Parameters
  • f_pointer (Callable[[ndarray], ndarray]) – The pointer to the function to derive.

  • step (float) –

    The default differentiation step.

    By default it is set to 1e-06.

  • parallel (bool) –

    Whether to differentiate the function in parallel.

    By default it is set to False.

  • design_space (DesignSpace | None) –

    The design space containing the upper bounds of the input variables. If None, consider that the input variables are unbounded.

    By default it is set to None.

  • normalize (bool) –

    If True, then the functions are normalized.

    By default it is set to True.

  • **parallel_args (int | bool | float) – The parallel execution options, see gemseo.core.parallel_execution.

Return type

None

f_gradient(x_vect, step=None, x_indices=None, **kwargs)[source]

Approximate the gradient of the function for a given input vector.

Parameters
  • x_vect (ndarray) – The input vector.

  • step (float | None) –

    The differentiation step. If None, use the default differentiation step.

    By default it is set to None.

  • x_indices (Sequence[int] | None) –

    The components of the input vector to be used for the differentiation. If None, use all the components.

    By default it is set to None.

  • **kwargs (Any) – The optional arguments for the function.

Returns

The approximated gradient.

Return type

ndarray

generate_perturbations(n_dim, x_vect, x_indices=None, step=None)[source]

Generate the input perturbations from the differentiation step.

These perturbations will be used to compute the output ones.

Parameters
  • n_dim (int) – The input dimension.

  • x_vect (ndarray) – The input vector.

  • x_indices (Sequence[int] | None) –

    The components of the input vector to be used for the differentiation. If None, use all the components.

    By default it is set to None.

  • step (float | None) –

    The differentiation step. If None, use the default differentiation step.

    By default it is set to None.

Returns

  • The input perturbations.

  • The differentiation step, either one global step or one step by input component.

Return type

tuple[ndarray, float | ndarray]

ALIAS = None
f_pointer: Callable[[numpy.ndarray], numpy.ndarray]

The pointer to the function to derive.

property step: float

The default approximation step.