powsybl / powsybl-math-native

Mozilla Public License 2.0
1 stars 0 forks source link

Creating a sparse matrix using SparseMatrix's CSC constructor resulted in LU decomposition failure #37

Open H-Dynamite opened 8 months ago

H-Dynamite commented 8 months ago

Describe the current behavior

Using SparseMatrix (int rowCount, int columnCount, int [] columnStart, int [] rowIndicators, double [] values) causes LU decomposition to fail

error log:

Exception in thread "main" com.powsybl.math.matrix.MatrixException: klu_analyze error KLU_INVALID at com.powsybl.math.matrix.SparseLUDecomposition.init(Native Method) at com.powsybl.math.matrix.SparseLUDecomposition.init(SparseLUDecomposition.java:49) at com.powsybl.math.matrix.SparseLUDecomposition.(SparseLUDecomposition.java:43) at com.powsybl.math.matrix.SparseMatrix.decomposeLU(SparseMatrix.java:330) at com.powsybl.math.matrix.SparseMatrixTest.main(SparseMatrixTest.java:121)

Describe the expected behavior

Expected decomposition successful

Describe the steps

SparseMatrix a =new SparseMatrix(2, 2, 2); a.add(0, 0, 1d); a.add(1, 0, 1d); a.add(0, 1, 1d); SparseMatrix m = new SparseMatrix(a.getRowCount(), a.getColumnCount(), a.getColumnStart(), a.getRowIndices(), a.getValues()); LUDecomposition decomposition = m.decomposeLU();

Problem solving:

This issue is caused by the variable "currentColumn" not being assigned during initialization

SparseMatrix(int rowCount, int columnCount, int[] columnStart, int[] rowIndices, double[] values) {
    this.rowCount = rowCount;
    this.columnCount = columnCount;
    this.columnStart = Objects.requireNonNull(columnStart);
    this.currentColumn = values.length;//Initialize here
    columnValueCount = new int[columnCount];
    this.rowIndices = new TIntArrayListHack(Objects.requireNonNull(rowIndices));
    this.values = new TDoubleArrayListHack(Objects.requireNonNull(values));
    fillColumnValueCount(this.columnCount, this.columnStart, columnValueCount, this.values);
}

}

Environment

powsybl-core 6.2.0-SNAPSHOT

Relevant Log Output

No response

Extra Information

No response

geofjamg commented 8 months ago

Thanks for reporting this issue. I will open a PR on core repo. Is your original need to copy an existing sparse matrix or build a sparse matrix from provided arrays ?

H-Dynamite commented 8 months ago

I initially wanted to use arrays to quickly construct a sparse matrix for testing the performance of KLU. Surprisingly, the decomposition speed of KLU is very fast. I am grateful that we have developed such a great library.