MCPcopy Index your code
hub / github.com/BeardedManZhao/mathematical-expression

github.com/BeardedManZhao/mathematical-expression @1.4.0-稳定版

Chat with this repo
repository ↗ · DeepWiki ↗ · release 1.4.0-稳定版 ↗ · + Follow
195 symbols 435 edges 33 files 149 documented · 76%

Browse by type

Functions 162 Types & classes 33
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

image mathematical-expression (MAE)

🫠 Important Notice

✅【commonly】 * PS, please try to use version 1.3.1 and above as much as possible. This will help you use a more stable version and fix all known bugs in 1.2.x*

⚠️【important】 The content of versions 1.3.7 and 1.4.0 is completely consistent, with the only difference being the change in the package module. ** Please note that we will refactor the package name to io.github.beardedManZhao.mathematicalExpression in versions 1.4.0 and all subsequent versions to avoid conflicting package names in Java's various dependencies~**

To avoid any compatibility issues caused by package updates, we have provided version 1.3.7. You can continue to use the old package name, but we strongly recommend using the new version, as the new package name has been updated to io.github.beardedManZhao.mathematicalExpression. If you have any questions or suggestions about changing the package name or updating, please contact us in a timely manner!!

introduce

This framework is an effective tool for mathematical formula analysis. It can analyze mathematical formulas including nested functions, including functions, and step accumulation of series. The return value is a numerical result object. At the same time, it can also be used for comparison operations. When comparing again, the return value is a Boolean result object.

  • Maven depends on coordinates

You can directly use Maven to import this framework into the project and use it effectively


<dependencies>
    <dependency>
        <groupId>io.github.BeardedManZhao</groupId>
        <artifactId>mathematical-expression</artifactId>
        <version>1.4.0</version>
    </dependency>
</dependencies>

You can also directly load "mathematical expression" into your framework through gradle, and use the following dependencies.

dependencies {
    implementation 'io.github.BeardedManZhao:mathematical-expression:1.4.0'
}

Historical version

You can check in the https://github.com/BeardedManZhao/mathematical-expression/tree/main/update Detailed change reports for all published versions were found in.

Why choose mathematical expression

Mathematical expression has advantages such as simplicity, speed, ease of use, and support for a wide range of languages. It has almost the same API usage as the C Java Python version.

Easy to use API

Calling the library is very simple, and you can use the following code for calculation. Of course, if you don't need to check, you can also compress the calculation code below into System.out.println(Mathematical_Expression.getInstance(Mathematical_Expression.bracketsCalculation2).calculation("(1+2)*3")); Can effectively reduce code load!

import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.Calculation;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;

public class MAIN {

    public static void main(String[] args) throws WrongFormat {
        final Calculation instance = Mathematical_Expression.getInstance(
                // Select the different computing components you want to use here
                Mathematical_Expression.bracketsCalculation2
        );
        // If you ensure the correctness of the expression, you can skip checking
        instance.check("(1+2)*3");
        System.out.println(instance.calculation("(1+2)*3"));
    }
}

Superb functionality, capable of handling numerous functions

Are you unfamiliar with programming? It's simple, you can completely customize functions using mathematical expressions, and we also have many built-in functions that are unified in function.io.github.beardedManZhao.mathematicalExpression.core.calculationFunctionPackage In class!!

// Import necessary classes and packages for mathematical calculations and functions

import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.Calculation;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.FunctionPackage;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.Functions;
import io.github.beardedManZhao.mathematicalExpression.core.container.CalculationResults;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;

// Define a class 'MAIN' with a function that calculates the factorial of x plus 1
@Functions("f(x) = x! + 1")
public class MAIN {
  public static void main(String[] args) throws WrongFormat {
    // Import built-in functions like 'sum' for use
    // Register the math function library
    Mathematical_Expression.register_function(FunctionPackage.MATH);

    // Alternatively, register custom functions, e.g., a function adding two numbers
    Mathematical_Expression.register_function("fTwo(x, y) = x + y");

    // Register all annotated functions in the 'MAIN' class for use
    Mathematical_Expression.register_function(MAIN.class);

    // Initialize the calculation component
    final Calculation instance = Mathematical_Expression.getInstance(Mathematical_Expression.functionFormulaCalculation2);

    // Perform a simple check on the expression
    instance.check("1 + sum(1,2,3,4) + f(3) * fTwo(1, 2)");

    // Calculate the expression, which can include functions
    final CalculationResults calculation = instance.calculation("1 + sum(1,2,3,4) + f(3) * fTwo(1, 2)");

    // Print the result
    System.out.println(calculation.getResult());
  }
}

Not enough support for calculating symbols? Don't worry, this library supports a wide variety of operators, and you can see all the operators here!

Symbol Name Symbolic syntax (n represents operands) Supported versions Symbolic significance
Addition operator n + n 1.0.0 Add two operands
Subtraction operator n - n 1.0.0 Subtracting two operands
Multiplication operator n * n 1.0.0 Multiplying two operands
Division operator n / n 1.0.0 Dividing two operands
Remainder operator n % n 1.0.0 Perform remainder operation on two operands
Factorial operator n! 1.3.2 Performing factorial operations on operands
Power operator n ^ n 1.3.5 Exponentiation operands

You can also achieve calculation operations with precision and caching operations by adjusting settings!

import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.Calculation;
import io.github.beardedManZhao.mathematicalExpression.core.container.CalculationResults;

import java.math.BigDecimal;

/**
 * This is the main entry point for the application, demonstrating mathematical expression parsing and evaluation.
 */
public class MAIN {
    public static void main(String[] args) {
        // Obtain an instance of the calculation component, which supports parentheses handling.
        final Calculation calculationInstance = Mathematical_Expression.getInstance(Mathematical_Expression.bracketsCalculation2);
        // Define a sample mathematical expression to evaluate.
        final String inputExpression = "0.3 * 3";
        // Enable caching to improve performance.
        Mathematical_Expression.Options.setUseCache(true);

        // Enable BigDecimal for more accurate results.
        Mathematical_Expression.Options.setUseBigDecimal(true);
        // Evaluate the expression and print the result.
        System.out.println(calculationInstance.calculation(inputExpression));

        // Disable BigDecimal for faster performance.
        Mathematical_Expression.Options.setUseBigDecimal(false);
        // Evaluate the expression and print the result.
        final CalculationResults calculation = calculationInstance.calculation(inputExpression);
        System.out.println(calculation);

        // Can extract different numerical objects
        System.out.println("Can extract different numerical objects!");
        final double result = (double) calculation.getResult();
        final BigDecimal bigDecimalResult = calculation.getBigDecimalResult();
        System.out.println(result);
        System.out.println(bigDecimalResult);
    }
}

A wide variety of computing components

In mathematical expression, we provide various computing components, and you can choose different computing components according to your needs to achieve different functions while maintaining the same API calling method.

import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.Calculation;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.Functions;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;

// Prepare a mathematical function x factorial+1
@Functions("f(x) = x! + 1")
public class MAIN {
    public static void main(String[] args) throws WrongFormat {
        // Register and use the main annotated function
        Mathematical_Expression.register_function(MAIN.class);
        final Calculation instance = Mathematical_Expression.getInstance(
                // Select the function calculation component here
                Mathematical_Expression.functionFormulaCalculation2
        );
        // If you ensure the correctness of the expression, you can skip checking
        instance.check("f(1 + 2) - 3");
        System.out.println(instance.calculation("f(1 + 2) - 3"));

        /*----------------------------------*/

        // You can also use the quick calculation component to calculate the sum between intervals [1+2, 30]
        final Calculation instance1 = Mathematical_Expression.getInstance(
                // Select the quick sum calculation component here, and the API will be the same as above
                Mathematical_Expression.fastSumOfIntervalsBrackets
        );
        instance1.check("1 + 2, 30");
        System.out.println(instance1.calculation("1 + 2, 30"));
    }
}

Ultra-high flexibility

The functions required in any step of it, as well as any calculated object, can be individually obtained for the desired operation. For example, a mathematical function is a complex object, and after its compilation is successful, you can directly obtain its function object, which is not limited to the following Used in mathematical expression!

import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.Functions;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.ManyToOneNumberFunction;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;

// Prepare a mathematical function x factorial+1
@Functions("f(x) = x! + 1")
public class MAIN {
    public static void main(String[] args) throws WrongFormat {
        // Register and use the main annotated function
        Mathematical_Expression.register_function(MAIN.class);
        // Extract f (x)=x+ We know that the name of the function object of 1 is f
        final ManyToOneNumberFunction f = Mathematical_Expression.getFunction("f");
        // Calculate using f alone
        final double run = f.run(3);
        System.out.println(run);
    }
}

Detailed execution records

In some calculation components, you can use the 'explain' function to calculate expressions. This function can fully plot the calculation process of the calculation component as a log result object, which can be plotted as a graph. The following are the supported components and usage examples.

Calculation component name Does it support explain When did support start Related knowledge
io.github.beardedManZhao.mathematicalExpression.core.calculation.PrefixExpressionOperation yes v1.3.5 click this
io.github.beardedManZhao.mathematicalExpression.core.calculation.BracketsCalculation2 yes v1.3.5 click this
io.github.beardedManZhao.mathematicalExpression.core.calculation.CumulativeCalculation yes v1.3.6 click this

Introducing a flowchart code generation library

You only need to import the dependency coordinates below to automatically import the relevant components. This library will help you draw a flowchart of the calculation process of com

Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

Shape

Method 162
Class 27
Interface 4
Enum 2

Languages

Java100%

Modules by API surface

src/main/java/io/github/beardedManZhao/mathematicalExpression/core/Mathematical_Expression.java14 symbols
src/main/java/io/github/beardedManZhao/mathematicalExpression/core/calculation/number/FunctionFormulaCalculation2.java11 symbols
src/main/java/io/github/beardedManZhao/mathematicalExpression/core/manager/CalculationManagement.java10 symbols
src/main/java/io/github/beardedManZhao/mathematicalExpression/core/container/CalculationBooleanResults.java10 symbols
src/main/java/io/github/beardedManZhao/mathematicalExpression/core/calculation/number/FastSumOfIntervalsBrackets.java10 symbols
src/main/java/io/github/beardedManZhao/mathematicalExpression/utils/StrUtils.java9 symbols
src/main/java/io/github/beardedManZhao/mathematicalExpression/core/container/CalculationNumberResults.java9 symbols
src/main/java/io/github/beardedManZhao/mathematicalExpression/core/calculation/number/PrefixExpressionOperation.java9 symbols
src/main/java/io/github/beardedManZhao/mathematicalExpression/core/calculation/function/ExpressionFunction.java9 symbols
src/main/java/io/github/beardedManZhao/mathematicalExpression/utils/NumberUtils.java8 symbols
src/main/java/io/github/beardedManZhao/mathematicalExpression/core/container/LogResults.java7 symbols
src/main/java/io/github/beardedManZhao/mathematicalExpression/core/calculation/number/CumulativeCalculation.java7 symbols

For agents

$ claude mcp add mathematical-expression \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page