A more compact form:
*bottom_up(lambda x: Integer(x) if isinstance(x, (float, Float)) else
x)(expression)*
On Monday, 14 September 2015 23:13:08 UTC+2, Francesco Bonazzi wrote:
>
> Try this one:
>
> n [1]: z = symbols('z')
>
> In [2]: expr = 2*z
>
> In [3]: e2 = expr.evalf()
>
> In [4]: e2
> Out[4]: 2.0⋅z
>
> In [5]: def exact_float_to_int(x):
> ...: if isinstance(x, (float, Float)):
> ...: return Integer(x)
> ...: return x
> ...:
>
> In [6]: from sympy.strategies.traverse import bottom_up
>
> In [7]: bottom_up(exact_float_to_int)(e2)
> Out[7]: 2⋅z
>
>
> Explanation: SymPy expressions are trees (data structures), *bottom_up*
> applies the defined function from the bottom nodes of the expression tree
> up to the trunk.
>
> On Monday, 14 September 2015 21:18:21 UTC+2, Christopher Ahern wrote:
>>
>> For an expression evalf converts integers to floats.
>>
>> z = symbols('z')
>> expr = 2*z
>> expr.evalf()
>>
>> This yields 2.0*z. Assuming that the coefficients in the expression are
>> actually integers, is there a way to convert them from floats to integers
>> in SymPy while leaving everything else as is?
>>
>
--
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.
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/0b353841-c4a7-488d-a946-d0fd5e372849%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.