/** * Rounds the given value to the specified number of decimal places. * The value is rounded using the {@link BigDecimal#ROUND_HALF_UP} method. * * @param x Value to round. * @param scale Number of digits to the right of the decimal point. * @return the rounded value. * @since 1.1 (previously in {@code MathUtils}, moved as of version 3.0) */ public static float round(float x, int scale) { return round(x, scale, BigDecimal.ROUND_HALF_UP); }
/** * Returns true if both arguments are NaN or they are * equal as defined by {@link #equals(float,float) equals(x, y, 1)}. * * @param x first value * @param y second value * @return {@code true} if the values are equal or both are NaN. * @since 2.2 */ public static boolean equalsIncludingNaN(float x, float y) { return (x != x || y != y) ? !(x != x ^ y != y) : equals(x, y, 1); }
/** * Returns true iff both arguments are null or have same dimensions and all * their elements are equal as defined by * {@link Precision#equalsIncludingNaN(double,double) this method}. * * @param x first array * @param y second array * @return true if the values are both null or have same dimension and * equal elements * @since 2.2 */ public static boolean equalsIncludingNaN(float[] x, float[] y) { if ((x == null) || (y == null)) { return !((x == null) ^ (y == null)); } if (x.length != y.length) { return false; } for (int i = 0; i < x.length; ++i) { if (!Precision.equalsIncludingNaN(x[i], y[i])) { return false; } } return true; }
/** {@inheritDoc} */ public boolean converged(final int iteration, final Evaluation previous, final Evaluation current) { final double prevRms = previous.getRMS(); final double currRms = current.getRMS(); return Precision.equals(prevRms, currRms, this.absTol) || Precision.equalsWithRelativeTolerance(prevRms, currRms, this.relTol); }
if (Precision.compareTo(entry, 0d, cutOff) > 0) { final double ratio = FastMath.abs(rhs / entry); int column = i + tableau.getArtificialVariableOffset(); final double entry = tableau.getEntry(row, column); if (Precision.equals(entry, 1d, maxUlps) && row.equals(tableau.getBasicRow(column))) { return row;
/** * Gets the block diagonal matrix D of the decomposition. * D is a block diagonal matrix. * Real eigenvalues are on the diagonal while complex values are on * 2x2 blocks { {real +imaginary}, {-imaginary, real} }. * * @return the D matrix. * * @see #getRealEigenvalues() * @see #getImagEigenvalues() */ public RealMatrix getD() { if (cachedD == null) { // cache the matrix for subsequent calls cachedD = MatrixUtils.createRealDiagonalMatrix(realEigenvalues); for (int i = 0; i < imagEigenvalues.length; i++) { if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) > 0) { cachedD.setEntry(i, i+1, imagEigenvalues[i]); } else if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) < 0) { cachedD.setEntry(i, i-1, imagEigenvalues[i]); } } } return cachedD; }
/** * Returns {@code true} if, both for the real part and for the imaginary * part, there is no double value strictly between the arguments or the * relative difference between them is smaller or equal to the given * tolerance. Returns {@code false} if either of the arguments is NaN. * * @param x First value (cannot be {@code null}). * @param y Second value (cannot be {@code null}). * @param eps Amount of allowed relative error. * @return {@code true} if the values are two adjacent floating point * numbers or they are within range of each other. * * @see Precision#equalsWithRelativeTolerance(double,double,double) * @since 3.3 */ public static boolean equalsWithRelativeTolerance(Complex x, Complex y, double eps) { return Precision.equalsWithRelativeTolerance(x.real, y.real, eps) && Precision.equalsWithRelativeTolerance(x.imaginary, y.imaginary, eps); }
final double entry = tableau.getEntry(i, col); if (Precision.compareTo(entry, 0d, maxUlps) > 0) { final double ratio = rhs / entry; int column = i + tableau.getArtificialVariableOffset(); final double entry = tableau.getEntry(row, column); if (Precision.equals(entry, 1d, maxUlps) && row.equals(tableau.getBasicRow(column))) { return row;
/** {@inheritDoc} */ public boolean converged(final int iteration, final Evaluation previous, final Evaluation current) { final double prevRms = previous.getRMS(); final double currRms = current.getRMS(); return Precision.equals(prevRms, currRms, this.absTol) || Precision.equalsWithRelativeTolerance(prevRms, currRms, this.relTol); }
/** {@inheritDoc} */ public int compare(final Vector2D o1, final Vector2D o2) { final double tolerance = getTolerance(); // need to take the tolerance value into account, otherwise collinear points // will not be handled correctly when building the upper/lower hull final int diff = Precision.compareTo(o1.getX(), o2.getX(), tolerance); if (diff == 0) { return Precision.compareTo(o1.getY(), o2.getY(), tolerance); } else { return diff; } } });
/** * Returns {@code true} if, both for the real part and for the imaginary * part, there is no double value strictly between the arguments or the * relative difference between them is smaller or equal to the given * tolerance. Returns {@code false} if either of the arguments is NaN. * * @param x First value (cannot be {@code null}). * @param y Second value (cannot be {@code null}). * @param eps Amount of allowed relative error. * @return {@code true} if the values are two adjacent floating point * numbers or they are within range of each other. * * @see Precision#equalsWithRelativeTolerance(double,double,double) * @since 3.3 */ public static boolean equalsWithRelativeTolerance(Complex x, Complex y, double eps) { return Precision.equalsWithRelativeTolerance(x.real, y.real, eps) && Precision.equalsWithRelativeTolerance(x.imaginary, y.imaginary, eps); }
/** * Rounds the given value to the specified number of decimal places. * The value is rounded using the {@link BigDecimal#ROUND_HALF_UP} method. * * @param x Value to round. * @param scale Number of digits to the right of the decimal point. * @return the rounded value. * @since 1.1 (previously in {@code MathUtils}, moved as of version 3.0) */ public static double round(double x, int scale) { return round(x, scale, BigDecimal.ROUND_HALF_UP); }
/** * Returns true iff they are equal as defined by * {@link #equals(double,double,int) equals(x, y, 1)}. * * @param x first value * @param y second value * @return {@code true} if the values are equal. */ public static boolean equals(double x, double y) { return equals(x, y, 1); }
if (Precision.equals(norm, 0.0, EPSILON)) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); if (Precision.equals(q, 0.0)) { if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) < 0) { z = w; s = r; } else { l = i; if (Precision.equals(imagEigenvalues[i], 0.0)) { if (w != 0.0) { matrixT[i][idx] = -r / w; if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) < 0) { z = w; r = ra; } else { l = i; if (Precision.equals(imagEigenvalues[i], 0.0)) { final Complex c = cdiv(-ra, -sa, w, q); matrixT[i][idx - 1] = c.getReal(); imagEigenvalues[i] * imagEigenvalues[i] - q * q; final double vi = (realEigenvalues[i] - p) * 2.0 * q; if (Precision.equals(vr, 0.0) && Precision.equals(vi, 0.0)) { vr = Precision.EPSILON * norm * (FastMath.abs(w) + FastMath.abs(q) + FastMath.abs(x) +
/** {@inheritDoc} */ public boolean converged(final int iteration, final Evaluation previous, final Evaluation current) { final double prevRms = previous.getRMS(); final double currRms = current.getRMS(); return Precision.equals(prevRms, currRms, this.absTol) || Precision.equalsWithRelativeTolerance(prevRms, currRms, this.relTol); }
/** * Returns {@code true} iff both arguments are {@code null} or have same * dimensions and all their elements are equal as defined by * {@link Precision#equalsIncludingNaN(double,double) this method}. * * @param x First array. * @param y Second array. * @return {@code true} if the values are both {@code null} or have same * dimension and equal elements. * @since 2.2 */ public static boolean equalsIncludingNaN(double[] x, double[] y) { if ((x == null) || (y == null)) { return !((x == null) ^ (y == null)); } if (x.length != y.length) { return false; } for (int i = 0; i < x.length; ++i) { if (!Precision.equalsIncludingNaN(x[i], y[i])) { return false; } } return true; }
/** * Returns whether the problem is at an optimal state. * @return whether the model has been solved */ boolean isOptimal() { for (int i = getNumObjectiveFunctions(); i < getWidth() - 1; i++) { final double entry = tableau.getEntry(0, i); if (Precision.compareTo(entry, 0d, epsilon) < 0) { return false; } } return true; }
/** * Returns {@code true} if, both for the real part and for the imaginary * part, there is no double value strictly between the arguments or the * relative difference between them is smaller or equal to the given * tolerance. Returns {@code false} if either of the arguments is NaN. * * @param x First value (cannot be {@code null}). * @param y Second value (cannot be {@code null}). * @param eps Amount of allowed relative error. * @return {@code true} if the values are two adjacent floating point * numbers or they are within range of each other. * * @see Precision#equalsWithRelativeTolerance(double,double,double) * @since 3.3 */ public static boolean equalsWithRelativeTolerance(Complex x, Complex y, double eps) { return Precision.equalsWithRelativeTolerance(x.real, y.real, eps) && Precision.equalsWithRelativeTolerance(x.imaginary, y.imaginary, eps); }
/** * {@inheritDoc} */ @Override public String toString() { return String.format( "index=%.0f,n=%.0f,np=%.2f,q=%.2f,dn=%.2f,prev=%d,next=%d", (double) index, Precision.round(intMarkerPosition, 0), Precision.round(desiredMarkerPosition, 2), Precision.round(markerHeight, 2), Precision.round(desiredMarkerIncrement, 2), previous.index, next.index); } }
/** * Returns true if the arguments are both NaN or they are * equal as defined by {@link #equals(double,double) equals(x, y, 1)}. * * @param x first value * @param y second value * @return {@code true} if the values are equal or both are NaN. * @since 2.2 */ public static boolean equalsIncludingNaN(double x, double y) { return (x != x || y != y) ? !(x != x ^ y != y) : equals(x, y, 1); }