Ejemplo n.º 1
0
def diff(t,x):
    match = Match()
    if match.match('a+b', t):
        return '{diff_a} + {diff_b}'.format(diff_a=diff(match.values['a'], x), diff_b=diff(match.values['b'], x))
    elif match.match('a-b', t):
        return '{diff_a} - {diff_b}'.format(diff_a=diff(match.values['a'], x), diff_b=diff(match.values['b'], x))
    elif match.match('a*b', t):
        return '{diff_a} * {b} + {a} * {diff_b}'.format(diff_a=diff(match.values['a'], x), b=match.values['b'], a=match.values['a'], diff_b=diff(match.values['b'], x))
    elif match.match('a/b', t):
        return '({diff_a} * {b} - {a} * {diff_b}) / {b} * {b}'.format(diff_a=diff(match.values['a'], x), diff_b=diff(match.values['b'], x), a=match.values['a'], b=match.values['b'])
    elif match.match('a**b', t):
        return diff('exp({b} * ln({a}))'.format(a=match.values['a'], b=match.values['b']), x)
    elif match.match('ln(a)', t):
        return '{diff_a} / {a}'.format(diff_a=diff(match.values['a'], x), a=match.values['a'])
    elif match.match('exp(a)', t):
        return '{diff_a} * exp({a})'.format(diff_a=diff(match.values['a'], x), a=match.values['a'])
    elif match.is_variable(t) and t == x:
        return '1'
    elif match.is_variable(t):
        return '0'
    elif match.is_number(t):
        return '0'
Ejemplo n.º 2
0
def evaluate(f, i):
    match = Match()
    if match.is_variable(f):
        return i[f]
    elif match.match('!g', f):
        return not evaluate(match.values['g'], i)
    elif match.match('g && h', f):
        return evaluate(match.values['g'], i) and evaluate(match.values['h'], i)
    elif match.match('g || h', f):
        return evaluate(match.values['g'], i) or evaluate(match.values['h'], i)
    elif match.match('g => h', f):
        return not(evaluate(match.values['g'], i)) or evaluate(match.values['h'], i)
    elif match.match('g <==> h', f):
        return evaluate(match.values['g'], i) == evaluate(match.values['h'], i)
    else:
        raise SyntaxError('Syntax error in evaluate(%s,%s)' % (f, i))