KickJava   Java API By Example, From Geeks To Geeks.

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


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.rpe.*;
12 /**
13  * Examples using vectors and matricies
14  */

15 public class RpExample {
16     static JEP j;
17     
18     public static void main(String JavaDoc args[]) {
19         j = new JEP();
20         j.addStandardConstants();
21         j.addStandardFunctions();
22         j.addComplex();
23         j.setAllowUndeclared(true);
24         j.setImplicitMul(true);
25         j.setAllowAssignment(true);
26
27         // parse and evaluate each equation in turn
28

29         doStuff("1*2*3+4*5*6+7*8*9");
30         doAll(new String JavaDoc[]{"x1=1","x2=2","x3=3","x4=4","x5=5","x6=6","x7=7","x8=8","x9=9",
31             "x1*x2*x3+x4*x5*x6+x7*x8*x9"});
32         doAll(new String JavaDoc[]{"x=0.7","cos(x)^2+sin(x)^2"});
33         // vectors and matricies can be used with assignment
34
// doStuff("x=[1,2,3]"); // Value: [1.0,2.0,3.0]
35
// doStuff("x+x"); // Value: [2.0,4.0,6.0]
36
// doStuff("x . x"); // Value: 14.0
37
// doStuff("x^x"); // Value: [0.0,0.0,0.0]
38
// doStuff("y=[[1,2],[3,4]]"); // Value: [[1.0,2.0],[3.0,4.0]]
39
// doStuff("y * y"); // Value: [[7.0,10.0],[15.0,22.0]]
40
// accessing the elements on an array or vector
41
// doStuff("ele(x,2)"); // Value: 2.0
42
// doStuff("ele(y,[1,2])"); // Value: 2.0
43
// using differentation
44
// doStuff("x=2"); // 2.0
45
// doStuff("y=[x^3,x^2,x]"); // [8.0,4.0,2.0]
46
// doStuff("z=diff(y,x)"); // [12.0,4.0,1.0]
47
// doStuff("diff([x^3,x^2,x],x)");
48
// System.out.println("dim(z) "+((MatrixVariableI) j.getVar("z")).getDimensions());
49
}
50
51     public static void doStuff(String JavaDoc str) {
52         try {
53             Node node = j.parse(str);
54
55             RpEval rpe = new RpEval(j);
56             RpCommandList list = rpe.compile(node);
57             double res = rpe.evaluate(list);
58
59             // conversion to String
60
System.out.println(str+"\nres " + res);
61             
62             // List of commands
63
System.out.println("Commands");
64             System.out.println(list.toString());
65         }
66         catch(ParseException e) { System.out.println("Parse error "+e.getMessage()); }
67         catch(Exception JavaDoc e) { System.out.println("evaluation error "+e.getMessage()); e.printStackTrace(); }
68     }
69
70     public static void doAll(String JavaDoc str[]) {
71         try {
72             RpEval rpe = new RpEval(j);
73
74             for(int i=0;i<str.length;++i)
75             {
76                 Node node = j.parse(str[i]);
77                 RpCommandList list = rpe.compile(node);
78                 double res = rpe.evaluate(list);
79
80                 // conversion to String
81
System.out.println(str[i]+"\nres " + res);
82             
83                 // List of commands
84
System.out.println("Commands");
85                 System.out.println(list.toString());
86             }
87
88         }
89         catch(ParseException e) { System.out.println("Parse error "+e.getMessage()); }
90         catch(Exception JavaDoc e) { System.out.println("evaluation error "+e.getMessage()); e.printStackTrace(); }
91     }
92 }
93
Popular Tags