exp4j is capable of evaluating expressions and functions in the real domain. It's a small (40KB) library without any external dependencies, that implements Dijkstra's Shunting Yard Algorithm. It comes with a standard set of built-in functions and operators, in addition users are able to create custom operations and functions for use in their mathematical expressions.
Calculable calc = new ExpressionBuilder("3 * sin(y) - 2 / (x - 2)") .withVariable("x", varX) .withVariable("y", varY) .build() double result1=calc.calculate();
Variable names must start with a letter or the underscore (_) and can only include letters, digits or underscores.
the following are valid variable names: * varX * _x1 * _var_X_1 while for e.g. 1_var_x is not as it does not start with a letter or a underscore.
One has to make sure that the variable names are passed into the ExpressionBuilder before calling build(). If variables are used in the expression without setting at least their names, an UnparseableErpessionExcetion will be thrown. The following three examples demonstrate the different possibilities to have exp4j recognize variables:
The feature to use implicit variable declarations as in new ExpressionBuilder("f(x,y)=x*y") has been removed. You will have to use ExpressionBuilder.withVariableNames() or ExpressionBuilder.withVariable() for variable declaration.
Since version 0.3.5 it is possible to use scientific notation for numbers (see http://en.wikipedia.org/wiki/Scientific_notation). The number is split into a significand/mantissa (y) and exponent (x) of the form 'yEx' which is evaluated as 'y * 10^x'. Be aware that 'e/E' is no operator and therefore an expression like "1.1e-(x*2)" can not be evaluated. An example using the Fine-structure constant 'α=7.2973525698 * 10^−3':
String expr = "7.2973525698e-3"; double expected = 7.2973525698d * Math.pow(10, -3); Calculable calc = new ExpressionBuilder(expr).build(); assertTrue(expected == calc.calculate());
you can extend the abstract class CustomFunction in order to use custom functions in expressions. you only have to implement the applyFunction(double[] values) method.
CustomFunction fooFunc = new CustomFunction("foo") { public double applyFunction(double[] values) { return values[0]*Math.E; } }; double varX=12d; Calculable calc = new ExpressionBuilder("foo(x)") .withCustomFunction(fooFunc) .withVariable("x",varX) .build(); assertTrue(calc.calculate() == Math.E * varX);
you can also define multi argument functions like max(a,b,c) via the constructor CustomFunction(String name,int argc), with argc being the argument count.
CustomFunction custom1 = new CustomFunction("max",3) { @Override public double applyFunction(double[] values) { double max=values[0]; for (int i=1;i<this.getArgumentCount();i++) { if (values[i] > max) { max=values[i]; } } return max; } }; double varX=Math.E; Calculable calc = new ExpressionBuilder("max(log(x),sin(x),x)") .withVariable("x", varX) .withCustomFunction(custom1) .build(); assertTrue(varX == calc.calculate());
you can extend the abstract class CustomOperator in order to declare custom operators for use in expressions, with the symbol being one of !,#,§,$,&,;,:,~,<,>,|,=. Be aware that adding a CustomOperator with a used symbol overwrites any existing operators including the builtin ones. So it's possible to overwrite e.g. the '+' operator. The Constructor of a CustomOperator takes up to 4 arguments:
CustomOperator factorial = new CustomOperator('!', true, 6, 1) { @Override double applyOperation(double[] values) { double tmp = 1d; int steps = 1; while (steps < values[0]) { tmp = tmp * (++steps); } return tmp; } }; Calculable calc = new ExpressionBuilder("11!").withOperation(factorial).build(); assertTrue(39916800d == calc.calculate());
In order to accommodate different use cases the precedence for the unary operators in respect to exponentiation can be set via the System property "exp4j.unary.precedence.high". You can set the property via System.getProperty(PROPERTY_UNARY_HIGH_PRECEDENCE,"false") in order to change evaluation from an expression like "-3^2" from "(-3)^2" to "-(3^2)":
String expr = "-3^2"; System.setProperty(ExpressionBuilder.PROPERTY_UNARY_HIGH_PRECEDENCE, "false"); Calculable calc = new ExpressionBuilder(expr).build(); assertTrue(-Math.pow(3, 2) == calc.calculate()); // evaluates to '-9' System.clearProperty(ExpressionBuilder.PROPERTY_UNARY_HIGH_PRECEDENCE); calc = new ExpressionBuilder(expr).build(); assertTrue(Math.pow(-3,2) == calc.calculate()); // evaluates to '9'
exp4j throws a ArithmeticException when a division by zero is attempted. When implementing CustomOperator or CustomFunction involving divisions the implementor has to make sure that a corresponding RuntimeException is thrown. The following example from the exp4j sources shows how to check for such a condition.
CustomOperator mod = new CustomOperator("%", true, 3) { @Override protected double applyOperation(double[] values) { if (values[1] == 0d){ throw new ArithmeticException("Division by zero!"); } return values[0] % values[1]; } };
Please let me know if you're project uses exp4j and you want it listed here