如何在 import org.apache.commons.math3.analysis.integration.SimpsonIntegrator 中使用标准的 SimpsonIntegrator;

How to use standard SimpsonIntegrator in import org.apache.commons.math3.analysis.integration.SimpsonIntegrator;

public double evalute(double distance){

    /**
     * equation (3.2)
     */
    this.from = 0;
    this.to = distance;
    this.n = 2;
    return - 10 * Math.log10(Math.exp(-IntSimpson(this.from, this.to, this.n)));
}

有我手动设计的IntSimpson()函数,但我想使用标准库!我该怎么做以及在哪里可以找到它?

如果你想真正使用集成器对象,你需要调用integrate method, which takes an instance of UnivariateFunction。如果你在Java8,这是一个单方法接口,所以它自动是一个功能接口。因此,您可以传递 lambda 或方法引用,如:

final SimpsonIntegrator si = new SimpsonIntegrator();
final double result = si.integrate(50, x -> 2*x, 0, 10);
System.out.println(result + " should be 100");

否则,您必须自己创建接口的实现,方法是让 class 实现它,或者使用匿名 class:

final double result = si.integrate(50, new UnivariateFunction() {
        @Override public double value(double x) {
            return 2*x;
        }
    }, 0, 10);
System.out.println(result + " should be 100");

有效!

final SimpsonIntegrator si = new SimpsonIntegrator();
final double result1 = si.integrate(10, x -> 2*Math.pow(x, 1), 0.0, 10.0);
System.out.println(result1 + " should be 100.0");
final double result2 = si.integrate(1000, x -> Math.sin(x), 0.0, Math.PI);
System.out.println(result2 + " should be 2.0000...");

谢谢Javier Martín