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
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Utilisez IntelliSense pour en savoir plus sur les attributs possibles.
// Pointez pour afficher la description des attributs existants.
// Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
}
]
}
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ const ChangeTypes = require('./lib/ChangeTypes');
const factor = require('./lib/factor');
const simplifyExpression = require('./lib/simplifyExpression');
const solveEquation = require('./lib/solveEquation');
const printMS = require('./lib/util/print');
const Node = require('./lib/node');
const Negative = require('./lib/Negative')
const flatten = require('./lib/util/flattenOperands');

module.exports = {
factor,
simplifyExpression,
solveEquation,
Node,
Negative,
printMS,
flatten,
ChangeTypes,
};
22 changes: 11 additions & 11 deletions lib/node/Creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ const NodeCreator = {
operator (op, args, implicit=false) {
switch (op) {
case '+':
return new math.expression.node.OperatorNode('+', 'add', args);
return new math.OperatorNode('+', 'add', args);
case '-':
return new math.expression.node.OperatorNode('-', 'subtract', args);
return new math.OperatorNode('-', 'subtract', args);
case '/':
return new math.expression.node.OperatorNode('/', 'divide', args);
return new math.OperatorNode('/', 'divide', args);
case '*':
return new math.expression.node.OperatorNode(
return new math.OperatorNode(
'*', 'multiply', args, implicit);
case '^':
return new math.expression.node.OperatorNode('^', 'pow', args);
return new math.OperatorNode('^', 'pow', args);
default:
throw Error('Unsupported operation: ' + op);
}
Expand All @@ -29,24 +29,24 @@ 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) {
return new math.expression.node.OperatorNode(
return new math.OperatorNode(
'-', 'unaryMinus', [content]);
},

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

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

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

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

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

Expand Down
48 changes: 44 additions & 4 deletions lib/simplifyExpression/distributeSearch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,58 @@ function expandBase (node) {
return Node.Status.noChange(node);
}

if (!Node.Type.isFunction(base, 'nthRoot') && !Node.Type.isOperator(base, '+')) {
if (!Node.Type.isFunction(base, 'nthRoot') && !(Node.Type.isOperator(base, '+') || Node.Type.isOperator(base, '*') || Node.Type.isOperator(base, '/'))) {
return Node.Status.noChange(node);
}

// If the base is an nthRoot node, it doesn't need the parenthesis
const expandedBase = Node.Type.isFunction(base, 'nthRoot')
? base
: node.args[0];
let expandedNode
const substeps = [];
let status;

const expandedNode = Node.Creator.operator('*', Array(parseFloat(exponent.value)).fill(expandedBase));

return Node.Status.nodeChanged(
ChangeTypes.EXPAND_EXPONENT, node, expandedNode, false);
if (Node.Type.isFunction(base, 'nthRoot')) {
expandedNode = Node.Creator.operator('*', Array(parseFloat(exponent.value)).fill(expandedBase));
return Node.Status.nodeChanged(
ChangeTypes.EXPAND_EXPONENT, node, expandedNode, false);
} else if (Node.Type.isOperator(base, '+')) {
// fpiou (modification du code pour permettre le développement (a+b)^2 = a^2+2*a*b+b^2))
acarre = Node.Creator.operator('^',[base.args[0],exponent])
bcarre = Node.Creator.operator('^',[base.args[1],exponent])
doubleproduit = Node.Creator.operator('*',[ Node.Creator.constant(2),Node.Creator.operator('*',base.args)])
expandedNode = Node.Creator.operator('+',[acarre,doubleproduit,bcarre])
status = Node.Status.nodeChanged(
ChangeTypes.EXPAND_EXPONENT, node, expandedNode, false)
substeps.push(status);
newNode = Node.Status.resetChangeGroups(status.newNode);
const childStatus = simplify(newNode);
if (childStatus.hasChanged()) {
status = Node.Status.childChanged(newNode, childStatus);
substeps.push(status);
newNode = Node.Status.resetChangeGroups(status.newNode);
}
if (substeps.length === 1) {
return substeps[0];
}
return Node.Status.nodeChanged(
ChangeTypes.DISTRIBUTE, node, newNode, false, substeps);
} else if (Node.Type.isOperator(base, '*')) {
// fpiou (modification du code pour permettre le "développement" (a*b)^2 = a^2*b^2))
for (let i=0;i<base.args.length;i++) {
base.args[i] = Node.Creator.operator('^',[base.args[i],exponent])
}
return Node.Status.nodeChanged(
ChangeTypes.EXPAND_EXPONENT, node, base, false);
} else if (Node.Type.isOperator(base, '/')) {
for (let i=0;i<2;i++) {
base.args[i] = Node.Creator.operator('^',[base.args[i],exponent])
}
return Node.Status.nodeChanged(
ChangeTypes.EXPAND_EXPONENT, node, base, false);
}
}

// Distributes unary minus into a parenthesis node.
Expand Down Expand Up @@ -297,6 +336,7 @@ function simplify(node) {
rearrangeCoefficient, // e.g. x*5 -> 5x
collectAndCombineSearch, // e.g 2x*4x -> 8x^2
distributeAndSimplifyMultiplication, // e.g. (2+x)(3+x) -> 2*(3+x) recurses
expandBase
];

let newNode = node.cloneDeep();
Expand Down
1 change: 1 addition & 0 deletions lib/solveEquation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const math = require('mathjs');
const stepThrough = require('./stepThrough');

function solveEquationString(equationString, debug=false) {
console.log('solveEquationString', equationString);
const comparators = ['<=', '>=', '=', '<', '>'];

for (let i = 0; i < comparators.length; i++) {
Expand Down
3 changes: 3 additions & 0 deletions lib/solveEquation/stepThrough.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ function stepThrough(leftNode, rightNode, comparator, debug=false) {

// Step through the math equation until nothing changes
do {
console.log('steps', steps);
steps = addSimplificationSteps(steps, equation, debug);


console.log('last new ascii', steps[steps.length - 1]?.newEquation?.ascii());
if (steps.length > 0) {
const lastStep = steps[steps.length - 1];
equation = Equation.createEquationFromString(
Expand Down
2 changes: 1 addition & 1 deletion lib/util/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

function evaluate(node) {
// TODO: once we swap in math-parser, call its evaluate function instead
return node.eval();
return node.evaluate();
}

module.exports = evaluate;
Loading