- JavaDoc

# JEP

- Introduction
- Basic Usage
- Variables
- Data types
- Operators
- Functions
- BigDecimal
- FAQ
- Migration
- Version History

# Advanced Features

- Manipulating Expressions
- Components
- Configurable Parser
- Serialization
- Grammar

# Data Types

Jep supports a number of different data types in the standard mode. These include numbers, strings, vectors, and complex numbers. By default numbers are represented by the `double` data type. It is however possible to change the internal representation as described in the section on Custom types.

## Strings

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 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 evaluate() function.

## Vectors

Vectors are ordered sets of Double elements. A vector is a list of values, separated by commas, and enclosed by square brackets. Some example expressions involving a vectors are listed below

    [3, 4, 5]*2  // evaluates as [6, 8, 10]

    a = [2, 3, 10]

    a[1] // evaluates as 2

    a[3] //evaluates as 10

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 evaluate() method.

You can access elements of a vector using square brackets. For example, to access the second element of a vector `v`, use `v[2]` to access its value.

## Complex numbers

Jep supports complex numbers in most operators and functions. The complex imaginary unit i is part of the standard variable table and is added to the parser when using the StandardComponents. A set of complex functions are also included. These include re(c), im(c), cmod(c), arg(c), conj(c), complex(x, y) and polar(r, theta). See the functions page for more information on these functions. You will need to import the com.singularsys.jep.standard.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. Some sample complex expressions are listed below:

    (1+2*i)^3
    e^(-i)
    re((1+2*i)^2 - (3+3*i)^2)

To obtain a complex value from an expression, you must use the evaluate() method. It will evaluate the expression and return the result as a Complex object. You will need to manually cast the Object returned to the Complex type.

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.

## Custom types

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. You may want to let variables have custom types, or create numbers with custom types.

You can create variables with custom types by using the addVariable(String name, Object value) method.

You can ensure that numbers are created with a specific type by setting the number factory. For example this is how the bigdecimal components are able to create numbers that are more precise than the `double` type allows. Simply implement the NumberFactory interface, implement the createNumber(String) method, and call setComponent(JepComponent) to load your number factory class into Jep.

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. For more information see the custom functions section.