Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
try:
if len(exprs) != ny:
raise ValueError("Callback returned unexpected (%d) number of expressions: %d" % (ny, len(exprs)))
except TypeError:
raise ValueError("Callback did not return an array_like of expressions: %s" % str(exprs))
cls._kwargs_roots_from_roots_cb(roots_cb, kwargs, x, _y, _p, be)
if first_step_factory is not None:
if 'first_step_exprs' in kwargs:
raise ValueError("Cannot override first_step_exprs.")
try:
kwargs['first_step_expr'] = first_step_factory(x, _y, _p, be)
except TypeError:
kwargs['first_step_expr'] = _ensure_4args(first_step_factory)(x, _y, _p, be)
if kwargs.get('dep_by_name', False):
exprs = [exprs[k] for k in names]
return cls(zip(y, exprs), x, kwargs.pop('params', None) if len(p) == 0 else p,
backend=be, names=names, **kwargs)
be = Backend(kwargs.pop('backend', None))
names = tuple(kwargs.pop('names', ''))
indep_name = indep_name or _get_indep_name(names)
try:
x = be.Symbol(indep_name, real=True)
except TypeError:
x = be.Symbol(indep_name)
y = be.real_symarray('y', ny)
p = be.real_symarray('p', nparams)
_y = dict(zip(names, y)) if kwargs.get('dep_by_name', False) else y
_p = dict(zip(kwargs['param_names'], p)) if kwargs.get('par_by_name', False) else p
try:
exprs = rhs(x, _y, _p, be)
except TypeError:
exprs = _ensure_4args(rhs)(x, _y, _p, be)
try:
if len(exprs) != ny:
raise ValueError("Callback returned unexpected (%d) number of expressions: %d" % (ny, len(exprs)))
except TypeError:
raise ValueError("Callback did not return an array_like of expressions: %s" % str(exprs))
cls._kwargs_roots_from_roots_cb(roots_cb, kwargs, x, _y, _p, be)
if first_step_factory is not None:
if 'first_step_exprs' in kwargs:
raise ValueError("Cannot override first_step_exprs.")
try:
kwargs['first_step_expr'] = first_step_factory(x, _y, _p, be)
except TypeError:
kwargs['first_step_expr'] = _ensure_4args(first_step_factory)(x, _y, _p, be)
callables should have the signature ``f(x) -> expression`` in x
roots_cb : callable
Callback with signature ``roots(x, y[:], p[:], backend=math) -> r[:]``.
Callback should return untransformed roots.
\\*\\*kwargs :
Keyword arguments passed onto :class:`TransformedSys`.
"""
ny, nparams = _get_ny_nparams_from_kw(ny, nparams, kwargs)
be = Backend(kwargs.pop('backend', None))
x, = be.real_symarray('x', 1)
y = be.real_symarray('y', ny)
p = be.real_symarray('p', nparams)
_y = dict(zip(kwargs['names'], y)) if kwargs.get('dep_by_name', False) else y
_p = dict(zip(kwargs['param_names'], p)) if kwargs.get('par_by_name', False) else p
exprs = _ensure_4args(cb)(x, _y, _p, be)
if dep_transf_cbs is not None:
dep_transf = [(fw(yi), bw(yi)) for (fw, bw), yi
in zip(dep_transf_cbs, y)]
else:
dep_transf = None
if indep_transf_cbs is not None:
indep_transf = indep_transf_cbs[0](x), indep_transf_cbs[1](x)
else:
indep_transf = None
if kwargs.get('dep_by_name', False):
exprs = [exprs[k] for k in kwargs['names']]
cls._kwargs_roots_from_roots_cb(roots_cb, kwargs, x, _y, _p, be)
return cls(list(zip(y, exprs)), x, dep_transf,
def _kwargs_roots_from_roots_cb(roots_cb, kwargs, x, _y, _p, be):
if roots_cb is not None:
if 'roots' in kwargs:
raise ValueError("Keyword argument ``roots`` already given.")
try:
roots = roots_cb(x, _y, _p, be)
except TypeError:
roots = _ensure_4args(roots_cb)(x, _y, _p, be)
kwargs['roots'] = roots
def __init__(self, f, jac=None, dfdx=None, jtimes=None, first_step_cb=None, roots_cb=None, nroots=None,
band=None, names=(), param_names=(), indep_name=None, description=None, dep_by_name=False,
par_by_name=False, latex_names=(), latex_param_names=(), latex_indep_name=None,
taken_names=None, pre_processors=None, post_processors=None, append_iv=False,
autonomous_interface=None, to_arrays_callbacks=None, autonomous_exprs=None,
_indep_autonomous_key=None, numpy=None, nnz=-1, **kwargs):
self.f_cb = _ensure_4args(f)
self.j_cb = _ensure_4args(jac) if jac is not None else None
self.jtimes_cb = _ensure_4args(jtimes) if jtimes is not None else None
self.dfdx_cb = dfdx
self.first_step_cb = first_step_cb
self.roots_cb = roots_cb
self.nroots = nroots or 0
if band is not None:
if not band[0] >= 0 or not band[1] >= 0:
raise ValueError("bands needs to be > 0 if provided")
self.band = band
self.nnz = nnz
self.names = tuple(names or ())
self.param_names = tuple(param_names or ())
self.indep_name = indep_name
self.description = description
self.dep_by_name = dep_by_name
self.par_by_name = par_by_name
self.latex_names = tuple(latex_names or ())
def __init__(self, f, jac=None, dfdx=None, jtimes=None, first_step_cb=None, roots_cb=None, nroots=None,
band=None, names=(), param_names=(), indep_name=None, description=None, dep_by_name=False,
par_by_name=False, latex_names=(), latex_param_names=(), latex_indep_name=None,
taken_names=None, pre_processors=None, post_processors=None, append_iv=False,
autonomous_interface=None, to_arrays_callbacks=None, autonomous_exprs=None,
_indep_autonomous_key=None, numpy=None, nnz=-1, **kwargs):
self.f_cb = _ensure_4args(f)
self.j_cb = _ensure_4args(jac) if jac is not None else None
self.jtimes_cb = _ensure_4args(jtimes) if jtimes is not None else None
self.dfdx_cb = dfdx
self.first_step_cb = first_step_cb
self.roots_cb = roots_cb
self.nroots = nroots or 0
if band is not None:
if not band[0] >= 0 or not band[1] >= 0:
raise ValueError("bands needs to be > 0 if provided")
self.band = band
self.nnz = nnz
self.names = tuple(names or ())
self.param_names = tuple(param_names or ())
self.indep_name = indep_name
self.description = description
self.dep_by_name = dep_by_name
self.par_by_name = par_by_name
def __init__(self, original_system, analytic_factory, **kwargs):
self._ori_sys = original_system
self.analytic_factory = _ensure_4args(analytic_factory)
roots = kwargs.pop('roots', self._ori_sys.roots)
_be = self._ori_sys.be
init_indep = self._ori_sys.init_indep or self._mk_init_indep(name=self._ori_sys.indep, be=self._ori_sys.be)
init_dep = self._ori_sys.init_dep or self._ori_sys._mk_init_dep()
if 'pre_processors' in kwargs or 'post_processors' in kwargs:
raise NotImplementedError("Cannot override pre-/postprocessors")
if 'backend' in kwargs and Backend(kwargs['backend']) != _be:
raise ValueError("Cannot mix backends.")
_pars = self._ori_sys.params
if self._ori_sys.par_by_name:
_pars = dict(zip(self._ori_sys.param_names, _pars))
self.original_dep = self._ori_sys.dep
_dep0 = (dict(zip(self.original_dep, init_dep)) if self._ori_sys.dep_by_name
else init_dep)
self.analytic_exprs = self.analytic_factory(init_indep, _dep0, _pars, _be)