Minus sign changes bottom_up behavior of evalf

15 views
Skip to first unread message

Duane Nykamp

unread,
Oct 7, 2014, 5:47:22 PM10/7/14
to sy...@googlegroups.com
I am mystified as to why a minus sign changes bottom_up behavior when applying evalf. 

(I have modified bottom_up only by getting rid of the condition "if args != rv.args:" as otherwise it won't evalf integers.  See below)

With the function

    def initial_evalf(w):
        try:
            return w.evalf()
        except:
            return w


With a plus sign, the constant factor e^(1) is evaluated as a float.

In [47]: bottom_up(sympify("1+exp(3*z+1)"), initial_evalf, atoms=True)
Out[47]: 2.71828182845905*exp(3.0*z) + 1.0

With a minus sign, the constant factor stays as e^(1.0).

In [48]: bottom_up(sympify("1-exp(3*z+1)"), initial_evalf, atoms=True)
Out[48]: -1.0*exp(3.0*z + 1.0) + 1.0

I can't figure out what is making the difference.  I'm trying to normalize expressions to be the same format, but I haven't figured out how to get the same behavior here.

Thanks,
Duane

Here's my modification of bottom_up with the one condition deleted:

def bottom_up(rv, F, atoms=False, nonbasic=False):
    """Apply ``F`` to all expressions in an expression tree from the
    bottom up. If ``atoms`` is True, apply ``F`` even if there are no args;
    if ``nonbasic`` is True, try to apply ``F`` to non-Basic objects.
    """
    try:
        if rv.args:
            args = tuple([bottom_up(a, F, atoms, nonbasic)
                for a in rv.args])
            rv = rv.func(*args)
            rv = F(rv)
        elif atoms:
            rv = F(rv)
    except AttributeError:
        if nonbasic:
            try:
                rv = F(rv)
            except TypeError:
                pass

    return rv






Duane Nykamp

unread,
Oct 7, 2014, 8:42:18 PM10/7/14
to sy...@googlegroups.com
Never mind.  I could have sworn I tested this in the git version of sympy and it still had this problem.  But now, the bug seems to have been fixed in the git version.

I finally tracked it down.  For some reason, the older version of sympy was picking up a customized version of exp() in another file in the same module in one case but not the other.  I don't get it.  But, hey, if this is fixed in the git version, all is well!

Duane
Reply all
Reply to author
Forward
0 new messages