Example usage for com.google.common.math BigIntegerMath factorial

List of usage examples for com.google.common.math BigIntegerMath factorial

Introduction

In this page you can find the example usage for com.google.common.math BigIntegerMath factorial.

Prototype

public static BigInteger factorial(int n) 

Source Link

Document

Returns n!

Usage

From source file:edu.isi.karma.modeling.alignment.SemanticModel.java

private List<HashMap<Node, String>> getPossibleNodeIdSets() {

    DirectedWeightedMultigraph<Node, LabeledLink> g = this.getGraph();
    List<HashMap<Node, String>> nodeIdSets = new ArrayList<>();
    if (g == null)
        return nodeIdSets;

    Set<Node> internalNodes = new HashSet<>();
    for (Node n : g.vertexSet())
        if (n instanceof InternalNode)
            internalNodes.add(n);//from ww w.  ja  v a2 s .c om

    Set<Node> columnNodes = new HashSet<>();
    for (Node n : g.vertexSet())
        if (n instanceof ColumnNode)
            columnNodes.add(n);

    Function<Node, String> sameUriNodes = new Function<Node, String>() {
        @Override
        public String apply(final Node n) {
            if (n == null || n.getLabel() == null)
                return null;
            return n.getLabel().getUri();
        }
    };

    Multimap<String, Node> index = Multimaps.index(internalNodes, sameUriNodes);
    int numberOfPossibleSets = 1;
    for (String s : index.keySet()) {
        Collection<Node> nodeGroup = index.get(s);
        if (nodeGroup != null && !nodeGroup.isEmpty()) {
            numberOfPossibleSets *= BigIntegerMath.factorial(nodeGroup.size()).intValue();
        }
    }
    //      System.out.println(numberOfPossibleSets);

    for (int i = 0; i < numberOfPossibleSets; i++) {
        HashMap<Node, String> nodeIds = new HashMap<>();
        NodeIdFactory nodeIdFactory = new NodeIdFactory();

        for (Node n : columnNodes) {
            ColumnNode cn;
            if (this.mappingToSourceColumns != null
                    && (cn = this.mappingToSourceColumns.get((ColumnNode) n)) != null) {
                nodeIds.put(n, cn.getId());
            } else {
                nodeIds.put(n, n.getId());
            }
        }

        for (String s : index.keySet()) {
            Collection<Node> nodeGroup = index.get(s);
            if (nodeGroup != null && nodeGroup.size() == 1) {
                Node n = nodeGroup.iterator().next();
                nodeIds.put(n, nodeIdFactory.getNodeId(n.getLabel().getUri()));
            }
        }

        nodeIdSets.add(nodeIds);
    }

    //      List<Node> nodes = new ArrayList<Node>();
    //      List<Set<String>> idList = new ArrayList<Set<String>>(); 
    //      Set<List<String>> idProductSets = null;
    //      NodeIdFactory nodeIdFactory = new NodeIdFactory();
    //      for (String s : index.keySet()) {
    //         Collection<Node> nodeGroup = index.get(s);
    //         if (nodeGroup == null)
    //            continue;
    //         nodes.addAll(nodeGroup);
    //         Set<String> ids = new TreeSet<String>();
    //         for (Node n : nodeGroup) {
    //            ids.add(nodeIdFactory.getNodeId(n.getLabel().getUri()));
    //         }
    //         idList.add(ids);
    //         
    //      }
    //      if (idList != null)
    //         idProductSets = Sets.cartesianProduct(idList);
    //      
    //      int i = 0;
    //      for (List<String> ids : idProductSets) {
    //         for (Node n : nodes) System.out.println("node: " + n.getLabel().getUri());
    //         for (String id : ids) System.out.println("id: " + id);
    //
    //         for (int j = 0; j < ids.size() && j < nodes.size(); j++)
    //            nodeIdSets.get(i).put(nodes.get(j), ids.get(j));
    //         i++;
    //      }

    int interval = numberOfPossibleSets;
    NodeIdFactory nodeIdFactory = new NodeIdFactory();
    for (String s : index.keySet()) {
        Collection<Node> nodeGroup = index.get(s);
        if (nodeGroup != null && nodeGroup.size() > 1) {
            Set<String> ids = new HashSet<>();
            List<Node> nodes = new ArrayList<>();
            nodes.addAll(nodeGroup);
            for (Node n : nodes)
                ids.add(nodeIdFactory.getNodeId(n.getLabel().getUri()));

            Collection<List<String>> permutations = Collections2.permutations(ids);
            List<List<String>> permList = new ArrayList<>();
            permList.addAll(permutations);

            interval = interval / BigIntegerMath.factorial(nodeGroup.size()).intValue();
            List<String> perm;
            int k = 0, count = 1;
            for (int i = 0; i < nodeIdSets.size(); i++) {
                HashMap<Node, String> nodeIds = nodeIdSets.get(i);
                if (count > interval) {
                    k = ++k % permList.size();
                    count = 1;
                }
                perm = permList.get(k);
                for (int j = 0; j < nodes.size(); j++)
                    nodeIds.put(nodes.get(j), perm.get(j));
                count++;
            }
        }
    }

    return nodeIdSets;
}

From source file:com.kodeblox.BigDecimalFunctions.java

/**
 * Calculates the <code>factorial</code> of a value. The result is rounded
 * according to the passed context <code>mc</code>.
 * /*from ww  w  .  j a  v a  2  s. co  m*/
 * @param value
 *            the number whose factorial is to be found.
 * @param mc
 *            rounding mode and precision for the result of this operation.
 * @return <code>value!</code>
 */
public static BigDecimal factorial(BigDecimal value, MathContext mc) {

    MathContext newMc = new MathContext(mc.getPrecision() + 3);

    // Using Google's Guava library for factorial.
    // Feeling too lazy to write my own :-D.
    return new BigDecimal(BigIntegerMath.factorial(Integer.parseInt(value.toString())).toString(), newMc)
            .round(mc);
}