(e, Omega, x, wsym)
return f, logw
e(x) ... the function
    Omega ... the mrv set
    wsym ... the symbol which is going to be used for w

    Returns the rewritten e in terms of w and log(w). See test_rewrite1()
    for examples and correct results.
e(x) ... the function
    Omega ... the mrv set
    wsym ... the symbol which is going to be used for w
e ( x ) ... the function Omega ... the mrv set wsym ... the symbol which is going to be used for w
def rewrite(e, Omega, x, wsym):
    """e(x) ... the function
    Omega ... the mrv set
    wsym ... the symbol which is going to be used for w

    Returns the rewritten e in terms of w and log(w). See test_rewrite1()
    for examples and correct results.
    """
    from sympy import ilcm
    if not isinstance(Omega, SubsSet):
        raise TypeError("Omega should be an instance of SubsSet")
    if len(Omega) == 0:
        raise ValueError("Length can not be 0")
    #all items in Omega must be exponentials
    for t in Omega.keys():
        if not t.func is exp:
            raise ValueError("Value should be exp")
    rewrites = Omega.rewrites
    Omega = list(Omega.items())

    nodes = build_expression_tree(Omega, rewrites)
    Omega.sort(key=lambda x: nodes[x[1]].ht(), reverse=True)

    # make sure we know the sign of each exp() term; after the loop,
    # g is going to be the "w" - the simplest one in the mrv set
    for g, _ in Omega:
        sig = sign(g.args[0], x)
        if sig != 1 and sig != -1:
            raise NotImplementedError('Result depends on the sign of %s' % sig)
    if sig == 1:
        wsym = 1/wsym  # if g goes to oo, substitute 1/w
    #O2 is a list, which results by rewriting each item in Omega using "w"
    O2 = []
    denominators = []
    for f, var in Omega:
        c = limitinf(f.args[0]/g.args[0], x)
        if c.is_Rational:
            denominators.append(c.q)
        arg = f.args[0]
        if var in rewrites:
            if not rewrites[var].func is exp:
                raise ValueError("Value should be exp")
            arg = rewrites[var].args[0]
        O2.append((var, exp((arg - c*g.args[0]).expand())*wsym**c))

    #Remember that Omega contains subexpressions of "e". So now we find
    #them in "e" and substitute them for our rewriting, stored in O2

    # the following powsimp is necessary to automatically combine exponentials,
    # so that the .subs() below succeeds:
    # TODO this should not be necessary
    f = powsimp(e, deep=True, combine='exp')
    for a, b in O2:
        f = f.subs(a, b)

    for _, var in Omega:
        assert not f.has(var)

    #finally compute the logarithm of w (logw).
    logw = g.args[0]
    if sig == 1:
        logw = -logw  # log(w)->log(1/w)=-log(w)

    # Some parts of sympy have difficulty computing series expansions with
    # non-integral exponents. The following heuristic improves the situation:
    exponent = reduce(ilcm, denominators, 1)
    f = f.subs(wsym, wsym**exponent)
    logw /= exponent

    return f, logw
def rewrite ( e , Omega , x , wsym ) : from sympy import ilcm if not isinstance ( Omega , SubsSet ) : raise TypeError ( "Omega should be an instance of SubsSet" ) if len ( Omega ) == 0 : raise ValueError ( "Length can not be 0" ) #all items in Omega must be exponentials for t in Omega . keys ( ) : if not t . func is exp : raise ValueError ( "Value should be exp" ) rewrites = Omega . rewrites Omega = list ( Omega . items ( ) ) nodes = build_expression_tree ( Omega , rewrites ) Omega . sort ( key = lambda x : nodes [ x [ 1 ] ] . ht ( ) , reverse = True ) # make sure we know the sign of each exp() term; after the loop, # g is going to be the "w" - the simplest one in the mrv set for g , _ in Omega : sig = sign ( g . args [ 0 ] , x ) if sig != 1 and sig != - 1 : raise NotImplementedError ( 'Result depends on the sign of %s' % sig ) if sig == 1 : wsym = 1 / wsym # if g goes to oo, substitute 1/w #O2 is a list, which results by rewriting each item in Omega using "w" O2 = [ ] denominators = [ ] for f , var in Omega : c = limitinf ( f . args [ 0 ] / g . args [ 0 ] , x ) if c . is_Rational : denominators . append ( c . q ) arg = f . args [ 0 ] if var in rewrites : if not rewrites [ var ] . func is exp : raise ValueError ( "Value should be exp" ) arg = rewrites [ var ] . args [ 0 ] O2 . append ( ( var , exp ( ( arg - c * g . args [ 0 ] ) . expand ( ) ) * wsym ** c ) ) #Remember that Omega contains subexpressions of "e". So now we find #them in "e" and substitute them for our rewriting, stored in O2 # the following powsimp is necessary to automatically combine exponentials, # so that the .subs() below succeeds: # TODO this should not be necessary f = powsimp ( e , deep = True , combine = 'exp' ) for a , b in O2 : f = f . subs ( a , b ) for _ , var in Omega : assert not f . has ( var ) #finally compute the logarithm of w (logw). logw = g . args [ 0 ] if sig == 1 : logw = - logw # log(w)->log(1/w)=-log(w) # Some parts of sympy have difficulty computing series expansions with # non-integral exponents. The following heuristic improves the situation: exponent = reduce ( ilcm , denominators , 1 ) f = f . subs ( wsym , wsym ** exponent ) logw /= exponent return f , logw