Functions

Struct Functions 

Source
pub struct Functions;
Expand description

A module containing Regular Mathematics Functions.

Implementations§

Source§

impl Functions

Source

pub fn derivative<F: Fn(f64) -> f64>(f: F, x: impl Into<f64> + Copy) -> f64

Uses the definition of a derivative to calculate the derivative of a function at a specific point of a given function.

§Parameters
  • f: A function that takes a single f64 argument and returns an f64. This is the function for which the derivative will be calculated.
  • x: The point at which the derivative will be calculated.
§Returns

The calculated derivative of the function at the given point.

§Example

use numerilib::Functions;

let function = |x: f64| x.powi(2);
let x = 2;

let derivative = Functions::derivative(function, x);

println!("The Derivative of x^2 at x=2 is: {}", derivative);

Source

pub fn right_riemann<F: Fn(f64) -> f64>( function: F, lower_limit: f64, upper_limit: f64, intervals: f64, ) -> f64

The Right Endpoint method to calculate a definite integral.

§Parameters
  • function: A function that takes a single f64 argument and returns an f64. This is the function to be integrated.
  • lower_limit: The lower limit of integration.
  • upper_limit: The upper limit of integration.
  • intervals: The number of intervals for the Riemann sum.
§Returns

The calculated definite integral using the Right Endpoint method.

§Example
use numerilib::Functions;

let lower_bound = 0_f64;
let upper_bound = 6_f64;
let intervals = 6_f64;
let function = |x: f64| x.powi(2);

let integral = Functions::right_riemann(function, lower_bound, upper_bound, intervals);

println!("The Integral of x^2 at [0,6] is: {}", integral)

Source

pub fn left_riemann<F: Fn(f64) -> f64>( function: F, lower_limit: f64, upper_limit: f64, intervals: f64, ) -> f64

The Left Endpoint method to calculate a definite integral.

§Parameters
  • function: A function that takes a single f64 argument and returns an f64. This is the function to be integrated.
  • lower_limit: The lower limit of integration.
  • upper_limit: The upper limit of integration.
  • intervals: The number of intervals for the Riemann sum.
§Returns

The calculated definite integral using the Left Endpoint method.

§Example
use numerilib::Functions;

let lower_bound = 0_f64;
let upper_bound = 6_f64;
let intervals = 6_f64;
let function = |x: f64| x.powi(2);

let integral = Functions::left_riemann(function, lower_bound, upper_bound, intervals);

println!("The Integral of x^2 at [0,6] is: {}", integral)

Source

pub fn midpoint_riemann<F: Fn(f64) -> f64>( function: F, lower_limit: f64, upper_limit: f64, intervals: f64, ) -> f64

The Midpoint method to calculate a definite integral.

§Parameters
  • function: A function that takes a single f64 argument and returns an f64. This is the function to be integrated.
  • lower_limit: The lower limit of integration.
  • upper_limit: The upper limit of integration.
  • intervals: The number of intervals for the Riemann sum.
§Returns

The calculated definite integral using the Midpoint method.

§Example
use numerilib::Functions;

let lower_bound = 0_f64;
let upper_bound = 6_f64;
let intervals = 6_f64;
let function = |x: f64| x.powi(2);

let integral = Functions::midpoint_riemann(function, lower_bound, upper_bound, intervals);

println!("The Integral of x^2 at [0,6] is: {}", integral)
Source

pub fn trapezoid<F: Fn(f64) -> f64>( function: F, lower_limit: f64, upper_limit: f64, intervals: f64, ) -> f64

The Trapezoid method to calculate a definite integral.

§Parameters
  • function: A function that takes a single f64 argument and returns an f64. This is the function to be integrated.
  • lower_limit: The lower limit of integration.
  • upper_limit: The upper limit of integration.
  • intervals: The number of intervals for the Trapezoidal rule.
§Returns

The calculated definite integral using the Trapezoidal rule.

§Example
use numerilib::Functions;

let lower_bound = 0_f64;
let upper_bound = 6_f64;
let intervals = 6_f64;
let function = |x: f64| x.powi(2);

let integral = Functions::trapezoid(function, lower_bound, upper_bound, intervals);

println!("The Integral of x^2 at [0,6] is: {}", integral)

Source

pub fn simpson<F: Fn(f64) -> f64>( function: F, lower_limit: f64, upper_limit: f64, intervals: f64, ) -> f64

Uses the Composite Simpson’s 1/3rd Rule to calculate a definite integral.

§Parameters
  • function: A function that takes a single f64 argument and returns an f64. This is the function to be integrated.
  • lower_limit: The lower limit of integration.
  • upper_limit: The upper limit of integration.
  • intervals: The number of intervals for the Simpson’s Rule.
§Returns

The calculated definite integral using Simpson’s Rule.

§Example
use numerilib::Functions;

let lower_bound = 0_f64;
let upper_bound = 6_f64;
let intervals = 6_f64;
let function = |x: f64| x.powi(2);

let integral = Functions::simpson(function, lower_bound, upper_bound, intervals);

println!("The Integral of x^2 at [0,6] is: {}", integral)

Source

pub fn boole_rule<F: Fn(f64) -> f64>( function: F, lower_limit: f64, upper_limit: f64, ) -> f64

Uses Boole’s Rule to calculate a definite integral.

§Parameters
  • function: A function that takes a single f64 argument and returns an f64. This is the function to be integrated.
  • lower_limit: The lower limit of integration.
  • upper_limit: The upper limit of integration.
§Returns

The calculated definite integral using Boole’s Rule.

§Example
use numerilib::Functions;

let lower_bound = 0_f64;
let upper_bound = 6_f64;
let function = |x: f64| x.powi(2);

let integral = Functions::boole_rule(function, lower_bound, upper_bound);

println!("The Integral of x^2 at [0,6] is: {}", integral)

Source

pub fn adaptive_quadrature<F: Fn(f64) -> f64>( function: F, lower_limit: f64, upper_limit: f64, tolerance: f64, ) -> f64

Uses Adaptive Quadrature to calculate a definite integral.

§Parameters
  • function: A function that takes a single f64 argument and returns an f64. This is the function to be integrated.
  • lower_limit: The lower limit of integration.
  • upper_limit: The upper limit of integration.
  • tolerance: The level of precision (ie: 1e-6) that is passed.
§Returns

The calculated definite integral using Adaptive Quadrature.

§Example
use numerilib::Functions;

let lower_bound = 0_f64;
let upper_bound = 6_f64;
let tolerance = 1e-12;
let function = |x: f64| x.powi(2);

let integral = Functions::adaptive_quadrature(function, lower_bound, upper_bound, tolerance);

println!("The Integral of x^2 at [0,6] is: {}", integral)

Source

pub fn summation<F: Fn(f64) -> f64>(start: f64, limit: f64, f: F) -> f64

Summations in Rust.

§Parameters
  • start: The starting value for the summation.
  • limit: The ending value for the summation.
  • f: A function that takes a single f64 argument and returns an f64. This is the function to be summed.
§Returns

The sum of applying the function to each value in the range [start, limit].

§Example #1: Constant
use numerilib::Functions;

let start = 0_f64;
let limit = 9_f64;
let function = |x: f64| 3_f64;    

let summation = Functions::summation(start, limit, function);    ///

println!("The summation of the constant 3 from [0,9] is: {}", summation);
§Example #2: Function
use numerilib::Functions;

let start = 4.5;    
let limit = 100_f64;    
let function = |x: f64| 1_f64 / x;

let summation = Functions::summation(start, limit, function);

println!("The summation of the function 1/x from [4.5,100] is: {}", summation);

Source

pub fn product<F: Fn(f64) -> f64>(start: f64, limit: f64, f: F) -> f64

Calculates the product of a function. a.k.a Capital Pi Notation.

§Parameters
  • start: The starting value for the product.
  • limit: The ending value for the product.
  • f: A function that takes a single f64 argument and returns an f64. This is the function to be multiplied in the product.
§Returns

The product of applying the function to each value in the range [start, limit].

§Example
use numerilib::Functions;

let start = 2_f64;
let limit = 7_f64;
let f = |x: f64| 3_f64;

let product_series = Functions::product(start, limit, f);

println!("The Product Series of the Constant 3 from [2,7] is: {}", product_series);
§Example #2: Function
use numerilib::Functions;

let start = 3_f64;
let limit = 7_f64;
let function = |x: f64| x.powi(2);

let summation = Functions::product(start, limit, function);

println!("The Product Series of the Function x^2 from [3,7] is: {}", summation);

Source

pub fn newmet<F: Fn(f64) -> f64>(guess: f64, func: F) -> f64

A rust implementation of the Newton–Raphson method for finding roots.

§Parameters
  • guess: An initial guess for the root.
  • func: A function that takes a single f64 argument and returns an f64. This is the function for which the root is found.
§Returns

The approximate root of the function.

§Example
use numerilib::Functions;

let x = 1.5_f64;
let function = |x: f64| x.powi(2) - 2_f64;
let newton = Functions::newmet(x, function);

println!("Using Newton's Method we can approximate the root of x^2-2 with a guess of 1.5 as: {}", newton);

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.