python - Scipy fsolve cannot take sparse matrix as jacobian -


i have following code, works fine (except of course not converge):

import numpy np scipy.optimize import fsolve scipy.sparse import csr_matrix  t = 100  def recurrence(p):     return p[2] - p[1] + 100   def equations(p):     n = len(p)     return [p[0], p[n-1]-1] + [recurrence(p[i-1:i+2])                                in range(1, n-1)]  def jacobian(p):     j = np.ones((t+1, t+1))     return j  p = np.ones(t + 1) sol = fsolve(equations, p, fprime=jacobian) 

however, when changed jacobian return sparse matrix, not work anymore:

def jacobian(p):     j = np.ones((t+1, t+1))     return csr_matrix(j)  typeerror: fsolve: there mismatch between input , output shape of 'fprime' argument 'jacobian'.shape should (101, 101) (1,). 

is there workaround here? of course example, working system larger value of t.


Comments