Search in sources :

Example 1 with QRDecomposition

use of org.apache.commons.math3.linear.QRDecomposition in project incubator-systemml by apache.

the class LibCommonsMath method computeQR.

/**
	 * Function to perform QR decomposition on a given matrix.
	 * 
	 * @param in matrix object
	 * @return array of matrix blocks
	 * @throws DMLRuntimeException if DMLRuntimeException occurs
	 */
private static MatrixBlock[] computeQR(MatrixObject in) throws DMLRuntimeException {
    Array2DRowRealMatrix matrixInput = DataConverter.convertToArray2DRowRealMatrix(in);
    // Perform QR decomposition
    QRDecomposition qrdecompose = new QRDecomposition(matrixInput);
    RealMatrix H = qrdecompose.getH();
    RealMatrix R = qrdecompose.getR();
    // Read the results into native format
    MatrixBlock mbH = DataConverter.convertToMatrixBlock(H.getData());
    MatrixBlock mbR = DataConverter.convertToMatrixBlock(R.getData());
    return new MatrixBlock[] { mbH, mbR };
}
Also used : QRDecomposition(org.apache.commons.math3.linear.QRDecomposition) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix)

Example 2 with QRDecomposition

use of org.apache.commons.math3.linear.QRDecomposition in project incubator-systemml by apache.

the class LibCommonsMath method computeSolve.

/**
	 * Function to solve a given system of equations.
	 * 
	 * @param in1 matrix object 1
	 * @param in2 matrix object 2
	 * @return matrix block
	 * @throws DMLRuntimeException if DMLRuntimeException occurs
	 */
private static MatrixBlock computeSolve(MatrixObject in1, MatrixObject in2) throws DMLRuntimeException {
    Array2DRowRealMatrix matrixInput = DataConverter.convertToArray2DRowRealMatrix(in1);
    Array2DRowRealMatrix vectorInput = DataConverter.convertToArray2DRowRealMatrix(in2);
    /*LUDecompositionImpl ludecompose = new LUDecompositionImpl(matrixInput);
		DecompositionSolver lusolver = ludecompose.getSolver();
		RealMatrix solutionMatrix = lusolver.solve(vectorInput);*/
    // Setup a solver based on QR Decomposition
    QRDecomposition qrdecompose = new QRDecomposition(matrixInput);
    DecompositionSolver solver = qrdecompose.getSolver();
    // Invoke solve
    RealMatrix solutionMatrix = solver.solve(vectorInput);
    return DataConverter.convertToMatrixBlock(solutionMatrix.getData());
}
Also used : QRDecomposition(org.apache.commons.math3.linear.QRDecomposition) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) DecompositionSolver(org.apache.commons.math3.linear.DecompositionSolver)

Example 3 with QRDecomposition

use of org.apache.commons.math3.linear.QRDecomposition in project incubator-systemml by apache.

the class LibCommonsMath method computeMatrixInverse.

/**
	 * Function to compute matrix inverse via matrix decomposition.
	 * 
	 * @param in commons-math3 Array2DRowRealMatrix
	 * @return matrix block
	 * @throws DMLRuntimeException if DMLRuntimeException occurs
	 */
private static MatrixBlock computeMatrixInverse(Array2DRowRealMatrix in) throws DMLRuntimeException {
    if (!in.isSquare())
        throw new DMLRuntimeException("Input to inv() must be square matrix -- given: a " + in.getRowDimension() + "x" + in.getColumnDimension() + " matrix.");
    QRDecomposition qrdecompose = new QRDecomposition(in);
    DecompositionSolver solver = qrdecompose.getSolver();
    RealMatrix inverseMatrix = solver.getInverse();
    return DataConverter.convertToMatrixBlock(inverseMatrix.getData());
}
Also used : QRDecomposition(org.apache.commons.math3.linear.QRDecomposition) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) DecompositionSolver(org.apache.commons.math3.linear.DecompositionSolver) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 4 with QRDecomposition

use of org.apache.commons.math3.linear.QRDecomposition in project gatk by broadinstitute.

the class DecomposeSingularValuesIntegrationTest method assertUnitaryMatrix.

/**
     * Assert that the given matrix is unitary.
     * @param m
     */
public static void assertUnitaryMatrix(final RealMatrix m) {
    final RealMatrix mInv = new QRDecomposition(m).getSolver().getInverse();
    final RealMatrix mT = m.transpose();
    for (int i = 0; i < mInv.getRowDimension(); i++) {
        for (int j = 0; j < mInv.getColumnDimension(); j++) {
            Assert.assertEquals(mInv.getEntry(i, j), mT.getEntry(i, j), 1e-7);
        }
    }
}
Also used : QRDecomposition(org.apache.commons.math3.linear.QRDecomposition) RealMatrix(org.apache.commons.math3.linear.RealMatrix)

Example 5 with QRDecomposition

use of org.apache.commons.math3.linear.QRDecomposition in project gatk by broadinstitute.

the class SingularValueDecomposerUnitTest method isUnitaryMatrix.

/**
     * Check that the given matrix is unitary.
     */
public static boolean isUnitaryMatrix(final RealMatrix m) {
    //Note can't use MatrixUtils.inverse because m may not be square
    final RealMatrix mInv = new QRDecomposition(m).getSolver().getInverse();
    final RealMatrix mT = m.transpose();
    for (int i = 0; i < mInv.getRowDimension(); i++) {
        for (int j = 0; j < mInv.getColumnDimension(); j++) {
            if (Math.abs(mInv.getEntry(i, j) - mT.getEntry(i, j)) > 1.0e-7) {
                return false;
            }
        }
    }
    return true;
}
Also used : QRDecomposition(org.apache.commons.math3.linear.QRDecomposition) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix)

Aggregations

QRDecomposition (org.apache.commons.math3.linear.QRDecomposition)6 RealMatrix (org.apache.commons.math3.linear.RealMatrix)6 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)4 DecompositionSolver (org.apache.commons.math3.linear.DecompositionSolver)2 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)1