Vectors and Matrices

There are two packages which offer support for vectors and matrices

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 slight 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:

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]"); // 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
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:

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);

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:

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 getDim method. After the preprocess method is called the nodes in the parse tree all implement MatrixNodeI which have a getDimensions 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.