On Jun 21, 2012, at 2:01 AM, Pietro Battiston <[email protected]> wrote:
> Hello,
>
> first, since I'm new on this list, let me thank you for this nice piece
> of software, which has revolutioned my way of preparing exercises for my
> university students.
That's great to hear.
>
> I'm having some trouble in understanding how Sympy treats the conversion
> of Python integers to its own type.
>
> Take the following code:
>
>
> a = 1
> b = 2
> c = Symbol( "c" )
>
> constr = a - b * c
>
> pprint( constr )
> pprint( constr.subs( {a: Symbol( "d" )} ) )
> pprint( constr.subs( {b: Symbol( "d" )} ) )
>
>
> It already looks strange to me, because while the first "subs" does what
> I would like it to do (and hence "pprint" prints "-2⋅c + d"), the second
> doesn't (and "pprint" prints "constr" unchanged). Now, I would be fine
> with not being able to replace integers with something else, but I'm
> puzzled with the incoherence.
>
> But what I can't really understand is why everything works as I intended
> if I just set "b" to 1 instead than 2. And the last "subs" stops working
> again if I set "a" to 2 instead than 1.
>
> Is there something I'm missing, or is my first "subs" just exploiting
> some secret magic which works only by chance and I should not rely on?
I would say that this is the case. I am actually surprised that subs
replaces ints, and I wouldn't rely on it. For example, if b were 1 and
a were 2, it would never work because the final expression would not
actually contain a 1. I do understand why this happens from a
technical standpoint, but it's untested and the behavior could easily
be removed in a future version. For what it's worth, though, both seem
to work in the development version.
What I would do instead is use symbols for a and b and use subs to
replace them with integers whenever you need them. That will always
work.
Aaron Meurer
>
> thanks for any clarification
>
> Pietro
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy?hl=en.