# MathParser

## General

Category
Free
Tag
Mathematics
Min SDK
16 (Android 4.1 Jelly Bean)
Registered
Feb 23, 2022
Favorites
0
https://github.com/Aghajari/MathParser
MathView
Katex Mathview
MathJS Android
kotlin-math
Calc

Language
Java
Version
N/A
Created
Feb 16, 2022
Updated
Aug 1, 2022 (Retired)
Owner
AmirHosseinAghajari (Aghajari)
Contributor
1
Activity
Generate
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
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

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

Copyright 2022 Amir Hossein Aghajari
you may not use this file except in compliance with the License.
You may obtain a copy of the License at