【问题标题】:Multilinear Interpolation for unknown number of points in n dimensionsn维中未知点数的多线性插值
【发布时间】:2013-11-11 10:14:11
【问题描述】:

我目前正在尝试创建一种方法,可以在 n 维空间中对未知数量的点(数量等于 2^n)进行多线性插值。

n = 1(无插值)和 n = 2(线性插值)的情况已经实现并且似乎有效。但是现在我已经在努力使双线性插值(对于 n = 4 的情况)维度不可知,我不知道我应该如何从那一点开始(n = 8,...,2^n)。

是否有解决此问题的通用方法,或者我应该对某些情况进行硬编码,否则抛出 UnsupportedOperationException?

我在下面添加了一个 SSCCE,希望能澄清我的问题。它由一个存储坐标和值的点类组成,还包含一些功能,例如计算到另一个点的距离和插值方法,其中已经包含我对 n = 1 和 n = 2 的情况的实现。

代码


import java.util.ArrayList;
import java.util.List;

public class Interpolator {

    public static void main(String[] args) {
        Interpolator ip = new Interpolator();

        Point currentPoint = new Point(new long[]{5, 5}, 0);

        List<Point> neighbors = new ArrayList<Point>() {{
            add(new Point(new long[]{3, 3}, 7));
            add(new Point(new long[]{10, 10}, 4));
        }};

        System.out.println(ip.interpolate(currentPoint, neighbors));
    }

    public float interpolate(Point currentPoint, List<Point> neighbors) {

        if (neighbors.size() == 1) {
            // no interpolation necessary with only one neighbor
            return neighbors.get(0).getValue();
        } else if (neighbors.size() == 2) {
            // distance between point and the two neighbors
            float distanceOne = currentPoint.distance(neighbors.get(0));
            float distanceTwo = currentPoint.distance(neighbors.get(1));
            float completeDistance = distanceOne + distanceTwo;

            // calculate weights
            float weightOne = 1 - distanceOne / completeDistance;
            float weightTwo = 1 - distanceTwo / completeDistance;

            // linear interpolation
            return neighbors.get(0).getValue() * weightOne
                    + neighbors.get(1).getValue() * weightTwo;
        } else if (neighbors.size() == 4) {
            //TODO: bilinear interpolation
        } else if(neighbors.size() == 8){
            //TODO: trilinear interpolation
        }

        //TODO: quadlinear interpolation or higher?
        return -1;
    }

    public static class Point {

        private long[] m_coordinates;
        private float m_value;

        public Point(final long[] coordinates, float value) {
            this.m_coordinates = coordinates;
            this.m_value = value;
        }

        public long[] getCoordinates() {
            return this.m_coordinates;
        }

        public int numDim() {
            return m_coordinates.length;
        }

        public long dim(final int i) {
            return this.m_coordinates[i];
        }

        public float distance(final Point otherPoint) {
            if (this.numDim() != otherPoint.numDim()) {
                throw new IllegalArgumentException(
                        "Can't measure distance between points with different dimensionality");
            }

            float sum = 0;
            for (int i = 0; i < this.numDim(); i++) {
                sum += Math.pow(this.dim(i) - otherPoint.dim(i), 2);
            }

            return (float) Math.sqrt(sum);
        }

        public float getValue() {
            return m_value;
        }
    }
}

【问题讨论】:

    标签: java multidimensional-array interpolation


    【解决方案1】:

    这似乎与您的算法一样,但与维度无关。我并不是说您的计算是正确的 - 我只是说我认为这是相同的计算,但对于 n 维度:

    public float interpolate(Point currentPoint, List<Point> neighbors) {
      int dimensions = neighbors.size();
      float[] distance = new float[dimensions];
      float sumDistances = 0;
      for (int i = 0; i < dimensions; i++) {
        distance[i] = currentPoint.distance(neighbors.get(i));
        sumDistances += distance[i];
      }
      float[] weight = new float[dimensions];
      for (int i = 0; i < dimensions; i++) {
        weight[i] = 1 - distance[i] / sumDistances;
      }
      float interpolatedDistance = 0;
      for (int i = 0; i < dimensions; i++) {
        interpolatedDistance += neighbors.get(i).getValue() * weight[i];
      }
      return interpolatedDistance;
    }
    

    【讨论】:

    • 我怀疑这是计算距离的正确策略。
    • @TilmanHausherr - 请解释一下。这与使用的 OP 算法相同,但推广到 n 维。
    • 我表达错误,对不起,我应该对这个问题发表评论,而不是对你的回答。我的意思是说我怀疑它是否是多线性插值的正确策略,因为它使用距离,而不是使用矩形、立方体等的内容大小,或者按照维基百科中的说明进行分步方法(例如用于三线性插值)。换句话说,我同意你的文字“我不是说你的计算是正确的”:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 2014-08-31
    • 1970-01-01
    相关资源
    最近更新 更多