Sage: Ticket #18259: comparison of symbolic functions
https://trac.sagemath.org/ticket/18259
<p>
We have the following strange (wrong) behavior:
</p>
<pre class="wiki">sage: f(x) = 2*x
sage: bool(f == 2*x)
True
</pre><p>
On the other hand we have
</p>
<pre class="wiki">sage: f(x) = 2*x
sage: g(y) = 2*y
sage: bool(f == g)
False
</pre>en-usSagehttps://trac.sagemath.org/chrome/site/logo_sagemath_trac.png
https://trac.sagemath.org/ticket/18259
Trac 1.1.6dkrennMon, 20 Apr 2015 12:46:48 GMT
https://trac.sagemath.org/ticket/18259#comment:1
https://trac.sagemath.org/ticket/18259#comment:1
<p>
This came up in <a class="needs_info ticket" href="https://trac.sagemath.org/ticket/18092" title="enhancement: evaluating symbolic expressions (without conversion to SR, i.e., ... (needs_info)">#18092</a>.
</p>
TicketnbruinTue, 21 Apr 2015 15:46:44 GMT
https://trac.sagemath.org/ticket/18259#comment:2
https://trac.sagemath.org/ticket/18259#comment:2
<p>
This is a consequence of how coercion and comparison are implemented:
</p>
<pre class="wiki">sage: var('y')
y
sage: f(x)=x*y
sage: A=x*y
sage: cSR=parent(f)
sage: cSR.coerce_map_from(SR)
Conversion map:
From: Symbolic Ring
To: Callable function ring with arguments (x,)
sage: SR.coerce_map_from(cSR) is None
True
</pre><p>
So, A coerces into the parent of f (and not the other way around): That means that for equality testing, A is coerced into the parent of f and then the comparison is done: comparison testing in sage is defined to be "equal up to coercion" (which probably necessarily breaks in all kinds of particular cases).
</p>
<p>
If you do *not* want these things to compare equal then you should break the coercion. That might be reasonable. Conversion can still exist. Currently you can do:
</p>
<pre class="wiki">sage: f+y^2
x |--> x*y + y^2
</pre><p>
without coercion but with conversion, this would need to be:
</p>
<pre class="wiki">sage: f+cSR(y^2)
x |--> x*y + y^2
</pre><p>
which does not seem unreasonable to me.
</p>
TicketvdelecroixTue, 21 Apr 2015 15:57:13 GMT
https://trac.sagemath.org/ticket/18259#comment:3
https://trac.sagemath.org/ticket/18259#comment:3
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/18259#comment:2" title="Comment 2">nbruin</a>:
Hi Niles,
</p>
<p>
Why not modifying the comparison codes for Callable function ring?
</p>
<blockquote class="citation">
<p>
If you do *not* want these things to compare equal then you should break the coercion. That might be reasonable. Conversion can still exist. Currently you can do:
</p>
<pre class="wiki">sage: f+y^2
x |--> x*y + y^2
</pre><p>
without coercion but with conversion, this would need to be:
</p>
<pre class="wiki">sage: f+cSR(y^2)
x |--> x*y + y^2
</pre><p>
which does not seem unreasonable to me.
</p>
</blockquote>
<p>
I definitely would like to be able to do <code>f+1</code> without an error!
</p>
<p>
Vincent
</p>
TicketnbruinTue, 21 Apr 2015 16:23:46 GMT
https://trac.sagemath.org/ticket/18259#comment:4
https://trac.sagemath.org/ticket/18259#comment:4
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/18259#comment:3" title="Comment 3">vdelecroix</a>:
</p>
<blockquote class="citation">
<p>
Why not modifying the comparison codes for Callable function ring?
</p>
</blockquote>
<p>
[...]
</p>
<blockquote class="citation">
<p>
I definitely would like to be able to do <code>f+1</code> without an error!
</p>
</blockquote>
<p>
The second would probably still work if the integers still coerce into <code>cSR</code> (and since <code>cSR</code> inherits from <code>SR</code> it would be a lot of work to break that). But it's a direct consequence that if <code>f+1</code> works then <code>bool( cSR(1) == 1)</code> will be true, basically because <code>cSR(1)-1</code> is then 0 (instead of an error), which is what happens in the example in the ticket too.
</p>
<p>
If you're going to change the comparison for the callable function ring you'd be letting <code>cSR</code> behave completely different from other sage parents. It would also be a lot of work because currently comparison on <code>cSR</code> is just inherited from <code>SR</code>.
</p>
<p>
The implementation of comparison tests on <code>SR</code> only comes into play after the coercion framework has had its way with the arguments.
</p>
Ticket