Example usage for java.lang Double isInfinite

List of usage examples for java.lang Double isInfinite

Introduction

In this page you can find the example usage for java.lang Double isInfinite.

Prototype

public static boolean isInfinite(double v) 

Source Link

Document

Returns true if the specified number is infinitely large in magnitude, false otherwise.

Usage

From source file:org.renjin.primitives.random.Beta.java

public static double rbeta(Context.Globals context, double aa, double bb) {
    double a, b, alpha;
    double r, s, t, u1, u2, v, w, y, z;

    boolean qsame;

    if (aa <= 0. || bb <= 0. || (Double.isInfinite(aa) && Double.isInfinite(bb))) {
        return (Double.NaN);
    }//from  w w w . j  av  a2 s.  c  o m

    if (Double.isInfinite(aa)) {
        return 1.0;
    }

    if (Double.isInfinite(bb)) {
        return 0.0;
    }

    /* Test if we need new "initializing" */
    qsame = (olda == aa) && (oldb == bb);
    if (!qsame) {
        olda = aa;
        oldb = bb;
    }

    a = Math.min(aa, bb);
    b = Math.max(aa, bb); /* a <= b */
    alpha = a + b;

    if (a <= 1.0) { /* --- Algorithm BC --- */

        /* changed notation, now also a <= b (was reversed) */

        if (!qsame) { /* initialize */
            beta = 1.0 / a;
            delta = 1.0 + b - a;
            k1 = delta * (0.0138889 + 0.0416667 * a) / (b * beta - 0.777778);
            k2 = 0.25 + (0.5 + 0.25 / delta) * a;
        }
        /* FIXME: "do { } while()", but not trivially because of "continue"s:*/
        for (;;) {
            u1 = context.rng.unif_rand();
            u2 = context.rng.unif_rand();
            if (u1 < 0.5) {
                y = u1 * u2;
                z = u1 * y;
                if (0.25 * u2 + z - y >= k1) {
                    continue;
                }
            } else {
                z = u1 * u1 * u2;
                if (z <= 0.25) {
                    v = beta * Math.log(u1 / (1.0 - u1));
                    if (v <= expmax) {
                        w = b * Math.exp(v);
                        if (Double.isInfinite(w)) {
                            w = Double.MAX_VALUE;
                        }
                    } else {
                        w = Double.MAX_VALUE;
                    }
                    break;
                }
                if (z >= k2) {
                    continue;
                }
            }

            v = beta * Math.log(u1 / (1.0 - u1));
            if (v <= expmax) {
                w = b * Math.exp(v);
                if (Double.isInfinite(w)) {
                    w = Double.MAX_VALUE;
                }
            } else {
                w = Double.MAX_VALUE;
            }

            if (alpha * (Math.log(alpha / (a + w)) + v) - 1.3862944 >= Math.log(z)) {
                break;
            }
        }
        return (aa == a) ? a / (a + w) : w / (a + w);

    } else { /* Algorithm BB */

        if (!qsame) { /* initialize */
            beta = Math.sqrt((alpha - 2.0) / (2.0 * a * b - alpha));
            gamma = a + 1.0 / beta;
        }
        do {
            u1 = context.rng.unif_rand();
            u2 = context.rng.unif_rand();

            v = beta * Math.log(u1 / (1.0 - u1));
            if (v <= expmax) {
                w = a * Math.exp(v);
                if (Double.isInfinite(w)) {
                    w = Double.MAX_VALUE;
                }
            } else {
                w = Double.MAX_VALUE;
            }

            z = u1 * u1 * u2;
            r = gamma * v - 1.3862944;
            s = a + r - w;
            if (s + 2.609438 >= 5.0 * z) {
                break;
            }
            t = Math.log(z);
            if (s > t) {
                break;
            }
        } while (r + alpha * Math.log(alpha / (b + w)) < t);

        return (aa != a) ? b / (b + w) : w / (b + w);
    }
}

From source file:ch.algotrader.util.RoundUtil.java

public static BigDecimal getBigDecimal(double value, int scale) {

    if (Double.isNaN(value) || Double.isInfinite(value)) {
        return null;
    } else {/*  ww  w.jav a2 s  .co  m*/
        BigDecimal decimal = new BigDecimal(value);
        return decimal.setScale(scale, BigDecimal.ROUND_HALF_UP);
    }
}

From source file:com.proofpoint.units.DataSize.java

public DataSize(double size, Unit unit) {
    Preconditions.checkArgument(!Double.isInfinite(size), "size is infinite");
    Preconditions.checkArgument(!Double.isNaN(size), "size is not a number");
    Preconditions.checkArgument(size >= 0, "size is negative");
    Preconditions.checkNotNull(unit, "unit is null");

    this.value = size;
    this.unit = unit;
}

From source file:org.renjin.stats.internals.distributions.Beta.java

@Internal
@DataParallel//from   w w  w.j  a  va 2 s  . c  o  m
public static double rbeta(Session context, double aa, double bb) {
    double a, b, alpha;
    double r, s, t, u1, u2, v, w, y, z;

    boolean qsame;

    if (aa <= 0. || bb <= 0. || (Double.isInfinite(aa) && Double.isInfinite(bb))) {
        return (Double.NaN);
    }

    if (Double.isInfinite(aa)) {
        return 1.0;
    }

    if (Double.isInfinite(bb)) {
        return 0.0;
    }

    /* Test if we need new "initializing" */
    qsame = (olda == aa) && (oldb == bb);
    if (!qsame) {
        olda = aa;
        oldb = bb;
    }

    a = Math.min(aa, bb);
    b = Math.max(aa, bb); /* a <= b */
    alpha = a + b;

    if (a <= 1.0) { /* --- Algorithm BC --- */

        /* changed notation, now also a <= b (was reversed) */

        if (!qsame) { /* initialize */
            beta = 1.0 / a;
            delta = 1.0 + b - a;
            k1 = delta * (0.0138889 + 0.0416667 * a) / (b * beta - 0.777778);
            k2 = 0.25 + (0.5 + 0.25 / delta) * a;
        }
        /* FIXME: "do { } while()", but not trivially because of "continue"s:*/
        for (;;) {
            u1 = context.rng.unif_rand();
            u2 = context.rng.unif_rand();
            if (u1 < 0.5) {
                y = u1 * u2;
                z = u1 * y;
                if (0.25 * u2 + z - y >= k1) {
                    continue;
                }
            } else {
                z = u1 * u1 * u2;
                if (z <= 0.25) {
                    v = beta * Math.log(u1 / (1.0 - u1));
                    if (v <= expmax) {
                        w = b * Math.exp(v);
                        if (Double.isInfinite(w)) {
                            w = Double.MAX_VALUE;
                        }
                    } else {
                        w = Double.MAX_VALUE;
                    }
                    break;
                }
                if (z >= k2) {
                    continue;
                }
            }

            v = beta * Math.log(u1 / (1.0 - u1));
            if (v <= expmax) {
                w = b * Math.exp(v);
                if (Double.isInfinite(w)) {
                    w = Double.MAX_VALUE;
                }
            } else {
                w = Double.MAX_VALUE;
            }

            if (alpha * (Math.log(alpha / (a + w)) + v) - 1.3862944 >= Math.log(z)) {
                break;
            }
        }
        return (aa == a) ? a / (a + w) : w / (a + w);

    } else { /* Algorithm BB */

        if (!qsame) { /* initialize */
            beta = Math.sqrt((alpha - 2.0) / (2.0 * a * b - alpha));
            gamma = a + 1.0 / beta;
        }
        do {
            u1 = context.rng.unif_rand();
            u2 = context.rng.unif_rand();

            v = beta * Math.log(u1 / (1.0 - u1));
            if (v <= expmax) {
                w = a * Math.exp(v);
                if (Double.isInfinite(w)) {
                    w = Double.MAX_VALUE;
                }
            } else {
                w = Double.MAX_VALUE;
            }

            z = u1 * u1 * u2;
            r = gamma * v - 1.3862944;
            s = a + r - w;
            if (s + 2.609438 >= 5.0 * z) {
                break;
            }
            t = Math.log(z);
            if (s > t) {
                break;
            }
        } while (r + alpha * Math.log(alpha / (b + w)) < t);

        return (aa != a) ? b / (b + w) : w / (b + w);
    }
}

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

@Override
public PiecewisePolynomialResult interpolate(final double[] xValues, final double[] yValues) {

    ArgumentChecker.notNull(xValues, "xValues");
    ArgumentChecker.notNull(yValues, "yValues");

    ArgumentChecker.isTrue(xValues.length == yValues.length, "xValues length = yValues length");
    ArgumentChecker.isTrue(xValues.length > 1, "Data points should be more than 1");

    final int nDataPts = xValues.length;

    for (int i = 0; i < nDataPts; ++i) {
        ArgumentChecker.isFalse(Double.isNaN(xValues[i]), "xData containing NaN");
        ArgumentChecker.isFalse(Double.isInfinite(xValues[i]), "xData containing Infinity");
        ArgumentChecker.isFalse(Double.isNaN(yValues[i]), "yData containing NaN");
        ArgumentChecker.isFalse(Double.isInfinite(yValues[i]), "yData containing Infinity");
    }/*from   ww  w . j  av a2 s. c o m*/

    for (int i = 0; i < nDataPts; ++i) {
        for (int j = i + 1; j < nDataPts; ++j) {
            ArgumentChecker.isFalse(xValues[i] == xValues[j], "xValues should be distinct");
        }
    }

    double[] xValuesSrt = Arrays.copyOf(xValues, nDataPts);
    double[] yValuesSrt = Arrays.copyOf(yValues, nDataPts);
    ParallelArrayBinarySort.parallelBinarySort(xValuesSrt, yValuesSrt);

    final DoubleMatrix2D coefMatrix = solve(xValuesSrt, yValuesSrt);

    for (int i = 0; i < coefMatrix.getNumberOfRows(); ++i) {
        for (int j = 0; j < coefMatrix.getNumberOfColumns(); ++j) {
            ArgumentChecker.isFalse(Double.isNaN(coefMatrix.getData()[i][j]), "Too large input");
            ArgumentChecker.isFalse(Double.isInfinite(coefMatrix.getData()[i][j]), "Too large input");
        }
        double ref = 0.;
        final double interval = xValuesSrt[i + 1] - xValuesSrt[i];
        for (int j = 0; j < 2; ++j) {
            ref += coefMatrix.getData()[i][j] * Math.pow(interval, 1 - j);
            ArgumentChecker.isFalse(Double.isNaN(coefMatrix.getData()[i][j]), "Too large input");
            ArgumentChecker.isFalse(Double.isInfinite(coefMatrix.getData()[i][j]), "Too large input");
        }
        final double bound = Math.max(Math.abs(ref) + Math.abs(yValuesSrt[i + 1]), 1.e-1);
        ArgumentChecker.isTrue(Math.abs(ref - yValuesSrt[i + 1]) < ERROR * bound,
                "Input is too large/small or data are not distinct enough");
    }

    return new PiecewisePolynomialResult(new DoubleMatrix1D(xValuesSrt), coefMatrix,
            coefMatrix.getNumberOfColumns(), 1);
}

From source file:com.opengamma.analytics.math.function.PiecewisePolynomialWithSensitivityFunction1D.java

/** 
 * @param pp {@link PiecewisePolynomialResultsWithSensitivity}
 * @param xKey /*from  w  w w  . j  av  a 2s  .c  o  m*/
 * @return Node sensitivity value at x=xKey
 */
public DoubleMatrix1D nodeSensitivity(final PiecewisePolynomialResultsWithSensitivity pp, final double xKey) {
    ArgumentChecker.notNull(pp, "null pp");
    ArgumentChecker.isFalse(Double.isNaN(xKey), "xKey containing NaN");
    ArgumentChecker.isFalse(Double.isInfinite(xKey), "xKey containing Infinity");

    if (pp.getDimensions() > 1) {
        throw new NotImplementedException();
    }

    final double[] knots = pp.getKnots().getData();
    final int nKnots = knots.length;
    int interval = FunctionUtils.getLowerBoundIndex(knots, xKey);
    if (interval == nKnots - 1) {
        interval--; // there is 1 less interval that knots
    }

    final double s = xKey - knots[interval];
    final DoubleMatrix2D a = pp.getCoefficientSensitivity(interval);
    final int nCoefs = a.getNumberOfRows();

    DoubleMatrix1D res = a.getRowVector(0);
    for (int i = 1; i < nCoefs; i++) {
        res = (DoubleMatrix1D) MA.scale(res, s);
        res = (DoubleMatrix1D) MA.add(res, a.getRowVector(i));
    }

    return res;
}

From source file:Main.java

/**
 * Determines the minimum and maximum values in the two dimensional array <tt>multi</tt>, ignoring any instances of <tt>noDataValue</tt>
 * ./*from  w  w w  .  j a v a2  s .  c  o  m*/
 * 
 * @param multi
 * @param noDataValue
 * @return a <tt>double[]</tt> where [0]==minimum and [1]==maximum
 */
public static double[] minMax(double[][] multi, double noDataValue) {
    double[] ret = null;
    double min = Double.POSITIVE_INFINITY, max = Double.NEGATIVE_INFINITY;
    double val;
    for (int i = 0; i < multi.length; i++) {
        for (int j = 0; j < multi[i].length; j++) {
            val = multi[i][j];
            if (val != noDataValue) {
                min = (val < min) ? val : min;
                max = (val > max) ? val : max;
            }
        }
    }
    if (!Double.isInfinite(min) & !Double.isInfinite(max)) {
        ret = new double[] { min, max };
    }

    return ret;
}

From source file:com.davidsoergel.stats.SimpleXYZSeries.java

public void addPoint(double x, double y, double z) throws StatsException {
    if (Double.isNaN(x) || Double.isInfinite(x)) {
        //throw new StatsException("Invalid x value in SimpleXYZSeries: " + x);
        logger.warn("Invalid x value in SimpleXYZSeries: " + x);
        return;//ww  w  .  ja v  a 2s . c  om
    }
    if (Double.isNaN(y) || Double.isInfinite(y)) {
        //throw new StatsException("Invalid y value in SimpleXYZSeries: " + y);
        logger.warn("Invalid y value in SimpleXYZSeries: " + y);
        return;
    }
    if (Double.isNaN(z) || Double.isInfinite(z)) {
        //throw new StatsException("Invalid z value in SimpleXYZSeries: " + z);
        logger.warn("Invalid z value in SimpleXYZSeries: " + z);
        return;
    }
    points.put(x, y, new XYZPoint(x, y, z));
    updateBounds(x, y, z);
}

From source file:com.davidsoergel.stats.HeatmapSeries.java

public void addPoint(final double x, final double y, final double z, final double startx, final double endx,
        final double starty, final double endy) throws StatsException {
    if (Double.isNaN(x) || Double.isInfinite(x)) {
        //throw new StatsException("Invalid x value in HeatmapSeries: " + x);
        logger.warn("Invalid x value in HeatmapSeries: " + x);
        return;//from  w w w.j  ava  2s. com
    }
    if (Double.isNaN(y) || Double.isInfinite(y)) {
        //throw new StatsException("Invalid y value in HeatmapSeries: " + y);
        logger.warn("Invalid y value in HeatmapSeries: " + y);
        return;
    }
    if (Double.isNaN(z) || Double.isInfinite(z)) {
        //throw new StatsException("Invalid z value in HeatmapSeries: " + z);
        logger.warn("Invalid z value in HeatmapSeries: " + z);
        return;
    }
    points.put(x, y, new HeatmapPoint(x, y, z)); //, startx, endx, starty, endy));
    updateBounds(x, y, z);
}

From source file:com.proofpoint.units.Duration.java

public Duration(double value, TimeUnit unit) {
    Preconditions.checkArgument(!Double.isInfinite(value), "value is infinite");
    Preconditions.checkArgument(!Double.isNaN(value), "value is not a number");
    Preconditions.checkArgument(value >= 0, "value is negative");
    Preconditions.checkNotNull(unit, "unit is null");

    this.value = value;
    this.unit = unit;
}