Java Array Interpolate interpolate(int[] data, double x)

Here you can find the source of interpolate(int[] data, double x)

Description

Returns the value of specified decimal index.

License

Open Source License

Parameter

Parameter Description
data the specified <tt>int</tt> matrix
x the decimal index

Return

the interpolation value

Declaration

public static final double interpolate(int[] data, double x) 

Method Source Code

//package com.java2s;

public class Main {
    /**//  w w w.  j a va2s . c  om
     * Returns the value of specified decimal index.
     * <p>
     * Calculating algorithm is implemented by first-order interpolation.
     * 
     * <pre>
     * f(x) = f(x') + (f(x'+1) - f(x')) * (x - x')
     * x' = rounded_down(x)
     * </pre>
     * 
     * </p>
     * 
     * @param data
     *            the specified <tt>int</tt> matrix
     * @param x
     *            the decimal index
     * @return the interpolation value
     */
    public static final double interpolate(int[] data, double x) {
        if (data.length == 0)
            return 0;
        if (x > data.length - 1)
            return data[data.length];
        if (x < 0)
            return data[0];
        int dx = (int) x;
        return data[dx] + (data[dx + 1] - (double) data[dx]) * (x - dx);
    }

    /**
     * Returns the value of specified decimal index.
     * <p>
     * Calculating algorithm is implemented by first-order interpolation.
     * 
     * <pre>
     * f(x) = f(x') + (f(x'+1) - f(x')) * (x - x')
     * x' = rounded_down(x)
     * </pre>
     * 
     * </p>
     * 
     * @param data
     *            the specified <tt>long</tt> array
     * @param x
     *            the decimal index
     * @return the interpolation value
     */
    public static final double interpolate(long[] data, double x) {
        if (data.length == 0)
            return 0;
        if (x > data.length - 1)
            return data[data.length];
        if (x < 0)
            return data[0];
        int dx = (int) x;
        return data[dx] + (data[dx + 1] - (double) data[dx]) * (x - dx);
    }

    /**
     * Returns the value of specified decimal index.
     * <p>
     * Calculating algorithm is implemented by first-order interpolation.
     * 
     * <pre>
     * f(x) = f(x') + (f(x'+1) - f(x')) * (x - x')
     * x' = rounded_down(x)
     * </pre>
     * 
     * </p>
     * 
     * @param data
     *            the specified <tt>float</tt> array
     * @param x
     *            the decimal index
     * @return the interpolation value
     */
    public static final double interpolate(float[] data, double x) {
        if (data.length == 0)
            return 0;
        if (x > data.length - 1)
            return data[data.length];
        if (x < 0)
            return data[0];
        int dx = (int) x;
        return data[dx] + (data[dx + 1] - (double) data[dx]) * (x - dx);
    }

    /**
     * Returns the value of specified decimal index.
     * <p>
     * Calculating algorithm is implemented by first-order interpolation.
     * 
     * <pre>
     * f(x) = f(x') + (f(x'+1) - f(x')) * (x - x')
     * x' = rounded_down(x)
     * </pre>
     * 
     * </p>
     * 
     * @param data
     *            the specified <tt>double</tt> array
     * @param x
     *            the decimal index
     * @return the interpolation value
     */
    public static final double interpolate(double[] data, double x) {
        if (data.length == 0)
            return 0;
        if (x > data.length - 1)
            return data[data.length];
        if (x < 0)
            return data[0];
        int dx = (int) x;
        return data[dx] + (data[dx + 1] - (double) data[dx]) * (x - dx);
    }

    /**
     * Returns the value of specified decimal index.
     * <p>
     * Calculating algorithm is implemented by first-order interpolation.
     * 
     * <pre>
     * f(x) = f(x') + (f(x'+1) - f(x')) * (x - x')
     * x' = rounded_down(x)
     * </pre>
     * 
     * </p>
     * 
     * @param data
     *            the specified <tt>Integer</tt> array
     * @param x
     *            the decimal index
     * @return the interpolation value
     */
    public static final double interpolate(Integer[] data, double x) {
        if (data.length == 0)
            return 0;
        if (x > data.length - 1)
            return data[data.length];
        if (x < 0)
            return data[0];
        int dx = (int) x;
        return data[dx] + (data[dx + 1] - (double) data[dx]) * (x - dx);
    }

    /**
     * Returns the value of specified decimal index.
     * <p>
     * Calculating algorithm is implemented by first-order interpolation.
     * 
     * <pre>
     * f(x) = f(x') + (f(x'+1) - f(x')) * (x - x')
     * x' = rounded_down(x)
     * </pre>
     * 
     * </p>
     * 
     * @param data
     *            the specified <tt>Long</tt> array
     * @param x
     *            the decimal index
     * @return the interpolation value
     */
    public static final double interpolate(Long[] data, double x) {
        if (data.length == 0)
            return 0;
        if (x > data.length - 1)
            return data[data.length];
        if (x < 0)
            return data[0];
        int dx = (int) x;
        return data[dx] + (data[dx + 1] - (double) data[dx]) * (x - dx);
    }

    /**
     * Returns the value of specified decimal index.
     * <p>
     * Calculating algorithm is implemented by first-order interpolation.
     * 
     * <pre>
     * f(x) = f(x') + (f(x'+1) - f(x')) * (x - x')
     * x' = rounded_down(x)
     * </pre>
     * 
     * </p>
     * 
     * @param data
     *            the specified <tt>Float</tt> array
     * @param x
     *            the decimal index
     * @return the interpolation value
     */
    public static final double interpolate(Float[] data, double x) {
        if (data.length == 0)
            return 0;
        if (x > data.length - 1)
            return data[data.length];
        if (x < 0)
            return data[0];
        int dx = (int) x;
        return data[dx] + (data[dx + 1] - (double) data[dx]) * (x - dx);
    }

    /**
     * Returns the value of specified decimal index.
     * <p>
     * Calculating algorithm is implemented by first-order interpolation.
     * 
     * <pre>
     * f(x,y) = f(x',y') + (f(x'+1,y') - f(x',y')) * (x - x') + (f(x',y'+1) - f(x',y')) * (y - y')
     * + (f(x'+1,y'+1)+f(x',y' )-f(x'+1,y' )-f(x',y'+1)) * (x - x') * (y - y')
     * x' = rounded_down(x)
     * </pre>
     * 
     * </p>
     * 
     * @param data
     *            the specified <tt>Double</tt> array
     * @param x
     *            the decimal index
     * @return the interpolation value
     */
    public static final double interpolate(Double[] data, double x) {
        if (data.length == 0)
            return 0;
        if (x > data.length - 1)
            return data[data.length];
        if (x < 0)
            return data[0];
        int dx = (int) x;
        return data[dx] + (data[dx + 1] - (double) data[dx]) * (x - dx);
    }

    /**
     * Returns the value of specified decimal position(x,y).
     * <p>
     * Calculating algorithm is implemented by 2D first-order interpolation.
     * 
     * <pre>
     * f(x,y) = f(x',y') + (f(x'+1,y') - f(x',y')) * (x - x') + (f(x',y'+1) - f(x',y')) * (y - y')
     * + (f(x'+1,y'+1)+f(x',y' )-f(x'+1,y' )-f(x',y'+1)) * (x - x') * (y - y')
     * x' = rounded_down(x)
     * </pre>
     * 
     * </p>
     * 
     * @param data
     *            the specified <tt>int</tt> matrix
     * @param x
     *            the decimal x coordinate
     * @param y
     *            the decimal y coordinate
     * @return the interpolation value
     */
    public static final double interpolate(int[][] data, double x, double y) {
        if (data.length == 0)
            return 0;
        if (x > data.length - 1 || y > data[0].length - 1)
            return data[data.length - 1][data[data.length - 1].length - 1];
        if (x < 0 || y < 0)
            return data[0][0];
        int dx = (int) x;
        int dy = (int) y;
        int p00 = data[dx][dy];
        int p01 = data[dx][dy + 1];
        int p10 = data[dx + 1][dy];
        int p11 = data[dx + 1][dy + 1];
        return p00 + (p10 - p00) * (x - dx) + (p01 - p00) * (y - dy)
                - (p11 + p00 - p10 - p11) * (x - dx) * (y - dy);
    }

    /**
     * Returns the value of specified decimal position(x,y).
     * <p>
     * Calculating algorithm is implemented by 2D first-order interpolation.
     * 
     * <pre>
     * f(x,y) = f(x',y') + (f(x'+1,y') - f(x',y')) * (x - x') + (f(x',y'+1) - f(x',y')) * (y - y')
     * + (f(x'+1,y'+1)+f(x',y' )-f(x'+1,y' )-f(x',y'+1)) * (x - x') * (y - y')
     * x' = rounded_down(x)
     * </pre>
     * 
     * </p>
     * 
     * @param data
     *            the specified <tt>long</tt> matrix
     * @param x
     *            the decimal x coordinate
     * @param y
     *            the decimal y coordinate
     * @return the interpolation value
     */
    public static final double interpolate(long[][] data, double x, double y) {
        if (data.length == 0)
            return 0;
        if (x > data.length - 1 || y > data[0].length - 1)
            return data[data.length - 1][data[data.length - 1].length - 1];
        if (x < 0 || y < 0)
            return data[0][0];
        int dx = (int) x;
        int dy = (int) y;
        long p00 = data[dx][dy];
        long p01 = data[dx][dy + 1];
        long p10 = data[dx + 1][dy];
        long p11 = data[dx + 1][dy + 1];
        return p00 + (p10 - p00) * (x - dx) + (p01 - p00) * (y - dy)
                - (p11 + p00 - p10 - p11) * (x - dx) * (y - dy);
    }

    /**
     * Returns the value of specified decimal position(x,y).
     * <p>
     * Calculating algorithm is implemented by 2D first-order interpolation.
     * 
     * <pre>
     * f(x,y) = f(x',y') + (f(x'+1,y') - f(x',y')) * (x - x') + (f(x',y'+1) - f(x',y')) * (y - y')
     * + (f(x'+1,y'+1)+f(x',y' )-f(x'+1,y' )-f(x',y'+1)) * (x - x') * (y - y')
     * x' = rounded_down(x)
     * </pre>
     * 
     * </p>
     * 
     * @param data
     *            the specified <tt>float</tt> matrix
     * @param x
     *            the decimal x coordinate
     * @param y
     *            the decimal y coordinate
     * @return the interpolation value
     */
    public static final double interpolate(float[][] data, double x, double y) {
        if (data.length == 0)
            return 0;
        if (x > data.length - 1 || y > data[0].length - 1)
            return data[data.length - 1][data[data.length - 1].length - 1];
        if (x < 0 || y < 0)
            return data[0][0];
        int dx = (int) x;
        int dy = (int) y;
        float p00 = data[dx][dy];
        float p01 = data[dx][dy + 1];
        float p10 = data[dx + 1][dy];
        float p11 = data[dx + 1][dy + 1];
        return p00 + (p10 - p00) * (x - dx) + (p01 - p00) * (y - dy)
                - (p11 + p00 - p10 - p11) * (x - dx) * (y - dy);
    }

    /**
     * Returns the value of specified decimal position(x,y).
     * <p>
     * Calculating algorithm is implemented by 2D first-order interpolation.
     * 
     * <pre>
     * f(x,y) = f(x',y') + (f(x'+1,y') - f(x',y')) * (x - x') + (f(x',y'+1) - f(x',y')) * (y - y')
     * + (f(x'+1,y'+1)+f(x',y' )-f(x'+1,y' )-f(x',y'+1)) * (x - x') * (y - y')
     * x' = rounded_down(x)
     * </pre>
     * 
     * </p>
     * 
     * @param data
     *            the specified <tt>double</tt> matrix
     * @param x
     *            the decimal x coordinate
     * @param y
     *            the decimal y coordinate
     * @return the interpolation value
     */
    public static final double interpolate(double[][] data, double x, double y) {
        if (data.length == 0)
            return 0;
        if (x < 0 || y < 0)
            return data[0][0];
        int dx = (int) x;
        int dy = (int) y;
        int maxY = data.length - 1;
        int maxX = data[maxY].length - 1;
        if (dy + 1 > maxY) {
            if (dx + 1 > maxX)
                return data[maxY][maxX];
            else
                // calculate when only y is at the end
                return (x - dx) * data[maxY][dx] + (dx + 1 - x) * data[maxY][dx + 1];
        } else if (dx + 1 > maxX)
            // calculate when only x is at the end
            return (y - dy) * data[dy][maxX] + (dy + 1 - y) * data[dy + 1][maxX];
        double p00 = data[dy][dx];
        double p01 = data[dy + 1][dx];
        double p10 = data[dy][dx + 1];
        double p11 = data[dy + 1][dx + 1];
        return p00 + (p10 - p00) * (x - dx) + (p01 - p00) * (y - dy)
                - (p11 + p00 - p10 - p11) * (x - dx) * (y - dy);
    }

    /**
     * Returns the value of specified decimal position(x,y).
     * <p>
     * Calculating algorithm is implemented by 2D first-order interpolation.
     * 
     * <pre>
     * f(x,y) = f(x',y') + (f(x'+1,y') - f(x',y')) * (x - x') + (f(x',y'+1) - f(x',y')) * (y - y')
     * + (f(x'+1,y'+1)+f(x',y' )-f(x'+1,y' )-f(x',y'+1)) * (x - x') * (y - y')
     * x' = rounded_down(x)
     * </pre>
     * 
     * </p>
     * 
     * @param data
     *            the specified <tt>double</tt> matrix
     * @param x
     *            the decimal x coordinate
     * @param y
     *            the decimal y coordinate
     * @return the interpolation value
     */
    public static final <T extends Number> double interpolate(T[][] data, double x, double y) {
        if (data.length == 0)
            return 0;
        if (x > data.length - 1 || y > data[0].length - 1)
            return data[data.length - 1][data[data.length - 1].length - 1].doubleValue();
        if (x < 0 || y < 0)
            return data[0][0].doubleValue();
        int dx = (int) x;
        int dy = (int) y;
        double p00 = data[dx][dy].doubleValue();
        double p01 = data[dx][dy + 1].doubleValue();
        double p10 = data[dx + 1][dy].doubleValue();
        double p11 = data[dx + 1][dy + 1].doubleValue();
        return p00 + (p10 - p00) * (x - dx) + (p01 - p00) * (y - dy)
                - (p11 + p00 - p10 - p11) * (x - dx) * (y - dy);
    }
}

Related

  1. interpolate(double[] points, double[] values, double interpolateAt)
  2. interpolate(double[] x, double D)
  3. interpolate(double[] X, double[] Y, double[] Z)
  4. interpolate(double[] x, int newLength)
  5. interpolate(float out[], float in1[], float in2[], int in2_idx, float coef, int length)
  6. interpolate_linear(int[] x, double[] y, int[] xi)
  7. interpolateArray(int[] array, int interval)
  8. interpolateColor(float[] result, float[] color0, float[] color1, double s)
  9. interpolateLinear(int[] v1, int[] v2, double t)