- JavaDoc

# JEP

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

# Advanced Features

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

# Vectors and Matrices

There are two packages which offer support for vectors and matrices

- org.lsmp.djep.vectorJep - standard vector and matrix handling
- org.lsmp.djep.matrixJep - advanced vector and matrix handling

for most applications the first package should suffice. The second package allows differentiation of equations with vectors and matrices; the overloading of the ^ operator to be both power and cross product; a speed advantages; and the calculation of the dimensions of each equation. The downside is that it adds about 65K to the size of the required packages.

Most of the functionality is common to both packages and the following syntax is allow:

- Vectors: `[1,2,3]` denotes a vector, variables and equations can be used throughout i.e. `[x,y,2*x+3*y]`
- Matrices: `[[1,2],[3,4]]` a matrix with rows [1,2] and [3,4].
- Tensors: `[[[1,2],[3,4]],[[5,6],[7,8]]]` higher rank tensors are also allowed.
- Addition and subtraction of vectors, matrices and tensors, using + and -.
- Multiplication of matrices using * (note size of matrices must match).
- Dot product of vectors `[1,2,3].[4,5,6]`
- Cross product of vectors. The ^ operator is overloaded to be either the cross product: `[1,2,3]^[4,5,6]` or the power operator 2^3. The ^^ operator can be used to force interpretation as a cross product.
- An `ele(vec,index)` function to access the elements of a vector and an `ele(matrix,[index1,index2])` to access an element of a matrix.
- As of Djep 1.1.0 new syntactical feature allows elements of a vector or matrix `a` to be accessed using the `a[2]` notation. This can be used to get and set values.
- As . is used to represent dot product it cannot normally be used inside a variable name. This behaviour can be changed by setting a flag in the parser.

Two interactive console applet illustrate the functionality of VectorJep and MatrixJep.

## vectorJep

The use of the package can be illustrated by

    import org.nfunk.jep.*;
    import org.lsmp.djep.vectorJep.*;

    public class VectorExample {
        static VectorJep j;

        public static void main(String args[])    {
        	// initialise
            j = new VectorJep();
            j.addStandardConstants();
            j.addStandardFunctions();
            j.addComplex();
            j.setAllowUndeclared(true);
            j.setImplicitMul(true);
            j.setAllowAssignment(true);

    		// parse and evaluate each equation in turn

            doStuff("[1,2,3]");               // Value: [1.0,2.0,3.0]
            doStuff("[1,2,3].[4,5,6]");       // Dot product value: 32.0
            doStuff("[1,2,3]*[4,5,6]");       // Vector by vector multiplication is
            								  // treated as a column vector times a row vector
            								  // Use dot product for row vec time col vec
            								  // Value: [[4.0,5.0,6.0],[8.0,10.0,12.0],[12.0,15.0,18.0]]
            doStuff("[1,2,3]^^[4,5,6]");      // Cross product, value: [-3.0,6.0,-3.0]
            doStuff("[1,2,3]+[4,5,6]");       // Value: [5.0,7.0,9.0]
            doStuff("[[1,2],[3,4]]");         // Value: [[1.0,2.0],[3.0,4.0]]
            doStuff("[[1,2],[3,4]]*[1,0]");   // Value: [1.0,3.0]
            doStuff("[1,0]*[[1,2],[3,4]]");   // Value: [1.0,2.0]
            doStuff("[[1,2],[3,4]]*[[1,2],[3,4]]");   // Value: [[7.0,10.0],[15.0,22.0]]
            doStuff("x=[1,2,3]");             // Value: [1.0,2.0,3.0]
            doStuff("x+x");                   // Value: [2.0,4.0,6.0]
            doStuff("x.x");                 // Value: 14.0
            doStuff("x^x");                  // Value: [0.0,0.0,0.0]
            doStuff("ele(x,2)");              // Value: 2.0
            doStuff("y=[[1,2],[3,4]]");       // Value: [[1.0,2.0],[3.0,4.0]]
            doStuff("y * y");                 // Value: [[7.0,10.0],[15.0,22.0]]
            doStuff("ele(y,[1,2])");          // Value: 2.0

        }
    	// parse, evaluate and print the value of the expression
        public static void doStuff(String str)    {
            try {
                Node node = j.parse(str);
                Object value = j.evaluate(node);
                System.out.println(str + "\tvalue " + value.toString());
            }
            catch(ParseException e) { System.out.println("Parse error "+e.getMessage()); }
            catch(Exception e) { System.out.println("evaluation error "+e.getMessage()); }
        }
    }

### Values returned by evaluate

The values returned by evaluateRaw(Node node) or getValueAsObject() or getVarValue(String name) are one of the types in `org.lsmp.djep.vectorJep.values`. These are:

- Scaler Scaler values.
- MVector a vector, note the M to distinguish the class from java.util.Vector.
- Matrix a matrix.
- Tensor a tensor of rank 3 or higher.
- MatrixValueI an interface defining common methods of above.

Several methods are common to all types getDim() (returns the dimension of object) getNumEles() (returns the total number of elements) getEle(i) (returns the i-th element of the object) setEle(i,value) (sets the value of an element). In addition `Matrix` has a getEle(row,col) and a setEle(row,col,value). These methods allow the individual elements of vector or matrix to be set and queried.

The evaluate(Node node) method behaves the same as evaluateRaw(Node node) however if the result is a Scaler then it will be unwrapped and the single value will be returned, typically a Double or Complex.

### Printing with vectorJep

To keep package size down print facilities are not provided in the `org.lsmp.djep.vectorJep` package. However it is easy to include them by using the `org.lsmp.djep.xjep.printVisitor` class. For example:

    import org.lsmp.djep.xjep.PrintVisitor;
    ....
    PrintVisitor pv = new PrintVisitor();
    Node node = j.parse("[1,2,3]");
    pv.print(node);
    String str = pv.toString(node);

### Element by element calculations

The vectorJep package can be set up so that element by element multiplication and division is performed instead of matrix multiplication. To switch this mode on use

    VectorJep j = new VectorJep();

    j.setElementMultiply(true);

Once set `[1,2,3]*[4,5,6]` will evaluate to `[4,10,18]`.

### Functions on Vectors and Matricies

Several functions are added with the VectorJep package these are:

Name | Description | Examples
--- | --- | ---
length | Finds the length of a vector or the number of elements in a matrix or tensor. | `length(5)==1, `length([1,2,3])==3`, `length([[1,2],[3,4]])==4``
size | Finds the size of a vector, matrix or tensor. | `size(5)==1`, `size([1,2,3])==3`, `size([[1,2],[3,4],[5,6])==[2,3]`
det | The determinant of a square matrix. | `det([[1,2],[3,4]])==-2`
trace | The trace of a square matrix. | `trace([[1,2],[3,4]])==5`
vsum | The sum of elements in a vector, matrix or tensor. | `vsum([1,2,3])==6`, `vsum([[1,2],[3,4]])==10`
trans | The transpose of a matrix. | `trans([[1,2],[3,4]])==[[1,3],[2,4]]`
getdiag | Extracts the diagonal from a square matrix. | `getdiag([[1,2],[3,4]])==[1,4]`
diag | Generates a square matrix with a given diagonal. | `diag([1,2,3])==[[1,0,0],[0,2,0],[0,0,3]]`
GenMat | Generates vectors and matrixies | First argument specifies size of the vector (3) and matricies ([2,2]). Second argument is formula for each element, constants (1), functions (rand()), Third argument (if present) is list of variables used in formula. For vectors a single variable is specified whos value runs from 1 to number of elements. For matricies a two variable list ([ii,jj]) is specified whos value are the column and row indicies.
    GenMat(3,1) -> [1,1,1]
    GenMat(3,ii,ii) -> [1,2,3]
    GenMat(3,rand()) -> [0.343,0.974,0.567]
    GenMat([2,2],ii+jj,[ii,jj]) -> [[2,3],[3,4]]
ele | Extracts an element from a vector, matrix or tensor. | `ele([1,2,3],2)==2`, `ele([[1,2],[3,4]],[1,2])==2`

The Map function applies a function to each element of a vector or matrix. The first argument is the function to apply. The second argument is either a variable or a list of variables. The third and subsequent arguments are the vectors or matricies the function is applied to. During evaluation the variables will be set to each element of the vectors or matricies. For example

    Map(x^3,x,[1,2,3]) -> [1.0,8.0,27.0]
    Map(x*y,[x,y],[1,2,3],[4,5,6]) -> [4.0,10.0,18.0]
    Map(if(x>0,x,0),x,[-2,-1,0,1,2]) -> [0.0,0.0,0.0,1.0,2.0]
    Map(abs(x),x,[[-2,-1],[1,2]]) -> [[2.0,1.0],[1.0,2.0]]

### variable names with dot in them

As the dot `.` symbol is used for the dot or scaler product, it cannot be normally be used in a varible name. This behaviour can be switched on or off using

    jep.getParser().setInitialTokenManagerState(ParserConstants.NO_DOT_IN_IDENTIFIERS);
    jep.getParser().setInitialTokenManagerState(ParserConstants.DEFAULT);

### Array element access

In DJep version 1.1.0 the `a[2]` notation can be used to get and set individual elements of an array.

`a=[1,2,3]; a[2]=4; c=a[3]; b=[[1,2],[3,4]]; b[2,1]=5;`

- This notation may break previous code as `r[1,2]` previously multiplied the vector `[1,2]` by the variable `r`. To multiple a vector by a scaler variable use `r*[1,2]`.
- Setting array elements is not compatable with storing the equations of variable discussed in xjep. In particular setting an element of a vector will not affect the differentation of the variable.
- The setting of array elements is acheived using the LValueI which adds a hook to permit a PostfixMathCommand to be used on the left hand side of an assignment.

## MatrixJep

The MatrixJep package offers the same functionality as the VectorJep package however it is implemented in a different manner internally which offers a few new features:

- A speed improvement - matrix operations are about a third faster.
- The preprocessing stage is used to calculate the dimension of each node.
- All the features of the XJep package are enabled.
- Differentiation is enabled.

It is essential that the preprocess method is called after an equation is parsed. This will find the dimensions of each node, process the diff operator and set the equations of variables.

A typical example of the use of this package is: (differences from vectorJep are shown in bold)

    import org.nfunk.jep.*;
    import org.lsmp.djep.matrixJep.*;
    import org.lsmp.djep.matrixJep.nodeTypes.*; // only needed if you wish
                                              // to find the dimension of a node
    public class MatrixExample {
        static MatrixJep j;
        public static void main(String args[])    {
           	// initialise
           	j = new MatrixJep();
           	j.addStandardConstants();
           	j.addStandardFunctions();
           	j.addComplex();
           	j.setAllowUndeclared(true);
           	j.setImplicitMul(true);
           	j.setAllowAssignment(true);

           	// parse and evaluate each equation in turn

           	doStuff("[1,2,3]");               // Value: [1.0,2.0,3.0]
           	doStuff("[1,2,3].[4,5,6]");       // Value: 32.0
           	doStuff("[1,2,3]^^[4,5,6]");      // Value: [-3.0,6.0,-3.0]
           	doStuff("[1,2,3]+[4,5,6]");       // Value: [5.0,7.0,9.0]
           	doStuff("[[1,2],[3,4]]");         // Value: [[1.0,2.0],[3.0,4.0]]
           	doStuff("[[1,2],[3,4]]*[1,0]");   // Value: [1.0,3.0]
           	doStuff("[1,0]*[[1,2],[3,4]]");   // Value: [1.0,2.0]
           	doStuff("[[1,2],[3,4]]*[[1,2],[3,4]]");   // Value: [[7.0,10.0],[15.0,22.0]]
           	doStuff("x=[1,2,3]");             // Value: [1.0,2.0,3.0]
           	doStuff("x+x");                   // Value: [2.0,4.0,6.0]
           	doStuff("x.x");                   // Value: 14.0

           		// ^ can be used to represent the cross product as well as power.
           	doStuff("x^x");                   // Value: [0.0,0.0,0.0]
           	doStuff("ele(x,2)");              // Value: 2.0
           	doStuff("y=[[1,2],[3,4]]");       // Value: [[1.0,2.0],[3.0,4.0]]
           	doStuff("y * y");                 // Value: [[7.0,10.0],[15.0,22.0]]
           	doStuff("ele(y,[1,2])");          // Value: 2.0
           	// using differentiation
           	doStuff("x=2");	                  // 2.0
           	doStuff("y=[x^3,x^2,x]");         // [8.0,4.0,2.0]
           	doStuff("z=diff(y,x)");	          // [12.0,4.0,1.0]
           	doStuff("diff([x^3,x^2,x],x)");

           	// Finding the dimension of a variable
           	System.out.println("dim(z) "+((MatrixVariableI) j.getVar("z")).getDimensions());
        }
        // parse, evaluate and print the value of the expression
        public static void doStuff(String str)    {
            try {
                Node node = j.parse(str);
                Node proc = j.preprocess(node);
                Node simp = j.simplify(proc);
                Object value = j.evaluate(simp);
                 // Print the equation and its dimension
                j.print(simp);
                System.out.print("\t dim "+((MatrixNodeI) simp).getDim());
                System.out.println("\tvalue " + value.toString());
            }
            catch(ParseException e) { System.out.println("Parse error "+e.getMessage()); }
            catch(Exception e) { System.out.println("evaluation error "+e.getMessage()); }
        }
    }

Note that in MatrixJep variables and Node have dimensions. Each variable will be of type MatrixVariableI and the dimension of this can be found by the getDimensions method. After the preprocess method is called the nodes in the parse tree all implement MatrixNodeI which have a getDim method. Each node also has a object of type MatrixValueI which stores intermediate values. By reusing these objects evaluation speeds are increased by a third.

## JAMA

JAMA http://math.nist.gov/javanumerics/jama/ is a library of matrix fuctions, offering matrix solving, and other advanced matrix operations.

The org.lsmp.djep.jama package allows these sophsticated matrix functions to be used with VectorJep and MatrixJep. Currently only a small subset (solve, rank, inverse) are implemented.