JEP supports a number of different data types and allows. These include numbers,
strings, vectors, complex
numbers. Numbers are represented by the double
data type
by default. It is however possible to change the internal representation as
described in the section Using custom
number classes.
Strings can be entered in an expression by using double quotes. They can be concatenated with the + operator and compared with the == and != relational operators. A sample expression involving the string type is "foo" + "bar" == "foobar", which would be evaluated by JEP as 1 (true).
To add a string as a variable, use the addVariable(String name, Object value) method. If the result of an expression is a string, it can be obtained by calling the getValueAsObject() function.
Vectors are ordered sets of Double elements. A vector is a list of values, separated by commas, and enclosed by square brackets. An example expression involving a vector is:
[3, 4, 5]*2
It would be evaluated as [6, 8, 10] by JEP. To add a vector as variable, use the addVariable(String name, Object value) method. If the result of an expression is a vector, it can be obtained by calling the getValueAsObject() function.
If you want to use complex numbers in your expression, you can call addComplex() after creating the parser object. This will add the imaginary unit as constant i. Two parser functions re() and im() are also added and can be used to obtain the real and imaginary components of complex numbers. You will need to import the org.nfunk.jep.type.Complex class to be able to manipulate complex values from expressions.
By using the imaginary unit constant i, you can work with complex numbers in your expressions. A sample complex expression would be (1+2*i)^3. Currently the (re, IM) notation is not supported.
To obtain a complex value from an expression, you must use the getComplexValue() function. It will evaluate the expression and return the result as a Complex object. Note that the class used internally for Complex numbers is also used for returning the value.
Adding a complex variable or constant to the parser before evaluating an expression can be done with addVariable(String name, double re, double IM). It takes three parameters: the name of the variable as string, the real component, and the imaginary component.
By default when an expression such as "1+2" is parsed, the constants "1" and "2" are created internally as Double objects. In most cases this is fine, but in some cases you may want to use custom classes for representing numbers.
This is made possible through creating a number class that implements the
NumberFactory
interface. It includes one method called createNumber(String
value), which should return an object initialized to the value of the
parameter. You can load your custom number factory with the JEP constructor
JEP(boolean traverse_in, Boolean allowUndeclared_in, Boolean implicitMul_in,
NumberFactory numberFactory_in)
.
The custom number objects need to be handled by custom functions which you can create by following the instructions on the Custom Functions page.
In most cases, you will only need to work with the few built in types that JEP supplies (Double, Complex, Vector, String). But suppose you want to evaluate expressions that involve other types. This is possible by using the addVariableAsObject(String name, Object value) method to add variables of any type. The only place where the type of a variable matters, is in the function classes.
When an expression is evaluated, values are operated on with the classes in the function package. These include the operators (such as Add and Subtract), as well as the functions (such as Sine and Cosine). Without making modifications to the source code, only the default types are handled with these classes. So, in order to be able to handle your own types, you will need to modify theses classes, or make your own function classes as described in the custom functions section.