@@ -10,7 +10,7 @@ var Expression = function(variable) {
1010 var t = new Term ( v ) ;
1111 this . terms = [ t ] ;
1212 } else if ( isInt ( variable ) ) {
13- var f = new Fraction ( variable , 1 )
13+ var f = new Fraction ( variable , 1 ) ;
1414 var t = new Term ( f ) ;
1515 this . terms = [ t ] ;
1616 } else if ( variable instanceof Fraction ) {
@@ -37,7 +37,7 @@ Expression.prototype.coefficients = function() {
3737 arr . push ( this . terms [ i ] . coefficient ( ) ) ;
3838 }
3939 return arr ;
40- }
40+ } ;
4141
4242Expression . prototype . simplify = function ( ) {
4343 var copy = this . copy ( ) ;
@@ -133,7 +133,7 @@ Expression.prototype.divide = function(a, simplify) {
133133 var copy = this . copy ( ) ;
134134
135135 for ( var i = 0 ; i < copy . terms . length ; i ++ ) {
136- copy . terms [ i ] = copy . terms [ i ] . divide ( thatTerm ) ;
136+ copy . terms [ i ] = copy . terms [ i ] . divide ( thatTerm , simplify ) ;
137137 }
138138
139139 return copy ;
@@ -143,15 +143,9 @@ Expression.prototype.divide = function(a, simplify) {
143143 return new Expression ( rational ) ;
144144 }
145145 } else if ( a instanceof Fraction || isInt ( a ) ) {
146- var copy = this . copy ( ) ;
147-
148- for ( var i = 0 ; i < copy . terms . length ; i ++ ) {
149- copy . terms [ i ] = copy . terms [ i ] . divide ( a ) ;
150- }
151-
152- return copy ;
146+ return this . divide ( new Expression ( a ) , simplify ) ;
153147 } else {
154- throw new TypeError ( "Invalid Argument (" + a . toString ( ) + "): Divisor must be of type Fraction or Integer." ) ;
148+ throw new TypeError ( "Invalid Argument (" + a . toString ( ) + "): Divisor must be of type Expression, Fraction or Integer." ) ;
155149 }
156150} ;
157151
@@ -177,7 +171,6 @@ Expression.prototype.pow = function(a, simplify) {
177171
178172Expression . prototype . eval = function ( values , simplify ) {
179173 var exp = new Expression ( ) ;
180- exp . constants = ( simplify ? [ this . constant ( ) ] : this . constants . slice ( ) ) ;
181174
182175 //add all evaluated terms of this to exp
183176 exp = this . terms . reduce ( function ( p , c ) { return p . add ( c . eval ( values , simplify ) , simplify ) ; } , exp ) ;
@@ -201,7 +194,7 @@ Expression.prototype.toRational = function() {
201194 var numer = copy ;
202195 var denom = new Expression ( 1 ) ;
203196 return new Rational ( numer , denom ) ;
204- }
197+ } ;
205198
206199Expression . prototype . toString = function ( options ) {
207200 var str = "" ;
@@ -493,9 +486,9 @@ Term.prototype.subtract = function(term) {
493486 var copy = this . copy ( ) ;
494487 copy . coefficients = [ copy . coefficient ( ) . subtract ( term . coefficient ( ) ) ] ;
495488 return copy ;
496- } else if ( a instanceof Rational ) {
489+ } else if ( term instanceof Rational ) {
497490 var exp = new Expression ( this ) ;
498- return exp . toRational ( ) . subtract ( a ) ;
491+ return exp . toRational ( ) . subtract ( term ) ;
499492 } else {
500493 throw new TypeError ( "Invalid Argument (" + term . toString ( ) + "): Subtrahend must be of type String, Expression, Term, Fraction or Integer." ) ;
501494 }
@@ -506,7 +499,13 @@ Term.prototype.multiply = function(a, simplify) {
506499
507500 if ( a instanceof Term ) {
508501 thisTerm . variables = thisTerm . variables . concat ( a . variables ) ;
509- thisTerm . coefficients = a . coefficients . concat ( thisTerm . coefficients ) ;
502+ thisTerm . coefficients = a . coefficients
503+ . concat ( thisTerm . coefficients )
504+ . filter ( function ( a ) {
505+ return ! ( a . numer == 1 && a . denom == 1 ) ;
506+ } ) ;
507+ if ( thisTerm . coefficients . length == 0 )
508+ thisTerm . coefficients . push ( new Fraction ( 1 , 1 ) ) ;
510509
511510 } else if ( isInt ( a ) || a instanceof Fraction ) {
512511 var newCoef = ( isInt ( a ) ? new Fraction ( a , 1 ) : a ) ;
@@ -619,13 +618,15 @@ Term.prototype.maxDegreeOfVariable = function(variable) {
619618} ;
620619
621620Term . prototype . canBeCombinedWith = function ( term ) {
622- if ( term instanceof Rational )
621+ if ( term instanceof Rational ||
622+ ( this . maxDegree ( ) == 0 &&
623+ term . maxDegree ( ) == 0 ) )
623624 return true ;
624625 var thisVars = this . variables ;
625626 var thatVars = term . variables ;
626627
627628 if ( thisVars . length != thatVars . length ) {
628- return this . maxDegree ( ) == 0 && term . maxDegree ( ) == 0 ;
629+ return false ;
629630 }
630631
631632 var matches = 0 ;
@@ -762,7 +763,7 @@ var Rational = function(a, b) {
762763 this . denom = b ;
763764 else
764765 this . denom = new Expression ( b ) ;
765- }
766+ } ;
766767
767768function toFraction ( a ) {
768769 var i ;
@@ -797,11 +798,11 @@ function gcd_term(a, b) {
797798
798799Rational . prototype . copy = function ( ) {
799800 return new Rational ( this . numer . copy ( ) , this . denom . copy ( ) ) ;
800- }
801+ } ;
801802
802803Rational . prototype . canBeCombinedWith = function ( a ) {
803804 return true ;
804- }
805+ } ;
805806
806807Rational . prototype . add = function ( a ) {
807808 if ( a instanceof Rational ) {
@@ -818,7 +819,7 @@ Rational.prototype.add = function(a) {
818819 } else {
819820 return this . add ( new Expression ( a ) ) ;
820821 }
821- }
822+ } ;
822823
823824Rational . prototype . subtract = function ( a ) {
824825 if ( a instanceof Rational ) {
@@ -828,7 +829,7 @@ Rational.prototype.subtract = function(a) {
828829 } else {
829830 return this . subtract ( new Expression ( a ) ) ;
830831 }
831- }
832+ } ;
832833
833834Rational . prototype . multiply = function ( a ) {
834835 if ( a instanceof Rational ) {
@@ -842,7 +843,7 @@ Rational.prototype.multiply = function(a) {
842843 } else {
843844 return this . multiply ( new Expression ( a ) ) ;
844845 }
845- }
846+ } ;
846847
847848Rational . prototype . divide = function ( a ) {
848849 if ( a instanceof Rational ) {
@@ -856,7 +857,7 @@ Rational.prototype.divide = function(a) {
856857 } else {
857858 return this . divide ( new Expression ( a ) ) ;
858859 }
859- }
860+ } ;
860861
861862Rational . prototype . reduce = function ( ) {
862863 var copy = this . copy ( ) ;
@@ -878,11 +879,11 @@ Rational.prototype.reduce = function() {
878879 copy . denom = copy . denom . divide ( g ) ;
879880
880881 return copy ;
881- }
882+ } ;
882883
883884Rational . prototype . toString = function ( ) {
884885 return "(" + this . numer + ") / (" + this . denom + ")" ;
885- }
886+ } ;
886887
887888module . exports = {
888889 Rational : Rational ,
0 commit comments