Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
40 changes: 34 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'

import * as query from '../query'
Expand Down Expand Up @@ -54,6 +54,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 @@ -65,7 +67,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 @@ -106,6 +110,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(build.negate(parse('2'))), -2)
assert.equal(print(build.negate(parse('2x^2'))), '-2 x^2')
assert.equal(print(build.negate(parse('2xyz'))), '-2 xyz')
assert.equal(print(build.negate(parse('-2'))), 2)
assert.equal(print(build.negate(parse('-2x^2'))), '2 x^2')
assert.equal(print(build.negate(parse('-2x^2 y^2'))), '2 x^2 y^2')
assert.equal(print(build.negate(parse('2x^2 y^2'))), '-2 x^2 y^2')
assert.equal(print(build.negate(parse('2/3'))), '-2 / 3')
assert.equal(print(build.negate(parse('-2/3'))), '2 / 3')
assert.equal(print(build.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 @@ -262,10 +287,13 @@ 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(parse('2/3')))
assert(query.isRationalNumber(parse('2.2')))
assert(!query.isRationalNumber(x))
assert(!query.isRationalNumber(parse('(2 + x)/2')))
})

it('getValue', () => {
Expand Down
22 changes: 22 additions & 0 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Functions to build nodes
*/

const q = require('./query.js')

export const apply = (op, args, options = {}) => ({
type: 'Apply',
op: op,
Expand Down Expand Up @@ -56,3 +58,23 @@ export const parensNode = parens
export const numberNode = number
export const identifierNode = identifier
export const applyNode = apply

// e.g 3 -> -3, -3x -> 3x, x + 3 -> -(x + 3), 2/3 -> -2 / 3
export const negate = (node) => {
if (q.isNeg(node)) {
return node.args[0]
} else if (q.isAdd(node)) {
return neg(node)
} else if (q.isFraction(node)) {
return div(negate(q.getNumerator(node)), q.getDenominator(node))
} else if (q.isPolynomialTerm(node)) {
if (q.isNeg(q.getCoefficient(node))) {
return apply(
'mul',
[negate(q.getCoefficient(node)), ...node.args.slice(1)],
{implicit: node.implicit})
} else {
return neg(node)
}
}
}
Loading