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

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

Introduction

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

Prototype

public static long factorial(final int n) throws NotPositiveException, MathArithmeticException 

Source Link

Document

Returns n!.

Usage

From source file:eu.socialsensor.main.BenchmarkConfiguration.java

public BenchmarkConfiguration(Configuration appconfig) {
    if (appconfig == null) {
        throw new IllegalArgumentException("appconfig may not be null");
    }//from  w w  w . j  av a2  s .co  m

    Configuration eu = appconfig.subset("eu");
    Configuration socialsensor = eu.subset("socialsensor");

    //metrics
    final Configuration metrics = socialsensor.subset(GraphDatabaseConfiguration.METRICS_NS.getName());

    final Configuration graphite = metrics.subset(GRAPHITE);
    this.graphiteHostname = graphite.getString(GRAPHITE_HOSTNAME, null);
    this.graphiteReportingInterval = graphite.getLong(GraphDatabaseConfiguration.GRAPHITE_INTERVAL.getName(),
            1000 /*default 1sec*/);

    final Configuration csv = metrics.subset(CSV);
    this.csvReportingInterval = metrics.getLong(CSV_INTERVAL, 1000 /*ms*/);
    this.csvDir = csv.containsKey(CSV_DIR)
            ? new File(csv.getString(CSV_DIR, System.getProperty("user.dir") /*default*/))
            : null;

    Configuration dynamodb = socialsensor.subset("dynamodb");
    this.dynamodbWorkerThreads = dynamodb.getInt("workers", 25);
    Configuration credentials = dynamodb.subset(CREDENTIALS);
    this.dynamodbPrecreateTables = dynamodb.getBoolean("precreate-tables", Boolean.FALSE);
    this.dynamodbTps = Math.max(1, dynamodb.getLong(TPS, 750 /*default*/));
    this.dynamodbConsistentRead = dynamodb.containsKey(CONSISTENT_READ) ? dynamodb.getBoolean(CONSISTENT_READ)
            : false;
    this.dynamodbDataModel = dynamodb.containsKey("data-model")
            ? BackendDataModel.valueOf(dynamodb.getString("data-model"))
            : null;
    this.dynamodbCredentialsFqClassName = credentials.containsKey(CLASS_NAME)
            ? credentials.getString(CLASS_NAME)
            : null;
    this.dynamodbCredentialsCtorArguments = credentials.containsKey(CONSTRUCTOR_ARGS)
            ? credentials.getString(CONSTRUCTOR_ARGS)
            : null;
    this.dynamodbEndpoint = dynamodb.containsKey(ENDPOINT) ? dynamodb.getString(ENDPOINT) : null;
    this.dynamodbTablePrefix = dynamodb.containsKey(TABLE_PREFIX) ? dynamodb.getString(TABLE_PREFIX)
            : Constants.DYNAMODB_TABLE_PREFIX.getDefaultValue();

    Configuration orient = socialsensor.subset("orient");
    orientLightweightEdges = orient.containsKey(LIGHTWEIGHT_EDGES) ? orient.getBoolean(LIGHTWEIGHT_EDGES)
            : null;

    Configuration sparksee = socialsensor.subset("sparksee");
    sparkseeLicenseKey = sparksee.containsKey(LICENSE_KEY) ? sparksee.getString(LICENSE_KEY) : null;

    Configuration titan = socialsensor.subset(TITAN); //TODO(amcp) move dynamodb ns into titan
    bufferSize = titan.getInt(BUFFER_SIZE, GraphDatabaseConfiguration.BUFFER_SIZE.getDefaultValue());
    blocksize = titan.getInt(IDS_BLOCKSIZE, GraphDatabaseConfiguration.IDS_BLOCK_SIZE.getDefaultValue());
    pageSize = titan.getInt(PAGE_SIZE, GraphDatabaseConfiguration.PAGE_SIZE.getDefaultValue());

    // database storage directory
    if (!socialsensor.containsKey(DATABASE_STORAGE_DIRECTORY)) {
        throw new IllegalArgumentException("configuration must specify database-storage-directory");
    }
    dbStorageDirectory = new File(socialsensor.getString(DATABASE_STORAGE_DIRECTORY));
    dataset = validateReadableFile(socialsensor.getString(DATASET), DATASET);

    // load the dataset
    DatasetFactory.getInstance().getDataset(dataset);

    if (!socialsensor.containsKey(PERMUTE_BENCHMARKS)) {
        throw new IllegalArgumentException("configuration must set permute-benchmarks to true or false");
    }
    permuteBenchmarks = socialsensor.getBoolean(PERMUTE_BENCHMARKS);

    List<?> benchmarkList = socialsensor.getList("benchmarks");
    benchmarkTypes = new ArrayList<BenchmarkType>();
    for (Object str : benchmarkList) {
        benchmarkTypes.add(BenchmarkType.valueOf(str.toString()));
    }

    selectedDatabases = new TreeSet<GraphDatabaseType>();
    for (Object database : socialsensor.getList("databases")) {
        if (!GraphDatabaseType.STRING_REP_MAP.keySet().contains(database.toString())) {
            throw new IllegalArgumentException(
                    String.format("selected database %s not supported", database.toString()));
        }
        selectedDatabases.add(GraphDatabaseType.STRING_REP_MAP.get(database));
    }
    scenarios = permuteBenchmarks ? Ints.checkedCast(CombinatoricsUtils.factorial(selectedDatabases.size()))
            : 1;

    resultsPath = new File(System.getProperty("user.dir"), socialsensor.getString("results-path"));
    if (!resultsPath.exists() && !resultsPath.mkdirs()) {
        throw new IllegalArgumentException("unable to create results directory");
    }
    if (!resultsPath.canWrite()) {
        throw new IllegalArgumentException("unable to write to results directory");
    }

    randomNodes = socialsensor.getInteger(RANDOM_NODES, new Integer(100));

    if (this.benchmarkTypes.contains(BenchmarkType.CLUSTERING)) {
        if (!socialsensor.containsKey(NODES_COUNT)) {
            throw new IllegalArgumentException("the CW benchmark requires nodes-count integer in config");
        }
        nodesCount = socialsensor.getInt(NODES_COUNT);

        if (!socialsensor.containsKey(RANDOMIZE_CLUSTERING)) {
            throw new IllegalArgumentException("the CW benchmark requires randomize-clustering bool in config");
        }
        randomizedClustering = socialsensor.getBoolean(RANDOMIZE_CLUSTERING);

        if (!socialsensor.containsKey(ACTUAL_COMMUNITIES)) {
            throw new IllegalArgumentException("the CW benchmark requires a file with actual communities");
        }
        actualCommunities = validateReadableFile(socialsensor.getString(ACTUAL_COMMUNITIES),
                ACTUAL_COMMUNITIES);

        final boolean notGenerating = socialsensor.containsKey(CACHE_VALUES);
        if (notGenerating) {
            List<?> objects = socialsensor.getList(CACHE_VALUES);
            cacheValues = new ArrayList<Integer>(objects.size());
            cacheValuesCount = null;
            cacheIncrementFactor = null;
            for (Object o : objects) {
                cacheValues.add(Integer.valueOf(o.toString()));
            }
        } else if (socialsensor.containsKey(CACHE_VALUES_COUNT)
                && socialsensor.containsKey(CACHE_INCREMENT_FACTOR)) {
            cacheValues = null;
            // generate the cache values with parameters
            if (!socialsensor.containsKey(CACHE_VALUES_COUNT)) {
                throw new IllegalArgumentException(
                        "the CW benchmark requires cache-values-count int in config when cache-values not specified");
            }
            cacheValuesCount = socialsensor.getInt(CACHE_VALUES_COUNT);

            if (!socialsensor.containsKey(CACHE_INCREMENT_FACTOR)) {
                throw new IllegalArgumentException(
                        "the CW benchmark requires cache-increment-factor int in config when cache-values not specified");
            }
            cacheIncrementFactor = socialsensor.getDouble(CACHE_INCREMENT_FACTOR);
        } else {
            throw new IllegalArgumentException(
                    "when doing CW benchmark, must provide cache-values or parameters to generate them");
        }
    } else {
        randomizedClustering = null;
        nodesCount = null;
        cacheValuesCount = null;
        cacheIncrementFactor = null;
        cacheValues = null;
        actualCommunities = null;
    }
}

From source file:org.cloudsimplus.sla.PoissonProcess.java

/**
 *
 * @return the probability to arrive {@link #getK() K} events in the current time.
 * @see <a href="https://en.wikipedia.org/wiki/Poisson_distribution">Poisson distribution</a>
 *//* w w w. j a v a 2  s . co m*/
public double probabilityToArriveNextKEvents() {
    //computes the Probability Mass Function (PMF) of the Poisson distribution
    return (Math.pow(getLambda(), k) * Math.exp(-getLambda())) / CombinatoricsUtils.factorial(k);
}

From source file:org.orekit.propagation.analytical.tle.TLETest.java

final double taylorConvert(final double m, final int n) {
    // convert one term of TLE mean motion Taylor series
    return m * 2 * FastMath.PI * CombinatoricsUtils.factorial(n) / FastMath.pow(Constants.JULIAN_DAY, n);
}

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);
}

From source file:vn.edu.vnu.uet.nlp.smt.ibm.IBMModel3.java

/**
 * This function returns the probability given an alignment. The phi
 * variable represents the fertility according to the current alignment,
 * which records how many output words are generated by each input word.
 *//* w w  w. j a v  a 2s  .c  om*/
double logProbability(ViterbiAlignment align, SentencePair p) {
    int[] a = align.getA();
    int[] phi = align.getPhi();

    int le = p.getE().length();
    int lf = p.getF().length();

    if (le - 2 * phi[0] <= 0) {
        return 0.0;
    }

    double p1 = 1 - p0;

    double total = 0.0;

    // Compute the NULL insertion
    total += Math.log(Math.pow(p1, phi[0]) * Math.pow(p0, le - 2 * phi[0]));

    // Compute the combination (le - fert[0]) choose fert[0]
    for (int i = 1; i <= phi[0]; i++) {
        total += Math.log((le - phi[0] - i + 1) / i);
    }

    // Compute fertilities term
    for (int i = 0; i <= lf; i++) {
        int f = p.getF().get(i);
        try {
            total += Math.log(CombinatoricsUtils.factorial(phi[i]) * n.get(getFertWord(phi[i], f)));
        } catch (MathArithmeticException e) {
            if (phi[i] > 20) {
                total += Math.log(n.get(getFertWord(phi[i], f)));
                total += Math.log(CombinatoricsUtils.factorial(20));

                for (int tmp = 21; tmp <= phi[i]; tmp++) {
                    total += Math.log(tmp);
                }
            } else {
                throw new MathArithmeticException();
            }
        }
    }

    // Multiply the lexical and distortion probabilities
    for (int j = 1; j <= le; j++) {
        int i = a[j];
        total += Math.log(t.get(p.getWordPair(j, i)));
        total += Math.log(d[j][i][le][lf]);
    }

    return Math.exp(total);
}

From source file:vn.edu.vnu.uet.nlp.smt.ibm.IBMModel3.java

/**
 * This function returns the probability given an alignment. The phi
 * variable represents the fertility according to the current alignment,
 * which records how many output words are generated by each input word.
 *//*from w  w w  .j a  va 2 s .  c  o m*/
private double normalProbability(ViterbiAlignment align, SentencePair p) {
    int[] a = align.getA();
    int[] phi = align.getPhi();

    int le = p.getE().length();
    int lf = p.getF().length();

    if (le - 2 * phi[0] <= 0) {
        return 0.0;
    }

    double p1 = 1 - p0;

    double total = 1.0;

    // Compute the NULL insertion
    total *= (Math.pow(p1, phi[0]) * Math.pow(p0, le - 2 * phi[0]));

    // Compute the combination (le - fert[0]) choose fert[0]
    for (int i = 1; i <= phi[0]; i++) {
        total *= ((le - phi[0] - i + 1) / i);
        if (total == 0.0) {
            return 0.0;
        }
    }

    // Compute fertilities term
    for (int i = 0; i <= lf; i++) {
        int f = p.getF().get(i);
        try {
            total *= (CombinatoricsUtils.factorial(phi[i]) * n.get(getFertWord(phi[i], f)));
        } catch (MathArithmeticException e) {
            if (phi[i] > 20) {
                total *= (n.get(getFertWord(phi[i], f)));
                total *= (CombinatoricsUtils.factorial(20));

                for (int tmp = 21; tmp <= phi[i]; tmp++) {
                    total *= (tmp);
                }
            } else {
                throw new MathArithmeticException();
            }
        }
    }

    // Multiply the lexical and distortion probabilities
    for (int j = 1; j <= le; j++) {
        int i = a[j];
        total *= (t.get(p.getWordPair(j, i)));
        total *= (d[j][i][le][lf]);
    }

    return total;
}