Wrapper for sparse null space algorithm
This is a feature request for a wrapper that uses the sparse null space finding algorithm in MUMPS.
Finding the null space of large sparse matrices is a common numerical task and (to my knowledge) no python library addresses the issue in a satisfactory way. Below is a rudimentary implementation based on the MUMPS wrapper of kwant
.
import numpy as np
import kwant.linalg.mumps as mumps
import scipy.linalg as la
def mumps_nullspace(a):
a = sp.sparse.coo_matrix(a, dtype=complex)
a.eliminate_zeros()
dtype, row, col, data = mumps._make_assembled_from_coo(a, overwrite_a=True)
mumps_instance = getattr(mumps._mumps, dtype+"mumps")(True)
n = a.shape[0]
row = row
col = col
data = data
mumps_instance.set_assembled_matrix(a.shape[0], row, col, data)
ordering='auto'
mumps_instance.icntl[7] = mumps.orderings[ordering]
mumps_instance.job = 1
mumps_instance.call()
# Find null pivots
mumps_instance.icntl[24] = 1
mumps_instance.job = 2
mumps_instance.call()
# Get the size of the null space
n_null = mumps_instance.infog[28]
if n_null == 0:
return np.empty((a.shape[1], 0))
# Initialize matrix for null space basis
nullspace = np.zeros((a.shape[1], n_null), dtype=complex, order='F')
# Set RHS
mumps_instance.set_dense_rhs(nullspace)
mumps_instance.job = 3
# Return all null space basis vectors, overwriting RHS
mumps_instance.icntl[25] = -1
mumps_instance.call()
# Orthonormalize
nullspace, _ = la.qr(nullspace, mode='economic')
return nullspace
Edited by Dániel Varjas