Exemplo n.º 1
0
def numdif(model, v, rhv, delta=0.005, silent=True):
    #        print('**',model.allvar[v]['terms']['frml'])

    nt = model.allvar[v]['terms']
    assignpos = nt.index(model.aequalterm)  # find the position of =
    rhsterms = nt[assignpos + 1:-1]
    vterm = udtryk_parse(rhv)[0]
    plusterm = udtryk_parse(f'({rhv}+{delta/2})', funks=model.funks)
    minusterm = udtryk_parse(f'({rhv}-{delta/2})', funks=model.funks)
    plus = itertools.chain.from_iterable(
        [plusterm if t == vterm else [t] for t in rhsterms])
    minus = itertools.chain.from_iterable(
        [minusterm if t == vterm else [t] for t in rhsterms])
    eplus = f'({"".join(tout(t) for t in plus)})'
    eminus = f'({"".join(tout(t) for t in minus)})'
    expression = f'({eplus}-{eminus})/{delta}'
    if not silent:
        print(expression)
    return expression
Exemplo n.º 2
0
 def makelag(var):
     vterm = udtryk_parse(var)[0]
     if vterm.lag:
         if vterm.lag[0] == '-':
             return f'{vterm.var}___lag___{vterm.lag[1:]}'
         elif vterm.lag[0] == '+':
             return f'{vterm.var}___lead___{vterm.lag[1:]}'
         else:
             return f'{vterm.var}___per___{vterm.lag}'
     else:
         return f'{vterm.var}___lag___0'
Exemplo n.º 3
0
def fouteval(model, databank):
    ''' takes a dict of derivatives for a model and makes a function which returns a function which evaluates the 
    derivatives in a period.
    The derivatives is both returned from the function and places in \n
    :model.difvalue'''
    columnsnr = model.get_columnsnr(databank)
    fib = []
    fib.append('def make_diffcalculate():\n')
    fib.append('  def diffcalculate(model,databank,periode):\n')
    fib.append('    ' + 'from math import exp, log \n')
    fib.append('    ' + 'import sys\n')
    fib.append('    ' + 'row=databank.index.get_loc(periode)\n')
    fib.append('    ' + 'values=databank.values\n')
    fib.append('    ' + 'diffvalue={} \n')
    fib.append('    ' + 'try :\n')
    for i, v in enumerate(sorted(model.diffendocur)):
        #        print(v)
        if i >= 20000000:  # to make small tests calculations
            break
        fib.append('        ' + 'diffvalue["' + v + '"]={} \n')
        for e in sorted(model.diffendocur[v]):
            #            print('   ',e)
            if e == 'D' + v or e == 'Z' + v: continue
            if e == v + '_D': continue  # dont want dummyes
            #            print(v.ljust(l),e.ljust(l),model.diffendocur[v][e],file=f)
            fib.append('        ' + 'diffvalue["' + v + '"]["' + e + '"]=')
            terms = udtryk_parse(settozero(str(model.diffendocur[v][e])))
            for t in terms:
                if t.op:
                    ud = t.op.lower()
                elif t.number:
                    ud = t.number
                elif t.var:
                    if t.lag == '':
                        ud = 'values[row,' + str(columnsnr[t.var]) + ']'
                    else:
                        ud = 'values[row' + t.lag + ',' + str(
                            columnsnr[t.var]) + ']'
                fib.append(ud)
            fib.append('\n')
    fib.append('    ' + 'except :\n')
    fib.append('    ' + '   ' + 'print("Error in",sys.exc_info())\n')
    fib.append('    ' + '   ' + 'raise\n')
    fib.append('    ' + 'model.diffvalue=diffvalue\n')
    fib.append('    ' + 'return diffvalue\n')
    fib.append('  return diffcalculate\n')
    #    print('Function to evaluate derivatives of',model.name,'created')
    #        print (fib)
    return ''.join(fib)