Example usage for org.apache.commons.math3.analysis.interpolation BivariateGridInterpolator interpolate

List of usage examples for org.apache.commons.math3.analysis.interpolation BivariateGridInterpolator interpolate

Introduction

In this page you can find the example usage for org.apache.commons.math3.analysis.interpolation BivariateGridInterpolator interpolate.

Prototype

BivariateFunction interpolate(double[] xval, double[] yval, double[][] fval)
        throws NoDataException, DimensionMismatchException;

Source Link

Document

Compute an interpolating function for the dataset.

Usage

From source file:es.uvigo.ei.sing.laimages.core.operations.Interpolator.java

private static double[][] calculateNewValues(double[][] data, int interpolationLevel) {
    final int initialRows = data.length;
    final int initialColumns = data[0].length;

    final BivariateGridInterpolator interpolator = new BilinearInterpolator();
    final BivariateFunction function = interpolator.interpolate(
            IntStream.range(0, initialRows).asDoubleStream().toArray(),
            IntStream.range(0, initialColumns).asDoubleStream().toArray(), data);

    final int newNumRows = CALCULATE_SIZE.applyAsInt(initialRows, interpolationLevel);
    final int newNumColumns = CALCULATE_SIZE.applyAsInt(initialColumns, interpolationLevel);

    final double[][] newValues = new double[newNumRows][newNumColumns];

    final double xFactor = ((double) initialRows - 1d) / ((double) newNumRows - 1d);
    final double yFactor = ((double) initialColumns - 1d) / ((double) newNumColumns - 1d);
    for (int i = 0; i < newValues.length; i++) {
        for (int j = 0; j < newValues[i].length; j++) {
            newValues[i][j] = function.value((double) i * xFactor, (double) j * yFactor);
        }/*from  ww  w  .j  a  v a2s.co m*/
    }

    return newValues;
}

From source file:com.gordoni.opal.BiInterpolator.java

public BiInterpolator(MapPeriod mp, int what) {
    this.mp = mp;

    Scenario scenario = mp.scenario;//from   w  ww. j  a  v a2 s  .  c  om
    Config config = scenario.config;

    xval = new double[mp.length[0]];
    for (int i = 0; i < xval.length; i++)
        xval[(xval.length - 1) - i] = scenario.scale[0].bucket_to_pf(mp.bottom[0] + i);
    yval = new double[mp.length[1]];
    for (int i = 0; i < yval.length; i++)
        yval[(yval.length - 1) - i] = scenario.scale[1].bucket_to_pf(mp.bottom[1] + i);

    fval = new double[mp.length[0]][mp.length[1]];
    MapPeriodIterator<MapElement> mpitr = mp.iterator();
    while (mpitr.hasNext()) {
        int[] bucket = mpitr.nextIndex().clone();
        MapElement me = mpitr.next();
        int xindex = (xval.length - 1) - (bucket[0] - mp.bottom[0]);
        int yindex = (yval.length - 1) - (bucket[1] - mp.bottom[1]);
        fval[xindex][yindex] = getWhat(me, what);
    }

    BivariateGridInterpolator interpolator;
    if (config.interpolation2.equals("spline"))
        interpolator = new PiecewiseBicubicSplineInterpolator();
    else {
        assert (false);
        return;
    }

    this.f = interpolator.interpolate(xval, yval, fval);
}

From source file:org.meteoinfo.math.interpolate.InterpUtil.java

/**
 * Make interpolation function for grid data
 * @param x X data/*from www.  j a va2  s . c  o m*/
 * @param y Y data
 * @param z Z data
 * @return Interpolation function
 */
public static BivariateFunction getBiInterpFunc(Array x, Array y, Array z) {
    double[] xd = (double[]) ArrayUtil.copyToNDJavaArray(x);
    double[] yd = (double[]) ArrayUtil.copyToNDJavaArray(y);
    double[][] zd = (double[][]) ArrayUtil.copyToNDJavaArray(z);
    BivariateGridInterpolator li = new BicubicInterpolator();
    BivariateFunction func = li.interpolate(xd, yd, zd);

    return func;
}

From source file:uk.ac.diamond.scisoft.analysis.dataset.function.Interpolation2D.java

public static Dataset interpolate(IDataset oldx, IDataset oldy, IDataset oldxy, IDataset newx, IDataset newy,
        BivariateGridInterpolator interpolator)
        throws NonMonotonicSequenceException, NumberIsTooSmallException {

    //check shapes
    if (oldx.getRank() != 1)
        throw new IllegalArgumentException("oldx Shape must be 1D");
    if (oldy.getRank() != 1)
        throw new IllegalArgumentException("oldy Shape must be 1D");
    if (oldxy.getRank() != 2)
        throw new IllegalArgumentException("oldxy Shape must be 2D");
    if (oldx.getShape()[0] != oldxy.getShape()[0])
        throw new IllegalArgumentException("oldx Shape must match oldxy Shape[0]");
    if (oldy.getShape()[0] != oldxy.getShape()[1])
        throw new IllegalArgumentException("oldy Shape must match oldxy Shape[1]");
    if (newx.getRank() != 1)
        throw new IllegalArgumentException("newx Shape must be 1D");
    if (newy.getRank() != 1)
        throw new IllegalArgumentException("newx Shape must be 1D");
    if (newy.getSize() != newx.getSize())
        throw new IllegalArgumentException("newx and newy Size must be identical");

    DoubleDataset oldx_dd = (DoubleDataset) DatasetUtils.cast(oldx, Dataset.FLOAT64);
    DoubleDataset oldy_dd = (DoubleDataset) DatasetUtils.cast(oldy, Dataset.FLOAT64);
    DoubleDataset oldxy_dd = (DoubleDataset) DatasetUtils.cast(oldxy, Dataset.FLOAT64);

    //unlike in Interpolation1D, we will not be sorting here, as it just too complicated
    //the user will be responsible for ensuring the arrays are properly sorted

    //oldxy_dd needs to be transformed into a double[][] array
    //this call may throw an exception that needs handling by the calling method
    BivariateFunction func = interpolator.interpolate(oldx_dd.getData(), oldy_dd.getData(),
            convertDoubleDataset2DtoPrimitive(oldxy_dd));

    Dataset rv = DatasetFactory.zeros(new int[] { newx.getSize() }, Dataset.FLOAT64);
    rv.setName(oldxy.getName() + "_interpolated");

    for (int i = 0; i < newx.getSize(); i++) {
        double val = 0.0;
        try {/*  ww w.  j  a v a 2  s.co m*/
            val = func.value(newx.getDouble(i), newy.getDouble(i));
            rv.set(val, i);
        } catch (OutOfRangeException e) {
            rv.set(0.0, i);
        }
    }

    return rv;
}