Class Define
java.lang.Object
com.singularsys.jep.functions.PostfixMathCommand
com.singularsys.jep.functions.NaryFunction
com.singularsys.extensions.lambda.Define
- All Implemented Interfaces:
JepComponent,PostfixMathCommandI,Serializable
- Direct Known Subclasses:
ChainedDefine
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
Operatorthan 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.Binarya binary operaterLeftleft bindingRightright bindingUnary, a unary operatorPrefix, a prefix operatorSuffix, a suffix operatorAssociative, an associative operatorCommutative, a commutative operatorOverload=*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 Summary
FieldsFields inherited from class com.singularsys.jep.functions.PostfixMathCommand
curNumberOfParameters, description, name, NaN, numberOfParameters -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidbuildFunctionFromOptions(String ls, LambdaFunction pfmc, String options) protected voidbuildOperatorFromOptions(String name, LambdaFunction pfmc, String options) Build an operator from the options string and add it to the operator table.booleancheckNumberOfParameters(int n) Checks the number of parameters of the function.Evaluate the functionprotected intCalculate the operator flags from the options string.Gets a light-weight instance suitable for using in multiple threads.protected OperatorgetOperatorProp(String prefix, String options) protected StringGet the value of a property from the options string.voidInitialize the component.Methods inherited from class com.singularsys.jep.functions.NaryFunction
runMethods inherited from class com.singularsys.jep.functions.PostfixMathCommand
asArray, asBool, asDouble, asInt, asLong, asStrictInt, asString, asType, getDescription, getDescription, getDescriptionWithType, getName, getNumberOfParameters, setCurNumberOfParameters, setDescription, setName, toString, toString
-
Field Details
-
ft
-
ot
-
jep
-
OpFlags
-
-
Constructor Details
-
Define
public Define()
-
-
Method Details
-
checkNumberOfParameters
public boolean checkNumberOfParameters(int n) Description copied from class:PostfixMathCommandChecks the number of parameters of the function. Functions which set numberOfParameter=-1 should overload this method- Specified by:
checkNumberOfParametersin interfacePostfixMathCommandI- Overrides:
checkNumberOfParametersin classPostfixMathCommand- Parameters:
n- number of parameters function will be called with.- Returns:
- false if an illegal number of parameters is supplied, true otherwise.
-
eval
Description copied from class:NaryFunctionEvaluate the function- Specified by:
evalin classNaryFunction- Parameters:
args- arguments to the function- Returns:
- value returned by the function
- Throws:
EvaluationException- if the calculation cannot be performed
-
getFlags
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
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
-
buildOperatorFromOptions
Build an operator from the options string and add it to the operator table.- Parameters:
name- the name of the operatorpfmc- the lambda function that implements the operatoroptions- a comma separated list of options
-
buildFunctionFromOptions
-
init
Description copied from interface:JepComponentInitialize 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:
initin interfaceJepComponent- Parameters:
jep- the current Jep instance
-
getLightWeightInstance
Description copied from interface:JepComponentGets a light-weight instance suitable for using in multiple threads.- Specified by:
getLightWeightInstancein interfaceJepComponent- Returns:
- either a new instance, null or 'this'.
-