Java Code Examples for org.apache.commons.math3.util.MathArrays#sortInPlace()
The following examples show how to use
org.apache.commons.math3.util.MathArrays#sortInPlace() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PolynomialFunctionLagrangeForm.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Evaluate the Lagrange polynomial using * <a href="http://mathworld.wolfram.com/NevillesAlgorithm.html"> * Neville's Algorithm</a>. It takes O(n^2) time. * * @param x Interpolating points array. * @param y Interpolating values array. * @param z Point at which the function value is to be computed. * @return the function value. * @throws DimensionMismatchException if {@code x} and {@code y} have * different lengths. * @throws NonMonotonicSequenceException * if {@code x} is not sorted in strictly increasing order. * @throws NumberIsTooSmallException if the size of {@code x} is less * than 2. */ public static double evaluate(double x[], double y[], double z) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { if (verifyInterpolationArray(x, y, false)) { return evaluateInternal(x, y, z); } // Array is not sorted. final double[] xNew = new double[x.length]; final double[] yNew = new double[y.length]; System.arraycopy(x, 0, xNew, 0, x.length); System.arraycopy(y, 0, yNew, 0, y.length); MathArrays.sortInPlace(xNew, yNew); // Second check in case some abscissa is duplicated. verifyInterpolationArray(xNew, yNew, true); return evaluateInternal(xNew, yNew, z); }
Example 2
Source File: PolynomialFunctionLagrangeForm.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Evaluate the Lagrange polynomial using * <a href="http://mathworld.wolfram.com/NevillesAlgorithm.html"> * Neville's Algorithm</a>. It takes O(n^2) time. * * @param x Interpolating points array. * @param y Interpolating values array. * @param z Point at which the function value is to be computed. * @return the function value. * @throws DimensionMismatchException if {@code x} and {@code y} have * different lengths. * @throws NonMonotonicSequenceException * if {@code x} is not sorted in strictly increasing order. * @throws NumberIsTooSmallException if the size of {@code x} is less * than 2. */ public static double evaluate(double x[], double y[], double z) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { if (verifyInterpolationArray(x, y, false)) { return evaluateInternal(x, y, z); } // Array is not sorted. final double[] xNew = new double[x.length]; final double[] yNew = new double[y.length]; System.arraycopy(x, 0, xNew, 0, x.length); System.arraycopy(y, 0, yNew, 0, y.length); MathArrays.sortInPlace(xNew, yNew); // Second check in case some abscissa is duplicated. verifyInterpolationArray(xNew, yNew, true); return evaluateInternal(xNew, yNew, z); }
Example 3
Source File: PolynomialFunctionLagrangeForm.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Evaluate the Lagrange polynomial using * <a href="http://mathworld.wolfram.com/NevillesAlgorithm.html"> * Neville's Algorithm</a>. It takes O(n^2) time. * * @param x Interpolating points array. * @param y Interpolating values array. * @param z Point at which the function value is to be computed. * @return the function value. * @throws DimensionMismatchException if {@code x} and {@code y} have * different lengths. * @throws NonMonotonicSequenceException * if {@code x} is not sorted in strictly increasing order. * @throws NumberIsTooSmallException if the size of {@code x} is less * than 2. */ public static double evaluate(double x[], double y[], double z) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { if (verifyInterpolationArray(x, y, false)) { return evaluateInternal(x, y, z); } // Array is not sorted. final double[] xNew = new double[x.length]; final double[] yNew = new double[y.length]; System.arraycopy(x, 0, xNew, 0, x.length); System.arraycopy(y, 0, yNew, 0, y.length); MathArrays.sortInPlace(xNew, yNew); // Second check in case some abscissa is duplicated. verifyInterpolationArray(xNew, yNew, true); return evaluateInternal(xNew, yNew, z); }
Example 4
Source File: PolynomialFunctionLagrangeForm.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Evaluate the Lagrange polynomial using * <a href="http://mathworld.wolfram.com/NevillesAlgorithm.html"> * Neville's Algorithm</a>. It takes O(n^2) time. * * @param x Interpolating points array. * @param y Interpolating values array. * @param z Point at which the function value is to be computed. * @return the function value. * @throws DimensionMismatchException if {@code x} and {@code y} have * different lengths. * @throws NonMonotonicSequenceException * if {@code x} is not sorted in strictly increasing order. * @throws NumberIsTooSmallException if the size of {@code x} is less * than 2. */ public static double evaluate(double x[], double y[], double z) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { if (verifyInterpolationArray(x, y, false)) { return evaluateInternal(x, y, z); } // Array is not sorted. final double[] xNew = new double[x.length]; final double[] yNew = new double[y.length]; System.arraycopy(x, 0, xNew, 0, x.length); System.arraycopy(y, 0, yNew, 0, y.length); MathArrays.sortInPlace(xNew, yNew); // Second check in case some abscissa is duplicated. verifyInterpolationArray(xNew, yNew, true); return evaluateInternal(xNew, yNew, z); }
Example 5
Source File: PolynomialFunctionLagrangeForm.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Evaluate the Lagrange polynomial using * <a href="http://mathworld.wolfram.com/NevillesAlgorithm.html"> * Neville's Algorithm</a>. It takes O(n^2) time. * * @param x Interpolating points array. * @param y Interpolating values array. * @param z Point at which the function value is to be computed. * @return the function value. * @throws DimensionMismatchException if {@code x} and {@code y} have * different lengths. * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException * if {@code x} is not sorted in strictly increasing order. * @throws NumberIsTooSmallException if the size of {@code x} is less * than 2. */ public static double evaluate(double x[], double y[], double z) { if (verifyInterpolationArray(x, y, false)) { return evaluateInternal(x, y, z); } // Array is not sorted. final double[] xNew = new double[x.length]; final double[] yNew = new double[y.length]; System.arraycopy(x, 0, xNew, 0, x.length); System.arraycopy(y, 0, yNew, 0, y.length); MathArrays.sortInPlace(xNew, yNew); // Second check in case some abscissa is duplicated. verifyInterpolationArray(xNew, yNew, true); return evaluateInternal(xNew, yNew, z); }
Example 6
Source File: PolynomialFunctionLagrangeForm.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Evaluate the Lagrange polynomial using * <a href="http://mathworld.wolfram.com/NevillesAlgorithm.html"> * Neville's Algorithm</a>. It takes O(n^2) time. * * @param x Interpolating points array. * @param y Interpolating values array. * @param z Point at which the function value is to be computed. * @return the function value. * @throws DimensionMismatchException if {@code x} and {@code y} have * different lengths. * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException * if {@code x} is not sorted in strictly increasing order. * @throws NumberIsTooSmallException if the size of {@code x} is less * than 2. */ public static double evaluate(double x[], double y[], double z) { if (verifyInterpolationArray(x, y, false)) { return evaluateInternal(x, y, z); } // Array is not sorted. final double[] xNew = new double[x.length]; final double[] yNew = new double[y.length]; System.arraycopy(x, 0, xNew, 0, x.length); System.arraycopy(y, 0, yNew, 0, y.length); MathArrays.sortInPlace(xNew, yNew); // Second check in case some abscissa is duplicated. verifyInterpolationArray(xNew, yNew, true); return evaluateInternal(xNew, yNew, z); }
Example 7
Source File: PolynomialFunctionLagrangeForm.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Evaluate the Lagrange polynomial using * <a href="http://mathworld.wolfram.com/NevillesAlgorithm.html"> * Neville's Algorithm</a>. It takes O(n^2) time. * * @param x Interpolating points array. * @param y Interpolating values array. * @param z Point at which the function value is to be computed. * @return the function value. * @throws DimensionMismatchException if {@code x} and {@code y} have * different lengths. * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException * if {@code x} is not sorted in strictly increasing order. * @throws NumberIsTooSmallException if the size of {@code x} is less * than 2. */ public static double evaluate(double x[], double y[], double z) { if (verifyInterpolationArray(x, y, false)) { return evaluateInternal(x, y, z); } // Array is not sorted. final double[] xNew = new double[x.length]; final double[] yNew = new double[y.length]; System.arraycopy(x, 0, xNew, 0, x.length); System.arraycopy(y, 0, yNew, 0, y.length); MathArrays.sortInPlace(xNew, yNew); // Second check in case some abscissa is duplicated. verifyInterpolationArray(xNew, yNew, true); return evaluateInternal(xNew, yNew, z); }
Example 8
Source File: PolynomialFunctionLagrangeForm.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Evaluate the Lagrange polynomial using * <a href="http://mathworld.wolfram.com/NevillesAlgorithm.html"> * Neville's Algorithm</a>. It takes O(n^2) time. * * @param x Interpolating points array. * @param y Interpolating values array. * @param z Point at which the function value is to be computed. * @return the function value. * @throws DimensionMismatchException if {@code x} and {@code y} have * different lengths. * @throws NonMonotonicSequenceException * if {@code x} is not sorted in strictly increasing order. * @throws NumberIsTooSmallException if the size of {@code x} is less * than 2. */ public static double evaluate(double x[], double y[], double z) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { if (verifyInterpolationArray(x, y, false)) { return evaluateInternal(x, y, z); } // Array is not sorted. final double[] xNew = new double[x.length]; final double[] yNew = new double[y.length]; System.arraycopy(x, 0, xNew, 0, x.length); System.arraycopy(y, 0, yNew, 0, y.length); MathArrays.sortInPlace(xNew, yNew); // Second check in case some abscissa is duplicated. verifyInterpolationArray(xNew, yNew, true); return evaluateInternal(xNew, yNew, z); }
Example 9
Source File: PolynomialFunctionLagrangeForm.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Construct a Lagrange polynomial with the given abscissas and function * values. The order of interpolating points are not important. * <p> * The constructor makes copy of the input arrays and assigns them.</p> * * @param x interpolating points * @param y function values at interpolating points * @throws DimensionMismatchException if the array lengths are different. * @throws NumberIsTooSmallException if the number of points is less than 2. * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException * if two abscissae have the same value. */ public PolynomialFunctionLagrangeForm(double x[], double y[]) { this.x = new double[x.length]; this.y = new double[y.length]; System.arraycopy(x, 0, this.x, 0, x.length); System.arraycopy(y, 0, this.y, 0, y.length); coefficientsComputed = false; if (!verifyInterpolationArray(x, y, false)) { MathArrays.sortInPlace(this.x, this.y); // Second check in case some abscissa is duplicated. verifyInterpolationArray(this.x, this.y, true); } }
Example 10
Source File: PolynomialFunctionLagrangeForm.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Construct a Lagrange polynomial with the given abscissas and function * values. The order of interpolating points are not important. * <p> * The constructor makes copy of the input arrays and assigns them.</p> * * @param x interpolating points * @param y function values at interpolating points * @throws DimensionMismatchException if the array lengths are different. * @throws NumberIsTooSmallException if the number of points is less than 2. * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException * if two abscissae have the same value. */ public PolynomialFunctionLagrangeForm(double x[], double y[]) { this.x = new double[x.length]; this.y = new double[y.length]; System.arraycopy(x, 0, this.x, 0, x.length); System.arraycopy(y, 0, this.y, 0, y.length); coefficientsComputed = false; if (!verifyInterpolationArray(x, y, false)) { MathArrays.sortInPlace(this.x, this.y); // Second check in case some abscissa is duplicated. verifyInterpolationArray(this.x, this.y, true); } }
Example 11
Source File: PolynomialFunctionLagrangeForm.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Construct a Lagrange polynomial with the given abscissas and function * values. The order of interpolating points are not important. * <p> * The constructor makes copy of the input arrays and assigns them.</p> * * @param x interpolating points * @param y function values at interpolating points * @throws DimensionMismatchException if the array lengths are different. * @throws NumberIsTooSmallException if the number of points is less than 2. * @throws NonMonotonicSequenceException * if two abscissae have the same value. */ public PolynomialFunctionLagrangeForm(double x[], double y[]) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { this.x = new double[x.length]; this.y = new double[y.length]; System.arraycopy(x, 0, this.x, 0, x.length); System.arraycopy(y, 0, this.y, 0, y.length); coefficientsComputed = false; if (!verifyInterpolationArray(x, y, false)) { MathArrays.sortInPlace(this.x, this.y); // Second check in case some abscissa is duplicated. verifyInterpolationArray(this.x, this.y, true); } }
Example 12
Source File: DatasetUtils.java From january with Eclipse Public License 1.0 | 5 votes |
/** * Sort in place given dataset and reorder ancillary datasets too * @param a dataset to be sorted * @param b ancillary datasets */ public static void sort(Dataset a, Dataset... b) { if (!InterfaceUtils.isNumerical(a.getClass())) { throw new UnsupportedOperationException("Sorting non-numerical datasets not supported yet"); } // gather all datasets as double dataset copies DoubleDataset s = copy(DoubleDataset.class, a); int l = b == null ? 0 : b.length; DoubleDataset[] t = new DoubleDataset[l]; int n = 0; for (int i = 0; i < l; i++) { if (b[i] != null) { if (!InterfaceUtils.isNumerical(b[i].getClass())) { throw new UnsupportedOperationException("Sorting non-numerical datasets not supported yet"); } t[i] = copy(DoubleDataset.class, b[i]); n++; } } double[][] y = new double[n][]; for (int i = 0, j = 0; i < l; i++) { if (t[i] != null) { y[j++] = t[i].getData(); } } MathArrays.sortInPlace(s.getData(), y); a.setSlice(s); for (int i = 0; i < l; i++) { if (b[i] != null) { b[i].setSlice(t[i]); } } }
Example 13
Source File: PolynomialFunctionLagrangeForm.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Construct a Lagrange polynomial with the given abscissas and function * values. The order of interpolating points are not important. * <p> * The constructor makes copy of the input arrays and assigns them.</p> * * @param x interpolating points * @param y function values at interpolating points * @throws DimensionMismatchException if the array lengths are different. * @throws NumberIsTooSmallException if the number of points is less than 2. * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException * if two abscissae have the same value. */ public PolynomialFunctionLagrangeForm(double x[], double y[]) { this.x = new double[x.length]; this.y = new double[y.length]; System.arraycopy(x, 0, this.x, 0, x.length); System.arraycopy(y, 0, this.y, 0, y.length); coefficientsComputed = false; if (!verifyInterpolationArray(x, y, false)) { MathArrays.sortInPlace(this.x, this.y); // Second check in case some abscissa is duplicated. verifyInterpolationArray(this.x, this.y, true); } }
Example 14
Source File: PolynomialFunctionLagrangeForm.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Construct a Lagrange polynomial with the given abscissas and function * values. The order of interpolating points are not important. * <p> * The constructor makes copy of the input arrays and assigns them.</p> * * @param x interpolating points * @param y function values at interpolating points * @throws DimensionMismatchException if the array lengths are different. * @throws NumberIsTooSmallException if the number of points is less than 2. * @throws NonMonotonicSequenceException * if two abscissae have the same value. */ public PolynomialFunctionLagrangeForm(double x[], double y[]) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { this.x = new double[x.length]; this.y = new double[y.length]; System.arraycopy(x, 0, this.x, 0, x.length); System.arraycopy(y, 0, this.y, 0, y.length); coefficientsComputed = false; if (!verifyInterpolationArray(x, y, false)) { MathArrays.sortInPlace(this.x, this.y); // Second check in case some abscissa is duplicated. verifyInterpolationArray(this.x, this.y, true); } }
Example 15
Source File: PolynomialFunctionLagrangeForm.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Construct a Lagrange polynomial with the given abscissas and function * values. The order of interpolating points are not important. * <p> * The constructor makes copy of the input arrays and assigns them.</p> * * @param x interpolating points * @param y function values at interpolating points * @throws DimensionMismatchException if the array lengths are different. * @throws NumberIsTooSmallException if the number of points is less than 2. * @throws NonMonotonicSequenceException * if two abscissae have the same value. */ public PolynomialFunctionLagrangeForm(double x[], double y[]) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { this.x = new double[x.length]; this.y = new double[y.length]; System.arraycopy(x, 0, this.x, 0, x.length); System.arraycopy(y, 0, this.y, 0, y.length); coefficientsComputed = false; if (!verifyInterpolationArray(x, y, false)) { MathArrays.sortInPlace(this.x, this.y); // Second check in case some abscissa is duplicated. verifyInterpolationArray(this.x, this.y, true); } }
Example 16
Source File: PolynomialFunctionLagrangeForm.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Construct a Lagrange polynomial with the given abscissas and function * values. The order of interpolating points are not important. * <p> * The constructor makes copy of the input arrays and assigns them.</p> * * @param x interpolating points * @param y function values at interpolating points * @throws DimensionMismatchException if the array lengths are different. * @throws NumberIsTooSmallException if the number of points is less than 2. * @throws NonMonotonicSequenceException * if two abscissae have the same value. */ public PolynomialFunctionLagrangeForm(double x[], double y[]) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { this.x = new double[x.length]; this.y = new double[y.length]; System.arraycopy(x, 0, this.x, 0, x.length); System.arraycopy(y, 0, this.y, 0, y.length); coefficientsComputed = false; if (!verifyInterpolationArray(x, y, false)) { MathArrays.sortInPlace(this.x, this.y); // Second check in case some abscissa is duplicated. verifyInterpolationArray(this.x, this.y, true); } }
Example 17
Source File: PSquarePercentile.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Estimate the quantile for the current marker. * * @return estimated quantile */ private double estimate() { final double di = difference(); final boolean isNextHigher = next.intMarkerPosition - intMarkerPosition > 1; final boolean isPreviousLower = previous.intMarkerPosition - intMarkerPosition < -1; if (di >= 1 && isNextHigher || di <= -1 && isPreviousLower) { final int d = di >= 0 ? 1 : -1; final double[] xval = new double[] { previous.intMarkerPosition, intMarkerPosition, next.intMarkerPosition }; final double[] yval = new double[] { previous.markerHeight, markerHeight, next.markerHeight }; final double xD = intMarkerPosition + d; UnivariateFunction univariateFunction = nonLinear.interpolate(xval, yval); markerHeight = univariateFunction.value(xD); // If parabolic estimate is bad then turn linear if (isEstimateBad(yval, markerHeight)) { int delta = xD - xval[1] > 0 ? 1 : -1; final double[] xBad = new double[] { xval[1], xval[1 + delta] }; final double[] yBad = new double[] { yval[1], yval[1 + delta] }; MathArrays.sortInPlace(xBad, yBad);// since d can be +/- 1 univariateFunction = linear.interpolate(xBad, yBad); markerHeight = univariateFunction.value(xD); } incrementPosition(d); } return markerHeight; }
Example 18
Source File: PSquarePercentile.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Estimate the quantile for the current marker. * * @return estimated quantile */ private double estimate() { final double di = difference(); final boolean isNextHigher = next.intMarkerPosition - intMarkerPosition > 1; final boolean isPreviousLower = previous.intMarkerPosition - intMarkerPosition < -1; if (di >= 1 && isNextHigher || di <= -1 && isPreviousLower) { final int d = di >= 0 ? 1 : -1; final double[] xval = new double[] { previous.intMarkerPosition, intMarkerPosition, next.intMarkerPosition }; final double[] yval = new double[] { previous.markerHeight, markerHeight, next.markerHeight }; final double xD = intMarkerPosition + d; UnivariateFunction univariateFunction = nonLinear.interpolate(xval, yval); markerHeight = univariateFunction.value(xD); // If parabolic estimate is bad then turn linear if (isEstimateBad(yval, markerHeight)) { int delta = xD - xval[1] > 0 ? 1 : -1; final double[] xBad = new double[] { xval[1], xval[1 + delta] }; final double[] yBad = new double[] { yval[1], yval[1 + delta] }; MathArrays.sortInPlace(xBad, yBad);// since d can be +/- 1 univariateFunction = linear.interpolate(xBad, yBad); markerHeight = univariateFunction.value(xD); } incrementPosition(d); } return markerHeight; }
Example 19
Source File: PolynomialFunctionLagrangeForm.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Construct a Lagrange polynomial with the given abscissas and function * values. The order of interpolating points are not important. * <p> * The constructor makes copy of the input arrays and assigns them.</p> * * @param x interpolating points * @param y function values at interpolating points * @throws DimensionMismatchException if the array lengths are different. * @throws NumberIsTooSmallException if the number of points is less than 2. * @throws NonMonotonicSequenceException * if two abscissae have the same value. */ public PolynomialFunctionLagrangeForm(double x[], double y[]) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { this.x = new double[x.length]; this.y = new double[y.length]; System.arraycopy(x, 0, this.x, 0, x.length); System.arraycopy(y, 0, this.y, 0, y.length); coefficientsComputed = false; if (!verifyInterpolationArray(x, y, false)) { MathArrays.sortInPlace(this.x, this.y); // Second check in case some abscissa is duplicated. verifyInterpolationArray(this.x, this.y, true); } }
Example 20
Source File: CorrelationDriftEstimator.java From thunderstorm with GNU General Public License v3.0 | 5 votes |
private static BinningResults binResultByFrame(double[] x, double[] y, double[] frame, int binCount) { double minFrame = findMinFrame(frame); double maxFrame = findMaxFrame(frame); if(maxFrame == minFrame) { throw new RuntimeException("Requires multiple frames."); } MathArrays.sortInPlace(frame, x, y); int detectionsPerBin = frame.length / binCount; //alloc space for binned results double[][] xBinnedByFrame = new double[binCount][]; double[][] yBinnedByFrame = new double[binCount][]; double[] binCenters = new double[binCount]; int currentPos = 0; for(int i = 0; i < binCount; i++) { int endPos = currentPos + detectionsPerBin; if(endPos >= frame.length || i == binCount - 1) { endPos = frame.length; } else { double frameAtEndPos = frame[endPos - 1]; while(endPos < frame.length - 1 && frame[endPos] == frameAtEndPos) { endPos++; } } if(currentPos > frame.length - 1) { xBinnedByFrame[i] = new double[0]; yBinnedByFrame[i] = new double[0]; binCenters[i] = maxFrame; } else { xBinnedByFrame[i] = Arrays.copyOfRange(x, currentPos, endPos); yBinnedByFrame[i] = Arrays.copyOfRange(y, currentPos, endPos); binCenters[i] = (frame[currentPos] + frame[endPos - 1]) / 2; } currentPos = endPos; } return new BinningResults(xBinnedByFrame, yBinnedByFrame, binCenters, (int) minFrame, (int) maxFrame); }