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 16 import org.nfunk.jep.*; 17 18 23 public class MDot extends MMultiply implements BinaryOperatorI 24 { 25 public MDot() { 26 numberOfParameters = 2; 27 } 28 public Dimensions calcDim(Dimensions l,Dimensions r) { 29 if(l.equals(r) && l.is1D()) return Dimensions.ONE; 30 else return null; 31 } 32 33 39 public MatrixValueI calcValue(MatrixValueI res,MatrixValueI lhs,MatrixValueI rhs) throws ParseException 40 { 41 return calcValue((Scaler) res,(MVector) lhs,(MVector) rhs); 42 } 43 44 public Scaler calcValue(Scaler res,MVector lhs,MVector rhs) throws ParseException 45 { 46 int len = lhs.getNumEles(); 47 Object val = mul(lhs.getEle(0),rhs.getEle(0)); 48 for(int i=1;i<len;++i) 49 val = add.add(val,mul(lhs.getEle(i),rhs.getEle(i))); 50 res.setEle(0,val); 51 return res; 52 } 53 54 public void run(Stack stack) throws ParseException 55 { 56 checkStack(stack); 58 Object param1,param2; 59 60 62 param2 = stack.pop(); 63 param1 = stack.pop(); 64 65 67 stack.push(dot(param1, param2)); 68 69 return; 70 } 71 72 73 public Object dot(Object param1, Object param2) throws ParseException 74 { 75 if(param1 instanceof MVector && param2 instanceof MVector) 76 return dot((MVector) param1,(MVector) param2); 77 else 78 return super.mul(param1,param2); 79 } 80 81 82 public Object dot(MVector lhs, MVector rhs) throws ParseException 83 { 84 if(!lhs.getDim().equals(rhs.getDim())) throw new ParseException("Dot: Miss match in sizes ("+lhs.getDim()+","+rhs.getDim()+")"); 85 Scaler res = new Scaler(); 86 calcValue(res,lhs,rhs); 87 return res.getEle(0); 88 } 89 } 90 | Popular Tags |