org.apache.commons.math3.util.ArithmeticUtils Java Examples
The following examples show how to use
org.apache.commons.math3.util.ArithmeticUtils.
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: MetricsSystemImpl.java From hadoop with Apache License 2.0 | 6 votes |
private synchronized void configureSinks() { sinkConfigs = config.getInstanceConfigs(SINK_KEY); int confPeriod = 0; for (Entry<String, MetricsConfig> entry : sinkConfigs.entrySet()) { MetricsConfig conf = entry.getValue(); int sinkPeriod = conf.getInt(PERIOD_KEY, PERIOD_DEFAULT); confPeriod = confPeriod == 0 ? sinkPeriod : ArithmeticUtils.gcd(confPeriod, sinkPeriod); String clsName = conf.getClassName(""); if (clsName == null) continue; // sink can be registered later on String sinkName = entry.getKey(); try { MetricsSinkAdapter sa = newSink(sinkName, conf.getString(DESC_KEY, sinkName), conf); sa.start(); sinks.put(sinkName, sa); } catch (Exception e) { LOG.warn("Error creating sink '"+ sinkName +"'", e); } } period = confPeriod > 0 ? confPeriod : config.getInt(PERIOD_KEY, PERIOD_DEFAULT); }
Example #2
Source File: DSCompiler.java From astor with GNU General Public License v2.0 | 6 votes |
/** Evaluate Taylor expansion of a derivative structure. * @param ds array holding the derivative structure * @param dsOffset offset of the derivative structure in its array * @param delta parameters offsets (Δx, Δy, ...) * @return value of the Taylor expansion at x + Δx, y + Δy, ... * @throws MathArithmeticException if factorials becomes too large */ public double taylor(final double[] ds, final int dsOffset, final double ... delta) throws MathArithmeticException { double value = 0; for (int i = getSize() - 1; i >= 0; --i) { final int[] orders = getPartialDerivativeOrders(i); double term = ds[dsOffset + i]; for (int k = 0; k < orders.length; ++k) { if (orders[k] > 0) { try { term *= FastMath.pow(delta[k], orders[k]) / ArithmeticUtils.factorial(orders[k]); } catch (NotPositiveException e) { // this cannot happen throw new MathInternalError(e); } } } value += term; } return value; }
Example #3
Source File: Fraction.java From astor with GNU General Public License v2.0 | 6 votes |
/** * <p>Multiplies the value of this fraction by another, returning the * result in reduced form.</p> * * @param fraction the fraction to multiply by, must not be {@code null} * @return a {@code Fraction} instance with the resulting values * @throws NullArgumentException if the fraction is {@code null} * @throws MathArithmeticException if the resulting numerator or denominator exceeds * {@code Integer.MAX_VALUE} */ public Fraction multiply(Fraction fraction) { if (fraction == null) { throw new NullArgumentException(LocalizedFormats.FRACTION); } if (numerator == 0 || fraction.numerator == 0) { return ZERO; } // knuth 4.5.1 // make sure we don't overflow unless the result *must* overflow. int d1 = ArithmeticUtils.gcd(numerator, fraction.denominator); int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator); return getReducedFraction (ArithmeticUtils.mulAndCheck(numerator/d1, fraction.numerator/d2), ArithmeticUtils.mulAndCheck(denominator/d2, fraction.denominator/d1)); }
Example #4
Source File: Math_26_Fraction_t.java From coming with MIT License | 6 votes |
/** * <p>Multiplies the value of this fraction by another, returning the * result in reduced form.</p> * * @param fraction the fraction to multiply by, must not be {@code null} * @return a {@code Fraction} instance with the resulting values * @throws NullArgumentException if the fraction is {@code null} * @throws MathArithmeticException if the resulting numerator or denominator exceeds * {@code Integer.MAX_VALUE} */ public Fraction multiply(Fraction fraction) { if (fraction == null) { throw new NullArgumentException(LocalizedFormats.FRACTION); } if (numerator == 0 || fraction.numerator == 0) { return ZERO; } // knuth 4.5.1 // make sure we don't overflow unless the result *must* overflow. int d1 = ArithmeticUtils.gcd(numerator, fraction.denominator); int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator); return getReducedFraction (ArithmeticUtils.mulAndCheck(numerator/d1, fraction.numerator/d2), ArithmeticUtils.mulAndCheck(denominator/d2, fraction.denominator/d1)); }
Example #5
Source File: Math_27_Fraction_t.java From coming with MIT License | 6 votes |
/** * <p>Multiplies the value of this fraction by another, returning the * result in reduced form.</p> * * @param fraction the fraction to multiply by, must not be {@code null} * @return a {@code Fraction} instance with the resulting values * @throws NullArgumentException if the fraction is {@code null} * @throws MathArithmeticException if the resulting numerator or denominator exceeds * {@code Integer.MAX_VALUE} */ public Fraction multiply(Fraction fraction) { if (fraction == null) { throw new NullArgumentException(LocalizedFormats.FRACTION); } if (numerator == 0 || fraction.numerator == 0) { return ZERO; } // knuth 4.5.1 // make sure we don't overflow unless the result *must* overflow. int d1 = ArithmeticUtils.gcd(numerator, fraction.denominator); int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator); return getReducedFraction (ArithmeticUtils.mulAndCheck(numerator/d1, fraction.numerator/d2), ArithmeticUtils.mulAndCheck(denominator/d2, fraction.denominator/d1)); }
Example #6
Source File: Fraction.java From astor with GNU General Public License v2.0 | 6 votes |
/** * <p>Multiplies the value of this fraction by another, returning the * result in reduced form.</p> * * @param fraction the fraction to multiply by, must not be {@code null} * @return a {@code Fraction} instance with the resulting values * @throws NullArgumentException if the fraction is {@code null} * @throws MathArithmeticException if the resulting numerator or denominator exceeds * {@code Integer.MAX_VALUE} */ public Fraction multiply(Fraction fraction) { if (fraction == null) { throw new NullArgumentException(LocalizedFormats.FRACTION); } if (numerator == 0 || fraction.numerator == 0) { return ZERO; } // knuth 4.5.1 // make sure we don't overflow unless the result *must* overflow. int d1 = ArithmeticUtils.gcd(numerator, fraction.denominator); int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator); return getReducedFraction (ArithmeticUtils.mulAndCheck(numerator/d1, fraction.numerator/d2), ArithmeticUtils.mulAndCheck(denominator/d2, fraction.denominator/d1)); }
Example #7
Source File: MetricsSystemImpl.java From big-c with Apache License 2.0 | 6 votes |
private synchronized void configureSinks() { sinkConfigs = config.getInstanceConfigs(SINK_KEY); int confPeriod = 0; for (Entry<String, MetricsConfig> entry : sinkConfigs.entrySet()) { MetricsConfig conf = entry.getValue(); int sinkPeriod = conf.getInt(PERIOD_KEY, PERIOD_DEFAULT); confPeriod = confPeriod == 0 ? sinkPeriod : ArithmeticUtils.gcd(confPeriod, sinkPeriod); String clsName = conf.getClassName(""); if (clsName == null) continue; // sink can be registered later on String sinkName = entry.getKey(); try { MetricsSinkAdapter sa = newSink(sinkName, conf.getString(DESC_KEY, sinkName), conf); sa.start(); sinks.put(sinkName, sa); } catch (Exception e) { LOG.warn("Error creating sink '"+ sinkName +"'", e); } } period = confPeriod > 0 ? confPeriod : config.getInt(PERIOD_KEY, PERIOD_DEFAULT); }
Example #8
Source File: Math_1_Fraction_s.java From coming with MIT License | 6 votes |
/** * <p>Multiplies the value of this fraction by another, returning the * result in reduced form.</p> * * @param fraction the fraction to multiply by, must not be {@code null} * @return a {@code Fraction} instance with the resulting values * @throws NullArgumentException if the fraction is {@code null} * @throws MathArithmeticException if the resulting numerator or denominator exceeds * {@code Integer.MAX_VALUE} */ public Fraction multiply(Fraction fraction) { if (fraction == null) { throw new NullArgumentException(LocalizedFormats.FRACTION); } if (numerator == 0 || fraction.numerator == 0) { return ZERO; } // knuth 4.5.1 // make sure we don't overflow unless the result *must* overflow. int d1 = ArithmeticUtils.gcd(numerator, fraction.denominator); int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator); return getReducedFraction (ArithmeticUtils.mulAndCheck(numerator/d1, fraction.numerator/d2), ArithmeticUtils.mulAndCheck(denominator/d2, fraction.denominator/d1)); }
Example #9
Source File: Math_1_Fraction_t.java From coming with MIT License | 6 votes |
/** * <p>Multiplies the value of this fraction by another, returning the * result in reduced form.</p> * * @param fraction the fraction to multiply by, must not be {@code null} * @return a {@code Fraction} instance with the resulting values * @throws NullArgumentException if the fraction is {@code null} * @throws MathArithmeticException if the resulting numerator or denominator exceeds * {@code Integer.MAX_VALUE} */ public Fraction multiply(Fraction fraction) { if (fraction == null) { throw new NullArgumentException(LocalizedFormats.FRACTION); } if (numerator == 0 || fraction.numerator == 0) { return ZERO; } // knuth 4.5.1 // make sure we don't overflow unless the result *must* overflow. int d1 = ArithmeticUtils.gcd(numerator, fraction.denominator); int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator); return getReducedFraction (ArithmeticUtils.mulAndCheck(numerator/d1, fraction.numerator/d2), ArithmeticUtils.mulAndCheck(denominator/d2, fraction.denominator/d1)); }
Example #10
Source File: Fraction.java From astor with GNU General Public License v2.0 | 6 votes |
/** * <p>Multiplies the value of this fraction by another, returning the * result in reduced form.</p> * * @param fraction the fraction to multiply by, must not be {@code null} * @return a {@code Fraction} instance with the resulting values * @throws NullArgumentException if the fraction is {@code null} * @throws MathArithmeticException if the resulting numerator or denominator exceeds * {@code Integer.MAX_VALUE} */ public Fraction multiply(Fraction fraction) { if (fraction == null) { throw new NullArgumentException(LocalizedFormats.FRACTION); } if (numerator == 0 || fraction.numerator == 0) { return ZERO; } // knuth 4.5.1 // make sure we don't overflow unless the result *must* overflow. int d1 = ArithmeticUtils.gcd(numerator, fraction.denominator); int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator); return getReducedFraction (ArithmeticUtils.mulAndCheck(numerator/d1, fraction.numerator/d2), ArithmeticUtils.mulAndCheck(denominator/d2, fraction.denominator/d1)); }
Example #11
Source File: Fraction.java From astor with GNU General Public License v2.0 | 6 votes |
/** * <p>Multiplies the value of this fraction by another, returning the * result in reduced form.</p> * * @param fraction the fraction to multiply by, must not be {@code null} * @return a {@code Fraction} instance with the resulting values * @throws NullArgumentException if the fraction is {@code null} * @throws MathArithmeticException if the resulting numerator or denominator exceeds * {@code Integer.MAX_VALUE} */ public Fraction multiply(Fraction fraction) { if (fraction == null) { throw new NullArgumentException(LocalizedFormats.FRACTION); } if (numerator == 0 || fraction.numerator == 0) { return ZERO; } // knuth 4.5.1 // make sure we don't overflow unless the result *must* overflow. int d1 = ArithmeticUtils.gcd(numerator, fraction.denominator); int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator); return getReducedFraction (ArithmeticUtils.mulAndCheck(numerator/d1, fraction.numerator/d2), ArithmeticUtils.mulAndCheck(denominator/d2, fraction.denominator/d1)); }
Example #12
Source File: Fraction.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Create a fraction given the numerator and denominator. The fraction is * reduced to lowest terms. * @param num the numerator. * @param den the denominator. * @throws MathArithmeticException if the denominator is {@code zero} */ public Fraction(int num, int den) { if (den == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION, num, den); } if (den < 0) { if (num == Integer.MIN_VALUE || den == Integer.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION, num, den); } num = -num; den = -den; } // reduce numerator and denominator by greatest common denominator. final int d = ArithmeticUtils.gcd(num, den); if (d > 1) { num /= d; den /= d; } // move sign to numerator. if (den < 0) { num = -num; den = -den; } this.numerator = num; this.denominator = den; }
Example #13
Source File: LongBufferedSumAggregator.java From spliceengine with GNU Affero General Public License v3.0 | 5 votes |
private void sum(int bufferLength) throws StandardException { long newSum = sum; try { for (int i = 0; i < bufferLength; i++) { newSum = ArithmeticUtils.addAndCheck(newSum, buffer[i]); } } catch (MathArithmeticException e) { throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE,"BIGINT"); } sum = newSum; }
Example #14
Source File: InverseHilbertMatrix.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Returns the {@code (i, j)} entry of the inverse Hilbert matrix. Exact * arithmetic is used; in case of overflow, an exception is thrown. * * @param i Row index (starts at 0). * @param j Column index (starts at 0). * @return The coefficient of the inverse Hilbert matrix. */ public long getEntry(final int i, final int j) { long val = i + j + 1; long aux = CombinatoricsUtils.binomialCoefficient(n + i, n - j - 1); val = ArithmeticUtils.mulAndCheck(val, aux); aux = CombinatoricsUtils.binomialCoefficient(n + j, n - i - 1); val = ArithmeticUtils.mulAndCheck(val, aux); aux = CombinatoricsUtils.binomialCoefficient(i + j, i); val = ArithmeticUtils.mulAndCheck(val, aux); val = ArithmeticUtils.mulAndCheck(val, aux); return ((i + j) & 1) == 0 ? val : -val; }
Example #15
Source File: DerivativeStructureTest.java From astor with GNU General Public License v2.0 | 5 votes |
@Test public void testReciprocal() { for (double x = 0.1; x < 1.2; x += 0.1) { DerivativeStructure r = new DerivativeStructure(1, 6, 0, x).reciprocal(); Assert.assertEquals(1 / x, r.getValue(), 1.0e-15); for (int i = 1; i < r.getOrder(); ++i) { double expected = ArithmeticUtils.pow(-1, i) * ArithmeticUtils.factorial(i) / FastMath.pow(x, i + 1); Assert.assertEquals(expected, r.getPartialDerivative(i), 1.0e-15 * FastMath.abs(expected)); } } }
Example #16
Source File: Fraction.java From astor with GNU General Public License v2.0 | 5 votes |
/** * <p>Creates a {@code Fraction} instance with the 2 parts * of a fraction Y/Z.</p> * * <p>Any negative signs are resolved to be on the numerator.</p> * * @param numerator the numerator, for example the three in 'three sevenths' * @param denominator the denominator, for example the seven in 'three sevenths' * @return a new fraction instance, with the numerator and denominator reduced * @throws MathArithmeticException if the denominator is {@code zero} */ public static Fraction getReducedFraction(int numerator, int denominator) { if (denominator == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION, numerator, denominator); } if (numerator==0) { return ZERO; // normalize zero. } // allow 2^k/-2^31 as a valid fraction (where k>0) if (denominator==Integer.MIN_VALUE && (numerator&1)==0) { numerator/=2; denominator/=2; } if (denominator < 0) { if (numerator==Integer.MIN_VALUE || denominator==Integer.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION, numerator, denominator); } numerator = -numerator; denominator = -denominator; } // simplify fraction. int gcd = ArithmeticUtils.gcd(numerator, denominator); numerator /= gcd; denominator /= gcd; return new Fraction(numerator, denominator); }
Example #17
Source File: DSCompilerTest.java From astor with GNU General Public License v2.0 | 5 votes |
@Test public void testSize() { for (int i = 0; i < 6; ++i) { for (int j = 0; j < 6; ++j) { long expected = ArithmeticUtils.binomialCoefficient(i + j, i); Assert.assertEquals(expected, DSCompiler.getCompiler(i, j).getSize()); Assert.assertEquals(expected, DSCompiler.getCompiler(j, i).getSize()); } } }
Example #18
Source File: InverseHilbertMatrix.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Returns the {@code (i, j)} entry of the inverse Hilbert matrix. Exact * arithmetic is used; in case of overflow, an exception is thrown. * * @param i Row index (starts at 0). * @param j Column index (starts at 0). * @return The coefficient of the inverse Hilbert matrix. */ public long getEntry(final int i, final int j) { long val = i + j + 1; long aux = ArithmeticUtils.binomialCoefficient(n + i, n - j - 1); val = ArithmeticUtils.mulAndCheck(val, aux); aux = ArithmeticUtils.binomialCoefficient(n + j, n - i - 1); val = ArithmeticUtils.mulAndCheck(val, aux); aux = ArithmeticUtils.binomialCoefficient(i + j, i); val = ArithmeticUtils.mulAndCheck(val, aux); val = ArithmeticUtils.mulAndCheck(val, aux); return ((i + j) & 1) == 0 ? val : -val; }
Example #19
Source File: SlidingWindowAssigner.java From flink with Apache License 2.0 | 5 votes |
protected SlidingWindowAssigner(long size, long slide, long offset, boolean isEventTime) { if (size <= 0 || slide <= 0) { throw new IllegalArgumentException( "SlidingWindowAssigner parameters must satisfy slide > 0 and size > 0"); } this.size = size; this.slide = slide; this.offset = offset; this.isEventTime = isEventTime; this.paneSize = ArithmeticUtils.gcd(size, slide); this.numPanesPerWindow = MathUtils.checkedDownCast(size / paneSize); }
Example #20
Source File: PascalDistribution.java From astor with GNU General Public License v2.0 | 5 votes |
/** {@inheritDoc} */ public double probability(int x) { double ret; if (x < 0) { ret = 0.0; } else { ret = ArithmeticUtils.binomialCoefficientDouble(x + numberOfSuccesses - 1, numberOfSuccesses - 1) * FastMath.pow(probabilityOfSuccess, numberOfSuccesses) * FastMath.pow(1.0 - probabilityOfSuccess, x); } return ret; }
Example #21
Source File: InverseHilbertMatrix.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Returns the {@code (i, j)} entry of the inverse Hilbert matrix. Exact * arithmetic is used; in case of overflow, an exception is thrown. * * @param i Row index (starts at 0). * @param j Column index (starts at 0). * @return The coefficient of the inverse Hilbert matrix. */ public long getEntry(final int i, final int j) { long val = i + j + 1; long aux = ArithmeticUtils.binomialCoefficient(n + i, n - j - 1); val = ArithmeticUtils.mulAndCheck(val, aux); aux = ArithmeticUtils.binomialCoefficient(n + j, n - i - 1); val = ArithmeticUtils.mulAndCheck(val, aux); aux = ArithmeticUtils.binomialCoefficient(i + j, i); val = ArithmeticUtils.mulAndCheck(val, aux); val = ArithmeticUtils.mulAndCheck(val, aux); return ((i + j) & 1) == 0 ? val : -val; }
Example #22
Source File: DSCompilerTest.java From astor with GNU General Public License v2.0 | 5 votes |
@Test public void testSize() { for (int i = 0; i < 6; ++i) { for (int j = 0; j < 6; ++j) { long expected = ArithmeticUtils.binomialCoefficient(i + j, i); Assert.assertEquals(expected, DSCompiler.getCompiler(i, j).getSize()); Assert.assertEquals(expected, DSCompiler.getCompiler(j, i).getSize()); } } }
Example #23
Source File: DSCompiler.java From astor with GNU General Public License v2.0 | 5 votes |
/** Evaluate Taylor expansion of a derivative structure. * @param ds array holding the derivative structure * @param dsOffset offset of the derivative structure in its array * @param delta parameters offsets (Δx, Δy, ...) * @return value of the Taylor expansion at x + Δx, y + Δy, ... */ public double taylor(final double[] ds, final int dsOffset, final double ... delta) { double value = 0; for (int i = getSize() - 1; i >= 0; --i) { final int[] orders = getPartialDerivativeOrders(i); double term = ds[dsOffset + i]; for (int k = 0; k < orders.length; ++k) { if (orders[k] > 0) { term *= FastMath.pow(delta[k], orders[k]) / ArithmeticUtils.factorial(orders[k]); } } value += term; } return value; }
Example #24
Source File: InverseHilbertMatrix.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Returns the {@code (i, j)} entry of the inverse Hilbert matrix. Exact * arithmetic is used; in case of overflow, an exception is thrown. * * @param i Row index (starts at 0). * @param j Column index (starts at 0). * @return The coefficient of the inverse Hilbert matrix. */ public long getEntry(final int i, final int j) { long val = i + j + 1; long aux = CombinatoricsUtils.binomialCoefficient(n + i, n - j - 1); val = ArithmeticUtils.mulAndCheck(val, aux); aux = CombinatoricsUtils.binomialCoefficient(n + j, n - i - 1); val = ArithmeticUtils.mulAndCheck(val, aux); aux = CombinatoricsUtils.binomialCoefficient(i + j, i); val = ArithmeticUtils.mulAndCheck(val, aux); val = ArithmeticUtils.mulAndCheck(val, aux); return ((i + j) & 1) == 0 ? val : -val; }
Example #25
Source File: PascalDistribution.java From astor with GNU General Public License v2.0 | 5 votes |
/** {@inheritDoc} */ public double probability(int x) { double ret; if (x < 0) { ret = 0.0; } else { ret = ArithmeticUtils.binomialCoefficientDouble(x + numberOfSuccesses - 1, numberOfSuccesses - 1) * FastMath.pow(probabilityOfSuccess, numberOfSuccesses) * FastMath.pow(1.0 - probabilityOfSuccess, x); } return ret; }
Example #26
Source File: Fraction.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Create a fraction given the numerator and denominator. The fraction is * reduced to lowest terms. * @param num the numerator. * @param den the denominator. * @throws MathArithmeticException if the denominator is {@code zero} */ public Fraction(int num, int den) { if (den == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION, num, den); } if (den < 0) { if (num == Integer.MIN_VALUE || den == Integer.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION, num, den); } num = -num; den = -den; } // reduce numerator and denominator by greatest common denominator. final int d = ArithmeticUtils.gcd(num, den); if (d > 1) { num /= d; den /= d; } // move sign to numerator. if (den < 0) { num = -num; den = -den; } this.numerator = num; this.denominator = den; }
Example #27
Source File: DerivativeStructureTest.java From astor with GNU General Public License v2.0 | 5 votes |
@Test public void testReciprocal() { for (double x = 0.1; x < 1.2; x += 0.1) { DerivativeStructure r = new DerivativeStructure(1, 6, 0, x).reciprocal(); Assert.assertEquals(1 / x, r.getValue(), 1.0e-15); for (int i = 1; i < r.getOrder(); ++i) { double expected = ArithmeticUtils.pow(-1, i) * CombinatoricsUtils.factorial(i) / FastMath.pow(x, i + 1); Assert.assertEquals(expected, r.getPartialDerivative(i), 1.0e-15 * FastMath.abs(expected)); } } }
Example #28
Source File: Fraction.java From astor with GNU General Public License v2.0 | 5 votes |
/** * <p>Creates a {@code Fraction} instance with the 2 parts * of a fraction Y/Z.</p> * * <p>Any negative signs are resolved to be on the numerator.</p> * * @param numerator the numerator, for example the three in 'three sevenths' * @param denominator the denominator, for example the seven in 'three sevenths' * @return a new fraction instance, with the numerator and denominator reduced * @throws MathArithmeticException if the denominator is {@code zero} */ public static Fraction getReducedFraction(int numerator, int denominator) { if (denominator == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION, numerator, denominator); } if (numerator==0) { return ZERO; // normalize zero. } // allow 2^k/-2^31 as a valid fraction (where k>0) if (denominator==Integer.MIN_VALUE && (numerator&1)==0) { numerator/=2; denominator/=2; } if (denominator < 0) { if (numerator==Integer.MIN_VALUE || denominator==Integer.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION, numerator, denominator); } numerator = -numerator; denominator = -denominator; } // simplify fraction. int gcd = ArithmeticUtils.gcd(numerator, denominator); numerator /= gcd; denominator /= gcd; return new Fraction(numerator, denominator); }
Example #29
Source File: PascalDistribution.java From astor with GNU General Public License v2.0 | 5 votes |
/** {@inheritDoc} */ public double probability(int x) { double ret; if (x < 0) { ret = 0.0; } else { ret = ArithmeticUtils.binomialCoefficientDouble(x + numberOfSuccesses - 1, numberOfSuccesses - 1) * FastMath.pow(probabilityOfSuccess, numberOfSuccesses) * FastMath.pow(1.0 - probabilityOfSuccess, x); } return ret; }
Example #30
Source File: InverseHilbertMatrix.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Returns the {@code (i, j)} entry of the inverse Hilbert matrix. Exact * arithmetic is used; in case of overflow, an exception is thrown. * * @param i Row index (starts at 0). * @param j Column index (starts at 0). * @return The coefficient of the inverse Hilbert matrix. */ public long getEntry(final int i, final int j) { long val = i + j + 1; long aux = ArithmeticUtils.binomialCoefficient(n + i, n - j - 1); val = ArithmeticUtils.mulAndCheck(val, aux); aux = ArithmeticUtils.binomialCoefficient(n + j, n - i - 1); val = ArithmeticUtils.mulAndCheck(val, aux); aux = ArithmeticUtils.binomialCoefficient(i + j, i); val = ArithmeticUtils.mulAndCheck(val, aux); val = ArithmeticUtils.mulAndCheck(val, aux); return ((i + j) & 1) == 0 ? val : -val; }