Example usage for org.apache.commons.math3.util CombinatoricsUtils factorialDouble

List of usage examples for org.apache.commons.math3.util CombinatoricsUtils factorialDouble

Introduction

In this page you can find the example usage for org.apache.commons.math3.util CombinatoricsUtils factorialDouble.

Prototype

public static double factorialDouble(final int n) throws NotPositiveException 

Source Link

Document

Compute n!, the<a href="http://mathworld.wolfram.com/Factorial.html"> factorial</a> of n (the product of the numbers 1 to n), as a double .

Usage

From source file:com.opengamma.strata.math.impl.function.special.OrthonormalHermitePolynomialFunctionTest.java

@Test
public void test() {
    final int n = 15;
    final DoubleFunction1D[] f1 = HERMITE.getPolynomials(n);
    final DoubleFunction1D[] f2 = ORTHONORMAL.getPolynomials(n);
    final double x = 3.4;
    for (int i = 0; i < f1.length; i++) {
        assertEquals(// www  .  j  a  v a2  s . c  o  m
                f1[i].applyAsDouble(x) / Math
                        .sqrt(CombinatoricsUtils.factorialDouble(i) * Math.pow(2, i) * Math.sqrt(Math.PI)),
                f2[i].applyAsDouble(x), EPS);
    }
}

From source file:com.opengamma.strata.math.impl.integration.GaussLaguerreWeightAndAbscissaFunction.java

/**
 * {@inheritDoc}/*from   w w w  .j a  v a  2 s.com*/
 */
@Override
public GaussianQuadratureData generate(int n) {
    ArgChecker.isTrue(n > 0);
    Pair<DoubleFunction1D, DoubleFunction1D>[] polynomials = LAGUERRE.getPolynomialsAndFirstDerivative(n,
            _alpha);
    Pair<DoubleFunction1D, DoubleFunction1D> pair = polynomials[n];
    DoubleFunction1D p1 = polynomials[n - 1].getFirst();
    DoubleFunction1D function = pair.getFirst();
    DoubleFunction1D derivative = pair.getSecond();
    double[] x = new double[n];
    double[] w = new double[n];
    double root = 0;
    for (int i = 0; i < n; i++) {
        root = ROOT_FINDER.getRoot(function, derivative, getInitialRootGuess(root, i, n, x));
        x[i] = root;
        w[i] = -GAMMA_FUNCTION.applyAsDouble(_alpha + n) / CombinatoricsUtils.factorialDouble(n)
                / (derivative.applyAsDouble(root) * p1.applyAsDouble(root));
    }
    return new GaussianQuadratureData(x, w);
}

From source file:com.opengamma.strata.math.impl.integration.GaussJacobiWeightAndAbscissaFunction.java

/**
 * {@inheritDoc}/*from w w w . j  a v  a 2 s.c  o m*/
 */
@Override
public GaussianQuadratureData generate(int n) {
    ArgChecker.isTrue(n > 0, "n > 0");
    Pair<DoubleFunction1D, DoubleFunction1D>[] polynomials = JACOBI.getPolynomialsAndFirstDerivative(n, _alpha,
            _beta);
    Pair<DoubleFunction1D, DoubleFunction1D> pair = polynomials[n];
    DoubleFunction1D previous = polynomials[n - 1].getFirst();
    DoubleFunction1D function = pair.getFirst();
    DoubleFunction1D derivative = pair.getSecond();
    double[] x = new double[n];
    double[] w = new double[n];
    double root = 0;
    for (int i = 0; i < n; i++) {
        double d = 2 * n + _c;
        root = getInitialRootGuess(root, i, n, x);
        root = ROOT_FINDER.getRoot(function, derivative, root);
        x[i] = root;
        w[i] = GAMMA_FUNCTION.applyAsDouble(_alpha + n) * GAMMA_FUNCTION.applyAsDouble(_beta + n)
                / CombinatoricsUtils.factorialDouble(n) / GAMMA_FUNCTION.applyAsDouble(n + _c + 1) * d
                * Math.pow(2, _c) / (derivative.applyAsDouble(root) * previous.applyAsDouble(root));
    }
    return new GaussianQuadratureData(x, w);
}

From source file:edu.asu.ca.kaushik.algorithms.permvector.MTPermutationVector.java

private int permVecLLLBound(int t, int k, int v) {
    double vTotm1 = Math.pow(v, t - 1);
    double vTotm2 = Math.pow(v, t - 2);

    double nume1 = CombinatoricsUtils.factorialDouble(t);
    double nume2 = CombinatoricsUtils.binomialCoefficientDouble((int) vTotm1, t);
    double nume3 = v * (vTotm1 - 1) / (v - 1);
    double nume4 = CombinatoricsUtils.binomialCoefficientDouble((int) vTotm2, t);
    double nume = nume1 * (nume2 - nume3 * nume4);
    double dnom = Math.pow(vTotm1, t);
    double q = 1 - (nume / dnom);

    double d = CombinatoricsUtils.binomialCoefficientDouble(k, t)
            - CombinatoricsUtils.binomialCoefficientDouble(k - t, t) - 1;

    return (int) Math.ceil((1 + Math.log(d + 1)) / Math.log(1 / q));
}

From source file:org.apache.solr.client.solrj.io.eval.FactorialEvaluator.java

@Override
public Object doWork(Object value) {

    if (null == value) {
        return null;
    } else if (value instanceof List) {
        return ((List<?>) value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());
    } else {//w w w  .j  a v a 2s  .com
        return CombinatoricsUtils.factorialDouble(((BigDecimal) value).intValue());
    }
}

From source file:org.orekit.forces.gravity.potential.GravityFieldFactoryTest.java

@Test
public void testNormalizationFirstElements() throws OrekitException {
    int max = 50;
    double[][] factors = GravityFieldFactory.getUnnormalizationFactors(max, max);
    Assert.assertEquals(max + 1, factors.length);
    for (int i = 0; i <= max; ++i) {
        Assert.assertEquals(i + 1, factors[i].length);
        for (int j = 0; j <= i; ++j) {
            double ref = FastMath.sqrt((2 * i + 1) * CombinatoricsUtils.factorialDouble(i - j)
                    / CombinatoricsUtils.factorialDouble(i + j));
            if (j > 0) {
                ref *= FastMath.sqrt(2);
            }/* w w w .  j  a v a2  s  .c om*/
            Assert.assertEquals(ref, factors[i][j], 2.0e-15);
        }
    }
}

From source file:stats.exam.assist.CombinationHelper.java

/**
 *Calculates the combination of n and r/*w ww . j  a  v a 2  s .  c  o  m*/
 * @param n
 * @param r
 * @return the combination of n and r
 */
public static double calculateCombination(int n, int r) {
    double nFactorial = CombinatoricsUtils.factorialDouble(n);
    double rFactorial = CombinatoricsUtils.factorialDouble(r);
    double nMinusRFactorial = CombinatoricsUtils.factorialDouble(n - r);

    return nFactorial / (rFactorial * nMinusRFactorial);
}

From source file:stats.exam.assist.HyperGeomForm.java

private double calculateCombination(int n, int r) {
    double nFactorial = CombinatoricsUtils.factorialDouble(n);
    double nMinusRfactorial = CombinatoricsUtils.factorialDouble(n - r);
    double rFactorial = CombinatoricsUtils.factorial(r);
    return nFactorial / (rFactorial * nMinusRfactorial);
}