scotty.derivatives module
scotty.derivatives module#
- scotty.derivatives.STENCILS: Dict[str, Dict[Tuple, float]] = {'d1_CFD2': {(-1,): -0.5, (1,): 0.5}, 'd1_CFD4': {(-2,): 0.08333333333333333, (-1,): -0.6666666666666666, (1,): 0.6666666666666666, (2,): -0.08333333333333333}, 'd1_FFD2': {(0,): -1.5, (1,): 2, (2,): -0.5}, 'd1d1_CFD_CFD2': {(-1, -1): 0.25, (-1, 1): -0.25, (1, -1): -0.25, (1, 1): 0.25}, 'd1d1_FFD_CFD2': {(0, -1): 0.25, (0, 0): 1, (0, 1): -1.25, (1, 0): -2, (1, 1): 2, (2, -1): -0.25, (2, 0): 1, (2, 1): -0.75}, 'd1d1_FFD_FFD2': {(0, 0): 2.25, (0, 1): -3, (0, 2): 0.75, (1, 0): -3, (1, 1): 4, (1, 2): -1, (2, 0): 0.75, (2, 1): -1, (2, 2): 0.25}, 'd2_CFD2': {(-1,): 1, (0,): -2, (1,): 1}, 'd2_FFD2': {(0,): 2, (1,): -5, (2,): 4, (3,): -1}}#
Finite difference stencils for
derivative
.The second-derivative stencils are optimised for minimal function evaluations.
The naming scheme here is
<order>_<kind><error>
with:<order>
isd<n>
for the ``n``th derivative, and this is repeated for multiple dimensions<kind>
is eitherFFD
for forward finite difference orCFD
for central finite difference, again repeated for each dimension<error>
is the order of the error scaling
So
d1d1_FFD_CFD2
is the first mixed-derivative of two dimensions, using forward differences for the first dimension and central for the second, with second-order error overall
- scotty.derivatives.cache(func)#
Simple caching of a function compatible with numpy array arguments
- scotty.derivatives.derivative(func, dims, args, spacings=1e-08, stencil=None, use_cache=True)#
Partial derivative of a function along one or more of its arguments.
Currently this can take partial derivatives in one or two arguments, given by
dims
and evaluated atargs
. The arguments tofunc
must be exactly the keys ofargs
, andfunc
must be able to take them as keyword arguments.The function will be evaluated at points around
args
given by the integer offsets in the stencil multiplied by the step size in each dimension given inspacings
.By default, calls to
func
with a given set of arguments will be cached, which enables re-use of evaluation points across multiple derivatives. This is particularly useful when needing to take partial derivatives in many directions. However, iffunc
has side effects, such as printing variables, these will not work, and you should setuse_cache = False
.- Parameters
func (Callable) – Function to take the derivative of
dims (Union[str, Tuple[str, ...]]) – The name(s) of the dimension(s) to take the derivative along.
dims
must appear in the keys ofargs
. For second derivatives in one dimension, pass a tuple with the argument name repeated, for example("x", "x")
for the second derivative inx
args (Dict[str, Union[float, numpy.ndarray[Any, numpy.dtype[numpy.float64]]]]) – Arguments to
func
at the point to evaluate the derivativespacings (Union[float, Dict[str, float]]) – Step size for derivative. If a single float, used for all dimensions, otherwise if a mapping of dimension names to step size then the keys must be identical to those of
dims
stencil (Optional[str]) – Stencil name (see
STENCILS
for supported stencils). Defaults to central differencesuse_cache (bool) – If true, then wraps
func
with the decoratorcache
- Returns
Derivative of
func
evaluated atlocation
- Return type
ArrayLike
Examples
>>> derivative(lambda x: np.sin(x), "x", {"x": 0.0}) 1.0
>>> derivative(lambda x, y: np.sin(x) * y**2, ("x", "y"), {"x": 0.0, "y": 2.0}) 4.0