Example usage for org.apache.commons.math3.util FastMath log

List of usage examples for org.apache.commons.math3.util FastMath log

Introduction

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

Prototype

public static double log(double base, double x) 

Source Link

Document

Computes the <a href="http://mathworld.wolfram.com/Logarithm.html"> logarithm</a> in a given base.

Usage

From source file:Math.MathCalc.java

/**
 * Return Logarithm of the given number in the given base
 *
 * @param number the number to calculate to Logarithm on
 * @param base base of the Logarithm//from  w w  w.  j a va2 s .  c  o  m
 * @return Logarithm of the given number in the given base
 */
public static double Log(double number, double base) {
    return FastMath.log(base, number);
}

From source file:edu.oregonstate.eecs.mcplan.ml.ClusterContingencyTable.java

public double entropyU() {
    double h = 0.0;
    for (int i = 0; i < R; ++i) {
        final double p = a[i] / ((double) N);
        if (p == 0.0) {
            continue;
        }/*w  ww  .  j a  va2 s. c om*/
        h += -p * FastMath.log(2, p);
    }
    return h;
}

From source file:edu.oregonstate.eecs.mcplan.ml.ClusterContingencyTable.java

public double entropyV() {
    double h = 0.0;
    for (int j = 0; j < C; ++j) {
        final double p = b[j] / ((double) N);
        if (p == 0.0) {
            continue;
        }//from  w w w. ja  v  a2s.com
        h += -p * FastMath.log(2, p);
    }
    return h;
}

From source file:edu.oregonstate.eecs.mcplan.ml.ClusterContingencyTable.java

public double mutualInformation() {
    final double Nd = N;
    double mi = 0.0;
    for (int i = 0; i < R; ++i) {
        for (int j = 0; j < C; ++j) {
            final double p = n[i][j] / Nd;
            if (p == 0) {
                continue;
            }//from www  .jav a  2s  .  com
            final double q = p / ((a[i] * b[j]) / (Nd * Nd));
            mi += p * FastMath.log(2, q);
        }
    }
    return mi;
}

From source file:edu.oregonstate.eecs.mcplan.ml.ClusterContingencyTable.java

public double expectedMutualInformation() {
    double emi = 0.0;
    for (int i = 0; i < R; ++i) {
        for (int j = 0; j < C; ++j) {
            // We take max( _, 1 ) instead of max( _, 0 ) as in the paper
            // because when nij is 0, the product is 0, but log( 0 )
            // causes NaN.
            final int start = Math.max(a[i] + b[j] - N, 1);
            final int end = Math.min(a[i], b[j]);
            for (int nij = start; nij <= end; ++nij) {
                final double p = nij / ((double) N);
                final double L = FastMath.log(2, N * nij / ((double) a[i] * b[j]));
                final double logNum = ArithmeticUtils.factorialLog(a[i]) + ArithmeticUtils.factorialLog(b[j])
                        + ArithmeticUtils.factorialLog(N - a[i]) + ArithmeticUtils.factorialLog(N - b[j]);
                final double logDenom = ArithmeticUtils.factorialLog(N) + ArithmeticUtils.factorialLog(nij)
                        + ArithmeticUtils.factorialLog(a[i] - nij) + ArithmeticUtils.factorialLog(b[j] - nij)
                        + ArithmeticUtils.factorialLog(N - a[i] - b[j] + nij);
                final double all = p * L * FastMath.exp(logNum - logDenom);
                emi += all;//from   w  ww .  ja v  a 2s.  c om
            }
        }
    }
    return emi;
}

From source file:com.clust4j.algo.NearestNeighborHeapSearch.java

/**
 * Constructor with logger object/*from ww  w  .  j  a  v  a  2  s  .co m*/
 * @param X
 * @param leaf_size
 * @param dist
 * @param logger
 */
protected NearestNeighborHeapSearch(final double[][] X, int leaf_size, DistanceMetric dist, Loggable logger) {
    this.data_arr = MatUtils.copy(X);
    this.leaf_size = leaf_size;
    this.logger = logger;

    if (leaf_size < 1)
        throw new IllegalArgumentException("illegal leaf size: " + leaf_size);

    if (!checkValidDistMet(dist)) {
        if (null != logger)
            logger.warn(dist + " is not valid for " + this.getClass() + ". Reverting to " + DEF_DIST);
        this.dist_metric = DEF_DIST;
    } else {
        this.dist_metric = dist;
    }

    // Whether the algorithm is using the infinity distance (Chebyshev)
    this.infinity_dist = this.dist_metric.getP() == Double.POSITIVE_INFINITY
            || Double.isInfinite(this.dist_metric.getP());

    // determine number of levels in the tree, and from this
    // the number of nodes in the tree.  This results in leaf nodes
    // with numbers of points between leaf_size and 2 * leaf_size
    MatUtils.checkDims(this.data_arr);
    N_SAMPLES = data_arr.length;
    N_FEATURES = X[0].length;

    /*
    // Should round up or always take floor function?...
    double nlev = FastMath.log(2, FastMath.max(1, (N_SAMPLES-1)/leaf_size)) + 1;
    this.n_levels = (int)FastMath.round(nlev);
    this.n_nodes = (int)(FastMath.pow(2, nlev) - 1);
    */

    this.n_levels = (int) (FastMath.log(2, FastMath.max(1, (N_SAMPLES - 1) / leaf_size)) + 1);
    this.n_nodes = (int) (FastMath.pow(2, n_levels) - 1);

    // allocate arrays for storage
    this.idx_array = VecUtils.arange(N_SAMPLES);

    // Add new NodeData objs to node_data arr
    this.node_data = new NodeData[n_nodes];
    for (int i = 0; i < node_data.length; i++)
        node_data[i] = new NodeData();

    // allocate tree specific data
    allocateData(this, n_nodes, N_FEATURES);
    recursiveBuild(0, 0, N_SAMPLES);
}

From source file:com.duy.pascal.interperter.libraries.math.MathLib.java

@PascalMethod(description = "")
public double Logn(double x, double n) {
    return FastMath.log(x, n);
}

From source file:com.duy.pascal.interperter.libraries.math.MathLib.java

@PascalMethod(description = "")
public double Log2(double x) {
    return FastMath.log(x, 2);
}

From source file:com.wwidesigner.math.DIRECTOptimizer.java

/**
 * Return the threshold diameter required for convergence, for a given
 * relative convergence threshold.// w w w .j a v a  2s. co  m
 */
protected double thresholdDiameter(double convergenceThreshold, int dimension) {
    if (convergenceThreshold <= 0.0) {
        return 0.0;
    }
    // Round the threshold down to the next smaller power of 1/3.
    // Rectangle is small when *all* sides are this size.
    double iterations = FastMath.ceil(FastMath.log(THIRD, convergenceThreshold));
    double threshold = FastMath.pow(THIRD, iterations);
    return 0.5 * FastMath.sqrt(dimension) * threshold;
}

From source file:org.apache.mahout.classifier.df.BreimanExample.java

@Override
public int run(String[] args) throws IOException {

    DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
    ArgumentBuilder abuilder = new ArgumentBuilder();
    GroupBuilder gbuilder = new GroupBuilder();

    Option dataOpt = obuilder.withLongName("data").withShortName("d").withRequired(true)
            .withArgument(abuilder.withName("path").withMinimum(1).withMaximum(1).create())
            .withDescription("Data path").create();

    Option datasetOpt = obuilder.withLongName("dataset").withShortName("ds").withRequired(true)
            .withArgument(abuilder.withName("dataset").withMinimum(1).withMaximum(1).create())
            .withDescription("Dataset path").create();

    Option nbtreesOpt = obuilder.withLongName("nbtrees").withShortName("t").withRequired(true)
            .withArgument(abuilder.withName("nbtrees").withMinimum(1).withMaximum(1).create())
            .withDescription("Number of trees to grow, each iteration").create();

    Option nbItersOpt = obuilder.withLongName("iterations").withShortName("i").withRequired(true)
            .withArgument(abuilder.withName("numIterations").withMinimum(1).withMaximum(1).create())
            .withDescription("Number of times to repeat the test").create();

    Option helpOpt = obuilder.withLongName("help").withDescription("Print out help").withShortName("h")
            .create();//from ww w . j  ava  2s. c  o m

    Group group = gbuilder.withName("Options").withOption(dataOpt).withOption(datasetOpt).withOption(nbItersOpt)
            .withOption(nbtreesOpt).withOption(helpOpt).create();

    Path dataPath;
    Path datasetPath;
    int nbTrees;
    int nbIterations;

    try {
        Parser parser = new Parser();
        parser.setGroup(group);
        CommandLine cmdLine = parser.parse(args);

        if (cmdLine.hasOption("help")) {
            CommandLineUtil.printHelp(group);
            return -1;
        }

        String dataName = cmdLine.getValue(dataOpt).toString();
        String datasetName = cmdLine.getValue(datasetOpt).toString();
        nbTrees = Integer.parseInt(cmdLine.getValue(nbtreesOpt).toString());
        nbIterations = Integer.parseInt(cmdLine.getValue(nbItersOpt).toString());

        dataPath = new Path(dataName);
        datasetPath = new Path(datasetName);
    } catch (OptionException e) {
        log.error("Error while parsing options", e);
        CommandLineUtil.printHelp(group);
        return -1;
    }

    // load the data
    FileSystem fs = dataPath.getFileSystem(new Configuration());
    Dataset dataset = Dataset.load(getConf(), datasetPath);
    Data data = DataLoader.loadData(dataset, fs, dataPath);

    // take m to be the first integer less than log2(M) + 1, where M is the
    // number of inputs
    int m = (int) Math.floor(FastMath.log(2.0, data.getDataset().nbAttributes()) + 1);

    Random rng = RandomUtils.getRandom();
    for (int iteration = 0; iteration < nbIterations; iteration++) {
        log.info("Iteration {}", iteration);
        runIteration(rng, data, m, nbTrees);
    }

    log.info("********************************************");
    log.info("Random Input Test Error : {}", sumTestErrM / nbIterations);
    log.info("Single Input Test Error : {}", sumTestErrOne / nbIterations);
    log.info("Mean Random Input Time : {}", DFUtils.elapsedTime(sumTimeM / nbIterations));
    log.info("Mean Single Input Time : {}", DFUtils.elapsedTime(sumTimeOne / nbIterations));
    log.info("Mean Random Input Num Nodes : {}", numNodesM / nbIterations);
    log.info("Mean Single Input Num Nodes : {}", numNodesOne / nbIterations);

    return 0;
}