XJep - extensions to Jep


The com.singularsys.extensions.xjep package provides the XJep which extends the standard Jep class and add various symbolic operations plus a preprocessing facility allowing symbolic operators and an enhanced variable type.

A simple usage is

XJep xjep = new XJep();
Node node = xjep.parse("1 x^2 + 0");
Node simp = xjep.simplify(node);
top

Symbolic functions

Symbolic operations exposed by the class include:

Node clean(Node node)
simplifies an expression using the Expression cleaner
Node simplify(Node node)
simplifies expression using the polynomials package.
Node expand(Node node)
expands brackets in an expression using the polynomials package.
Node deepCopy(Node node)
produces a copy of a node
Node equals(Node l,Node r)
compares two simplified expressions without expansion.
Node substitute(Node orig,String name,Object value)
substitutes all occurrences of variable name with value.
Node substitute(Node orig,String name,Node replacement)
substitutes all occurrences of variable name with the expression specified by replacement.
Node substitute(Node orig,String sub)
perform substitution using expression of the form x=.....
Node substitute(Node orig, String[] names, Object[] values)
substitute a set of valuables by a set of values.
Node substitute(Node orig, String[] names, Node[] replacement)
substitute a set of valuables by a set of expressions.
Node substitute(Node orig, Node[] subs)
substitute using a set of expressions of the form x=... .
Node substituteConstantVariables(Node orig)
substitutes all constant variables like pi and e by their values.
Node replaceVariablesByEquations(Node node)
replace any symbolic variables by their equations
Node replaceRHSVariablesByEquations(Node node)
replace any symbolic variables on the RHS of an assignment by their equations

Expression can be interrogated using Set<Variable> getVarsInEquation(Node n)

which returns a set of all the variables mentioned in a single expression. A more detailed analysis of expression can be found by using the com.singularsys.jep.walkers.TreeAnalyzer class.

top

Preprocessing

The class adds a preprocessing step Node preprocess(Node node) which process any symbolic operations, like expansion or symbolic differentiation, that appear in an expression. Jep function like expand implement CommandVisitorI. This adds a Node process(Node node, Node children[]) method which is executed by the CommandVisitor whenever the preprocess(Node) method is called. For example

XJep xjep = new XJep();
xjep.addFunction("expand",new Expand());
xjep.reinitializeComponents();
Node node = xjep.parse("expand((x+1)*(x-1))");
Node exp = xjep.preprocess(node);

Current preprocessor operators available are: com.singularsys.extensions.xjep.Clean, com.singularsys.extensions.polynomials.Simplify, com.singularsys.extensions.polynomials.Expand, com.singularsys.extensions.polynomials.Compare, com.singularsys.extensions.xjep.XAssign (see variables section below) and com.singularsys.extensions.djep.Diff differentiation see differentiation documentation.

top

XVariables

The xjep package introduce a new type of variable, XVariable, which allow variables to be associated with equations. Variable created by an XJep instance will automatically be of this type. In the processing step any assignment expression like y=x^2+1 set equation of the left hand variable to the expression on the right. The is acheived using the XAssign Jep function.

The equation for a variable can be recovered by using XVariable.getEquation(), and the value of a variable can be calculated from its equation using XJep.calcVarValue(name,flag). The flag option controls how any variables in the expression are evaluated. If set to true then all variables with equations are automatically evaluate. For example if z=y*y and y=x+1 then to evaluate z the equation for variable y will first be evaluated. And as y appears twice its equation will be evaluated twice. If the flag is set to false then equations will only be evaluated as needed. Each variable has a validValue flag, this is set to true whenever the value is set either by calling jep.addVariable(name,value) or through evaluating an equation with the variable on the left hand side. This flag can be cleared by calling Variable.setValidValue(false) or by calling VariableTable.clearValues() which clears all values. So with the above example the y will only be evaluated once.

XJep xjep = new XJep();
xjep.addVariable("x", 3.0); // set variable with no equation
Node node1 = xjep.parse("y=x+1"); // equation with variable on lhs
xjep.preprocess(node1); // sets the equation for variable y
Node node2 = xjep.parse("z=y*y");
xjep.preprocess(node2); // sets equation to variable z

// Get equation for variable y
XVariable varY = (XVariable) xjep.getVariable("y");
assertEquals("x+1.0",xjep.toString(varY.getEquation()));

// calculate the values of z using the calcVarValue method
// will also calculate value of variable y
Object val = xjep.calcVarValue("z",true);
assertEquals(16.0,val);

// Clear all the variable values
xjep.getVariableTable().clearValues();
// change value of x
xjep.setVariable("x", 4.0);
// recalculate z, also recalculates y (once)
Object val2 = xjep.calcVarValue("z",false);
assertEquals(25.0,val2);

// changing x without clearing values will use
// previously calculated results
xjep.setVariable("x", 5.0);
Object val3 = xjep.calcVarValue("z",false);
assertEquals(25.0,val3);

// But setting the flag to true recalculates 
// Subsequent equations
xjep.setVariable("x", 6.0);
Object val4 = xjep.calcVarValue("z",true);
assertEquals(49.0,val4);

The motivation behind this scheme comes into play if differentiation when partial derivatives of variables are automatically calculated.

Summary of Variable Use

Summary of use of variables in the XJep package:

Class Method Action
Jep public void addConstant(String name,Object value)
Adds a constant variable whose value can not be changed.
Jep public void addVariable(String name,Object value) Adds a variable or change the value of an existing variable. Throws exception on error.
Jep
public boolean setVariableValue(String name,Object value) Sets the value of a mutable variable. Returns false on error. New in Jep 3.5.
Jep
public Variable getVariable(String name) Returns the object representing the variable.
Jep
public Object getVariableValue(String name)
Gets the value of the variable. Does not re-calculate.
XJep
public Object calcVarValue(String name) Calculates the value of a variable from its equation.
XJep
public preprocess(Node node)
Causes the equations of variable on the lhs of an assignment equation to be set.
XVariable
public Node getEquation()
Returns the equation of a variable.
XVariable
public Object calcValue()
Calculates the value of a variable from its equation.
VariableTable
public void clearValues()
Marks all non constant variables as invalid.
top

Examples