Java Code Examples for org.apache.commons.math3.util.ArithmeticUtils#binomialCoefficient()

The following examples show how to use org.apache.commons.math3.util.ArithmeticUtils#binomialCoefficient() . 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: CollectionUtils.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
/**
 * Generates all combinations of items in the input of the given length.
 *
 * @param input   Input to generate the combinations from.
 * @param length  Number of items per combination.
 * @return All combinations of length in the input.
 */
public static <T> Set<List<T>> getAllItemCombinations(T[] input, int length) {
  if (length > input.length) {
    throw new IllegalArgumentException("Length must not be larger than the length of the input.");
  }
  if (input == null || input.length == 0) {
    return new HashSet<>();
  }
  long totalCombinations = ArithmeticUtils.binomialCoefficient(input.length, length);
  if (totalCombinations > Integer.MAX_VALUE) {
    throw new IllegalArgumentException("Too many combinations for the given input and length");
  }
  Set<List<T>> allCombinations = new HashSet<>((int) totalCombinations);
  computeAllItemCombinationsRecursive(input, allCombinations, null, 0, length);
  return allCombinations;
}
 
Example 2
Source File: DSCompilerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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 3
Source File: PolynomialsUtilsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testJacobiEvaluationAt1() {
    for (int v = 0; v < 10; ++v) {
        for (int w = 0; w < 10; ++w) {
            for (int i = 0; i < 10; ++i) {
                PolynomialFunction jacobi = PolynomialsUtils.createJacobiPolynomial(i, v, w);
                double binomial = ArithmeticUtils.binomialCoefficient(v + i, i);
                Assert.assertTrue(Precision.equals(binomial, jacobi.value(1.0), 1));
            }
        }
    }
}
 
Example 4
Source File: InverseHilbertMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 5
Source File: PolynomialsUtilsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testJacobiEvaluationAt1() {
    for (int v = 0; v < 10; ++v) {
        for (int w = 0; w < 10; ++w) {
            for (int i = 0; i < 10; ++i) {
                PolynomialFunction jacobi = PolynomialsUtils.createJacobiPolynomial(i, v, w);
                double binomial = ArithmeticUtils.binomialCoefficient(v + i, i);
                Assert.assertTrue(Precision.equals(binomial, jacobi.value(1.0), 1));
            }
        }
    }
}
 
Example 6
Source File: InverseHilbertMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 7
Source File: PolynomialsUtilsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testJacobiEvaluationAt1() {
    for (int v = 0; v < 10; ++v) {
        for (int w = 0; w < 10; ++w) {
            for (int i = 0; i < 10; ++i) {
                PolynomialFunction jacobi = PolynomialsUtils.createJacobiPolynomial(i, v, w);
                double binomial = ArithmeticUtils.binomialCoefficient(v + i, i);
                Assert.assertTrue(Precision.equals(binomial, jacobi.value(1.0), 1));
            }
        }
    }
}
 
Example 8
Source File: InverseHilbertMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 9
Source File: DSCompilerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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 10
Source File: PolynomialsUtilsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testJacobiEvaluationAt1() {
    for (int v = 0; v < 10; ++v) {
        for (int w = 0; w < 10; ++w) {
            for (int i = 0; i < 10; ++i) {
                PolynomialFunction jacobi = PolynomialsUtils.createJacobiPolynomial(i, v, w);
                double binomial = ArithmeticUtils.binomialCoefficient(v + i, i);
                Assert.assertTrue(Precision.equals(binomial, jacobi.value(1.0), 1));
            }
        }
    }
}
 
Example 11
Source File: InverseHilbertMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 12
Source File: DSCompilerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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 13
Source File: PolynomialsUtilsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testJacobiEvaluationAt1() {
    for (int v = 0; v < 10; ++v) {
        for (int w = 0; w < 10; ++w) {
            for (int i = 0; i < 10; ++i) {
                PolynomialFunction jacobi = PolynomialsUtils.createJacobiPolynomial(i, v, w);
                double binomial = ArithmeticUtils.binomialCoefficient(v + i, i);
                Assert.assertTrue(Precision.equals(binomial, jacobi.value(1.0), 1));
            }
        }
    }
}
 
Example 14
Source File: InverseHilbertMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}