def __init__( self, f, g, y, t=0.0, f_helpers=(), g_helpers=(), control_pars=(), seed=None, additive=False, do_cse=False, ): self.state = y self.n = len(self.state) self.t = t self.parameters = [] self.noises = [] self.noise_index = None self.new_y = None self.new_t = None self.RNG = np.random.RandomState(seed) self.additive = additive from jitcsde import t, y f_subs = list(reversed(f_helpers)) g_subs = list(reversed(g_helpers)) lambda_args = [t] for i in range(self.n): symbol = symengine.Symbol("dummy_argument_%i" % i) lambda_args.append(symbol) f_subs.append((y(i), symbol)) g_subs.append((y(i), symbol)) lambda_args.extend(control_pars) f_wc = list( ordered_subs(entry, f_subs).simplify(ratio=1) for entry in f()) g_wc = list( ordered_subs(entry, g_subs).simplify(ratio=1) for entry in g()) lambdify = symengine.LambdifyCSE if do_cse else symengine.Lambdify core_f = lambdify(lambda_args, f_wc) self.f = lambda t, Y: core_f(np.hstack([t, Y, self.parameters])) if self.additive: core_g = lambdify([t] + list(control_pars), g_wc) self.g = lambda t: core_g(np.hstack([t, self.parameters])) else: core_g = lambdify(lambda_args, g_wc) self.g = lambda t, Y: core_g(np.hstack([t, Y, self.parameters]))
def generate_f_lambda(self, simplify=None, do_cse=False): """ translates the symbolic derivative to a function using SymEngines `Lambdify` or `LambdifyCSE`. Parameters ---------- simplify : boolean Whether the derivative should be `simplified <http://docs.sympy.org/dev/modules/simplify/simplify.html>`_ (with `ratio=1.0`) before translating to C code. The main reason why you could want to enable this is if you expect your derivative not to be optimised and not be so large that simplifying takes a considerable amount of time. If `None`, this will be automatically disabled for `n>10`. do_cse : boolean Whether a common-subexpression detection, namely `LambdifyCSE`, should be used. """ self._prepare_lambdas() # working copy f_sym_wc = (ordered_subs(entry,self._lambda_subs) for entry in self.f_sym()) if simplify is None: simplify = self.n<=10 if simplify: f_sym_wc = (entry.simplify(ratio=1.0) for entry in f_sym_wc) lambdify = symengine.LambdifyCSE if do_cse else symengine.Lambdify core_f = lambdify(self._lambda_args,list(f_sym_wc)) # self.f = lambda t,Y,*c_pars: core_f(np.hstack([t,Y,c_pars])) self.f = _lambda_wrapper(core_f,self.control_pars) self.compile_attempt = False
def generate_f_lambda(self, simplify=None, do_cse=False): """ translates the symbolic derivative to a function using SymEngines `Lambdify` or `LambdifyCSE`. Parameters ---------- simplify : boolean Whether the derivative should be `simplified <http://docs.sympy.org/dev/modules/simplify/simplify.html>`_ (with `ratio=1.0`) before translating to C code. The main reason why you could want to enable this is if you expect your derivative not to be optimised and not be so large that simplifying takes a considerable amount of time. If `None`, this will be automatically disabled for `n>10`. do_cse : boolean Whether a common-subexpression detection, namely `LambdifyCSE`, should be used. """ self._prepare_lambdas() # working copy f_sym_wc = (ordered_subs(entry, self._lambda_subs) for entry in self.f_sym()) if simplify is None: simplify = self.n <= 10 if simplify: f_sym_wc = (entry.simplify(ratio=1.0) for entry in f_sym_wc) lambdify = symengine.LambdifyCSE if do_cse else symengine.Lambdify core_f = lambdify(self._lambda_args, list(f_sym_wc)) self.f = lambda t, Y: core_f(np.hstack([t, Y, self.control_par_values]) ) self.compile_attempt = False
def generate_jac_lambda(self, do_cse=False): """ translates the symbolic Jacobain to a function using SymEngines `Lambdify` or `LambdifyCSE`. If the symbolic Jacobian has not been generated, it is generated by calling `generate_jac_sym`. Parameters ---------- do_cse : boolean Whether a common-subexpression detection, namely `LambdifyCSE`, should be used. """ self._prepare_lambdas() jac_matrix = symengine.Matrix( [[ordered_subs(entry, self._lambda_subs) for entry in line] for line in self.jac_sym]) lambdify = symengine.LambdifyCSE if do_cse else symengine.Lambdify core_jac = lambdify(self._lambda_args, jac_matrix) self.jac = lambda t, Y: core_jac( np.hstack([t, Y, self.control_par_values])) self.compile_attempt = False
def generate_jac_lambda(self,do_cse=False): """ translates the symbolic Jacobain to a function using SymEngines `Lambdify` or `LambdifyCSE`. If the symbolic Jacobian has not been generated, it is generated by calling `generate_jac_sym`. Parameters ---------- do_cse : boolean Whether a common-subexpression detection, namely `LambdifyCSE`, should be used. """ self._prepare_lambdas() jac_matrix = symengine.Matrix([ [ordered_subs(entry,self._lambda_subs) for entry in line] for line in self.jac_sym ]) lambdify = symengine.LambdifyCSE if do_cse else symengine.Lambdify core_jac = lambdify(self._lambda_args,jac_matrix) # self.jac = lambda t,Y,*c_pars: core_jac(np.hstack([t,Y,c_pars])) self.jac = _lambda_wrapper(core_jac,self.control_pars) self.compile_attempt = False