1 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 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 39 if(param1 instanceof MVector) 40 { 41 if(param2 instanceof Scaler) 42 { 43 int index = ((Double ) param2.getEle(0)).intValue()-1; 44 Object 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 d1 = (Double ) vec.getEle(0); 57 Double d2 = (Double ) vec.getEle(1); 58 Object 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); 77 Object param1,param2; 78 79 81 param2 = stack.pop(); 82 param1 = stack.pop(); 83 84 if(param1 instanceof MVector) 85 { 86 if(param2 instanceof Double ) 87 { 88 Object val = ((MVector) param1).getEle(((Double ) 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 d1 = (Double ) vec.getEle(0); 102 Double d2 = (Double ) vec.getEle(1); 103 Object 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 |