-
Notifications
You must be signed in to change notification settings - Fork 277
How to use mathsteps
To import mathsteps:
const mathsteps = require('mathsteps');
mathsteps provides step-by-step results for a few types of math problems (with more to come):
- simplifying an expression e.g.
steps = mathsteps.simplifyExpression('2x + 2x + x + x');
- solving an equation e.g.
steps = mathsteps.solveEquation('2x + 3x = 35');
- factoring a polynomial e.g.
steps = mathsteps.factorPolynomial('x^2 + 2x + 1')
simplifyExpression
and factorPolynomial
return a list of NodeStatus
objects.
solveEquation
returns a list of EquationStatus
objects.
Let's define some of the relevant objects that mathsteps returns.
A mathsteps Node (actually a mathjs node) is a mathematical expression stored as an Abstract Syntax Tree (AST). The node is a reference to the head of this tree.
e.g. 2 + 3 is stored as the following tree:
node -> +
/ \
2 3
A NodeStatus
is an object representing a single math step involving a node. A change made to an expression. Each NodeStatus stores the following:
- oldNode - The 'before` expression
- newNode - The 'after' expression
- changeType - The type of change that happened in this step. A full list of change types can be found in
mathsteps.ChangeTypes
- subSteps - A list of NodeStatuses representing sub-steps of this step. For example, ADD_FRACTIONS is a step that actually groups the steps for the changes involved in adding two fractions together, including getting a common denominator and adding the numerators together.
It's also worth noting that the the oldNode
and newNode
might have a changeGroup
value associated with some nodes in their trees. The changeGroup flags what part of the expression was affected by the change. When multiple parts of the expression are affected by a step but in different ways, for example identifying like terms, they will have different values for the changeGroup.
This allows for color-coding changes.
A mathsteps Equation is an object that defines a comparative between two mathsteps Nodes. As such an equation defines:
- leftNode - the left side of the equation
- rightNode - the right side of the equation
- comparator - how the sides are compares: =, >, >=, <, <=
An EquationStatus
is similar to a NodeStatus
but deals with Equation objects. Each EquationStatus stores:
- oldEquation - The 'before` equation
- newEquation - The 'after' equation
- changeType - The type of change that happened in this step. A full list of change types can be found in
mathsteps.ChangeTypes
- subSteps - A list of EquationStatuses representing sub-steps of this step. For example, SIMPLIFY_LEFT_SIDE is a step that groups the steps for simplifying the left side of an equation, and has substeps detailing each change.