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 20 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
37 changes: 31 additions & 6 deletions lib/__test__/query.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert'
import {parse} from 'math-parser'
import {parse, print} from 'math-parser'
import stringify from 'json-stable-stringify'

const reverseAlphabetical = (a, b) => a.key < b.key ? 1 : -1
Expand Down 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,7 +66,9 @@ 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', () => {
Expand Down Expand Up @@ -105,6 +109,27 @@ describe('query', () => {
assert(!query.isPolynomial(parse('1/x')))
})

it('hasCoeff', () => {
assert(query.hasCoeff(parse('2x')))
assert(query.hasCoeff(parse('2x^2')))
assert(query.hasCoeff(parse('2 x y z')))
assert(!query.hasCoeff(parse('2')))
assert(!query.hasCoeff(parse('x')))
})

it('negate', () => {
assert.equal(print(query.negate(parse('2'))), -2)
assert.equal(print(query.negate(parse('2x^2'))), `-2 x^2`)
assert.equal(print(query.negate(parse('2xyz'))), `-2 xyz`)
assert.equal(print(query.negate(parse('-2'))), 2)
assert.equal(print(query.negate(parse('-2x^2'))), `2 x^2`)
assert.equal(print(query.negate(parse('-2x^2 y^2'))), `2 x^2 y^2`)
assert.equal(print(query.negate(parse('2x^2 y^2'))), `-2 x^2 y^2`)
assert.equal(print(query.negate(parse('2/3'))), `-2 / 3`)
assert.equal(print(query.negate(parse('-2/3'))), `2 / 3`)
assert.equal(print(query.negate(parse('(x+3)/3'))), `-(x + 3) / 3`)
})

it('hasSameBase', () => {
assert(query.hasSameBase(parse('x^1'), parse('x^2')))
assert(query.hasSameBase(parse('(x+1)^2'), parse('(x+1)^x')))
Expand Down Expand Up @@ -221,10 +246,10 @@ describe('query', () => {
assert(!query.isRel(x))
})

it('isNumber', () => {
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.

👍

assert(query.isRationalNumber(a))
assert(query.isRationalNumber(parse('-2')))
assert(!query.isRationalNumber(x))
})

it('getValue', () => {
Expand Down
Loading