Re: [sage-devel] Re: How should ordering of constants be defined?

2018-02-20 Thread Erik Bray
On Sat, Feb 17, 2018 at 8:22 AM, Ralf Stephan  wrote:
> I'm afraid the issue is more complicated.
>
> On Friday, February 16, 2018 at 5:04:30 PM UTC+1, Erik Bray wrote:
>>
>> On Python 2 this works:
>>
>> sage: bool(pi <= pi)
>> True
>>
>> This works fine because the Constant class implements __eq__, and so
>> if asking if pi <= pi that's good enough for it.
>
>
> Actually Constants.__eq__ is never called here. Pi is an expression not a
> constant. With expression relations ultimately Expression.__nonzero__ does
> all the work. It calls OP(pi.pyobject(), pi.pyobject()) with OP =
> operator.le. This calls Expression._richcmp_(pi, pi, Py_LT) (why?) which
> consults Pynac. Pynac checks if LHS-RHS is trivially zero which it is and
> returns True. No floating point involved, just symbolics.

Right, but pi.pyobject() is sage.symbolic.constants.Pi, a subclass of
Constant, so this invokes Constant.__le__ (when we call __nonzero__)
which isn't defined on Python 3 (on Python 2 it has a default
implementation that first checks equality at least, and then falls
back on a nonsensical < implementation).

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.


Re: [sage-devel] Re: How should ordering of constants be defined?

2018-02-20 Thread Erik Bray
On Sat, Feb 17, 2018 at 8:47 AM, Ralf Stephan  wrote:
> On Saturday, February 17, 2018 at 8:22:13 AM UTC+1, Ralf Stephan wrote:
>>
>> This baffles me and I would like to know why the computation differs from
>> the above.
>
>
> Ah ok, _richcmp_ is Py2 specific. Then we want to do the symbolic check
> before the inexact ones by changing Expression.__nonzero__(). Please cc me
> on the ticket.

I haven't made a ticket yet, I don't think, but I will when I do.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.


[sage-devel] Re: How should ordering of constants be defined?

2018-02-16 Thread Ralf Stephan
On Saturday, February 17, 2018 at 8:22:13 AM UTC+1, Ralf Stephan wrote:
>
> This baffles me and I would like to know why the computation differs from 
> the above.
>

Ah ok, _richcmp_ is Py2 specific. Then we want to do the symbolic check 
before the inexact ones by changing Expression.__nonzero__(). Please cc me 
on the ticket.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.


[sage-devel] Re: How should ordering of constants be defined?

2018-02-16 Thread Ralf Stephan
On Saturday, February 17, 2018 at 8:22:13 AM UTC+1, Ralf Stephan wrote:
>
> IThis calls Expression._richcmp_(pi, pi, Py_LT)
>
 
Py_LE of course.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.


[sage-devel] Re: How should ordering of constants be defined?

2018-02-16 Thread Ralf Stephan
I'm afraid the issue is more complicated.

On Friday, February 16, 2018 at 5:04:30 PM UTC+1, Erik Bray wrote:
>
> On Python 2 this works: 
>
> sage: bool(pi <= pi) 
> True 
>
> This works fine because the Constant class implements __eq__, and so 
> if asking if pi <= pi that's good enough for it.


Actually Constants.__eq__ is never called here. Pi is an expression not a 
constant. With expression relations ultimately Expression.__nonzero__ does 
all the work. It calls OP(pi.pyobject(), pi.pyobject()) with OP = 
operator.le. This calls Expression._richcmp_(pi, pi, Py_LT) (why?) which 
consults Pynac. Pynac checks if LHS-RHS is trivially zero which it is and 
returns True. No floating point involved, just symbolics.

> So on Python 3 we get: 

>
> sage: bool(pi <= pi) 
> TypeError Traceback (most recent call 
> last) 
> ... 
> TypeError: '<=' not supported between instances of 'Pi' and 'Pi' 
>

This baffles me and I would like to know why the computation differs from 
the above.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.


[sage-devel] Re: How should ordering of constants be defined?

2018-02-16 Thread Volker Braun
Comparing two named real constants by float value sounds good to me.

In general, when comparing general symbolic expressions, there are some 
numerical tests with increcasing accuracy iirc...

 



On Friday, February 16, 2018 at 5:04:30 PM UTC+1, Erik Bray wrote:
>
> Hi, 
>
> In working on the Python 3 port of Sage I ran into a problem with 
> constants defined in sage.symbolic.constants (there are only a few). 
>
> On Python 2 this works: 
>
> sage: bool(pi <= pi) 
> True 
>
> This works fine because the Constant class implements __eq__, and so 
> if asking if pi <= pi that's good enough for it. 
>
> However, on Python 3 <= will explicitly call __le__ if it's defined 
> and raise a TypeError otherwise.  Or at the very least __lt__ must be 
> implemented, and with __eq__ and __lt__ defined it's possible to 
> derive the other comparison operators. 
>
> So on Python 3 we get: 
>
> sage: bool(pi <= pi) 
> TypeError Traceback (most recent call 
> last) 
> ... 
> TypeError: '<=' not supported between instances of 'Pi' and 'Pi' 
>
> So I could define __lt__ between Constants and fix this.  The question 
> is how it should be defined.  That is, to what level of precision? 
> For the constants we have defined, taking their float values should be 
> good enough for comparison between constants.  But I'm not sure if 
> that's good enough in general...? 
>
> Thanks, 
> E 
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.