CombinatoricsUtils.checkBinomial(n, k); if ((n == k) || (k == 0)) { return 0; return FastMath.log(binomialCoefficient(n,k)); return FastMath.log(binomialCoefficientDouble(n, k)); return binomialCoefficientLog(n, n - k);
/** * 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! < 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}. * @deprecated use {@link CombinatoricsUtils#factorialDouble(int)} */ @Deprecated public static double factorialDouble(final int n) throws NotPositiveException { return CombinatoricsUtils.factorialDouble(n); }
return (1l << (n - 1)) - 1l; } else if (k == n - 1) { return binomialCoefficient(n, 2); } else { for (int j = 1; j <= k; ++j) { sign = -sign; sum += sign * binomialCoefficient(k, j) * ArithmeticUtils.pow(j, n); if (sum < 0) { return sum / factorial(k);
CombinatoricsUtils.checkBinomial(n, k); if ((n == k) || (k == 0)) { return 1d; return binomialCoefficientDouble(n, n - k); return binomialCoefficient(n,k);
public static long binomialCoefficient(final int n, final int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException { return CombinatoricsUtils.binomialCoefficient(n, k);
/** * Returns n!. Shorthand for {@code n} <a * href="http://mathworld.wolfram.com/Factorial.html"> Factorial</a>, the * product of the numbers {@code 1,...,n}. * <p> * <Strong>Preconditions</strong>: * <ul> * <li> {@code n >= 0} (otherwise * {@code IllegalArgumentException} is thrown)</li> * <li> The result is small enough to fit into a {@code long}. The * largest value of {@code n} for which {@code n!} < * Long.MAX_VALUE} is 20. If the computed value exceeds {@code Long.MAX_VALUE} * an {@code ArithMeticException } is thrown.</li> * </ul> * </p> * * @param n argument * @return {@code n!} * @throws MathArithmeticException if the result is too large to be represented * by a {@code long}. * @throws NotPositiveException if {@code n < 0}. * @throws MathArithmeticException if {@code n > 20}: The factorial value is too * large to fit in a {@code long}. * @deprecated use {@link CombinatoricsUtils#factorial(int)} */ @Deprecated public static long factorial(final int n) throws NotPositiveException, MathArithmeticException { return CombinatoricsUtils.factorial(n); }
CombinatoricsUtils.checkBinomial(n, k); if ((n == k) || (k == 0)) { return 1; return binomialCoefficient(n, n - k);
public static double binomialCoefficientDouble(final int n, final int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException { return CombinatoricsUtils.binomialCoefficientDouble(n, k);
/** * Compute the natural logarithm of the factorial of {@code n}. * * @param n Argument. * @return {@code n!} * @throws NotPositiveException if {@code n < 0}. * @deprecated use {@link CombinatoricsUtils#factorialLog(int)} */ @Deprecated public static double factorialLog(final int n) throws NotPositiveException { return CombinatoricsUtils.factorialLog(n); }
/** {@inheritDoc} */ @Override public double logProbability(int x) { double ret; if (x < 0) { ret = Double.NEGATIVE_INFINITY; } else { ret = CombinatoricsUtils.binomialCoefficientLog(x + numberOfSuccesses - 1, numberOfSuccesses - 1) + logProbabilityOfSuccess * numberOfSuccesses + log1mProbabilityOfSuccess * x; } return ret; }
int k, IterationOrder iterationOrder) { CombinatoricsUtils.checkBinomial(n, k); this.n = n; this.k = k;
/** * @param vector vector whose dot product with hashed vectors is to be maximized * @return indices of partitions containing candidates to check */ int[] getCandidateIndices(float[] vector) { int mainIndex = getIndexFor(vector); // Simple cases int numHashes = getNumHashes(); if (numHashes == maxBitsDiffering) { return allIndices; } if (maxBitsDiffering == 0) { return new int[] { mainIndex }; } // Other cases int howMany = 0; for (int i = 0; i <= maxBitsDiffering; i++) { howMany += (int) CombinatoricsUtils.binomialCoefficient(numHashes, i); } int[] result = new int[howMany]; System.arraycopy(candidateIndicesPrototype, 0, result, 0, howMany); for (int i = 0; i < howMany; i++) { result[i] ^= mainIndex; } return result; }
CombinatoricsUtils.checkBinomial(n, k); if ((n == k) || (k == 0)) { return 1d; return binomialCoefficientDouble(n, n - k); return binomialCoefficient(n,k);
double inv = 1.0 / CombinatoricsUtils.factorial(i); for (int j = 0; j < y.length; ++j) { y[j] *= inv;
CombinatoricsUtils.checkBinomial(n, k); if ((n == k) || (k == 0)) { return 1; return binomialCoefficient(n, n - k);
/** {@inheritDoc} */ public double probability(int x) { double ret; if (x < 0) { ret = 0.0; } else { ret = CombinatoricsUtils.binomialCoefficientDouble(x + numberOfSuccesses - 1, numberOfSuccesses - 1) * FastMath.pow(probabilityOfSuccess, numberOfSuccesses) * FastMath.pow(1.0 - probabilityOfSuccess, x); } return ret; }
/** * 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); }
/** * Returns the natural {@code log} of the <a * href="http://mathworld.wolfram.com/BinomialCoefficient.html"> Binomial * Coefficient</a>, "{@code n choose k}", the number of * {@code k}-element subsets that can be selected from an * {@code n}-element set. * <p> * <Strong>Preconditions</strong>: * <ul> * <li> {@code 0 <= k <= n } (otherwise * {@code IllegalArgumentException} is thrown)</li> * </ul></p> * * @param n the size of the set * @param k the size of the subsets to be counted * @return {@code n choose k} * @throws NotPositiveException if {@code n < 0}. * @throws NumberIsTooLargeException if {@code k > n}. * @throws MathArithmeticException if the result is too large to be * represented by a long integer. * @deprecated use {@link CombinatoricsUtils#binomialCoefficientLog(int, int)} */ @Deprecated public static double binomialCoefficientLog(final int n, final int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException { return CombinatoricsUtils.binomialCoefficientLog(n, k); }
int k, IterationOrder iterationOrder) { CombinatoricsUtils.checkBinomial(n, k); this.n = n; this.k = k;
CombinatoricsUtils.checkBinomial(n, k); if ((n == k) || (k == 0)) { return 0; return Math.log(binomialCoefficient(n,k)); return Math.log(binomialCoefficientDouble(n, k)); return binomialCoefficientLog(n, n - k);