gemseo / core

func_operations module

Functional operations

Classes:

LinerarComposition(orig_function, …)

Composes a function with a linear operator defined by a matrix computes orig_f(Mat.dot(x))

RestrictedFunction(orig_function, …)

Restrict an MDOFunction to a subset of its input vector Fixes the rest of the indices.

class gemseo.core.func_operations.LinerarComposition(orig_function, interp_operator)[source]

Bases: gemseo.core.function.MDOFunction

Composes a function with a linear operator defined by a matrix computes orig_f(Mat.dot(x))

Constructor.

Parameters
  • orig_function – the original function to restrict

  • interp_operator – operator matrix, the output of the function will be f(interp_operator.dot(x))

Attributes:

AVAILABLE_TYPES

COEFF_FORMAT_1D

COEFF_FORMAT_ND

DEFAULT_ARGS_BASE

DICT_REPR_ATTR

INDEX_PREFIX

TYPE_EQ

TYPE_INEQ

TYPE_OBJ

TYPE_OBS

args

Accessor to the arguments list.

default_repr

dim

Accessor to the dimension.

expr

Accessor to the expression.

f_type

Accessor to the function type.

func

Accessor to the func property.

jac

Accessor to the jacobian.

n_calls

Returns the number of calls to execute() which triggered the _run() multiprocessing safe.

name

Accessor to the name of the function.

outvars

Accessor to the array of input variable names used by the function.

Methods:

apply_operator(other_f, operator, operator_repr)

Defines addition/substraction for MDOFunction Supports automatic differentiation if other_f and self have a Jacobian.

check_grad(x_vect[, method, step, error_max])

Checks the gradient of self.

concatenate(functions, name[, f_type])

Concatenate functions.

convex_linear_approx(x_vect[, …])

Return the convex linearization of the function at a given point

evaluate(x_vect[, force_real])

Evaluate the function.

generate_args(input_dim[, args])

Generate the strings for a function arguments.

get_data_dict_repr()

Returns a dict representation of self for serialization Pointers to functions are removed.

has_args()

Check if MDOFunction has an args.

has_dim()

Check if MDOFunction has a dimension.

has_expr()

Check if MDOFunction has an expression.

has_f_type()

Check if MDOFunction has an type.

has_jac()

Check if MDOFunction has a jacobian.

has_outvars()

Check if MDOFunction has an array of input variables.

init_from_dict_repr(**kwargs)

Initalizes a new Function from a data dict typically used for deserialization.

is_constraint()

Returns True if self.f_type is eq or ineq.

linear_approximation(x_vect[, name, f_type, …])

Return the first-order Taylor polynomial of the function at a given point

offset(value)

Adds an offset value to the function.

quadratic_approx(x_vect, hessian_approx[, args])

Return a quadratic appproximation of the (scalar-valued) function at a given point

restrict(frozen_indexes, frozen_values, …)

Return a restriction of the function

set_pt_from_database(database, design_space)

self.__call__(x) returns f(x) if x is in the database and self.name in the database keys Idem for jac.

AVAILABLE_TYPES = ['obj', 'eq', 'ineq', 'obs']
COEFF_FORMAT_1D = '{:.2e}'
COEFF_FORMAT_ND = '{: .2e}'
DEFAULT_ARGS_BASE = 'x'
DICT_REPR_ATTR = ['name', 'f_type', 'expr', 'args', 'dim', 'special_repr']
INDEX_PREFIX = '!'
TYPE_EQ = 'eq'
TYPE_INEQ = 'ineq'
TYPE_OBJ = 'obj'
TYPE_OBS = 'obs'
apply_operator(other_f, operator, operator_repr)

Defines addition/substraction for MDOFunction Supports automatic differentiation if other_f and self have a Jacobian.

Parameters
  • other_f – param operator:

  • operator_repr – the representation as a string

  • operator – the operator as a function pointer

property args

Accessor to the arguments list.

check_grad(x_vect, method='FirstOrderFD', step=1e-06, error_max=1e-08)

Checks the gradient of self.

Parameters
  • x_vect (ndarray) – the vector at which the function is checked

  • method (str, optional) – FirstOrderFD or ComplexStep (Default value = “FirstOrderFD”)

  • step (float, optional) – the step for approximation (Default value = 1e-6)

  • error_max (float, optional) – Default value = 1e-8)

static concatenate(functions, name, f_type=None)

Concatenate functions.

Parameters
  • functions (list(MDOFunction)) – functions to be concatenated

  • name (str) – name of the concatenation

  • f_type (str, optional) – type of the concatenation function

Returns

concatenation of the functions

Return type

MDOFunction

convex_linear_approx(x_vect, approx_indexes=None, sign_threshold=1e-09)

Return the convex linearization of the function at a given point

\(\newcommand{\xref}{\hat{x}}\newcommand{\dim}{d}\) The convex linearization of a function \(f\) at a point \(\xref\) is defined as

\[\begin{split}\newcommand{\partialder}{\frac{\partial f}{\partial x_i}(\xref)} f(x) \approx f(\xref) + \sum_{\substack{i = 1 \\ \partialder > 0}}^{\dim} \partialder \, (x_i - \xref_i) - \sum_{\substack{i = 1 \\ \partialder < 0}}^{\dim} \partialder \, \xref_i^2 \, \left(\frac{1}{x_i} - \frac{1}{\xref_i}\right).\end{split}\]

\(\newcommand{\approxinds}{I}\) Optionally, one may require the convex linearization of \(f\) with respect to a subset \(x_{i \in \approxinds} \subset \{x_1, \dots, x_{\dim}\}\) of its variables rather than all of them:

\[\begin{split}f(x) = f(x_{i \in \approxinds}, x_{i \not\in \approxinds}) \approx f(\xref_{i \in \approxinds}, x_{i \not\in \approxinds}) + \sum_{\substack{i \in \approxinds \\ \partialder > 0}} \partialder \, (x_i - \xref_i) - \sum_{\substack{i \in \approxinds \\ \partialder < 0}} \partialder \, \xref_i^2 \, \left(\frac{1}{x_i} - \frac{1}{\xref_i}\right).\end{split}\]
Parameters
  • x_vect (ndarray) – point defining the convex linearization

  • approx_indexes (ndarray, optional) – array of booleans specifying w.r.t. which variables the function should be approximated (by default, all of them)

  • sign_threshold (float, optional) – threshold for the sign of the derivatives

Returns

convex linearization of the function at the given point

Return type

MDOFunction

property default_repr
property dim

Accessor to the dimension.

evaluate(x_vect, force_real=False)

Evaluate the function.

Parameters
  • x_vect (ndarray) – the function argument

  • force_real (bool, optional) – if True, cast the results to real value

Returns

the function value

Return type

Number or ndarray

property expr

Accessor to the expression.

property f_type

Accessor to the function type.

property func

Accessor to the func property.

static generate_args(input_dim, args=None)

Generate the strings for a function arguments.

Parameters
  • input_dim (int) – number of scalar input arguments

  • args (list(str), optional) – the initial function arguments strings

Returns

the arguments strings

Return type

list(str)

get_data_dict_repr()

Returns a dict representation of self for serialization Pointers to functions are removed.

Returns

a dict with attributes names as keys

Return type

dict

has_args()

Check if MDOFunction has an args.

Returns

True if self has an args

Return type

bool

has_dim()

Check if MDOFunction has a dimension.

Returns

True if self has a dimension

Return type

bool

has_expr()

Check if MDOFunction has an expression.

Returns

True if self has an expression

Return type

bool

has_f_type()

Check if MDOFunction has an type.

Returns

True if self has a type

Return type

bool

has_jac()

Check if MDOFunction has a jacobian.

Returns

True if self has a jacobian

Return type

bool

has_outvars()

Check if MDOFunction has an array of input variables.

Returns

True if self has a dimension

Return type

bool

static init_from_dict_repr(**kwargs)

Initalizes a new Function from a data dict typically used for deserialization.

Parameters

kwargs – key value pairs from DICT_REPR_ATTR

is_constraint()

Returns True if self.f_type is eq or ineq.

Returns

True if and only if the function is a contraint

Return type

bool

property jac

Accessor to the jacobian.

linear_approximation(x_vect, name=None, f_type=None, args=None)

Return the first-order Taylor polynomial of the function at a given point

\(\newcommand{\xref}{\hat{x}}\newcommand{\dim}{d}\) The first-order Taylor polynomial of a (possibly vector-valued) function \(f\) at a point \(\xref\) is defined as

\[\newcommand{\partialder}{\frac{\partial f}{\partial x_i}(\xref)} f(x) \approx f(\xref) + \sum_{i = 1}^{\dim} \partialder \, (x_i - \xref_i).\]
Parameters
  • x_vect (ndarray) – point defining the Taylor polynomial

  • name (str, optional) – name of the linear approximation

  • f_type (str, optional) – the function type of the linear approximation

  • args (list(str), optional) – names for each scalar variable, or a name base

Returns

first-order Taylor polynomial of the function at the given point

Return type

MDOLinearFunction

property n_calls

Returns the number of calls to execute() which triggered the _run() multiprocessing safe.

property name

Accessor to the name of the function.

offset(value)

Adds an offset value to the function.

Parameters

value (Number or ndarray) – the offset value

Returns

the offset function as an MDOFunction

Return type

MDOFunction

property outvars

Accessor to the array of input variable names used by the function.

quadratic_approx(x_vect, hessian_approx, args=None)

Return a quadratic appproximation of the (scalar-valued) function at a given point

\(\newcommand{\xref}{\hat{x}}\newcommand{\dim}{d}\newcommand{ \hessapprox}{\hat{H}}\) For a given approximation \(\hessapprox\) of the Hessian matrix of a function \(f\) at a point \(\xref\), the quadratic approximation of \(f\) is defined as

\[\newcommand{\partialder}{\frac{\partial f}{\partial x_i}(\xref)} f(x) \approx f(\xref) + \sum_{i = 1}^{\dim} \partialder \, (x_i - \xref_i) + \frac{1}{2} \sum_{i = 1}^{\dim} \sum_{j = 1}^{\dim} \hessapprox_{ij} (x_i - \xref_i) (x_j - \xref_j).\]
Parameters
  • x_vect (ndarray) – point defining the Taylor polynomial

  • hessian_approx (ndarray) – approximation of the Hessian matrix at x_vect

  • args (list(str), optional) – names for each scalar variable, or a name base

Returns

second-order Taylor polynomial of the function at the given point

Return type

MDOQuadraticFunction

restrict(frozen_indexes, frozen_values, input_dim, name=None, f_type=None, expr=None, args=None)

Return a restriction of the function

\(\newcommand{\frozeninds}{I}\newcommand{\xfrozen}{\hat{x}}\newcommand{ \frestr}{\hat{f}}\) For a subset \(\approxinds\) of the variables indexes of a function \(f\) to remain frozen at values \(\xfrozen_{i \in \frozeninds}\) the restriction of \(f\) is given by

\[\frestr: x_{i \not\in \approxinds} \longmapsto f(\xref_{i \in \approxinds}, x_{i \not\in \approxinds}).\]
Parameters
  • frozen_indexes (ndarray) – indexes of the variables that will be frozen

  • frozen_values (ndarray) – values of the variables that will be frozen

  • input_dim (int) – dimension of the function input (before restriction)

  • name (str, optional) – name of the restriction

  • f_type (str, optional) – type of the restriction

  • expr (str, optional) – the expression of the restriction

  • args (list(str), optional) – arguments names of the restriction

Returns

restriction of the function

Return type

MDOFunction

set_pt_from_database(database, design_space, normalize=False, jac=True, x_tolerance=1e-10)

self.__call__(x) returns f(x) if x is in the database and self.name in the database keys Idem for jac.

Parameters
  • database (Database) – the database to read

  • design_space (DesignSpace) – the design space used for normalization

  • normalize (bool, optional) – if True, x_n is unnormalized before call

  • jac (bool, optional) – if True, a jacobian pointer is also generated

  • x_tolerance (float, optional) – tolerance on the distance between inputs

class gemseo.core.func_operations.RestrictedFunction(orig_function, restriction_indices, restriction_values)[source]

Bases: gemseo.core.function.MDOFunction

Restrict an MDOFunction to a subset of its input vector Fixes the rest of the indices.

Constructor.

Parameters
  • orig_function – the original function to restrict

  • restriction_indices – indices array of the input vector to fix

  • restriction_values – the values of the input vector at indices ‘restriction_indices’ are set to restriction_values

Attributes:

AVAILABLE_TYPES

COEFF_FORMAT_1D

COEFF_FORMAT_ND

DEFAULT_ARGS_BASE

DICT_REPR_ATTR

INDEX_PREFIX

TYPE_EQ

TYPE_INEQ

TYPE_OBJ

TYPE_OBS

args

Accessor to the arguments list.

default_repr

dim

Accessor to the dimension.

expr

Accessor to the expression.

f_type

Accessor to the function type.

func

Accessor to the func property.

jac

Accessor to the jacobian.

n_calls

Returns the number of calls to execute() which triggered the _run() multiprocessing safe.

name

Accessor to the name of the function.

outvars

Accessor to the array of input variable names used by the function.

Methods:

apply_operator(other_f, operator, operator_repr)

Defines addition/substraction for MDOFunction Supports automatic differentiation if other_f and self have a Jacobian.

check_grad(x_vect[, method, step, error_max])

Checks the gradient of self.

concatenate(functions, name[, f_type])

Concatenate functions.

convex_linear_approx(x_vect[, …])

Return the convex linearization of the function at a given point

evaluate(x_vect[, force_real])

Evaluate the function.

generate_args(input_dim[, args])

Generate the strings for a function arguments.

get_data_dict_repr()

Returns a dict representation of self for serialization Pointers to functions are removed.

has_args()

Check if MDOFunction has an args.

has_dim()

Check if MDOFunction has a dimension.

has_expr()

Check if MDOFunction has an expression.

has_f_type()

Check if MDOFunction has an type.

has_jac()

Check if MDOFunction has a jacobian.

has_outvars()

Check if MDOFunction has an array of input variables.

init_from_dict_repr(**kwargs)

Initalizes a new Function from a data dict typically used for deserialization.

is_constraint()

Returns True if self.f_type is eq or ineq.

linear_approximation(x_vect[, name, f_type, …])

Return the first-order Taylor polynomial of the function at a given point

offset(value)

Adds an offset value to the function.

quadratic_approx(x_vect, hessian_approx[, args])

Return a quadratic appproximation of the (scalar-valued) function at a given point

restrict(frozen_indexes, frozen_values, …)

Return a restriction of the function

set_pt_from_database(database, design_space)

self.__call__(x) returns f(x) if x is in the database and self.name in the database keys Idem for jac.

AVAILABLE_TYPES = ['obj', 'eq', 'ineq', 'obs']
COEFF_FORMAT_1D = '{:.2e}'
COEFF_FORMAT_ND = '{: .2e}'
DEFAULT_ARGS_BASE = 'x'
DICT_REPR_ATTR = ['name', 'f_type', 'expr', 'args', 'dim', 'special_repr']
INDEX_PREFIX = '!'
TYPE_EQ = 'eq'
TYPE_INEQ = 'ineq'
TYPE_OBJ = 'obj'
TYPE_OBS = 'obs'
apply_operator(other_f, operator, operator_repr)

Defines addition/substraction for MDOFunction Supports automatic differentiation if other_f and self have a Jacobian.

Parameters
  • other_f – param operator:

  • operator_repr – the representation as a string

  • operator – the operator as a function pointer

property args

Accessor to the arguments list.

check_grad(x_vect, method='FirstOrderFD', step=1e-06, error_max=1e-08)

Checks the gradient of self.

Parameters
  • x_vect (ndarray) – the vector at which the function is checked

  • method (str, optional) – FirstOrderFD or ComplexStep (Default value = “FirstOrderFD”)

  • step (float, optional) – the step for approximation (Default value = 1e-6)

  • error_max (float, optional) – Default value = 1e-8)

static concatenate(functions, name, f_type=None)

Concatenate functions.

Parameters
  • functions (list(MDOFunction)) – functions to be concatenated

  • name (str) – name of the concatenation

  • f_type (str, optional) – type of the concatenation function

Returns

concatenation of the functions

Return type

MDOFunction

convex_linear_approx(x_vect, approx_indexes=None, sign_threshold=1e-09)

Return the convex linearization of the function at a given point

\(\newcommand{\xref}{\hat{x}}\newcommand{\dim}{d}\) The convex linearization of a function \(f\) at a point \(\xref\) is defined as

\[\begin{split}\newcommand{\partialder}{\frac{\partial f}{\partial x_i}(\xref)} f(x) \approx f(\xref) + \sum_{\substack{i = 1 \\ \partialder > 0}}^{\dim} \partialder \, (x_i - \xref_i) - \sum_{\substack{i = 1 \\ \partialder < 0}}^{\dim} \partialder \, \xref_i^2 \, \left(\frac{1}{x_i} - \frac{1}{\xref_i}\right).\end{split}\]

\(\newcommand{\approxinds}{I}\) Optionally, one may require the convex linearization of \(f\) with respect to a subset \(x_{i \in \approxinds} \subset \{x_1, \dots, x_{\dim}\}\) of its variables rather than all of them:

\[\begin{split}f(x) = f(x_{i \in \approxinds}, x_{i \not\in \approxinds}) \approx f(\xref_{i \in \approxinds}, x_{i \not\in \approxinds}) + \sum_{\substack{i \in \approxinds \\ \partialder > 0}} \partialder \, (x_i - \xref_i) - \sum_{\substack{i \in \approxinds \\ \partialder < 0}} \partialder \, \xref_i^2 \, \left(\frac{1}{x_i} - \frac{1}{\xref_i}\right).\end{split}\]
Parameters
  • x_vect (ndarray) – point defining the convex linearization

  • approx_indexes (ndarray, optional) – array of booleans specifying w.r.t. which variables the function should be approximated (by default, all of them)

  • sign_threshold (float, optional) – threshold for the sign of the derivatives

Returns

convex linearization of the function at the given point

Return type

MDOFunction

property default_repr
property dim

Accessor to the dimension.

evaluate(x_vect, force_real=False)

Evaluate the function.

Parameters
  • x_vect (ndarray) – the function argument

  • force_real (bool, optional) – if True, cast the results to real value

Returns

the function value

Return type

Number or ndarray

property expr

Accessor to the expression.

property f_type

Accessor to the function type.

property func

Accessor to the func property.

static generate_args(input_dim, args=None)

Generate the strings for a function arguments.

Parameters
  • input_dim (int) – number of scalar input arguments

  • args (list(str), optional) – the initial function arguments strings

Returns

the arguments strings

Return type

list(str)

get_data_dict_repr()

Returns a dict representation of self for serialization Pointers to functions are removed.

Returns

a dict with attributes names as keys

Return type

dict

has_args()

Check if MDOFunction has an args.

Returns

True if self has an args

Return type

bool

has_dim()

Check if MDOFunction has a dimension.

Returns

True if self has a dimension

Return type

bool

has_expr()

Check if MDOFunction has an expression.

Returns

True if self has an expression

Return type

bool

has_f_type()

Check if MDOFunction has an type.

Returns

True if self has a type

Return type

bool

has_jac()

Check if MDOFunction has a jacobian.

Returns

True if self has a jacobian

Return type

bool

has_outvars()

Check if MDOFunction has an array of input variables.

Returns

True if self has a dimension

Return type

bool

static init_from_dict_repr(**kwargs)

Initalizes a new Function from a data dict typically used for deserialization.

Parameters

kwargs – key value pairs from DICT_REPR_ATTR

is_constraint()

Returns True if self.f_type is eq or ineq.

Returns

True if and only if the function is a contraint

Return type

bool

property jac

Accessor to the jacobian.

linear_approximation(x_vect, name=None, f_type=None, args=None)

Return the first-order Taylor polynomial of the function at a given point

\(\newcommand{\xref}{\hat{x}}\newcommand{\dim}{d}\) The first-order Taylor polynomial of a (possibly vector-valued) function \(f\) at a point \(\xref\) is defined as

\[\newcommand{\partialder}{\frac{\partial f}{\partial x_i}(\xref)} f(x) \approx f(\xref) + \sum_{i = 1}^{\dim} \partialder \, (x_i - \xref_i).\]
Parameters
  • x_vect (ndarray) – point defining the Taylor polynomial

  • name (str, optional) – name of the linear approximation

  • f_type (str, optional) – the function type of the linear approximation

  • args (list(str), optional) – names for each scalar variable, or a name base

Returns

first-order Taylor polynomial of the function at the given point

Return type

MDOLinearFunction

property n_calls

Returns the number of calls to execute() which triggered the _run() multiprocessing safe.

property name

Accessor to the name of the function.

offset(value)

Adds an offset value to the function.

Parameters

value (Number or ndarray) – the offset value

Returns

the offset function as an MDOFunction

Return type

MDOFunction

property outvars

Accessor to the array of input variable names used by the function.

quadratic_approx(x_vect, hessian_approx, args=None)

Return a quadratic appproximation of the (scalar-valued) function at a given point

\(\newcommand{\xref}{\hat{x}}\newcommand{\dim}{d}\newcommand{ \hessapprox}{\hat{H}}\) For a given approximation \(\hessapprox\) of the Hessian matrix of a function \(f\) at a point \(\xref\), the quadratic approximation of \(f\) is defined as

\[\newcommand{\partialder}{\frac{\partial f}{\partial x_i}(\xref)} f(x) \approx f(\xref) + \sum_{i = 1}^{\dim} \partialder \, (x_i - \xref_i) + \frac{1}{2} \sum_{i = 1}^{\dim} \sum_{j = 1}^{\dim} \hessapprox_{ij} (x_i - \xref_i) (x_j - \xref_j).\]
Parameters
  • x_vect (ndarray) – point defining the Taylor polynomial

  • hessian_approx (ndarray) – approximation of the Hessian matrix at x_vect

  • args (list(str), optional) – names for each scalar variable, or a name base

Returns

second-order Taylor polynomial of the function at the given point

Return type

MDOQuadraticFunction

restrict(frozen_indexes, frozen_values, input_dim, name=None, f_type=None, expr=None, args=None)

Return a restriction of the function

\(\newcommand{\frozeninds}{I}\newcommand{\xfrozen}{\hat{x}}\newcommand{ \frestr}{\hat{f}}\) For a subset \(\approxinds\) of the variables indexes of a function \(f\) to remain frozen at values \(\xfrozen_{i \in \frozeninds}\) the restriction of \(f\) is given by

\[\frestr: x_{i \not\in \approxinds} \longmapsto f(\xref_{i \in \approxinds}, x_{i \not\in \approxinds}).\]
Parameters
  • frozen_indexes (ndarray) – indexes of the variables that will be frozen

  • frozen_values (ndarray) – values of the variables that will be frozen

  • input_dim (int) – dimension of the function input (before restriction)

  • name (str, optional) – name of the restriction

  • f_type (str, optional) – type of the restriction

  • expr (str, optional) – the expression of the restriction

  • args (list(str), optional) – arguments names of the restriction

Returns

restriction of the function

Return type

MDOFunction

set_pt_from_database(database, design_space, normalize=False, jac=True, x_tolerance=1e-10)

self.__call__(x) returns f(x) if x is in the database and self.name in the database keys Idem for jac.

Parameters
  • database (Database) – the database to read

  • design_space (DesignSpace) – the design space used for normalization

  • normalize (bool, optional) – if True, x_n is unnormalized before call

  • jac (bool, optional) – if True, a jacobian pointer is also generated

  • x_tolerance (float, optional) – tolerance on the distance between inputs