principia_materia.mathematics.finite_difference module
- class principia_materia.mathematics.finite_difference.FiniteDifference(order, fdtype='c')
Bases:
object
Compute finite difference
- Parameters:
order (int, or tuple of int) – The order of the finite difference derivative. If input type int, compute FD of a single variable to given order. If input tuple of int, compute FD of len(order) variables, to respective order.
fdtype (choice between "c", "f" and "b") –
- Type of finite difference:
”c” for central, “f” for forward, “b” for backward.
- order
The order of derivative at each variable.
- Type:
array of int, shape(nvar, )
- delta
The delta(s) to evaluate at using the finite difference. The delta can be different at each variable.
- Type:
array of float, shape(nvar, ndelta)
- uniq_steps
The unique steps needed to compute finite difference for the given order and delta configuration.
- Type:
array of float, shape(n, nvar)
- add_delta(new_delta, decimal=6)
Add extra delta(s) and found the map from old steps to new steps.
- compute_finite_difference(fvals)
Compute finite difference
- Parameters:
fvals (array of numbers, shape (len(uniq_steps), ) or (len(uniq_steps), N...)) – Values of the target function at each step, can process multiple values simultaneously.
- Returns:
fd_result – Result of the finite difference.
- Return type:
array of numbers, shape fvals.shape[1:]
- property delta
- get_stencils_1d(order)
Compute the single variable FD stencil to given order.
Equations for coefficients on each stencil can be found on Finite difference Wikipedia page
- Parameters:
order (int) – The order of the derivative
- set_delta(delta, overwrite=False)
Set delta for finite difference computation
- Parameters:
delta (float or array of float, shape of (N, ) or (len(order), N)) –
( N != len(order) ) The delta(s) of the derivative. If pass in a float, use the same delta for each order. If pass in array:
In the case of shape(N, ), use same delta for all variables, and compute N deltas at the same time.
In the case of shape(len(order), N), use different delta for each variable, and compute N deltas at the same time.
- set_stencils()
Set stencils for FD derivatives.
Find the stencils needed to compute finite difference.
- set_stencils_1d()
Find stencils for each FD variable.
- set_steps(decimal=6)
Find unique steps needed to compute finite differece.
With the found stencils, find all the steps needed to evaluate for the target function, and find the unique steps as well as the inverse map to the full steps grid.
- Parameters:
decimal (int) – The precision to consider 2 steps equal.
- property uniq_steps