|
|
|
@ -6,11 +6,11 @@ use crate::math::Scalar;
@@ -6,11 +6,11 @@ use crate::math::Scalar;
|
|
|
|
|
|
|
|
|
|
// an unknown variable with an id
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] |
|
|
|
|
struct Unknown(i64); |
|
|
|
|
pub struct Unknown(i64); |
|
|
|
|
|
|
|
|
|
type UnknownSet = BTreeSet<Unknown>; |
|
|
|
|
pub type UnknownSet = BTreeSet<Unknown>; |
|
|
|
|
|
|
|
|
|
trait Unknowns { |
|
|
|
|
pub trait Unknowns { |
|
|
|
|
fn unknowns(&self) -> UnknownSet; |
|
|
|
|
fn has_unknowns(&self) -> bool; |
|
|
|
|
fn has_unknown(&self, u: Unknown) -> bool; |
|
|
|
@ -47,7 +47,7 @@ impl fmt::Display for Unknown {
@@ -47,7 +47,7 @@ impl fmt::Display for Unknown {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)] |
|
|
|
|
enum Expr { |
|
|
|
|
pub enum Expr { |
|
|
|
|
Unkn(Unknown), |
|
|
|
|
Const(Scalar), |
|
|
|
|
Sum(Exprs), |
|
|
|
@ -56,7 +56,7 @@ enum Expr {
@@ -56,7 +56,7 @@ enum Expr {
|
|
|
|
|
Div(Box<Expr>, Box<Expr>), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type Exprs = Vec<Expr>; |
|
|
|
|
pub type Exprs = Vec<Expr>; |
|
|
|
|
|
|
|
|
|
impl Unknowns for Exprs { |
|
|
|
|
fn unknowns(&self) -> UnknownSet { |
|
|
|
@ -259,26 +259,26 @@ impl Unknowns for Expr {
@@ -259,26 +259,26 @@ impl Unknowns for Expr {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl Expr { |
|
|
|
|
fn new_sum(e1: Expr, e2: Expr) -> Expr { |
|
|
|
|
pub fn new_sum(e1: Expr, e2: Expr) -> Expr { |
|
|
|
|
Expr::Sum(vec![e1, e2]) |
|
|
|
|
} |
|
|
|
|
fn new_product(e1: Expr, e2: Expr) -> Expr { |
|
|
|
|
pub fn new_product(e1: Expr, e2: Expr) -> Expr { |
|
|
|
|
Expr::Product(vec![e1, e2]) |
|
|
|
|
} |
|
|
|
|
fn new_neg(e1: Expr) -> Expr { |
|
|
|
|
pub fn new_neg(e1: Expr) -> Expr { |
|
|
|
|
Expr::Neg(Box::new(e1)) |
|
|
|
|
} |
|
|
|
|
fn new_div(num: Expr, den: Expr) -> Expr { |
|
|
|
|
pub fn new_div(num: Expr, den: Expr) -> Expr { |
|
|
|
|
Expr::Div(Box::new(num), Box::new(den)) |
|
|
|
|
} |
|
|
|
|
fn new_minus(e1: Expr, e2: Expr) -> Expr { |
|
|
|
|
pub fn new_minus(e1: Expr, e2: Expr) -> Expr { |
|
|
|
|
Expr::Sum(vec![e1, Expr::new_neg(e2)]) |
|
|
|
|
} |
|
|
|
|
fn new_inv(den: Expr) -> Expr { |
|
|
|
|
pub fn new_inv(den: Expr) -> Expr { |
|
|
|
|
Expr::new_div(Expr::Const(1.), den) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn is_zero(&self) -> bool { |
|
|
|
|
pub fn is_zero(&self) -> bool { |
|
|
|
|
use Expr::*; |
|
|
|
|
match self { |
|
|
|
|
Const(c) => relative_eq!(*c, 0.), |
|
|
|
@ -286,7 +286,7 @@ impl Expr {
@@ -286,7 +286,7 @@ impl Expr {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn is_one(&self) -> bool { |
|
|
|
|
pub fn is_one(&self) -> bool { |
|
|
|
|
use Expr::*; |
|
|
|
|
match self { |
|
|
|
|
Const(c) => relative_eq!(*c, 1.), |
|
|
|
@ -294,7 +294,7 @@ impl Expr {
@@ -294,7 +294,7 @@ impl Expr {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn simplify(self) -> Expr { |
|
|
|
|
pub fn simplify(self) -> Expr { |
|
|
|
|
use Expr::*; |
|
|
|
|
match self { |
|
|
|
|
Sum(es) => { |
|
|
|
@ -384,7 +384,7 @@ impl Expr {
@@ -384,7 +384,7 @@ impl Expr {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn distribute(self) -> Expr { |
|
|
|
|
pub fn distribute(self) -> Expr { |
|
|
|
|
use Expr::*; |
|
|
|
|
trace!("distribute {}", self); |
|
|
|
|
match self { |
|
|
|
@ -444,7 +444,7 @@ impl fmt::Display for Expr {
@@ -444,7 +444,7 @@ impl fmt::Display for Expr {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)] |
|
|
|
|
struct Eqn(Expr, Expr); |
|
|
|
|
pub struct Eqn(Expr, Expr); |
|
|
|
|
|
|
|
|
|
impl Unknowns for Eqn { |
|
|
|
|
fn unknowns(&self) -> UnknownSet { |
|
|
|
@ -473,11 +473,15 @@ fn ord_by_unkn(a: Expr, b: Expr, u: Unknown) -> Option<(Expr, Expr)> {
@@ -473,11 +473,15 @@ fn ord_by_unkn(a: Expr, b: Expr, u: Unknown) -> Option<(Expr, Expr)> {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl Eqn { |
|
|
|
|
fn simplify(self) -> Eqn { |
|
|
|
|
pub fn new(l: Expr, r: Expr) -> Self { |
|
|
|
|
Self(l, r) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn simplify(self) -> Eqn { |
|
|
|
|
Eqn(self.0.simplify(), self.1.simplify()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn solve(&self, for_u: Unknown) -> Option<Expr> { |
|
|
|
|
pub fn solve(&self, for_u: Unknown) -> Option<Expr> { |
|
|
|
|
use Expr::*; |
|
|
|
|
if !self.has_unknown(for_u) { |
|
|
|
|
return None; |
|
|
|
@ -528,7 +532,6 @@ impl Eqn {
@@ -528,7 +532,6 @@ impl Eqn {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
Const(_) => return None, |
|
|
|
|
_ => return None, |
|
|
|
|
}; |
|
|
|
|
l = new_l; |
|
|
|
|
r = new_r; |
|
|
|
@ -543,7 +546,7 @@ impl fmt::Display for Eqn {
@@ -543,7 +546,7 @@ impl fmt::Display for Eqn {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)] |
|
|
|
|
struct Eqns(Vec<Eqn>); |
|
|
|
|
pub struct Eqns(Vec<Eqn>); |
|
|
|
|
|
|
|
|
|
impl Unknowns for Eqns { |
|
|
|
|
fn unknowns(&self) -> UnknownSet { |