JEP - Java Math Expression Parser

- JavaDoc

# JEP

- Basic Usage
- Variables
- Data types
- Operators
- Functions
- Advanced Features
- Grammar
- FAQ
- Version History

# Extensions

- Overview
- XJep
- Differentiation
- Vectors and Matrices
- Groups
- Version History

# Functions

Note that you can always add new custom functions. Each of the following functions can be applied to objects of the types indicated.

Functions added with addStandardFunctions():

|  | Double | Complex | String | Vector
Sine | sin(x) |  |  |  |
Cosine | cos(x) |  |  |  |
Tangent | tan(x) |  |  |  |
Arc Sine | asin(x) |  |  |  |
Arc Cosine | acos(x) |  |  |  |
Arc Tangent | atan(x) |  |  |  |
Arc Tangent (with 2 parameters) | atan2(y, x) |  |  |  |
Hyperbolic Sine | sinh(x) |  |  |  |
Hyperbolic Cosine | cosh(x) |  |  |  |
Hyperbolic Tangent | tanh(x) |  |  |  |
Inverse Hyperbolic Sine | asinh(x) |  |  |  |
Inverse Hyperbolic Cosine | acosh(x) |  |  |  |
Inverse Hyperbolic Tangent | atanh(x) |  |  |  |
Natural Logarithm | ln(x) |  |  |  |
Logarithm base 10 | log(x) |  |  |  |
Exponential (e^x) | exp(x) |  |  |  |
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,z) |  |  |  |
If | if(cond,trueval,falseval) |  |  |  |
Str (number to string) | str(x) |  |  |  |
Binomial coefficients | binom(n,i) | Integer values |  |  |

Functions added with addComplex():

|  | Double | Complex | String | Vector
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 imaginar parts | complex(x,y) |  |  |  |
Polar, constructs a complex number from modulus and argument | polar(r,theta) |  |  |  |

## Custom Functions

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).

1. Create a class that extends PostfixMathCommand (in the org.nfunk.jep.function package). In this example we will name it "Half"
2. In the constructor set the number of arguments to be taken. In our case this is one. Do this by writing "numberOfParameters = 1;"
   If you want to allow any number of parameters, initialize the 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.
3. Implement the run(Stack 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 inStack) throws ParseException {

          // 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 ParseException("Invalid parameter type");
          }
       }

4. In your main program or applet that is using the parser, add the new function. Do this by writing

       parser.addFunction("half", new Half());

5. If numberOfParameters == -1 you may wish to overwrite the `boolean checkNumberOfParameters(int n)` to catch an illegal number of arguments at parse time.

Source files

- CustFunc.java (a testing class)
- Half.java (the Half function class)