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: ExLeft-hand side of the equation.
rhs: ExRight-hand side of the equation.
Implementations§
Source§impl Equation
impl Equation
Sourcepub fn swap(&self) -> Equation
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");Sourcepub fn apply(&self, f: impl Fn(&Ex) -> Ex) -> Equation
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)");Sourcepub fn to_zero_equation(&self) -> Equation
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");Sourcepub fn solve(&self, var: &Ex) -> Result<Vec<Ex>, SymplexError>
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");Sourcepub fn solve_for(&self, var: &Ex) -> Result<Vec<Equation>, SymplexError>
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"]);Sourcepub fn solve_or_empty(&self, var: &Ex) -> Vec<Ex> ⓘ
pub fn solve_or_empty(&self, var: &Ex) -> Vec<Ex> ⓘ
Solve this equation, returning empty vec on failure.
Sourcepub fn subs_i64(&self, old: &Ex, val: i64) -> Equation
pub fn subs_i64(&self, old: &Ex, val: i64) -> Equation
Substitute an integer value in both sides.
Sourcepub fn is_satisfied(&self) -> Option<bool>
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.
Sourcepub fn is_identity(&self) -> Option<bool>
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);Trait Implementations§
impl Eq for Equation
Source§impl PartialEq for Equation
Structural equality: both sides are the same arena nodes.
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.
Auto Trait Implementations§
impl !RefUnwindSafe for Equation
impl !UnwindSafe for Equation
impl Freeze for Equation
impl Send for Equation
impl Sync for Equation
impl Unpin for Equation
impl UnsafeUnpin for Equation
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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