KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************************
2
3 JEP - Java Math Expression Parser 2.24
4       December 30 2002
5       (c) Copyright 2002, Nathan Funk
6       See LICENSE.txt for license information.
7
8 *****************************************************************************/

9 package org.lsmp.djep.vectorJep.function;
10
11 import java.util.*;
12
13 import org.lsmp.djep.vectorJep.*;
14 import org.lsmp.djep.vectorJep.values.*;
15 import org.nfunk.jep.*;
16 import org.nfunk.jep.function.*;
17
18 /**
19  * An overloaded operator, either cross product or power.
20  * If the arguments are 3D vectors then treat as cross product.
21  * Otherwise treet as power.
22  * @author Rich Morris
23  * Created on 27-Jul-2003
24  */

25 public class ExteriorProduct extends PostfixMathCommand implements BinaryOperatorI
26 {
27     private Subtract sub = new Subtract();
28     private Multiply mul = new Multiply();
29     
30     public ExteriorProduct()
31     {
32         numberOfParameters = 2;
33     }
34
35     public Dimensions calcDim(Dimensions ldim,Dimensions rdim) throws ParseException
36     {
37         if(ldim.equals(Dimensions.THREE)&&rdim.equals(Dimensions.THREE))
38             return Dimensions.THREE;
39         throw new ParseException("^ only implemented for three dimensions vectors");
40     }
41
42     public MatrixValueI calcValue(
43         MatrixValueI res,
44         MatrixValueI lhs,
45         MatrixValueI rhs) throws ParseException
46     {
47         res.setEle(0,sub.sub(
48                 mul.mul(lhs.getEle(1),rhs.getEle(2)),
49                 mul.mul(lhs.getEle(2),rhs.getEle(1))));
50         res.setEle(1,sub.sub(
51                 mul.mul(lhs.getEle(2),rhs.getEle(0)),
52                 mul.mul(lhs.getEle(0),rhs.getEle(2))));
53         res.setEle(2,sub.sub(
54                 mul.mul(lhs.getEle(0),rhs.getEle(1)),
55                 mul.mul(lhs.getEle(1),rhs.getEle(0))));
56         return res;
57         
58     }
59
60     public void run(Stack inStack)
61         throws ParseException
62     {
63         checkStack(inStack); // check the stack
64

65         Object JavaDoc param2 = inStack.pop();
66         Object JavaDoc param1 = inStack.pop();
67         
68         inStack.push(crosspower(param1, param2));
69     }
70     
71     public Object JavaDoc crosspower(Object JavaDoc param1, Object JavaDoc param2)
72         throws ParseException
73     {
74         if(param1 instanceof MVector && param2 instanceof MVector)
75             return exteriorProduct((MVector) param1,(MVector) param2);
76         throw new ParseException("Sorry: can currently only do cross product on 3D vectors");
77     }
78     
79     public Object JavaDoc exteriorProduct(MVector lhs, MVector rhs) throws ParseException
80     {
81         if(!lhs.getDim().equals(Dimensions.THREE)
82          || !lhs.getDim().equals(Dimensions.THREE) )
83             throw new ParseException("Cross: Miss match in sizes ("+lhs.getDim()+","+rhs.getDim()+")");
84         MVector res = new MVector(3);
85         return calcValue(res,lhs,rhs);
86     }
87 }
88
Popular Tags