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
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions lib/__test__/__snapshots__/query.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,45 @@ exports[`query getCoefficientsAndConstants 3x^2 * 2x^2 1`] = `
}
`;

exports[`query getCoefficientsAndConstants x + 4 + 2^x + x 1`] = `
{
"others": [
{
"type": "Apply",
"op": "pow",
"args": [
{
"value": "2",
"type": "Number"
},
{
"type": "Identifier",
"name": "x"
}
]
}
],
"constants": [
{
"value": "4",
"type": "Number"
}
],
"coefficientMap": {
"x": [
{
"value": "1",
"type": "Number"
},
{
"value": "1",
"type": "Number"
}
]
}
}
`;

exports[`query getCoefficientsAndConstants x^3 + y^3 + x y z + 3 1`] = `
{
"others": [
Expand Down Expand Up @@ -210,6 +249,55 @@ exports[`query getCoefficientsAndConstants x^3 + y^3 + x y z + 3 1`] = `
}
`;

exports[`query getCoefficientsAndConstants z + 2*(y + x) + 4 + z 1`] = `
{
"others": [
{
"type": "Apply",
"op": "mul",
"args": [
{
"value": "2",
"type": "Number"
},
{
"type": "Apply",
"op": "add",
"args": [
{
"type": "Identifier",
"name": "y"
},
{
"type": "Identifier",
"name": "x"
}
]
}
]
}
],
"constants": [
{
"value": "4",
"type": "Number"
}
],
"coefficientMap": {
"z": [
{
"value": "1",
"type": "Number"
},
{
"value": "1",
"type": "Number"
}
]
}
}
`;

exports[`query getVariableFactors 2 x y z 1`] = `
[
{
Expand All @@ -236,6 +324,29 @@ exports[`query getVariableFactors 2x 1`] = `
]
`;

exports[`query getVariableFactors 2x^2 * y 1`] = `
[
{
"type": "Apply",
"op": "pow",
"args": [
{
"type": "Identifier",
"name": "x"
},
{
"value": "2",
"type": "Number"
}
]
},
{
"type": "Identifier",
"name": "y"
}
]
`;

exports[`query getVariableFactors 2x^2 1`] = `
[
{
Expand All @@ -255,6 +366,39 @@ exports[`query getVariableFactors 2x^2 1`] = `
]
`;

exports[`query getVariableFactors x^2 * 2/3 y^2 1`] = `
[
{
"type": "Apply",
"op": "pow",
"args": [
{
"type": "Identifier",
"name": "x"
},
{
"value": "2",
"type": "Number"
}
]
},
{
"type": "Apply",
"op": "pow",
"args": [
{
"type": "Identifier",
"name": "y"
},
{
"value": "2",
"type": "Number"
}
]
}
]
`;

exports[`query getVariableFactors x^2 1`] = `
[
{
Expand Down
9 changes: 7 additions & 2 deletions lib/__test__/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ describe('query', () => {
'x^2',
'2x^2',
'2 x y z',
'2x^2 * y',
'x^2 * 2/3 y^2'
])

suite('getCoefficientsAndConstants', query.getCoefficientsAndConstants, [
Expand All @@ -64,10 +66,13 @@ describe('query', () => {
'x^3 + y^3 + x y z + 3',
'2/3 + 3 + cos(4)',
'3x^2 * 2x^2',
'2/3(x + 1)^1'
'2/3(x + 1)^1',
'x + 4 + 2^x + x',
'z + 2*(y + x) + 4 + z'
])

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.

assert(query.isVariableFactor(parse('x')))
assert(query.isVariableFactor(parse('xyz')))
assert(query.isVariableFactor(parse('x^2')))
Expand Down
15 changes: 7 additions & 8 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,16 @@ export const getCoefficient = (node) => {
}
}

export const getVariableFactors = (node) => {
export const getVariableFactors = (node, factors = []) => {
if (isVariableFactor(node)) {
return [node]
factors.push(node)
} else if (isMul(node)) {
return node.args.filter(isVariableFactor)
node.args.forEach(arg => getVariableFactors(arg, factors))
} else if (isNeg(node)) {
// TODO: figure out how to handle (x)(-y)(z)
return getVariableFactors(node.args[0])
} else {
return []
}
return factors
}

export const getVariableFactorName = (node) => {
Expand Down Expand Up @@ -213,10 +212,10 @@ export const sortVariables = (variables) =>
export const getCoefficientsAndConstants = (node, coefficientMap = {}, constants = [], others = []) => {
if (isNumber(node) || isConstantFraction(node)) {
constants.push(node)
} else if (isFunction(node)) {
} else if (isFunction(node) || hasConstantBase(node)) {
// cos, sin, f(a), etc
others.push(node)
} else if (isPolynomialTerm(node) && !hasConstantBase(node)) {
} else if (isPolynomialTerm(node)) {
// x^2, 3x^2

// sort the variables first (2yzx -> 2xyz)
Expand All @@ -233,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?

node.args.forEach(function(arg) {
getCoefficientsAndConstants(arg, coefficientMap, constants, others)
Expand Down