Skip to main content

gradient

Function gradient 

Source
pub fn gradient<F>(f: &F, point: &[f64], h: f64) -> Vec<f64>
where F: Fn(&[f64]) -> f64,
Expand description

Gradient (vector derivative) of a scalar function

§Arguments

  • f - Function of multiple variables
  • point - Point at which to evaluate gradient
  • h - Step size

§Example

use dodecet_encoder::calculus;

let f = |p: &[f64]| p[0] * p[0] + p[1] * p[1]; // x² + y²
let point = vec![1.0, 2.0];
let grad = calculus::gradient(&f, &point, 0.01);
assert!((grad[0] - 2.0).abs() < 0.1); // ∂f/∂x = 2x = 2
assert!((grad[1] - 4.0).abs() < 0.1); // ∂f/∂y = 2y = 4