Install mathsteps via npm
masterInstall the mathsteps package using npm to use it in your Node.js projects.
npm install mathstepsrepository·master·Indexed 24 days ago
https://github.com/google/mathstepsA step-by-step mathematical solver that provides intermediate transformations for simplifying expressions, solving equations, and factoring. It includes a core API with functions like simplifyExpression(), solveEquation(), and factor(), as well as utilities for representing equations and mathematical nodes such as PolynomialTerm and NthRootTerm. The library supports ASCII and LaTeX formatting for equations and integrates with mathjs for parsing.
Install the mathsteps package using npm to use it in your Node.js projects.
npm install mathstepsUse mathsteps.solveEquation(equation) to get a sequence of steps to solve a mathematical equation. Each step contains:
oldEquation: The equation before the change. Use .ascii() to get a string representation.changeType: The type of transformation applied.newEquation: The equation after the change. Use .ascii() to get a string representation.substeps: An array of substeps involved in the transformation.Note: If you are using mathsteps version 0.1.6 or lower, use .print() instead of .ascii() to get the string representation of the equation.
const steps = mathsteps.solveEquation('2x + 3x = 35');
steps.forEach(step => {
console.log("before change: " + step.oldEquation.ascii()); // e.g. before change: 2x + 3x = 35
console.log("change: " + step.changeType); // e.g. change: SIMPLIFY_LEFT_SIDE
console.log("after change: " + step.newEquation.ascii()); // e.g. after change: 5x = 35
console.log("# of substeps: " + step.substeps.length); // e.g. # of substeps: 2
});Use mathsteps.simplifyExpression(expression) to get a sequence of steps that simplify a given mathematical expression. Each step in the returned array contains:
oldNode: The state of the expression before the change.changeType: The type of transformation applied.newNode: The state of the expression after the change.substeps: An array of substeps involved in the transformation.const mathsteps = require('mathsteps');
const steps = mathsteps.simplifyExpression('2x + 2x + x + x');
steps.forEach(step => {
console.log("before change: " + step.oldNode.toString()); // before change: 2 x + 2 x + x + x
console.log("change: " + step.changeType); // change: ADD_POLYNOMIAL_TERMS
console.log("after change: " + step.newNode.toString()); // after change: 6 x
console.log("# of substeps: " + step.substeps.length); // # of substeps: 3
});You can inspect all possible transformation types used by the solver by accessing the mathsteps.ChangeTypes object.
const changes = mathsteps.ChangeTypes;The simplifyExpressionString function takes a mathematical expression as a string and returns an array of steps representing the simplification process. It uses mathjs to parse the input. If the expression is invalid or cannot be parsed, it returns an empty array [].
Parameters:
expressionString (String): The mathematical expression to simplify.debug (Boolean, optional): If set to true, enables debug mode during the step-through process. Defaults to false.The Equation class provides methods to convert the mathematical expression into human-readable string formats.
ascii(showPlusMinus=false): Returns an ASCII representation of the equation. Use showPlusMinus: true to control the display of plus/minus signs.latex(showPlusMinus=false): Returns a LaTeX formatted string of the equation. Use showPlusMinus: true to control the display of plus/minus signs.factor() to find the factors of a given mathematical expression. This function is part of the core mathsteps API.The PolynomialTerm class is a subclass of Term used to represent polynomial terms where the base node is a symbol (e.g., x^2, 2y, z).
Key characteristics:
PolynomialTerm if its base node is a symbol (checked via NodeType.isSymbol).x^2, 2y, z, 3x/5.4 (constant), 2+x (addition), 3*7 (multiplication of constants), x-z (subtraction).Methods:
getSymbolNode(): Returns the base node of the term.getSymbolName(): Returns the name of the base symbol.PolynomialTerm.isPolynomialTerm(node, [onlyImplicitMultiplication]): A static method that returns true if the provided node is a polynomial term.simplifyExpression() to reduce a mathematical expression to its simplest form. This function is part of the core mathsteps API.Use NthRootTerm.isNthRootTerm(node, onlyImplicitMultiplication) to determine if a mathematical node represents an nth root term.
An nth root term is defined as a term where the base node is an nthRoot function. For example:
nthRoot(x^2) is an nth root term.4*nthRoot(10)^2 is an nth root term.4x^2 is not an nth root term (the base is x, not an nthRoot function).Parameters:
node: The mathematical node to check.onlyImplicitMultiplication (optional): A boolean flag to control how implicit multiplication is handled during the check.mathsteps package exports several core classes and types used to represent mathematical expressions and their states. These include term representations (like PolynomialTerm, NthRootTerm, and MixedNumber), the base Term class, and utility classes like Creator, CustomType, Type, and Status.clone() method creates a deep copy of an Equation instance. It ensures that the leftNode and rightNode are cloned using their own cloneDeep() methods, preventing side effects when modifying the nodes of the new equation.