compilator.compilator

Package compilator :: Module compilator |
[hide private]
[frames] | no frames]

# Source Code for Module compilator.compilator

     1  from scipy import *
     2  from sympy import *
     3  from model.model import *
     4  from model.symbolic import *
     5
     6 -class Compilator:
     7      model = None
     8
     9 -    def __init__(self,model):
    10          self.model = model
    11
    12 -    def equations_to_function(self,eqs):
    13          variables = self.model.variables
    14          parameters = self.model.parameters
    15          exovariables = self.model.exovariables
    16
    17 -    def build_substitution_list(self,for_matlab=False,for_c=True):
    18          if for_matlab:
    19              mi = 1
    20          else:
    21              mi = 0;
    22          subs_dict = dict()
    23          for i in range(len(self.model.variables)):
    24              if for_c:
    25                  subs_dict[self.model.variables[i]] = "x("+str(i+ mi) + ")"
    26              else:
    27                  subs_dict[self.model.variables[i]] = "x["+str(i+ mi) + "]"
    28          exovariables_index = dict()
    29          for i in range(len(self.model.exovariables)):
    30              if for_c:
    31                  subs_dict[self.model.exovariables[i]] = "y("+str(i+mi)+")"
    32              else:
    33                  subs_dict[self.model.exovariables[i]] = "y["+str(i+mi)+"]"
    34          parameters_index = dict()
    35          for i in range(len(self.model.parameters)):
    36              if for_c:
    37                  subs_dict[self.model.parameters[i]] = "parm("+str(i+mi)+")"
    38              else:
    39                  subs_dict[self.model.parameters[i]] = "parm["+str(i+mi)+"]"
    40          for i in range(len(self.model.shocks)):
    41              # temporary
    42              if for_c:
    43                  subs_dict[self.model.shocks[i]] = "0"
    44              else:
    45                  subs_dict[self.model.shocks[i]] = "0"
    46          return subs_dict
    47
    48 -    def tabify_expression(self,eq,for_matlab=False,for_c=True):
    49          # works when all variables are expressed without lag
    50          subs_dict = self.build_substitution_list(for_matlab,not for_c)
    51          def f(expr):
    52              if expr.__class__ in [Variable,Parameter]:
    53                      vname = subs_dict[expr]
    54                      return(Symbol(vname))
    55              else:
    56                  return(expr)
    57          return self.model.map_function_to_expression(f,eq)
    58
    59 -    def get_gaps_static_python(self):
    60          current_equations = []
    61          current_equations = self.model.current_equations()
    62          tab_eq = []
    63          for eq in current_equations:
    64              #if eq.is_endogenous:
    65              tab_eq.append(self.tabify_expression(eq))
    66          # count endogenous equations (should be in Model)s:
    67          code  = "def gaps_static(x,y,parm):\n"
    68          #code += "    print('x',x)\n"
    69          #code += "    print('y',y)\n"
    70          #code += "    print('parm',parm)\n"
    71          code += "    gaps = zeros(%s)\n" % len(tab_eq)
    72          i = 0
    73          for eq in tab_eq:
    74              eq_s = str(eq.gap())
    75              code += "    gaps[" + str(i) + "] = " + eq_s + "\n"
    76              i = i + 1
    77          #code += "    print('gaps :', gaps)\n"
    78          code += "    return gaps"
    79          exec(code)
    80          print(code)
    81          #import psyco
    82          #psyco.bind(gaps_static)
    83          return(gaps_static)
    84
    85 -    def write_gaps_ccode_static(self):
    86          current_equations = self.model.current_equations()
    87          tab_eq = map(self.tabify_expression, current_equations)
    88          code = "py::tuple gaps(" + str(len(tab_eq)) + ");\n"
    89          for i in range(len(tab_eq)):
    90              eq_s = str(tab_eq[i].gap()).replace('**','^') # to be fixed with function pow()
    91              code += "gaps[" + str(i) + "] = " + eq_s + ";\n"
    92          code += "return_val = gaps;"
    93
    94          return(code)
    95