def asjacobian(J):
spsolve = scipy.sparse.linalg.spsolve<EOL>if isinstance(J, Jacobian):<EOL><INDENT>return J<EOL><DEDENT>elif inspect.isclass(J) and issubclass(J, Jacobian):<EOL><INDENT>return J()<EOL><DEDENT>elif isinstance(J, np.ndarray):<EOL><INDENT>if J.ndim > <NUM_LIT:2>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>J = np.atleast_2d(np.asarray(J))<EOL>if J.shape[<NUM_LIT:0>] != J.shape[<NUM_LIT:1>]:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return Jacobian(matvec=lambda v: dot(J, v),<EOL>rmatvec=lambda v: dot(J.conj().T, v),<EOL>solve=lambda v: solve(J, v),<EOL>rsolve=lambda v: solve(J.conj().T, v),<EOL>dtype=J.dtype, shape=J.shape)<EOL><DEDENT>elif scipy.sparse.isspmatrix(J):<EOL><INDENT>if J.shape[<NUM_LIT:0>] != J.shape[<NUM_LIT:1>]:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return Jacobian(matvec=lambda v: J*v,<EOL>rmatvec=lambda v: J.conj().T * v,<EOL>solve=lambda v: spsolve(J, v),<EOL>rsolve=lambda v: spsolve(J.conj().T, v),<EOL>dtype=J.dtype, shape=J.shape)<EOL><DEDENT>elif hasattr(J, '<STR_LIT>') and hasattr(J, '<STR_LIT>') and hasattr(J, '<STR_LIT>'):<EOL><INDENT>return Jacobian(matvec=getattr(J, '<STR_LIT>'),<EOL>rmatvec=getattr(J, '<STR_LIT>'),<EOL>solve=J.solve,<EOL>rsolve=getattr(J, '<STR_LIT>'),<EOL>update=getattr(J, '<STR_LIT>'),<EOL>setup=getattr(J, '<STR_LIT>'),<EOL>dtype=J.dtype,<EOL>shape=J.shape)<EOL><DEDENT>elif callable(J):<EOL><INDENT>class Jac(Jacobian):<EOL><INDENT>def update(self, x, F):<EOL><INDENT>self.x = x<EOL><DEDENT>def solve(self, v, tol=<NUM_LIT:0>):<EOL><INDENT>m = J(self.x)<EOL>if isinstance(m, np.ndarray):<EOL><INDENT>return solve(m, v)<EOL><DEDENT>elif scipy.sparse.isspmatrix(m):<EOL><INDENT>return spsolve(m, v)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>def matvec(self, v):<EOL><INDENT>m = J(self.x)<EOL>if isinstance(m, np.ndarray):<EOL><INDENT>return dot(m, v)<EOL><DEDENT>elif scipy.sparse.isspmatrix(m):<EOL><INDENT>return m*v<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>def rsolve(self, v, tol=<NUM_LIT:0>):<EOL><INDENT>m = J(self.x)<EOL>if isinstance(m, np.ndarray):<EOL><INDENT>return solve(m.conj().T, v)<EOL><DEDENT>elif scipy.sparse.isspmatrix(m):<EOL><INDENT>return spsolve(m.conj().T, v)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>def rmatvec(self, v):<EOL><INDENT>m = J(self.x)<EOL>if isinstance(m, np.ndarray):<EOL><INDENT>return dot(m.conj().T, v)<EOL><DEDENT>elif scipy.sparse.isspmatrix(m):<EOL><INDENT>return m.conj().T * v<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>return Jac()<EOL><DEDENT>elif isinstance(J, str):<EOL><INDENT>return dict(broyden1=BroydenFirst,<EOL>broyden2=BroydenSecond,<EOL>anderson=Anderson,<EOL>diagbroyden=DiagBroyden,<EOL>linearmixing=LinearMixing,<EOL>excitingmixing=ExcitingMixing,<EOL>krylov=KrylovJacobian)[J]()<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>
Convert given object to one suitable for use as a Jacobian.