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

Some additional helper functions #7

Open
wants to merge 23 commits into
base: master
Choose a base branch
from

Conversation

aliang8
Copy link
Contributor

@aliang8 aliang8 commented Jul 4, 2017

getVariableFactors 2x^2 * y
getCoefficientsAndConstants x + 4 + 2^x + 4
negate -3x -> 3x
hasCoeff 2x^2 -> true, x -> false

]
`;

exports[`query getVariableFactors 2x^2 * z^2 + y^3 1`] = `
Copy link
Member

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.

@aliang8 aliang8 changed the title Fix getVariableFactors Fix getVariableFactors and getCoefficientsAndConstants Jul 5, 2017
])

it('isVariableFactor', () => {
it('isVariableFactor', () => {
console.log(query.isPolynomial(parse('2*(y+x)')))
Copy link
Member

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

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?

Copy link
Contributor Author

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.

Copy link
Member

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?

@aliang8 aliang8 changed the title Fix getVariableFactors and getCoefficientsAndConstants Some additional helper functions Jul 6, 2017
@@ -41,25 +41,25 @@ const relationIdentifierMap = {

export const isRel = node => isApply(node) && node.op in relationIdentifierMap

export const isNumber = node => {
Copy link
Member

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.

Copy link
Contributor Author

@aliang8 aliang8 Jul 6, 2017

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.

Copy link
Member

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.

Copy link
Contributor Author

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', () => {
Copy link
Member

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.

Copy link
Contributor Author

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

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.

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