From 398e4c709bab30fabc78992e4d740ddb837dfaaf Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Tue, 5 Feb 2019 21:30:48 -0800 Subject: [PATCH] rename solver to eqn and make pub stuff --- src/math/{solver.rs => eqn.rs} | 43 ++++++++++++++++++---------------- src/math/mod.rs | 2 +- 2 files changed, 24 insertions(+), 21 deletions(-) rename src/math/{solver.rs => eqn.rs} (96%) diff --git a/src/math/solver.rs b/src/math/eqn.rs similarity index 96% rename from src/math/solver.rs rename to src/math/eqn.rs index f114a01..53ffb24 100644 --- a/src/math/solver.rs +++ b/src/math/eqn.rs @@ -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; +pub type UnknownSet = BTreeSet; -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 { } #[derive(Clone, Debug, PartialEq)] -enum Expr { +pub enum Expr { Unkn(Unknown), Const(Scalar), Sum(Exprs), @@ -56,7 +56,7 @@ enum Expr { Div(Box, Box), } -type Exprs = Vec; +pub type Exprs = Vec; impl Unknowns for Exprs { fn unknowns(&self) -> UnknownSet { @@ -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 { } } - 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 { } } - fn simplify(self) -> Expr { + pub fn simplify(self) -> Expr { use Expr::*; match self { Sum(es) => { @@ -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 { } #[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)> { } 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 { + pub fn solve(&self, for_u: Unknown) -> Option { use Expr::*; if !self.has_unknown(for_u) { return None; @@ -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 { } #[derive(Clone, Debug, PartialEq)] -struct Eqns(Vec); +pub struct Eqns(Vec); impl Unknowns for Eqns { fn unknowns(&self) -> UnknownSet { diff --git a/src/math/mod.rs b/src/math/mod.rs index 88d0fb5..d6936be 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -1,4 +1,4 @@ -pub mod solver; +pub mod eqn; pub type Scalar = f64;