{"id":25319682,"url":"https://github.com/aghajari/mathparser","last_synced_at":"2025-10-28T22:31:09.388Z","repository":{"id":65075308,"uuid":"459949777","full_name":"Aghajari/MathParser","owner":"Aghajari","description":"MathParser is a simple but powerful open-source math tool that parses and evaluates algebraic expressions written in pure java.","archived":false,"fork":false,"pushed_at":"2022-08-01T09:22:26.000Z","size":70,"stargazers_count":47,"open_issues_count":1,"forks_count":7,"subscribers_count":3,"default_branch":"main","last_synced_at":"2023-09-17T06:31:40.564Z","etag":null,"topics":["math","math-expressions","math-parser","mathematics"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Aghajari.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-02-16T10:10:44.000Z","updated_at":"2023-09-03T18:08:14.000Z","dependencies_parsed_at":"2022-12-24T03:53:26.041Z","dependency_job_id":null,"html_url":"https://github.com/Aghajari/MathParser","commit_stats":null,"previous_names":[],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aghajari%2FMathParser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aghajari%2FMathParser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aghajari%2FMathParser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aghajari%2FMathParser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Aghajari","download_url":"https://codeload.github.com/Aghajari/MathParser/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238734068,"owners_count":19521557,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["math","math-expressions","math-parser","mathematics"],"created_at":"2025-02-13T20:54:37.221Z","updated_at":"2025-10-28T22:31:09.095Z","avatar_url":"https://github.com/Aghajari.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MathParser\n**MathParser** is a simple but powerful open-source math tool that parses and evaluates algebraic expressions written in pure java.\n\nThis project is designed for University first project of Advanced-Programming at the Department of Computer Engineering, Amirkabir University of Technology.\n\n## Syntax\n\n1. Create an instance of `MathParser`\n```java\nMathParser parser = MathParser.create();\n```\n\n2. Parse an expression\n```java\nSystem.out.println(parser.parse(\"2 + 2\"));                       // 4.0\nSystem.out.println(parser.parse(\"5^2 * (2 + 3 * 4) + 5!/4\"));    // 380.0\n```\n\n### Add Expression (Function or Variable)\n```java\nparser.addExpression(\"f(x, y) = 2(x + y)\");                      // addFunction\nparser.addExpression(\"x0 = 1 + 2 ^ 2\");                          // addVariable\nparser.addExpression(\"y0 = 2x0\");                                // addVariable\nSystem.out.println(parser.parse(\"1 + 2f(x0, y0)/3\"));            // 21.0\n```\n\n### Built-in functions\nMathParser identifies all static methods in [Math](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html) and [Built-in Functions](https://github.com/Aghajari/MathParser/blob/main/MathParser/src/com/aghajari/math/Functions.java). Functions and Variables are case-insensitive.\n\n```java\nSystem.out.println(parser.parse(\"sin(3pi/2) + tan(45°)\"));\n```\n\nBuilt-in Functions includes integral, derivative, limit and sigma\n```java\nSystem.out.println(parser.parse(\"2 ∫(x, (x^3)/(x+1), 5, 10)\"));  // 517.121062\nSystem.out.println(parser.parse(\"derivative(x, x^3, 2)\"));       // 12.0\nSystem.out.println(parser.parse(\"lim(x-\u003e2, x^(x + 2)) / 2\"));    // 8.0\nSystem.out.println(parser.parse(\"Σ(i, 2i^2, 1, 5)\"));            // 220.0\n```\n\nSupports factorial, binary, hexadecimal, octal:\n```java\nSystem.out.println(parser.parse(\"5!/4\"));                        // 30.0\nSystem.out.println(parser.parse(\"(0b100)!\"));                    // 4! = 24.0\nSystem.out.println(parser.parse(\"log2((0xFF) + 1)\"));            // log2(256) = 8.0\nSystem.out.println(parser.parse(\"(0o777)\"));                     // 511.0\n```\n\nSupports IF conditions:\n```java\nSystem.out.println(parser.parse(\"2 + if(2^5 \u003e= 5!, 1, 0)\"));     // 2.0\n\nparser.addExpression(\"gcd(x, y) = if(y = 0, x, gcd(y, x % y))\"); // GCD Recursive\nSystem.out.println(parser.parse(\"gcd(8, 20)\"));                  // 4.0\n```\nGCD Recursive was only an example, gcd is a built-in function so you don't need to add it as an expression.\n```java\nSystem.out.println(parser.parse(\"gcd(8, 20, 100, 150)\"));        // 2.0\n```\nSome functions such as `sum`, `max`, `min` and `gcd` get array.\n\n### Import new Functions Easy A!\n\n1. Create a class and static methods as your functions:\n```java\npublic class MyFunctions {\n\n    public static double test(double a, double b) {\n        return a * b / 2;\n    }\n\n    public static double minus(Double... a) {\n        double out = a[0];\n        for (int i = 1; i \u003c a.length; i++)\n            out -= a[i];\n        return out;\n    }\n    \n}\n```\n2. Add the class to the parser\n```java\nparser.addFunctions(MyFunctions.class);\n```\n3. Done!\n```java\nSystem.out.println(parser.parse(\"test(10, 5)\"));                 // 25.0\nSystem.out.println(parser.parse(\"minus(100, 25, 5, 2)\"));        // 68.0\n```\n\nLicense\n=======\n\n    Copyright 2022 Amir Hossein Aghajari\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n\n\n\u003cbr\u003e\u003cbr\u003e\n\u003cdiv align=\"center\"\u003e\n  \u003cimg width=\"64\" alt=\"LCoders | AmirHosseinAghajari\" src=\"https://user-images.githubusercontent.com/30867537/90538314-a0a79200-e193-11ea-8d90-0a3576e28a18.png\"\u003e\n  \u003cbr\u003e\u003ca\u003eAmir Hossein Aghajari\u003c/a\u003e • \u003ca href=\"mailto:amirhossein.aghajari.82@gmail.com\"\u003eEmail\u003c/a\u003e • \u003ca href=\"https://github.com/Aghajari\"\u003eGitHub\u003c/a\u003e\n\u003c/div\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faghajari%2Fmathparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faghajari%2Fmathparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faghajari%2Fmathparser/lists"}