def __init__(self, nddistr=None, d=None): super(TwoVarsModel, self).__init__(nddistr, [d]) self.eliminate_other([d]) self.d = d self.vars = [] self.symvars = [] for var in nddistr.Vars: #self.free_rvs: self.vars.append(var) self.symvars.append(var.getSymname()) #print "=====", self.vars #print self.symvars #print self.dep_rvs #print self.rv_to_equation self.symop = self.rv_to_equation[d] if len(self.vars) != 2: raise Exception("use it with two variables") x = self.symvars[0] y = self.symvars[1] z = sympy.Symbol("z") self.fun_alongx = eq_solve(self.symop, z, y)[0] self.fun_alongy = eq_solve(self.symop, z, x)[0] self.lfun_alongx = my_lambdify([x, z], self.fun_alongx, "numpy") self.lfun_alongy = my_lambdify([y, z], self.fun_alongy, "numpy") self.Jx = 1 * sympy.diff(self.fun_alongx, z) #print "Jx=", self.Jx #print "fun_alongx=", self.fun_alongx self.Jy = 1 * sympy.diff(self.fun_alongy, z) self.lJx = my_lambdify([x, z], self.Jx, "numpy") self.lJy = my_lambdify([y, z], self.Jy, "numpy") self.z = z
def var_change_helper(self, free_var, dep_var): """Compute inverse transformation and Jacobian for substituting dep_var for free_var.""" free_var = self.prepare_var(free_var) dep_var = self.prepare_var(dep_var) # inverve transformation equation = self.rv_to_equation[dep_var] - dep_var.getSymname() solutions = eq_solve(self.rv_to_equation[dep_var], dep_var.getSymname(), free_var.getSymname()) var_changes = [] for uj in solutions: # remove complex valued solutions vj = uj.atoms(sympy.Symbol) hvj = {} for v in vj: #print self.sym_to_rv[v].range() hvj[v] = self.sym_to_rv[v].range()[1] if len(solutions) > 1 and not sympy.im(uj.subs(hvj)) == 0: continue uj_symbols = list(sorted(uj.atoms(sympy.Symbol), key=str)) inv_transf = my_lambdify(uj_symbols, uj, "numpy") inv_transf_vars = [self.sym_to_rv[s] for s in uj_symbols] if params.models.debug_info: #print "vars to change: ", free_var.getSymname(), " <- ", dep_var.getSymname(), "=", self.rv_to_equation[dep_var] #print "equation: ", dep_var.getSymname(), "=", self.rv_to_equation[dep_var] print("substitution: ", free_var.getSymname(), "=", uj, end=' ') #print "variables: ", uj_symbols, inv_transf_vars # Jacobian #J = sympy.Abs(sympy.diff(uj, dep_var.getSymname())) J = sympy.diff(uj, dep_var.getSymname()) print(J.atoms()) J_symbols = list(sorted(J.atoms(sympy.Symbol), key=str)) if len(J_symbols) > 0: jacobian_vars = [self.sym_to_rv[s] for s in J_symbols] jacobian = my_lambdify(J_symbols, J, "numpy") jacobian = NDFun(len(jacobian_vars), jacobian_vars, jacobian, safe=True, abs=True) else: jacobian = NDConstFactor(abs(float(J))) jacobian_vars = [] if params.models.debug_info: print("; Jacobian=", J) #print "variables: ", J_symbols, jacobian_vars var_changes.append((uj, inv_transf, inv_transf_vars, jacobian)) return var_changes, equation
def var_change_helper(self, free_var, dep_var): """Compute inverse transformation and Jacobian for substituting dep_var for free_var.""" free_var = self.prepare_var(free_var) dep_var = self.prepare_var(dep_var) # inverve transformation equation = self.rv_to_equation[dep_var] - dep_var.getSymname() solutions = eq_solve(self.rv_to_equation[dep_var], dep_var.getSymname(), free_var.getSymname()) var_changes = [] for uj in solutions: # remove complex valued solutions vj = uj.atoms(sympy.Symbol) hvj = {} for v in vj: #print self.sym_to_rv[v].range() hvj[v]=self.sym_to_rv[v].range()[1] if len(solutions)>1 and not sympy.im(uj.subs(hvj))==0: continue uj_symbols = list(sorted(uj.atoms(sympy.Symbol), key = str)) inv_transf = my_lambdify(uj_symbols, uj, "numpy") inv_transf_vars = [self.sym_to_rv[s] for s in uj_symbols] if params.models.debug_info: #print "vars to change: ", free_var.getSymname(), " <- ", dep_var.getSymname(), "=", self.rv_to_equation[dep_var] #print "equation: ", dep_var.getSymname(), "=", self.rv_to_equation[dep_var] print("substitution: ", free_var.getSymname(), "=", uj, end=' ') #print "variables: ", uj_symbols, inv_transf_vars # Jacobian #J = sympy.Abs(sympy.diff(uj, dep_var.getSymname())) J = sympy.diff(uj, dep_var.getSymname()) print(J.atoms()) J_symbols = list(sorted(J.atoms(sympy.Symbol), key = str)) if len(J_symbols) > 0: jacobian_vars = [self.sym_to_rv[s] for s in J_symbols] jacobian = my_lambdify(J_symbols, J, "numpy") jacobian = NDFun(len(jacobian_vars), jacobian_vars, jacobian, safe = True, abs = True) else: jacobian = NDConstFactor(abs(float(J))) jacobian_vars = [] if params.models.debug_info: print("; Jacobian=", J) #print "variables: ", J_symbols, jacobian_vars var_changes.append((uj, inv_transf, inv_transf_vars, jacobian)) return var_changes, equation
def solveCutsX(self, fun, ay, by): axc = eq_solve(fun, ay, self.symvars[0])[0] bxc = eq_solve(fun, by, self.symvars[0])[0] return (axc, bxc)