KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > djep > DJep


1 /* @author rich
2  * Created on 16-Nov-2003
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.lsmp.djep.djep;
9 import org.lsmp.djep.xjep.*;
10 import org.nfunk.jep.*;
11 /**
12  * Adds differentation facilities to JEP.
13  * For example
14  * <pre>
15  * DJep j = new DJep();
16  * j.addStandardDiffRules();
17  * ....
18  * Node node = j.parse("x^3");
19  * Node diff = j.differentiate(node,"x");
20  * Node simp = j.simplify(diff);
21  * j.println(simp);
22  * Node node2 = j.parse("diff(x^4,x)");
23  * Node proc = j.preprocess(node2);
24  * Node simp2 = j.simplify(proc);
25  * j.println(simp2);
26  * </pre>
27  * @author Rich Morris
28  * Created on 16-Nov-2003
29  */

30 public class DJep extends XJep {
31     protected DifferentiationVisitor dv = new DifferentiationVisitor(this);
32     /**
33      * Standard constructor.
34      * Use this instead of JEP or XJep if differentation facilities are required.
35      */

36     public DJep()
37     {
38         this.pv = new DPrintVisitor();
39 // this.vf = ;
40
this.symTab = new DSymbolTable(new DVariableFactory());
41
42         addFunction("diff",new Diff());
43     }
44     /**
45      * Differentiate an equation with respect to a variable.
46      * @param node top node of the expresion tree to differentiate.
47      * @param name differentiate with respect to this variable.
48      * @return the top node of a new parse tree representing the derivative.
49      * @throws ParseException if for some reason equation cannot be differentiated,
50      * ususaly if it has not been taught how to differentiate a particular function.
51      */

52     public Node differentiate(Node node,String JavaDoc name) throws ParseException
53     {
54         return dv.differentiate(node,name,this);
55     }
56     protected DJep(DJep j)
57     {
58         super((XJep) j);
59         this.dv=j.dv;
60     }
61
62     public XJep newInstance()
63     {
64         DJep newJep = new DJep(this);
65         return newJep;
66     }
67     public XJep newInstance(SymbolTable st)
68     {
69         DJep newJep = new DJep(this);
70         newJep.symTab = st;
71         return newJep;
72     }
73
74     /**
75      * Returns the visitor used for differentiation. Allows more advanced functions.
76      */

77     public DifferentiationVisitor getDifferentationVisitor() { return dv; }
78     /**
79      * Adds the standard set of differentation rules.
80      * @return false if there was a parse error.
81      */

82     public boolean addStandardDiffRules() { return dv.addStandardDiffRules(); }
83     //public DPrintVisitor getDPrintVisitor() { return (DPrintVisitor) pv; }
84

85     /**
86      * Adds a rule with instruction on how to differentiate a function.
87      * @param rule
88      */

89     public void addDiffRule(DiffRulesI rule) {
90         dv.addDiffRule(rule);
91     }
92
93 }
94
Popular Tags