daredare.compilator.compilator

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

# Source Code for Module daredare.compilator.compilator

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