Skip to content
This repository has been archived by the owner on May 23, 2019. It is now read-only.

Commit

Permalink
Merge pull request #78 from Benjadahl/master
Browse files Browse the repository at this point in the history
Support for dividing monomial expressions
  • Loading branch information
nicolewhite committed Jun 8, 2017
2 parents cc0f735 + ce8bb33 commit 40c1de6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
50 changes: 47 additions & 3 deletions src/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Expression.prototype.simplify = function() {

Expression.prototype.copy = function() {
var copy = new Expression();

//copy all constants
copy.constants = this.constants.map(function(c){return c.copy();});
//copy all terms
Expand Down Expand Up @@ -154,6 +154,50 @@ Expression.prototype.divide = function(a, simplify) {
copy.constants = copy.constants.map(function(c){return c.divide(a,simplify);});

return copy;
} else if (a instanceof Expression) {
//Simplify both expressions
var num = this.copy().simplify();
var denom = a.copy().simplify();

//Total amount of terms and constants
var numTotal = num.terms.length + num.constants.length;
var denomTotal = denom.terms.length + denom.constants.length;

//Check if both terms are monomial
if (numTotal === 1 && denomTotal === 1) {
//Devide coefficients
var numCoef = num.terms[0].coefficients[0];
var denomCoef = denom.terms[0].coefficients[0];

//The expressions have just been simplified - only one coefficient per term
num.terms[0].coefficients[0] = numCoef.divide(denomCoef, simplify);
denom.terms[0].coefficients[0] = new Fraction(1, 1);

//Cancel variables
for (var i = 0; i < num.terms[0].variables.length; i++) {
var numVar = num.terms[0].variables[i];
for (var j = 0; j < denom.terms[0].variables.length; j++) {
var denomVar = denom.terms[0].variables[j];
//Check for equal variables
if (numVar.variable === denomVar.variable) {
//Use the rule for division of powers
num.terms[0].variables[i].degree = numVar.degree - denomVar.degree;
denom.terms[0].variables[j].degree = 0;
}
}
}

//Invers all degrees of remaining variables
for (var i = 0; i < denom.terms[0].variables.length; i++) {
denom.terms[0].variables[i].degree *= -1;
}
//Multiply the inversed variables to the numenator
num = num.multiply(denom, simplify);

return num;
} else {
throw new TypeError("Invalid Argument ((" + num.toString() + ")/("+ denom.toString() + ")): Only monomial expressions can be divided.");
}
} else {
throw new TypeError("Invalid Argument (" + a.toString() + "): Divisor must be of type Fraction or Integer.");
}
Expand Down Expand Up @@ -633,7 +677,7 @@ Term.prototype.toString = function(options) {
Term.prototype.toTex = function(dict) {
var dict = (dict === undefined) ? {} : dict;
dict.multiplication = !("multiplication" in dict) ? "cdot" : dict.multiplication;

var op = " \\" + dict.multiplication + " ";

var str = "";
Expand Down Expand Up @@ -702,4 +746,4 @@ module.exports = {
Expression: Expression,
Term: Term,
Variable: Variable
};
};
18 changes: 15 additions & 3 deletions test/expression-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,20 @@ describe("Expression division", function() {
expect(answer.toString()).toEqual("1/5x");
});

it("should not allow dividing by another expression", function() {
expect(function(){x.divide(y);}).toThrow("Invalid Argument (y): Divisor must be of type Fraction or Integer.");
it("should allow dividing monomial expressions", function() {
expect(x.divide(y).toString()).toEqual("xy^-1");
});

it("should not allow division of a multinominal denomenator", function() {
var multi = new Expression("x").add(3);

expect(function(){x.divide(multi);}).toThrow("Invalid Argument ((x)/(x + 3)): Only monomial expressions can be divided.");
});

it("should not allow division of a multinominal numenator", function() {
var multi = new Expression("x").add(3);

expect(function(){multi.divide(x);}).toThrow("Invalid Argument ((x + 3)/(x)): Only monomial expressions can be divided.");
});

it("should throw an exception if dividing by zero", function() {
Expand Down Expand Up @@ -846,4 +858,4 @@ describe("Expression summation", function() {

expect(answer.toString()).toEqual("4y + 30");
});
});
});

0 comments on commit 40c1de6

Please sign in to comment.