Struct Calculus

Source
pub struct Calculus;

Implementations§

Source§

impl Calculus

Source

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.0

provides better accuracy than forward/backward difference by using points on both sides

Source

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

Source

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.0

keeps y constant while differentiating in x direction

Source

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.0

keeps x constant while differentiating in y direction

Source

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.333

approximates area using sum of rectangles with equal width

Source

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.333

uses linear approximation between points for better accuracy than rectangle method

Source

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.333

uses quadratic approximation for higher accuracy than trapezoid method

Source

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

Source

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

Source

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

Source

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)

Source

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

Source

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

Source

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

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.