KickJava   Java API By Example, From Geeks To Geeks.

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


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
13 /**
14  * Examples using differentation
15  */

16 public class XJepExample {
17
18     public static void main(String JavaDoc args[])
19     {
20         /* initilisation */
21         XJep j = new XJep();
22         j.addStandardConstants();
23         j.addStandardFunctions();
24         j.addComplex();
25         j.setAllowUndeclared(true);
26         j.setAllowAssignment(true);
27         j.setImplicitMul(true);
28
29         try
30         {
31             Node node10 = j.parse("x=3");
32             Node node11 = j.preprocess(node10);
33             System.out.println(j.evaluate(node11));
34             Node node12 = j.parse("y=x^2");
35             Node node13 = j.preprocess(node12);
36             System.out.println(j.evaluate(node13));
37             Node node14 = j.parse("z=y+x");
38             Node node15 = j.simplify(j.preprocess(node14));
39             System.out.println(j.evaluate(node15));
40
41             // If a variable is changed then any expresion tree
42
// it depends on needs to be re-evaluated to bring
43
// values of other variables upto date
44
j.setVarValue("x",new Double JavaDoc(4));
45             System.out.println(j.evaluate(node13));
46             System.out.println(j.evaluate(node15));
47             System.out.println("z: "+j.getVarValue("z").toString());
48             
49             // the findVarValue method will automatically
50
// re-calculate the value of variables specified by
51
// equations if needed. However a lazy
52

53             j.setVarValue("x",new Double JavaDoc(5));
54             System.out.println("j.setVarValue(\"x\",new Double(5));");
55             System.out.println("j.findVarValue(y): "+j.calcVarValue("y").toString());
56             System.out.println("j.findVarValue(z): "+j.calcVarValue("z").toString());
57
58             // if j.getSymbolTable().clearValues();
59
// is called before values of equations are set
60
// then the values of intermediate equations
61
// are automatically calculated, so you can jump
62
// straight to the chase: no need to calculate
63
// y explititly to find the value of z.
64
j.getSymbolTable().clearValues();
65             j.setVarValue("x",new Double JavaDoc(6));
66             System.out.println("j.setVarValue(\"x\",new Double(6));");
67             System.out.println("j.findVarValue(z): "+j.calcVarValue("z").toString());
68
69             j.getSymbolTable().clearValues();
70             j.setVarValue("x",new Double JavaDoc(7));
71             System.out.println(j.evaluate(node15));
72             System.out.println("z: "+j.getVarValue("z").toString());
73             
74             // now see if reentrancy works
75

76             j.restartParser("x=1; // semi colon; in comment \ny=2; z=3;");
77             Node node21;
78             while((node21 = j.continueParsing()) != null)
79                 j.println(node21);
80         }
81         catch(ParseException e)
82         {
83             System.out.println("Error with parsing");
84         }
85         catch(Exception JavaDoc e)
86         {
87             System.out.println("Error with evaluation");
88         }
89     }
90 }
91
Popular Tags