List of usage examples for org.apache.commons.math3.analysis.interpolation LinearInterpolator LinearInterpolator
LinearInterpolator
From source file:be.ugent.maf.cellmissy.analysis.singlecell.processing.impl.interpolation.TrackLinearInterpolator.java
@Override public InterpolatedTrack interpolateTrack(double[] time, double[] x, double[] y) { // create a new linear interpolator LinearInterpolator linearInterpolator = new LinearInterpolator(); 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 . ja v a 2 s . com // call the interpolator, and actually do the interpolation try { PolynomialSplineFunction functionX = linearInterpolator.interpolate(time, x); PolynomialSplineFunction functionY = linearInterpolator.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: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 2s .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); 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.itemanalysis.psychometrics.scaling.PercentileRank.java
/** * Creates a TreeMap<Integer, Double> lookup table of percentile ranks. * The key is a score level and the rho is a percentile rank. This * method is useful when finding percentile ranks that correspond to an examinee's * raw score. After calling this method, individual elements in the * TreeMap can be accessed with getPercentileRankAt(int rho) or * valueIterator().//from w w w . j a v a2s . c o m * */ public void createLookupTable() { prankTable = new TreeMap<Integer, Double>(); Iterator<Comparable<?>> iter = freqTable.valuesIterator(); int index = 0; int length = freqTable.getUniqueCount(); double[] xval = new double[length + 2]; double[] yval = new double[length + 2]; int x = 0; int xstar = 0; //create boundaries below the minimum possible test score //and above teh maximum possible test score. //This change allows for the interpolation of percentile //ranks at the min and max possible test score. xval[0] = min - .5; yval[0] = 0; xval[length + 1] = (double) max + 0.5; yval[length + 1] = 100; index = 1; //compute percentile ranks fro observed scores while (iter.hasNext()) { x = ((Long) iter.next()).intValue(); xstar = Double.valueOf(Math.floor(x + 0.5)).intValue(); xval[index] = Double.valueOf(xstar); yval[index] = Double.valueOf(percentileRank(x, xstar)); index++; } //interpolate values LinearInterpolator interpolator = new LinearInterpolator(); PolynomialSplineFunction splineFunction = interpolator.interpolate(xval, yval); //create lookup table with interpolated values x = min; double y = 0.0; while (x <= max) { y = splineFunction.value(x); prankTable.put(x, y); x += 1; } }
From source file:com.itemanalysis.psychometrics.kernel.KernelDensity.java
public UnivariateFunction getInterpolater() { UnivariateInterpolator interpolator = new LinearInterpolator(); UnivariateFunction func = interpolator.interpolate(points, evaluate()); return func;// w ww .ja va 2s.com }
From source file:com.itemanalysis.psychometrics.kernel.KernelDensity.java
/** * Returns interpolator for squared evaluate * * @return/*from w w w .j av a 2s . c o m*/ */ public UnivariateFunction getInterpolater2() { UnivariateInterpolator interpolator = new LinearInterpolator(); double[] d = evaluate(); for (int i = 0; i < d.length; i++) { d[i] = d[i] * d[i]; } UnivariateFunction func = interpolator.interpolate(points, d); return func; }
From source file:malware_classification.Malware_Classification.java
private ArrayList<double[]> interpolate_data(ArrayList<double[]> data_orig, ArrayList<double[]> timestamp_orig, int bin_size, int num_bins) { ArrayList<double[]> interp_data = new ArrayList<>(); int num_points_orig = data_orig.size(); // TODO: change some of the num_points_orig to num_bins double[] x = new double[num_points_orig]; double[] y = new double[num_points_orig]; int num_cols = data_orig.get(0).length; for (int i = 0; i < num_bins; i++) { interp_data.add(new double[num_cols]); }/* w w w . j ava 2s . c o m*/ for (int col = 0; col < num_cols; col++) { // To use LinearInterpolator, first need arrays for (int j = 0; j < num_points_orig; j++) { x[j] = timestamp_orig.get(j)[col]; y[j] = data_orig.get(j)[col]; } LinearInterpolator lin_interp = new LinearInterpolator(); PolynomialSplineFunction interp_func = lin_interp.interpolate(x, y); for (int j = 0; j < num_bins; j++) { double curr_bin = bin_size * j; double[] knots = interp_func.getKnots(); // logger.info() if (interp_func.isValidPoint(curr_bin)) { interp_data.get(j)[col] = interp_func.value(curr_bin); } else if (knots[0] > curr_bin) //bin is too small { interp_data.get(j)[col] = y[0]; } else if (knots[knots.length - 1] < curr_bin) // bin is larger than data { interp_data.get(j)[col] = y[y.length - 1]; } else { logger.warning("Cannot interpolate at bin starting at " + curr_bin); } } } return interp_data; }
From source file:gdsc.smlm.ij.plugins.DriftCalculator.java
private void interpolate(double[] dx, double[] dy, double[] originalDriftTimePoints) { // Interpolator can only create missing values within the range provided by the input values. // The two ends have to be extrapolated. // TODO: Perform extrapolation. Currently the end values are used. // Find end points int startT = 0; while (originalDriftTimePoints[startT] == 0) startT++;/*from w w w.j a va2 s .c om*/ int endT = originalDriftTimePoints.length - 1; while (originalDriftTimePoints[endT] == 0) endT--; // Extrapolate using a constant value for (int t = startT; t-- > 0;) { dx[t] = dx[startT]; dy[t] = dy[startT]; } for (int t = endT; ++t < dx.length;) { dx[t] = dx[endT]; dy[t] = dy[endT]; } double[][] values = extractValues(originalDriftTimePoints, startT, endT, dx, dy); PolynomialSplineFunction fx, fy; if (values[0].length < 3) { fx = new LinearInterpolator().interpolate(values[0], values[1]); fy = new LinearInterpolator().interpolate(values[0], values[2]); } else { fx = new SplineInterpolator().interpolate(values[0], values[1]); fy = new SplineInterpolator().interpolate(values[0], values[2]); } for (int t = startT; t <= endT; t++) { if (originalDriftTimePoints[t] == 0) { dx[t] = fx.value(t); dy[t] = fy.value(t); } } this.interpolationStart = startT; this.interpolationEnd = endT; }
From source file:org.apache.solr.client.solrj.io.eval.LerpEvaluator.java
@Override public Object doWork(Object... objects) throws IOException { Object first = objects[0];/* w ww . j av a2 s.co m*/ double[] x = null; double[] y = null; if (objects.length == 1) { //Only the y values passed y = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(); x = new double[y.length]; for (int i = 0; i < y.length; i++) { x[i] = i; } } else if (objects.length == 2) { Object second = objects[1]; x = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(); y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(); } LinearInterpolator interpolator = new LinearInterpolator(); PolynomialSplineFunction spline = interpolator.interpolate(x, y); List<Number> list = new ArrayList(); for (double xvalue : x) { list.add(spline.value(xvalue)); } VectorFunction vec = new VectorFunction(spline, list); vec.addToContext("x", x); vec.addToContext("y", y); return vec; }
From source file:org.esa.s3tbx.olci.radiometry.rayleigh.RayleighAux.java
public static void initDefaultAuxiliary() { try {//from w w w . j ava2 s . co m ElevationModelDescriptor getasse30 = ElevationModelRegistry.getInstance().getDescriptor(GETASSE_30); elevationModel = getasse30.createDem(Resampling.NEAREST_NEIGHBOUR); Path coeffMatrix = installAuxdata().resolve(COEFF_MATRIX_TXT); JSONParser jsonObject = new JSONParser(); JSONObject parse = (JSONObject) jsonObject.parse(new FileReader(coeffMatrix.toString())); tau_ray = parseJSON1DimArray(parse, TAU_RAY); thetas = parseJSON1DimArray(parse, THETA); ArrayList<double[][][]> ray_coeff_matrix = parseJSON3DimArray(parse, RAY_COEFF_MATRIX); rayCooefMatrixA = ray_coeff_matrix.get(0); rayCooefMatrixB = ray_coeff_matrix.get(1); rayCooefMatrixC = ray_coeff_matrix.get(2); rayCooefMatrixD = ray_coeff_matrix.get(3); double[] lineSpace = getLineSpace(0, 1, 17); double[] rayAlbedoLuts = parseJSON1DimArray(parse, RAY_ALBEDO_LUT); linearInterpolate = new LinearInterpolator().interpolate(lineSpace, rayAlbedoLuts); } catch (IOException | ParseException e) { e.printStackTrace(); } }
From source file:org.lightjason.agentspeak.action.builtin.TestCActionMathInterpolate.java
/** * test single interpolate//from www . j ava2s .co m */ @Test public final void singleinterpolate() { final List<ITerm> l_return = new ArrayList<>(); new CSingleInterpolate().execute(false, IContext.EMPTYPLAN, Stream .of(new LinearInterpolator().interpolate(new double[] { 3, 6 }, new double[] { 11, 13 }), 3, 4) .map(CRawTerm::from).collect(Collectors.toList()), l_return); Assert.assertEquals(l_return.size(), 2); Assert.assertEquals(l_return.get(0).<Number>raw(), 11.0); Assert.assertEquals(l_return.get(1).<Number>raw(), 11.666666666666666); }