heyoka.dtens#

class heyoka.dtens#

Class to store derivative tensors.

Note

A tutorial explaining the use of this class is available.

This class is used to store the derivative tensors computed by diff_tensors().

Methods

__init__(self)

Default constructor.

get_derivatives(self, order[, component])

Get the derivatives for the specified order and component.

hessian(self, component)

Hessian of a component.

index_of(self, vidx)

Get the position corresponding to the input indices vector.

Attributes

args

The list of arguments with respect to which the derivatives are computed.

gradient

The gradient of the function.

jacobian

The Jacobian of the function as a 2D array.

nargs

The number of arguments with respect to which the derivatives are computed.

nouts

The number of function components.

order

The maximum differentiation order.

__init__(self)#

Default constructor.

This constructor will initialise the object into an empty state.

Note that usually dtens object are returned by the invocation of diff_tensors(), rather than being constructed directly by the user.

property args#

The list of arguments with respect to which the derivatives are computed.

Return type:

list[expression]

get_derivatives(self, order: int, component: int | None = None) list[tuple[list[int], expression]]#

Get the derivatives for the specified order and component.

The derivatives are returned as a sorted list mapping vectors of indices to the corresponding derivatives, as explained in detail in the tutorial.

If an invalid order or component is provided, an empty list will be returned.

Parameters:
  • order – the differentiation order.

  • component – the function component. If not provided, the derivatives for all components will be returned.

Returns:

the list of derivatives for the desired order and component(s).

Examples:
>>> import heyoka as hy
>>> x, y = hy.make_vars("x", "y")
>>> dt = hy.diff_tensors([x**2, x*y], [x, y])
>>> dt.get_derivatives(diff_order=1) # Fetch the Jacobian.
[([0, 1, 0], (2.0000000000000000 * x)), ([0, 0, 1], 0.0000000000000000), ([1, 1, 0], y), ([1, 0, 1], x)]
>>> dt.get_derivatives(diff_order=1, component=0) # Fetch the gradient of the first component.
[([0, 1, 0], (2.0000000000000000 * x)), ([0, 0, 1], 0.0000000000000000)]
property gradient#

The gradient of the function.

Return type:

list[expression]

Raises:

ValueError – if the function has not exactly 1 component or if the maximum derivative order is zero.

hessian(self, component: int) numpy.ndarray[expression]#

Hessian of a component.

Added in version 4.0.0.

This method will return the Hessian of the selected function component as a 2D array.

Parameters:

component – the index of the function component whose Hessian will be returned.

Returns:

the Hessian of the selected component.

Raises:

ValueError – if component is invalid or if the derivative order is not at least 2.

index_of(self, vidx: list[int] | tuple[int, list[tuple[int, int]]]) int#

Get the position corresponding to the input indices vector.

This method will return the positional index of the derivative corresponding to the vector of indices vidx, which can be supplied in dense or sparse format.

If vidx is invalid, the length of self will be returned.

Parameters:

vidx – the input indices vector.

Returns:

the positional index of vidx in self.

property jacobian#

The Jacobian of the function as a 2D array.

Return type:

numpy.ndarray[expression]

Raises:

ValueError – if the function has zero components or if the maximum derivative order is zero.

property nargs#

The number of arguments with respect to which the derivatives are computed.

Return type:

int

property nouts#

The number of function components.

Return type:

int

property order#

The maximum differentiation order.

Return type:

int