Kronecker Power of sparse matrices problem
Why the following code does not work? I try it also in loops.
import numpy
from scipy import sparse
from scipy.sparse import coo_matrix
def SparseMatrixPower(A,p):
if p == 1:
return(A)
elif Mod(p,2):
return(SparseMatrixPower(A,SparseMatrixProduct(A,A)), (p - 1) / 2)
else:
return(SparseMatrixPower(SparseMatrixProduct(A,A), p / 2))
def SparseMatrixProduct(A,B):
return(sparse.kron(A,B)+sparse.kronsum(A,B))
A=sparse.coo_matrix([[0,1,2],[1,1,2],[2,2,3]])
B=sparse.coo_matrix([[0,1,2],[1,1,2],[2,2,3]])
SparseMatrixProduct(A,B)
SparseMatrixPower(A,3)