mathsteps

repository·master·Indexed 24 days ago

https://github.com/google/mathsteps

A 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.

Tokens
2K
Snippets
4
Records
17
Agent score
80%

What's inside mathsteps

  1. Solve equations with solveEquation()

    master

    Use 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
    });
  2. Simplify mathematical expressions with simplifyExpression()

    master

    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
    });
  3. Simplify a math expression string with simplifyExpressionString()

    master

    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.
  4. Format an Equation as ASCII or LaTeX

    master

    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.
  5. Use the PolynomialTerm class to represent polynomial mathematical nodes

    master

    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:

    • A node is considered a PolynomialTerm if its base node is a symbol (checked via NodeType.isSymbol).
    • Examples of polynomial terms: x^2, 2y, z, 3x/5.
    • Examples of non-polynomial terms: 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.
  6. Identify nth root terms with NthRootTerm.isNthRootTerm

    master

    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.
  7. Access the mathsteps core mathematical types and interfaces

    master
    The 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.