Skip to content

Commit

Permalink
implement assignment operators for Term, Variable and Expression
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhickman committed May 28, 2018
1 parent 9523a8f commit 1ca31b0
Showing 1 changed file with 122 additions and 14 deletions.
136 changes: 122 additions & 14 deletions src/operators.rs
Expand Up @@ -138,11 +138,17 @@ impl ops::Add<Expression> for Variable {
impl ops::Add<Variable> for Expression {
type Output = Expression;
fn add(mut self, v: Variable) -> Expression {
self.terms.push(Term::new(v, 1.0));
self += v;
self
}
}

impl ops::AddAssign<Variable> for Expression {
fn add_assign(&mut self, v: Variable) {
self.terms.push(Term::new(v, 1.0));
}
}

impl ops::Neg for Variable {
type Output = Term;
fn neg(self) -> Term {
Expand Down Expand Up @@ -211,11 +217,17 @@ impl ops::Sub<Expression> for Variable {
impl ops::Sub<Variable> for Expression {
type Output = Expression;
fn sub(mut self, v: Variable) -> Expression {
self.terms.push(Term::new(v, -1.0));
self -= v;
self
}
}

impl ops::SubAssign<Variable> for Expression {
fn sub_assign(&mut self, v: Variable) {
self.terms.push(Term::new(v, -1.0));
}
}

impl ops::Mul<f64> for Variable {
type Output = Term;
fn mul(self, v: f64) -> Term {
Expand Down Expand Up @@ -263,18 +275,30 @@ impl ops::Div<f32> for Variable {
impl ops::Mul<f64> for Term {
type Output = Term;
fn mul(mut self, v: f64) -> Term {
self.coefficient *= v;
self *= v;
self
}
}

impl ops::MulAssign<f64> for Term {
fn mul_assign(&mut self, v: f64) {
self.coefficient *= v;
}
}

impl ops::Mul<f32> for Term {
type Output = Term;
fn mul(self, v: f32) -> Term {
self.mul(v as f64)
}
}

impl ops::MulAssign<f32> for Term {
fn mul_assign(&mut self, v: f32) {
self.mul_assign(v as f64)
}
}

impl ops::Mul<Term> for f64 {
type Output = Term;
fn mul(self, mut t: Term) -> Term {
Expand All @@ -293,18 +317,30 @@ impl ops::Mul<Term> for f32 {
impl ops::Div<f64> for Term {
type Output = Term;
fn div(mut self, v: f64) -> Term {
self.coefficient /= v;
self /= v;
self
}
}

impl ops::DivAssign<f64> for Term {
fn div_assign(&mut self, v: f64) {
self.coefficient /= v;
}
}

impl ops::Div<f32> for Term {
type Output = Term;
fn div(self, v: f32) -> Term {
self.div(v as f64)
}
}

impl ops::DivAssign<f32> for Term {
fn div_assign(&mut self, v: f32) {
self.div_assign(v as f64)
}
}

impl ops::Add<f64> for Term {
type Output = Expression;
fn add(self, v: f64) -> Expression {
Expand Down Expand Up @@ -351,11 +387,17 @@ impl ops::Add<Expression> for Term {
impl ops::Add<Term> for Expression {
type Output = Expression;
fn add(mut self, t: Term) -> Expression {
self.terms.push(t);
self += t;
self
}
}

impl ops::AddAssign<Term> for Expression {
fn add_assign(&mut self, t: Term) {
self.terms.push(t);
}
}

impl ops::Neg for Term {
type Output = Term;
fn neg(mut self) -> Term {
Expand Down Expand Up @@ -411,21 +453,33 @@ impl ops::Sub<Expression> for Term {
impl ops::Sub<Term> for Expression {
type Output = Expression;
fn sub(mut self, t: Term) -> Expression {
self.terms.push(-t);
self -= t;
self
}
}

impl ops::SubAssign<Term> for Expression {
fn sub_assign(&mut self, t: Term) {
self.terms.push(-t);
}
}

// Expression

impl ops::Mul<f64> for Expression {
type Output = Expression;
fn mul(mut self, v: f64) -> Expression {
self *= v;
self
}
}

impl ops::MulAssign<f64> for Expression {
fn mul_assign(&mut self, v: f64) {
self.constant *= v;
for t in &mut self.terms {
*t = *t * v;
}
self
}
}

Expand All @@ -436,6 +490,12 @@ impl ops::Mul<f32> for Expression {
}
}

impl ops::MulAssign<f32> for Expression {
fn mul_assign(&mut self, v: f32) {
self.mul_assign(v as f64)
}
}

impl ops::Mul<Expression> for f64 {
type Output = Expression;
fn mul(self, mut e: Expression) -> Expression {
Expand All @@ -457,11 +517,17 @@ impl ops::Mul<Expression> for f32 {
impl ops::Div<f64> for Expression {
type Output = Expression;
fn div(mut self, v: f64) -> Expression {
self /= v;
self
}
}

impl ops::DivAssign<f64> for Expression {
fn div_assign(&mut self, v: f64) {
self.constant /= v;
for t in &mut self.terms {
*t = *t / v;
}
self
}
}

Expand All @@ -472,21 +538,39 @@ impl ops::Div<f32> for Expression {
}
}

impl ops::DivAssign<f32> for Expression {
fn div_assign(&mut self, v: f32) {
self.div_assign(v as f64)
}
}

impl ops::Add<f64> for Expression {
type Output = Expression;
fn add(mut self, v: f64) -> Expression {
self.constant += v;
self += v;
self
}
}

impl ops::AddAssign<f64> for Expression {
fn add_assign(&mut self, v: f64) {
self.constant += v;
}
}

impl ops::Add<f32> for Expression {
type Output = Expression;
fn add(self, v: f32) -> Expression {
self.add(v as f64)
}
}

impl ops::AddAssign<f32> for Expression {
fn add_assign(&mut self, v: f32) {
self.add_assign(v as f64)
}
}

impl ops::Add<Expression> for f64 {
type Output = Expression;
fn add(self, mut e: Expression) -> Expression {
Expand All @@ -504,10 +588,16 @@ impl ops::Add<Expression> for f32 {

impl ops::Add<Expression> for Expression {
type Output = Expression;
fn add(mut self, mut e: Expression) -> Expression {
fn add(mut self, e: Expression) -> Expression {
self += e;
self
}
}

impl ops::AddAssign<Expression> for Expression {
fn add_assign(&mut self, mut e: Expression) {
self.terms.append(&mut e.terms);
self.constant += e.constant;
self
}
}

Expand All @@ -522,18 +612,30 @@ impl ops::Neg for Expression {
impl ops::Sub<f64> for Expression {
type Output = Expression;
fn sub(mut self, v: f64) -> Expression {
self.constant -= v;
self -= v;
self
}
}

impl ops::SubAssign<f64> for Expression {
fn sub_assign(&mut self, v: f64) {
self.constant -= v;
}
}

impl ops::Sub<f32> for Expression {
type Output = Expression;
fn sub(self, v: f32) -> Expression {
self.sub(v as f64)
}
}

impl ops::SubAssign<f32> for Expression {
fn sub_assign(&mut self, v: f32) {
self.sub_assign(v as f64)
}
}

impl ops::Sub<Expression> for f64 {
type Output = Expression;
fn sub(self, mut e: Expression) -> Expression {
Expand All @@ -552,10 +654,16 @@ impl ops::Sub<Expression> for f32 {

impl ops::Sub<Expression> for Expression {
type Output = Expression;
fn sub(mut self, mut e: Expression) -> Expression {
fn sub(mut self, e: Expression) -> Expression {
self -= e;
self
}
}

impl ops::SubAssign<Expression> for Expression {
fn sub_assign(&mut self, mut e: Expression) {
e.negate();
self.terms.append(&mut e.terms);
self.constant += e.constant;
self
}
}

0 comments on commit 1ca31b0

Please sign in to comment.