KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djepExamples > DiffExample


1 /* @author rich
2  * Created on 26-Feb-2004
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
9 package org.lsmp.djepExamples;
10 import org.nfunk.jep.*;
11 import org.lsmp.djep.xjep.*;
12 import org.lsmp.djep.djep.*;
13
14 /**
15  * Examples using differentation
16  */

17 public class DiffExample {
18
19     public static void main(String JavaDoc args[])
20     {
21         /* initilisation */
22         DJep j = new DJep();
23         j.addStandardConstants();
24         j.addStandardFunctions();
25         j.addComplex();
26         j.setAllowUndeclared(true);
27         j.setAllowAssignment(true);
28         j.setImplicitMul(true);
29         j.addStandardDiffRules();
30
31         try
32         {
33             // parse the string
34
Node node = j.parse("sin(x^2)");
35             // differentiate wrt x
36
Node diff = j.differentiate(node,"x");
37             // print
38
j.println(diff);
39             // simplify
40
Node simp = j.simplify(diff);
41             // print
42
j.println(simp);
43             
44             // This time the differentation is specified by
45
// the diff(eqn,var) function
46
Node node2 = j.parse("diff(cos(x^3),x)");
47             // To actually make diff do its work the
48
// equation needs to be preprocessed
49
Node processed = j.preprocess(node2);
50             j.println(processed);
51             // finally simplify
52
Node simp2 = j.simplify(processed);
53             j.println(simp2);
54             
55             // Now combine assignment and differentation
56
Node node3 = j.parse("y=x^5");
57             j.preprocess(node3);
58             Node node4 = j.parse("diff(y^2+x,x)");
59             Node simp3 = j.simplify(j.preprocess(node4));
60
61             j.println(simp3); // default printing will be 2*y*5*x^4+1
62

63             PrintVisitor pv = j.getPrintVisitor();
64             pv.setMode(DPrintVisitor.PRINT_PARTIAL_EQNS,false);
65             j.println(simp3); // no expansion will be 2*y*dy/dx+1
66

67             pv.setMode(DPrintVisitor.PRINT_PARTIAL_EQNS,true);
68             pv.setMode(DPrintVisitor.PRINT_VARIABLE_EQNS,true);
69             j.println(simp3); // full expansion: 2*x^5*5*x^4+1
70

71             pv.setMode(DPrintVisitor.PRINT_VARIABLE_EQNS,false);
72
73 // Node node5 = j.parse("y");
74
// j.println(node5);
75
// ((DPrintVisitor)j.getPrintVisitor()).setPrintVariableEquations(true);
76
// j.println(node5);
77

78             j.getSymbolTable().setVarValue("x",new Double JavaDoc(5));
79             System.out.println(j.evaluate(simp3));
80             j.evaluate(node3);
81             System.out.println(j.getSymbolTable().getVar("y").getValue());
82             j.getSymbolTable().setVarValue("x",new Double JavaDoc(0));
83             System.out.println(j.evaluate(simp));
84             
85             Node node10 = j.parse("x=3");
86             Node node11 = j.preprocess(node10);
87             System.out.println(j.evaluate(node11));
88             Node node12 = j.parse("y=x^2");
89             Node node13 = j.preprocess(node12);
90             System.out.println(j.evaluate(node13));
91             Node node14 = j.parse("z=diff(y,x)");
92             Node node15 = j.simplify(j.preprocess(node14));
93             System.out.println(j.evaluate(node15));
94
95             // If a variable is changed then any expresion tree
96
// it depends on needs to be re-evaluated to bring
97
// values of other variables upto date
98
j.setVarValue("x",new Double JavaDoc(4));
99             System.out.println(j.evaluate(node13));
100             System.out.println(j.evaluate(node15));
101             System.out.println("z: "+j.getVarValue("z").toString());
102             
103             // the findVarValue method will automatically
104
// re-calculate the value of variables specified by
105
// equations if needed. However a lazy
106

107             j.setVarValue("x",new Double JavaDoc(5));
108             System.out.println("j.setVarValue(\"x\",new Double(5));");
109             System.out.println("j.findVarValue(y): "+j.calcVarValue("y").toString());
110             System.out.println("j.findVarValue(z): "+j.calcVarValue("z").toString());
111
112             // if j.getSymbolTable().clearValues();
113
// is called before values of equations are set
114
// then the values of intermediate equations
115
// are automatically calculated, so you can jump
116
// straight to the chase: no need to calculate
117
// y explititly to find the value of z.
118
j.getSymbolTable().clearValues();
119             j.setVarValue("x",new Double JavaDoc(6));
120             System.out.println("j.setVarValue(\"x\",new Double(6));");
121             System.out.println("j.findVarValue(z): "+j.calcVarValue("z").toString());
122
123             j.getSymbolTable().clearValues();
124             j.setVarValue("x",new Double JavaDoc(7));
125             System.out.println(j.evaluate(node15));
126             System.out.println("z: "+j.getVarValue("z").toString());
127         }
128         catch(ParseException e)
129         {
130             System.out.println("Error with parsing");
131         }
132         catch(Exception JavaDoc e)
133         {
134             System.out.println("Error with evaluation");
135         }
136     }
137 }
138
Popular Tags