如何在java数组中实现线性插值方法?

如何在java数组中实现线性插值方法?,java,arrays,linear-interpolation,Java,Arrays,Linear Interpolation,我正在编写一个简单的线性插值程序。我在实现算法时遇到了一些麻烦。假设总共有12个数字,我们将让用户输入其中的3个(位置0、位置6和位置12)。然后程序将计算其他数字。下面是我实现这一点的一段代码: static double[] interpolate(double a, double b){ double[] array = new double[6]; for(int i=0;i<6;i++){ array[i] = a + (i-0) * (b-a)/

我正在编写一个简单的线性插值程序。我在实现算法时遇到了一些麻烦。假设总共有12个数字,我们将让用户输入其中的3个(位置0、位置6和位置12)。然后程序将计算其他数字。下面是我实现这一点的一段代码:

static double[] interpolate(double a, double b){
    double[] array = new double[6];
    for(int i=0;i<6;i++){
        array[i] = a + (i-0) * (b-a)/6;
    }
    return array;
}

static double[] interpolate2(double a, double b){
    double[] array = new double[13];
    for(int i=6;i<=12;i++){
        array[i] = a + (i-6) * (b-a)/6;
    }
    return array;
}
静态双[]插值(双a,双b){
double[]数组=新的double[6];

对于(int i=0;i如果要将间隔内插到不同的数字计数,只需将输出数字计数添加到函数参数即可。 例如:

/***
*插值法
*@param开始时间间隔的开始时间
*@param end间隔的末尾
*@param count输出插值数的计数
*@返回具有指定计数的插值数数组
*/
公共静态双[]插值(双起点、双终点、整数计数){
如果(计数<2){
抛出新的IllegalArgumentException(“插值:非法计数!”);
}
double[]数组=新的double[count+1];
对于(int i=0;i
/***
 * Interpolating method
 * @param start start of the interval
 * @param end end of the interval
 * @param count count of output interpolated numbers
 * @return array of interpolated number with specified count
 */
public static double[] interpolate(double start, double end, int count) {
    if (count < 2) {
        throw new IllegalArgumentException("interpolate: illegal count!");
    }
    double[] array = new double[count + 1];
    for (int i = 0; i <= count; ++ i) {
        array[i] = start + i * (end - start) / count;
    }
    return array;
}