Example usage for org.apache.commons.math.analysis SplineInterpolator interpolate

List of usage examples for org.apache.commons.math.analysis SplineInterpolator interpolate

Introduction

In this page you can find the example usage for org.apache.commons.math.analysis SplineInterpolator interpolate.

Prototype

public UnivariateRealFunction interpolate(double x[], double y[]) 

Source Link

Document

Computes an interpolating function for the data set.

Usage

From source file:magma.agent.behavior.function.SplineFunction.java

/**
 * Creates a spline function from the previously set x and y values
 *//*ww  w.j  av  a2s  .  com*/
public void initialize() {
    try {
        SplineInterpolator interpolator = new SplineInterpolator();
        spline = (PolynomialSplineFunction) interpolator.interpolate(xValues, yValues);
        xMin = xValues[0];
        xMax = xValues[xValues.length - 1];
    } catch (IllegalArgumentException e) {
        System.out.println(e + "\n" + Arrays.toString(xValues));
        throw e;
    }
}

From source file:org.xenmaster.scheduler.analytics.Modeler.java

public static void main(String[] args) {
    try {//from  ww w.jav a  2  s  .  co m
        ArrayList<Double> vals;
        try (BufferedReader br = new BufferedReader(new FileReader("data.csv"))) {
            vals = new ArrayList<>();
            String line = br.readLine();
            while (line != null) {
                String[] split = StringUtils.split(line, ',');
                vals.add(Double.parseDouble(split[0]));
                line = br.readLine();
            }
        }

        double[] idx = new double[1440];
        for (int i = 0; i < idx.length; i++) {
            idx[i] = i;
        }
        double y[] = new double[1440];
        for (int i = 0; i < idx.length; i++) {
            y[i] = vals.get(i).doubleValue();
        }
        SplineInterpolator si = new SplineInterpolator();
        PolynomialSplineFunction psf = (PolynomialSplineFunction) si.interpolate(idx, y);

        for (int i = 0; i < 3; i++) {
            psf = psf.polynomialSplineDerivative();
        }

        try (BufferedWriter bw = new BufferedWriter(new FileWriter("derp.csv", false))) {
            for (double i = psf.getKnots()[0]; i < psf.getKnots()[psf.getKnots().length - 1]; i++) {
                bw.append("" + psf.value(i) + "\n");
            }
        } catch (ArgumentOutsideDomainException | IOException ex) {

        }

    } catch (IOException ex) {
        Logger.getLogger(Modeler.class.getName()).log(Level.SEVERE, null, ex);
    }
}