-
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
Some additional helper functions #7
base: master
Are you sure you want to change the base?
Conversation
] | ||
`; | ||
|
||
exports[`query getVariableFactors 2x^2 * z^2 + y^3 1`] = ` |
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.
y^3
is not a factor, it's a term. If you want a function that finds all variable factors we should have a separate function for that.
lib/__test__/query.test.js
Outdated
]) | ||
|
||
it('isVariableFactor', () => { | ||
it('isVariableFactor', () => { | ||
console.log(query.isPolynomial(parse('2*(y+x)'))) |
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.
I think these two lines should be reverted.
lib/query.js
Outdated
@@ -232,7 +232,7 @@ export const getCoefficientsAndConstants = (node, coefficientMap = {}, constants | |||
} else { | |||
coefficientMap[key].push(coefficient) | |||
} | |||
} else if(isPolynomial(node) || isApply(node)) { | |||
} else if(isPolynomial(node) || isAdd(node) || (isMul(node) && node.args.every(isPolynomialTerm))) { | |||
// 2x^2 + 3x + 1, 3x^2 * 2x^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.
Given the comment, shouldn't checking isPolynomial(node)
enough since we've already ruled out constants and single term polynomials. Why are the other conditions necessary?
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.
I don't think so; it leaves out non-polynomial expressions such as x + 4 + x + 2^x
that we would still want to collect and combine.
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.
Gotcha. Can you update the comment with that example?
@@ -41,25 +41,25 @@ const relationIdentifierMap = { | |||
|
|||
export const isRel = node => isApply(node) && node.op in relationIdentifierMap | |||
|
|||
export const isNumber = 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.
I think we should keep isNumber
in addition to isRationalNumber
.
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.
Hmmm I don't see how it would be useful, but I add it back.
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.
I guess we isInteger
. Okay, let's leave this as is. We can always add it back later if it does turn out we need it.
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.
Sure :D
assert(query.isNumber(a)) | ||
assert(query.isNumber(parse('-2'))) | ||
assert(!query.isNumber(x)) | ||
it('isRationalNumber', () => { |
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.
Maybe add some tests that check for factions and decimals.
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.
👍
lib/query.js
Outdated
} | ||
|
||
// e.g 3 -> -3, -3x -> 3x, x + 3 -> -(x + 3), 2/3 -> -2 / 3 | ||
export const negate = (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.
I think this should be part of build
not query.
getVariableFactors
2x^2 * y
getCoefficientsAndConstants
x + 4 + 2^x + 4
negate
-3x -> 3x
hasCoeff
2x^2 -> true, x -> false