Example usage for java.lang Math log

List of usage examples for java.lang Math log

Introduction

In this page you can find the example usage for java.lang Math log.

Prototype

@HotSpotIntrinsicCandidate
public static double log(double a) 

Source Link

Document

Returns the natural logarithm (base e) of a double value.

Usage

From source file:beast.evolution.speciation.BirthDeathGernhard08Model.java

/**
 * scaling coefficient of tree */*www. j  av a 2 s  .c  om*/
 */
@Override
protected double logCoeff(final int taxonCount) {
    switch (type) {
    case UNSCALED:
        break;
    case TIMESONLY:
        return logGamma(taxonCount + 1);
    case ORIENTED:
        return Math.log(taxonCount);
    case LABELED: {
        final double two2nm1 = (taxonCount - 1) * Math.log(2.0);
        if (conditionalOnRoot) {
            return two2nm1 - Math.log(taxonCount - 1) - logGamma(taxonCount + 1);
        } else if (conditionalOnOrigin) {
            return two2nm1 - logGamma(taxonCount + 1);
        } else {
            return two2nm1 - logGamma(taxonCount);
        }
    }
    }
    return 0.0;
}

From source file:org.fhcrc.cpl.viewer.quant.gui.PanelWithLogRatioHistAndFields.java

/**
 * Set the log ratios, build the histogram and display, removing the old one if there was one.  Nothing gets
 * cleaned up related to the old chart; it'll just hang around intil GC
 * todo: do I need to dispose of the old chart in a better way?
 * @param logRatios//  w ww .  j ava 2  s .c  o m
 */
public void setLogRatios(List<Float> logRatios) {
    _log.debug("setLogRatios 1");
    if (logRatioHistogram != null) {
        remove(logRatioHistogram);
    }
    this.logRatios = logRatios;

    float minLogRatioBound = (float) Math.log(minRatioBound);
    float maxLogRatioBound = (float) Math.log(maxRatioBound);

    _log.debug("setLogRatios 2");

    List<Float> boundedLogRatios = new ArrayList<Float>(logRatios.size());
    for (float logRatio : logRatios)
        boundedLogRatios.add(Math.min(maxLogRatioBound, Math.max(minLogRatioBound, logRatio)));

    logRatioHistogram = new PanelWithHistogram(boundedLogRatios, "Log Ratios", 200);
    Dimension histDimension = new Dimension(300, 80);
    if (!Float.isNaN(domainCrosshairValue)) {
        logRatioHistogram.getChart().getXYPlot().setDomainCrosshairValue(domainCrosshairValue, true);
        logRatioHistogram.getChart().getXYPlot().setDomainCrosshairVisible(true);
    }

    _log.debug("setLogRatios 1");

    logRatioHistogram.setPreferredSize(histDimension);
    logRatioHistogram.getChart().removeLegend();
    logRatioHistogram.getChart().getXYPlot().getDomainAxis().setLowerBound(minLogRatioBound);
    logRatioHistogram.getChart().getXYPlot().getDomainAxis().setUpperBound(maxLogRatioBound);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;
    gbc.anchor = GridBagConstraints.LINE_START;
    gbc.insets = new Insets(0, 0, 0, 0);
    gbc.weighty = 10;
    gbc.weightx = 1;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    add(logRatioHistogram, gbc);

    histMouseListener = new LogRatioHistMouseListener(logRatioHistogram);
    ChartPanel histChartPanel = logRatioHistogram.getChartPanel();
    histChartPanel.setMouseZoomable(false);
    logRatioHistogram.getChartPanel().addMouseListener(histMouseListener);
    logRatioHistogram.getChartPanel().addMouseMotionListener(histMouseListener);
    histMouseListener.addRangeUpdateListener(new LogRatioHistogramListener(this));

    logRatioHistogram.updateUI();
    //if there are specified minHigh and maxHigh values, and they're valid, draw the initially selected region
    if (minHighRatio > maxLowRatio && minHighRatio > 0 && maxLowRatio > 0)
        updateSelectedRegion();

    //remove axes from chart
    ((XYPlot) logRatioHistogram.getPlot()).getRangeAxis().setVisible(false);
    ((XYPlot) logRatioHistogram.getPlot()).getDomainAxis().setVisible(false);

    logRatioHistogram.updateUI();
}

From source file:de.andreasschoknecht.LS3.Query.java

/**
 * Calculate the logarithm of a number with base 2 (natural logarithm).
 *
 * @param number The number of which the logarithm shall be calculated
 * @return The logarithm of number with base 2
 */// ww w  .  j a v a  2  s.  com
private static double calcLogBase2(double number) {
    return Math.log(number) / Math.log(2);
}

From source file:beast.evolution.operators.GMRFMultilocusSkyrideBlockUpdateOperator.java

private double getNewPrecision(double currentValue, double scaleFactor) {
    double length = scaleFactor - 1 / scaleFactor;
    double returnValue;

    if (scaleFactor == 1)
        return currentValue;
    if (Randomizer.nextDouble() < length / (length + 2 * Math.log(scaleFactor))) {
        returnValue = (1 / scaleFactor + length * Randomizer.nextDouble()) * currentValue;
    } else {//www  .  j  av a  2s  . c om
        returnValue = Math.pow(scaleFactor, 2.0 * Randomizer.nextDouble() - 1) * currentValue;
    }

    return returnValue;
}

From source file:com.bc.jexp.impl.DefaultNamespace.java

private void registerDefaultFunctions() {
    registerFunction(new AbstractFunction.D("sin", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return FastMath.sin(args[0].evalD(env));
        }/*from ww  w. j a va2s .c om*/
    });
    registerFunction(new AbstractFunction.D("cos", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return FastMath.cos(args[0].evalD(env));
        }
    });
    registerFunction(new AbstractFunction.D("tan", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return FastMath.tan(args[0].evalD(env));
        }
    });
    registerFunction(new AbstractFunction.D("asin", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return FastMath.asin(args[0].evalD(env));
        }
    });
    registerFunction(new AbstractFunction.D("acos", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return FastMath.acos(args[0].evalD(env));
        }
    });
    registerFunction(new AbstractFunction.D("atan", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return FastMath.atan(args[0].evalD(env));
        }
    });
    registerFunction(new AbstractFunction.D("atan2", 2) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return Math.atan2(args[0].evalD(env), args[1].evalD(env));
        }
    });
    registerFunction(new AbstractFunction.D("log", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return Math.log(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("log10", 1) {
        public double evalD(EvalEnv env, Term[] args) throws EvalException {
            return Math.log10(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("exp", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return FastMath.exp(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("exp10", 1) {

        public double evalD(EvalEnv env, Term[] args) throws EvalException {
            return FastMath.pow(10.0, args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("sqr", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            double v = args[0].evalD(env);
            return v * v;
        }
    });

    registerFunction(new AbstractFunction.D("sqrt", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return Math.sqrt(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("pow", 2) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return FastMath.pow(args[0].evalD(env), args[1].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.I("min", 2) {

        public int evalI(final EvalEnv env, final Term[] args) {
            return Math.min(args[0].evalI(env), args[1].evalI(env));
        }
    });

    registerFunction(new AbstractFunction.D("min", 2) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return Math.min(args[0].evalD(env), args[1].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.I("max", 2) {

        public int evalI(final EvalEnv env, final Term[] args) {
            return Math.max(args[0].evalI(env), args[1].evalI(env));
        }
    });

    registerFunction(new AbstractFunction.D("max", 2) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return Math.max(args[0].evalD(env), args[1].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("floor", 1) {
        public double evalD(EvalEnv env, Term[] args) throws EvalException {
            return Math.floor(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("round", 1) {
        public double evalD(EvalEnv env, Term[] args) throws EvalException {
            return Math.round(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("ceil", 1) {
        public double evalD(EvalEnv env, Term[] args) throws EvalException {
            return Math.ceil(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("rint", 1) {
        public double evalD(EvalEnv env, Term[] args) throws EvalException {
            return Math.rint(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.I("sign", 1) {

        public int evalI(final EvalEnv env, final Term[] args) {
            return ExtMath.sign(args[0].evalI(env));
        }
    });

    registerFunction(new AbstractFunction.D("sign", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return ExtMath.sign(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.I("abs", 1) {

        public int evalI(final EvalEnv env, final Term[] args) {
            return Math.abs(args[0].evalI(env));
        }
    });

    registerFunction(new AbstractFunction.D("abs", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return Math.abs(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("deg", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return Math.toDegrees(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("rad", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return Math.toRadians(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("ampl", 2) {

        public double evalD(final EvalEnv env, final Term[] args) {
            final double a = args[0].evalD(env);
            final double b = args[1].evalD(env);
            return Math.sqrt(a * a + b * b);
        }
    });

    registerFunction(new AbstractFunction.D("phase", 2) {

        public double evalD(final EvalEnv env, final Term[] args) {
            final double a = args[0].evalD(env);
            final double b = args[1].evalD(env);
            return Math.atan2(b, a);
        }
    });

    registerFunction(new AbstractFunction.B("feq", 2) {
        public boolean evalB(EvalEnv env, Term[] args) throws EvalException {
            final double x1 = args[0].evalD(env);
            final double x2 = args[1].evalD(env);
            return ExtMath.feq(x1, x2, EPS);
        }
    });

    registerFunction(new AbstractFunction.B("feq", 3) {
        public boolean evalB(EvalEnv env, Term[] args) throws EvalException {
            final double x1 = args[0].evalD(env);
            final double x2 = args[1].evalD(env);
            final double eps = args[2].evalD(env);
            return ExtMath.feq(x1, x2, eps);
        }
    });

    registerFunction(new AbstractFunction.B("fneq", 2) {
        public boolean evalB(EvalEnv env, Term[] args) throws EvalException {
            final double x1 = args[0].evalD(env);
            final double x2 = args[1].evalD(env);
            return ExtMath.fneq(x1, x2, EPS);
        }
    });

    registerFunction(new AbstractFunction.B("fneq", 3) {
        public boolean evalB(EvalEnv env, Term[] args) throws EvalException {
            final double x1 = args[0].evalD(env);
            final double x2 = args[1].evalD(env);
            final double eps = args[2].evalD(env);
            return ExtMath.fneq(x1, x2, eps);
        }
    });

    registerFunction(new AbstractFunction.B("inf", 1) {
        public boolean evalB(EvalEnv env, Term[] args) throws EvalException {
            return Double.isInfinite(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.B("nan", 1) {
        public boolean evalB(EvalEnv env, Term[] args) throws EvalException {
            return Double.isNaN(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("distance", -1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            double sqrSum = 0.0;
            final int n = args.length / 2;
            for (int i = 0; i < n; i++) {
                final double v = args[i + n].evalD(env) - args[i].evalD(env);
                sqrSum += v * v;
            }
            return Math.sqrt(sqrSum);
        }
    });

    registerFunction(new AbstractFunction.D("distance_deriv", -1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            double sqrSum = 0.0;
            final int n = args.length / 2;
            for (int i = 0; i < n - 1; i++) {
                final double v1 = args[i + 1].evalD(env) - args[i].evalD(env);
                final double v2 = args[i + n + 1].evalD(env) - args[i + n].evalD(env);
                sqrSum += (v1 - v2) * (v1 - v2);
            }
            return Math.sqrt(sqrSum);
        }
    });

    registerFunction(new AbstractFunction.D("distance_integ", -1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            double sqrSum = 0.0;
            double v1Sum = 0.0;
            double v2Sum = 0.0;
            final int n = args.length / 2;
            for (int i = 0; i < n; i++) {
                v1Sum += args[i].evalD(env);
                v2Sum += args[i + n].evalD(env);
                sqrSum += (v2Sum - v1Sum) * (v2Sum - v1Sum);
            }
            return Math.sqrt(sqrSum);
        }
    });

    registerFunction(new AbstractFunction.B("inrange", -1) {

        public boolean evalB(final EvalEnv env, final Term[] args) {
            final int n1 = args.length / 3;
            final int n2 = n1 + args.length / 3;
            for (int i = 0; i < n1; i++) {
                final double v = args[i].evalD(env);
                final double v1 = args[i + n1].evalD(env);
                final double v2 = args[i + n2].evalD(env);
                if (v < v1 || v > v2) {
                    return false;
                }
            }
            return true;
        }
    });

    registerFunction(new AbstractFunction.B("inrange_deriv", -1) {

        public boolean evalB(final EvalEnv env, final Term[] args) {
            final int n1 = args.length / 3;
            final int n2 = 2 * n1;
            for (int i = 0; i < n1 - 1; i++) {
                final double v = args[i + 1].evalD(env) - args[i].evalD(env);
                final double v1 = args[i + n1 + 1].evalD(env) - args[i + n1].evalD(env);
                final double v2 = args[i + n2 + 1].evalD(env) - args[i + n2].evalD(env);
                if (v < v1 || v > v2) {
                    return false;
                }
            }
            return true;
        }
    });

    registerFunction(new AbstractFunction.B("inrange_integ", -1) {

        public boolean evalB(final EvalEnv env, final Term[] args) {
            final int n1 = args.length / 3;
            final int n2 = 2 * n1;
            double vSum = 0.0;
            double v1Sum = 0.0;
            double v2Sum = 0.0;
            for (int i = 0; i < n1; i++) {
                vSum += args[i].evalD(env);
                v1Sum += args[i + n1].evalD(env);
                v2Sum += args[i + n2].evalD(env);
                if (vSum < v1Sum || vSum > v2Sum) {
                    return false;
                }
            }
            return true;
        }
    });

}

From source file:Tsne.java

/**
 * Convert data to probability//from w w  w  .  java 2  s  .  c o  m
 * co-occurrences (aka calculating the kernel)
 * @param d the data to convert
 * @param u the perplexity of the model
 * @return the probabilities of co-occurrence
 */
public INDArray computeGaussianPerplexity(final INDArray d, double u) {
    int n = d.rows();
    final INDArray p = zeros(n, n);
    final INDArray beta = ones(n, 1);
    final double logU = Math.log(u);

    log.info("Calculating probabilities of data similarities..");
    for (int i = 0; i < n; i++) {
        if (i % 500 == 0 && i > 0)
            log.info("Handled " + i + " records");

        double betaMin = Double.NEGATIVE_INFINITY;
        double betaMax = Double.POSITIVE_INFINITY;
        NDArrayIndex[] range = new NDArrayIndex[] {
                NDArrayIndex.concat(NDArrayIndex.interval(0, i), NDArrayIndex.interval(i + 1, d.columns())) };

        INDArray row = d.slice(i).get(range);
        Pair<INDArray, INDArray> pair = hBeta(row, beta.getDouble(i));
        INDArray hDiff = pair.getFirst().sub(logU);
        int tries = 0;

        //while hdiff > tolerance
        while (BooleanIndexing.and(abs(hDiff), Conditions.greaterThan(tolerance)) && tries < 50) {
            //if hdiff > 0
            if (BooleanIndexing.and(hDiff, Conditions.greaterThan(0))) {
                if (Double.isInfinite(betaMax))
                    beta.putScalar(i, beta.getDouble(i) * 2.0);
                else
                    beta.putScalar(i, (beta.getDouble(i) + betaMax) / 2.0);
                betaMin = beta.getDouble(i);
            } else {
                if (Double.isInfinite(betaMin))
                    beta.putScalar(i, beta.getDouble(i) / 2.0);
                else
                    beta.putScalar(i, (beta.getDouble(i) + betaMin) / 2.0);
                betaMax = beta.getDouble(i);
            }

            pair = hBeta(row, beta.getDouble(i));
            hDiff = pair.getFirst().subi(logU);
            tries++;
        }

        p.slice(i).put(range, pair.getSecond());

    }

    //dont need data in memory after
    log.info("Mean value of sigma " + sqrt(beta.rdiv(1)).mean(Integer.MAX_VALUE));
    BooleanIndexing.applyWhere(p, Conditions.isNan(), new Value(realMin));

    //set 0 along the diagonal
    INDArray permute = p.transpose();

    INDArray pOut = p.add(permute);

    pOut.divi(pOut.sum(Integer.MAX_VALUE));
    BooleanIndexing.applyWhere(pOut, Conditions.lessThan(Nd4j.EPS_THRESHOLD), new Value(Nd4j.EPS_THRESHOLD));
    //ensure no nans
    return pOut;

}

From source file:com.opengamma.analytics.math.interpolation.ExponentialExtrapolator1D.java

private double[] getRightSensitivities(final Interpolator1DDataBundle data, final Double value) {
    Validate.notNull(data, "data");
    Validate.notNull(value, "value");
    final double x = data.lastKey();
    final double y = data.lastValue();
    final double m = Math.log(y) / x;
    final double ex = Math.exp(m * value);
    final double[] result = new double[data.size()];
    result[data.size() - 1] = ex * value / (x * y);
    return result;
}

From source file:com.itemanalysis.psychometrics.polycor.PolychoricLogLikelihoodTwoStep.java

public double value(double x) {
    double logLike = 0.0;
    if (x > 1.0) {
        rho = maxcor;//ww  w  . ja v  a2  s  .c  o  m
    } else if (x < -1.0) {
        rho = -maxcor;
    } else {
        rho = x;
    }

    for (int i = 0; i < nrow; i++) {
        for (int j = 0; j < ncol; j++) {
            Pd[i][j] = bvnorm.cumulativeProbability(alpha[i], beta[j], rho);
            Pc[i][j] = Pd[i][j];
            if (i > 0) {
                Pd[i][j] -= Pc[i - 1][j];
            }
            if (j > 0) {
                Pd[i][j] -= Pc[i][j - 1];
            }
            if (i > 0 && j > 0) {
                Pd[i][j] += Pc[i - 1][j - 1];
            }
            logLike += data[i][j] * Math.log(Pd[i][j]);
        }
    }
    return -logLike;
}

From source file:eu.amidst.core.exponentialfamily.EF_Gamma.java

/**
 * {@inheritDoc}/*from   ww w  .  j a  v a2  s. c  o  m*/
 */
@Override
public void updateMomentFromNaturalParameters() {
    double alpha = this.naturalParameters.get(0) + 1;
    double beta = -this.naturalParameters.get(1);
    this.momentParameters.set(0, Gamma.digamma(alpha) - Math.log(beta));
    this.momentParameters.set(1, alpha / beta);
}

From source file:moa.classifiers.core.statisticaltests.Cramer.java

private double phiLog(double x) {
    return (Math.log(1 + x));
}