calculi

Struct Equation

Source
pub struct Equation {
    pub text: String,
    pub expression: Component,
}
Expand description

The equation struct containing the equation text and the parsed component.Component.

Various functions can be executed on this equation to solve it or it’s variables.

Fields§

§text: String

The equation in string form

§expression: Component

The equation in a component tree

Implementations§

Source§

impl Equation

Source

pub fn new<T: Into<String>>(text: T) -> Equation

Creates a new equation from an equation in string form

§Examples
let eq = calculi::Equation::new("a * sqrt(x + 1)");
Source

pub fn derive(&self) -> Equation

Get the derivative of an equation

§Examples
let eq = calculi::Equation::new("x^sin(x)").derive();

assert_eq!(eq.text, "x ^ sin(x) * (cos(x) * ln(x) + sin(x) * 1 / x)");
Source

pub fn solve_with<'a>( &self, vars_raw: impl IntoIterator<Item = (&'a str, f32)>, ) -> Component

Get the output of an equation with the given variable definitions

§Examples
let eq = calculi::Equation::new("a * sqrt(x + 1)");

assert_eq!(eq.solve_with(vec![("a", 2.0), ("x", 8.0)]).to_float().unwrap(), 6.0);
Source

pub fn solve_for<'a>( &self, outcome: f32, vars: impl IntoIterator<Item = (&'a str, f32)>, ) -> (Component, f32)

Attempt to solve equation that contains an unknown variable Returns left over outcome and expression if solving failed

§Examples
let eq = calculi::Equation::new("a * sqrt(x + 1)");

let solved = eq.solve_for(9.0, vec![("x", 8.0)]);

assert_eq!(solved.0.to_string(), "a");
assert_eq!(solved.1, 3.0);

Trait Implementations§

Source§

impl Debug for Equation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Component> for Equation

Source§

fn from(expression: Component) -> Self

Converts to this type from the input type.

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.