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

Overview

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

2, x^(x + 2)) / 2")); // 8.0 System.out.println(parser.parse("Σ(i, 2i^2, 1, 5)")); // 220.0">
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:

= 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">
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 Aghajari • EmailGitHub
You might also like...

🔍 Open Source Enterprise Cognitive Search Engine

OpenK9 OpenK9 is a new Cognitive Search Engine that allows you to build next generation search experiences. It employs a scalable architecture and mac

Dec 10, 2022

DataLink is a new open source solution to bring Flink development to data center.

DataLink is a new open source solution to bring Flink development to data center.

DataLink 简介 DataLink 是一个创新的数据中台解决方案,它基于 SpringCloud Alibaba 和 Apache Flink 实现。它使用了时下最具影响力的实时计算框架Flink,而且紧跟社区发展,试图只通过一种计算框架来解决离线与实时的问题,实现Sql语义化的批流一体,帮助

Dec 28, 2022

DataLink is a new open source solution to bring Flink development to data center.

DataLink is a new open source solution to bring Flink development to data center.

DataLink 简介 DataLink 是一个创新的数据中台解决方案,它基于 SpringCloud Alibaba 和 Apache Flink 实现。它使用了时下最具影响力的实时计算框架Flink,而且紧跟社区发展,试图只通过一种计算框架来解决离线与实时的问题,实现Sql语义化的批流一体,帮助

Dec 22, 2021

A open-source addon for Meteor Client.

A open-source addon for Meteor Client.

A open-source addon for Meteor Client

Dec 28, 2022

Java Statistical Analysis Tool, a Java library for Machine Learning

Java Statistical Analysis Tool JSAT is a library for quickly getting started with Machine Learning problems. It is developed in my free time, and made

Dec 20, 2022

DFA来过滤敏感词工具。--- The sensitive word tool for java with DFA.

sensitive-word-plus sensitive-word-plus 基于 DFA 算法实现的高性能敏感词工具。 站在巨人肩膀上,本项目是根据sensitive-word 做的升级 创作目的 基于sensitive-word-plus 实现返回敏感词类型 实现一款好用敏感词工具。 基于 D

Sep 22, 2022
Owner
AmirHosseinAghajari
AmirHosseinAghajari
JML - Java Math Library.

JML JML - Java Math Library. JML is a Java Math Library for solving Advanced Mathematical calculations. Disclaimer This project is under heavy develop

Java Math Library 3 Sep 23, 2021
java math accurate implementation & experiments

Marlin-Math Accurate and fastest Math functions in java, like the Marlin renderer ! Rationale Java supports Quadratic & Cubic curves in Java2D & JavaF

Laurent Bourgès 7 Nov 18, 2021
neutriNote - Markdown + Math in Just 3 MB!

Official | FAQ | Documentation | Mastodon | XDA neutriNote (Community Edition) What is neutriNote? In a nutshell, all-in-one preservation of written t

AppML 186 Jan 3, 2023
Math World is an android application specialized in mathematics discover more about it in README.

Math World App Math World is an Android Application specialized in mathematics, where the application includes some sections related to arithmetic, un

null 7 Mar 12, 2022
Datumbox is an open-source Machine Learning framework written in Java which allows the rapid development of Machine Learning and Statistical applications.

Datumbox Machine Learning Framework The Datumbox Machine Learning Framework is an open-source framework written in Java which allows the rapid develop

Vasilis Vryniotis 1.1k Dec 9, 2022
A powerful, extendable, flexible yet simple to use commands annotation framework.

Lamp Background Building commands has always been a core concept in many applications, and, lots of times, a really boring and cumbersome one to pull

Revxrsal 95 Jan 5, 2023
oj! Algorithms - ojAlgo - is Open Source Java code that has to do with mathematics, linear algebra and optimisation.

oj! Algorithms oj! Algorithms - ojAlgo - is Open Source Java code that has to do with mathematics, linear algebra and optimisation. General informatio

Optimatika 403 Dec 14, 2022
CompreFace is a free and open-source face recognition system from Exadel

CompreFace can be easily integrated into any system without prior machine learning skills. CompreFace provides REST API for face recognition, face verification, face detection, landmark detection, age, and gender recognition and is easily deployed with docker

Exadel 2.6k Dec 31, 2022
An Open Source Java Library for the Rubiks Cube!

?? Table of contents Overview What is Cubot? Why would you want it? Documentation Installation Updates ?? Overview A Java library to help you : Virtua

Akshath Raghav 13 Oct 17, 2022