Search in sources :

Example 1 with CSCMatrix

use of org.openlca.core.matrix.format.CSCMatrix in project olca-modules by GreenDelta.

the class JuliaSolver method solve.

@Override
public double[] solve(MatrixReader a, int idx, double d) {
    if (!a.isSquare())
        throw new NonSquareMatrixException(a.rows(), a.columns());
    if (Julia.hasSparseLibraries() && (a instanceof HashPointMatrix || a instanceof CSCMatrix)) {
        var csc = CSCMatrix.of(a);
        double[] f = new double[csc.rows];
        f[idx] = d;
        double[] b = new double[csc.rows];
        Julia.umfSolve(csc.rows, csc.columnPointers, csc.rowIndices, csc.values, f, b);
        return b;
    }
    var A = MatrixConverter.dense(a);
    var lu = A == a ? A.copy() : A;
    double[] b = new double[A.rows()];
    b[idx] = d;
    int info = Julia.solve(A.columns(), 1, lu.data, b);
    if (info > 0)
        throw new SingularMatrixException();
    if (info < 0)
        throw new InsufficientDataException();
    return b;
}
Also used : SingularMatrixException(org.apache.commons.math3.linear.SingularMatrixException) InsufficientDataException(org.apache.commons.math3.exception.InsufficientDataException) NonSquareMatrixException(org.apache.commons.math3.linear.NonSquareMatrixException) CSCMatrix(org.openlca.core.matrix.format.CSCMatrix) HashPointMatrix(org.openlca.core.matrix.format.HashPointMatrix)

Example 2 with CSCMatrix

use of org.openlca.core.matrix.format.CSCMatrix in project olca-modules by GreenDelta.

the class NativeSolver method solve.

@Override
public double[] solve(MatrixReader a, int idx, double d) {
    if (!a.isSquare())
        throw new NonSquareMatrixException(a.rows(), a.columns());
    if (hasSparseSupport() && (a instanceof HashPointMatrix || a instanceof CSCMatrix)) {
        var csc = CSCMatrix.of(a);
        double[] f = new double[csc.rows];
        f[idx] = d;
        double[] b = new double[csc.rows];
        Julia.umfSolve(csc.rows, csc.columnPointers, csc.rowIndices, csc.values, f, b);
        return b;
    }
    var A = MatrixConverter.dense(a);
    var lu = A == a ? A.copy() : A;
    double[] b = new double[A.rows()];
    b[idx] = d;
    int info = Julia.solve(A.columns(), 1, lu.data, b);
    if (info > 0)
        throw new SingularMatrixException();
    if (info < 0)
        throw new InsufficientDataException();
    return b;
}
Also used : SingularMatrixException(org.apache.commons.math3.linear.SingularMatrixException) InsufficientDataException(org.apache.commons.math3.exception.InsufficientDataException) NonSquareMatrixException(org.apache.commons.math3.linear.NonSquareMatrixException) CSCMatrix(org.openlca.core.matrix.format.CSCMatrix) HashPointMatrix(org.openlca.core.matrix.format.HashPointMatrix)

Aggregations

InsufficientDataException (org.apache.commons.math3.exception.InsufficientDataException)2 NonSquareMatrixException (org.apache.commons.math3.linear.NonSquareMatrixException)2 SingularMatrixException (org.apache.commons.math3.linear.SingularMatrixException)2 CSCMatrix (org.openlca.core.matrix.format.CSCMatrix)2 HashPointMatrix (org.openlca.core.matrix.format.HashPointMatrix)2