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) Check Check    
Cosine cos(x) Check Check    
Tangent tan(x) Check Check    
Arc Sine asin(x) Check Check    
Arc Cosine acos(x) Check Check    
Arc Tangent atan(x) Check Check    
Arc Tangent (with 2 parameters) atan2(y, x) Check      
Hyperbolic Sine sinh(x) Check Check    
Hyperbolic Cosine cosh(x) Check Check    
Hyperbolic Tangent tanh(x) Check Check    
Inverse Hyperbolic Sine asinh(x) Check Check    
Inverse Hyperbolic Cosine acosh(x) Check Check    
Inverse Hyperbolic Tangent atanh(x) Check Check    
Natural Logarithm ln(x) Check Check    
Logarithm base 10 log(x) Check Check    
Exponential (e^x) exp(x) Check Check    
Absolute Value / Magnitude abs(x) Check Check    
Random number (between 0 and 1) rand()        
Modulus mod(x,y) = x % y Check      
Square Root sqrt(x) Check Check    
Sum sum(x,y,z) Check Check Check  
If if(cond,trueval,falseval) Check      
Str (number to string) str(x) Check Check Check Check
Binomial coefficients binom(n,i) Integer values

Functions added with addComplex():
    Double Complex String Vector
Real Component re(c) Check Check    
Imaginary Component im(c) Check Check    
Complex Modulus (Absolute Value) cmod(c) Check Check    
Argument (Angle of complex value, in radians) arg(c) Check Check    
Complex conjugate conj(c) Check Check    
Complex, constructs a complex number from real and imaginar parts complex(x,y) Check      
Polar, constructs a complex number from modulus and argument polar(r,theta) Check      

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