pub enum DiffScheme {
Forward,
Backward,
Central,
Central4th,
}Expand description
Which finite-difference stencil to use.
The choice is a cost against accuracy trade, and both halves are measured — see the module-level “Achievable accuracy” table for the observed error floors and “Cost” for the evaluation counts.
§Units
Dimensionless — a mode selector, not a quantity.
Variants§
Forward
(f(x + h) - f(x)) / h. Truncation error O(h).
The cheapest scheme for a Jacobian: the base evaluation f(x) is shared
by every column, so an n-dimensional Jacobian costs n + 1
evaluations rather than 2n.
Backward
(f(x) - f(x - h)) / h. Truncation error O(h).
Same cost and accuracy as Forward; it exists for
callers whose function is undefined or unphysical just above x — a
saturation pressure at the phase boundary, a volume fraction at 1.
Central
(f(x + h) - f(x - h)) / (2h). Truncation error O(h^2). The
default.
This is the scheme both existing workspace implementations use — see the module-level “Provenance” section.
Central4th
Richardson extrapolation of two central differences, (4*D(h/2) - D(h)) / 3. Truncation error O(h^4).
The most accurate scheme here and the most expensive: 4 evaluations per
derivative and 4n per Jacobian, because the h and h/2 stencils
share no points.
Implementations§
Source§impl DiffScheme
impl DiffScheme
Sourcepub fn default_relative_step(self) -> f64
pub fn default_relative_step(self) -> f64
The relative step size that balances truncation against round-off for
this scheme in f64.
Truncation error goes as h^p and round-off as eps/h, so the balance
is at h ~ eps^(1/(p+1)):
| Scheme | Order p | Optimal relative step |
|---|---|---|
Forward, Backward | 1 | crate::math::minimise::SQRT_EPSILON = 1.4901161193847656e-8 |
Central | 2 | CBRT_EPSILON = 6.0554544523933395e-6 |
Central4th | 4 | FIFTH_ROOT_EPSILON = 7.40095979741405e-4 |
§Units
Dimensionless — it multiplies a length scale in x to give a step in
x.
Sourcepub fn evaluations_per_derivative(self) -> usize
pub fn evaluations_per_derivative(self) -> usize
Sourcepub fn evaluations_per_jacobian(self, n: usize) -> usize
pub fn evaluations_per_jacobian(self, n: usize) -> usize
How many evaluations of the vector function an n-dimensional Jacobian
costs.
Forward and Backward get n + 1
because the unperturbed evaluation is shared across all n columns;
Central gets 2n and Central4th
4n because their stencils are symmetric about x and so share nothing.
§Arguments
n— the dimension of the point, dimensionless.
§Units
A count, dimensionless.
§Example
use outram_foam_basic_lib::math::differentiate::DiffScheme;
assert_eq!(DiffScheme::Forward.evaluations_per_jacobian(10), 11);
assert_eq!(DiffScheme::Central.evaluations_per_jacobian(10), 20);
assert_eq!(DiffScheme::Central4th.evaluations_per_jacobian(10), 40);