Java Code Examples for org.apache.commons.math3.util.CombinatoricsUtils#factorial()

The following examples show how to use org.apache.commons.math3.util.CombinatoricsUtils#factorial() . 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: AssociatedLegendrePolynomialTest.java    From cineast with MIT License 6 votes vote down vote up
/**
 * Tests if orthogonality relation regarding a fixed l holds true for all l between 0 and 5.
 */
@Test
@DisplayName("Test Orthogonality (l)")
public void testOrthogonalityL() {
    final double dx = 1e-4;
    for (int l1=0;l1<=5;l1++) {
        for (int l2=0;l2<=5;l2++) {
            for (int m=0;m<=l1 && m<=l2;m++) {
                final AssociatedLegendrePolynomial alp1 = new AssociatedLegendrePolynomial(l1,m);
                final AssociatedLegendrePolynomial alp2 = new AssociatedLegendrePolynomial(l2,m);
                double result = 0.0;
                final double expected = (2.0)/(2*l1+1) * ((double)CombinatoricsUtils.factorial(l1+m)/(double)CombinatoricsUtils.factorial(l1-m)) * MathHelper.kronecker(l1,l2);
                for (double x = -1.0; x <= 1.0; x+=dx) {
                    result += (alp1.value(x) * alp2.value(x)) * dx;
                }
                assertEquals(expected, result,  1e-3);
            }
        }

    }
}
 
Example 2
Source File: DSCompiler.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** 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 (&Delta;x, &Delta;y, ...)
 * @return value of the Taylor expansion at x + &Delta;x, y + &Delta;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]) /
                    CombinatoricsUtils.factorial(orders[k]);
                } catch (NotPositiveException e) {
                    // this cannot happen
                    throw new MathInternalError(e);
                }
            }
        }
        value += term;
    }
    return value;
}
 
Example 3
Source File: DSCompiler.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** 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 (&Delta;x, &Delta;y, ...)
 * @return value of the Taylor expansion at x + &Delta;x, y + &Delta;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]) /
                    CombinatoricsUtils.factorial(orders[k]);
                } catch (NotPositiveException e) {
                    // this cannot happen
                    throw new MathInternalError(e);
                }
            }
        }
        value += term;
    }
    return value;
}
 
Example 4
Source File: DSCompiler.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** 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 (&Delta;x, &Delta;y, ...)
 * @return value of the Taylor expansion at x + &Delta;x, y + &Delta;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]) /
                    CombinatoricsUtils.factorial(orders[k]);
                } catch (NotPositiveException e) {
                    // this cannot happen
                    throw new MathInternalError(e);
                }
            }
        }
        value += term;
    }
    return value;
}
 
Example 5
Source File: DerivativeStructureTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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 6
Source File: DerivativeStructureTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test
public void testLog() {
    double[] epsilon = new double[] { 1.0e-16, 1.0e-16, 3.0e-14, 7.0e-13, 3.0e-11 };
    for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
        for (double x = 0.1; x < 1.2; x += 0.001) {
            DerivativeStructure log = new DerivativeStructure(1, maxOrder, 0, x).log();
            Assert.assertEquals(FastMath.log(x), log.getValue(), epsilon[0]);
            for (int n = 1; n <= maxOrder; ++n) {
                double refDer = -CombinatoricsUtils.factorial(n - 1) / FastMath.pow(-x, n);
                Assert.assertEquals(refDer, log.getPartialDerivative(n), epsilon[n]);
            }
        }
    }
}
 
Example 7
Source File: DerivativeStructureTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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 8
Source File: DerivativeStructureTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test
public void testLog() {
    double[] epsilon = new double[] { 1.0e-16, 1.0e-16, 3.0e-14, 7.0e-13, 3.0e-11 };
    for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
        for (double x = 0.1; x < 1.2; x += 0.001) {
            DerivativeStructure log = new DerivativeStructure(1, maxOrder, 0, x).log();
            Assert.assertEquals(FastMath.log(x), log.getValue(), epsilon[0]);
            for (int n = 1; n <= maxOrder; ++n) {
                double refDer = -CombinatoricsUtils.factorial(n - 1) / FastMath.pow(-x, n);
                Assert.assertEquals(refDer, log.getPartialDerivative(n), epsilon[n]);
            }
        }
    }
}
 
Example 9
Source File: DerivativeStructureTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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 10
Source File: DerivativeStructureTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test
public void testLog() {
    double[] epsilon = new double[] { 1.0e-16, 1.0e-16, 3.0e-14, 7.0e-13, 3.0e-11 };
    for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
        for (double x = 0.1; x < 1.2; x += 0.001) {
            DerivativeStructure log = new DerivativeStructure(1, maxOrder, 0, x).log();
            Assert.assertEquals(FastMath.log(x), log.getValue(), epsilon[0]);
            for (int n = 1; n <= maxOrder; ++n) {
                double refDer = -CombinatoricsUtils.factorial(n - 1) / FastMath.pow(-x, n);
                Assert.assertEquals(refDer, log.getPartialDerivative(n), epsilon[n]);
            }
        }
    }
}
 
Example 11
Source File: Factorial.java    From tutorials with MIT License 4 votes vote down vote up
public long factorialUsingApacheCommons(int n) {
    return CombinatoricsUtils.factorial(n);
}