Apparently coo_matrix does not work properly for mapmath matrix. Is there any equivalent implementation or simple algorithm to implement for mpmath?
import mpmath
import numpy as np
from scipy.sparse import coo_matrix
A = np.diag([1]*5)
A_coo = coo_matrix(A)
print(A_coo.row)
print(A_coo.col)
print(A_coo.data)
A_mp = mpmath.diag([1]*5)
A_mp_coo = coo_matrix(A_mp)
print(A_mp_coo.row)
print(A_mp_coo.col)
print(A_mp_coo.data)
output
[0 1 2 3 4]
[0 1 2 3 4]
[1 1 1 1 1]
# ----------------------
[0 0 0 0 0]
[ 0 6 12 18 24]
[mpf('1.0') mpf('1.0') mpf('1.0') mpf('1.0') mpf('1.0')]
Edit:
What I think is loop over every element and find the i,j index of nonzero elements of mpmath matrix then use the following :
size=np.count_nonzero(A_mp)
rows, cols = ... # looping over evey elements to find the indices on nonzero elements
compactMatrix = np.zeros((3, size))
for i in range(size):
compactMatrix[0, i] = rows[i]
compactMatrix[1, i] = cols[i]
compactMatrix[2, i] = A_mp[rows[i], cols[i]]
but it seems vary naive and not fast.