Example usage for org.apache.commons.math3.linear QRDecomposition QRDecomposition

List of usage examples for org.apache.commons.math3.linear QRDecomposition QRDecomposition

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear QRDecomposition QRDecomposition.

Prototype

public QRDecomposition(RealMatrix matrix) 

Source Link

Document

Calculates the QR-decomposition of the given matrix.

Usage

From source file:org.apache.sysml.runtime.matrix.data.LibCommonsMath.java

/**
 * Function to solve a given system of equations.
 * //from w w  w  .ja  va 2s.  c  o  m
 * @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());
}

From source file:org.apache.sysml.runtime.matrix.data.LibCommonsMath.java

/**
 * Function to compute matrix inverse via matrix decomposition.
 * /*from  w  w  w. j a v  a 2s.  co m*/
 * @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());
}

From source file:org.eclipse.dataset.LinearAlgebra.java

/**
 * Calculate QR decomposition A = Q R/*from   w w w . j  a  v  a  2s  .  c  om*/
 * @param a
 * @return array of Q and R
 */
public static Dataset[] calcQRDecomposition(Dataset a) {
    QRDecomposition qrd = new QRDecomposition(createRealMatrix(a));
    return new Dataset[] { createDataset(qrd.getQT()).getTransposedView(), createDataset(qrd.getR()) };
}

From source file:org.meteoinfo.math.linalg.LinalgUtil.java

/**
 * Calculates the QR-decomposition of a matrix.
 * The QR-decomposition of a matrix A consists of two matrices Q and R that satisfy: A = QR, Q 
 * is orthogonal (QTQ = I), and R is upper triangular. If A is mn, Q is mm and R mn.
 * @param a Given matrix./*from w w w.j a  v a  2  s .c  o m*/
 * @return Result Q/R arrays.
 */
public static Array[] qr(Array a) {
    int m = a.getShape()[0];
    int n = a.getShape()[1];
    Array Qa = Array.factory(DataType.DOUBLE, new int[] { m, m });
    Array Ra = Array.factory(DataType.DOUBLE, a.getShape());
    double[][] aa = (double[][]) ArrayUtil.copyToNDJavaArray(a);
    RealMatrix matrix = new Array2DRowRealMatrix(aa, false);
    QRDecomposition decomposition = new QRDecomposition(matrix);
    RealMatrix Q = decomposition.getQ();
    RealMatrix R = decomposition.getR();
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
            Qa.setDouble(i * m + j, Q.getEntry(i, j));
        }
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            Ra.setDouble(i * n + j, R.getEntry(i, j));
        }
    }

    return new Array[] { Qa, Ra };
}

From source file:org.ojalgo.benchmark.contestant.ACM.java

@Override
public BenchmarkContestant<RealMatrix>.LeastSquaresSolver getLeastSquaresSolver() {
    return new LeastSquaresSolver() {

        @Override/* w  w w.  j a va2 s.c o  m*/
        public RealMatrix apply(final RealMatrix body, final RealMatrix rhs) {

            final QRDecomposition tmpQR = new QRDecomposition(body);

            return tmpQR.getSolver().solve(rhs);
        }

    };
}

From source file:org.ojalgo.benchmark.lab.library.ACM.java

@Override
public ProducingBinaryMatrixMatrixOperation<RealMatrix, Array2DRowRealMatrix> getOperationEquationSystemSolver(
        final int numbEquations, final int numbVariables, final int numbSolutions, final boolean spd) {
    if (numbEquations == numbVariables) {
        if (spd) {
            return (body, rhs) -> {
                final CholeskyDecomposition cholesky = new CholeskyDecomposition(body);
                return cholesky.getSolver().solve(rhs);
            };//  ww w. j a  va 2 s.  com
        } else {
            return (body, rhs) -> {
                final LUDecomposition lu = new LUDecomposition(body);
                return lu.getSolver().solve(rhs);
            };
        }
    } else if (numbEquations > numbVariables) {
        return (body, rhs) -> {
            final QRDecomposition qr = new QRDecomposition(body);
            return qr.getSolver().solve(rhs);
        };
    } else {
        return null;
    }
}

From source file:org.orekit.orbits.Orbit.java

/** Create an inverse Jacobian.
 * @param type type of the position angle to use
 * @return inverse Jacobian/*from  w  w w .j a  va2  s. com*/
 */
private double[][] createInverseJacobian(final PositionAngle type) {

    // get the direct Jacobian
    final double[][] directJacobian = new double[6][6];
    getJacobianWrtCartesian(type, directJacobian);

    // invert the direct Jacobian
    final RealMatrix matrix = MatrixUtils.createRealMatrix(directJacobian);
    final DecompositionSolver solver = new QRDecomposition(matrix).getSolver();
    return solver.getInverse().getData();

}

From source file:org.orekit.propagation.numerical.JacobiansMapper.java

/** Set the Jacobian with respect to state into a one-dimensional additional state array.
 * <p>//from  ww w.  j a  va2 s. co m
 * This method converts the Jacobians to cartesian parameters and put the converted data
 * in the one-dimensional {@code p} array.
 * </p>
 * @param state spacecraft state
 * @param dY1dY0 Jacobian of current state at time t?
 * with respect to state at some previous time t
 * @param dY1dP Jacobian of current state at time t?
 * with respect to parameters (may be null if there are no parameters)
 * @param p placeholder where to put the one-dimensional additional state
 * @see #getStateJacobian(SpacecraftState, double[][])
 */
void setInitialJacobians(final SpacecraftState state, final double[][] dY1dY0, final double[][] dY1dP,
        final double[] p) {

    // set up a converter between state parameters and cartesian parameters
    final RealMatrix dY1dC1 = new Array2DRowRealMatrix(getdYdC(state), false);
    final DecompositionSolver solver = new QRDecomposition(dY1dC1).getSolver();

    // convert the provided state Jacobian to cartesian parameters
    final RealMatrix dC1dY0 = solver.solve(new Array2DRowRealMatrix(dY1dY0, false));

    // map the converted state Jacobian to one-dimensional array
    int index = 0;
    for (int i = 0; i < stateDimension; ++i) {
        for (int j = 0; j < stateDimension; ++j) {
            p[index++] = dC1dY0.getEntry(i, j);
        }
    }

    if (parameters > 0) {
        // convert the provided state Jacobian to cartesian parameters
        final RealMatrix dC1dP = solver.solve(new Array2DRowRealMatrix(dY1dP, false));

        // map the converted parameters Jacobian to one-dimensional array
        for (int i = 0; i < stateDimension; ++i) {
            for (int j = 0; j < parameters; ++j) {
                p[index++] = dC1dP.getEntry(i, j);
            }
        }
    }

}

From source file:org.ujmp.commonsmath.AbstractCommonsMathDenseDoubleMatrix2D.java

public Matrix[] qr() {
    QRDecomposition qr = new QRDecomposition(matrix);
    Matrix q = CommonsMathDenseDoubleMatrix2DFactory.INSTANCE.dense(qr.getQ());
    Matrix r = CommonsMathDenseDoubleMatrix2DFactory.INSTANCE.dense(qr.getR());
    return new Matrix[] { q, r };
}

From source file:org.ujmp.commonsmath.AbstractCommonsMathDenseDoubleMatrix2D.java

public Matrix solve(Matrix b) {
    if (b instanceof AbstractCommonsMathDenseDoubleMatrix2D) {
        AbstractCommonsMathDenseDoubleMatrix2D b2 = (AbstractCommonsMathDenseDoubleMatrix2D) b;
        if (isSquare()) {
            RealMatrix ret = new LUDecomposition(matrix).getSolver().solve(b2.matrix);
            return CommonsMathDenseDoubleMatrix2DFactory.INSTANCE.dense(ret);
        } else {//from ww w . ja v  a2  s  .c  o m
            RealMatrix ret = new QRDecomposition(matrix).getSolver().solve(b2.matrix);
            return CommonsMathDenseDoubleMatrix2DFactory.INSTANCE.dense(ret);
        }
    } else {
        return super.solve(b);
    }
}