Java Code Examples for org.apache.commons.math3.util.FastMath#floor()
The following examples show how to use
org.apache.commons.math3.util.FastMath#floor() .
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: Percentile.java From astor with GNU General Public License v2.0 | 6 votes |
/** * 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 kthSelector 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 kthSelector) { final double fpos = FastMath.floor(pos); final int intPos = (int) fpos; final double dif = pos - fpos; if (pos < 1) { return kthSelector.select(work, pivotsHeap, 0); } if (pos >= length) { return kthSelector.select(work, pivotsHeap, length - 1); } final double lower = kthSelector.select(work, pivotsHeap, intPos - 1); final double upper = kthSelector.select(work, pivotsHeap, intPos); return lower + dif * (upper - lower); }
Example 2
Source File: Gamma.java From astor with GNU General Public License v2.0 | 6 votes |
/** * <p> * Returns the value of log Γ(x) for x > 0. * </p> * <p> * For x ≤ 8, the implementation is based on the double precision * implementation in the <em>NSWC Library of Mathematics Subroutines</em>, * {@code DGAMLN}. For x > 8, the implementation is based on * </p> * <ul> * <li><a href="http://mathworld.wolfram.com/GammaFunction.html">Gamma * Function</a>, equation (28).</li> * <li><a href="http://mathworld.wolfram.com/LanczosApproximation.html"> * Lanczos Approximation</a>, equations (1) through (5).</li> * <li><a href="http://my.fit.edu/~gabdo/gamma.txt">Paul Godfrey, A note on * the computation of the convergent Lanczos complex Gamma * approximation</a></li> * </ul> * * @param x Argument. * @return the value of {@code log(Gamma(x))}, {@code Double.NaN} if * {@code x <= 0.0}. */ public static double logGamma(double x) { double ret; if (Double.isNaN(x) || (x <= 0.0)) { ret = Double.NaN; } else if (x < 0.5) { return logGamma1p(x) - FastMath.log(x); } else if (x <= 2.5) { return logGamma1p((x - 0.5) - 0.5); } else if (x <= 8.0) { final int n = (int) FastMath.floor(x - 1.5); double prod = 1.0; for (int i = 1; i <= n; i++) { prod *= x - i; } return logGamma1p(x - (n + 1)) + FastMath.log(prod); } else { double sum = lanczos(x); double tmp = x + LANCZOS_G + .5; ret = ((x + .5) * FastMath.log(tmp)) - tmp + HALF_LOG_2_PI + FastMath.log(sum / x); } return ret; }
Example 3
Source File: Percentile.java From astor with GNU General Public License v2.0 | 6 votes |
/** * 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 kthSelector 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 kthSelector) { final double fpos = FastMath.floor(pos); final int intPos = (int) fpos; final double dif = pos - fpos; if (pos < 1) { return kthSelector.select(work, pivotsHeap, 0); } if (pos >= length) { return kthSelector.select(work, pivotsHeap, length - 1); } final double lower = kthSelector.select(work, pivotsHeap, intPos - 1); final double upper = kthSelector.select(work, pivotsHeap, intPos); return lower + dif * (upper - lower); }
Example 4
Source File: UniformIntegerDistribution.java From astor with GNU General Public License v2.0 | 5 votes |
/** {@inheritDoc} */ @Override public int sample() { final double r = random.nextDouble(); final double scaled = r * upper + (1 - r) * lower + r; return (int) FastMath.floor(scaled); }
Example 5
Source File: PolynomialsUtils.java From astor with GNU General Public License v2.0 | 5 votes |
/** Get the coefficients array for a given degree. * @param degree degree of the polynomial * @param coefficients list where the computed coefficients are stored * @param generator recurrence coefficients generator * @return coefficients array */ private static PolynomialFunction buildPolynomial(final int degree, final List<BigFraction> coefficients, final RecurrenceCoefficientsGenerator generator) { final int maxDegree = (int) FastMath.floor(FastMath.sqrt(2 * coefficients.size())) - 1; synchronized (PolynomialsUtils.class) { if (degree > maxDegree) { computeUpToDegree(degree, maxDegree, generator, coefficients); } } // coefficient for polynomial 0 is l [0] // coefficients for polynomial 1 are l [1] ... l [2] (degrees 0 ... 1) // coefficients for polynomial 2 are l [3] ... l [5] (degrees 0 ... 2) // coefficients for polynomial 3 are l [6] ... l [9] (degrees 0 ... 3) // coefficients for polynomial 4 are l[10] ... l[14] (degrees 0 ... 4) // coefficients for polynomial 5 are l[15] ... l[20] (degrees 0 ... 5) // coefficients for polynomial 6 are l[21] ... l[27] (degrees 0 ... 6) // ... final int start = degree * (degree + 1) / 2; final double[] a = new double[degree + 1]; for (int i = 0; i <= degree; ++i) { a[i] = coefficients.get(start + i).doubleValue(); } // build the polynomial return new PolynomialFunction(a); }
Example 6
Source File: RandomDataImpl.java From astor with GNU General Public License v2.0 | 5 votes |
/** {@inheritDoc} */ public int nextInt(int lower, int upper) { if (lower >= upper) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper, false); } double r = getRan().nextDouble(); double scaled = r * upper + (1.0 - r) * lower + r; return (int) FastMath.floor(scaled); }
Example 7
Source File: RandomDataImpl.java From astor with GNU General Public License v2.0 | 5 votes |
/** {@inheritDoc} */ public long nextLong(long lower, long upper) { if (lower >= upper) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper, false); } double r = getRan().nextDouble(); double scaled = r * upper + (1.0 - r) * lower + r; return (long)FastMath.floor(scaled); }
Example 8
Source File: UniformIntegerDistribution.java From astor with GNU General Public License v2.0 | 5 votes |
/** {@inheritDoc} */ @Override public int sample() { final double r = random.nextDouble(); final double scaled = r * upper + (1 - r) * lower + r; return (int) FastMath.floor(scaled); }
Example 9
Source File: RandomDataGenerator.java From astor with GNU General Public License v2.0 | 5 votes |
/** {@inheritDoc} */ public int nextSecureInt(int lower, int upper) throws NumberIsTooLargeException { if (lower >= upper) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper, false); } SecureRandom sec = getSecRan(); final double r = sec.nextDouble(); final double scaled = r * upper + (1.0 - r) * lower + r; return (int)FastMath.floor(scaled); }
Example 10
Source File: ConvexHullGenerator2DAbstractTest.java From astor with GNU General Public License v2.0 | 5 votes |
@Test public void testConvexHull() { // execute 100 random variations for (int i = 0; i < 100; i++) { // randomize the size from 4 to 100 int size = (int) FastMath.floor(random.nextDouble() * 96.0 + 4.0); List<Vector2D> points = createRandomPoints(size); ConvexHull2D hull = generator.generate(reducePoints(points)); checkConvexHull(points, hull); } }
Example 11
Source File: PolynomialsUtils.java From astor with GNU General Public License v2.0 | 5 votes |
/** Get the coefficients array for a given degree. * @param degree degree of the polynomial * @param coefficients list where the computed coefficients are stored * @param generator recurrence coefficients generator * @return coefficients array */ private static PolynomialFunction buildPolynomial(final int degree, final List<BigFraction> coefficients, final RecurrenceCoefficientsGenerator generator) { final int maxDegree = (int) FastMath.floor(FastMath.sqrt(2 * coefficients.size())) - 1; synchronized (PolynomialsUtils.class) { if (degree > maxDegree) { computeUpToDegree(degree, maxDegree, generator, coefficients); } } // coefficient for polynomial 0 is l [0] // coefficients for polynomial 1 are l [1] ... l [2] (degrees 0 ... 1) // coefficients for polynomial 2 are l [3] ... l [5] (degrees 0 ... 2) // coefficients for polynomial 3 are l [6] ... l [9] (degrees 0 ... 3) // coefficients for polynomial 4 are l[10] ... l[14] (degrees 0 ... 4) // coefficients for polynomial 5 are l[15] ... l[20] (degrees 0 ... 5) // coefficients for polynomial 6 are l[21] ... l[27] (degrees 0 ... 6) // ... final int start = degree * (degree + 1) / 2; final double[] a = new double[degree + 1]; for (int i = 0; i <= degree; ++i) { a[i] = coefficients.get(start + i).doubleValue(); } // build the polynomial return new PolynomialFunction(a); }
Example 12
Source File: Fraction.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Create a fraction given the double value and either the maximum error * allowed or the maximum number of denominator digits. * <p> * * NOTE: This constructor is called with EITHER * - a valid epsilon value and the maxDenominator set to Integer.MAX_VALUE * (that way the maxDenominator has no effect). * OR * - a valid maxDenominator value and the epsilon value set to zero * (that way epsilon only has effect if there is an exact match before * the maxDenominator value is reached). * </p><p> * * It has been done this way so that the same code can be (re)used for both * scenarios. However this could be confusing to users if it were part of * the public API and this constructor should therefore remain PRIVATE. * </p> * * See JIRA issue ticket MATH-181 for more details: * * https://issues.apache.org/jira/browse/MATH-181 * * @param value the double value to convert to a fraction. * @param epsilon maximum error allowed. The resulting fraction is within * {@code epsilon} of {@code value}, in absolute terms. * @param maxDenominator maximum denominator value allowed. * @param maxIterations maximum number of convergents * @throws FractionConversionException if the continued fraction failed to * converge. */ private Fraction(double value, double epsilon, int maxDenominator, int maxIterations) throws FractionConversionException { long overflow = Integer.MAX_VALUE; double r0 = value; long a0 = (long)FastMath.floor(r0); if (a0 > overflow) { throw new FractionConversionException(value, a0, 1l); } // check for (almost) integer arguments, which should not go // to iterations. if (FastMath.abs(a0 - value) < epsilon) { this.numerator = (int) a0; this.denominator = 1; return; } long p0 = 1; long q0 = 0; long p1 = a0; long q1 = 1; long p2 = 0; long q2 = 1; int n = 0; boolean stop = false; do { ++n; double r1 = 1.0 / (r0 - a0); long a1 = (long)FastMath.floor(r1); p2 = (a1 * p1) + p0; q2 = (a1 * q1) + q0; if ((p2 > overflow) || (q2 > overflow)) { throw new FractionConversionException(value, p2, q2); } double convergent = (double)p2 / (double)q2; if (n < maxIterations && FastMath.abs(convergent - value) > epsilon && q2 < maxDenominator) { p0 = p1; p1 = p2; q0 = q1; q1 = q2; a0 = a1; r0 = r1; } else { stop = true; } } while (!stop); if (n >= maxIterations) { throw new FractionConversionException(value, maxIterations); } if (q2 < maxDenominator) { this.numerator = (int) p2; this.denominator = (int) q2; } else { this.numerator = (int) p1; this.denominator = (int) q1; } }
Example 13
Source File: Math_26_Fraction_s.java From coming with MIT License | 4 votes |
/** * Create a fraction given the double value and either the maximum error * allowed or the maximum number of denominator digits. * <p> * * NOTE: This constructor is called with EITHER * - a valid epsilon value and the maxDenominator set to Integer.MAX_VALUE * (that way the maxDenominator has no effect). * OR * - a valid maxDenominator value and the epsilon value set to zero * (that way epsilon only has effect if there is an exact match before * the maxDenominator value is reached). * </p><p> * * It has been done this way so that the same code can be (re)used for both * scenarios. However this could be confusing to users if it were part of * the public API and this constructor should therefore remain PRIVATE. * </p> * * See JIRA issue ticket MATH-181 for more details: * * https://issues.apache.org/jira/browse/MATH-181 * * @param value the double value to convert to a fraction. * @param epsilon maximum error allowed. The resulting fraction is within * {@code epsilon} of {@code value}, in absolute terms. * @param maxDenominator maximum denominator value allowed. * @param maxIterations maximum number of convergents * @throws FractionConversionException if the continued fraction failed to * converge. */ private Fraction(double value, double epsilon, int maxDenominator, int maxIterations) throws FractionConversionException { long overflow = Integer.MAX_VALUE; double r0 = value; long a0 = (long)FastMath.floor(r0); if (a0 > overflow) { throw new FractionConversionException(value, a0, 1l); } // check for (almost) integer arguments, which should not go // to iterations. if (FastMath.abs(a0 - value) < epsilon) { this.numerator = (int) a0; this.denominator = 1; return; } long p0 = 1; long q0 = 0; long p1 = a0; long q1 = 1; long p2 = 0; long q2 = 1; int n = 0; boolean stop = false; do { ++n; double r1 = 1.0 / (r0 - a0); long a1 = (long)FastMath.floor(r1); p2 = (a1 * p1) + p0; q2 = (a1 * q1) + q0; if ((p2 > overflow) || (q2 > overflow)) { throw new FractionConversionException(value, p2, q2); } double convergent = (double)p2 / (double)q2; if (n < maxIterations && FastMath.abs(convergent - value) > epsilon && q2 < maxDenominator) { p0 = p1; p1 = p2; q0 = q1; q1 = q2; a0 = a1; r0 = r1; } else { stop = true; } } while (!stop); if (n >= maxIterations) { throw new FractionConversionException(value, maxIterations); } if (q2 < maxDenominator) { this.numerator = (int) p2; this.denominator = (int) q2; } else { this.numerator = (int) p1; this.denominator = (int) q1; } }
Example 14
Source File: Floor.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} */ public double value(double x) { return FastMath.floor(x); }
Example 15
Source File: Vector2DUtil.java From NOVA-Core with GNU Lesser General Public License v3.0 | 4 votes |
public static Vector2D floor(Vector2D vec) { return new Vector2D(FastMath.floor(vec.getX()), FastMath.floor(vec.getY())); }
Example 16
Source File: DerivativeStructure.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} * @since 3.2 */ public DerivativeStructure floor() { return new DerivativeStructure(compiler.getFreeParameters(), compiler.getOrder(), FastMath.floor(data[0])); }
Example 17
Source File: Floor.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} */ public double value(double x) { return FastMath.floor(x); }
Example 18
Source File: Percentile.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Returns an estimate of the <code>p</code>th percentile of the values * in the <code>values</code> array, starting with the element in (0-based) * position <code>begin</code> in the array and including <code>length</code> * values. * <p> * Calls to this method do not modify the internal <code>quantile</code> * state of this statistic.</p> * <p> * <ul> * <li>Returns <code>Double.NaN</code> if <code>length = 0</code></li> * <li>Returns (for any value of <code>p</code>) <code>values[begin]</code> * if <code>length = 1 </code></li> * <li>Throws <code>MathIllegalArgumentException</code> if <code>values</code> * is null , <code>begin</code> or <code>length</code> is invalid, or * <code>p</code> is not a valid quantile value (p must be greater than 0 * and less than or equal to 100)</li> * </ul></p> * <p> * See {@link Percentile} for a description of the percentile estimation * algorithm used.</p> * * @param values array of input values * @param p the percentile to compute * @param begin the first (0-based) element to include in the computation * @param length the number of array elements to include * @return the percentile value * @throws MathIllegalArgumentException if the parameters are not valid or the * input array is null */ public double evaluate(final double[] values, final int begin, final int length, final double p) throws MathIllegalArgumentException { test(values, begin, length); if ((p > 100) || (p <= 0)) { throw new OutOfRangeException( LocalizedFormats.OUT_OF_BOUNDS_QUANTILE_VALUE, p, 0, 100); } if (length == 0) { return Double.NaN; } if (length == 1) { return values[begin]; // always return single value for n = 1 } double n = length; double pos = p * (n + 1) / 100; double fpos = FastMath.floor(pos); int intPos = (int) fpos; double dif = pos - fpos; double[] work; int[] pivotsHeap; if (values == getDataRef()) { work = getDataRef(); pivotsHeap = cachedPivots; } else { work = new double[length]; System.arraycopy(values, begin, work, 0, length); pivotsHeap = new int[(0x1 << MAX_CACHED_LEVELS) - 1]; Arrays.fill(pivotsHeap, -1); } if (pos < 1) { return select(work, pivotsHeap, 0); } if (pos >= n) { return select(work, pivotsHeap, length - 1); } double lower = select(work, pivotsHeap, intPos - 1); double upper = select(work, pivotsHeap, intPos); return lower + dif * (upper - lower); }
Example 19
Source File: Math_1_Fraction_t.java From coming with MIT License | 4 votes |
/** * Create a fraction given the double value and either the maximum error * allowed or the maximum number of denominator digits. * <p> * * NOTE: This constructor is called with EITHER * - a valid epsilon value and the maxDenominator set to Integer.MAX_VALUE * (that way the maxDenominator has no effect). * OR * - a valid maxDenominator value and the epsilon value set to zero * (that way epsilon only has effect if there is an exact match before * the maxDenominator value is reached). * </p><p> * * It has been done this way so that the same code can be (re)used for both * scenarios. However this could be confusing to users if it were part of * the public API and this constructor should therefore remain PRIVATE. * </p> * * See JIRA issue ticket MATH-181 for more details: * * https://issues.apache.org/jira/browse/MATH-181 * * @param value the double value to convert to a fraction. * @param epsilon maximum error allowed. The resulting fraction is within * {@code epsilon} of {@code value}, in absolute terms. * @param maxDenominator maximum denominator value allowed. * @param maxIterations maximum number of convergents * @throws FractionConversionException if the continued fraction failed to * converge. */ private Fraction(double value, double epsilon, int maxDenominator, int maxIterations) throws FractionConversionException { long overflow = Integer.MAX_VALUE; double r0 = value; long a0 = (long)FastMath.floor(r0); if (FastMath.abs(a0) > overflow) { throw new FractionConversionException(value, a0, 1l); } // check for (almost) integer arguments, which should not go to iterations. if (FastMath.abs(a0 - value) < epsilon) { this.numerator = (int) a0; this.denominator = 1; return; } long p0 = 1; long q0 = 0; long p1 = a0; long q1 = 1; long p2 = 0; long q2 = 1; int n = 0; boolean stop = false; do { ++n; double r1 = 1.0 / (r0 - a0); long a1 = (long)FastMath.floor(r1); p2 = (a1 * p1) + p0; q2 = (a1 * q1) + q0; if ((FastMath.abs(p2) > overflow) || (FastMath.abs(q2) > overflow)) { // in maxDenominator mode, if the last fraction was very close to the actual value // q2 may overflow in the next iteration; in this case return the last one. if (epsilon == 0.0 && FastMath.abs(q1) < maxDenominator) { break; } throw new FractionConversionException(value, p2, q2); } double convergent = (double)p2 / (double)q2; if (n < maxIterations && FastMath.abs(convergent - value) > epsilon && q2 < maxDenominator) { p0 = p1; p1 = p2; q0 = q1; q1 = q2; a0 = a1; r0 = r1; } else { stop = true; } } while (!stop); if (n >= maxIterations) { throw new FractionConversionException(value, maxIterations); } if (q2 < maxDenominator) { this.numerator = (int) p2; this.denominator = (int) q2; } else { this.numerator = (int) p1; this.denominator = (int) q1; } }
Example 20
Source File: ComplexUtil.java From nd4j with Apache License 2.0 | 2 votes |
/** * 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()); }