From 7113a122572809416d18e267115b655a6c49ae9d Mon Sep 17 00:00:00 2001 From: Nitin Date: Sun, 26 Feb 2017 00:40:12 +0530 Subject: [PATCH 1/5] readme changed --- README.md | 89 ++++++++++++++++++++++++------------------------------- 1 file changed, 38 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 4e4af18c..7335388c 100644 --- a/README.md +++ b/README.md @@ -1,74 +1,61 @@ -## A step by step solver for math +# Mathsteps +[![Join the chat at https://gitter.im/mathsteps-chat/Lobby](https://badges.gitter.im/mathsteps-chat/Lobby.svg)](https://gitter.im/mathsteps-chat/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Build Status](https://travis-ci.org/socraticorg/mathsteps.svg?branch=master)](https://travis-ci.org/socraticorg/mathsteps) -[![Join the chat at https://gitter.im/mathsteps-chat/Lobby](https://badges.gitter.im/mathsteps-chat/Lobby.svg)](https://gitter.im/mathsteps-chat/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -[![Build Status](https://travis-ci.org/socraticorg/mathsteps.svg?branch=master)](https://travis-ci.org/socraticorg/mathsteps) +Mathsteps is a step-by-step math solver. It helps in solving equations, simplifying an expression and factoring a polynomial. (more to come 😄) -https://www.youtube.com/watch?v=iCrargw1rrM +**Solve an equation** -## Requirements - -Mathsteps requires Node version > 6.0.0 - -## Usage +```javascript +const steps = mathsteps.solveEquation('2x + 3x = 35'); +``` -To install mathsteps using npm: +**See all the change types** +```javascript +const changes = mathsteps.ChangeTypes; +``` +Learn [how to use mathsteps](https://github.com/socraticorg/mathsteps/wiki/How-to-use-mathsteps) and [how the codebase works](https://github.com/socraticorg/mathsteps/wiki/Mathsteps-organization). - npm install mathsteps +## Examples +Here is an example to get you started. -```js +```javascript const mathsteps = require('mathsteps'); const steps = mathsteps.simplifyExpression('2x + 2x + x + x'); steps.forEach(step => { - console.log("before change: " + step.oldNode); // before change: 2 x + 2 x + x + x - console.log("change: " + step.changeType); // change: ADD_POLYNOMIAL_TERMS - console.log("after change: " + step.newNode); // after change: 6 x - console.log("# of substeps: " + step.substeps.length); // # of substeps: 3 + console.log("before change: " + step.oldNode); + console.log("change: " + step.changeType); + console.log("after change: " + step.newNode); + console.log("no of substeps: " + step.substeps.length); }); -``` - -To solve an equation: -```js -const steps = mathsteps.solveEquation('2x + 3x = 35'); -steps.forEach(step => { - console.log("before change: " + step.oldEquation.print()); // e.g. before change: 2x + 3x = 35 - console.log("change: " + step.changeType); // e.g. change: SIMPLIFY_LEFT_SIDE - console.log("after change: " + step.newEquation.print()); // e.g. after change: 5x = 35 - console.log("# of substeps: " + step.substeps.length); // e.g. # of substeps: 2 -}); +/* +before change: 2 x + 2 x + x + x +change: ADD_POLYNOMIAL_TERMS +after change: 6 x +no of substeps: 3 +*/ ``` +In this example we have an equation `2x + 2x + x + x` that is being simplified by the method `simplifyExpression`. It returns the output which tells about the equation before and after applying the change, what change was applied and the no of substeps. -To see all the change types: -```js -const changes = mathsteps.ChangeTypes; +## Installation +Mathsteps is available as the `mathsteps` package on [npm](https://www.npmjs.com/package/mathsteps). +``` +npm install mathsteps --save ``` - - ## Contributing +The main objective of the mathsteps is to continue to evolve and provide more operations and features. Read below to learn how can you contribute to the project. -Hi! If you're interested in working on this, that would be super awesome! -Learn more here: [CONTRIBUTING.md](CONTRIBUTING.md). - -## Build - -First clone the project from github: - - git clone https://github.com/socraticorg/mathsteps.git - cd mathsteps - -Install the project dependencies: - - npm install +**Contributing guide** +Read our [contributing guide](CONTRIBUTING.md) to suggest more features, report bugs or contribute code. ## Test +``` +npm test +``` -To execute tests for the library, install the project dependencies once: - - npm install - -Then, the tests can be executed: +## License +Apache-2.0 - npm test From f5ec0d63224beb0fbee86bbbb9b2b10a5c6b9561 Mon Sep 17 00:00:00 2001 From: Nitin Tulswani Date: Sun, 26 Feb 2017 00:44:05 +0530 Subject: [PATCH 2/5] breaks --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7335388c..d42d1dc5 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ npm install mathsteps --save The main objective of the mathsteps is to continue to evolve and provide more operations and features. Read below to learn how can you contribute to the project. **Contributing guide** + Read our [contributing guide](CONTRIBUTING.md) to suggest more features, report bugs or contribute code. ## Test From d9f4271be32d38952ec4f0227434951f742837d7 Mon Sep 17 00:00:00 2001 From: Nitin Date: Thu, 13 Apr 2017 22:33:52 +0530 Subject: [PATCH 3/5] flow --- .flowconfig | 9 +++++++++ README.md | 1 + lib/node/Creator.js | 22 +++++++++++++++------- lib/node/Type.js | 20 +++++++++++--------- package.json | 2 ++ 5 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 .flowconfig diff --git a/.flowconfig b/.flowconfig new file mode 100644 index 00000000..f58eb0d3 --- /dev/null +++ b/.flowconfig @@ -0,0 +1,9 @@ +[ignore] +.*/test/.* +.*/scripts/.* + +[include] + +[libs] + +[options] diff --git a/README.md b/README.md index 7335388c..d42d1dc5 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ npm install mathsteps --save The main objective of the mathsteps is to continue to evolve and provide more operations and features. Read below to learn how can you contribute to the project. **Contributing guide** + Read our [contributing guide](CONTRIBUTING.md) to suggest more features, report bugs or contribute code. ## Test diff --git a/lib/node/Creator.js b/lib/node/Creator.js index 165a6863..86c76f93 100644 --- a/lib/node/Creator.js +++ b/lib/node/Creator.js @@ -1,14 +1,22 @@ +// @flow + /* Functions to generate any mathJS node supported by the stepper see http://mathjs.org/docs/expressions/expression_trees.html#nodes for more information on nodes in mathJS */ +type value = string | number; + +type baseNode = { + [property: string]: value +} + const math = require('mathjs'); const NodeType = require('./Type'); const NodeCreator = { - operator (op, args, implicit=false) { + operator (op: string, args: Array, implicit: boolean=false) { switch (op) { case '+': return new math.expression.node.OperatorNode('+', 'add', args); @@ -28,27 +36,27 @@ const NodeCreator = { // In almost all cases, use Negative.negate (with naive = true) to add a // unary minus to your node, rather than calling this constructor directly - unaryMinus (content) { + unaryMinus (content: Array) { return new math.expression.node.OperatorNode( '-', 'unaryMinus', [content]); }, - constant (val) { + constant (val: number) { return new math.expression.node.ConstantNode(val); }, - symbol (name) { + symbol (name: string) { return new math.expression.node.SymbolNode(name); }, - parenthesis (content) { + parenthesis (content: value) { return new math.expression.node.ParenthesisNode(content); }, // exponent might be null, which means there's no exponent node. // similarly, coefficient might be null, which means there's no coefficient // the symbol node can never be null. - polynomialTerm (symbol, exponent, coeff, explicitCoeff=false) { + polynomialTerm (symbol: baseNode, exponent: baseNode, coeff: Object, explicitCoeff: boolean=false) { let polyTerm = symbol; if (exponent) { polyTerm = this.operator('^', [polyTerm, exponent]); @@ -68,7 +76,7 @@ const NodeCreator = { }, // Given a root value and a radicand (what is under the radical) - nthRoot (radicandNode, rootNode) { + nthRoot (radicandNode: baseNode, rootNode: baseNode) { const symbol = NodeCreator.symbol('nthRoot'); return new math.expression.node.FunctionNode(symbol, [radicandNode, rootNode]); } diff --git a/lib/node/Type.js b/lib/node/Type.js index 97f7406e..4717c9a2 100644 --- a/lib/node/Type.js +++ b/lib/node/Type.js @@ -1,25 +1,27 @@ +// @flow + /* For determining the type of a mathJS node. */ const NodeType = {}; -NodeType.isOperator = function(node, operator=null) { +NodeType.isOperator = function(node: Object, operator: string | null) { return node.type === 'OperatorNode' && node.fn !== 'unaryMinus' && '*+-/^'.includes(node.op) && (operator ? node.op === operator : true); }; -NodeType.isParenthesis = function(node) { +NodeType.isParenthesis = function(node: Object) { return node.type === 'ParenthesisNode'; }; -NodeType.isUnaryMinus = function(node) { +NodeType.isUnaryMinus = function(node: Object) { return node.type === 'OperatorNode' && node.fn === 'unaryMinus'; }; -NodeType.isFunction = function(node, functionName=null) { +NodeType.isFunction = function(node: Object, functionName: string | null) { if (node.type !== 'FunctionNode') { return false; } @@ -29,7 +31,7 @@ NodeType.isFunction = function(node, functionName=null) { return true; }; -NodeType.isSymbol = function(node, allowUnaryMinus=true) { +NodeType.isSymbol = function(node: Object, allowUnaryMinus: boolean | true) { if (node.type === 'SymbolNode') { return true; } @@ -41,7 +43,7 @@ NodeType.isSymbol = function(node, allowUnaryMinus=true) { } }; -NodeType.isConstant = function(node, allowUnaryMinus=false) { +NodeType.isConstant = function(node: Object, allowUnaryMinus: boolean=false) { if (node.type === 'ConstantNode') { return true; } @@ -59,7 +61,7 @@ NodeType.isConstant = function(node, allowUnaryMinus=false) { } }; -NodeType.isConstantFraction = function(node, allowUnaryMinus=false) { +NodeType.isConstantFraction = function(node: Object, allowUnaryMinus:boolean=false) { if (NodeType.isOperator(node, '/')) { return node.args.every(n => NodeType.isConstant(n, allowUnaryMinus)); } @@ -68,7 +70,7 @@ NodeType.isConstantFraction = function(node, allowUnaryMinus=false) { } }; -NodeType.isConstantOrConstantFraction = function(node, allowUnaryMinus=false) { +NodeType.isConstantOrConstantFraction = function(node: Object, allowUnaryMinus:boolean=false) { if (NodeType.isConstant(node, allowUnaryMinus) || NodeType.isConstantFraction(node, allowUnaryMinus)) { return true; @@ -78,7 +80,7 @@ NodeType.isConstantOrConstantFraction = function(node, allowUnaryMinus=false) { } }; -NodeType.isIntegerFraction = function(node, allowUnaryMinus=false) { +NodeType.isIntegerFraction = function(node: Object, allowUnaryMinus:boolean=false) { if (!NodeType.isConstantFraction(node, allowUnaryMinus)) { return false; } diff --git a/package.json b/package.json index 4bd61c77..e84d71b5 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,12 @@ "eslint": "^3.10.2", "eslint-config-google": "^0.7.0", "eslint-plugin-sort-requires": "^2.1.0", + "flow-bin": "^0.43.1", "mocha": "2.4.5" }, "scripts": { "lint": "node_modules/.bin/eslint .", + "flow": "flow", "test": "node_modules/.bin/mocha --recursive", "setup-hooks": "ln -s ../../scripts/git-hooks/pre-commit.sh .git/hooks/pre-commit" }, From b417d6c222d251fb0fc8a46d4c09dffb557e7871 Mon Sep 17 00:00:00 2001 From: Nitin Date: Thu, 13 Apr 2017 22:38:03 +0530 Subject: [PATCH 4/5] flow --- lib/node/Creator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node/Creator.js b/lib/node/Creator.js index 86c76f93..c6878ccc 100644 --- a/lib/node/Creator.js +++ b/lib/node/Creator.js @@ -49,7 +49,7 @@ const NodeCreator = { return new math.expression.node.SymbolNode(name); }, - parenthesis (content: value) { + parenthesis (content: baseNode) { return new math.expression.node.ParenthesisNode(content); }, From 353c2526006be309ca35fbe25415ec06687382b9 Mon Sep 17 00:00:00 2001 From: Nitin Date: Thu, 13 Apr 2017 22:44:20 +0530 Subject: [PATCH 5/5] removed flow code from Type.js --- lib/node/Type.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/node/Type.js b/lib/node/Type.js index 4717c9a2..97f7406e 100644 --- a/lib/node/Type.js +++ b/lib/node/Type.js @@ -1,27 +1,25 @@ -// @flow - /* For determining the type of a mathJS node. */ const NodeType = {}; -NodeType.isOperator = function(node: Object, operator: string | null) { +NodeType.isOperator = function(node, operator=null) { return node.type === 'OperatorNode' && node.fn !== 'unaryMinus' && '*+-/^'.includes(node.op) && (operator ? node.op === operator : true); }; -NodeType.isParenthesis = function(node: Object) { +NodeType.isParenthesis = function(node) { return node.type === 'ParenthesisNode'; }; -NodeType.isUnaryMinus = function(node: Object) { +NodeType.isUnaryMinus = function(node) { return node.type === 'OperatorNode' && node.fn === 'unaryMinus'; }; -NodeType.isFunction = function(node: Object, functionName: string | null) { +NodeType.isFunction = function(node, functionName=null) { if (node.type !== 'FunctionNode') { return false; } @@ -31,7 +29,7 @@ NodeType.isFunction = function(node: Object, functionName: string | null) { return true; }; -NodeType.isSymbol = function(node: Object, allowUnaryMinus: boolean | true) { +NodeType.isSymbol = function(node, allowUnaryMinus=true) { if (node.type === 'SymbolNode') { return true; } @@ -43,7 +41,7 @@ NodeType.isSymbol = function(node: Object, allowUnaryMinus: boolean | true) { } }; -NodeType.isConstant = function(node: Object, allowUnaryMinus: boolean=false) { +NodeType.isConstant = function(node, allowUnaryMinus=false) { if (node.type === 'ConstantNode') { return true; } @@ -61,7 +59,7 @@ NodeType.isConstant = function(node: Object, allowUnaryMinus: boolean=false) { } }; -NodeType.isConstantFraction = function(node: Object, allowUnaryMinus:boolean=false) { +NodeType.isConstantFraction = function(node, allowUnaryMinus=false) { if (NodeType.isOperator(node, '/')) { return node.args.every(n => NodeType.isConstant(n, allowUnaryMinus)); } @@ -70,7 +68,7 @@ NodeType.isConstantFraction = function(node: Object, allowUnaryMinus:boolean=fal } }; -NodeType.isConstantOrConstantFraction = function(node: Object, allowUnaryMinus:boolean=false) { +NodeType.isConstantOrConstantFraction = function(node, allowUnaryMinus=false) { if (NodeType.isConstant(node, allowUnaryMinus) || NodeType.isConstantFraction(node, allowUnaryMinus)) { return true; @@ -80,7 +78,7 @@ NodeType.isConstantOrConstantFraction = function(node: Object, allowUnaryMinus:b } }; -NodeType.isIntegerFraction = function(node: Object, allowUnaryMinus:boolean=false) { +NodeType.isIntegerFraction = function(node, allowUnaryMinus=false) { if (!NodeType.isConstantFraction(node, allowUnaryMinus)) { return false; }