1 8 package org.lsmp.djep.xjep.function; 9 10 import java.util.*; 11 12 import org.nfunk.jep.*; 13 import org.nfunk.jep.function.*; 14 15 21 public class Simpson extends SumType { 22 23 static Add add = new Add(); 24 static Multiply mul = new Multiply(); 25 26 public Simpson() 27 { 28 super("Simpson"); 29 } 30 31 32 public Object evaluate(Object elements[]) throws ParseException 33 { 34 Object ret; 35 if(elements.length % 2 != 1) 36 throw new ParseException("Simpson: there should be an odd number of ordinates, its"+elements.length); 37 38 ret = add.add(elements[0],elements[elements.length-1]); 39 for(int i=1;i<elements.length-2;i+=2) 40 { 41 ret = add.add(ret,elements[i]); 42 } 43 return ret; 44 } 45 48 public Object evaluate( 49 Node node, 50 Variable var, 51 double min, 52 double max, 53 double inc, 54 Object data, 55 ParserVisitor pv, 56 Stack stack) 57 throws ParseException { 58 int i=0; 60 double val; 61 Object [] res=new Object [(int) ((max-min)/inc)+1]; 62 for(i=0,val=min;val<=max;++i,val=min+i*inc) 63 { 64 var.setValue(new Double (val)); 65 66 node.jjtGetChild(0).jjtAccept(pv,data); 67 checkStack(stack); res[i] = stack.pop(); 69 } 70 Object ret = evaluate(res); 71 stack.push(ret); 72 return ret; 73 } 74 75 } 76 | Popular Tags |