Skip to content
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

Merged
merged 29 commits into from
May 20, 2017
Merged

Adding Polynomials Rule #25

merged 29 commits into from
May 20, 2017

Conversation

aliang8
Copy link
Contributor

@aliang8 aliang8 commented May 20, 2017

Added addingPolynomial rule in collect-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 to y? Also added comments in the code, let me know if it is good or if I should remove it!

null, {implicit: true}
)

return term
Copy link
Member

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)
Copy link
Member

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.

@@ -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'],
Copy link
Member

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.

['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'],
Copy link
Member

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]
Copy link
Member

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?

['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'],
Copy link
Member

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.

@@ -84,6 +85,10 @@ const getVariableFactorName = (node) => {
}
}

var alphabetize = function(variable) {
return variable.split('').sort().join('')
}
Copy link
Member

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.

['-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)'],
Copy link
Member

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'.

['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'],
Copy link
Member

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`.

@@ -192,4 +194,72 @@ const COLLECT_LIKE_TERMS = defineRule(
}
)

export const ADD_POLYNOMIAL_TERMS = defineRule(
// MATCH PATTERN
Copy link
Member

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.

},


// REWRITE PATTERN
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// REWRITE FUNCTION

// TODO: find a better way to specify the location
// [[5 node]]
const newCoeffNode =
build.numberNode(newCoeff, null, null)
Copy link
Member

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.

@@ -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'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this failing?

@kevinbarabash kevinbarabash merged commit 495cfb6 into semantic-math:master May 20, 2017
@kevinbarabash
Copy link
Member

@aliang8 looks good. Thanks for making those changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants