bag.math.dfun

This module defines the differentiable function class.

Module Contents

Classes

DiffFunction

An abstract class representing a differentiable scalar function.

InLinTransformFunction

A DiffFunction where the input undergoes a linear transformation first.

ScaleAddFunction

A DiffFunction multiply by a scalar then added to a scalar.

SumDiffFunction

Sum or Difference of two DiffFunctions

ProdFunction

product of two DiffFunctions

DivFunction

division of two DiffFunctions

PwrFunction

a DiffFunction raised to a power.

VectorDiffFunction

A differentiable vector function.

Functions

_intersection(*args)

class bag.math.dfun.DiffFunction(input_ranges: List[Tuple[Optional[float], Optional[float]]], delta_list: Optional[List[float]] = None)[source]

Bases: abc.ABC

An abstract class representing a differentiable scalar function.

Supports Numpy broadcasting. Defaults to using finite difference for derivative calculation.

Parameters:
  • input_ranges (List[Tuple[Optional[float], Optional[float]]]) – input ranges.

  • delta_list (Optional[List[float]]) – a list of finite difference step size for each input. If None, finite difference will be disabled.

property input_ranges: List[Tuple[Optional[float], Optional[float]]][source]
property ndim: int[source]

Number of input dimensions.

abstract __call__(xi)[source]

Interpolate at the given coordinates.

Numpy broadcasting rules apply.

Parameters:

xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The interpolated values at the given coordinates.

Return type:

np.multiarray.ndarray

get_input_range(idx: int) Tuple[Optional[float], Optional[float]][source]

Returns the input range of the given dimension.

deriv(xi, j)[source]

Calculate the derivative at the given coordinates with respect to input j.

Numpy broadcasting rules apply.

Parameters:
  • xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

  • j (int) – input index.

Returns:

val – The derivatives at the given coordinates.

Return type:

np.multiarray.ndarray

jacobian(xi)[source]

Calculate the Jacobian at the given coordinates.

Numpy broadcasting rules apply.

If finite difference step sizes are not specified, will call deriv() in a for loop to compute the Jacobian.

Parameters:

xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The Jacobian matrices at the given coordinates.

Return type:

np.multiarray.ndarray

_fd(xi, idx, delta)[source]

Calculate the derivative along the given index using central finite difference.

Parameters:
  • xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

  • idx (int) – The index to calculate the derivative on.

  • delta (float) – The finite difference step size.

Returns:

val – The derivatives at the given coordinates.

Return type:

np.multiarray.ndarray

_fd_jacobian(xi, delta_list)[source]

Calculate the Jacobian matrix using central finite difference.

Parameters:
  • xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

  • delta_list (List[float]) – list of finite difference step sizes for each input.

Returns:

val – The Jacobian matrices at the given coordinates.

Return type:

np.multiarray.ndarray

transform_input(amat: numpy.multiarray.ndarray, bmat: numpy.multiarray.ndarray) DiffFunction[source]

Returns f(Ax + B), where f is this function and A, B are matrices.

Parameters:
  • amat (np.multiarray.ndarray) – the input transform matrix.

  • bmat (np.multiarray.ndarray) – the input shift matrix.

Returns:

dfun – a scalar differential function.

Return type:

DiffFunction

__add__(other: Union[DiffFunction, float, int, numpy.multiarray.ndarray]) DiffFunction[source]
__radd__(other: Union[DiffFunction, float, int, numpy.multiarray.ndarray]) DiffFunction[source]
__sub__(other: Union[DiffFunction, float, int, numpy.multiarray.ndarray]) DiffFunction[source]
__rsub__(other: Union[DiffFunction, float, int, numpy.multiarray.ndarray]) DiffFunction[source]
__mul__(other: Union[DiffFunction, float, int, numpy.multiarray.ndarray]) DiffFunction[source]
__rmul__(other: Union[DiffFunction, float, int, numpy.multiarray.ndarray]) DiffFunction[source]
__pow__(other: Union[float, int, numpy.multiarray.ndarray]) DiffFunction[source]
__div__(other: Union[DiffFunction, float, int, numpy.multiarray.ndarray]) DiffFunction[source]
__truediv__(other: Union[DiffFunction, float, int, numpy.multiarray.ndarray]) DiffFunction[source]
__rdiv__(other: Union[DiffFunction, float, int, numpy.multiarray.ndarray]) DiffFunction[source]
__rtruediv__(other: Union[DiffFunction, float, int, numpy.multiarray.ndarray]) DiffFunction[source]
__neg__() DiffFunction[source]
class bag.math.dfun.InLinTransformFunction(f1: DiffFunction, amat: numpy.multiarray.ndarray, bmat: numpy.multiarray.ndarray)[source]

Bases: DiffFunction

A DiffFunction where the input undergoes a linear transformation first.

This function computes f(Ax + B), where A and B are matrices.

Parameters:
  • f1 (DiffFunction) – the parent function.

  • amat (np.multiarray.ndarray) – the input transform matrix.

  • bmat (np.multiarray.ndarray) – the input shift matrix.

_get_arg(xi)[source]
__call__(xi)[source]

Interpolate at the given coordinates.

Numpy broadcasting rules apply.

Parameters:

xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The interpolated values at the given coordinates.

Return type:

np.multiarray.ndarray

deriv(xi, j)[source]

Calculate the derivative at the given coordinates with respect to input j.

Numpy broadcasting rules apply.

Parameters:
  • xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

  • j (int) – input index.

Returns:

val – The derivatives at the given coordinates.

Return type:

np.multiarray.ndarray

jacobian(xi)[source]

Calculate the Jacobian at the given coordinates.

Numpy broadcasting rules apply.

If finite difference step sizes are not specified, will call deriv() in a for loop to compute the Jacobian.

Parameters:

xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The Jacobian matrices at the given coordinates.

Return type:

np.multiarray.ndarray

class bag.math.dfun.ScaleAddFunction(f1: DiffFunction, adder: float, scaler: float)[source]

Bases: DiffFunction

A DiffFunction multiply by a scalar then added to a scalar.

Parameters:
  • f1 (DiffFunction) – the first function.

  • adder (float) – constant to add.

  • scaler (float) – constant to multiply.

__call__(xi)[source]

Interpolate at the given coordinates.

Numpy broadcasting rules apply.

Parameters:

xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The interpolated values at the given coordinates.

Return type:

np.multiarray.ndarray

deriv(xi, j)[source]

Calculate the derivative at the given coordinates with respect to input j.

Numpy broadcasting rules apply.

Parameters:
  • xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

  • j (int) – input index.

Returns:

val – The derivatives at the given coordinates.

Return type:

np.multiarray.ndarray

jacobian(xi)[source]

Calculate the Jacobian at the given coordinates.

Numpy broadcasting rules apply.

If finite difference step sizes are not specified, will call deriv() in a for loop to compute the Jacobian.

Parameters:

xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The Jacobian matrices at the given coordinates.

Return type:

np.multiarray.ndarray

bag.math.dfun._intersection(*args)[source]
class bag.math.dfun.SumDiffFunction(f1: DiffFunction, f2: DiffFunction, f2_sgn: float = 1.0)[source]

Bases: DiffFunction

Sum or Difference of two DiffFunctions

Parameters:
__call__(xi)[source]

Interpolate at the given coordinates.

Numpy broadcasting rules apply.

Parameters:

xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The interpolated values at the given coordinates.

Return type:

np.multiarray.ndarray

deriv(xi, j)[source]

Calculate the derivative at the given coordinates with respect to input j.

Numpy broadcasting rules apply.

Parameters:
  • xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

  • j (int) – input index.

Returns:

val – The derivatives at the given coordinates.

Return type:

np.multiarray.ndarray

jacobian(xi)[source]

Calculate the Jacobian at the given coordinates.

Numpy broadcasting rules apply.

If finite difference step sizes are not specified, will call deriv() in a for loop to compute the Jacobian.

Parameters:

xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The Jacobian matrices at the given coordinates.

Return type:

np.multiarray.ndarray

class bag.math.dfun.ProdFunction(f1: DiffFunction, f2: DiffFunction)[source]

Bases: DiffFunction

product of two DiffFunctions

Parameters:
__call__(xi)[source]

Interpolate at the given coordinates.

Numpy broadcasting rules apply.

Parameters:

xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The interpolated values at the given coordinates.

Return type:

np.multiarray.ndarray

deriv(xi, j)[source]

Calculate the derivative at the given coordinates with respect to input j.

Numpy broadcasting rules apply.

Parameters:
  • xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

  • j (int) – input index.

Returns:

val – The derivatives at the given coordinates.

Return type:

np.multiarray.ndarray

jacobian(xi)[source]

Calculate the Jacobian at the given coordinates.

Numpy broadcasting rules apply.

If finite difference step sizes are not specified, will call deriv() in a for loop to compute the Jacobian.

Parameters:

xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The Jacobian matrices at the given coordinates.

Return type:

np.multiarray.ndarray

class bag.math.dfun.DivFunction(f1: DiffFunction, f2: DiffFunction)[source]

Bases: DiffFunction

division of two DiffFunctions

Parameters:
__call__(xi)[source]

Interpolate at the given coordinates.

Numpy broadcasting rules apply.

Parameters:

xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The interpolated values at the given coordinates.

Return type:

np.multiarray.ndarray

deriv(xi, j)[source]

Calculate the derivative at the given coordinates with respect to input j.

Numpy broadcasting rules apply.

Parameters:
  • xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

  • j (int) – input index.

Returns:

val – The derivatives at the given coordinates.

Return type:

np.multiarray.ndarray

jacobian(xi)[source]

Calculate the Jacobian at the given coordinates.

Numpy broadcasting rules apply.

If finite difference step sizes are not specified, will call deriv() in a for loop to compute the Jacobian.

Parameters:

xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The Jacobian matrices at the given coordinates.

Return type:

np.multiarray.ndarray

class bag.math.dfun.PwrFunction(f: DiffFunction, pwr: float, scale: float = 1.0)[source]

Bases: DiffFunction

a DiffFunction raised to a power.

Parameters:
  • f (DiffFunction) – the DiffFunction.

  • pwr (float) – the power.

  • scale (float) – scaling factor. Used to implement a / x.

__call__(xi)[source]

Interpolate at the given coordinates.

Numpy broadcasting rules apply.

Parameters:

xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The interpolated values at the given coordinates.

Return type:

np.multiarray.ndarray

deriv(xi, j)[source]

Calculate the derivative at the given coordinates with respect to input j.

Numpy broadcasting rules apply.

Parameters:
  • xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

  • j (int) – input index.

Returns:

val – The derivatives at the given coordinates.

Return type:

np.multiarray.ndarray

jacobian(xi)[source]

Calculate the Jacobian at the given coordinates.

Numpy broadcasting rules apply.

If finite difference step sizes are not specified, will call deriv() in a for loop to compute the Jacobian.

Parameters:

xi (array_like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The Jacobian matrices at the given coordinates.

Return type:

np.multiarray.ndarray

class bag.math.dfun.VectorDiffFunction(fun_list: List[DiffFunction])[source]

Bases: object

A differentiable vector function.

Parameters:

fun_list (List[DiffFunction]) – list of interpolator functions, one for each element of the output vector.

property in_dim: int[source]

Input dimension number.

property out_dim: int[source]

Output dimension number.

get_input_range(idx: int) Tuple[Optional[float], Optional[float]][source]

Returns the input range of the given dimension.

__call__(xi)[source]

Returns the output vector at the given coordinates.

Parameters:

xi (array-like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The interpolated values at the given coordinates.

Return type:

numpy.array

jacobian(xi)[source]

Calculate the Jacobian matrices of this function at the given coordinates.

Parameters:

xi (array-like) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The jacobian matrix at the given coordinates.

Return type:

numpy.array

deriv(xi, i, j)[source]

Compute the derivative of output i with respect to input j

Parameters:
  • xi (array-like) – The coordinates to evaluate, with shape (…, ndim)

  • i (int) – output index.

  • j (int) – input index.

Returns:

val – The derivatives at the given coordinates.

Return type:

numpy.array