Note that you can always add new custom functions. Each of the following functions can be applied to objects of the types indicated.
The StandardFunctionTable includes all of these functions by default. They can also be included individually by calling jep.getFunctionTable().addFunction(String, PostfixMathCommandI)
. All standard function classes are located in the jep.functions package. The BigDecimal functions are kept separate in the jep.bigdecimal.functions package.
All functions accept arguments of the Double
and Complex
type, except atan2
which only accepts Double
arguments.
Description | Function Name | Class Name |
Sine | sin(x) | Sine |
Cosine | cos(x) | Cosine |
Tangent | tan(x) | Tangent |
Arc Sine | asin(x) | ArcSine |
Arc Cosine | acos(x) | ArcCosine |
Arc Tangent | atan(x) | ArcTangent |
Arc Tangent (with 2 parameters) | atan2(y, x) | ArcTangent2 |
Hyperbolic Sine | sinh(x) | SineH |
Hyperbolic Cosine | cosh(x) | CosineH |
Hyperbolic Tangent | tanh(x) | TanH |
Inverse Hyperbolic Sine | asinh(x) | ArcSineH |
Inverse Hyperbolic Cosine | acosh(x) | ArcCosineH |
Inverse Hyperbolic Tangent | atanh(x) | ArcTanH |
All functions accept arguments of the Double and Complex types.
Description | Function Name | Class Name |
Natural Logarithm | ln(x) | NaturalLogarithm |
Logarithm base 10 | log(x) | Logarithm |
Logarithm base 2 | lg(x) | LogBase2 |
Exponential (e^x) | exp(x) | Exp |
Power | pow(x) | Power |
Description | Function Name | Class Name |
Round | round(x), round(x, p) | Round |
Floor | floor(x) | Floor |
Ceiling | ceil(x) | Ceil |
Description | Double | Complex | String | Vector | |
If | if(cond, trueval, falseval) | ![]() |
|||
Absolute Value / Magnitude | abs(x) | ![]() |
![]() |
||
Random number (between 0 and 1) |
rand() | ||||
Modulus | mod(x,y) = x % y |
![]() |
|||
Square Root | sqrt(x) | ![]() |
![]() |
||
Sum | sum(x,y,...) | ![]() |
![]() |
![]() |
|
Str (number to string) | str(x) | ![]() |
![]() |
![]() |
![]() |
Binomial coefficients | binom(n, i) | ![]() |
Description | Double | Complex | |
Real Component | re(c) | ![]() |
![]() |
Imaginary Component | im(c) | ![]() |
![]() |
Complex Modulus (Absolute Value) | cmod(c) | ![]() |
![]() |
Argument (Angle of complex value, in radians) | arg(c) | ![]() |
![]() |
Complex conjugate | conj(c) | ![]() |
![]() |
Complex, constructs a complex number from real and imaginary parts | complex(x, y) | ![]() |
|
Polar, constructs a complex number from modulus and argument | polar(r, theta) | ![]() |
JEP allows you to add custom functions by calling getFunctionTable().addFunction(String
name, PostfixMathCommandI function)
. Custom functions are no different
from built-in functions, so you can use the source code of the built-in functions
as a template. The class methods are accessed both during parsing and evaluation.
During parsing, the parser needs to know how many parameters are allowed.
During evaluation, the run(Stack<Object>)
method is called to perform the actual calculation.
The following is an example of how a custom function can be added.
Assume you want to add a function "half" to divide a number by two (for demonstration purposes).
com.singularsys.jep.functions
package). In this example we will name it "Half"numberOfParameters = 1;
numberOfParameters
value to -1. It is highly recommended to study the Sum.java
code as an example
of a function that accepts any number of parameters. run(Stack<Object> inStack)
method. The parameters are passed in
a Stack object. This same stack is used as the output for the function.
So we first need to pop the parameter off the stack, calculate the result,
and finally pop the result back on the stack.
public void run(Stack<Object> inStack) throws EvaluationException { // check the stack checkStack(inStack); // get the parameter from the stack Object param = inStack.pop(); // check whether the argument is of the right type if (param instanceof Double) { // calculate the result double r = ((Double)param).doubleValue() / 2; // push the result on the inStack inStack.push(new Double(r)); } else { throw new EvaluationException("Invalid parameter type"); } }
jep.getFunctionTable().addFunction("half", new Half());
boolean
checkNumberOfParameters(int n)
to catch an illegal number of arguments
at parse time.Source files