Beispiel #1
0
 def _get_jacobian(self):
     jac = []
     for param in self.params:
         # Differentiate to every param
         f = sympy.diff(self.model, param)
         # Make them into pythonic functions
         jac.append(sympy_to_scipy(f, self.vars, self.params))
     return jac
Beispiel #2
0
 def __init__(self, model, *args, **kwargs):
     """
     :param model: sympy expression.
     :param x: xdata to fit to.  NxM
     :param y: ydata             Nx1
     """
     super(BaseFit, self).__init__(*args, **kwargs)
     self.model = model
     # Get all parameters and variables from the model.
     self.vars, self.params = seperate_symbols(self.model)
     # Compile a scipy function
     self.scipy_func = sympy_to_scipy(self.model, self.vars, self.params)
Beispiel #3
0
 def jacobian(self):
     """
     Get the scipy functions for the Jacobian. This returns functions only, not values.
     :return: array of derivative functions in all parameters, not values.
     """
     if not self.__jac:
         self.__jac = []
         for param in self.params:
             # Differentiate to every param
             f = sympy.diff(self.model, param)
             # Make them into pythonic functions
             self.__jac.append(sympy_to_scipy(f, self.vars, self.params))
     return self.__jac
Beispiel #4
0
 def make_jac(constraint_lhs, p, x):
     """
     :param constraint_lhs: equation of a constraint. The lhs is assumed to be an eq, rhs a number.
     :param p: current value of the parameters to be evaluated.
     :return: numerical jacobian.
     """
     sym_jac = []
     for param in self.params:
         sym_jac.append(sympy.diff(constraint_lhs, param))
     ans = np.array(
         [sympy_to_scipy(jac, self.vars, self.params)(x, p) for jac in
          sym_jac]
     )
     return ans
Beispiel #5
0
    def get_constraints(self):
        """
            Turns self.constraints into a scipy compatible format.
            :return: dict of scipy compatile statements.
            """
        from sympy import Eq, Gt, Ge, Ne, Lt, Le

        cons = []
        types = {
            Eq: 'eq', Gt: 'ineq', Ge: 'ineq', Ne: 'ineq', Lt: 'ineq', Le: 'ineq'
        }

        def make_jac(constraint_lhs, p, x):
            """
            :param constraint_lhs: equation of a constraint. The lhs is assumed to be an eq, rhs a number.
            :param p: current value of the parameters to be evaluated.
            :return: numerical jacobian.
            """
            sym_jac = []
            for param in self.params:
                sym_jac.append(sympy.diff(constraint_lhs, param))
            ans = np.array(
                [sympy_to_scipy(jac, self.vars, self.params)(x, p) for jac in
                 sym_jac]
            )
            return ans

        for key, constraint in enumerate(self.constraints):
            # jac = make_jac(c, p)
            cons.append({
                'type': types[constraint.__class__],
                # Assume the lhs is the equation.
                'fun': lambda p, x, c: sympy_to_scipy(c.lhs, self.vars, self.params)(x, p),
                # Assume the lhs is the equation.
                'jac' : lambda p, x, c: make_jac(c.lhs, p, x),
                'args': (self.xdata, constraint)
            })
        cons = tuple(cons)
        return cons