- JavaDoc

# JEP

- Basic Usage
- Variables
- Data types
- Operators
- Functions
- Advanced Features
- Grammar
- FAQ
- Version History

# Extensions

- Overview
- XJep
- Differentiation
- Vectors and Matrices
- Groups
- Version History

# DJep - differentiation of equations in JEP

Faculties for differentiation are offered by DJep class in the `org.lsmp.djep.djep` package. The DJep class should be used instead of the JEP or XJep classes and has all the features of both classes.

An interactive console applet interactive console applet illustrates the functionality of DJep.

## 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 it's use

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

    public class DiffExample {
        public static void main(String[] args) {
        /* initialisation */
        DJep j = new DJep();
        j.addStandardConstants();
        j.addStandardFunctions();
        j.addComplex();
        j.setAllowUndeclared(true);
        j.setAllowAssignment(true);
        j.setImplicitMul(true);

        // Sets up standard rules for differentiating sin(x) etc.
        j.dv.addStandardDiffRules();

        try
        {
            // parse the string
            Node node = j.parse("sin(x^2)");
            // differentiate wrt x
            Node diff = j.differentiate(node,"x");
            j.println(diff);
            // simplify
            Node simp = j.simplify(diff);
            // print
            j.println(simp);

            // This time the differentiation is specified by
            // the diff(eqn,var) function
            Node node2 = j.parse("diff(cos(x^3),x)");
            // To actually make diff do its work the
            // equation needs to be preprocessed
            Node processed = j.preprocess(node2);
            j.println(processed);
            // finally simplify
            Node simp2 = j.simplify(processed);
            j.println(simp2);
        }
        catch(ParseException e)
        {
            System.out.println("Error with parsing");
        }
    }

Note that it is usually necessary to simplify 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`".

Note also that if the `diff(eqn,var)` operator is used then the `preprocess` must be called. This method will scan 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`".

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 node1 = j.parse("y=x^5");
    j.preprocess(node1);
    // A diff operator with an equation involving a variable as first argument
    Node node2 = j.parse("diff(y,x)");
    Node simp2 = j.simplify(j.preprocess(node4));
    j.println(simp2);

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 simp4 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

    j.println(simp2);                      // prints 5.0*x^4.0
    j.getPrintVisitor().setMode(DPrintVisitor.PRINT_PARTIAL_EQNS,false);
    j.println(simp2);                      // prints dy/dx
    Node node5 = j.parse("y");
    j.println(node5);                      // prints y
    j.dpv.setPrintVariableEquations(true);
    j.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 org.lsmp.djep.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

        j.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.