There are two packages which offer support for vectors and matrices
Most of the functionality is common to both packages and the following syntax is allow:
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()); }
}
}
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:
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);
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.