On Mar 30, 6:52 pm, jegerjensen <[email protected]> wrote:
> > > In [1]: if x < y and x > y:
> > > ...: print "I would not expect to see this printed"
>
> > But why would you want to do this? "if x < y" doesn't make sense if x and
> > y are symbols.
>
> Because it could make sense to compare python objects that way. (but
> see below)
>
> > But then we loose the shortcut x < y => Lt(x, y).
>
> Actually no, x < y can return Lt(x, y). The comparison is only handed
> over to python when you try to evaluate it with bool(Lt(x, y)). (I
> think I am only describing how things work today, but from a different
> perspective.)
>
> > This will change in Python 3. There, objects have to be the same type to
> > be compared.
>
> Oh, that changes the situation. The future python programmer will not
> expect the comparison operators to apply everywhere, so then I can
> understand your point that "x < y doesn't make sense if x and y are
> symbols."
>
> But still, it would be easy to work with a rule that says that
> bool(Relational(...)) hands over the
> evaluation to python. This would mean that "if x < y:" acts like the
> comparison python would normally do, and "if ask(x < y, Assume(x-
> y,Q.whatever):" tries to determine the mathematical question.
I strongly discourage the usage of bool() for such things, you should
rather use a method, like .doit() or something. It confuses the math's
and python's concept of truth.
You could have code like
if x:
do something
which tests for example if x was defined, so it should just return
True (python's concept). But mathematically, rather None should be
returned, because we do not know, whether the symbol is True or not.
And it is not even technically possible, because ask() would quite
often return None, which can not be returned by bool().
> I think the central point in my objection is that if you write a piece
> of python code using the sympy libraries, it would be very confusing
> if some comparisons turn out to be always True. Because of this, I
> suggest that relational objects should not simply return True to
> indicate that they are ``nonzero''. Since the mathematical problem
> can be non-trivial or impossible to determine, that is not an option,
> so instead the comparisons should be handed over to python, completely
> transparent to the user.
In my opinion they should just return True, and the comparison should
not be abused. Currently it's usage is not very transparent I think.
Maybe it should be better completely disabled.
>
> All that is needed if we go for this solution is a section in the
> "gotchas and pitfalls" explaining what the user should do to evaluate
> a mathematical inequality, and what "if x<y:" really means.
The operators '==' and '!=' are strictly necessary in a non-
mathematical way. Currently the relational operators are used
mathematically. If want to be strictly consistent, we have to remove
them. Also, the only work for real symbols, not for general symbols.
This can be a nice shortcut, but it is dangerous if you confuse them
with the non-mathematical operators.
Also, should (x < y) raise an exception when x and y are not real or
automatically create such assumptions?
Vinzent
--
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.