MCPcopy Index your code
hub / github.com/Aghajari/MathParser

github.com/Aghajari/MathParser @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
207 symbols 412 edges 17 files 55 documented · 27%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

MathParser

MathParser is a simple but powerful open-source math tool that parses and evaluates algebraic expressions written in pure java.

This project is designed for University first project of Advanced-Programming at the Department of Computer Engineering, Amirkabir University of Technology.

Syntax

  1. Create an instance of MathParser
MathParser parser = MathParser.create();
  1. Parse an expression
System.out.println(parser.parse("2 + 2"));                       // 4.0
System.out.println(parser.parse("5^2 * (2 + 3 * 4) + 5!/4"));    // 380.0

Add Expression (Function or Variable)

parser.addExpression("f(x, y) = 2(x + y)");                      // addFunction
parser.addExpression("x0 = 1 + 2 ^ 2");                          // addVariable
parser.addExpression("y0 = 2x0");                                // addVariable
System.out.println(parser.parse("1 + 2f(x0, y0)/3"));            // 21.0

Built-in functions

MathParser identifies all static methods in Math and Built-in Functions. Functions and Variables are case-insensitive.

System.out.println(parser.parse("sin(3pi/2) + tan(45°)"));

Built-in Functions includes integral, derivative, limit and sigma

System.out.println(parser.parse("2 ∫(x, (x^3)/(x+1), 5, 10)"));  // 517.121062
System.out.println(parser.parse("derivative(x, x^3, 2)"));       // 12.0
System.out.println(parser.parse("lim(x->2, x^(x + 2)) / 2"));    // 8.0
System.out.println(parser.parse("Σ(i, 2i^2, 1, 5)"));            // 220.0

Supports factorial, binary, hexadecimal, octal:

System.out.println(parser.parse("5!/4"));                        // 30.0
System.out.println(parser.parse("(0b100)!"));                    // 4! = 24.0
System.out.println(parser.parse("log2((0xFF) + 1)"));            // log2(256) = 8.0
System.out.println(parser.parse("(0o777)"));                     // 511.0

Supports IF conditions:

System.out.println(parser.parse("2 + if(2^5 >= 5!, 1, 0)"));     // 2.0

parser.addExpression("gcd(x, y) = if(y = 0, x, gcd(y, x % y))"); // GCD Recursive
System.out.println(parser.parse("gcd(8, 20)"));                  // 4.0

GCD Recursive was only an example, gcd is a built-in function so you don't need to add it as an expression.

System.out.println(parser.parse("gcd(8, 20, 100, 150)"));        // 2.0

Some functions such as sum, max, min and gcd get array.

Import new Functions Easy A!

  1. Create a class and static methods as your functions:
public class MyFunctions {

    public static double test(double a, double b) {
        return a * b / 2;
    }

    public static double minus(Double... a) {
        double out = a[0];
        for (int i = 1; i < a.length; i++)
            out -= a[i];
        return out;
    }

}
  1. Add the class to the parser
parser.addFunctions(MyFunctions.class);
  1. Done!
System.out.println(parser.parse("test(10, 5)"));                 // 25.0
System.out.println(parser.parse("minus(100, 25, 5, 2)"));        // 68.0

License

Copyright 2022 Amir Hossein Aghajari
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

LCoders | AmirHosseinAghajari

Amir Hossein AghajariEmailGitHub

Extension points exported contracts — how you extend this code

MathFunction (Interface)
(no doc) [2 implementers]
MathParser/src/com/aghajari/math/MathFunction.java

Core symbols most depended-on inside this repo

trim
called by 26
MathParser/src/com/aghajari/math/MathParser.java
apply
called by 18
MathParser/src/com/aghajari/math/MathParser.java
get
called by 14
MathParser/src/com/aghajari/math/MathParser.java
getVariable
called by 13
MathParser/src/com/aghajari/math/MathParser.java
parse
called by 9
MathParser/src/com/aghajari/math/MathParser.java
realTrim
called by 7
MathParser/src/com/aghajari/math/Utils.java
wrap
called by 7
MathParser/src/com/aghajari/math/MathFunction.java
calculate
called by 6
MathParser/src/com/aghajari/math/MathFunction.java

Shape

Method 186
Class 19
Interface 2

Languages

Java100%

Modules by API surface

MathParser/src/com/aghajari/math/MathParser.java63 symbols
MathParser/src/com/aghajari/math/Functions.java59 symbols
MathParser/src/com/aghajari/math/custom/Integration.java18 symbols
MathParser/src/com/aghajari/math/Utils.java11 symbols
MathParser/src/com/aghajari/math/MathFunction.java8 symbols
MathParser/src/com/aghajari/math/exception/MathParserException.java7 symbols
MathParser/src/com/aghajari/math/custom/RadicalFunction.java7 symbols
MathParser/src/com/aghajari/math/custom/LogFunction.java7 symbols
MathParser/src/com/aghajari/math/custom/LimitFunction.java5 symbols
MathParser/src/com/aghajari/math/exception/MathVariableNotFoundException.java4 symbols
MathParser/src/com/aghajari/math/exception/MathFunctionNotFoundException.java3 symbols
MathParser/src/com/aghajari/math/exception/MathFunctionInvalidArgumentsException.java3 symbols

For agents

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

⬇ download graph artifact