/** Get the closest long to x. * @param x number from which closest long is requested * @return closest long to x */ public static long round(double x) { return (long) floor(x + 0.5); }
/** Get the closest int to x. * @param x number from which closest int is requested * @return closest int to x */ public static int round(final float x) { return (int) floor(x + 0.5f); }
/** {@inheritDoc} */ public double value(double x) { return FastMath.floor(x); } }
@Override public int[] apply(int[] countsPerSecond) { int len = countsPerSecond.length / 60; int[] ret = new int[len]; int cumulative = 0; for (int i = 0; i < countsPerSecond.length; i++) { cumulative += countsPerSecond[i]; if (i % 60 == 59) { ret[(int) FastMath.floor(i / 60)] = cumulative; } } // make cumulative curve for (int i = ret.length - 1; i > 0; i--) { ret[i] -= ret[i - 1]; } return ret; } }
/** Get the smallest whole number larger than x. * @param x number from which ceil is requested * @return a double number c such that c is an integer c - 1.0 < x <= c */ public static double ceil(double x) { double y; if (x != x) { // NaN return x; } y = floor(x); if (y == x) { return y; } y += 1.0; if (y == 0) { return x*y; } return y; }
/** Get the whole number that is the nearest to x, or the even one if x is exactly half way between two integers. * @param x number from which nearest whole number is requested * @return a double number r such that r is an integer r - 0.5 <= x <= r + 0.5 */ public static double rint(double x) { double y = floor(x); double d = x - y; if (d > 0.5) { if (y == -1.0) { return -0.0; // Preserve sign of operand } return y+1.0; } if (d < 0.5) { return y; } /* half way, round to even */ long z = (long) y; return (z & 1) == 0 ? y : y + 1.0; }
/** * Normalize an angle in a 2π wide interval around a center value. * <p>This method has three main uses:</p> * <ul> * <li>normalize an angle between 0 and 2π:<br/> * {@code a = MathUtils.normalizeAngle(a, FastMath.PI);}</li> * <li>normalize an angle between -π and +π<br/> * {@code a = MathUtils.normalizeAngle(a, 0.0);}</li> * <li>compute the angle between two defining angular positions:<br> * {@code angle = MathUtils.normalizeAngle(end, start) - start;}</li> * </ul> * <p>Note that due to numerical accuracy and since π cannot be represented * exactly, the result interval is <em>closed</em>, it cannot be half-closed * as would be more satisfactory in a purely mathematical view.</p> * @param a angle to normalize * @param center center of the desired 2π interval for the result * @return a-2kπ with integer k and center-π <= a-2kπ <= center+π * @since 1.2 */ public static double normalizeAngle(double a, double center) { return a - TWO_PI * FastMath.floor((a + FastMath.PI - center) / TWO_PI); }
/** {@inheritDoc} * @since 3.2 */ public Decimal64 floor() { return new Decimal64(FastMath.floor(value)); }
/** {@inheritDoc} */ public SparseGradient floor() { return createConstant(FastMath.floor(value)); }
/** * <p>Reduce {@code |a - offset|} to the primary interval * {@code [0, |period|)}.</p> * * <p>Specifically, the value returned is <br/> * {@code a - |period| * floor((a - offset) / |period|) - offset}.</p> * * <p>If any of the parameters are {@code NaN} or infinite, the result is * {@code NaN}.</p> * * @param a Value to reduce. * @param period Period. * @param offset Value that will be mapped to {@code 0}. * @return the value, within the interval {@code [0 |period|)}, * that corresponds to {@code a}. */ public static double reduce(double a, double period, double offset) { final double p = FastMath.abs(period); return a - p * FastMath.floor((a - offset) / p) - offset; }
/** * Given a d-statistic in the range [0, 1] and the two sample sizes n and m, * an integral d-statistic in the range [0, n*m] is calculated, that can be used for * comparison with other integral d-statistics. Depending whether {@code strict} is * {@code true} or not, the returned value divided by (n*m) is greater than * (resp greater than or equal to) the given d value (allowing some tolerance). * * @param d a d-statistic in the range [0, 1] * @param n first sample size * @param m second sample size * @param strict whether the returned value divided by (n*m) is allowed to be equal to d * @return the integral d-statistic in the range [0, n*m] */ private static long calculateIntegralD(double d, int n, int m, boolean strict) { final double tol = 1e-12; // d-values within tol of one another are considered equal long nm = n * (long)m; long upperBound = (long)FastMath.ceil((d - tol) * nm); long lowerBound = (long)FastMath.floor((d + tol) * nm); if (strict && lowerBound == upperBound) { return upperBound + 1l; } else { return upperBound; } }
/** * Return the floor value of the given complex number * * @param num the number to getScalar the absolute value for * @return the absolute value of this complex number */ public static IComplexNumber floor(IComplexNumber num) { Complex c = new Complex(FastMath.floor(num.realComponent().doubleValue()), FastMath.floor(num.imaginaryComponent().doubleValue())); return Nd4j.createDouble(c.getReal(), c.getImaginary()); }
/** {@inheritDoc} * @since 3.2 */ public DerivativeStructure floor() { return new DerivativeStructure(compiler.getFreeParameters(), compiler.getOrder(), FastMath.floor(data[0])); }
protected double getPercentile(Number quantile, INDArray sorted) { if (quantile.intValue() == 0) return sorted.getDouble(0); else if (quantile.intValue() == 100) return sorted.getDouble(sorted.length() - 1); double pos = (quantile.doubleValue() / 100.0) * (double) (sorted.length() + 1); double fposition = FastMath.floor(pos); int position = (int)fposition; double diff = pos - fposition; double lower = sorted.getDouble(position-1); double upper = sorted.getDouble(position); return lower + diff * (upper - lower); }
protected double getPercentile(Number quantile, INDArray sorted) { if (quantile.intValue() == 0) return sorted.getDouble(0); else if (quantile.intValue() == 100) return sorted.getDouble(sorted.length() - 1); double pos = (quantile.doubleValue() / 100.0) * (double) (sorted.length() + 1); double fposition = FastMath.floor(pos); int position = (int) fposition; double diff = pos - fposition; double lower = sorted.getDouble(position - 1); double upper = sorted.getDouble(position); return lower + diff * (upper - lower); }
if (z < 15.0) { double z2 = 2.0 * z; if (FastMath.floor(z2) == z2) { ret = EXACT_STIRLING_ERRORS[(int) z2]; } else {
/** * {@inheritDoc}This method in particular for R_2 averages the * values at ceil(p+0.5) and floor(p-0.5). */ @Override protected double estimate(final double[] values, final int[] pivotsHeap, final double pos, final int length, final KthSelector selector) { final double low = super.estimate(values, pivotsHeap, FastMath.ceil(pos - 0.5), length, selector); final double high = super.estimate(values, pivotsHeap,FastMath.floor(pos + 0.5), length, selector); return (low + high) / 2; }
final RecurrenceCoefficientsGenerator generator) { synchronized (coefficients) { final int maxDegree = (int) FastMath.floor(FastMath.sqrt(2 * coefficients.size())) - 1; if (degree > maxDegree) { computeUpToDegree(degree, maxDegree, generator, coefficients);
/** * Compute n!, the<a href="http://mathworld.wolfram.com/Factorial.html"> * factorial</a> of {@code n} (the product of the numbers 1 to n), as a * {@code double}. * The result should be small enough to fit into a {@code double}: The * largest {@code n} for which {@code n!} does not exceed * {@code Double.MAX_VALUE} is 170. If the computed value exceeds * {@code Double.MAX_VALUE}, {@code Double.POSITIVE_INFINITY} is returned. * * @param n Argument. * @return {@code n!} * @throws NotPositiveException if {@code n < 0}. */ public static double factorialDouble(final int n) throws NotPositiveException { if (n < 0) { throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER, n); } if (n < 21) { return FACTORIALS[n]; } return FastMath.floor(FastMath.exp(CombinatoricsUtils.factorialLog(n)) + 0.5); }
/** * Estimation based on K<sup>th</sup> selection. This may be overridden * in specific enums to compute slightly different estimations. * * @param work array of numbers to be used for finding the percentile * @param pos indicated positional index prior computed from calling * {@link #index(double, int)} * @param pivotsHeap an earlier populated cache if exists; will be used * @param length size of array considered * @param selector a {@link KthSelector} used for pivoting during search * @return estimated percentile */ protected double estimate(final double[] work, final int[] pivotsHeap, final double pos, final int length, final KthSelector selector) { final double fpos = FastMath.floor(pos); final int intPos = (int) fpos; final double dif = pos - fpos; if (pos < 1) { return selector.select(work, pivotsHeap, 0); } if (pos >= length) { return selector.select(work, pivotsHeap, length - 1); } final double lower = selector.select(work, pivotsHeap, intPos - 1); final double upper = selector.select(work, pivotsHeap, intPos); return lower + dif * (upper - lower); }