KickJava   Java API By Example, From Geeks To Geeks.

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


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

15 public class VectorExample {
16     static VectorJep j;
17     
18     public static void main(String JavaDoc args[]) {
19         j = new VectorJep();
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]"); // Value: [1.0,2.0,3.0]
30
doStuff("[1,2,3].[4,5,6]"); // Value: 32.0
31
doStuff("[1,2,3]^^[4,5,6]"); // Value: [-3.0,6.0,-3.0]
32
doStuff("[1,2,3]+[4,5,6]"); // Value: [5.0,7.0,9.0]
33
doStuff("[[1,2],[3,4]]"); // Value: [[1.0,2.0],[3.0,4.0]]
34
doStuff("[[1,2],[3,4]]*[1,0]"); // Value: [1.0,3.0]
35
doStuff("[1,0]*[[1,2],[3,4]]"); // Value: [1.0,2.0]
36
doStuff("[[1,2],[3,4]]*[[1,2],[3,4]]"); // Value: [[7.0,10.0],[15.0,22.0]]
37
doStuff("x=[1,2,3]"); // Value: [1.0,2.0,3.0]
38
doStuff("x+x"); // Value: [2.0,4.0,6.0]
39
doStuff("x.x"); // Value: 14.0
40
doStuff("x^x"); // Value: [0.0,0.0,0.0]
41
doStuff("ele(x,2)"); // Value: 2.0
42
doStuff("y=[[1,2],[3,4]]"); // Value: [[1.0,2.0],[3.0,4.0]]
43
doStuff("y * y"); // Value: [[7.0,10.0],[15.0,22.0]]
44
doStuff("ele(y,[1,2])"); // Value: 2.0
45
}
46
47     public static void doStuff(String JavaDoc str) {
48         try {
49             Node node = j.parse(str);
50             Object JavaDoc value = j.evaluate(node);
51             System.out.println(str + "\tvalue " + value.toString());
52         }
53         catch(ParseException e) { System.out.println("Parse error "+e.getMessage()); }
54         catch(Exception JavaDoc e) { System.out.println("evaluation error "+e.getMessage()); e.printStackTrace(); }
55     }
56 }
57
Popular Tags