Java Code Examples for org.apache.commons.math3.linear.QRDecomposition#getSolver()

The following examples show how to use org.apache.commons.math3.linear.QRDecomposition#getSolver() . 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: LibCommonsMath.java    From systemds with Apache License 2.0 6 votes vote down vote up
/**
 * Function to solve a given system of equations.
 * 
 * @param in1 matrix object 1
 * @param in2 matrix object 2
 * @return matrix block
 */
private static MatrixBlock computeSolve(MatrixBlock in1, MatrixBlock in2) {
	//convert to commons math BlockRealMatrix instead of Array2DRowRealMatrix
	//to avoid unnecessary conversion as QR internally creates a BlockRealMatrix
	BlockRealMatrix matrixInput = DataConverter.convertToBlockRealMatrix(in1);
	BlockRealMatrix vectorInput = DataConverter.convertToBlockRealMatrix(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);
}
 
Example 2
Source File: LibCommonsMath.java    From systemds with Apache License 2.0 6 votes vote down vote up
/**
 * Function to solve a given system of equations.
 * 
 * @param in1 matrix object 1
 * @param in2 matrix object 2
 * @return matrix block
 */
private static MatrixBlock computeSolve(MatrixBlock in1, MatrixBlock in2) {
	//convert to commons math BlockRealMatrix instead of Array2DRowRealMatrix
	//to avoid unnecessary conversion as QR internally creates a BlockRealMatrix
	BlockRealMatrix matrixInput = DataConverter.convertToBlockRealMatrix(in1);
	BlockRealMatrix vectorInput = DataConverter.convertToBlockRealMatrix(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);
}
 
Example 3
Source File: LibCommonsMath.java    From systemds with Apache License 2.0 5 votes vote down vote up
/**
 * Function to compute matrix inverse via matrix decomposition.
 * 
 * @param in commons-math3 Array2DRowRealMatrix
 * @return matrix block
 */
private static MatrixBlock computeMatrixInverse(Array2DRowRealMatrix in) {
	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());
}
 
Example 4
Source File: LibCommonsMath.java    From systemds with Apache License 2.0 5 votes vote down vote up
/**
 * Function to compute matrix inverse via matrix decomposition.
 * 
 * @param in commons-math3 Array2DRowRealMatrix
 * @return matrix block
 */
private static MatrixBlock computeMatrixInverse(Array2DRowRealMatrix in) {
	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());
}
 
Example 5
Source File: QRDecompositionCommonsResult.java    From Strata with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an instance.
 * 
 * @param qr The result of the QR decomposition, not null
 */
public QRDecompositionCommonsResult(QRDecomposition qr) {
  ArgChecker.notNull(qr, "qr");
  _q = CommonsMathWrapper.unwrap(qr.getQ());
  _r = CommonsMathWrapper.unwrap(qr.getR());
  _qTranspose = _q.transpose();
  _solver = qr.getSolver();
}
 
Example 6
Source File: InvertMatrix.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
 * Calculates pseudo inverse of a matrix using QR decomposition
 * @param arr the array to invert
 * @return the pseudo inverted matrix
 */
public static INDArray pinvert(INDArray arr, boolean inPlace) {

    // TODO : do it natively instead of relying on commons-maths

    RealMatrix realMatrix = CheckUtil.convertToApacheMatrix(arr);
    QRDecomposition decomposition = new QRDecomposition(realMatrix, 0);
    DecompositionSolver solver = decomposition.getSolver();

    if (!solver.isNonSingular()) {
        throw new IllegalArgumentException("invalid array: must be singular matrix");
    }

    RealMatrix pinvRM = solver.getInverse();

    INDArray pseudoInverse = CheckUtil.convertFromApacheMatrix(pinvRM);

    if (inPlace)
        arr.assign(pseudoInverse);
    return pseudoInverse;

}
 
Example 7
Source File: InvertMatrix.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
/**
 * Calculates pseudo inverse of a matrix using QR decomposition
 * @param arr the array to invert
 * @return the pseudo inverted matrix
 */
public static INDArray pinvert(INDArray arr, boolean inPlace) {

    // TODO : do it natively instead of relying on commons-maths

    RealMatrix realMatrix = CheckUtil.convertToApacheMatrix(arr);
    QRDecomposition decomposition = new QRDecomposition(realMatrix, 0);
    DecompositionSolver solver = decomposition.getSolver();

    if (!solver.isNonSingular()) {
        throw new IllegalArgumentException("invalid array: must be singular matrix");
    }

    RealMatrix pinvRM = solver.getInverse();

    INDArray pseudoInverse = CheckUtil.convertFromApacheMatrix(pinvRM, arr.dataType());

    if (inPlace)
        arr.assign(pseudoInverse);
    return pseudoInverse;

}
 
Example 8
Source File: CommonsMathLinearSystemSolver.java    From myrrix-recommender with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isNonSingular(RealMatrix M) {
  QRDecomposition decomposition = new RRQRDecomposition(M, SINGULARITY_THRESHOLD);
  DecompositionSolver solver = decomposition.getSolver();
  return solver.isNonSingular();
}