1 8 package org.lsmp.djep.matrixJep.function; 9 import org.nfunk.jep.function.*; 10 import org.lsmp.djep.matrixJep.*; 11 import org.lsmp.djep.matrixJep.nodeTypes.*; 12 import org.lsmp.djep.vectorJep.*; 13 import org.lsmp.djep.vectorJep.function.NaryOperatorI; 14 import org.lsmp.djep.vectorJep.values.*; 15 import org.nfunk.jep.*; 16 import org.nfunk.jep.type.*; 17 18 37 public class MIf extends PostfixMathCommand implements NaryOperatorI, MatrixSpecialEvaluationI 38 { 39 public MIf() { 40 super(); 41 numberOfParameters = -1; 42 } 43 44 45 public Dimensions calcDim(Dimensions dims[]) throws ParseException 46 { 47 int num =dims.length; 48 if( num < 3 || num > 4) 49 throw new ParseException("If operator must have 3 or 4 arguments."); 50 51 Dimensions condDim = dims[0]; 52 if(!condDim.equals(Dimensions.ONE)) 53 throw new ParseException("First argument of if opperator must be 0 dimensional"); 54 Dimensions posDim = dims[1]; 55 for(int i=2;i<num;++i) 56 if(!posDim.equals(dims[i])) 57 throw new ParseException("Dimensions for each argument of if must be equal"); 58 return posDim; 59 } 60 61 64 public MatrixValueI calcValue(MatrixValueI res, 65 MatrixValueI inputs[]) throws ParseException 66 { 67 throw new ParseException("Called calc value for If"); 68 } 69 70 73 public MatrixValueI evaluate(MatrixNodeI node,MatrixEvaluator visitor,MatrixJep j) throws ParseException 74 { 75 int num =node.jjtGetNumChildren(); 76 if( num < 3 || num > 4) 77 throw new ParseException("If operator must have 3 or 4 arguments."); 78 79 81 MatrixValueI cond = (MatrixValueI) node.jjtGetChild(0).jjtAccept(visitor,null); 82 Object condVal = cond.getEle(0); 83 double val; 85 if(condVal instanceof Double ) 86 { 87 val = ((Double ) condVal).doubleValue(); 88 } 89 else if(condVal instanceof Complex) 90 { 91 val = ((Complex) condVal).re(); 92 } 93 else 94 throw new ParseException("Condition in if operator must be double or complex"); 95 MatrixValueI res; 96 if(val>0.0) 97 { 98 res = (MatrixValueI) node.jjtGetChild(1).jjtAccept(visitor,null); 99 } 100 else if(num ==3 || val <0.0) 101 { 102 res = (MatrixValueI) node.jjtGetChild(2).jjtAccept(visitor,null); 103 } 104 else 105 { 106 res = (MatrixValueI) node.jjtGetChild(3).jjtAccept(visitor,null); 107 } 108 MatrixValueI mvalue = node.getMValue(); 109 mvalue.setEles(res); 110 return mvalue; 111 } 112 } 113 | Popular Tags |