pub struct Calculus;Implementations§
Source§impl Calculus
impl Calculus
Sourcepub fn derivative<F: Fn(f64) -> f64>(f: F, x: f64, h: f64) -> f64
pub fn derivative<F: Fn(f64) -> f64>(f: F, x: f64, h: f64) -> f64
calculates first derivative via central difference method
§Example
let f = |x| x.powi(2);
let derivative = Calculus::derivative(f, 2.0, 0.0001); // ≈ 4.0provides better accuracy than forward/backward difference by using points on both sides
Sourcepub fn second_derivative<F: Fn(f64) -> f64>(f: F, x: f64, h: f64) -> f64
pub fn second_derivative<F: Fn(f64) -> f64>(f: F, x: f64, h: f64) -> f64
computes second derivative using central difference
§Example
let f = |x| x.powi(3);
let second_deriv = Calculus::second_derivative(f, 2.0, 0.0001);approximates second derivative with three points
Sourcepub fn partial_x<F: Fn(f64, f64) -> f64>(f: F, x: f64, y: f64, h: f64) -> f64
pub fn partial_x<F: Fn(f64, f64) -> f64>(f: F, x: f64, y: f64, h: f64) -> f64
finds partial derivative wrt x using central difference
§Example
let f = |x, y| x*x + y*y;
let dx = Calculus::partial_x(f, 1.0, 2.0, 0.0001); // ≈ 2.0keeps y constant while differentiating in x direction
Sourcepub fn partial_y<F: Fn(f64, f64) -> f64>(f: F, x: f64, y: f64, h: f64) -> f64
pub fn partial_y<F: Fn(f64, f64) -> f64>(f: F, x: f64, y: f64, h: f64) -> f64
finds partial derivative wrt y using central difference
§Example
let f = |x, y| x*x + y*y;
let dy = Calculus::partial_y(f, 1.0, 2.0, 0.0001); // ≈ 4.0keeps x constant while differentiating in y direction
Sourcepub fn integrate_rectangle<F: Fn(f64) -> f64>(
f: F,
a: f64,
b: f64,
n: usize,
) -> f64
pub fn integrate_rectangle<F: Fn(f64) -> f64>( f: F, a: f64, b: f64, n: usize, ) -> f64
computes definite integral using rectangle method
§Example
let f = |x| x.powi(2);
let integral = Calculus::integrate_rectangle(f, 0.0, 1.0, 1000); // ≈ 0.333approximates area using sum of rectangles with equal width
Sourcepub fn integrate_trapezoid<F: Fn(f64) -> f64>(
f: F,
a: f64,
b: f64,
n: usize,
) -> f64
pub fn integrate_trapezoid<F: Fn(f64) -> f64>( f: F, a: f64, b: f64, n: usize, ) -> f64
computes definite integral using trapezoidal method
§Example
let f = |x| x.powi(2);
let integral = Calculus::integrate_trapezoid(f, 0.0, 1.0, 1000); // ≈ 0.333uses linear approximation between points for better accuracy than rectangle method
Sourcepub fn integrate_simpson<F: Fn(f64) -> f64>(
f: F,
a: f64,
b: f64,
n: usize,
) -> f64
pub fn integrate_simpson<F: Fn(f64) -> f64>( f: F, a: f64, b: f64, n: usize, ) -> f64
computes definite integral using simpson’s method
§Example
let f = |x| x.powi(2);
let integral = Calculus::integrate_simpson(f, 0.0, 1.0, 1000); // ≈ 0.333uses quadratic approximation for higher accuracy than trapezoid method
Sourcepub fn euler<F: Fn(f64, f64) -> f64>(
f: F,
x0: f64,
y0: f64,
h: f64,
steps: usize,
) -> Vec<(f64, f64)>
pub fn euler<F: Fn(f64, f64) -> f64>( f: F, x0: f64, y0: f64, h: f64, steps: usize, ) -> Vec<(f64, f64)>
solves first-order ODE using euler’s method
§Example
let f = |x, y| x + y;
let solution = Calculus::euler(f, 0.0, 1.0, 0.1, 10);basic numerical method for ODEs using linear approximation
Sourcepub fn runge_kutta4<F: Fn(f64, f64) -> f64>(
f: F,
x0: f64,
y0: f64,
h: f64,
steps: usize,
) -> Vec<(f64, f64)>
pub fn runge_kutta4<F: Fn(f64, f64) -> f64>( f: F, x0: f64, y0: f64, h: f64, steps: usize, ) -> Vec<(f64, f64)>
solves ODE using 4th order runge-kutta method
§Example
let f = |x, y| x + y;
let solution = Calculus::runge_kutta4(f, 0.0, 1.0, 0.1, 10);more accurate than euler’s method by using weighted average of slopes
Sourcepub fn taylor_series<F: Fn(f64) -> f64>(
f: F,
df: &[F],
a: f64,
x: f64,
terms: usize,
) -> f64
pub fn taylor_series<F: Fn(f64) -> f64>( f: F, df: &[F], a: f64, x: f64, terms: usize, ) -> f64
approximates function using taylor series expansion
§Example
let f = |x| x.exp();
let df = [|x| x.exp()]; // derivatives
let approx = Calculus::taylor_series(f, &df, 0.0, 0.1, 2);uses function and its derivatives to create polynomial approximation
Sourcepub fn gradient<F: Fn(f64, f64) -> f64>(
f: F,
x: f64,
y: f64,
h: f64,
) -> (f64, f64)
pub fn gradient<F: Fn(f64, f64) -> f64>( f: F, x: f64, y: f64, h: f64, ) -> (f64, f64)
computes gradient of 2D scalar field
§Example
let f = |x, y| x*x + y*y;
let grad = Calculus::gradient(f, 1.0, 1.0, 0.0001);returns vector of partial derivatives (∂f/∂x, ∂f/∂y)
Sourcepub fn divergence<F: Fn(f64, f64) -> (f64, f64)>(
f: F,
x: f64,
y: f64,
h: f64,
) -> f64
pub fn divergence<F: Fn(f64, f64) -> (f64, f64)>( f: F, x: f64, y: f64, h: f64, ) -> f64
calculates divergence of 2D vector field
§Example
let f = |x, y| (x*y, x+y);
let div = Calculus::divergence(f, 1.0, 1.0, 0.0001);computes sum of partial derivatives ∂P/∂x + ∂Q/∂y
Sourcepub fn curl_z<F: Fn(f64, f64) -> (f64, f64)>(
f: F,
x: f64,
y: f64,
h: f64,
) -> f64
pub fn curl_z<F: Fn(f64, f64) -> (f64, f64)>( f: F, x: f64, y: f64, h: f64, ) -> f64
finds z-component of curl for 2D vector field
§Example
let f = |x, y| (x*y, x+y);
let curl = Calculus::curl_z(f, 1.0, 1.0, 0.0001);computes \partial Q/\partial x - \partial P/\partial y
Sourcepub fn gradient_descent<F: Fn(f64, f64) -> f64>(
f: F,
x: f64,
y: f64,
learning_rate: f64,
tolerance: f64,
max_iterations: usize,
) -> (f64, f64)
pub fn gradient_descent<F: Fn(f64, f64) -> f64>( f: F, x: f64, y: f64, learning_rate: f64, tolerance: f64, max_iterations: usize, ) -> (f64, f64)
finds local minimum using gradient descent
§Example
let f = |x, y| x*x + y*y;
let min = Calculus::gradient_descent(f, 1.0, 1.0, 0.1, 1e-6, 1000);iteratively moves in direction of steepest descent