Exemplo n.º 1
0
    def from_callback(cls,
                      cb,
                      transf_cbs,
                      nx,
                      nparams=0,
                      pre_adj=None,
                      **kwargs):
        """ Generate a TransformedSys instance from a callback

        Parameters
        ----------
        cb : callable
        transf_cbs : pair or iterable of pairs of callables
        nx : int
        nparams : int
        pre_adj : callable
        \\*\\*kwargs : passed onto TransformedSys
        """
        be = Backend(kwargs.pop('backend', None))
        x = be.real_symarray('x', nx)
        p = be.real_symarray('p', nparams)
        try:
            transf = [(transf_cbs[idx][0](xi), transf_cbs[idx][1](xi))
                      for idx, xi in enumerate(x)]
        except TypeError:
            transf = zip(_map2(transf_cbs[0], x), _map2(transf_cbs[1], x))
        try:
            exprs = cb(x, p, be)
        except TypeError:
            exprs = _ensure_3args(cb)(x, p, be)
        return cls(x, _map2l(pre_adj, exprs), transf, p, backend=be, **kwargs)
Exemplo n.º 2
0
    def from_callback(cls, cb, ny, nparams=0, **kwargs):
        """ Create an instance from a callback.

        Parameters
        ----------
        cb : callbable
            Signature ``rhs(x, y[:], p[:]) -> f[:]``
        ny : int
            length of y
        nparams : int (default: 0)
            length of p
        \*\*kwargs :
            keyword arguments passed onto :class:`SymbolicSys`

        Returns
        -------
        An instance of :class:`SymbolicSys`.
        """
        be = Backend(kwargs.pop('backend', None))
        x, = be.real_symarray('x', 1)
        y = be.real_symarray('y', ny)
        p = be.real_symarray('p', nparams)
        try:
            exprs = cb(x, y, p, be)
        except TypeError:
            exprs = _ensure_4args(cb)(x, y, p, be)
        return cls(zip(y, exprs), x, p, backend=be, **kwargs)
Exemplo n.º 3
0
    def from_callback(cls, cb, nx=None, nparams=None, **kwargs):
        """ Generate a SymbolicSys instance from a callback"""
        if kwargs.get('x_by_name', False):
            if 'names' not in kwargs:
                raise ValueError("Need ``names`` in kwargs.")
            if nx is None:
                nx = len(kwargs['names'])
            elif nx != len(kwargs['names']):
                raise ValueError("Inconsistency between nx and length of ``names``.")
        if kwargs.get('par_by_name', False):
            if 'param_names' not in kwargs:
                raise ValueError("Need ``param_names`` in kwargs.")
            if nparams is None:
                nparams = len(kwargs['param_names'])
            elif nparams != len(kwargs['param_names']):
                raise ValueError("Inconsistency between ``nparam`` and length of ``param_names``.")

        if nparams is None:
            nparams = 0

        if nx is None:
            raise ValueError("Need ``nx`` of ``names`` together with ``x_by_name==True``.")
        be = Backend(kwargs.pop('backend', None))
        x = be.real_symarray('x', nx)
        p = be.real_symarray('p', nparams)
        _x = dict(zip(kwargs['names'], x)) if kwargs.get('x_by_name', False) else x
        _p = dict(zip(kwargs['param_names'], p)) if kwargs.get('par_by_name', False) else p
        try:
            exprs = cb(_x, _p, be)
        except TypeError:
            exprs = _ensure_3args(cb)(_x, _p, be)
        return cls(x, exprs, p, backend=be, **kwargs)
Exemplo n.º 4
0
    def from_callback(cls, cb, transf_cbs, nx, nparams=0, pre_adj=None,
                      **kwargs):
        """ Generate a TransformedSys instance from a callback

        Parameters
        ----------
        cb: callable
        transf_cbs: pair or iterable of pairs of callables
        nx: int
        nparams: int
        pre_adj: callable
        \*\*kwargs: passed onto TransformedSys
        """
        be = Backend(kwargs.pop('backend', None))
        x = be.real_symarray('x', nx)
        p = be.real_symarray('p', nparams)
        try:
            transf = [(transf_cbs[idx][0](xi),
                       transf_cbs[idx][1](xi))
                      for idx, xi in enumerate(x)]
        except TypeError:
            transf = zip(_map2(transf_cbs[0], x), _map2(transf_cbs[1], x))
        try:
            exprs = cb(x, p, be)
        except TypeError:
            exprs = _ensure_3args(cb)(x, p, be)
        return cls(x, _map2l(pre_adj, exprs), transf, p, backend=be, **kwargs)
Exemplo n.º 5
0
    def from_callback(cls, cb, ny, nparams=0, dep_transf_cbs=None,
                      indep_transf_cbs=None, **kwargs):
        """
        Create an instance from a callback.

        Analogous to :func:`SymbolicSys.from_callback`.

        Parameters
        ----------
        cb : callable
            Signature ``rhs(x, y[:], p[:]) -> f[:]``
        ny : int
            length of y
        nparams : int
            length of p
        dep_transf_cbs : iterable of pairs callables
            callables should have the signature f(yi) -> expression in yi
        indep_transf_cbs : pair of callbacks
            callables should have the signature f(x) -> expression in x
        \*\*kwargs :
            keyword arguments passed onto :class:`TransformedSys`
        """
        be = Backend(kwargs.pop('backend', None))
        x, = be.real_symarray('x', 1)
        y = be.real_symarray('y', ny)
        p = be.real_symarray('p', nparams)
        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

        return cls(list(zip(y, exprs)), x, dep_transf,
                   indep_transf, p, backend=be, **kwargs)
Exemplo n.º 6
0
    def from_callback(cls, cb, nx=None, nparams=None, **kwargs):
        """ Generate a SymbolicSys instance from a callback"""
        if kwargs.get('x_by_name', False):
            if 'names' not in kwargs:
                raise ValueError("Need ``names`` in kwargs.")
            if nx is None:
                nx = len(kwargs['names'])
            elif nx != len(kwargs['names']):
                raise ValueError(
                    "Inconsistency between nx and length of ``names``.")
        if kwargs.get('par_by_name', False):
            if 'param_names' not in kwargs:
                raise ValueError("Need ``param_names`` in kwargs.")
            if nparams is None:
                nparams = len(kwargs['param_names'])
            elif nparams != len(kwargs['param_names']):
                raise ValueError(
                    "Inconsistency between ``nparam`` and length of ``param_names``."
                )

        if nparams is None:
            nparams = 0

        if nx is None:
            raise ValueError(
                "Need ``nx`` of ``names`` together with ``x_by_name==True``.")
        be = Backend(kwargs.pop('backend', None))
        x = be.real_symarray('x', nx)
        p = be.real_symarray('p', nparams)
        _x = dict(zip(kwargs['names'], x)) if kwargs.get('x_by_name',
                                                         False) else x
        _p = dict(zip(kwargs['param_names'], p)) if kwargs.get(
            'par_by_name', False) else p
        try:
            exprs = cb(_x, _p, be)
        except TypeError:
            exprs = _ensure_3args(cb)(_x, _p, be)
        return cls(x, exprs, p, backend=be, **kwargs)