def sort_assignments_topologically( assignments: Sequence[Union[Assignment, Node]] ) -> List[Union[Assignment, Node]]: """Sorts assignments in topological order, such that symbols used on rhs occur first on a lhs""" edges = [] for c1, e1 in enumerate(assignments): if hasattr(e1, 'lhs') and hasattr(e1, 'rhs'): symbols = [e1.lhs] elif isinstance(e1, Node): symbols = e1.symbols_defined else: raise NotImplementedError( f"Cannot sort topologically. Object of type {type(e1)} cannot be handled." ) for lhs in symbols: for c2, e2 in enumerate(assignments): if isinstance(e2, Assignment) and lhs in e2.rhs.free_symbols: edges.append((c1, c2)) elif isinstance(e2, Node) and lhs in e2.undefined_symbols: edges.append((c1, c2)) return [ assignments[i] for i in sp.topological_sort((range(len(assignments)), edges)) ]
def fix_parameters(self, **kwargs): """Takes an estimated parameter from a DSGESelf and converts it to a calibrated one.""" for para, value in kwargs.items(): para = Parameter(para) self['par_ordering'].remove(para) self['other_para'].append(para) self['para_func'][str(para)] = value context_tuple = ([(p, Parameter(p)) for p in self.parameters] + [(p.name, p) for p in self['other_para']]) context = dict(context_tuple) context['exp'] = sympy.exp context['log'] = sympy.log to_replace = {} for p in self['other_para']: to_replace[p] = eval(str(self['para_func'][p.name]), context) to_replace = list(to_replace.items()) from itertools import combinations, permutations edges = [(i,j) for i,j in permutations(to_replace,2) if type(i[1]) not in [float,int] and i[1].has(j[0])] from sympy import default_sort_key, topological_sort edges = [(v[0],dep) for v in to_replace for dep in sympy.sympify(v[1]).atoms(Parameter) if dep in self['other_para']] para_func = topological_sort([self['other_para'], edges], default_sort_key)[::-1] self['other_para'] = para_func return self
def transformation(self, prob: Problem): if not prob.sympified: raise ValueError('Problem must be sympified.') if len(prob.quantities) == 0: return prob quantities = prob.quantities dependencies = [] for quantity_i, quantity_j in permutations(quantities, 2): if quantity_i.expr.has(quantity_j.sym): dependencies.append((quantity_i, quantity_j)) try: ordered_quantities = sympy.topological_sort( (quantities, dependencies), key=lambda _q: _q.name) except ValueError: raise ValueError('Cycle found in dependencies in quantities.') for quantity in ordered_quantities: prob.subs_all(quantity.sym, quantity.expr) prob.quantities.remove(quantity) # TODO add quantity calculation to Trajectory class return prob
def smc(model, t0=0): import sympy from sympy.printing import fcode cmodel = model.compile_model() template = fortran_model #open('fortran_model.f90').read() write_prior_file(cmodel.prior, '.') system_matrices = model.python_sims_matrices(matrix_format='symbolic') npara = len(model.parameters) para = sympy.IndexedBase('para', shape=(npara + 1, )) fortran_subs = dict( zip([sympy.symbols('garbage')] + model.parameters, para)) fortran_subs[0] = 0.0 fortran_subs[1] = 1.0 fortran_subs[100] = 100.0 fortran_subs[2] = 2.0 fortran_subs[400] = 400.0 fortran_subs[4] = 4.0 context = dict([(p.name, p) for p in model.parameters + model['other_para']]) context['exp'] = sympy.exp context['log'] = sympy.log to_replace = {} for p in model['other_para']: to_replace[p] = eval(str(model['para_func'][p.name]), context) to_replace = list(to_replace.items()) print(to_replace) from itertools import combinations, permutations edges = [(i, j) for i, j in permutations(to_replace, 2) if type(i[1]) not in [float, int] and i[1].has(j[0])] from sympy import default_sort_key, topological_sort para_func = topological_sort([to_replace, edges], default_sort_key) to_write = [ 'GAM0', 'GAM1', 'PSI', 'PPI', 'self%QQ', 'DD2', 'self%ZZ', 'self%HH' ] fmats = [ fcode((mat.subs(para_func)).subs(fortran_subs), assign_to=n, source_format='free', standard=95, contract=False) for mat, n in zip(system_matrices, to_write) ] sims_mat = '\n\n'.join(fmats) template = template.format(model=model, yy=cmodel.yy, p0='', t0=t0, sims_mat=sims_mat) return template
def smc(model, t0=0): import sympy from sympy.printing import fcode cmodel = model.compile_model() template = fortran_model # open('fortran_model.f90').read() write_prior_file(cmodel.prior, ".") system_matrices = model.python_sims_matrices(matrix_format="symbolic") npara = len(model.parameters) para = sympy.IndexedBase("para", shape=(npara + 1,)) from .symbols import Parameter fortran_subs = dict( zip( [sympy.symbols("garbage")] + [Parameter(px) for px in model.parameters], para, ) ) fortran_subs[0] = 0.0 fortran_subs[1] = 1.0 fortran_subs[100] = 100.0 fortran_subs[2] = 2.0 fortran_subs[400] = 400.0 fortran_subs[4] = 4.0 context_tuple = [(p, Parameter(p)) for p in model.parameters] + [ (p.name, p) for p in model["other_para"] ] context = dict(context_tuple) context["exp"] = sympy.exp context["log"] = sympy.log to_replace = {} for p in model["other_para"]: to_replace[p] = eval(str(model["para_func"][p.name]), context) to_replace = list(to_replace.items()) print(to_replace) from itertools import combinations, permutations edges = [ (i, j) for i, j in permutations(to_replace, 2) if type(i[1]) not in [float, int] and i[1].has(j[0]) ] from sympy import default_sort_key, topological_sort para_func = topological_sort([to_replace, edges], default_sort_key) to_write = ["GAM0", "GAM1", "PSI", "PPI", "self%QQ", "DD2", "self%ZZ", "self%HH"] fmats = [ fcode( (mat.subs(para_func)).subs(fortran_subs), assign_to=n, source_format="free", standard=95, contract=False, ) for mat, n in zip(system_matrices, to_write) ] sims_mat = "\n\n".join(fmats) template = template.format( model=model, yy=cmodel.yy, p0="", t0=t0, sims_mat=sims_mat ) return template