KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > matrixJep > MatrixJep


1 /* @author rich
2  * Created on 19-Dec-2003
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 package org.lsmp.djep.matrixJep;
9
10 import org.lsmp.djep.djep.*;
11 import org.lsmp.djep.djep.diffRules.*;
12 import org.nfunk.jep.*;
13 import org.nfunk.jep.function.*;
14 import org.lsmp.djep.vectorJep.values.*;
15 import org.lsmp.djep.vectorJep.function.*;
16 import org.lsmp.djep.xjep.*;
17 import org.lsmp.djep.matrixJep.function.*;
18 import org.lsmp.djep.matrixJep.nodeTypes.*;
19 /**
20  * An extension of JEP which allows advanced vector and matrix handeling and differentation.
21  *
22  * @author Rich Morris
23  * Created on 19-Dec-2003
24  */

25 public class MatrixJep extends DJep {
26
27     protected MatrixPreprocessor dec = new MatrixPreprocessor();
28     protected MatrixVariableFactory mvf = new MatrixVariableFactory();
29     protected MatrixEvaluator mev = new MatrixEvaluator();
30     
31     public MatrixJep() {
32         super();
33         nf = new MatrixNodeFactory();
34         symTab = new DSymbolTable(mvf);
35         opSet = new MatrixOperatorSet();
36         this.parser.setInitialTokenManagerState(Parser.NO_DOT_IN_IDENTIFIERS);
37
38         Operator tens = ((MatrixOperatorSet) opSet).getMList();
39         pv.addSpecialRule(tens,(PrintVisitor.PrintRulesI) tens.getPFMC());
40         addDiffRule(new PassThroughDiffRule(tens.getName(),tens.getPFMC()));
41         Operator cross = ((MatrixOperatorSet) opSet).getCross();
42         addDiffRule(new MultiplyDiffRule(cross.getName(),cross));
43         Operator dot = ((MatrixOperatorSet) opSet).getDot();
44         addDiffRule(new MultiplyDiffRule(dot.getName(),dot));
45     }
46
47     public void addStandardFunctions()
48     {
49         super.addStandardFunctions();
50         this.getFunctionTable().remove("if");
51         addFunction("pow",new Power());
52         addFunction("if",new MIf());
53         addFunction("ele",new Ele());
54     }
55
56     /** Evaluate a node. If the result is a scaler it
57      * will be unwrapped, i.e. it will return a Double and not a Scaler.
58      */

59     public Object JavaDoc evaluate(Node node) throws ParseException
60     {
61         Object JavaDoc res = mev.evaluate((MatrixNodeI) node,this);
62         if(res instanceof Scaler)
63             return ((Scaler) res).getEle(0);
64         else
65             return res;
66     }
67
68     /** Evaluate a node. Does not unwrap scalers. */
69     public Object JavaDoc evaluateRaw(Node node) throws ParseException
70     {
71         Object JavaDoc res = mev.evaluate((MatrixNodeI) node,this);
72         return res;
73     }
74
75     /** Preprocesses an equation to allow the diff and eval operators to be used. */
76     public Node preprocess(Node node) throws ParseException
77     {
78         return dec.preprocess(node,this);
79     }
80
81     /* (non-Javadoc)
82      * @see org.nfunk.jep.JEP#getValueAsObject()
83      */

84     public Object JavaDoc getValueAsObject() {
85         try
86         {
87             Object JavaDoc res = mev.evaluate((MatrixNodeI) getTopNode(),this);
88             if(res instanceof Scaler)
89                 return ((Scaler) res).getEle(0);
90             else
91                 return res;
92         }
93         catch(Exception JavaDoc e)
94         {
95             this.errorList.addElement("Error during evaluation:");
96             this.errorList.addElement(e.getMessage());
97             return null;
98         }
99     }
100 }
101
Popular Tags