KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > vectorJep > function > Ele


1 /* @author rich
2  * Created on 15-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.vectorJep.function;
9
10 import java.util.*;
11 import org.lsmp.djep.vectorJep.Dimensions;
12 import org.lsmp.djep.vectorJep.values.*;
13 import org.nfunk.jep.ParseException;
14 import org.nfunk.jep.function.PostfixMathCommand;
15
16 /**
17  * ele(x,i) returns the i-th element of x.
18  * @author Rich Morris
19  * Created on 15-Nov-2003
20  */

21 public class Ele extends PostfixMathCommand implements BinaryOperatorI {
22
23     public Ele() {
24         super();
25         numberOfParameters = 2;
26     }
27
28     public Dimensions calcDim(Dimensions ldim, Dimensions rdim)
29         throws ParseException {
30         return Dimensions.ONE;
31     }
32
33     public MatrixValueI calcValue(MatrixValueI res,
34         MatrixValueI param1,MatrixValueI param2) throws ParseException
35     {
36 // Number num = (Number) rhs.getEle(0);
37
// res.setEle(0,lhs.getEle(num.intValue()-1));
38

39         if(param1 instanceof MVector)
40         {
41             if(param2 instanceof Scaler)
42             {
43                 int index = ((Double JavaDoc) param2.getEle(0)).intValue()-1;
44                 Object JavaDoc val = ((MVector) param1).getEle(index);
45                 res.setEle(0,val);
46             }
47             else throw new ParseException("Bad second argument to ele, expecting a double "+param2.toString());
48         }
49         else if(param1 instanceof Matrix)
50         {
51             if(param2 instanceof MVector)
52             {
53                 MVector vec = (MVector) param2;
54                 if(vec.getDim().equals(Dimensions.TWO))
55                 {
56                     Double JavaDoc d1 = (Double JavaDoc) vec.getEle(0);
57                     Double JavaDoc d2 = (Double JavaDoc) vec.getEle(1);
58                     Object JavaDoc val = ((Matrix) param1).getEle(d1.intValue()-1,d2.intValue()-1);
59                     res.setEle(0,val);
60                 }
61             }
62             else throw new ParseException("Bad second argument to ele, expecting [i,j] "+param2.toString());
63         }
64         else if(param1 instanceof Tensor)
65         {
66             throw new ParseException("Sorry don't know how to find elements for a tensor");
67         }
68         else
69             throw new ParseException("ele requires a vector matrix or tensor for first argument it has "+param1.toString());
70         return res;
71     }
72     
73     public void run(Stack stack) throws ParseException
74     {
75         checkStack(stack); // check the stack
76

77         Object JavaDoc param1,param2;
78      
79         // get the parameter from the stack
80

81         param2 = stack.pop();
82         param1 = stack.pop();
83                 
84         if(param1 instanceof MVector)
85         {
86             if(param2 instanceof Double JavaDoc)
87             {
88                 Object JavaDoc val = ((MVector) param1).getEle(((Double JavaDoc) param2).intValue()-1);
89                 stack.push(val);
90                 return;
91             }
92             else throw new ParseException("Bad second argument to ele, expecting a double "+param2.toString());
93         }
94         else if(param1 instanceof Matrix)
95         {
96             if(param2 instanceof MVector)
97             {
98                 MVector vec = (MVector) param2;
99                 if(vec.getDim().equals(Dimensions.TWO))
100                 {
101                     Double JavaDoc d1 = (Double JavaDoc) vec.getEle(0);
102                     Double JavaDoc d2 = (Double JavaDoc) vec.getEle(1);
103                     Object JavaDoc val = ((Matrix) param1).getEle(d1.intValue()-1,d2.intValue()-1);
104                     stack.push(val);
105                     return;
106                 }
107             }
108             else throw new ParseException("Bad second argument to ele, expecting [i,j] "+param2.toString());
109         }
110         else if(param1 instanceof Tensor)
111         {
112             throw new ParseException("Sorry don't know how to find elements for a tensor");
113         }
114         throw new ParseException("ele requires a vector matrix or tensor for first argument it has "+param1.toString());
115     }
116 }
117
Popular Tags