DJep - differentiation of equations in Jep


Faculties for differentiation are offered by DJep class in the com.singularsys.extensions.djep package. The DJep class should be used instead of the Jep and has all the features Jep and the additional features of XJep.

Usage

There are two main ways differentiation can be used:

  1. Using the differentiate(Node node,String name) method of DJep.
  2. Using the diff operator in an equation.

The following code gives an example of the first method:

DJep jep = new DJep();
// Sets up standard rules for differentiating sin(x) etc.
jep.addStandardDiffRules();
jep.reinitializeComponents();
// parse the string
Node node = jep.parse("sin(x^2)");
// differentiate wrt x
Node diff = jep.differentiate(node,"x");
jep.println(diff);
// cleanup
Node cleaned = jep.clean(diff);
// print
jep.println(cleaned);

Note that it is usually necessary to clean up an expression after it has been differentiated. This is because the algorithm works by repeated applications of the sum, product, quotient and chain rules. Hence the derivative of x^2 will be 2*x^1 which can be simplified to 2*x.

The second method uses the diff(f,x) operator in an expression. This operator is processed using the XJep.preprocess(Node) method. This scans the equation looking for any instances of diff when it encounters one it will differentiate the first argument with respect to the second argument, which must be a variable. For example preprocessing will convert diff(x^3,x) to 3*x^2.

Node node2 = jep.parse("diff(cos(x^3),x)");
// To actually make diff do its work the
// equation needs to be preprocessed
Node processed = jep.preprocess(node2);
jep.println(processed);
// cleanup
Node cleaned2 = jep.clean(processed);
jep.println(cleaned2);

The diff operator can be used several times in an expression allowing higher derivatives to be used, for example diff(diff(x^2*y,y),x) is allowed.

Assignment and differentiation

Differentiation can be combined with assignment so it is possible to set the equation of a variable using y=x^5 and then differentiate it using diff(y,x):

// Combine assignment and differentiation
Node node3 = jep.parse("y=x^5");
jep.preprocess(node3);
// A diff operator with an equation involving a variable as first argument
Node node4 = jep.parse("diff(y,x)");
Node preproc4 = jep.preprocess(node4);
Node cleaned4 = jep.clean(preproc4);
jep.println(cleaned4);

When diff(y,x) is encountered during preprocessing and y is a variable with an equation then special PartialDerivative variable {dy/dx} is created and its equation calculated from the equation for y. In the above example the variable {dy/dx} would have equation 5*x^4, and cleaned4 would be just contain a reference to the {dy/dx} variable. Things work correctly for more complicated expressions like diff(y^2+x,x) which would become 2*y*{dy/dx}+1.

A slight change is made to the printing routines which can allow printing of the equations for normal variable (default: off) and partial derivatives (default: on). Hence

jep.println(preproc);                      // prints 5.0*x^4.0
jep.getPrintVisitor().setMode(DPrintVisitor.PRINT_PARTIAL_EQNS,false);
jep.println(preproc);                      // prints dy/dx
    
Node node5 = jep.parse("y");
jep.println(node5);                      // prints y
jep.getPrintVisitor().setMode(DPrintVisitor.PRINT_VARIABLE_EQNS,true);
jep.println(node5);                        // prints x^5

Differentiation of functions

The chain rule is used for differentiating functions diff(f(y),x) -> diff(f,y)*diff(y,x) and the instructions for differentiating a function are specified by objects which implement the DiffRulesI interface. A variety of different classes are provided in the com.singularsys.extensions.djep.diffRules package which allow specific rules to be created. A rule is added using the addDiffRule method and the standard rules are added using addStandardDiffRules. For example

    jep.addDiffRule(new MacroDiffRules(j,"cos","-sin(x)"))

adds the rule for differentiating cos which is specified by the string -sin(x). There are several other ways rules can be constructed which are discussed in the Javadoc.

Matrices and Differentiation

Vector and Matrices can be combined with differentiation by using the com.singularsys.extensions.matrixdiff package. This includes the class com.singularsys.extensions.matrixdiff.MDJep which extends the DJep class and allows for variables to have dimensions.

MDJep djep = new MDJep();
djep.addStandardDiffRules();
djep.reinitializeComponents();

// Expression with vectors and dot product  
String s = "[x,x^2,x^3].[1,x,x^2]";
Node n = djep.parse(s);
Node dn = djep.differentiate(n, "x");
djep.println(dn);
Node clean=djep.clean(dn);
djep.println(clean);

Examples

top