Example usage for java.lang Math exp

List of usage examples for java.lang Math exp

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static double exp(double a) 

Source Link

Document

Returns Euler's number e raised to the power of a double value.

Usage

From source file:com.opengamma.analytics.math.statistics.descriptive.LognormalFisherKurtosisFromVolatilityCalculator.java

@Override
public Double evaluate(final Double sigma, final Double t) {
    Validate.notNull(sigma, "sigma");
    Validate.notNull(t, "t");
    final double y = Math.sqrt(Math.exp(sigma * sigma * t) - 1);
    final double y2 = y * y;
    return y2 * (16 + y2 * (15 + y2 * (6 + y2)));
}

From source file:com.opengamma.analytics.financial.timeseries.returns.ContinuouslyCompoundedGeometricMeanReturnCalculator.java

@Override
public Double evaluate(final double[] x) {
    Validate.notNull(x, "x");
    ArgumentChecker.notEmpty(x, "x");
    final int n = x.length;
    double mult = Math.exp(x[0]);
    for (int i = 1; i < n; i++) {
        mult *= Math.exp(x[i]);//  w  w w .j  a v  a2  s .  c om
    }
    return Math.log(mult) / n;
}

From source file:Main.java

public static double gamma(double x) {
    double g = 1.0;
    double f;//www  . j av  a 2  s .  c o  m
    if (x > 0.0) {
        while (x < 3.0) {
            g *= x;
            ++x;
        }
        f = (1.0 - 2.0 / (7.0 * Math.pow(x, 2.0)) * (1.0 - 2.0 / (3.0 * Math.pow(x, 2.0))))
                / (30.0 * Math.pow(x, 2.0));
        f = (1.0 - f) / (12.0 * x) + x * (Math.log(x) - 1.0);
        f = Math.exp(f) / g * Math.pow(6.283185307179586 / x, 0.5);
    } else {
        f = Double.POSITIVE_INFINITY;
    }
    return f;
}

From source file:geogebra.util.MyMath.java

final public static double cosh(double a) {
    return (Math.exp(a) + Math.exp(-a)) * 0.5;
}

From source file:beast.math.distribution.GammaDistributionTest.java

/**
 * This test stochastically draws gamma// w ww  . ja  v a2 s  .c o m
 * variates and compares the coded pdf 
 * with the actual pdf.  
 * The tolerance is required to be at most 1e-10.
 */

static double mypdf(double value, double shape, double scale) {
    return Math.exp(
            (shape - 1) * Math.log(value) - value / scale - Gamma.logGamma(shape) - shape * Math.log(scale));
}

From source file:Main.java

private static int getDoubleChars(double number, char[] buf, int charPos, int significantDigits) {
    if (number != number) {
        STR_NAN.getChars(0, STR_NAN_LEN, buf, charPos);
        return charPos + STR_NAN_LEN;
    }//from  w  ww .  java 2s .co m

    //we need to detect -0.0 to be compatible with JDK
    boolean negative = (Double.doubleToRawLongBits(number) & 0x8000000000000000L) != 0;
    if (negative) {
        buf[charPos++] = '-';
        number = -number;
    }

    if (number == Double.POSITIVE_INFINITY) {
        STR_INFINITY.getChars(0, STR_INFINITY_LEN, buf, charPos);
        return charPos + STR_INFINITY_LEN;
    }

    if (number == 0) {
        buf[charPos++] = '0';
        buf[charPos++] = '.';
        buf[charPos++] = '0';
    } else {
        int exponent = 0;

        // calc. the power (base 10) for the given number:
        int pow = (int) Math.floor(Math.log(number) / LN10);

        // use exponential formatting if number too big or too small
        if (pow < -3 || pow > 6) {
            exponent = pow;
            number /= Math.exp(LN10 * exponent);
        } // if

        // Recalc. the pow if exponent removed and d has changed
        pow = (int) Math.floor(Math.log(number) / LN10);

        // Decide how many insignificant zeros there will be in the
        // lead of the number.
        int insignificantDigits = -Math.min(0, pow);

        // Force it to start with at least "0." if necessarry
        pow = Math.max(0, pow);
        double divisor = Math.pow(10, pow);

        // Loop over the significant digits (17 for double, 8 for float)
        for (int i = 0, end = significantDigits + insignificantDigits, div; i < end; i++) {

            // Add the '.' when passing from 10^0 to 10^-1
            if (pow == -1) {
                buf[charPos++] = '.';
            } // if

            // Find the divisor
            div = (int) (number / divisor);
            // This might happen with 1e6: pow = 5 ( instead of 6 )
            if (div == 10) {
                buf[charPos++] = '1';
                buf[charPos++] = '0';
            } // if
            else {
                buf[charPos] = (char) (div + '0');
                charPos++;
            } // else

            number -= div * divisor;
            divisor /= 10.0;
            pow--;

            // Break the loop if we have passed the '.'
            if (number == 0 && divisor < 0.1)
                break;
        } // for

        // Remove trailing zeros
        while (buf[charPos - 1] == '0')
            charPos--;

        // Avoid "4." instead of "4.0"
        if (buf[charPos - 1] == '.')
            charPos++;
        if (exponent != 0) {
            buf[charPos++] = 'E';
            if (exponent < 0) {
                buf[charPos++] = '-';
                exponent = -exponent;
            }
            if (exponent >= 100)
                buf[charPos++] = (char) (exponent / 100 + '0');
            if (exponent >= 10)
                buf[charPos++] = (char) (exponent / 10 % 10 + '0');
            buf[charPos++] = (char) (exponent % 10 + '0');
        } // if
    }
    return charPos;
}

From source file:geogebra.util.MyMath.java

final public static double sinh(double a) {
    return (Math.exp(a) - Math.exp(-a)) * 0.5;
}

From source file:emlab.util.TrendEstimator.java

public static double[] estimateGeometricTrend(double[][] input, double[] predictionYears) {
    //Get logarithm of second trend
    for (int i = 0; i < input.length; i++) {
        input[i][1] = Math.log(input[i][1]);
    }//w ww  . jav  a 2s  . c o  m
    //input[1]=log;
    SimpleRegression sr = new SimpleRegression();
    sr.addData(input);
    double result[] = new double[predictionYears.length];
    for (int i = 0; i < result.length; i++) {
        result[i] = Math.exp(sr.predict(predictionYears[i]));
    }
    return result;

}

From source file:Util.java

/**
 * Sums an array of numbers log(x1)...log(xn).  This saves some of
 *  the unnecessary calls to Math.log in the two-argument version.
 * <p>//from   w  w  w.j  av a  2  s .c o  m
 * Note that this implementation IGNORES elements of the input
 *  array that are more than LOGTOLERANCE (currently 30.0) less
 *  than the maximum element.
 * <p>
 * Cursory testing makes me wonder if this is actually much faster than
 *  repeated use of the 2-argument version, however -cas.
 * @param vals An array log(x1), log(x2), ..., log(xn)
 * @return log(x1+x2+...+xn)
 */
public static double sumLogProb(double[] vals) {
    double max = Double.NEGATIVE_INFINITY;
    int len = vals.length;
    int maxidx = 0;

    for (int i = 0; i < len; i++) {
        if (vals[i] > max) {
            max = vals[i];
            maxidx = i;
        }
    }

    boolean anyAdded = false;
    double intermediate = 0.0;
    double cutoff = max - LOGTOLERANCE;

    for (int i = 0; i < maxidx; i++) {
        if (vals[i] >= cutoff) {
            anyAdded = true;
            intermediate += Math.exp(vals[i] - max);
        }
    }
    for (int i = maxidx + 1; i < len; i++) {
        if (vals[i] >= cutoff) {
            anyAdded = true;
            intermediate += Math.exp(vals[i] - max);
        }
    }

    if (anyAdded) {
        return max + Math.log(1.0 + intermediate);
    } else {
        return max;
    }

}

From source file:geogebra.util.MyMath.java

final public static double tanh(double a) {
    double e = Math.exp(2.0 * a);
    return (e - 1.0) / (e + 1.0);
}