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

Adding Polynomials Rule #25

Merged
merged 29 commits into from
May 20, 2017
Merged
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
2 changes: 1 addition & 1 deletion lib/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export const DISTRIBUTE_NEGATIVE_ONE =
// COLLECT AND COMBINE
export {default as COLLECT_LIKE_TERMS} from './rules/collect-like-terms'


export {ADD_POLYNOMIAL_TERMS} from './rules/collect-like-terms'

// SOLVING FOR A VARIABLE

Expand Down
72 changes: 70 additions & 2 deletions lib/rules/collect-like-terms.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {parse, print} from 'math-parser'
import {build, query} from 'math-nodes'

import {canApplyRule} from '../matcher.js'
import flattenOperands from '../flatten-operands.js'
import {defineRule, populatePattern} from '../matcher'

const clone = node => JSON.parse(JSON.stringify(node))
Expand Down Expand Up @@ -107,6 +108,7 @@ const getCoefficientsAndConstants = (node) => {
constants.push(arg)
} else {
const sortedVariables = sortVariables(getVariableFactors(arg))

const coefficient = getCoefficient(arg)
const implicit = isImplicit(arg)

Expand All @@ -132,7 +134,7 @@ const COLLECT_LIKE_TERMS = defineRule(
const {constants, coefficientMap} = getCoefficientsAndConstants(node)
hasLikeTerms = constants.length > 1 ||
Object.keys(coefficientMap)
.some(key => coefficientMap[key].length > 1)
.some(key => coefficientMap[key].length > 1)
}
return hasLikeTerms ? {node} : null
},
Expand Down Expand Up @@ -192,4 +194,70 @@ const COLLECT_LIKE_TERMS = defineRule(
}
)

export const ADD_POLYNOMIAL_TERMS = defineRule(
// MATCH FUNCTION
(node) => {
let hasLikeTerms = false

if (isPolynomial(node)) {

// e.g 2x + 2x + 4 + 6
// coefficient map: {'x': [[2 node] [2 node]}
// constants: [[4 node] [6 node]]
const {constants, coefficientMap} = getCoefficientsAndConstants(node)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we run COLLECT_LIKE_TERMS first then we shouldn't have to do any grouping.


// checks if at least one key has more than 1
// coefficient term
hasLikeTerms = Object.keys(coefficientMap)
.some(key => coefficientMap[key].length > 1)
}
return hasLikeTerms ? {node} : null
},


// REWRITE FUNCTION
(node) => {
const {constants, coefficientMap} = getCoefficientsAndConstants(node)

// One gigantic applyNode
// Use example: 2x + 3x + 4
const result = build.applyNode(
'add',

// for each identifier, generate the simplified term
// e.g 2x + 3x -> 5x
Object.keys(coefficientMap).sort().map(key => {
// [[2 node], [3 node]]
const coeffs = coefficientMap[key]
// x
const variable = parse(key)

// 5
const newCoeff =
coeffs.reduce((runningTotal, value) =>
runningTotal + query.getValue(value), 0)

// [[5 node]]
const newCoeffNode = build.numberNode(newCoeff)

// [[5x node]]
const term = build.applyNode(
'mul', [clone(newCoeffNode), clone(variable)],
{implicit: true}
)

return flattenOperands(term)
}))

// Adding the constants if there are any
if (constants.length > 1) {
result.args.push(build.applyNode('add', constants))
} else if (constants.length > 0) {
result.args.push(constants[0])
}

return result
}
)

export default COLLECT_LIKE_TERMS
11 changes: 11 additions & 0 deletions test/rules_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ describe('rules', () => {
['2x + 7y + 5 + 3y + 9x + 11', '(2 x + 9 x) + (7 y + 3 y) + (5 + 11)'],
])

suite('add polynomials', rules.ADD_POLYNOMIAL_TERMS, [
['2x + 2x + 2 + 4', '4 x + (2 + 4)'],
['3y^2 - 2y^2 + y^4', '1 y^2 + 1 y^4'],
['x - x', '0 x'],
['2x + 3x + 2y + 3y', '5 x + 5 y'],
['-2y + 3y', '1 y'],
['3 xy + 2 xy', '5 xy'],
['3 xy - 2 xy + x^2y^2', '1 x^2 y^2 + 1 xy'],
['2 x y + 2 y x', '4 x y'],
])

suite('handles basic arithmetic', rules.SIMPLIFY_ARITHMETIC, [
['1 + 2', '3'],
['1 + 2 + 3', '6'],
Expand Down