It's because the string form of the expression contains division of
numeric literals, which exec evaluates to floats. If you want to avoid
this, you should use sympify(), or use the srepr() form of the
expression instead of the str() form.

Aaron Meurer

On Wed, Sep 11, 2013 at 7:51 AM, Boris Kheyfets <[email protected]> wrote:
> Why this
>
> d2bdy2 = b_i.diff(y_i, 2)
> exec("pprint({{'{0}': {0}}})".format("d2bdy2"))
>
> is not equivalent to
>
> def assign_and_pprint(Name, Expr):
>     exec("{Name} = {Expr}".format(Name=Name, Expr=Expr))
>     exec("pprint({{'{Name}': {Name}}})".format(Name=Name))
>
> assign_and_pprint("d2bdy2", b_i.diff(y_i, 2))
>
> Compelete example:
>
> #!/usr/bin/python2.7
>
> from sympy import *
>
> def assign_and_pprint(Name, Expr):
>     exec("{Name} = {Expr}".format(Name=Name, Expr=Expr))
>     exec("pprint({{'{Name}': {Name}}})".format(Name=Name))
>
>
> var("""
> b_i y_i nu
> """, positive=True)
>
> b_i = 2**(S(2)/3) / (nu**(S(4)/3) * (y_i - 1)**(S(8)/3))
>
> d2bdy2 = b_i.diff(y_i, 2)
> exec("pprint({{'{0}': {0}}})".format("d2bdy2"))
>
> assign_and_pprint("d2bdy2", b_i.diff(y_i, 2))
>
> Which gives:
>
> ⎧                  2/3      ⎫
> ⎪              88⋅2         ⎪
> ⎨d2bdy2: ───────────────────⎬
> ⎪           4/3         14/3⎪
> ⎩        9⋅ν   ⋅(yᵢ - 1)    ⎭
> ⎧                          -1.33333333333333         -4.66666666666667⎫
> ⎨d2bdy2: 15.5212547303557⋅ν                 ⋅(yᵢ - 1)                 ⎬
> ⎩                                                                     ⎭
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sympy.
> For more options, visit https://groups.google.com/groups/opt_out.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to