From 626be425906a41a4f4d7dd7707c705b25a0876c6 Mon Sep 17 00:00:00 2001 From: Mike Lodato Date: Fri, 15 May 2015 22:25:20 -0400 Subject: [PATCH 1/3] collect terms with common exponent --- coffeequate/src/operators/Mul.coffee | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/coffeequate/src/operators/Mul.coffee b/coffeequate/src/operators/Mul.coffee index 1bb27d0..2e608a6 100644 --- a/coffeequate/src/operators/Mul.coffee +++ b/coffeequate/src/operators/Mul.coffee @@ -309,6 +309,25 @@ define [ if constantterm?.evaluate?() == 0 return new terminals.Constant("0") + newLikeTerms = [] + for term0 in liketerms + [base0, power0] = term0 + + found = false + for term1 in newLikeTerms + [base1, power1] = term1 + + if power1.equals?(power0, equivalencies) + unless base1 instanceof Mul + base1 = new Mul(base1) + term1[0] = base1 + base1.children.push(base0) + found = true + break + + newLikeTerms.push(term0) unless found + liketerms = newLikeTerms + newMul = null for liketerm in liketerms if liketerm[1].evaluate?() != 1 @@ -651,4 +670,4 @@ define [ return false return true - return Mul \ No newline at end of file + return Mul From dc289adf2aa7a13d3a551dab2ae4fe8b755f22ec Mon Sep 17 00:00:00 2001 From: Mike Lodato Date: Fri, 15 May 2015 23:11:15 -0400 Subject: [PATCH 2/3] simplify terms after collecting by exponent --- coffeequate/src/operators/Mul.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coffeequate/src/operators/Mul.coffee b/coffeequate/src/operators/Mul.coffee index 2e608a6..6636cba 100644 --- a/coffeequate/src/operators/Mul.coffee +++ b/coffeequate/src/operators/Mul.coffee @@ -326,7 +326,7 @@ define [ break newLikeTerms.push(term0) unless found - liketerms = newLikeTerms + liketerms = ([base.expandAndSimplify(), power] for [base, power] in newLikeTerms) newMul = null for liketerm in liketerms From c4049cc2a5fe775655e8c80c4879a0254adba981 Mon Sep 17 00:00:00 2001 From: Mike Lodato Date: Mon, 18 May 2015 13:52:37 -0400 Subject: [PATCH 3/3] fix stack overflow in common-exponent simplification --- coffeequate/src/operators/Mul.coffee | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/coffeequate/src/operators/Mul.coffee b/coffeequate/src/operators/Mul.coffee index 6636cba..2bbcbf3 100644 --- a/coffeequate/src/operators/Mul.coffee +++ b/coffeequate/src/operators/Mul.coffee @@ -314,19 +314,21 @@ define [ [base0, power0] = term0 found = false - for term1 in newLikeTerms - [base1, power1] = term1 - - if power1.equals?(power0, equivalencies) - unless base1 instanceof Mul - base1 = new Mul(base1) - term1[0] = base1 - base1.children.push(base0) - found = true - break + unless power0.evaluate?() == 1 + for term1 in newLikeTerms + [base1, power1] = term1 + + if power1.equals?(power0, equivalencies) + base1.push(base0) + found = true + break - newLikeTerms.push(term0) unless found - liketerms = ([base.expandAndSimplify(), power] for [base, power] in newLikeTerms) + newLikeTerms.push([[base0], power0]) unless found + liketerms = ([ + (if base.length == 1 then base[0] else new Mul(base...)) + .expandAndSimplify(), + power + ] for [base, power] in newLikeTerms) newMul = null for liketerm in liketerms