用Java外推 [英] Extrapolation in java

查看:90
本文介绍了用Java外推的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够通过LinearInterpolator().interpolate(x1,y1)使用Apache Math的插值.不幸的是,我找不到推论的方法.

I've been able to use Apache Math's interpolation using the LinearInterpolator().interpolate(x1, y1). Unfortunately, I could not find a way to extrapolate.

如何在Java中进行线性外推?

How can I do linear extrapolation in java?

x1 = [1、2、3、4、5];

x1 = [1, 2, 3, 4, 5];

y1 = [2,4,8,16,32];

y1 = [2, 4, 8, 16, 32];

我想知道任何x2的值,而不仅仅是x1范围内的那个.

I would like to know the values of any x2 not just the one in the range of the x1.

如果我尝试提取6的值,则会得到:OutOfRangeException,如果{@code v}在 *样条函数(小于最小结点或大于 最大的结点).

If I try to extract the value of 6 I get an: OutOfRangeException if {@code v} is outside of the domain of the * spline function (smaller than the smallest knot point or larger than the largest knot point).

这是我简单的插值函数.我想要一个选项来启用外推,就像在MathLab(interp2)中一样.使用x1和y1数组作为该函数的输入,我得到Apache的OutOfRangeException,因为x1数组中不包含值6.

Here is my simple interpolate function. I would like an option to enable the extrapolation just like in MathLab(interp2). Using x1 and y1 arrays an input for that function I get the Apache's OutOfRangeException because the value 6 is not contained in the x1 array.

public static List<Double> interpolateLinear(double[] x1, double[] y1, Double[] x2) {
    List<Double> resultList;
    final PolynomialSplineFunction function = new LinearInterpolator().interpolate(x1, y1);
    resultList = Arrays.stream(x2).map(aDouble -> function.value(aDouble)).collect(Collectors.toList());
    return resultList;
}

Edit2:必须先阅读PolynomialSplineFunction对象的.value方法才能正确处理它,但是一切顺利(所有功劳归功于用户

Had to read a little bit on the .value method of the PolynomialSplineFunction object to get it right but there it goes (all the credit goes to user Joni) Thanks man:

public static double[] interpolateLinear(double[] x1, double[] y1, double[] x2) {
    final PolynomialSplineFunction function = new LinearInterpolator().interpolate(x1, y1);
    final PolynomialFunction[] splines = function.getPolynomials();
    final PolynomialFunction firstFunction = splines[0];
    final PolynomialFunction lastFunction = splines[splines.length - 1];

    final double[] knots = function.getKnots();
    final double firstKnot = knots[0];
    final double lastKnot = knots[knots.length - 1];

    double[] resultList = Arrays.stream(x2).map(aDouble -> {
        if (aDouble > lastKnot) {
            return lastFunction.value(aDouble - knots[knots.length - 2]);
        } else if (aDouble < firstKnot)
            return firstFunction.value(aDouble - knots[0]);
        return function.value(aDouble);
    }).toArray();
    return resultList;
}

推荐答案

您可以从插值器中获取第一个和最后一个多项式样条,并使用它们进行推断.

You can get the first and last polynomial splines from the interpolator, and use those to extrapolate.

PolynomialSplineFunction function = new LinearInterpolator().interpolate(x1, y1);
PolynomialFunction[] splines = function.getPolynomials();
PolynomialFunction first = splines[0];
PolynomialFunction last = splines[splines.length-1];
// use first and last to extrapolate 

虽然您不会从6中获得64.您应该从线性外推法得到48.这表明外推必然会给您错误的答案.

You won't get 64 from 6 though. You should expect 48 from a linear extrapolation. Which goes to show that extrapolation is bound to give you wrong answers.

这篇关于用Java外推的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆