KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* @author rich
2  * Created on 26-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 org.lsmp.djep.vectorJep.*;
11 import org.lsmp.djep.vectorJep.values.*;
12 import org.nfunk.jep.ParseException;
13 import org.nfunk.jep.function.*;
14 import java.util.*;
15 /**
16  * An overloaded power function, if both arguments are vectors returns
17  * the exteriour product, else return standard power.
18  * @author Rich Morris
19  * Created on 26-Nov-2003
20  */

21 public class MPower extends PostfixMathCommand implements BinaryOperatorI
22 {
23     private static Power pow = new Power();
24     private static ExteriorProduct cross = new ExteriorProduct();
25
26     public MPower() {
27         super();
28     }
29     public Dimensions calcDim(Dimensions ldim,Dimensions rdim) throws ParseException
30     {
31         if(ldim.equals(Dimensions.ONE) && rdim.equals(Dimensions.ONE))
32             return Dimensions.ONE;
33         if(ldim.equals(Dimensions.THREE) && rdim.equals(Dimensions.THREE))
34             return Dimensions.THREE;
35         throw new ParseException("Power: both sides must be either 0 dimensional or 3D vectors");
36     }
37
38     public MatrixValueI calcValue(
39         MatrixValueI res,
40         MatrixValueI lhs,
41         MatrixValueI rhs) throws ParseException
42     {
43         if(lhs.getDim().equals(Dimensions.ONE)
44          && rhs.getDim().equals(Dimensions.ONE))
45         {
46             res.setEle(0,pow.power(lhs.getEle(0),rhs.getEle(0)));
47             return res;
48         }
49         if(lhs.getDim().equals(Dimensions.THREE)
50          && rhs.getDim().equals(Dimensions.THREE))
51         {
52             return cross.calcValue(res,lhs,rhs);
53         }
54         throw new ParseException("Power: both sides must be either 0 dimensional or 3D vectors");
55     }
56     
57     public void run(Stack inStack)
58         throws ParseException
59     {
60         checkStack(inStack); // check the stack
61

62         Object JavaDoc param2 = inStack.pop();
63         Object JavaDoc param1 = inStack.pop();
64         
65         if(param1 instanceof MVector && param2 instanceof MVector)
66             inStack.push(cross.crosspower(param1, param2));
67         else
68             inStack.push(pow.power(param1,param2));
69     }
70
71 }
72
Popular Tags