<-- Back to Current Category
Java Math Expression Parser
By: Programmer, Tue Mar 9th, 2010
JbcParser makes it possible to parse and evaluate mathematical expressions in your Java programs. This math parser library precompiles the expression into an abstract syntax tree so that repeated expressions are as fast as it gets.
JbcParser - Math Parser for Java features include:
Easy to use, simple class API.
Comes with predefined functions.
You can create custom functions/variables and get callbacks to your own functions in your source code.
Function/variable names start with letters and can contain letters, numbers and ’_’.
Optimization: Constant expression elimination for repeated tasks.
Operators: +, -, /, *, ^
Boolean Operators: <, >, =, &, |, ! ,<>, >=, <=
Paranthesis: (, {, [
Functions in the form of: f(x,y,z, ...)
Function parameter calculations are only done if needed.
List of predefined functions is available in the documentation.
Provides localization support.
Royalty free distribution.
Source code is included.
JbcParser is especially useful in scientific and engineering programs as well as financial spread sheet implementations.
To be efficient in repeated calculations, parser creates a parse tree at first and re-uses this parse tree for each evaluation without the need to reparse.
Optimizer: If Optimization is on, the parse tree will be optimized by calculating constant expression sections at once so that further evaluation requests will be quicker without the need to re-evaluate those constant branches.
JbcParser comes with the Java Math Parser source code and there is also help documentation available for reference. Download is a single zip file with size of around 290KB. Package contains source files, Jar file, sample Applet, JavaDocs.
Examples of typical expressions are:
SIN(3.14)+5^2+POW(2,MAX(X*2,Y))
Functions, variables, constants can be nested.
Common math functions are defined by default.
2*[LN(1+X) / LOG(1-X)]
Paranthesis can be (, {, [ for readability.
IF(X>0, 3/X, F(X))
You can avoid Division By Zero errors as in 3/X.
(3/X will not be evaluated if X>0 is false - parameters to functions are evaluated only if required by the internal logic of a function).
IF(a,b,c) branching function is supported.
Boolean operators are supported. Any non-zero value is TRUE, 0 is FALSE.
Functions can be defined to have 1,2,...N numbers of parameters.
PI*(R^2)
Constants supported. For example, PI is a constant, not a variable. When optimization is turned on, constants may improve the speed of repeated evaluations where expression does not change but variable values change.
X+Y/LOG(1+5)
If Optimization is turned ON, LOG(1+5) will be optimized away since it is a constant.
VOLUME(HEIGHT, WIDTH, LENGTH)
You can create your own functions and variables and name them as you wish.
You can replace a predefined function with your own implementation.
X+COSH(3E-2)
Scientific notation is supported: 3E-2
SUM(1,2,3,4,5,6,...n)
Functions that take unknown number of parameters are supported.