Example usage for org.apache.commons.math3.util Precision round

List of usage examples for org.apache.commons.math3.util Precision round

Introduction

In this page you can find the example usage for org.apache.commons.math3.util Precision round.

Prototype

public static float round(float x, int scale) 

Source Link

Document

Rounds the given value to the specified number of decimal places.

Usage

From source file:matrix.MatrixTest.java

/**
 * @param args the command line arguments
 *//*from  w  ww.  ja  va  2  s. c o m*/
public static void main(String[] args) {
    // TODO code application logic here
    Matrix testMatrix = new Matrix();
    System.out.println("My testMatrix (should be identity 2x2):");
    System.out.println(testMatrix);

    Matrix testMatrix2 = Matrix.IdentityMatrix(3);
    System.out.println("My testMatrix2 (should be identity 3x3) : ");
    System.out.println(testMatrix2);

    Matrix testMatrix3 = Matrix.IdentityMatrix(3);
    System.out.println("My testMatrix3 (should be identity 3x3) : ");
    System.out.println(testMatrix3);

    int[][] myArray = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 2 } };

    Matrix testMatrix4 = new Matrix(myArray);

    System.out.println("My testMatrix4 :");
    System.out.println(testMatrix4);

    double[][] mySecondArray = { { 1, 2, 3 }, { 4, 12, 6 }, { 7, 8, 11 } };

    Matrix testMatrix5 = new Matrix(mySecondArray);

    System.out.println("My testMatrix5 :");
    System.out.println(testMatrix5);

    System.out.println("Result of testMatrix2.equals(testMatrix3): " + testMatrix2.equals(testMatrix3));
    System.out.println("Result of testMatrix.equals(testMatrix3): " + testMatrix.equals(testMatrix3));
    System.out.println("Result of testMatrix3.equals(testMatrix4): " + testMatrix3.equals(testMatrix4));
    try {
        System.out.println(
                "\nResult of Matrix.mult(testMatrix4, testMatrix3):\n" + Matrix.mult(testMatrix4, testMatrix3));
        System.out.println("\nResult of testMatrix3.mult(testMatrix4):\n" + testMatrix3.mult(testMatrix4));
        System.out.println("\nResult of testMatrix4.mult(testMatrix5):\n" + testMatrix4.mult(testMatrix5));
        System.out.println("\nResult of testMatrix5.mult(testMatrix4):\n" + testMatrix5.mult(testMatrix4));
    } catch (DimensionMismatchException ex) {
        System.out.println(ex.getMessage());
    }

    SquareMatrix testSquare1 = new SquareMatrix(testMatrix5);
    try {
        LUPDecomposition testDecompose = new LUPDecomposition();
        testDecompose.decompose(testSquare1);
    } catch (NonInvertibleMatrixException ex) {
        System.out.println("LUDecomposition of testDecompose object threw exception:\n" + ex.getMessage());
    }

    double[][] myThirdArray = { { 7, 4, 2, 0 }, { 6, 3, -1, 2 }, { 4, 6, 2, 5 }, { 8, 2, -7, 1 } };

    SquareMatrix testSquare2 = new SquareMatrix(new Matrix(myThirdArray));

    try {
        LUPDecomposition testDecompose2 = new LUPDecomposition();
        testDecompose2.decompose(testSquare2);
    } catch (NonInvertibleMatrixException ex) {
        System.out.println("LUDecomposition of testDecompose2 object threw exception:\n" + ex.getMessage());
    }

    System.out.println("Smoke test for testSquare2.determinant():\t" + testSquare2.determinant());
    System.out.println("Second smoke test for testSquare1.determinant():\t" + testSquare1.determinant() + "\n");
    try {
        SquareMatrix InvertedTestSquare2 = SquareMatrix.invert(testSquare2);
        System.out.println("InvertedTestSquare2:\n" + InvertedTestSquare2);
        System.out.println("Result of testSquare2.mult(InvertedTestSquare2): (should be identity matrix)\n"
                + testSquare2.mult(InvertedTestSquare2));
        System.out.println("Result of InvertedTestSquare2.mult(TestSquare2): (should be identity matrix)\n"
                + InvertedTestSquare2.mult(testSquare2));
    } catch (NonInvertibleMatrixException ex) {
        System.out.println("Inverting matrix testSquare2 threw exception:\n" + ex.getMessage());
    } catch (DimensionMismatchException ex) {
        System.out.println("Multiplying Matrix failed. (shouldn't have happened, exiting");
        System.exit(-1);
    }

    SquareMatrix testSquare3 = new SquareMatrix(Matrix.transpose(testSquare2));

    System.out.println(
            "testSquare2:\n" + testSquare2 + "\ntestSquare3: (transpose of testSquare2)\n" + testSquare3);
    try {
        System.out.println("Result of testSquare2.subtract(testSquare3)\n" + testSquare2.subtract(testSquare3));
    } catch (DimensionMismatchException ex) {
        System.out.println("Subtracting Matrix failed. (shouldn't have happened, exiting");
        System.exit(-1);
    }

    /*float[][] fail = {
     {1, 2, 3},
     {4, 5, 7},
     {3, 6, 8},
     {4, 5, 9}
     };*/
    //Matrix goodMatrix = new Matrix(fail);
    //SquareMatrix badMatrix = new SquareMatrix(goodMatrix);
    //System.out.print(badMatrix);
    Matrix random = Matrix.random(3, 5, 100, 1);

    System.out.println("random matrix: (should be 3x5)\n" + random);

    int BMFsize = 500;
    System.out.println("smoke checking my mult method with " + BMFsize + "x" + BMFsize + " matrices.\n");

    Matrix big1 = Matrix.random(BMFsize, BMFsize, 2, -2);
    Matrix big2 = Matrix.random(BMFsize, BMFsize, 2, -2);
    SquareMatrix big3 = null;

    try {
        big3 = new SquareMatrix(big1.mult(big2));
    } catch (DimensionMismatchException ex) {
        System.out.println("this exception shouldn't have happened, bailing");
        System.exit(-1);
    }

    System.out.println("smoke checking determinant method with " + BMFsize + "x" + BMFsize + " matrix.\n");
    System.out.println("Result of big3.determinant():\t" + big3.determinant() + "\n");

    System.out.println("Performing basic check of QR Decomp with known 4x4 matrix.\n");

    double[][] qrTest = { { 4, 8, 9, 1 }, { 2, -3, 1, -1 }, { 8, 15, 3, 1 }, { 7, 1, 2, 3 } };

    Matrix QRTest = new Matrix(qrTest);

    QRDecomposition qr = new QRDecomposition();

    qr.decompose(QRTest);

    System.out.println("\nQ:\n" + qr.Q + "\nR:\n" + qr.R + "\nQRtest: (original matrix)\n" + QRTest);

    try {
        System.out.println("Result of Q.mult(R): (should be the original matrix)\n" + qr.Q.mult(qr.R));
    } catch (DimensionMismatchException ex) {
        System.out.println("this exception shouldn't have happened, bailing");
        System.exit(-1);
    }

    try {
        System.out.println("Result of Q.mult(Q.transpose()): (should be the identity matrix)\n"
                + qr.Q.mult(qr.Q.transpose()));
    } catch (DimensionMismatchException ex) {
        System.out.println("this exception shouldn't have happened, bailing");
        System.exit(-1);
    }

    /*System.out.println("Beginning initial testing of eigenvalue function on QRTest:\n" + QRTest);
            
     Complex[] eigen = SquareMatrix.eigenvalues(new SquareMatrix(QRTest));
            
     for (Complex e : eigen) {
     System.out.println("eigenvalue:" + e);
     }*/
    int SMFSize = 10;

    //SquareMatrix big4 = new SquareMatrix(Matrix.random(SMFSize, SMFSize, -5, 5));
    //this matrix is causing a bug, using it to troubleshooot
    double[][] big4Matrix = { { -2.476, -2.814, 4.29, -3.649 }, { 2.839, -2.859, 1.623, -2.926 },
            { -0.392, -3.206, -0.401, -2.174 }, { 2.241, -4.435, -3.963, 4.102 } };
    SquareMatrix big4 = new SquareMatrix(big4Matrix);

    if (SMFSize < 6) {
        System.out.println(
                "Smoke test of eigenvalue function with " + SMFSize + "x" + SMFSize + " matrix:\n" + big4);
    } //only print it if it will fit on the screen
    Complex eigen2[] = big4.eigenvalues();

    //System.out.println(big4.toCopyableString());//used for debugging
    int eig = 1;
    for (Complex e : eigen2) {
        System.out.println("eigenvalue #" + eig++ + ":\t" + Precision.round(e.getReal(), 3) + " + "
                + Precision.round(e.getImaginary(), 3) + "i");
    }

    double[] vector = new double[5];
    double[] vector2 = new double[5];

    Matrix v1 = new Matrix(vector, MatrixConstants.ROW_VECTOR);
    Matrix v2 = new Matrix(vector2, MatrixConstants.COLUMN_VECTOR);

    System.out.println("\nv1: (should be 1x5)\n" + v1 + "\nv2: (should be 5x1)\n" + v2);

    double[][] qrTest2 = { { 4, 2, 2, 1 }, { 2, -3, 1, 1 }, { 2, 1, 3, 1 }, { 1, 1, 1, 2 } };

    Matrix QRTest2 = new Matrix(qrTest2);

    System.out.println("Testing hessenberg() with QRTest2:\n" + QRTest2);

    System.out.println("Hessenberg output for QRTest2:\n" + qr.hessenberg(QRTest2));

    int big6size = 5;
    SquareMatrix big6 = new SquareMatrix(Matrix.random(big6size, big6size, -5, 5));

    System.out.println("Testing hessenberg() with big6:\n" + big6);

    System.out.println("Hessenberg output with big6:\n" + qr.hessenberg(big6));

    //System.out.println(big6.toCopyableString());
}

From source file:com.datatorrent.netlet.benchmark.util.BenchmarkResults.java

private static String fromNanoTime(double nanoTime) {
    if (nanoTime > 1000000d) {
        return Precision.round(nanoTime / 1000000d, PRECISION) + " millis";
    } else if (nanoTime > 1000d) {
        return Precision.round(nanoTime / 1000d, PRECISION) + " micros";
    } else {/*from  w  ww.ja  va2s .c om*/
        return Precision.round(nanoTime, PRECISION) + " nanos";
    }
}

From source file:Math.MathCalc.java

/**
 * Return rounded double number/*  ww  w .  j  a v  a 2 s.c om*/
 *
 * @param num the number to round
 * @param decimalPlaces decimal places to cut the number
 * @return rounded double number
 */
public static double Round(double num, int decimalPlaces) {
    return Precision.round(num, decimalPlaces);
}

From source file:co.turnus.analysis.util.AnalysisUtil.java

public static double round(double a) {
    return Precision.round(a, PRECISION_SCALE);
}

From source file:br.prof.salesfilho.oci.image.GraphicBuilder.java

public void addSeire(double[] values, String serieName) {
    final XYSeries serie = new XYSeries(serieName);
    for (int i = 0; i < values.length; i++) {
        serie.add(i, Precision.round(values[i], 2));
    }/*w w  w. j  a va  2 s .com*/
    collectionDataset.addSeries(serie);
}

From source file:cc.kave.episodes.statistics.EpisodesStatistics.java

public Map<Double, Integer> bidirectEpisodes(Set<Episode> episodes, int frequency) {
    Map<Double, Integer> total = initBd(episodes, frequency);

    for (Episode ep : episodes) {
        if (!valid(ep, frequency)) {
            continue;
        }//from  w w  w .  j  ava2s  .  c o m
        double bd = ep.getBidirectMeasure();
        double roundBd = Precision.round(bd, ROUNDVALUE);
        int count = total.get(roundBd);
        total.put(roundBd, count + 1);

        if (roundBd > 0.1) {
            for (double distr = bd - 0.001; distr > 0.0999; distr -= 0.001) {
                double tempDistr = Precision.round(distr, ROUNDVALUE);
                if (total.containsKey(tempDistr)) {
                    int tempCount = total.get(tempDistr);
                    total.put(tempDistr, tempCount + 1);
                }
            }
        }
    }
    return total;
}

From source file:io.rhiot.component.gp2y1010au0f.Gp2y1010au0fConsumer.java

@Override
protected int poll() throws Exception {
    int iterations = 10;
    double sum = 0.0;

    for (int i = 0; i < iterations; i++) {
        getEndpoint().getIledPin().high();
        TimeUnit.MICROSECONDS.sleep(getEndpoint().getSamplingDelay());
        double adcValue = getEndpoint().getMcp3008GpioProvider().getValue(getEndpoint().getAnalogPin());
        getEndpoint().getIledPin().low();

        double voltage = (REF_VOLTAGE / 1023.0) * adcValue * 11;

        if (voltage > MIN_VOLTAGE) {
            sum += (voltage - MIN_VOLTAGE) * 0.2;
        }/*ww  w .  j  av  a 2  s .c  o  m*/

        TimeUnit.MILLISECONDS.sleep(50);
    }

    double average = sum / iterations;

    if (average > 0) {
        Exchange exchange = ExchangeBuilder.anExchange(getEndpoint().getCamelContext())
                .withBody(Precision.round(average, 2)).build();
        exchange.setFromEndpoint(getEndpoint());
        getProcessor().process(exchange);
        return 1;
    }

    return 0;
}

From source file:br.prof.salesfilho.oci.image.GraphicBuilder.java

public void createCombinedChart(Map<String, double[]> mapSeries, String legendTitle) {
    XYSeriesCollection xds;// ww w .j  ava 2  s  .  c  o m
    final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new NumberAxis(legendTitle));
    plot.setGap(10.0);
    for (Map.Entry<String, double[]> entrySet : mapSeries.entrySet()) {
        String serieName = entrySet.getKey();
        double[] values = entrySet.getValue();

        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();

        final XYSeries series = new XYSeries(serieName);
        for (int i = 0; i < values.length; i++) {
            series.add(i, Precision.round(values[i], 2));
            renderer.setSeriesVisible(i, true, true);
            renderer.setSeriesShapesVisible(i, true);
        }
        xds = new XYSeriesCollection();
        xds.addSeries(series);

        final NumberAxis rangeAxis = new NumberAxis(serieName);

        final XYPlot subplot = new XYPlot(xds, null, rangeAxis, renderer);
        subplot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        plot.add(subplot);

    }
    this.chart = new JFreeChart(this.title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
    chartPanel.setChart(chart);
}

From source file:com.itemanalysis.psychometrics.factoranalysis.GPArotationTest.java

@Test
public void testM255Varimax() {
    System.out.println("Varimax rotation test: m255 data");

    /**/*from w w  w. jav a2  s  .  c  o m*/
     * True result obtained form R using GPArotation package
     */
    double[][] true_Varimax = { { 0.2806153, 0.1303408, 0.7332729 }, { 0.2620153, 0.1714751, 0.7434001 },
            { 0.3129422, 0.2735077, 0.6396424 }, { 0.2277365, 0.2707234, 0.5740148 },
            { 0.3650465, 0.4339803, 0.5203103 }, { 0.3193267, 0.7036671, 0.2683278 },
            { 0.2056908, 0.7280409, 0.1543441 }, { 0.2398168, 0.4488439, 0.2356959 },
            { 0.4424600, 0.4637861, 0.3346261 }, { 0.3748683, 0.4973271, 0.1762550 },
            { 0.7710955, 0.2932897, 0.3783441 }, { 0.6437897, 0.2491322, 0.2953552 }, };

    RealMatrix L = new Array2DRowRealMatrix(m255MINRESLoadings);
    GPArotation gpa = new GPArotation();
    RotationResults R = gpa.rotate(L, RotationMethod.VARIMAX, false, 1000, 1e-5);
    RealMatrix Lr = R.getFactorLoadings();
    //        System.out.println(R.toString());

    for (int i = 0; i < Lr.getRowDimension(); i++) {
        for (int j = 0; j < Lr.getColumnDimension(); j++) {
            assertEquals("  loading: ", Precision.round(true_Varimax[i][j], 4),
                    Precision.round(Lr.getEntry(i, j), 5), 1e-4);
        }

    }

}

From source file:cc.kave.episodes.statistics.EpisodesStatistics.java

private Map<Double, Integer> initBd(Set<Episode> episodes, int freqThresh) {
    Map<Double, Integer> initializer = Maps.newLinkedHashMap();

    for (Episode ep : episodes) {
        int epFreq = ep.getFrequency();
        if (epFreq < freqThresh) {
            continue;
        }//w ww  .jav a2s.  com
        double bd = ep.getBidirectMeasure();
        double reoundBd = Precision.round(bd, ROUNDVALUE);
        if (!initializer.containsKey(reoundBd)) {
            initializer.put(reoundBd, 0);
        }
    }
    return initializer;
}