Skip to main content

Equation

Struct Equation 

Source
pub struct Equation {
    pub lhs: Ex,
    pub rhs: Ex,
}
Expand description

A symbolic equation lhs = rhs.

§Examples

use symplex::prelude::*;
use symplex::eq::Equation;

let ctx = Context::new();
let x = ctx.symbol("x");
let eq = Equation::new(&x + 1, ctx.int(5));
assert_eq!(format!("{eq}"), "x + 1 = 5");

Fields§

§lhs: Ex

Left-hand side of the equation.

§rhs: Ex

Right-hand side of the equation.

Implementations§

Source§

impl Equation

Source

pub fn new(lhs: Ex, rhs: Ex) -> Self

Create a new equation lhs = rhs.

§Panics

Panics if the two sides belong to different contexts.

Source

pub fn lhs(&self) -> &Ex

The left-hand side.

Source

pub fn rhs(&self) -> &Ex

The right-hand side.

Source

pub fn swap(&self) -> Equation

The equation rhs = lhs.

use symplex::prelude::*;

let ctx = Context::new();
let x = ctx.symbol("x");
let eq = Equation::new(ctx.int(5), &x + 1).swap();
assert_eq!(format!("{eq}"), "x + 1 = 5");
Source

pub fn apply(&self, f: impl Fn(&Ex) -> Ex) -> Equation

Apply the same function to both sides.

use symplex::prelude::*;

let ctx = Context::new();
let x = ctx.symbol("x");
let eq = Equation::new(x.exp(), ctx.int(2)).apply(|s| s.ln());
assert_eq!(format!("{}", eq.simplify()), "x = ln(2)");
Source

pub fn to_zero_equation(&self) -> Equation

Move everything to the left: the equation lhs - rhs = 0.

For the bare expression lhs - rhs use to_expr (or the ZeroForm trait, which the solvers accept).

use symplex::prelude::*;

let ctx = Context::new();
let x = ctx.symbol("x");
let eq = Equation::new(x.powi(2), &x + 2).to_zero_equation();
assert_eq!(format!("{eq}"), "x^2 - x - 2 = 0");
Source

pub fn solve(&self, var: &Ex) -> Result<Vec<Ex>, SymplexError>

Solve this equation for var, returning the solution values.

Internally computes lhs - rhs and solves for zero.

§Examples
use symplex::prelude::*;
use symplex::eq::Equation;

let ctx = Context::new();
let x = ctx.symbol("x");
let eq = Equation::new(&x + 1, ctx.int(5));
let roots = eq.solve(&x).unwrap();
assert_eq!(format!("{}", roots[0]), "4");
Source

pub fn solve_for(&self, var: &Ex) -> Result<Vec<Equation>, SymplexError>

Solve for var, returning each solution as an equation var = value.

This is the form you want when the result feeds further substitution or display; solve returns the bare values.

use symplex::prelude::*;

let ctx = Context::new();
let x = ctx.symbol("x");
let sols = Equation::new(x.powi(2), ctx.int(4)).solve_for(&x).unwrap();
let mut shown: Vec<String> = sols.iter().map(|e| format!("{e}")).collect();
shown.sort();
assert_eq!(shown, vec!["x = -2", "x = 2"]);
Source

pub fn solve_or_empty(&self, var: &Ex) -> Vec<Ex>

Solve this equation, returning empty vec on failure.

Source

pub fn subs(&self, old: &Ex, new: &Ex) -> Equation

Substitute a variable in both sides.

Source

pub fn subs_i64(&self, old: &Ex, val: i64) -> Equation

Substitute an integer value in both sides.

Source

pub fn simplify(&self) -> Equation

Simplify both sides.

Source

pub fn expand(&self) -> Equation

Expand both sides.

Source

pub fn eval(&self) -> Equation

Evaluate both sides.

Source

pub fn is_satisfied(&self) -> Option<bool>

Check if the equation is satisfied (both sides mathematically equal).

Three-valued via Ex::equals; same as is_identity.

Source

pub fn is_identity(&self) -> Option<bool>

Does the equation hold for all values of its symbols?

Some(true) when lhs − rhs simplifies to zero, Some(false) when the two sides are provably different (e.g. they differ by a nonzero constant), None when undetermined — see Ex::equals.

use symplex::prelude::*;

let ctx = Context::new();
let x = ctx.symbol("x");
let pyth = Equation::new(&x.sin().powi(2) + &x.cos().powi(2), ctx.one());
assert_eq!(pyth.is_identity(), Some(true));
assert_eq!(Equation::new(&x + 1, x.clone()).is_identity(), Some(false));
assert_eq!(Equation::new(x.clone(), ctx.int(3)).is_identity(), None);
Source

pub fn to_expr(&self) -> Ex

Convert to a single expression lhs - rhs.

Trait Implementations§

Source§

impl Add for Equation

Source§

type Output = Equation

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Equation) -> Equation

Performs the + operation. Read more
Source§

impl Add<&Equation> for &Equation

Source§

type Output = Equation

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &Equation) -> Equation

Performs the + operation. Read more
Source§

impl<T: ToEx> Add<T> for &Equation

Source§

type Output = Equation

The resulting type after applying the + operator.
Source§

fn add(self, rhs: T) -> Equation

Performs the + operation. Read more
Source§

impl<T: ToEx> Add<T> for Equation

Source§

type Output = Equation

The resulting type after applying the + operator.
Source§

fn add(self, rhs: T) -> Equation

Performs the + operation. Read more
Source§

impl Clone for Equation

Source§

fn clone(&self) -> Equation

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Equation

Source§

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

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

impl Display for Equation

Source§

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

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

impl Div for Equation

Source§

type Output = Equation

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Equation) -> Equation

Performs the / operation. Read more
Source§

impl Div<&Equation> for &Equation

Source§

type Output = Equation

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &Equation) -> Equation

Performs the / operation. Read more
Source§

impl<T: ToEx> Div<T> for &Equation

Source§

type Output = Equation

The resulting type after applying the / operator.
Source§

fn div(self, rhs: T) -> Equation

Performs the / operation. Read more
Source§

impl<T: ToEx> Div<T> for Equation

Source§

type Output = Equation

The resulting type after applying the / operator.
Source§

fn div(self, rhs: T) -> Equation

Performs the / operation. Read more
Source§

impl Eq for Equation

Source§

impl Mul for Equation

Source§

type Output = Equation

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Equation) -> Equation

Performs the * operation. Read more
Source§

impl Mul<&Equation> for &Equation

Source§

type Output = Equation

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Equation) -> Equation

Performs the * operation. Read more
Source§

impl<T: ToEx> Mul<T> for &Equation

Source§

type Output = Equation

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: T) -> Equation

Performs the * operation. Read more
Source§

impl<T: ToEx> Mul<T> for Equation

Source§

type Output = Equation

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: T) -> Equation

Performs the * operation. Read more
Source§

impl Neg for &Equation

Source§

type Output = Equation

The resulting type after applying the - operator.
Source§

fn neg(self) -> Equation

Performs the unary - operation. Read more
Source§

impl Neg for Equation

Source§

type Output = Equation

The resulting type after applying the - operator.
Source§

fn neg(self) -> Equation

Performs the unary - operation. Read more
Source§

impl PartialEq for Equation

Structural equality: both sides are the same arena nodes.

x + 1 = 5 and 1 + x = 5 are equal (same canonical form); x + 1 = 5 and x = 4 are not, even though they are equivalent.

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Sub for Equation

Source§

type Output = Equation

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Equation) -> Equation

Performs the - operation. Read more
Source§

impl Sub<&Equation> for &Equation

Source§

type Output = Equation

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &Equation) -> Equation

Performs the - operation. Read more
Source§

impl<T: ToEx> Sub<T> for &Equation

Source§

type Output = Equation

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: T) -> Equation

Performs the - operation. Read more
Source§

impl<T: ToEx> Sub<T> for Equation

Source§

type Output = Equation

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: T) -> Equation

Performs the - operation. Read more
Source§

impl ZeroForm for Equation

Source§

fn to_zero_form(&self) -> Ex

Return the expression that equals zero when the equation holds.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more