List of usage examples for org.apache.commons.math3.analysis.interpolation SplineInterpolator SplineInterpolator
SplineInterpolator
From source file:area.math.nm.lib.Interpolation.java
public String splineInterpolation(double x[], double y[], double input) { UnivariateInterpolator interpolator = new SplineInterpolator(); UnivariateFunction function = interpolator.interpolate(x, y); double interpolationX = input; double interpolatedY = function.value(input); System.out.println("f(" + interpolationX + ") = " + interpolatedY); String result = new String(Double.toString(interpolatedY)); return (result); }
From source file:area.math.nm.lib.Interpolation.java
public UnivariateFunction splinePlot(double x[], double y[]) { UnivariateInterpolator interpolator = new SplineInterpolator(); UnivariateFunction function = interpolator.interpolate(x, y); return (function); }
From source file:net.djnn.addons.interpolation.CubicSplineInterpolation.java
public CubicSplineInterpolation(Element parent, String name, Element toMove, double[] t, double[] x) { super(parent, name); mu = new DoubleProperty(this, "mu", 0); SplineInterpolator sp = new SplineInterpolator(); psf = sp.interpolate(x, t);// w ww . j a va 2s . c om this.property = new DoubleProperty(toMove); new Binding(this, null, mu, new NativeAction(this, null, null, 1) { public void callback(Element e) { double muVal = mu.getValue(); if (muVal > 1) muVal = 1; if (muVal < 0) muVal = 0; property.setValue(psf.value(muVal)); } }); }
From source file:be.ugent.maf.cellmissy.analysis.singlecell.processing.impl.interpolation.TrackSplineInterpolator.java
@Override public InterpolatedTrack interpolateTrack(double[] time, double[] x, double[] y) { // create a new spline interpolator SplineInterpolator splineInterpolator = new SplineInterpolator(); int interpolationPoints = PropertiesConfigurationHolder.getInstance().getInt("numberOfInterpolationPoints"); // create arrays to hold the interpolant time, the interpolated X and the interpolated Y double[] interpolantTime = new double[interpolationPoints]; double[] interpolatedX = new double[interpolationPoints]; double[] interpolatedY = new double[interpolationPoints]; // the step used for the interpolation in both direction double interpolationStep = (time[time.length - 1] - time[0]) / interpolationPoints; // check for monotonicity boolean monotonic = MathArrays.isMonotonic(time, MathArrays.OrderDirection.INCREASING, false); // in case time is not monotonic, sort in place time, x and y coordinates if (!monotonic) { MathArrays.sortInPlace(time, x, y); }/*from w w w. j av a 2 s . c o m*/ // call the interpolator, and actually do the interpolation try { PolynomialSplineFunction functionX = splineInterpolator.interpolate(time, x); PolynomialSplineFunction functionY = splineInterpolator.interpolate(time, y); // get the polynomial functions in both directions PolynomialFunction polynomialFunctionX = functionX.getPolynomials()[0]; PolynomialFunction polynomialFunctionY = functionY.getPolynomials()[0]; for (int i = 0; i < interpolationPoints; i++) { interpolantTime[i] = time[0] + (i * interpolationStep); interpolatedX[i] = functionX.value(interpolantTime[i]); interpolatedY[i] = functionY.value(interpolantTime[i]); } for (int k = 0; k < interpolationPoints; k++) { if (Double.isNaN(interpolatedX[k]) | Double.isNaN(interpolatedY[k])) { return null; } } return new InterpolatedTrack(interpolantTime, interpolatedX, interpolatedY, polynomialFunctionX, polynomialFunctionY); } catch (NumberIsTooSmallException e) { LOG.error(e.getMessage()); return null; } }
From source file:de.bund.bfr.math.InterpolationFactory.java
public UnivariateFunction createInterpolationFunction(double[] x, double[] y) { switch (type) { case STEP://ww w . j a va2s . c o m return new StepFunction(x, y); case SPLINE: return new SplineInterpolator().interpolate(x, y); default: throw new RuntimeException("Unknown type of InterpolationFactory: " + type); } }
From source file:imperial.modaclouds.monitoring.sda.weka.CreateArff.java
/** * Create arff file given the data// w w w . j a v a 2 s . c om * * @param timestamps_str the timestamps data * @param data the values of the metrics * @param metricName the metric name * @param fileName the file name to keep the arff file */ public static void create(ArrayList<ArrayList<String>> timestamps_str, ArrayList<ArrayList<String>> data, ArrayList<String> metricName, String fileName) { System.out.println("data: " + data.get(0)); long min_timestamp = Long.valueOf(Collections.min(timestamps_str.get(0))); long max_timestamp = Long.valueOf(Collections.max(timestamps_str.get(0))); for (int i = 1; i < timestamps_str.size(); i++) { long min_temp = Long.valueOf(Collections.min(timestamps_str.get(i))); long max_temp = Long.valueOf(Collections.max(timestamps_str.get(i))); if (max_temp < max_timestamp) { max_timestamp = max_temp; } if (min_temp > min_timestamp) { min_timestamp = min_temp; } } for (int i = 0; i < timestamps_str.size(); i++) { Iterator<String> iter_time = timestamps_str.get(i).iterator(); Iterator<String> iter_data = data.get(i).iterator(); while (iter_time.hasNext()) { long temp_timestamps = Long.valueOf(iter_time.next()); if (temp_timestamps < min_timestamp || temp_timestamps > max_timestamp) { iter_time.remove(); iter_data.next(); iter_data.remove(); } } } double[] timestamps = convertDoubles(timestamps_str.get(0)); double[] targetData = convertDoubles(data.get(0)); double[][] otherData = new double[data.size() - 1][timestamps.length]; for (int i = 0; i < data.size() - 1; i++) { double[] timestamps_temp = convertDoubles(timestamps_str.get(i)); double[] targetData_temp = convertDoubles(data.get(i)); SplineInterpolator spline = new SplineInterpolator(); Map<Double, Integer> map = new TreeMap<Double, Integer>(); for (int j = 0; j < timestamps_temp.length; j++) { map.put(timestamps_temp[j], j); } Collection<Integer> indices = map.values(); int[] indices_int = ArrayUtils.toPrimitive(indices.toArray(new Integer[indices.size()])); double[] timestamps_temp_new = new double[indices_int.length]; double[] targetData_temp_new = new double[indices_int.length]; for (int j = 0; j < indices_int.length; j++) { timestamps_temp_new[j] = timestamps_temp[indices_int[j]]; targetData_temp_new[j] = targetData_temp[indices_int[j]]; } PolynomialSplineFunction polynomical = spline.interpolate(timestamps_temp_new, targetData_temp_new); for (int j = 0; j < timestamps.length; j++) { try { otherData[i][j] = polynomical.value(timestamps[j]); } catch (Exception ex) { otherData[i][j] = targetData_temp_new[j]; } } } ArrayList<Attribute> attributes; Instances dataSet; attributes = new ArrayList<Attribute>(); for (String metric : metricName) { attributes.add(new Attribute(metric)); } dataSet = new Instances("data", attributes, 0); for (int i = 0; i < timestamps.length; i++) { double[] instanceValue1 = new double[dataSet.numAttributes()]; instanceValue1[0] = timestamps[i]; instanceValue1[1] = targetData[i]; for (int j = 0; j < data.size() - 1; j++) { instanceValue1[2 + j] = otherData[j][i]; } DenseInstance denseInstance1 = new DenseInstance(1.0, instanceValue1); dataSet.add(denseInstance1); } ArffSaver saver = new ArffSaver(); saver.setInstances(dataSet); try { String workingDir = System.getProperty("user.dir"); System.out.println("workingDir: " + workingDir); saver.setFile(new File(workingDir + "/" + fileName)); saver.writeBatch(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.gordoni.opal.UniInterpolator.java
public UniInterpolator(MapPeriod mp, int what) { this.mp = mp; Scenario scenario = mp.scenario;//from w w w . j a v a 2 s . c o m 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); fval = new double[mp.length[0]]; MapPeriodIterator<MapElement> mpitr = mp.iterator(); while (mpitr.hasNext()) { int[] bucket = mpitr.nextIndex().clone(); MapElement me = mpitr.next(); int xindex = (fval.length - 1) - (bucket[0] - mp.bottom[0]); fval[xindex] = getWhat(me, what); } UnivariateInterpolator interpolator; if (config.interpolation1.equals("linear")) interpolator = new LinearInterpolator(); else if (config.interpolation1.equals("spline")) interpolator = new SplineInterpolator(); else { assert (false); return; } this.f = interpolator.interpolate(xval, fval); }
From source file:com.gordoni.opal.LSInterpolator.java
public LSInterpolator(MapPeriod mp, int what, boolean linear_spline) { this.mp = mp; this.linear_spline = linear_spline; Scenario scenario = mp.scenario;//from ww w . ja v a 2 s . co m 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); } if (!linear_spline) { // spline-linear. double tmp[] = xval; xval = yval; yval = tmp; fval = Utils.zipDoubleArrayArray(fval); } this.f = new UnivariateFunction[fval.length]; for (int i = 0; i < fval.length; i++) { UnivariateInterpolator interpolator; interpolator = new SplineInterpolator(); this.f[i] = interpolator.interpolate(yval, fval[i]); } }
From source file:eu.crisis_economics.abm.simulation.injection.TestFromFileNumercialModelParameter.java
/** * Test whether an instance of {@link FromFileTimeseriesParameter} correctly * reads, and correctly reports, a timeseries from a datafile. This unit test * operates as follows:<br><br> * // w w w. ja va 2 s. com * {@code (a)} * A temporary file {@code F}, named {@code "./paramter-test.dat"} is created * in the local directory;<br> * {@code (b)} * {@code F} is populated with a short discrete subsequence from the expression * {@code f(T) = T**2};<br> * {@code (c)} * {@code F} is parsed by an instance of {@link FromFileTimeseriesParameter};<br> * {@code (d)} * An {@link EmptySimulation} is run for {@code 10} cycles. At each cycle, * it is asserted that the {@link ModelParameter} yields the same expression * {@code f(T)} as is indicated by {@code F}.<br><br> * * For convenience {@code F} is not deleted at the end of the above session. */ @Test public void testReadModelParameterTimeSeriesFromFile() { final double[] expectedX = new double[] { 2., 3., 4., 5., 6., }, expectedY = new double[] { 4., 9., 16., 25., 36., }; final String fileName = "./paramter-test.dat"; try { final File file = new File(fileName); if (!file.exists()) file.createNewFile(); final FileWriter stream = new FileWriter(file.getAbsoluteFile()); final BufferedWriter writer = new BufferedWriter(stream); for (int i = 0; i < expectedX.length; ++i) writer.write(expectedX[i] + "\t" + expectedY[i] + "\n"); writer.close(); stream.close(); } catch (final IOException e) { Assert.fail(); } ModelParameter<Double> parameter = null; try { parameter = new FromFileTimeseriesParameter(fileName, "[name]", new SplineInterpolator()); } catch (final IOException e) { Assert.fail(); } Simulation.repeat(this, "sample", CustomSimulationCycleOrdering.create(0.), parameter, expectedX, expectedY); while (Simulation.getTime() < 10.0) state.schedule.step(state); }
From source file:de.treichels.hott.mdlviewer.swt.SwtCurveImageGenerator.java
private Image getImage(final Curve curve, final double scale, final boolean description) { // pitch curves start with 0% instead of -100% final boolean pitchCurve = curve.getPoint().get(0).getPosition() == 0; Image image;//from w w w. j av a2s . co m GC g = null; try { // 200x250 pixel image with 5 pixel border scaled by factor scale image = new Image(Display.getDefault(), (int) (10 + 200 * scale), (int) (10 + 250 * scale)); g = new GC(image); g.setAntialias(SWT.ON); g.setTextAntialias(SWT.ON); // clear background g.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); g.fillRectangle(0, 0, (int) (10 + 200 * scale), (int) (10 + 250 * scale)); // outer rectangle g.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK)); g.setLineWidth(1); g.drawRectangle(5, 5, (int) (200 * scale), (int) (250 * scale)); g.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_GRAY)); // +100% horizontal line g.drawLine(5, (int) (5 + 25 * scale), (int) (5 + 200 * scale), (int) (5 + 25 * scale)); // -100% horizontal line g.drawLine(5, (int) (5 + 225 * scale), (int) (5 + 200 * scale), (int) (5 + 225 * scale)); // 0% horizontal line g.drawLine(5, (int) (5 + 125 * scale), (int) (5 + 200 * scale), (int) (5 + 125 * scale)); if (!pitchCurve) { // 0% vertical line g.drawLine((int) (5 + 100 * scale), 5, (int) (5 + 100 * scale), (int) (5 + 250 * scale)); } g.setFont(new Font(Display.getDefault(), new FontData("Arial", 10, SWT.BOLD))); g.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK)); // determine number of enabled curve points int numPoints = 0; for (final CurvePoint p : curve.getPoint()) if (p.isEnabled()) numPoints++; final double[] xVals = new double[numPoints]; final double[] yVals = new double[numPoints]; int i = 0; // store coordinates for (final CurvePoint p : curve.getPoint()) if (p.isEnabled()) { if (i == 0) // first point x coordinate is fixed to -100% (0% // for pitch curve) xVals[i] = pitchCurve ? 0 : -100; else if (i == numPoints - 1) // last point x coordinate is fixed to +100% xVals[i] = 100; else xVals[i] = p.getPosition(); yVals[i] = p.getValue(); if (description) { int x0; int y0; if (pitchCurve) { x0 = (int) (5 + xVals[i] * 2 * scale); y0 = (int) (5 + (225 - yVals[i] * 2) * scale); } else { x0 = (int) (5 + (100 + xVals[i]) * scale); y0 = (int) (5 + (125 - yVals[i]) * scale); } // draw point g.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK)); g.fillOval(x0 - 3, y0 - 3, 6, 6); // draw text g.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); g.fillRectangle(x0 - 5, y0 + 3, 9, 13); g.drawText(Integer.toString(p.getNumber() + 1), x0 - 4, y0 + 2, true); } i++; } g.setLineWidth(2); if (numPoints > 2 && curve.isSmoothing()) { // use a spline interpolator to smooth the curve final SplineInterpolator s = new SplineInterpolator(); final PolynomialSplineFunction function = s.interpolate(xVals, yVals); int x0 = 5; int y0; // starting point screen coordinates if (pitchCurve) y0 = (int) (5 + (225 - yVals[0] * 2) * scale); else y0 = (int) (5 + (125 - yVals[0]) * scale); // draw line pixel-by-pixel while (x0 < (int) (4 + 200 * scale)) { final int x1 = x0 + 1; int y1; if (pitchCurve) y1 = (int) (5 + (225 - function.value((x1 - 5) / scale / 2) * 2) * scale); else y1 = (int) (5 + (125 - function.value((x1 - 5) / scale - 100)) * scale); g.drawLine(x0, y0, x1, y1); x0 = x1; y0 = y1; } } else // draw line segments for (i = 0; i < numPoints - 1; i++) { int x0, y0, x1, y1; if (pitchCurve) { x0 = (int) (5 + xVals[i] * 2 * scale); y0 = (int) (5 + (225 - yVals[i] * 2) * scale); x1 = (int) (5 + xVals[i + 1] * 2 * scale); y1 = (int) (5 + (225 - yVals[i + 1] * 2) * scale); } else { x0 = (int) (5 + (100 + xVals[i]) * scale); y0 = (int) (5 + (125 - yVals[i]) * scale); x1 = (int) (5 + (100 + xVals[i + 1]) * scale); y1 = (int) (5 + (125 - yVals[i + 1]) * scale); } g.drawLine(x0, y0, x1, y1); } } finally { if (g != null) g.dispose(); } return image; }