FormulaJ is a multithreading Java library for evaluating mathematical expressions. It supports +, -, *, ^, and % operators, boolean expressions, a short expression format (2x + 3y), and variables. It includes a standard library with 20 mathematical functions, that can delegate unknown functions or symbols. Users can create and use their own custom functions in an easy way.
- Expressions may have variables
- Supports the use of implicit variables; e.g., sum ("x + y * z ^ x", 1, 2, 3)
- Supports nested functions
- FormulaJ comes with a standard library with 20 mathematical functions
- Users can create and use their own custom functions
- Users can provide and use their own custom expression evaluator
- An expression can be associated to a variable; e.g., c = a * b
-
See instructions in Maven Repository
-
ExpressionBuilder. newMathExpression("a * b") .withVariable("a", Decimal.from(7)) .withVariable("b", Decimal.from(8)) .evaluate();
or
Decimal value = ExpressionBuilder.<Decimal> evaluate("x + y * z ^ x", 1, 2, 3);
The variables are resolved according with their position and their name. If a variable is used more than one time in the expression, we don't need to repeat its value. In that expression we have three variables (x, y, z) and the variable x appears two times in the expression. As we can see, the method was called with just three values (1,2,3). The analyzer knows that the value of x is 1, y is 2 and z is 3.
-
Functions are instances of formulaj.expression.function.Function.
-
The return of a function is a formulaj.expression.Decimal.
-
To create a function you can extended the class formulaj.expression.function.math.FunctionSupport and write its code in the method eval. For instance:
public class Times extends FunctionSupport { /** * Creates an object of the {@link Times} function. */ public Times() { super(2); }
@Override protected Decimal eval(Decimal[] arguments) { return arguments[0].times(arguments[1]); }
}
Decimal value = ExpressionBuilder.<Decimal> newMathExpression("times(2,3)")
.withFunction(new Times())
.evaluate();
To report an issue or request a new feature you just have to open an issue in the repository issue tracker (https://github.com/alessandroleite/formulaj/issues).
To contribute, follow this steps:
- Fork this project
- Add the progress label to the issue you want to solve (add a comments to say that you work on it)
- Create a topic branch for this issue
- When you have finish your work, open a pull request (use the issue title for the pull request title)
Comments and contributions are welcome.
Copyright (C) 2013-2015 Contributors.
FormulaJ is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
FormulaJ is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see [http://www.gnu.org/licenses/].