Java Code Examples for org.apache.commons.math.linear.MatrixUtils#createRealMatrix()

The following examples show how to use org.apache.commons.math.linear.MatrixUtils#createRealMatrix() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VectorialCovariance.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get the covariance matrix.
 * @return covariance matrix
 */
public RealMatrix getResult() {

    int dimension = sums.length;
    RealMatrix result = MatrixUtils.createRealMatrix(dimension, dimension);

    if (n > 1) {
        double c = 1.0 / (n * (isBiasCorrected ? (n - 1) : n));
        int k = 0;
        for (int i = 0; i < dimension; ++i) {
            for (int j = 0; j <= i; ++j) {
                double e = c * (n * productsSums[k++] - sums[i] * sums[j]);
                result.setEntry(i, j, e);
                result.setEntry(j, i, e);
            }
        }
    }

    return result;

}
 
Example 2
Source File: BiDiagonalTransformerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testMatricesValues() {
   BiDiagonalTransformer transformer =
        new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare));
   final double s17 = Math.sqrt(17.0);
    RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] {
            {  -8 / (5 * s17), 19 / (5 * s17) },
            { -19 / (5 * s17), -8 / (5 * s17) }
    });
    RealMatrix bRef = MatrixUtils.createRealMatrix(new double[][] {
            { -3 * s17 / 5, 32 * s17 / 85 },
            {      0.0,     -5 * s17 / 17 }
    });
    RealMatrix vRef = MatrixUtils.createRealMatrix(new double[][] {
            { 1.0,  0.0 },
            { 0.0, -1.0 }
    });

    // check values against known references
    RealMatrix u = transformer.getU();
    Assert.assertEquals(0, u.subtract(uRef).getNorm(), 1.0e-14);
    RealMatrix b = transformer.getB();
    Assert.assertEquals(0, b.subtract(bRef).getNorm(), 1.0e-14);
    RealMatrix v = transformer.getV();
    Assert.assertEquals(0, v.subtract(vRef).getNorm(), 1.0e-14);

    // check the same cached instance is returned the second time
    Assert.assertTrue(u == transformer.getU());
    Assert.assertTrue(b == transformer.getB());
    Assert.assertTrue(v == transformer.getV());

}
 
Example 3
Source File: CholeskyDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test non positive definite matrix */
@Test(expected = NotPositiveDefiniteMatrixException.class)
public void testNotPositiveDefinite() throws MathException {
    new CholeskyDecompositionImpl(MatrixUtils.createRealMatrix(new double[][] {
            { 14, 11, 13, 15, 24 },
            { 11, 34, 13, 8,  25 },
            { 13, 13, 14, 15, 21 },
            { 15, 8,  15, 18, 23 },
            { 24, 25, 21, 23, 45 }
    }));
}
 
Example 4
Source File: LUDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test that U is upper triangular */
public void testUUpperTriangular() {
    RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
    RealMatrix u = new LUDecompositionImpl(matrix).getU();
    for (int i = 0; i < u.getRowDimension(); i++) {
        for (int j = 0; j < i; j++) {
            assertEquals(u.getEntry(i, j), 0, entryTolerance);
        }
    }
}
 
Example 5
Source File: CholeskyDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test non-symmetric matrix */
@Test(expected = NotSymmetricMatrixException.class)
public void testNotSymmetricMatrixException() throws MathException {
    double[][] changed = testData.clone();
    changed[0][changed[0].length - 1] += 1.0e-5;
    new CholeskyDecompositionImpl(MatrixUtils.createRealMatrix(changed));
}
 
Example 6
Source File: CorrelatedRandomVectorGeneratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public CorrelatedRandomVectorGeneratorTest() {
    mean = new double[] { 0.0, 1.0, -3.0, 2.3 };

    RealMatrix b = MatrixUtils.createRealMatrix(4, 3);
    int counter = 0;
    for (int i = 0; i < b.getRowDimension(); ++i) {
        for (int j = 0; j < b.getColumnDimension(); ++j) {
            b.setEntry(i, j, 1.0 + 0.1 * ++counter);
        }
    }
    RealMatrix bbt = b.multiply(b.transpose());
    covariance = MatrixUtils.createRealMatrix(mean.length, mean.length);
    for (int i = 0; i < covariance.getRowDimension(); ++i) {
        covariance.setEntry(i, i, bbt.getEntry(i, i));
        for (int j = 0; j < covariance.getColumnDimension(); ++j) {
            double s = bbt.getEntry(i, j);
            covariance.setEntry(i, j, s);
            covariance.setEntry(j, i, s);
        }
    }

    RandomGenerator rg = new JDKRandomGenerator();
    rg.setSeed(17399225432l);
    GaussianRandomGenerator rawGenerator = new GaussianRandomGenerator(rg);
    generator = new CorrelatedRandomVectorGenerator(mean,
                                                    covariance,
                                                    1.0e-12 * covariance.getNorm(),
                                                    rawGenerator);
}
 
Example 7
Source File: SingularValueDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test matrices values */
public void testMatricesValues1() {
   SingularValueDecomposition svd =
        new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare));
    RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] {
            { 3.0 / 5.0, -4.0 / 5.0 },
            { 4.0 / 5.0,  3.0 / 5.0 }
    });
    RealMatrix sRef = MatrixUtils.createRealMatrix(new double[][] {
            { 3.0, 0.0 },
            { 0.0, 1.0 }
    });
    RealMatrix vRef = MatrixUtils.createRealMatrix(new double[][] {
            { 4.0 / 5.0,  3.0 / 5.0 },
            { 3.0 / 5.0, -4.0 / 5.0 }
    });

    // check values against known references
    RealMatrix u = svd.getU();
    assertEquals(0, u.subtract(uRef).getNorm(), normTolerance);
    RealMatrix s = svd.getS();
    assertEquals(0, s.subtract(sRef).getNorm(), normTolerance);
    RealMatrix v = svd.getV();
    assertEquals(0, v.subtract(vRef).getNorm(), normTolerance);

    // check the same cached instance is returned the second time
    assertTrue(u == svd.getU());
    assertTrue(s == svd.getS());
    assertTrue(v == svd.getV());

}
 
Example 8
Source File: CholeskyDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected = NotPositiveDefiniteMatrixException.class)
public void testMath274() throws MathException {
    new CholeskyDecompositionImpl(MatrixUtils.createRealMatrix(new double[][] {
            { 0.40434286, -0.09376327, 0.30328980, 0.04909388 },
            {-0.09376327,  0.10400408, 0.07137959, 0.04762857 },
            { 0.30328980,  0.07137959, 0.30458776, 0.04882449 },
            { 0.04909388,  0.04762857, 0.04882449, 0.07543265 }

    }));
}
 
Example 9
Source File: CholeskyDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test that L is lower triangular */
@Test
public void testLLowerTriangular() throws MathException {
    RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
    RealMatrix l = new CholeskyDecompositionImpl(matrix).getL();
    for (int i = 0; i < l.getRowDimension(); i++) {
        for (int j = i + 1; j < l.getColumnDimension(); j++) {
            assertEquals(0.0, l.getEntry(i, j), 0.0);
        }
    }
}
 
Example 10
Source File: CholeskyDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected = NotPositiveDefiniteMatrixException.class)
public void testMath274() throws MathException {
    new CholeskyDecompositionImpl(MatrixUtils.createRealMatrix(new double[][] {
            { 0.40434286, -0.09376327, 0.30328980, 0.04909388 },
            {-0.09376327,  0.10400408, 0.07137959, 0.04762857 },
            { 0.30328980,  0.07137959, 0.30458776, 0.04882449 },
            { 0.04909388,  0.04762857, 0.04882449, 0.07543265 }

    }));
}
 
Example 11
Source File: QRDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test matrices values */
public void testMatricesValues() {
    QRDecomposition qr =
        new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x3NonSingular));
    RealMatrix qRef = MatrixUtils.createRealMatrix(new double[][] {
            { -12.0 / 14.0,   69.0 / 175.0,  -58.0 / 175.0 },
            {  -6.0 / 14.0, -158.0 / 175.0,    6.0 / 175.0 },
            {   4.0 / 14.0,  -30.0 / 175.0, -165.0 / 175.0 }
    });
    RealMatrix rRef = MatrixUtils.createRealMatrix(new double[][] {
            { -14.0,  -21.0, 14.0 },
            {   0.0, -175.0, 70.0 },
            {   0.0,    0.0, 35.0 }
    });
    RealMatrix hRef = MatrixUtils.createRealMatrix(new double[][] {
            { 26.0 / 14.0, 0.0, 0.0 },
            {  6.0 / 14.0, 648.0 / 325.0, 0.0 },
            { -4.0 / 14.0,  36.0 / 325.0, 2.0 }
    });

    // check values against known references
    RealMatrix q = qr.getQ();
    assertEquals(0, q.subtract(qRef).getNorm(), 1.0e-13);
    RealMatrix qT = qr.getQT();
    assertEquals(0, qT.subtract(qRef.transpose()).getNorm(), 1.0e-13);
    RealMatrix r = qr.getR();
    assertEquals(0, r.subtract(rRef).getNorm(), 1.0e-13);
    RealMatrix h = qr.getH();
    assertEquals(0, h.subtract(hRef).getNorm(), 1.0e-13);

    // check the same cached instance is returned the second time
    assertTrue(q == qr.getQ());
    assertTrue(r == qr.getR());
    assertTrue(h == qr.getH());
    
}
 
Example 12
Source File: SingularValueDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test matrices values */
public void testMatricesValues2() {

    RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] {
        {  0.0 / 5.0,  3.0 / 5.0,  0.0 / 5.0 },
        { -4.0 / 5.0,  0.0 / 5.0, -3.0 / 5.0 },
        {  0.0 / 5.0,  4.0 / 5.0,  0.0 / 5.0 },
        { -3.0 / 5.0,  0.0 / 5.0,  4.0 / 5.0 }
    });
    RealMatrix sRef = MatrixUtils.createRealMatrix(new double[][] {
        { 4.0, 0.0, 0.0 },
        { 0.0, 3.0, 0.0 },
        { 0.0, 0.0, 2.0 }
    });
    RealMatrix vRef = MatrixUtils.createRealMatrix(new double[][] {
        {  80.0 / 125.0,  -60.0 / 125.0, 75.0 / 125.0 },
        {  24.0 / 125.0,  107.0 / 125.0, 60.0 / 125.0 },
        { -93.0 / 125.0,  -24.0 / 125.0, 80.0 / 125.0 }
    });

    // check values against known references
    SingularValueDecomposition svd =
        new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testNonSquare));
    RealMatrix u = svd.getU();
    assertEquals(0, u.subtract(uRef).getNorm(), normTolerance);
    RealMatrix s = svd.getS();
    assertEquals(0, s.subtract(sRef).getNorm(), normTolerance);
    RealMatrix v = svd.getV();
    assertEquals(0, v.subtract(vRef).getNorm(), normTolerance);

    // check the same cached instance is returned the second time
    assertTrue(u == svd.getU());
    assertTrue(s == svd.getS());
    assertTrue(v == svd.getV());

}
 
Example 13
Source File: CholeskyDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test non positive definite matrix */
@Test(expected = NotPositiveDefiniteMatrixException.class)
public void testNotPositiveDefinite() throws MathException {
    new CholeskyDecompositionImpl(MatrixUtils.createRealMatrix(new double[][] {
            { 14, 11, 13, 15, 24 },
            { 11, 34, 13, 8,  25 },
            { 13, 13, 14, 15, 21 },
            { 15, 8,  15, 18, 23 },
            { 24, 25, 21, 23, 45 }
    }));
}
 
Example 14
Source File: CholeskyDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test that LT is transpose of L */
@Test
public void testLTTransposed() throws MathException {
    RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
    CholeskyDecomposition llt = new CholeskyDecompositionImpl(matrix);
    RealMatrix l  = llt.getL();
    RealMatrix lt = llt.getLT();
    double norm = l.subtract(lt.transpose()).getNorm();
    assertEquals(0, norm, 1.0e-15);
}
 
Example 15
Source File: CholeskyDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected = NotPositiveDefiniteMatrixException.class)
public void testMath274() throws MathException {
    new CholeskyDecompositionImpl(MatrixUtils.createRealMatrix(new double[][] {
            { 0.40434286, -0.09376327, 0.30328980, 0.04909388 },
            {-0.09376327,  0.10400408, 0.07137959, 0.04762857 },
            { 0.30328980,  0.07137959, 0.30458776, 0.04882449 },
            { 0.04909388,  0.04762857, 0.04882449, 0.07543265 }
        
    }));
}
 
Example 16
Source File: SingularValueDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test matrices values */
public void testMatricesValues1() {
   SingularValueDecomposition svd =
        new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare));
    RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] {
            { 3.0 / 5.0, -4.0 / 5.0 },
            { 4.0 / 5.0,  3.0 / 5.0 }
    });
    RealMatrix sRef = MatrixUtils.createRealMatrix(new double[][] {
            { 3.0, 0.0 },
            { 0.0, 1.0 }
    });
    RealMatrix vRef = MatrixUtils.createRealMatrix(new double[][] {
            { 4.0 / 5.0,  3.0 / 5.0 },
            { 3.0 / 5.0, -4.0 / 5.0 }
    });

    // check values against known references
    RealMatrix u = svd.getU();
    assertEquals(0, u.subtract(uRef).getNorm(), normTolerance);
    RealMatrix s = svd.getS();
    assertEquals(0, s.subtract(sRef).getNorm(), normTolerance);
    RealMatrix v = svd.getV();
    assertEquals(0, v.subtract(vRef).getNorm(), normTolerance);

    // check the same cached instance is returned the second time
    assertTrue(u == svd.getU());
    assertTrue(s == svd.getS());
    assertTrue(v == svd.getV());
    
}
 
Example 17
Source File: LUDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test that L is lower triangular with unit diagonal */
public void testLLowerTriangular() {
    RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
    RealMatrix l = new LUDecompositionImpl(matrix).getL();
    for (int i = 0; i < l.getRowDimension(); i++) {
        assertEquals(l.getEntry(i, i), 1, entryTolerance);
        for (int j = i + 1; j < l.getColumnDimension(); j++) {
            assertEquals(l.getEntry(i, j), 0, entryTolerance);
        }
    }
}
 
Example 18
Source File: CholeskyDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test that L is lower triangular */
@Test
public void testLLowerTriangular() throws MathException {
    RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
    RealMatrix l = new CholeskyDecompositionImpl(matrix).getL();
    for (int i = 0; i < l.getRowDimension(); i++) {
        for (int j = i + 1; j < l.getColumnDimension(); j++) {
            assertEquals(0.0, l.getEntry(i, j), 0.0);
        }
    }
}
 
Example 19
Source File: BiDiagonalTransformerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testSingularMatrix() {
   BiDiagonalTransformer transformer =
        new BiDiagonalTransformer(MatrixUtils.createRealMatrix(new double[][] {
            { 1.0, 2.0, 3.0 },
            { 2.0, 3.0, 4.0 },
            { 3.0, 5.0, 7.0 }
        }));
   final double s3  = Math.sqrt(3.0);
   final double s14 = Math.sqrt(14.0);
   final double s1553 = Math.sqrt(1553.0);
   RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] {
       {  -1.0 / s14,  5.0 / (s3 * s14),  1.0 / s3 },
       {  -2.0 / s14, -4.0 / (s3 * s14),  1.0 / s3 },
       {  -3.0 / s14,  1.0 / (s3 * s14), -1.0 / s3 }
   });
   RealMatrix bRef = MatrixUtils.createRealMatrix(new double[][] {
       { -s14, s1553 / s14,   0.0 },
       {  0.0, -87 * s3 / (s14 * s1553), -s3 * s14 / s1553 },
       {  0.0, 0.0, 0.0 }
   });
   RealMatrix vRef = MatrixUtils.createRealMatrix(new double[][] {
       { 1.0,   0.0,         0.0        },
       { 0.0,  -23 / s1553,  32 / s1553 },
       { 0.0,  -32 / s1553, -23 / s1553 }
   });

   // check values against known references
   RealMatrix u = transformer.getU();
   Assert.assertEquals(0, u.subtract(uRef).getNorm(), 1.0e-14);
   RealMatrix b = transformer.getB();
   Assert.assertEquals(0, b.subtract(bRef).getNorm(), 1.0e-14);
   RealMatrix v = transformer.getV();
   Assert.assertEquals(0, v.subtract(vRef).getNorm(), 1.0e-14);

   // check the same cached instance is returned the second time
   Assert.assertTrue(u == transformer.getU());
   Assert.assertTrue(b == transformer.getB());
   Assert.assertTrue(v == transformer.getV());

}
 
Example 20
Source File: EigenSolverTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** test solve */
public void testSolve() {
    RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {
            { 91,  5, 29, 32, 40, 14 },
            {  5, 34, -1,  0,  2, -1 },
            { 29, -1, 12,  9, 21,  8 },
            { 32,  0,  9, 14,  9,  0 },
            { 40,  2, 21,  9, 51, 19 },
            { 14, -1,  8,  0, 19, 14 }
    });
    DecompositionSolver es = new EigenDecompositionImpl(m, MathUtils.SAFE_MIN).getSolver();
    RealMatrix b = MatrixUtils.createRealMatrix(new double[][] {
            { 1561, 269, 188 },
            {   69, -21,  70 },
            {  739, 108,  63 },
            {  324,  86,  59 },
            { 1624, 194, 107 },
            {  796,  69,  36 }
    });
    RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] {
            { 1,   2, 1 },
            { 2,  -1, 2 },
            { 4,   2, 3 },
            { 8,  -1, 0 },
            { 16,  2, 0 },
            { 32, -1, 0 }
    });

    // using RealMatrix
    assertEquals(0, es.solve(b).subtract(xRef).getNorm(), 2.0e-12);

    // using double[]
    for (int i = 0; i < b.getColumnDimension(); ++i) {
        assertEquals(0,
                     new ArrayRealVector(es.solve(b.getColumn(i))).subtract(xRef.getColumnVector(i)).getNorm(),
                     2.0e-11);
    }

    // using Array2DRowRealMatrix
    for (int i = 0; i < b.getColumnDimension(); ++i) {
        assertEquals(0,
                     es.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(),
                     2.0e-11);
    }

    // using RealMatrix with an alternate implementation
    for (int i = 0; i < b.getColumnDimension(); ++i) {
        ArrayRealVectorTest.RealVectorTestImpl v =
            new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(i));
        assertEquals(0,
                     es.solve(v).subtract(xRef.getColumnVector(i)).getNorm(),
                     2.0e-11);
    }

}