KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > matrixJep > function > MIf


1 /* @author rich
2  * Created on 18-Nov-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.function;
9 import org.nfunk.jep.function.*;
10 import org.lsmp.djep.matrixJep.*;
11 import org.lsmp.djep.matrixJep.nodeTypes.*;
12 import org.lsmp.djep.vectorJep.*;
13 import org.lsmp.djep.vectorJep.function.NaryOperatorI;
14 import org.lsmp.djep.vectorJep.values.*;
15 import org.nfunk.jep.*;
16 import org.nfunk.jep.type.*;
17
18 /**
19  * The if(condExpr,posExpr,negExpr) function.
20  * The value of trueExpr will be returned if condExpr is >0 (true)
21  * and value of negExpr will be returned if condExpr is &lt;= 0 (false).
22  * <p>
23  * This function performs lazy evaluation so that
24  * only posExpr or negExpr will be evaluated.
25  * For Complex numbers only the real part is used.
26  * <p>
27  * An alternate form if(condExpr,posExpr,negExpr,zeroExpr)
28  * is also availiable. Note most computations
29  * are caried out over floating point doubles so
30  * testing for zero can be dangerous.
31  * <p>
32  * This function implements the SpecialEvaluationI interface
33  * so that it handles seting the value of a variable.
34  * @author Rich Morris
35  * Created on 18-Nov-2003
36  */

37 public class MIf extends PostfixMathCommand implements NaryOperatorI, MatrixSpecialEvaluationI
38 {
39     public MIf() {
40         super();
41         numberOfParameters = -1;
42     }
43
44     /** Find the dimension of this node. */
45     public Dimensions calcDim(Dimensions dims[]) throws ParseException
46     {
47         int num =dims.length;
48         if( num < 3 || num > 4)
49             throw new ParseException("If operator must have 3 or 4 arguments.");
50
51         Dimensions condDim = dims[0];
52         if(!condDim.equals(Dimensions.ONE))
53             throw new ParseException("First argument of if opperator must be 0 dimensional");
54         Dimensions posDim = dims[1];
55         for(int i=2;i<num;++i)
56             if(!posDim.equals(dims[i]))
57                 throw new ParseException("Dimensions for each argument of if must be equal");
58         return posDim;
59     }
60
61     /** This method should not be called.
62      * Use {@link #evaluate} instead.
63      */

64     public MatrixValueI calcValue(MatrixValueI res,
65         MatrixValueI inputs[]) throws ParseException
66     {
67         throw new ParseException("Called calc value for If");
68     }
69
70     /**
71      * Evaluate the node, uses lazy evaluation.
72      */

73     public MatrixValueI evaluate(MatrixNodeI node,MatrixEvaluator visitor,MatrixJep j) throws ParseException
74     {
75         int num =node.jjtGetNumChildren();
76         if( num < 3 || num > 4)
77             throw new ParseException("If operator must have 3 or 4 arguments.");
78
79         // get value of argument
80

81         MatrixValueI cond = (MatrixValueI) node.jjtGetChild(0).jjtAccept(visitor,null);
82         Object JavaDoc condVal = cond.getEle(0);
83         // convert to double
84
double val;
85         if(condVal instanceof Double JavaDoc)
86         {
87             val = ((Double JavaDoc) condVal).doubleValue();
88         }
89         else if(condVal instanceof Complex)
90         {
91             val = ((Complex) condVal).re();
92         }
93         else
94             throw new ParseException("Condition in if operator must be double or complex");
95         MatrixValueI res;
96         if(val>0.0)
97         {
98             res = (MatrixValueI) node.jjtGetChild(1).jjtAccept(visitor,null);
99         }
100         else if(num ==3 || val <0.0)
101         {
102             res = (MatrixValueI) node.jjtGetChild(2).jjtAccept(visitor,null);
103         }
104         else
105         {
106             res = (MatrixValueI) node.jjtGetChild(3).jjtAccept(visitor,null);
107         }
108         MatrixValueI mvalue = node.getMValue();
109         mvalue.setEles(res);
110         return mvalue;
111     }
112 }
113
Popular Tags