1 9 10 package org.lsmp.djep.xjep; 11 12 import org.nfunk.jep.*; 13 import org.nfunk.jep.type.*; 14 15 24 public class TreeUtils { 25 28 protected static Double ZERO = new Double (0.0); 29 30 protected static Double ONE = new Double (1.0); 31 32 protected static Double MINUSONE = new Double (-1.0); 33 34 protected static Complex CZERO = new Complex(0.0,0.0); 35 36 protected static Complex CONE = new Complex(1.0,0.0); 37 38 protected static Complex CI = new Complex(0.0,1.0); 39 40 protected static Complex CMINUSONE = new Complex(-1.0,0.0); 41 42 protected static Complex CMINUSI = new Complex(0.0,-1.0); 43 44 protected static Double NAN = new Double (Double.NaN); 45 46 protected static Double PosInf = new Double (Double.POSITIVE_INFINITY); 47 48 protected static Double NegInf = new Double (Double.NEGATIVE_INFINITY); 49 50 53 public TreeUtils() {} 54 56 60 public String getName(Node node) throws IllegalArgumentException 61 { 62 if(isVariable(node)) 63 return ((ASTVarNode) node).getName(); 64 if(isFunction(node)) 65 return ((ASTFunNode) node).getName(); 66 67 throw new IllegalArgumentException ("Tried to find the name of constant node"); 68 } 69 70 71 77 78 82 public Object getValue(Node node) throws IllegalArgumentException 83 { 84 if(!isConstant(node)) throw new IllegalArgumentException ("Tried to find the value of a non constant node"); 85 return ((ASTConstant) node).getValue(); 86 } 87 88 92 public double doubleValue(Node node) throws IllegalArgumentException 93 { 94 return ((Double ) getValue(node)).doubleValue(); 95 } 96 100 public long longValue(Node node) throws IllegalArgumentException 101 { 102 return ((Number ) getValue(node)).longValue(); 103 } 104 108 public int intValue(Node node) throws IllegalArgumentException 109 { 110 return ((Number ) getValue(node)).intValue(); 111 } 112 113 117 public Complex complexValue(Node node) throws IllegalArgumentException 118 { 119 return ((Complex) getValue(node)); 120 } 121 122 125 public boolean isConstant(Node node) 126 { 127 return (node instanceof ASTConstant); 128 } 129 130 133 public boolean isReal(Node node) 134 { 135 return (node instanceof ASTConstant) 136 && ( ((ASTConstant) node).getValue() instanceof Double ); 137 } 138 139 142 public boolean isInteger(Node node) 143 { 144 if(isReal(node)) 145 { 146 Number val = (Number ) ((ASTConstant) node).getValue(); 147 double x = val.doubleValue(); 148 double xInt = Math.rint(x); 149 return x == xInt; 150 } 151 else return false; 152 } 153 154 157 public boolean isZero(Node node) 158 { 159 return ( isReal(node) 160 && ( ((ASTConstant) node).getValue().equals(ZERO)) ) 161 ||( isComplex(node) 162 && ( ((Complex) ((ASTConstant) node).getValue()).equals(CZERO,0.0) ) ); 163 } 164 165 169 170 public boolean isZero(Node node,double tol) 171 { 172 return ( isReal(node) 173 && 174 ( (((ASTConstant) node).getValue().equals(ZERO)) ) 175 || Math.abs(doubleValue(node)) < tol ) 176 ||( isComplex(node) 177 && ( ((Complex) ((ASTConstant) node).getValue()).equals(CZERO,tol) ) ); 178 } 179 180 183 public boolean isOne(Node node) 184 { 185 return ( isReal(node) 186 && ( ((ASTConstant) node).getValue().equals(ONE)) ) 187 ||( isComplex(node) 188 && ( ((Complex) ((ASTConstant) node).getValue()).equals(CONE,0.0) ) ); 189 } 190 191 194 public boolean isMinusOne(Node node) 195 { 196 return ( isReal(node) 197 && ( ((ASTConstant) node).getValue().equals(MINUSONE)) ) 198 ||( isComplex(node) 199 && ( ((Complex) ((ASTConstant) node).getValue()).equals(CMINUSONE,0.0) ) ); 200 } 201 205 206 public boolean isInfinity(Node node) 207 { 208 if(isReal(node)) 209 { 210 Double dub = (Double ) ((ASTConstant) node).getValue(); 211 return dub.isInfinite(); 212 } 213 if(isComplex(node)) 214 { 215 Complex z = (Complex) ((ASTConstant) node).getValue(); 216 return Double.isInfinite(z.re()) 217 || Double.isInfinite(z.im()); 218 } 219 return false; 220 } 221 222 225 public boolean isNaN(Node node) 226 { 227 if(isReal(node)) 228 { 229 Double dub = (Double ) ((ASTConstant) node).getValue(); 230 return dub.isNaN(); 231 } 232 if(isComplex(node)) 233 { 234 Complex z = (Complex) ((ASTConstant) node).getValue(); 235 return Double.isNaN(z.re()) 236 || Double.isNaN(z.im()); 237 } 238 return false; 239 } 240 241 244 public boolean isNegative(Node node) 245 { 246 return isReal(node) 247 && ( ((Double ) ((ASTConstant) node).getValue()).doubleValue() < 0.0 ); 248 } 249 250 253 public boolean isPositive(Node node) 254 { 255 return isReal(node) 256 && ( ((Double ) ((ASTConstant) node).getValue()).doubleValue() > 0.0 ); 257 } 258 259 262 public boolean isComplex(Node node) 263 { 264 return isConstant(node) 265 && ( ((ASTConstant) node).getValue() instanceof Complex ); 266 } 267 268 271 public boolean isVariable(Node node) 272 { 273 return (node instanceof ASTVarNode); 274 } 275 276 279 public boolean isOperator(Node node) 280 { 281 return (node instanceof ASTFunNode) && ((ASTFunNode) node).isOperator(); 282 } 283 284 public boolean isBinaryOperator(Node node) 285 { 286 if(isOperator(node)) 287 { 288 return ((XOperator) ((ASTFunNode) node).getOperator()).isBinary(); 289 } 290 return false; 291 } 292 293 public boolean isUnaryOperator(Node node) 294 { 295 if(isOperator(node)) 296 { 297 return ((XOperator) ((ASTFunNode) node).getOperator()).isUnary(); 298 } 299 return false; 300 } 301 302 305 public Operator getOperator(Node node) 306 { 307 if(isOperator(node)) 308 return ((ASTFunNode) node).getOperator(); 309 else return null; 310 } 311 312 315 public boolean isFunction(Node node) 316 { 317 return (node instanceof ASTFunNode); 318 } 319 320 323 public static Node copyChildrenIfNeeded(Node node,Node children[]) throws ParseException 324 { 325 int n=node.jjtGetNumChildren(); 326 if(n!=children.length) 327 throw new ParseException("copyChildrenIfNeeded: umber of children of node not the same as supplied children"); 328 for(int i=0;i<n;++i) 329 if(node.jjtGetChild(i) != children[i]) 330 { 331 node.jjtAddChild(children[i],i); 332 children[i].jjtSetParent(node); 333 } 334 return node; 335 } 336 337 338 static public Node[] getChildrenAsArray(Node node) 339 { 340 int n = node.jjtGetNumChildren(); 341 Node[] children = new Node[n]; 342 for(int i=0;i<n;++i) 343 children[i]=node.jjtGetChild(i); 344 return children; 345 } 346 347 public Object getCI() { return CI; } 348 public Object getCMINUSI() {return CMINUSI; } 349 public Object getCMINUSONE() {return CMINUSONE; } 350 public Object getCONE() {return CONE; } 351 public Object getCZERO() {return CZERO; } 352 public Object getMINUSONE() {return MINUSONE; } 353 public Object getONE() {return ONE; } 354 public Object getZERO() {return ZERO; } 355 public Object getNAN() { return NAN; } 356 public Object getPositiveInfinity() { return PosInf; } 357 public Object getNegativeInfinity() { return NegInf; } 358 public Object getNumber(double val) { return new Double (val); } 359 } 360 | Popular Tags |