Class Define

All Implemented Interfaces:
JepComponent, PostfixMathCommandI, Serializable
Direct Known Subclasses:
ChainedDefine

public class Define extends NaryFunction implements JepComponent
Defines a lambda function as a regular function.
 define('square', x=> x^2);
 square(5);
 
Since Jep 4.1 this function has a third optional parameter for the description of the function.
 define("power", [x,y]=> x^y, "x to the power of y");
 
It also allows defining operators.
 define('**',
        [[[a,b],[c,d]],[[x,y],[z,w]]]=>[[a x+b z,a y+ b w],[c x+d z,c y+ d w]],
  "'matrix mul',
  'Operator,Prec=*,Left,Binary');
 
An optional fourth parameter is a comma separated list of options.
  • If the first option is Operator than an operator is defined.
  • Prec=* indicates that the operator should have the same precedence as the "*" operator.
  • IPrec=+ indicates that the operator should tighter than the "+" operator.
  • Binary a binary operater
  • Left left binding
  • Right right binding
  • Unary, a unary operator
  • Prefix, a prefix operator
  • Suffix, a suffix operator
  • Associative, an associative operator
  • Commutative, a commutative operator
  • Overload=* indicates that the operator should overload an existing * operator.

Functions can also be overloaded,

define('tr',[ [[a,b],[c,d]] ]=>a+d,'trace 2d');
define('tr',[ [[a,b,c],[d,e,f],[i,j,k]] ]=>a+e+k,'trace 3d','Function,Overload=tr');

After defining a function or operator the jep.reinitializeComponents() method should be called to make the new function or operator available in the parser. For example 2D matrix multiplication can be added to the * operator as follows:

Node node = jep.parse("define('*',"
  + "[[[a,b],[c,d]],[[x,y],[z,w]]]"
   + "    => [[a x+b z,a y+ b w],[c x+d z,c y+ d w]],"
  + "'matrix mul','Operator,Overload=*');");
Object res = jep.evaluate(node);
jep.reinitializeComponents();
See Also:
  • Field Details

  • Constructor Details

    • Define

      public Define()
  • Method Details

    • checkNumberOfParameters

      public boolean checkNumberOfParameters(int n)
      Description copied from class: PostfixMathCommand
      Checks the number of parameters of the function. Functions which set numberOfParameter=-1 should overload this method
      Specified by:
      checkNumberOfParameters in interface PostfixMathCommandI
      Overrides:
      checkNumberOfParameters in class PostfixMathCommand
      Parameters:
      n - number of parameters function will be called with.
      Returns:
      false if an illegal number of parameters is supplied, true otherwise.
    • eval

      public Object eval(Object[] args) throws EvaluationException
      Description copied from class: NaryFunction
      Evaluate the function
      Specified by:
      eval in class NaryFunction
      Parameters:
      args - arguments to the function
      Returns:
      value returned by the function
      Throws:
      EvaluationException - if the calculation cannot be performed
    • getFlags

      protected int getFlags(String options)
      Calculate the operator flags from the options string.
      Parameters:
      options - a comma separated list of options
      Returns:
      the sum of the flags for all matching options
    • getProp

      protected String getProp(String prefix, String options)
      Get the value of a property from the options string.
      Parameters:
      prefix - the prefix to look for, like "Prec=" or "Overload="
      options - a comma separated list of options
      Returns:
      the value of the matching property or null if not found
    • getOperatorProp

      protected Operator getOperatorProp(String prefix, String options)
    • buildOperatorFromOptions

      protected void buildOperatorFromOptions(String name, LambdaFunction pfmc, String options)
      Build an operator from the options string and add it to the operator table.
      Parameters:
      name - the name of the operator
      pfmc - the lambda function that implements the operator
      options - a comma separated list of options
    • buildFunctionFromOptions

      protected void buildFunctionFromOptions(String ls, LambdaFunction pfmc, String options)
    • init

      public void init(Jep jep)
      Description copied from interface: JepComponent
      Initialize the component. This method is called whenever a component is added to Jep. Hence, it allows components to keep track of the other components they may rely on.
      Specified by:
      init in interface JepComponent
      Parameters:
      jep - the current Jep instance
    • getLightWeightInstance

      public JepComponent getLightWeightInstance()
      Description copied from interface: JepComponent
      Gets a light-weight instance suitable for using in multiple threads.
      Specified by:
      getLightWeightInstance in interface JepComponent
      Returns:
      either a new instance, null or 'this'.