pub trait Integrate {
// Required methods
fn integrate(&self) -> Integral;
fn evaluate_integral(&self, lower_bound: f64, upper_bound: f64) -> f64;
}
Expand description
types that implement the Integrate trait can safely be integrated within the domain ℝ.
Required Methods§
Sourcefn integrate(&self) -> Integral
fn integrate(&self) -> Integral
Method will return an instance of Integral that can be taylored to specific usecases.
Example:
let function = Function::from("sin(x)");
let mut integral = function.integrate();
// specify bounds and precision for the integral
integral
.set_lower_bound(0.)
.set_upper_bound(PI / 2.)
.set_precision(20000);
// evaluate the integral
let value = integral.evaluate().unwrap();
// note that the value of the evaluated integral must be unwrapped if using the `integrate()`
// method because the method cannot guarantee that bounds have been set at the point of
// evaluating. The evaluate_integral() method which is implemented for any instance with the
// Integrate trait is safer and is guaranteed to yield a valid result.
// round the value to 10 decimal places
value.round_to(10);
assert_eq!(value, 1.);
Also note that if the precision is not specified, it will default to 1000.
Sourcefn evaluate_integral(&self, lower_bound: f64, upper_bound: f64) -> f64
fn evaluate_integral(&self, lower_bound: f64, upper_bound: f64) -> f64
Method will return the value of the definite integral of the function evaluated from the provided lower and upper bound.
Example:
let function = Function::from("cos(x)");
// the evaluate_integral() method will automatically round the result to five decimal points.
// This is because higher precision cannot be guaranteed with using the standard precision set
// for the method. Provided that the function is defined for all values between the lower and
// upper bounds, the method will always return a valid result.
let value = function.evaluate_integral(0., PI);
assert_eq!(value, 0.);