/** Check interpolation can be performed. * @exception NoDataException if interpolation cannot be performed * because sample is empty */ private void checkInterpolation() throws NoDataException { if (abscissae.isEmpty()) { throw new NoDataException(LocalizedFormats.EMPTY_INTERPOLATION_SAMPLE); } }
/** * Get the elements type from an array. * * @param <T> Type of the field elements. * @param d Data array. * @return the field to which the array elements belong. * @throws NoDataException if array is empty. */ protected static <T extends FieldElement<T>> Field<T> extractField(final T[] d) throws NoDataException { if (d.length == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } return d[0].getField(); }
/** * Ensures that the provided arrays fulfills the assumptions. * * @param x first sample * @param y second sample * @throws NullArgumentException if {@code x} or {@code y} are {@code null}. * @throws NoDataException if {@code x} or {@code y} are zero-length. */ private void ensureDataConformance(final double[] x, final double[] y) throws NullArgumentException, NoDataException { if (x == null || y == null) { throw new NullArgumentException(); } if (x.length == 0 || y.length == 0) { throw new NoDataException(); } }
/** * Get the elements type from an array. * * @param <T> Type of the field elements. * @param d Data array. * @return the field to which the array elements belong. * @throws NullArgumentException if the array is {@code null}. * @throws NoDataException if the array is empty. */ protected static <T extends FieldElement<T>> Field<T> extractField(final T[][] d) throws NoDataException, NullArgumentException { if (d == null) { throw new NullArgumentException(); } if (d.length == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } if (d[0].length == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } return d[0][0].getField(); }
/** * Uses Horner's Method to evaluate the polynomial with the given coefficients at * the argument. * * @param coefficients Coefficients of the polynomial to evaluate. * @param argument Input value. * @return the value of the polynomial. * @throws NoDataException if {@code coefficients} is empty. * @throws NullArgumentException if {@code coefficients} is {@code null}. */ protected static double evaluate(double[] coefficients, double argument) throws NullArgumentException, NoDataException { MathUtils.checkNotNull(coefficients); int n = coefficients.length; if (n == 0) { throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY); } double result = coefficients[n - 1]; for (int j = n - 2; j >= 0; j--) { result = argument * result + coefficients[j]; } return result; }
/** * Returns the coefficients of the derivative of the polynomial with the given coefficients. * * @param coefficients Coefficients of the polynomial to differentiate. * @return the coefficients of the derivative or {@code null} if coefficients has length 1. * @throws NoDataException if {@code coefficients} is empty. * @throws NullArgumentException if {@code coefficients} is {@code null}. */ protected static double[] differentiate(double[] coefficients) throws NullArgumentException, NoDataException { MathUtils.checkNotNull(coefficients); int n = coefficients.length; if (n == 0) { throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY); } if (n == 1) { return new double[]{0}; } double[] result = new double[n - 1]; for (int i = n - 1; i > 0; i--) { result[i - 1] = i * coefficients[i]; } return result; }
/** * Returns the sum of the (signed) differences between corresponding elements of the * input arrays -- i.e., sum(sample1[i] - sample2[i]). * * @param sample1 the first array * @param sample2 the second array * @return sum of paired differences * @throws DimensionMismatchException if the arrays do not have the same * (positive) length. * @throws NoDataException if the sample arrays are empty. */ public static double sumDifference(final double[] sample1, final double[] sample2) throws DimensionMismatchException, NoDataException { int n = sample1.length; if (n != sample2.length) { throw new DimensionMismatchException(n, sample2.length); } if (n <= 0) { throw new NoDataException(LocalizedFormats.INSUFFICIENT_DIMENSION); } double result = 0; for (int i = 0; i < n; i++) { result += sample1[i] - sample2[i]; } return result; }
/** * Construct a polynomial with the given coefficients. The first element * of the coefficients array is the constant term. Higher degree * coefficients follow in sequence. The degree of the resulting polynomial * is the index of the last non-null element of the array, or 0 if all elements * are null. * <p> * The constructor makes a copy of the input array and assigns the copy to * the coefficients property.</p> * * @param c Polynomial coefficients. * @throws NullArgumentException if {@code c} is {@code null}. * @throws NoDataException if {@code c} is empty. */ public PolynomialFunction(double c[]) throws NullArgumentException, NoDataException { super(); MathUtils.checkNotNull(c); int n = c.length; if (n == 0) { throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY); } while ((n > 1) && (c[n - 1] == 0)) { --n; } this.coefficients = new double[n]; System.arraycopy(c, 0, this.coefficients, 0, n); }
/** * Loads new y sample data, overriding any previous data. * * @param y the array representing the y sample * @throws NullArgumentException if y is null * @throws NoDataException if y is empty */ protected void newYSampleData(double[] y) { if (y == null) { throw new NullArgumentException(); } if (y.length == 0) { throw new NoDataException(); } this.yVector = new ArrayRealVector(y); }
final int nRows = d.length; if (nRows == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN);
/** * Calculates |z[i]| for all i * * @param z sample * @return |z| * @throws NullArgumentException if {@code z} is {@code null} * @throws NoDataException if {@code z} is zero-length. */ private double[] calculateAbsoluteDifferences(final double[] z) throws NullArgumentException, NoDataException { if (z == null) { throw new NullArgumentException(); } if (z.length == 0) { throw new NoDataException(); } final double[] zAbs = new double[z.length]; for (int i = 0; i < z.length; ++i) { zAbs[i] = FastMath.abs(z[i]); } return zAbs; }
throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN);
/** * Ensures that the provided arrays fulfills the assumptions. * * @param x first sample * @param y second sample * @throws NullArgumentException if {@code x} or {@code y} are {@code null}. * @throws NoDataException if {@code x} or {@code y} are zero-length. * @throws DimensionMismatchException if {@code x} and {@code y} do not * have the same length. */ private void ensureDataConformance(final double[] x, final double[] y) throws NullArgumentException, NoDataException, DimensionMismatchException { if (x == null || y == null) { throw new NullArgumentException(); } if (x.length == 0 || y.length == 0) { throw new NoDataException(); } if (y.length != x.length) { throw new DimensionMismatchException(y.length, x.length); } }
throw new NoDataException(LocalizedFormats.EMPTY_SELECTED_ROW_INDEX_ARRAY); throw new NoDataException(LocalizedFormats.EMPTY_SELECTED_COLUMN_INDEX_ARRAY);
final int nRows = subMatrix.length; if (nRows == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN);
/** * Check if submatrix ranges indices are valid. * Rows and columns are indicated counting from 0 to n-1. * * @param selectedRows Array of row indices. * @param selectedColumns Array of column indices. * @throws NullArgumentException if the arrays are {@code null}. * @throws NoDataException if the arrays have zero length. * @throws OutOfRangeException if row or column selections are not valid. */ protected void checkSubMatrixIndex(final int[] selectedRows, final int[] selectedColumns) throws NoDataException, NullArgumentException, OutOfRangeException { if (selectedRows == null || selectedColumns == null) { throw new NullArgumentException(); } if (selectedRows.length == 0 || selectedColumns.length == 0) { throw new NoDataException(); } for (final int row : selectedRows) { checkRowIndex(row); } for (final int column : selectedColumns) { checkColumnIndex(column); } }
/** * Computes the quantization error. * The quantization error is the average distance between a feature vector * and its "best matching unit" (closest neuron). * * @param data Feature vectors. * @param neurons List of neurons to scan. * @param distance Distance function. * @return the error. * @throws NoDataException if {@code data} is empty. */ public static double computeQuantizationError(Iterable<double[]> data, Iterable<Neuron> neurons, DistanceMeasure distance) { double d = 0; int count = 0; for (double[] f : data) { ++count; d += distance.compute(f, findBest(f, neurons, distance).getFeatures()); } if (count == 0) { throw new NoDataException(); } return d / count; }
throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN);
/** Interpolate value at a specified abscissa. * @param x interpolation abscissa * @return interpolated value * @exception NoDataException if sample is empty * @throws NullArgumentException if x is null */ public T[] value(T x) throws NoDataException, NullArgumentException { // safety check MathUtils.checkNotNull(x); if (abscissae.isEmpty()) { throw new NoDataException(LocalizedFormats.EMPTY_INTERPOLATION_SAMPLE); } final T[] value = MathArrays.buildArray(x.getField(), topDiagonal.get(0).length); T valueCoeff = x.getField().getOne(); for (int i = 0; i < topDiagonal.size(); ++i) { T[] dividedDifference = topDiagonal.get(i); for (int k = 0; k < value.length; ++k) { value[k] = value[k].add(dividedDifference[k].multiply(valueCoeff)); } final T deltaX = x.subtract(abscissae.get(i)); valueCoeff = valueCoeff.multiply(deltaX); } return value; }