org.lsmp.djep.xjep.function.Trapezium Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jep Show documentation
Show all versions of jep Show documentation
JEP is a Java library for parsing and evaluating mathematical expressions.
/* @author rich
* Created on 18-Nov-2003
*/
package org.lsmp.djep.xjep.function;
import org.nfunk.jep.*;
import org.nfunk.jep.function.*;
/**
* The trapezium rule for approximation to a definite integral.
* h * (y0 + yn + 2(y1+y2+...+y_(n-1)) /2
* where h = (xn-x0)/n, yi = f(xi)
* Trapezium(x^2,x,0,10,0.5)
* finds an approximation for int(x^2) where x runs from 0 to 10 in steps of
* h=0.5.
*
* @author Rich Morris
* Created on 10-Sept-2004
*/
public class Trapezium extends SumType {
static Add add = new Add();
static Multiply mul = new Multiply();
static Double HALF = new Double(1.0/2);
static Double THIRD = new Double(1.0/3);
static Double TWO = new Double(2);
static Double FOUR = new Double(4);
public Trapezium()
{
super("Trapezium");
}
public Trapezium(String name) { super(name); }
public Object evaluate(Object elements[]) throws ParseException
{
if(elements.length <2)
throw new ParseException("Trapezium: there should two or more ordinates, its"+elements.length);
Object ret = mul.mul(HALF,
add.add(elements[0],
elements[elements.length-1]));
for(int i=1;i