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 };
}
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());
}
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());
}
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);
}
}
}
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;
}
Aggregations