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

trying to update to 9, but there are a lot of complications #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ npm test
## TODOs
- Fix usage of factoring for simplification
- Throw error instead of returning empty array?
- update to latest mathjs

### Done (compared to https://github.com/google/mathsteps)
- Port to TypeScript!
Expand Down
7 changes: 7 additions & 0 deletions example-consumer/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import {solveEquation} from '@taskbase/mathsteps';
import {simplifyExpression} from '@taskbase/mathsteps';

const solved = solveEquation('2x + 2x = 2');
console.log('solved:');
console.log(solved);

const simplified = simplifyExpression('2x + 2x');
console.log('simplided:');
console.log(simplified);
68 changes: 40 additions & 28 deletions lib/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
},
"homepage": "https://github.com/taskbase/mathsteps#readme",
"dependencies": {
"mathjs": "3.11.2"
"mathjs": "^9.2.0"
}
}
7 changes: 7 additions & 0 deletions lib/src/equation/Equation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ export class Equation {

// Prints an Equation properly using the print module
ascii(showPlusMinus = false) {
console.log(`============`);
console.log(JSON.stringify(this.leftNode));
console.log(JSON.stringify(this.rightNode));
console.log(`============`);

const leftSide = printAscii(this.leftNode, showPlusMinus);
const rightSide = printAscii(this.rightNode, showPlusMinus);

const comparator = this.comparator;

return `${leftSide} ${comparator} ${rightSide}`;
Expand Down Expand Up @@ -45,6 +51,7 @@ export class Equation {
"Expected two sides of an equation using comparator: " + comparator
);
}

const leftNode = math.parse(sides[0]);
const rightNode = math.parse(sides[1]);

Expand Down
40 changes: 11 additions & 29 deletions lib/src/node/Creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,15 @@ class NodeCreatorClass {
operator(op, args, implicit = false) {
switch (op) {
case "+":
return new (math.expression as any).node.OperatorNode("+", "add", args);
return new (math as any).OperatorNode("+", "add", args);
case "-":
return new (math.expression as any).node.OperatorNode(
"-",
"subtract",
args
);
return new (math as any).OperatorNode("-", "subtract", args);
case "/":
return new (math.expression as any).node.OperatorNode(
"/",
"divide",
args
);
return new (math as any).OperatorNode("/", "divide", args);
case "*":
return new (math.expression as any).node.OperatorNode(
"*",
"multiply",
args,
implicit
);
return new (math as any).OperatorNode("*", "multiply", args, implicit);
case "^":
return new (math.expression as any).node.OperatorNode("^", "pow", args);
return new (math as any).OperatorNode("^", "pow", args);
default:
throw Error("Unsupported operation: " + op);
}
Expand All @@ -41,25 +28,23 @@ class NodeCreatorClass {
// 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) {
return new (math.expression as any).node.OperatorNode("-", "unaryMinus", [
content,
]);
return new (math as any).OperatorNode("-", "unaryMinus", [content]);
}

constant(val) {
return new (math.expression as any).node.ConstantNode(val);
return new (math as any).ConstantNode(val);
}

symbol(name) {
return new (math.expression as any).node.SymbolNode(name);
return new (math as any).SymbolNode(name);
}

parenthesis(content) {
return new (math.expression as any).node.ParenthesisNode(content);
return new (math as any).ParenthesisNode(content);
}

list(content) {
return new (math.expression as any).node.ArrayNode(content);
return new (math as any).ArrayNode(content);
}

// exponent might be null, which means there's no exponent node.
Expand Down Expand Up @@ -92,10 +77,7 @@ class NodeCreatorClass {
// Given a root value and a radicand (what is under the radical)
nthRoot(radicandNode, rootNode) {
const symbol = NodeCreator.symbol("nthRoot");
return new (math.expression as any).node.FunctionNode(symbol, [
radicandNode,
rootNode,
]);
return new (math as any).FunctionNode(symbol, [radicandNode, rootNode]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/util/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
* */
export function evaluate(node) {
// TODO: once we swap in math-parser, call its evaluate function instead
return node.eval();
return node.evaluate();
}
4 changes: 4 additions & 0 deletions lib/src/util/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export function printTreeTraversal(node, parentNode = undefined) {
}

if (NodeType.isOperator(node)) {
if (node.op === "*" && node.args[0].value === 0) {
return `0`;
}

if (node.op === "/" && NodeType.isOperator(node.args[1])) {
return `${printTreeTraversal(node.args[0])} / (${printTreeTraversal(
node.args[1]
Expand Down
3 changes: 2 additions & 1 deletion lib/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"module": "commonjs",
"target": "es2015",
"declaration": true,
"outDir": "../dist"
"outDir": "../dist",
"sourceMap": true
},
"include": [
"./**/*.ts"
Expand Down
Loading