-
Notifications
You must be signed in to change notification settings - Fork 282
WIP: Substitute symbols for expressions from optional scope #212
base: master
Are you sure you want to change the base?
Changes from 9 commits
b1fa321
9ed6e86
9e225bd
c62a82d
1219478
633522f
6effc8b
b382470
de2fa8e
921585d
0cbfe98
f378d87
c18ba6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,4 +2,4 @@ | |
| node_modules | ||
| *.log | ||
| .DS_Store | ||
| .idea | ||
| .idea | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| function substituteScope(scope) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you name this file
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also can you add a comment explaining the purpose of this function? I'm a bit confused about what it does - is it replacing scope things? Shouldn't that happen in the steps instead? |
||
| const newScope = Object.assign({}, scope); | ||
|
|
||
| for (var symbol in newScope) { | ||
| const targetVal = newScope[symbol].toString(); | ||
| for (var sym in newScope) { | ||
| const valStr = newScope[sym].toString(); | ||
| const replaced = valStr.replace(symbol, targetVal); | ||
| newScope[sym] = replaced; | ||
| } | ||
| } | ||
|
|
||
| return newScope; | ||
| } | ||
|
|
||
| module.exports = substituteScope; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| const math = require('mathjs'); | ||
| const stepThrough = require('./stepThrough'); | ||
| const Substitute = require('../Substitute'); | ||
|
|
||
| function simplifyExpressionString(expressionString, debug=false) { | ||
| function simplifyExpressionString(expressionString, debug=false, scope={}) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it'd be nice if debug was the last argument, though I know that's a lot of moving code around so let me know if you want help going through and editing it all
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also I think we should replace |
||
| const newScope = Substitute(scope); | ||
| let exprNode; | ||
| try { | ||
| exprNode = math.parse(expressionString); | ||
|
|
@@ -10,7 +12,7 @@ function simplifyExpressionString(expressionString, debug=false) { | |
| return []; | ||
| } | ||
| if (exprNode) { | ||
| return stepThrough(exprNode, debug); | ||
| return stepThrough(exprNode, debug, newScope); | ||
| } | ||
| return []; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ const divisionSearch = require('./divisionSearch'); | |
| const fractionsSearch = require('./fractionsSearch'); | ||
| const functionsSearch = require('./functionsSearch'); | ||
| const multiplyFractionsSearch = require('./multiplyFractionsSearch'); | ||
| const substituteScopeSearch = require('./substituteScopeSearch'); | ||
|
|
||
| const clone = require('../util/clone'); | ||
| const flattenOperands = require('../util/flattenOperands'); | ||
|
|
@@ -19,7 +20,7 @@ const removeUnnecessaryParens = require('../util/removeUnnecessaryParens'); | |
|
|
||
| // Given a mathjs expression node, steps through simplifying the expression. | ||
| // Returns a list of details about each step. | ||
| function stepThrough(node, debug=false) { | ||
| function stepThrough(node, debug=false, scope={}) { | ||
| if (debug) { | ||
| // eslint-disable-next-line | ||
| console.log('\n\nSimplifying: ' + print(node, false, true)); | ||
|
|
@@ -37,15 +38,15 @@ function stepThrough(node, debug=false) { | |
| let iters = 0; | ||
|
|
||
| // Now, step through the math expression until nothing changes | ||
| nodeStatus = step(node); | ||
| nodeStatus = step(node, scope); | ||
| while (nodeStatus.hasChanged()) { | ||
| if (debug) { | ||
| logSteps(nodeStatus); | ||
| } | ||
| steps.push(removeUnnecessaryParensInStep(nodeStatus)); | ||
|
|
||
| node = Status.resetChangeGroups(nodeStatus.newNode); | ||
| nodeStatus = step(node); | ||
| nodeStatus = step(node, scope); | ||
|
|
||
| if (iters++ === MAX_STEP_COUNT) { | ||
| // eslint-disable-next-line | ||
|
|
@@ -60,7 +61,7 @@ function stepThrough(node, debug=false) { | |
|
|
||
| // Given a mathjs expression node, performs a single step to simplify the | ||
| // expression. Returns a Node.Status object. | ||
| function step(node) { | ||
| function step(node, scope={}) { | ||
| let nodeStatus; | ||
|
|
||
| node = flattenOperands(node); | ||
|
|
@@ -69,6 +70,9 @@ function step(node) { | |
| const simplificationTreeSearches = [ | ||
| // Basic simplifications that we always try first e.g. (...)^0 => 1 | ||
| basicsSearch, | ||
| // Substitute symbols present in the optional scope object with | ||
| // their respective expressions. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add an example similar to the other simplification searches here?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added an example but let me know if it needs to be reworded.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks good to me! awesome |
||
| substituteScopeSearch, | ||
| // Simplify any division chains so there's at most one division operation. | ||
| // e.g. 2/x/6 -> 2/(x*6) e.g. 2/(x/6) => 2 * 6/x | ||
| divisionSearch, | ||
|
|
@@ -91,7 +95,7 @@ function step(node) { | |
| ]; | ||
|
|
||
| for (let i = 0; i < simplificationTreeSearches.length; i++) { | ||
| nodeStatus = simplificationTreeSearches[i](node); | ||
| nodeStatus = simplificationTreeSearches[i](node, scope); | ||
| // Always update node, since there might be changes that didn't count as | ||
| // a step. Remove unnecessary parens, in case one a step results in more | ||
| // parens than needed. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| const ChangeTypes = require('../../ChangeTypes'); | ||
| const Node = require('../../node'); | ||
| const TreeSearch = require('../../TreeSearch'); | ||
|
|
||
| // Searches through the tree, prioritizing deeper nodes, and substitutes | ||
| // in-scope values for their respective expressions on a symbol node if possible. | ||
| // Returns a Node.Status object. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. awesome documentation :D |
||
| const search = TreeSearch.postOrder(scopeSubstitution); | ||
|
|
||
| function scopeSubstitution(node, scope) { | ||
| if (Node.Type.isSymbol(node)) { | ||
| return substituteAndSimplifySymbols(node, scope); | ||
| } | ||
| else { | ||
| return Node.Status.noChange(node); | ||
| } | ||
| } | ||
|
|
||
| // SUBSTITUTES | ||
| // Returns a Node.Status object with substeps | ||
| function substituteAndSimplifySymbols(node, scope) { | ||
| if (!Node.Type.isSymbol(node)) { | ||
| return Node.Status.noChange(node); | ||
| } | ||
|
|
||
| const symbolName = node.name; | ||
| if (scope.hasOwnProperty(symbolName)) { | ||
| // when declared at top, kept getting TypeError: ___ is not a function | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you explain a bit more about when the error happens? I can try replicating it on my computer and see what's up. Did it say |
||
| const simplifyExpression = require('../../simplifyExpression'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so What you have here also means if you have My suggestion instead is:
I know it's a bit different from your idea, so we could chat more about this and make sure we're on the same page and both happy with whatever we decide to do. I think adding some tests that show multiple steps will also make it more clear what this changes. Let me know if you have more questions or comments about this! |
||
| const substeps = simplifyExpression(scope[symbolName]); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of simplifying the expression here, I think it's best to just do then any simplification of the value can happen in later steps instead of substeps and it makes this code less complicated! what do you think?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit turned around here, perhaps due to the approach I took. Since simplifyExpression is where the scope substitution takes place, I'm concerned calling math.parse(scope[symbolName]) here won't be effective since it will not perform the scope substitution. That may be why you advised to put the substituteScope call deeper in the simplify function? Left as is in my recent (incremental) PR. |
||
| if (substeps.length === 0) { | ||
| const newNode = Node.Creator.constant(Number(scope[symbolName])); | ||
| return Node.Status.nodeChanged( | ||
| ChangeTypes.SUBSTITUTE_SCOPE_SYMBOL, node, newNode); | ||
| } | ||
| else { | ||
| const newNode = substeps.slice(-1)[0].newNode; | ||
| return Node.Status.nodeChanged( | ||
| ChangeTypes.SUBSTITUTE_SCOPE_SYMBOL, node, newNode, false, substeps); | ||
| } | ||
| } | ||
| else { | ||
| return Node.Status.noChange(node); | ||
| } | ||
| } | ||
|
|
||
| module.exports = search; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,8 @@ const solveEquation = require('../../lib/solveEquation'); | |
|
|
||
| const NO_STEPS = 'no-steps'; | ||
|
|
||
| function testSolve(equationString, outputStr, debug=false) { | ||
| const steps = solveEquation(equationString, debug); | ||
| function testSolve(equationString, outputStr, debug=false, scope={}) { | ||
| const steps = solveEquation(equationString, debug, scope); | ||
| let lastStep; | ||
| if (steps.length === 0) { | ||
| lastStep = NO_STEPS; | ||
|
|
@@ -96,6 +96,8 @@ describe('solveEquation for =', function () { | |
| ['2/(1 + 1 + 4x) = 1/3', 'x = 1'], | ||
| ['(3 + x) / (x^2 + 3) = 1', 'x = [0, 1]'], | ||
| ['6/x + 8/(2x) = 10', 'x = 1'], | ||
| // Use of nested scope (i.e., baz depends on bar) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you actually make a new test file just for testing with scope? :) and add a bunch more tests for more equations but also expressions (no equals sign) I'd also reoreder the arguments in the new test file so that the scope comes after the input and before the output (makes a bit more sense when reading it)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ALSO THIS IS REALLY COOL that you added this functionality!!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oo also could you test substeps to show how things get sub'd in one at a time? there are some tests that already test substeps you can probably base off of but let me know if you want help setting that up
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made some progress on this, but I struggled to get any of the simplify tests to pass. Decided to push my incremental changes in hopes we can work through this together. The simplify tests weren't originally designed with scope in mind, and my quick attempt to incorporate it fell short.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah I think putting the tests for this in their own completely different file would help - then we don't need to update the other tests to deal with scope and can do a different setup for these tests. Let me know if you want help going through that - I'm a lot more free this week and next than I have been recently and would love to help (sorry for just getting to this PR again now!) |
||
| ['2y = baz - x^2', 'y = 400', false, {baz: '(bar^2)', x: 10, bar: '(foo + x)', foo: 20 }] | ||
| // TODO: fix these cases, fail because lack of factoring support, for complex #s, | ||
| // for taking the sqrt of both sides, etc | ||
| // ['(x + y) (y + 2) = 0', 'y = -y'], | ||
|
|
@@ -111,7 +113,7 @@ describe('solveEquation for =', function () { | |
| // this gives us 6.3995 when it should actually be 6.4 :( | ||
| // ['x - 3.4= ( x - 2.5)/( 1.3)', 'x = 6.4'] | ||
| ]; | ||
| tests.forEach(t => testSolve(t[0], t[1], t[2])); | ||
| tests.forEach(t => testSolve(t[0], t[1], t[2], t[3])); | ||
| }); | ||
|
|
||
| describe('solveEquation for non = comparators', function() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you keep the new line at the end of the file? Github seems to like it and puts a 🚫 if you don't, so I've adopted liking it too ^_^
I see it in a few other files, so just scroll this PR and you'll see where!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hopefully caught all these in my latest PR. Will pay attention to this going forward.