MathParser

Additional

Language
Java
Version
N/A
Created
Feb 16, 2022
Updated
Aug 1, 2022 (Retired)
Owner
AmirHosseinAghajari (Aghajari)
Contributor
AmirHosseinAghajari (Aghajari)
1
Activity
Badge
Generate
Download
Source code

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.




Amir Hossein Aghajari • EmailGitHub