Example usage for java.lang StrictMath max

List of usage examples for java.lang StrictMath max

Introduction

In this page you can find the example usage for java.lang StrictMath max.

Prototype

@HotSpotIntrinsicCandidate
public static double max(double a, double b) 

Source Link

Document

Returns the greater of two double values.

Usage

From source file:Main.java

public static void main(String[] args) {

    int i1 = 45, i2 = 23, i3 = -12;

    System.out.println(StrictMath.max(i1, i2));

    System.out.println(StrictMath.max(i1, i3));
}

From source file:Main.java

public static void main(String[] args) {

    float f1 = 60, f2 = 40, f3 = -5;

    System.out.println(StrictMath.max(f1, f2));

    System.out.println(StrictMath.max(f1, f3));
}

From source file:Main.java

public static void main(String[] args) {

    double d1 = 12, d2 = 34, d3 = -56;

    System.out.println(StrictMath.max(d1, d2));

    System.out.println(StrictMath.max(d1, d3));
}

From source file:Main.java

public static void main(String[] args) {

    long l1 = 1234567890987654321L, l2 = 9876543212435L, l3 = -9876543212435L;

    System.out.println(StrictMath.max(l1, l2));

    System.out.println(StrictMath.max(l1, l3));
}

From source file:net.nicoulaj.benchmarks.math.DoubleMax.java

@GenerateMicroBenchmark
public void strictmath(BlackHole hole) {
    for (int i = 1; i < data.length - 1; i += 2)
        hole.consume(StrictMath.max(data[i - 1], data[i]));
}

From source file:weka.gui.beans.JFreeChartOffscreenChartRenderer.java

/**
 * Render histogram(s) (numeric attribute) or bar chart (nominal attribute).
 * Some implementations may not be able to render more than one histogram/bar
 * on the same chart - the implementation can either throw an exception or
 * just process the first series in this case.
 * //  w w  w. ja v  a 2s . c  om
 * @param width the width of the resulting chart in pixels
 * @param height the height of the resulting chart in pixels
 * @param series a list of Instances - one for each series to be plotted
 * @param attsToPlot the attribute to plot corresponding to the Instances in
 *          the series list
 * @param optionalArgs optional arguments to the renderer (may be null)
 * 
 * @return a BufferedImage containing the chart
 * @throws Exception if there is a problem rendering the chart
 */
public BufferedImage renderHistogram(int width, int height, List<Instances> series, String attToPlot,
        List<String> additionalArgs) throws Exception {

    String plotTitle = "Bar Chart";
    String userTitle = getOption(additionalArgs, "-title");
    plotTitle = (userTitle != null) ? userTitle : plotTitle;
    String colorAtt = getOption(additionalArgs, "-color");
    String pareto = getOption(additionalArgs, "-pareto");

    boolean doPareto = false;
    if (pareto != null && pareto.length() == 0 && series.size() == 1) {
        doPareto = true;
    }

    if (series.size() == 1 && colorAtt != null && colorAtt.length() > 0) {
        int colIndex = getIndexOfAttribute(series.get(0), colorAtt);

        if (colIndex >= 0 && series.get(0).attribute(colIndex).isNominal() && !doPareto) {
            // split single series out into multiple instances objects - one
            // per class
            series = splitToClasses(series.get(0), colIndex);
            for (Instances insts : series) {
                insts.setClassIndex(colIndex);
            }
        }
    }

    Instances masterInstances = series.get(0);
    int attIndex = getIndexOfAttribute(masterInstances, attToPlot);
    if (attIndex < 0) {
        attIndex = 0;
    }

    if (!(series.get(0).attribute(attIndex).isNominal()
            || series.get(0).attribute(attIndex).isRelationValued())) {
        doPareto = false;
    }

    // Do a pareto chart
    if (doPareto) {
        final DefaultKeyedValues data = new DefaultKeyedValues();
        AttributeStats attStats = masterInstances.attributeStats(attIndex);
        double[] attValFreqs = attStats.nominalWeights;
        for (int i = 0; i < attValFreqs.length; i++) {
            Number freq = new Double(attValFreqs[i]);
            data.addValue(masterInstances.attribute(attIndex).value(i), freq);
        }

        data.sortByValues(SortOrder.DESCENDING);
        final KeyedValues cumulative = DataUtilities.getCumulativePercentages(data);
        final CategoryDataset dataset = DatasetUtilities
                .createCategoryDataset(masterInstances.attribute(attIndex).name(), data);

        final JFreeChart chart = ChartFactory.createBarChart(plotTitle,
                masterInstances.attribute(attIndex).name(), "Fequency/weight mass", dataset,
                PlotOrientation.VERTICAL, true, false, false);

        final CategoryPlot plot = chart.getCategoryPlot();

        final CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setLowerMargin(0.02);
        domainAxis.setUpperMargin(0.02);

        LineAndShapeRenderer renderer2 = new LineAndShapeRenderer();
        CategoryDataset dataset2 = DatasetUtilities.createCategoryDataset("Cumulative", cumulative);
        final NumberAxis axis2 = new NumberAxis("Percent");
        axis2.setNumberFormatOverride(NumberFormat.getPercentInstance());
        // plot.
        plot.setRangeAxis(1, axis2);
        plot.setDataset(1, dataset2);
        plot.setRenderer(1, renderer2);
        plot.mapDatasetToRangeAxis(1, 1);
        plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

        chart.setBackgroundPaint(java.awt.Color.white);
        BufferedImage image = chart.createBufferedImage(width, height);
        return image;
    }

    boolean seriesAreClasses = false;
    int classIndex = masterInstances.classIndex();

    if (classIndex >= 0 && !masterInstances.attribute(classIndex).isNumeric()
            && !masterInstances.attribute(classIndex).isRelationValued()
            && masterInstances.attributeStats(classIndex).distinctCount == 1) {
        // series correspond to class labels (assume that subsequent series only
        // contain instances of one class)...
        seriesAreClasses = true;
    }

    // bar chart for a nominal attribute
    if (masterInstances.attribute(attIndex).isNominal() || masterInstances.attribute(attIndex).isString()) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        // do the master series
        String masterSeriesTitle = masterInstances.relationName();
        if (seriesAreClasses) {
            for (int i = 0; i < masterInstances.numInstances(); i++) {
                Instance current = masterInstances.instance(i);
                if (!current.isMissing(classIndex)) {
                    masterSeriesTitle = current.stringValue(classIndex);
                    break;
                }
            }
        }

        AttributeStats attStats = masterInstances.attributeStats(attIndex);
        double[] attValFreqs = attStats.nominalWeights;
        for (int i = 0; i < attValFreqs.length; i++) {
            Number freq = new Double(attValFreqs[i]);
            dataset.addValue(freq, masterSeriesTitle, masterInstances.attribute(attIndex).value(i));
        }

        // any subsequent series
        for (int i = 1; i < series.size(); i++) {
            Instances nextSeries = series.get(i);

            String seriesTitle = nextSeries.relationName();

            if (seriesAreClasses) {
                for (int j = 0; j < nextSeries.numInstances(); j++) {
                    Instance current = nextSeries.instance(j);
                    if (!current.isMissing(classIndex)) {
                        seriesTitle = current.stringValue(classIndex);
                        break;
                    }
                }

                attStats = nextSeries.attributeStats(attIndex);
                attValFreqs = attStats.nominalWeights;
                for (int j = 0; j < attValFreqs.length; j++) {
                    Number freq = new Double(attValFreqs[j]);
                    dataset.addValue(freq, seriesTitle, nextSeries.attribute(attIndex).value(j));
                }
            }
        }

        JFreeChart chart = null;

        if (series.size() == 1) {
            chart = ChartFactory.createBarChart(plotTitle, masterInstances.attribute(attIndex).name(),
                    "Fequency/weight mass", dataset, PlotOrientation.VERTICAL, true, false, false);
        } else {
            chart = ChartFactory.createStackedBarChart(plotTitle, masterInstances.attribute(attIndex).name(),
                    "Fequency/weight mass", dataset, PlotOrientation.VERTICAL, true, false, false);
        }

        chart.setBackgroundPaint(java.awt.Color.white);
        BufferedImage image = chart.createBufferedImage(width, height);
        return image;

    } else {
        // histogram for numeric attributes
        HistogramDataset dataset = new HistogramDataset();

        // combine all series in order to get overall std dev, and range
        Instances temp = new Instances(masterInstances);
        for (int i = 1; i < series.size(); i++) {
            Instances additional = series.get(i);
            for (Instance tempI : additional) {
                temp.add(tempI);
            }
        }

        AttributeStats stats = temp.attributeStats(attIndex);
        Stats numericStats = stats.numericStats;
        double intervalWidth = 3.49 * numericStats.stdDev * StrictMath.pow(temp.numInstances(), (-1.0 / 3.0));
        double range = numericStats.max - numericStats.min;
        int numBins = StrictMath.max(1, (int) StrictMath.round(range / intervalWidth));

        // do the master series
        String masterSeriesTitle = masterInstances.relationName();
        if (seriesAreClasses) {
            for (int i = 0; i < masterInstances.numInstances(); i++) {
                Instance current = masterInstances.instance(i);
                if (!current.isMissing(current.classAttribute())) {
                    masterSeriesTitle = current.stringValue(current.classAttribute());
                    break;
                }
            }
        }

        // have to set min, max and num bins (using heuristic from AttSummPanel).
        // Make sure
        // to set series length to num instances - num missing values for att
        stats = masterInstances.attributeStats(attIndex);
        /*
         * numericStats = stats.numericStats; //numericStats.calculateDerived();
         * intervalWidth = StrictMath.max(1, 3.49 * numericStats.stdDev *
         * StrictMath.pow(masterInstances.numInstances(), (-1.0/3.0)));
         */

        double[] seriesVals = new double[masterInstances.numInstances() - stats.missingCount];
        int count = 0;
        for (int i = 0; i < masterInstances.numInstances(); i++) {
            Instance current = masterInstances.instance(i);
            if (!current.isMissing(attIndex)) {
                seriesVals[count++] = current.value(attIndex);
            }
        }

        dataset.addSeries(masterSeriesTitle, seriesVals, numBins, numericStats.min, numericStats.max);

        // any subsequent series
        for (int i = 1; i < series.size(); i++) {
            Instances nextSeries = series.get(i);

            String seriesTitle = nextSeries.relationName();

            if (seriesAreClasses) {
                for (int j = 0; j < nextSeries.numInstances(); j++) {
                    Instance current = nextSeries.instance(j);
                    if (!current.isMissing(nextSeries.classAttribute())) {
                        seriesTitle = current.stringValue(nextSeries.classAttribute());
                        break;
                    }
                }
            }

            stats = nextSeries.attributeStats(attIndex);
            /*
             * numericStats = stats.numericStats; //
             * numericStats.calculateDerived(); intervalWidth = StrictMath.max(1,
             * 3.49 * numericStats.stdDev *
             * StrictMath.pow(masterInstances.numInstances(), (-1.0/3.0))); range =
             * numericStats.max - numericStats.min; numBins = StrictMath.max(1,
             * (int) StrictMath.round(range / intervalWidth));
             */
            seriesVals = new double[nextSeries.numInstances() - stats.missingCount];
            count = 0;
            for (int j = 0; j < nextSeries.numInstances(); j++) {
                Instance current = nextSeries.instance(j);
                if (!current.isMissing(attIndex)) {
                    seriesVals[count++] = current.value(attIndex);
                }
            }

            dataset.addSeries(seriesTitle, seriesVals, numBins, numericStats.min, numericStats.max);
        }

        JFreeChart chart = ChartFactory.createHistogram(plotTitle, masterInstances.attribute(attIndex).name(),
                null, dataset, PlotOrientation.VERTICAL, true, false, false);

        // chart.setBackgroundPaint(java.awt.Color.white);
        XYPlot xyplot = (XYPlot) chart.getPlot();
        xyplot.setForegroundAlpha(0.50F);
        XYBarRenderer xybarrenderer = (XYBarRenderer) xyplot.getRenderer();
        xybarrenderer.setDrawBarOutline(false);
        xybarrenderer.setShadowVisible(false);

        BufferedImage image = chart.createBufferedImage(width, height);
        return image;
    }
}

From source file:org.cirdles.calamari.algorithms.WeightedMeanCalculators.java

/**
 * Adapted from Simon Bodorkos interpretation of Ludwig:
 * https://github.com/CIRDLES/ET_Redux/wiki/SHRIMP:-Sub-wtdLinCorr. Note the
 * logic is simplified and output values are stored in object of type
 * WtdLinCorrResults. Indexing in Java is 0-based, hence the use of i-1 and
 * minIndex - 1 in the calls to deletePoint.
 *
 * @param y/*  w  w  w . j  a  v a  2  s  .c  o m*/
 * @param sigRho
 * @param x
 * @return
 */
public static WtdLinCorrResults wtdLinCorr(double[] y, double[][] sigRho, double[] x) {

    WtdLinCorrResults wtdLinCorrResults = new WtdLinCorrResults();

    boolean linReg = (y.length == x.length);

    int avg1LinRegr2 = linReg ? 2 : 1;
    int n = y.length;
    double[] mswdRatList = new double[] { 0.0, 0.1, 0.15, 0.2, 0.2, 0.25 };

    double mswdRatToler = (n > 7) ? 0.3 : mswdRatList[n - avg1LinRegr2 - 1];
    //        int maxRej = (int) StrictMath.ceil((n - avg1LinRegr2) / 8.0);
    // incorrect statement found by Griffin Hiers Feb 2017
    int maxRej = 1 + (int) StrictMath.floor((n - avg1LinRegr2) / 8.0);
    //        boolean[] rej = new boolean[n]; // not used

    double minProb = 0.1;
    //        double wLrej = 0;
    int pass = 0;
    int minIndex = -1;
    double minMSWD = 0.0;
    double maxProb = 0.0;

    double[] y1 = y.clone();
    double[] y2 = y.clone();
    double[] x1 = x.clone();
    double[] x2 = x.clone();
    double[][] sigRho1 = sigRho.clone();
    double[][] sigRho2 = sigRho.clone();

    double[] sigmaY = new double[n];
    for (int i = 0; i < n; i++) {
        sigmaY[i] = sigRho[i][i];
    }

    double f = StrictMath.max(TukeyBiweight.calculateMedian(sigmaY), 1e-10);
    for (int i = 0; i < n; i++) {
        sigRho1[i][i] = StrictMath.max(sigRho1[i][i], f);
        sigRho2[i][i] = sigRho1[i][i];
    }

    boolean doContinue = true;
    int nw = n;
    DeletePointResults deletePointResults;
    double[] probW = new double[n + 1];
    double[] mswdW = new double[n + 1];
    double[] sigmaInterW = new double[n + 1];
    double[] interW = new double[n + 1];
    double[] slopeW = new double[n + 1];
    double[] sigmaSlopeW = new double[n + 1];
    double[] covSlopeInterW = new double[n + 1];

    do {
        for (int i = 0; i < (n + 1); i++) {
            if (i > 0) {
                deletePointResults = deletePoint(i - 1, y1, sigRho1, x1);

                y2 = deletePointResults.getY2();
                sigRho2 = deletePointResults.getSigRho2();
                x2 = deletePointResults.getX2();
                nw = n - 1;
            }

            if ((nw == 1) && !linReg) {
                probW[i] = 1.0;
                mswdW[i] = 0.0;
                sigmaInterW[i] = 1.0;
                interW[i] = 1.0;
            } else if (linReg) {
                WeightedLinearCorrResults weightedLinearCorrResults = weightedLinearCorr(y2, x2, sigRho2);

                slopeW[i] = weightedLinearCorrResults.getSlope();
                interW[i] = weightedLinearCorrResults.getIntercept();
                mswdW[i] = weightedLinearCorrResults.getMswd();
                probW[i] = weightedLinearCorrResults.getProb();
                sigmaSlopeW[i] = weightedLinearCorrResults.getSlopeSig();
                sigmaInterW[i] = weightedLinearCorrResults.getInterceptSig();
                covSlopeInterW[i] = weightedLinearCorrResults.getSlopeInterceptCov();
                // bad is never used

            } else {
                WtdAvCorrResults wtdAvCorrResults = wtdAvCorr(y2, convertCorrelationsToCovariances(sigRho2));

                interW[i] = wtdAvCorrResults.getMeanVal();
                sigmaInterW[i] = wtdAvCorrResults.getSigmaMeanVal();
                mswdW[i] = wtdAvCorrResults.getMswd();
                probW[i] = wtdAvCorrResults.getProb();
            }

            if (i == 0) {
                if (probW[0] > 0.1) {
                    minIndex = 0;
                    //                        minMSWD = mswdW[0]; // assignment never used
                    // exit for loop of i
                    break;
                }

                maxProb = probW[0];
            }
        } // for loop of i

        if (minIndex == 0) {
            doContinue = false;
        } else {
            minIndex = 0;
            minMSWD = mswdW[0];

            for (int i = 1; i < (n + 1); i++) {
                double mswdRat = mswdW[i] / StrictMath.max(1e-32, mswdW[0]);
                if ((mswdRat < mswdRatToler) && (mswdW[i] < minMSWD) && (probW[i] > minProb)) {
                    //                        rej[i] = true; not used
                    //                        wLrej++; not used
                    minIndex = i;
                    maxProb = probW[i];
                    minMSWD = mswdW[i];
                }
            }

            pass++;

            // note check for pass > 0 in original code is redundant
            if ((minIndex == 0) || (pass == maxRej) || (maxProb > 0.1)) {
                doContinue = false;
            } else {
                deletePointResults = deletePoint(minIndex - 1, y1, sigRho1, x1);

                y2 = deletePointResults.getY2();
                sigRho2 = deletePointResults.getSigRho2();
                x2 = deletePointResults.getX2();
                n -= 1;

                y1 = new double[n];
                if (linReg) {
                    x1 = new double[n];
                }

                sigRho1 = new double[n][n];

                for (int i = 0; i < n; i++) {
                    y1[i] = y2[i];
                    if (linReg) {
                        x1[i] = x2[i];
                    }
                    System.arraycopy(sigRho2[i], 0, sigRho1[i], 0, n);
                }
            }
        }
    } while (doContinue);

    double intercept = interW[minIndex];
    double sigmaIntercept = sigmaInterW[minIndex];
    double mswd = mswdW[minIndex];
    double probfit = probW[minIndex];

    if (linReg && (minIndex > 0)) {
        wtdLinCorrResults.setSlope(slopeW[minIndex]);
        wtdLinCorrResults.setSigmaSlope(sigmaSlopeW[minIndex]);
        wtdLinCorrResults.setCovSlopeInter(covSlopeInterW[minIndex]);
    }

    if (probfit < 0.05) {
        sigmaIntercept *= StrictMath.sqrt(mswd);

        if (linReg) {
            wtdLinCorrResults.setSigmaSlope(wtdLinCorrResults.getSigmaSlope() * StrictMath.sqrt(mswd));
        }
    }

    wtdLinCorrResults.setBad(false);
    wtdLinCorrResults.setIntercept(intercept);
    wtdLinCorrResults.setSigmaIntercept(sigmaIntercept);
    wtdLinCorrResults.setMswd(mswd);
    wtdLinCorrResults.setProbFit(probfit);
    wtdLinCorrResults.setMinIndex(minIndex);

    return wtdLinCorrResults;
}