-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding Polynomials Rule #25
Conversation
lib/rules/collect-like-terms.js
Outdated
null, {implicit: true} | ||
) | ||
|
||
return term |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll want to flatten this term before you return it so that this will handle cases like 2 x y + 3 x y
// e.g 2x + 2x + 4 + 6 | ||
// coefficient map: {'x': [[2 node] [2 node]} | ||
// constants: [[4 node] [6 node]] | ||
const {constants, coefficientMap} = getCoefficientsAndConstants(node) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we run COLLECT_LIKE_TERMS
first then we shouldn't have to do any grouping.
test/rules_test.js
Outdated
@@ -205,6 +205,14 @@ describe('rules', () => { | |||
['2x + 7y + 5 + 3y + 9x + 11', '(2 x + 9 x) + (7 y + 3 y) + (5 + 11)'], | |||
]) | |||
|
|||
suite('add polynomials', rules.ADD_POLYNOMIAL_TERMS, [ | |||
['2x + 2x + 2 + 4', '4 x + (2 + 4)'], | |||
['2x + 2y', '2 x + 2 y'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one fails b/c the pattern match returns false. We should have better error handling for this, see #27.
test/rules_test.js
Outdated
['2x + 2x + 2 + 4', '4 x + (2 + 4)'], | ||
['2x + 2y', '2 x + 2 y'], | ||
['2x + 3x + 2y + 3y', '5 x + 5 y'], | ||
['-2y + 2x - 3x^2', '-2y + 2x - 3x^2'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same reason as the other failure.
lib/.#matcher.js
Outdated
@@ -0,0 +1 @@ | |||
[email protected] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add these files to your global .gitignore?
test/rules_test.js
Outdated
['2x + 2y', '2 x + 2 y'], | ||
['2x + 3x + 2y + 3y', '5 x + 5 y'], | ||
['-2y + 2x - 3x^2', '-2y + 2x - 3x^2'], | ||
['-2y + 3y', '1 y'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may want to add some cases that include terms with multiple variables factors or factors with exponents and definitely some with subtraction.
lib/rules/collect-like-terms.js
Outdated
@@ -84,6 +85,10 @@ const getVariableFactorName = (node) => { | |||
} | |||
} | |||
|
|||
var alphabetize = function(variable) { | |||
return variable.split('').sort().join('') | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't being called anywhere.
test/rules_test.js
Outdated
['-2y + 3y', '1 y'], | ||
['3 xy + 2 xy', '5 xy'], | ||
['3 xy - 2 xy + x^2y^2', '1 (x^2 y^2) + 1 xy'], | ||
['2 x y + 2 y x', '4 (x y)'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be '2 x y + 2 y x', '4 x y'
.
test/rules_test.js
Outdated
['2x + 3x + 2y + 3y', '5 x + 5 y'], | ||
['-2y + 3y', '1 y'], | ||
['3 xy + 2 xy', '5 xy'], | ||
['3 xy - 2 xy + x^2y^2', '1 (x^2 y^2) + 1 xy'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you change these to use x y
instead of xy
? Also,
x^2y^2 --> 1 (x^2 y^2)
should probably be
x^2y^2 --> 1 x^2 y^2
This will require calling `flattenOperands` on `term` before returning it from the rewrite function of `ADD_POLYNOMIAL_TERMS`.
lib/rules/collect-like-terms.js
Outdated
@@ -192,4 +194,72 @@ const COLLECT_LIKE_TERMS = defineRule( | |||
} | |||
) | |||
|
|||
export const ADD_POLYNOMIAL_TERMS = defineRule( | |||
// MATCH PATTERN |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor nit... this should be // MATCH FUNCTION
.
lib/rules/collect-like-terms.js
Outdated
}, | ||
|
||
|
||
// REWRITE PATTERN |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// REWRITE FUNCTION
lib/rules/collect-like-terms.js
Outdated
// TODO: find a better way to specify the location | ||
// [[5 node]] | ||
const newCoeffNode = | ||
build.numberNode(newCoeff, null, null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be numberNode(newCoeff)
now.
test/rules_test.js
Outdated
@@ -110,7 +110,7 @@ describe('rules', () => { | |||
['x * -1', '-x'], | |||
['(x + 1) * -1', '-(x + 1)'], | |||
['x^((x + 1) * -1)', 'x^-(x + 1)'], | |||
['2x * 2 * -1', '2 x * -2'], | |||
//['2x * 2 * -1', '2 x * -2'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this failing?
@aliang8 looks good. Thanks for making those changes. |
Added
addingPolynomial
rule incollect-like-terms.js
. Can move to a separate file. Added some test cases.Should we leave it as
1 y
in-2y + 3y
or should we change it toy
? Also added comments in the code, let me know if it is good or if I should remove it!