Example usage for org.apache.commons.math.util MathUtils pow

List of usage examples for org.apache.commons.math.util MathUtils pow

Introduction

In this page you can find the example usage for org.apache.commons.math.util MathUtils pow.

Prototype

public static BigInteger pow(final BigInteger k, BigInteger e) throws IllegalArgumentException 

Source Link

Document

Raise a BigInteger to a BigInteger power.

Usage

From source file:com.xiantrimble.combinatorics.CombMathUtilsImpl.java

private long p(int k, DistinctM dm) {
    long result = 0;

    // create a stack for the calculation.
    FastList<PartialCombinationCount> stack = stackFactory.object();

    // add the initial partial combination.
    // //from   w  w  w . j a v a  2  s .c o  m
    stack.addFirst(pccFactory.object().init(k, dm, dm.m, 0, 1, 1));

    while (!stack.isEmpty()) {
        // get the next combination to expand.
        PartialCombinationCount pc = stack.removeFirst();

        //System.out.println(pc);

        // Start the expansion of this partial combination.
        // pc.k = the number of elements that still need to be added to the combination.
        // pc.dm = the next distinct m to consider.
        // pc.dmk = the size of the next combination of elements to add.
        // pc.ldm = the number of distinct unused elements to the left of mdi minus the number of distinct used elements at mdi.
        // pc.size = the number of combinations already in the solution (in k - pc.k)
        // pc.pd = the permutation count denominator.

        // get the current distinct m
        DistinctM cdm = pc.dm;
        //System.out.println(cdm);

        // if there could never be an answer, then bail out.
        if (pc.k > (cdm.count + pc.ldm) * pc.dmk + cdm.rn) {
            //System.out.println("OPTIMIZED DUE TO LACK OF ELEMENTS.");
            pccFactory.recycle(pc);
            continue;
        }

        // for each number of pc.dmk sized sets that we can create, add new partial combinations.
        for (int e = 0; e <= pc.dm.count + pc.ldm && e * pc.dmk <= pc.k; e++) {
            int nextK = pc.k - (e * pc.dmk);
            int nextDmk = pc.dmk - 1;
            long nextSize = pc.size * MathUtils.binomialCoefficient(pc.dm.count + pc.ldm, e);
            long nextPd = pc.pd * MathUtils.pow(MathUtils.factorial(pc.dmk), e);

            //System.out.println("e:"+e+", nextK:"+nextK+", nextDmk:"+nextDmk+", nextDmi:"+nextDmi+", nextSize:"+nextSize);

            // if nextK is zero, then this set of combinations is complete.
            if (nextK == 0) {
                result += (nextSize * (MathUtils.factorial(k) / nextPd));
                continue;
            }

            // if nextDmk is zero, then we have run out of items to place into k.
            else if (nextDmk == 0)
                continue;

            // if we are on the last distinct m, or the next distinct m is not big enough, stay at dmi.
            else if (pc.dm.next == null || pc.dm.next.m < nextDmk) {
                int nextLdm = pc.ldm - e;
                stack.addFirst(pccFactory.object().init(nextK, pc.dm, nextDmk, nextLdm, nextSize, nextPd));
            }

            // we need to advance to the next dmi.
            else {
                int nextLdm = pc.ldm - e + cdm.count;
                stack.addFirst(pccFactory.object().init(nextK, pc.dm.next, nextDmk, nextLdm, nextSize, nextPd));
            }
        }
        pccFactory.recycle(pc);
    }

    stackFactory.recycle(stack);

    //System.out.println("Result: "+result);
    return result;
}