KickJava   Java API By Example, From Geeks To Geeks.

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


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.vectorJep.*;
12 import org.lsmp.djep.xjep.*;
13 /**
14  * Examples using vectors and matricies
15  */

16 public class VectorPrint {
17     static XJep j;
18 // static PrintVisitor pv;
19

20     public static void main(String JavaDoc args[]) {
21         j = new XJep(new VectorJep());
22
23         j.addStandardConstants();
24         j.addStandardFunctions();
25         //j.addFunction("ele",new Ele());
26
j.addComplex();
27         j.setAllowUndeclared(true);
28         j.setImplicitMul(true);
29         j.setAllowAssignment(true);
30
31         // parse and evaluate each equation in turn
32

33         doStuff("3^2");
34         doStuff("[1,2,3]"); // Value: [1.0,2.0,3.0]
35
doStuff("[1,2,3].[4,5,6]"); // Value: 32.0
36
doStuff("[1,2,3]^^[4,5,6]"); // Value: [-3.0,6.0,-3.0]
37
doStuff("[1,2,3]+[4,5,6]"); // Value: [5.0,7.0,9.0]
38
doStuff("[[1,2],[3,4]]"); // Value: [[1.0,2.0],[3.0,4.0]]
39
doStuff("[[1,2],[3,4]]*[1,0]"); // Value: [1.0,3.0]
40
doStuff("[1,0]*[[1,2],[3,4]]"); // Value: [1.0,2.0]
41
doStuff("x=[1,2,3]"); // Value: [1.0,2.0,3.0]
42
doStuff("x+x"); // Value: [2.0,4.0,6.0]
43
doStuff("x . x"); // Value: 14.0
44
doStuff("x^^x"); // Value: [0.0,0.0,0.0]
45
doStuff("ele(x,2)"); // Value: 2.0
46
doStuff("y=[[1,2],[3,4]]"); // Value: [[1.0,2.0],[3.0,4.0]]
47
doStuff("y * y"); // Value: [[7.0,10.0],[15.0,22.0]]
48
doStuff("ele(y,[1,2])"); // Value: 2.0
49
}
50
51     public static void doStuff(String JavaDoc str) {
52         try {
53             Node node = j.parse(str);
54             Object JavaDoc value = j.evaluate(node);
55             j.print(node);
56             System.out.println("\tvalue " + value.toString());
57         }
58         catch(ParseException e) { System.out.println("Parse error "+e.getMessage()); }
59         catch(Exception JavaDoc e) {
60             System.out.println("evaluation error "+e.getMessage());
61             e.printStackTrace(); }
62     }
63 }
64
Popular Tags