【问题标题】:1D Array resizing with Spline Functions using Apache Math - How to?使用 Apache Math 使用样条函数调整 1D 数组大小 - 如何?
【发布时间】:2020-04-04 20:23:46
【问题描述】:

我正在寻找一个使用 Apache Commons - Math 的样条函数调整一维数组大小的示例。

我需要的是一种扩展和/或缩小输入数组 (double[]) 的方法。

我在尝试在线搜索时找不到一个很好的例子。

【问题讨论】:

    标签: java math apache-commons spline apache-commons-math


    【解决方案1】:

    这里的诀窍是你需要两个arrays 来创建一个spline,但你只有一个。因此,您需要制造一个array。您可以假设输入array 包含您的y 值,并且新制造的数组包含您的x 值,因此对于任何给定的x,您都有相应的y

    免责声明,我尚未测试此代码,因此请务必进行相应调整。

    // To expand the array
    public static double[] expand(double[] array, int newSize) {
    
        final int length = array.length;
    
        // let's calculate the new step size
        double step = (double) length / (newSize + 1);
    
        // fabricated array of x values
        double[] x = new double[length];
        for(int i = 0; i < length; ++i) {
            x[i] = i;
        }
    
        // using Linear interpolator but it can be any other interpolator
        LinearInterpolator li = new LinearInterpolator(); // or other interpolator
        PolynomialSplineFunction psf = li.interpolate(x, array);
    
        double[] expandedArray = new double[newSize];
        double xi = x[0];
        for (int i = 0; i < newSize - 1; ++i) {
           expandedArray[i] = psf.value(xi);
           xi += step;
        }
        expandedArray[newSize - 1] = array[length - 1];
        return expandedArray;
    }
    

    对于shrink 数组,您可以decimate 输入array 即只需创建一个新的较小的array 并根据新的步长取值或像以前一样使用interpolator

    // To shrink the array
    public static double[] shrink(double[] array, int newSize) {
    
        final int length = array.length;
    
        // let's calculate the new step size
        double step = (double) length / (newSize - 1);
    
        // fabricated array of x values
        double[] x = new double[length];
        for(int i = 0; i < length; ++i) {
            x[i] = i;
        }
    
        // using Linear interpolator but it can be any other interpolator
        LinearInterpolator li = new LinearInterpolator(); // or other interpolator
        PolynomialSplineFunction psf = li.interpolate(x, array);
    
        double[] expandedArray = new double[newSize];
        double xi = x[0];
        for (int i = 0; i < newSize - 1; ++i) {
           expandedArray[i] = psf.value(xi);
           xi += step;
        }
        expandedArray[newSize - 1] = array[length - 1];
        return expandedArray;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-25
      • 2014-05-24
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多