Example usage for java.lang Math round

List of usage examples for java.lang Math round

Introduction

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

Prototype

public static long round(double a) 

Source Link

Document

Returns the closest long to the argument, with ties rounding to positive infinity.

Usage

From source file:Main.java

/**
 * Convert a pair of likelihoods into a value suitable for passing to baseAndProbDiffToSqValue.
 * @param secondBestLikelihood Probability of the 2nd-best base call.  1 > secondBestLikelihood > thirdBestLikelihood.
 * @param thirdBestLikelihood Probability of the 3rd-best base call.  thirdBestLikelihood > 0.
 * @return ratio of input probabilities for storing in SQ tag.
 *///  w ww  .  j a v  a2 s. c  o  m
public static byte sqScaledProbabilityRatio(final double secondBestLikelihood,
        final double thirdBestLikelihood) {
    if (secondBestLikelihood >= 1.0 || thirdBestLikelihood <= 0 || thirdBestLikelihood > secondBestLikelihood) {
        throw new IllegalArgumentException("Likelihoods out of range.  second best: " + secondBestLikelihood
                + "; third best: " + thirdBestLikelihood);
    }
    // Cap value at QUALITY_MASK
    return (byte) (Math.min(Math.round(-10.0 * Math.log10(thirdBestLikelihood / secondBestLikelihood)),
            QUALITY_MASK));
}

From source file:Main.java

/**
 * For some reason, can't find this utility method in the java framework.
 * //  ww w . j a  v a2s. co  m
 * @param sDateTime
 *            an xsd:dateTime string
 * @return an equivalent java.util.Date
 * @throws ParseException
 */
public static Date parseXsdDateTime(String sDateTime) throws ParseException {
    final DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

    int iDotPosition = NORMAL_IDOT_POSITION;
    if (sDateTime.charAt(0) == '-') {
        iDotPosition = IDOT_POSITION_IFNEG;
    }
    Date result;
    if (sDateTime.length() <= iDotPosition) {
        return format.parse(sDateTime + "Z");
    }

    String millis = null;
    char c = sDateTime.charAt(iDotPosition);
    if (c == '.') {
        // if datetime has milliseconds, separate them
        int eoms = iDotPosition + 1;
        while (Character.isDigit(sDateTime.charAt(eoms))) {
            eoms += 1;
        }
        millis = sDateTime.substring(iDotPosition, eoms);
        sDateTime = sDateTime.substring(0, iDotPosition) + sDateTime.substring(eoms);
        c = sDateTime.charAt(iDotPosition);
    }
    if (c == '+' || c == '-') {
        format.setTimeZone(TimeZone.getTimeZone("GMT" + sDateTime.substring(iDotPosition)));
        sDateTime = sDateTime.substring(0, iDotPosition) + "Z";
    } else if (c != 'Z') {
        throw new ParseException("Illegal timezone specification.", iDotPosition);
    }

    result = format.parse(sDateTime);
    if (millis != null) {
        result.setTime(result.getTime() + Math.round(Float.parseFloat(millis) * ONE_SEC_IN_MILLISECS));
    }

    result = offsetDateFromGMT(result);
    return result;
}

From source file:endrov.typeLineageAutoNucJH.FitGaussian.java

private static double[] fitGaussian2D_(EvPixels p, double sigmaInit, final double midxInit,
        final double midyInit) {
    //sigma00, sigma01, sigma11, mu_x, mu_y, c 

    p = p.getReadOnly(EvPixelsType.DOUBLE);
    final double[] arrPixels = p.getArrayDouble();
    final int w = p.getWidth();
    final int h = p.getHeight();

    int extent = (int) Math.round(3 * sigmaInit);
    extent = Math.max(extent, 2);

    final int sx = Math.max(0, (int) (midxInit - extent));
    final int ex = Math.min(w, (int) (midxInit + extent + 1)); //+1 to the right?
    final int sy = Math.max(0, (int) (midyInit - extent));
    final int ey = Math.min(h, (int) (midyInit + extent + 1));

    double minIntensity = Double.MAX_VALUE;
    double maxIntensity = -Double.MAX_VALUE;
    for (int y = sy; y < ey; y++) {
        int base = y * w;
        double dy2 = y - midyInit;
        dy2 = dy2 * dy2;/*  w  w  w.  j  a va2  s .  co m*/
        for (int x = sx; x < ex; x++) {
            double dx2 = x - midxInit;
            dx2 = dx2 * dx2;
            double t = arrPixels[base + x];
            //if(dx2+dy2<=extent*extent)
            {
                if (t < minIntensity)
                    minIntensity = t;
                if (t > maxIntensity)
                    maxIntensity = t;
            }
        }
    }

    //double[] weights=new double[]{1};
    double[] startPoint = new double[] { sigmaInit, 0, sigmaInit, midxInit, midyInit, minIntensity,
            maxIntensity - minIntensity };
    //double[] output=new double[startPoint.length];

    try {
        MultivariateRealFunction func = new MultivariateRealFunction() {
            //      opt.optimize(

            public double value(double[] arg) throws FunctionEvaluationException, IllegalArgumentException {
                double sigma00 = arg[0];
                double sigma01 = arg[1];
                double sigma11 = arg[2];
                double mu0 = arg[3];
                double mu1 = arg[4];
                double C = arg[5];
                double D = arg[6];

                double sumError = 0;

                Matrix2d sigma = new Matrix2d(sigma00, sigma01, sigma01, sigma11);
                Matrix2d sigmaInv = new Matrix2d();
                sigma.invert(sigmaInv);
                double sigmaDet = sigma.determinant();
                double front = 1.0 / (2 * Math.PI * Math.sqrt(sigmaDet));
                //System.out.println("front: "+front);
                //System.out.println("sigma inv "+sigmaInv);

                if (mu0 < sx || mu0 > ex)
                    sumError += 1000000;
                if (mu1 < sy || mu1 > ey)
                    sumError += 1000000;
                if (sigma00 < 1)
                    sumError += 1000000;
                //if(sigma01<0)               sumError+=1000000;
                if (sigma11 < 1)
                    sumError += 1000000;
                if (D <= 0)
                    sumError += 1000000;

                for (int y = sy; y < ey; y++) {
                    int base = y * w;
                    double dy2 = y - midyInit;
                    dy2 = dy2 * dy2;
                    for (int x = sx; x < ex; x++) {
                        double dx2 = x - midxInit;
                        dx2 = dx2 * dx2;
                        double thisReal = arrPixels[base + x];
                        //                  if(dx2+dy2<=extent*extent)
                        {
                            //               DoubleMatrix2D sigma=new DenseDoubleMatrix2D(new double[][]{{sigma00,sigma01},{sigma01,sigma11}});
                            //double sigmaDet=sigma00*sigma11-sigma01*sigma01;

                            double dx0 = x - mu0;
                            double dx1 = y - mu1;

                            //http://en.wikipedia.org/wiki/Multivariate_normal_distribution

                            Vector2d vX = new Vector2d(dx0, dx1);
                            Vector2d op = new Vector2d(vX);
                            sigmaInv.transform(op);
                            double upper = -0.5 * op.dot(vX);
                            double exp = Math.exp(upper);

                            //System.out.println("front "+front+" "+exp+" C "+C+" thisreal"+thisReal+" upper "+upper);

                            if (upper > -0.4)
                                exp = 1;
                            else
                                exp = Math.max(0, 1 + upper + 0.4);

                            /*
                            if(exp<0.7)
                               exp=0;
                            else
                               exp=1;
                            */

                            double thisExpected = D * front * exp + C;
                            double diff = thisExpected - thisReal;
                            sumError += diff * diff;

                        }
                    }
                }

                //System.out.println(sigma00+"\t"+sigma01+"\t"+sigma11+"\tC"+C+"\tmu "+mu0+","+mu1+"\terr "+sumError);
                return sumError;
                //            return new double[]{sumError};
            }

        };

        NelderMead opt = new NelderMead();
        //LevenbergMarquardtOptimizer opt=new LevenbergMarquardtOptimizer();
        opt.setMaxIterations(10000);
        RealPointValuePair pair = opt.optimize(func, GoalType.MINIMIZE, startPoint);

        int numit = opt.getIterations();
        System.out.println("#it " + numit);
        System.out.println("err " + func.value(pair.getPointRef()));
        return pair.getPointRef();

        //         for(int i=0;i<startPoint.length;i++)
        //         System.out.println("i: "+i+"  "+output[i]);
        //, output, weights, startPoint);
    }
    /*
    catch (MaxIterationsExceededException e)
       {
       System.out.println("max it reached");
               
       }*/
    catch (Exception e) {
        e.printStackTrace();
    }

    //Maybe this is a bad point?
    System.out.println("max it reached");
    return startPoint;
    //      return output;
}

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

@Override
public Double evaluate(final double[] x) {
    Validate.notNull(x, "x was null");
    final int length = x.length;
    Validate.isTrue(length > 0, "x was empty");
    final double[] winsorized = Arrays.copyOf(x, length);
    Arrays.sort(winsorized);//from   www  . ja v  a  2s .  c o m
    final int value = (int) Math.round(length * _gamma);
    final double x1 = winsorized[value];
    final double x2 = winsorized[length - value - 1];
    for (int i = 0; i < value; i++) {
        winsorized[i] = x1;
        winsorized[length - 1 - i] = x2;
    }
    return MEAN_CALCULATOR.evaluate(winsorized);
}

From source file:ceptraj.tool.Bearing.java

public static Bearing getFineGrainBearingFromValue(double value) {

    int tmp = (int) Math.round(value / 22.5);

    for (Bearing b : values()) {
        if (ArrayUtils.contains(b.getAllBearingValues(), tmp)) {
            //            if (Arrays.asList(b.getAllBearingValues()).contains(tmp)) {
            return b;
        }/*from   w  w  w .  j  a va2s.com*/
    }

    return UNSP;
}

From source file:com.jennifer.ui.util.MathUtil.java

public static double interpolateRound(double a, double b, double t) {
    return Math.round(interpolateNumber(a, b, t));
}

From source file:geogebra.kernel.statistics.AlgoInversePascal.java

protected final void compute() {

    if (input[0].isDefined() && input[1].isDefined() && input[2].isDefined()) {
        int param = (int) Math.round(a.getDouble());
        double param2 = b.getDouble();
        double val = c.getDouble();
        try {//  w  w w .j  a v  a2 s  .  c  o  m
            PascalDistribution dist = getPascalDistribution(param, param2);

            double result = dist.inverseCumulativeProbability(val);

            // eg InversePascal[1,1,1] returns  2147483647 
            if (result >= Integer.MAX_VALUE)
                num.setValue(param);
            else
                num.setValue(result + 1);

        } catch (Exception e) {
            num.setUndefined();
        }
    } else
        num.setUndefined();
}

From source file:eu.novait.imageresizer.helpers.ImageConverter.java

public void process() throws IOException {
    BufferedImage inImage = ImageIO.read(this.file);
    double ratio = (double) inImage.getWidth() / (double) inImage.getHeight();
    int w = this.width;
    int h = this.height;
    if (inImage.getWidth() >= inImage.getHeight()) {
        w = this.width;
        h = (int) Math.round(w / ratio);
    } else {/*  w  w w. j a  va 2  s .  com*/
        h = this.height;
        w = (int) Math.round(ratio * h);
    }
    int type = inImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : inImage.getType();
    BufferedImage outImage = new BufferedImage(w, h, type);
    Graphics2D g = outImage.createGraphics();
    g.drawImage(inImage, 0, 0, w, h, null);
    g.dispose();
    String ext = FilenameUtils.getExtension(this.file.getAbsolutePath());
    String t = "jpg";
    switch (ext) {
    case "png":
        t = "png";
        break;
    }
    ImageIO.write(outImage, t, this.outputfile);
}

From source file:net.sourceforge.jabm.spring.RandomIntegerFactoryBean.java

@Override
public Integer getObject() {
    return new Integer((int) Math.round(distribution.nextDouble()));
}

From source file:Main.java

private static int applyMaskColor(int color, int mask, float alpha) {
    int[] rgb = { Color.red(color), Color.green(color), Color.blue(color) };
    int[] maskRgb = { Color.red(mask), Color.green(mask), Color.blue(mask) };
    for (int j = 0; j < 3; j++) {
        rgb[j] = Math.round(rgb[j] * alpha) + Math.round(maskRgb[j] * (1 - alpha));
        if (rgb[j] > 255) {
            rgb[j] = 255;// www  . j  a v  a2  s  .  c o  m
        } else if (rgb[j] < 0) {
            rgb[j] = 0;
        }
    }
    return Color.rgb(rgb[0], rgb[1], rgb[2]);
}