bag.math.dfun
This module defines the differentiable function class.
Module Contents
Classes
An abstract class representing a differentiable scalar function. |
|
A DiffFunction where the input undergoes a linear transformation first. |
|
A DiffFunction multiply by a scalar then added to a scalar. |
|
Sum or Difference of two DiffFunctions |
|
product of two DiffFunctions |
|
division of two DiffFunctions |
|
a DiffFunction raised to a power. |
|
A differentiable vector function. |
Functions
|
- 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:
- 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.
- _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:
- __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.
- __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
- class bag.math.dfun.SumDiffFunction(f1: DiffFunction, f2: DiffFunction, f2_sgn: float = 1.0)[source]
Bases:
DiffFunction
Sum or Difference of two DiffFunctions
- Parameters:
f1 (DiffFunction) – the first function.
f2 (DiffFunction) – the second function.
f2_sgn (float) – 1 if adding, -1 if subtracting.
- __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:
f1 (DiffFunction) – the first function.
f2 (DiffFunction) – the second function.
- __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:
f1 (DiffFunction) – the first function.
f2 (DiffFunction) – the second function.
- __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.
- 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