MathNet - Least computationally expensive way of extracting a large submatrix from a large matrix

287 views Asked by At

I'm trying to extract a large submatrix from a parent matrix using the MathNet package. I couldn't find a builtin function to do it so i wrote this simple function in VB.net:

Private Function Extract(s As Matrix, RowsAndColumns As Int32()) As SparseMatrix
    Dim Sadj = MathNet.Numerics.LinearAlgebra.Double.SparseMatrix.Build.Sparse(RowsAndColumns.Count, RowsAndColumns.Count)
    For i = 0 To RowsAndColumns.Count - 1
        For j = 0 To RowsAndColumns.Count - 1
            Sadj(i, j) = s(RowsAndColumns (i), RowsAndColumns (j))
        Next
    Next
    Return Sadj
End Function

However, the performance of this function is very slow because typically the original matrix is very big and the submatrix is close to it in size. So i am looking at possible ways of optimizing this. Any help would be appreciated.

As a bit of a background, i am trying to extract a submatrix from a big sparse matrix and then to use CSparse to solve a large system of equations.

Dim Sred = Extract(S, FreeDOFs)
Dim storage = DirectCast(Sred.Storage, MathNet.Numerics.LinearAlgebra.Storage.SparseCompressedRowMatrixStorage(Of Double)) ' Get CSR storage.
Dim A = New CSparse.Double.SparseMatrix(Sred.ColumnCount, Sred.ColumnCount) With {.ColumnPointers = storage.RowPointers, .RowIndices = storage.ColumnIndices, .Values = storage.Values} ' Create CSparse matrix and Assign storage arrays.      
For i = 0 To NumofLoadCases - 1
    Dim Fred = Extract(NodeLoads.Item(ASP.AnalysisLoadCases.Keys(i)) - Pf.Item(ASP.AnalysisLoadCases.Keys(i)), FreeDOFs)
    Dim Dred = Sred.LU().Solve(Fred)
    Dvec.Add(ASP.AnalysisLoadCases.Keys(i), ReverseExtract(Dred, FreeDOFs, 6 * NumOfNodes))
Next
1

There are 1 answers

0
Joseph On BEST ANSWER

Since the submatrix is so big and Sparse , it is more efficient to construct it while i am constructing the main matrix. This way i don't have to rewrite a very large number of zeros. This is significantly faster!