Browse Source

Improve ergonomics

eqn_relations
Alex Mikhalev 6 years ago
parent
commit
8a33818790
  1. 6
      src/main.rs
  2. 8
      src/math/eqn.rs
  3. 6
      src/math/expr.rs
  4. 2
      src/math/region.rs
  5. 4
      src/math/vec.rs

6
src/main.rs

@ -28,15 +28,15 @@ fn main() {
let u2 = math::Unknown(2); let u2 = math::Unknown(2);
// let u1 = eqn::Expr::from(1.); // let u1 = eqn::Expr::from(1.);
// let u2 = eqn::Expr::from(1.); // let u2 = eqn::Expr::from(1.);
let origin = CPoint::new_single(Point2::new((0.).into(), (0.).into())).into_ref(); let origin = CPoint::new_single(Point2::new(0., 0.)).into_ref();
// let p1 = Point::new_ref(Var::new_full(Point2::new((1.).into(), (1.).into()))); // let p1 = Point::new_ref(Var::new_full(Point2::new((1.).into(), (1.).into())));
let p1 = CPoint::new( let p1 = CPoint::new(
Point2::new(0., 0.), Point2::new(0., 0.),
Region2::Singleton(Point2::new((u1).into(), (u2).into())), Region2::Singleton(Point2::new((u1).into(), (u2).into())),
) )
.into_ref(); .into_ref();
let p2 = CPoint::new_full(Point2::new((4.).into(), (4.).into())).into_ref(); let p2 = CPoint::new_full(Point2::new(4., 4.)).into_ref();
let p3 = CPoint::new_full(Point2::new((2.).into(), (2.).into())).into_ref(); let p3 = CPoint::new_full(Point2::new(2., 2.)).into_ref();
let mut points: Vec<PointRef> = vec![origin.clone(), p1.clone(), p2.clone(), p3.clone()]; let mut points: Vec<PointRef> = vec![origin.clone(), p1.clone(), p2.clone(), p3.clone()];
let print_points = |points: &Vec<PointRef>| { let print_points = |points: &Vec<PointRef>| {
println!( println!(

8
src/math/eqn.rs

@ -3,6 +3,7 @@ use std::fmt;
use super::expr::Expr; use super::expr::Expr;
use super::unknown::*; use super::unknown::*;
use super::Scalar; use super::Scalar;
use crate::math::expr::Expr::*;
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct Eqn(pub Expr, pub Expr); pub struct Eqn(pub Expr, pub Expr);
@ -47,17 +48,18 @@ impl Eqn {
if !self.has_unknown(for_u) { if !self.has_unknown(for_u) {
return None; return None;
} }
trace!("solve: {}", self);
let (l, r) = ( let (l, r) = (
self.0 self.0
.clone() /*.distribute()*/ .clone().distribute()
.simplify(), .simplify(),
self.1 self.1
.clone() /*.distribute()*/ .clone().distribute()
.simplify(), .simplify(),
); );
let (mut l, mut r) = ord_by_unkn(l, r, for_u)?; let (mut l, mut r) = ord_by_unkn(l, r, for_u)?;
loop { loop {
trace!("solve: {} == {}", l, r); trace!("solve iter: {} == {}", l, r);
let (new_l, new_r): (Expr, Expr) = match l { let (new_l, new_r): (Expr, Expr) = match l {
Unkn(u) => return if u == for_u { Some(r.simplify()) } else { None }, Unkn(u) => return if u == for_u { Some(r.simplify()) } else { None },
Sum(es) => { Sum(es) => {

6
src/math/expr.rs

@ -31,14 +31,16 @@ impl Unknowns for Exprs {
fn write_separated_exprs(es: &Exprs, f: &mut fmt::Formatter, sep: &str) -> fmt::Result { fn write_separated_exprs(es: &Exprs, f: &mut fmt::Formatter, sep: &str) -> fmt::Result {
let mut is_first = true; let mut is_first = true;
write!(f, "(")?;
for e in es { for e in es {
if is_first { if is_first {
is_first = false; is_first = false;
} else { } else {
write!(f, "{}", sep)? write!(f, "{}", sep)?
} }
write!(f, "({})", e)? write!(f, "{}", e)?
} }
write!(f, ")")?;
Ok(()) Ok(())
} }
@ -451,7 +453,7 @@ impl fmt::Display for Expr {
Const(c) => write!(f, "{}", c), Const(c) => write!(f, "{}", c),
Sum(es) => write_separated_exprs(es, f, " + "), Sum(es) => write_separated_exprs(es, f, " + "),
Product(es) => write_separated_exprs(es, f, " * "), Product(es) => write_separated_exprs(es, f, " * "),
Div(num, den) => write!(f, "({}) / ({})", num, den), Div(num, den) => write!(f, "({} / {})", num, den),
Neg(e) => write!(f, "-({})", e), Neg(e) => write!(f, "-({})", e),
} }
} }

2
src/math/region.rs

@ -150,7 +150,7 @@ impl fmt::Display for Line2 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!( write!(
f, f,
"{{ <x, y> = {} + {} * {} }}", "{{ (x, y, t): ⟨x, y⟩ = {} + t{} & t ∈ {} }}",
self.start, self.dir, self.extent self.start, self.dir, self.extent
) )
} }

4
src/math/vec.rs

@ -149,7 +149,7 @@ use std::fmt;
impl<T: fmt::Display> fmt::Display for Point2<T> { impl<T: fmt::Display> fmt::Display for Point2<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "<{}, {}>", self.x, self.y) write!(f, "⟨{}, {}⟩", self.x, self.y)
} }
} }
@ -161,7 +161,7 @@ pub struct Rot2 {
impl fmt::Display for Rot2 { impl fmt::Display for Rot2 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "<{}, {}>", self.cos, self.sin) write!(f, "⟨{}, {}⟩", self.cos, self.sin)
} }
} }

Loading…
Cancel
Save