1 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 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); 65 Object param2 = inStack.pop(); 66 Object param1 = inStack.pop(); 67 68 inStack.push(crosspower(param1, param2)); 69 } 70 71 public Object crosspower(Object param1, Object 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 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 |