Sage: Ticket #14326: Substituting numeric one in symbolic expression gives symbolic one
https://trac.sagemath.org/ticket/14326
<p>
consider the following in Sage 5.8:
</p>
<pre class="wiki">sage: u(n) = n^100 / 100^n; u(1.)
1/100
</pre><p>
This is inconsistent with:
</p>
<pre class="wiki">sage: n=1.; n^100 / 100^n
0.0100000000000000
</pre><p>
and with:
</p>
<pre class="wiki">sage: v = lambda(n): n^100 / 100^n; v(1.)
0.0100000000000000
</pre><p>
and:
</p>
<pre class="wiki">sage: def w(n): return n^100 / 100^n
sage: w(1.)
0.0100000000000000
</pre>en-usSagehttps://trac.sagemath.org/chrome/site/logo_sagemath_trac.png
https://trac.sagemath.org/ticket/14326
Trac 1.1.6zimmermaThu, 21 Mar 2013 08:30:05 GMTcc set
https://trac.sagemath.org/ticket/14326#comment:1
https://trac.sagemath.org/ticket/14326#comment:1
<ul>
<li><strong>cc</strong>
<em>mmezzarobba</em> added
</li>
</ul>
TicketzimmermaTue, 07 May 2013 09:04:00 GMT
https://trac.sagemath.org/ticket/14326#comment:2
https://trac.sagemath.org/ticket/14326#comment:2
<p>
Jeroen, do you have an idea who to include in cc to help isolate this?
</p>
<p>
Paul
</p>
TicketjdemeyerTue, 13 Aug 2013 15:35:53 GMTmilestone changed
https://trac.sagemath.org/ticket/14326#comment:3
https://trac.sagemath.org/ticket/14326#comment:3
<ul>
<li><strong>milestone</strong>
changed from <em>sage-5.11</em> to <em>sage-5.12</em>
</li>
</ul>
Ticketvbraun_spamThu, 30 Jan 2014 21:20:52 GMTmilestone changed
https://trac.sagemath.org/ticket/14326#comment:4
https://trac.sagemath.org/ticket/14326#comment:4
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.1</em> to <em>sage-6.2</em>
</li>
</ul>
Ticketvbraun_spamTue, 06 May 2014 15:20:58 GMTmilestone changed
https://trac.sagemath.org/ticket/14326#comment:5
https://trac.sagemath.org/ticket/14326#comment:5
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.2</em> to <em>sage-6.3</em>
</li>
</ul>
Ticketvbraun_spamSun, 10 Aug 2014 16:51:03 GMTmilestone changed
https://trac.sagemath.org/ticket/14326#comment:6
https://trac.sagemath.org/ticket/14326#comment:6
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.3</em> to <em>sage-6.4</em>
</li>
</ul>
TicketkcrismanFri, 14 Nov 2014 20:22:31 GMTcc, summary changed
https://trac.sagemath.org/ticket/14326#comment:7
https://trac.sagemath.org/ticket/14326#comment:7
<ul>
<li><strong>cc</strong>
<em>burcin</em> added
</li>
<li><strong>summary</strong>
changed from <em>function gives symbolic output for numeric input</em> to <em>Substituting numeric one in symbolic expression gives symbolic one</em>
</li>
</ul>
<p>
Wow, this is weird. Here is a <em>much</em> simpler example.
</p>
<pre class="wiki">sage: w(n) = n
sage: w(1.)
1.00000000000000
sage: w(n) = n^2
sage: w(1.)
1
</pre><p>
In fact, even
</p>
<pre class="wiki">sage: (x^2).subs(x=1.)
1
</pre><p>
works. Yuck.
</p>
<p>
Somehow the custom power method is not doing its job when you substitute . But I don't see an obvious place in Ginac where this would get screwed up...
</p>
<p>
Aha.
</p>
<pre class="wiki">sage: z = x^2
sage: z.subs(x=1.)
1
sage: z.subs(x=2.)
4.00000000000000
</pre><p>
Because one does get treated differently. Though
</p>
<pre class="wiki">sage: z = x
sage: z.subs(x=1.)
1.00000000000000
</pre><p>
so it also has something to do with the coercion that happens in the power method for symbolic expressions.
</p>
TicketkcrismanFri, 14 Nov 2014 20:40:25 GMT
https://trac.sagemath.org/ticket/14326#comment:8
https://trac.sagemath.org/ticket/14326#comment:8
<p>
Okay, I think this <em>might</em> be a bug in Ginac, or possibly in how we <em>use</em> Ginac. In the Ginac definition of <a class="ext-link" href="http://www.ginac.de/reference/power_8cpp_source.html#l00371"><span class="icon"></span>automatic rewriting of power::eval</a>, we have
</p>
<pre class="wiki">00399 // ^(x,1) -> x
00400 if (eexponent.is_equal(_ex1))
00401 return basis;
...
00413 // ^(1,x) -> 1
00414 if (ebasis.is_equal(_ex1))
00415 return _ex1;
</pre><p>
The other rewriting rules are probably harmless, though if
</p>
<pre class="wiki">sage: z
x^n
sage: z.subs(x=0.)
0.000000000000000^n
sage: z.subs(x=0)
0^n
sage: z.subs(n=0)
1
sage: z.subs(n=0.)
1
</pre><p>
where the <code>0^n</code> business is because Ginac checks if the exponent is numerical because it doesn't want to evaluate something that could, in principle, still become <code>0^0</code>.
</p>
<p>
Unfortunately, I'm not sure how to monkey-patch Pynac into recognizing this situation, and I certainly don't want to do a catch in the symbolic expression code, that is really the wrong place. Here's hoping someone really intimately familiar with our back-and-forth to Pynac sees an easy fix.
</p>
TicketjdemeyerSat, 15 Nov 2014 09:34:50 GMT
https://trac.sagemath.org/ticket/14326#comment:9
https://trac.sagemath.org/ticket/14326#comment:9
<p>
The problem looks similar to <a class="closed ticket" href="https://trac.sagemath.org/ticket/17130" title="defect: Fix coercion bugs in symbolic functions (closed: fixed)">#17130</a>, but that's about <code>__call__</code>.
</p>
TicketzimmermaMon, 17 Nov 2014 09:41:37 GMT
https://trac.sagemath.org/ticket/14326#comment:10
https://trac.sagemath.org/ticket/14326#comment:10
<p>
do the patches from <a class="closed ticket" href="https://trac.sagemath.org/ticket/17130" title="defect: Fix coercion bugs in symbolic functions (closed: fixed)">#17130</a> solve this ticket?
</p>
<p>
Paul
</p>
TicketzimmermaMon, 17 Nov 2014 10:16:29 GMT
https://trac.sagemath.org/ticket/14326#comment:11
https://trac.sagemath.org/ticket/14326#comment:11
<p>
Dear Karl-Dieter,
</p>
<blockquote class="citation">
<p>
I think this might be a bug in Ginac, or possibly in how we use Ginac.
</p>
</blockquote>
<p>
please could you ask the Ginac developers if this is a bug in Ginac?
And if not, how to replace <code>_ex1</code> to get the correct "one"?
</p>
<p>
Paul
</p>
TicketkcrismanMon, 17 Nov 2014 15:14:26 GMT
https://trac.sagemath.org/ticket/14326#comment:12
https://trac.sagemath.org/ticket/14326#comment:12
<blockquote class="citation">
<blockquote class="citation">
<p>
I think this might be a bug in Ginac, or possibly in how we use Ginac.
</p>
</blockquote>
<p>
please could you ask the Ginac developers if this is a bug in Ginac?
And if not, how to replace <code>_ex1</code> to get the correct "one"?
</p>
</blockquote>
<p>
I am really not familiar enough with Ginac proper to do either of these with any technical knowledge, unfortunately. And the Ginac developer(s) are not particularly responsive right now to any but the most informed pieces of information, apparently. If someone can figured out how to replicate this in Ginac that would be great, but again it could be us misusing it, I'm not sure.
</p>
<p>
I would be surprised if <a class="closed ticket" href="https://trac.sagemath.org/ticket/17130" title="defect: Fix coercion bugs in symbolic functions (closed: fixed)">#17130</a> fixed this, based on my experiments above.
</p>
TicketburcinFri, 21 Nov 2014 10:26:07 GMT
https://trac.sagemath.org/ticket/14326#comment:13
https://trac.sagemath.org/ticket/14326#comment:13
<p>
Looking at the code snippet from <a class="ticket" href="https://trac.sagemath.org/ticket/14326#comment:8" title="Comment 8">comment:8</a> only, I don't think this is a bug in Ginac. The intended behavior is just different in Pynac, so we have to patch Pynac.
</p>
<p>
Ginac wants to keep unique reference counted expression objects for expressions that are equal. That is why they return <code>_ex1</code> on line 415. In Pynac, we do not have a unique "one", we should just return <code>ebasis</code> in this line to keep the precision/type of the base.
</p>
TicketkcrismanFri, 21 Nov 2014 14:29:20 GMT
https://trac.sagemath.org/ticket/14326#comment:14
https://trac.sagemath.org/ticket/14326#comment:14
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/14326#comment:13" title="Comment 13">burcin</a>:
</p>
<p>
Thanks for replying!
</p>
<blockquote class="citation">
<p>
Looking at the code snippet from <a class="ticket" href="https://trac.sagemath.org/ticket/14326#comment:8" title="Comment 8">comment:8</a> only, I don't think this is a bug in Ginac. The intended behavior is just different in Pynac, so we have to patch Pynac.
</p>
<p>
Ginac wants to keep unique reference counted expression objects for expressions that are equal. That is why they return <code>_ex1</code> on line 415. In Pynac, we do not have a unique "one", we should just return <code>ebasis</code> in this line to keep the precision/type of the base.
</p>
</blockquote>
<p>
Of course! That makes perfect sense - once you say it, before it was murky :(
</p>
<p>
What about for the other case, of <code>x^1.0</code>? We have
</p>
<pre class="wiki">sage: (x^2).subs(x=1.)
1
sage: (2^x).subs(x=1.)
2
sage: (x^2).subs(x=2.)
4.00000000000000
sage: (2^x).subs(x=2.)
4.00000000000000
</pre><p>
so it would seem that if the exponent is not exact, we want the whole thing to be numerical. I guess we could just strip out that simplification completely, but I don't know if that would give us anything useful either.
</p>
TicketrwsTue, 05 May 2015 07:16:13 GMTstatus, milestone changed
https://trac.sagemath.org/ticket/14326#comment:15
https://trac.sagemath.org/ticket/14326#comment:15
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>needs_review</em>
</li>
<li><strong>milestone</strong>
changed from <em>sage-6.4</em> to <em>sage-duplicate/invalid/wontfix</em>
</li>
</ul>
<p>
This is fixed in pynac-0.3.6 (and a duplicate of <a class="closed ticket" href="https://trac.sagemath.org/ticket/12257" title="defect: doctest that only integer +/- 1 gets special treatment (closed: fixed)">#12257</a>), please review <a class="closed ticket" href="https://trac.sagemath.org/ticket/18362" title="defect: upgrade to pynac-0.3.7 (closed: fixed)">#18362</a> and <a class="closed ticket" href="https://trac.sagemath.org/ticket/12257" title="defect: doctest that only integer +/- 1 gets special treatment (closed: fixed)">#12257</a>.
</p>
TicketmmezzarobbaMon, 25 May 2015 10:26:35 GMTstatus changed
https://trac.sagemath.org/ticket/14326#comment:16
https://trac.sagemath.org/ticket/14326#comment:16
<ul>
<li><strong>status</strong>
changed from <em>needs_review</em> to <em>positive_review</em>
</li>
</ul>
TicketvbraunFri, 19 Jun 2015 08:39:24 GMTstatus changed; resolution set
https://trac.sagemath.org/ticket/14326#comment:17
https://trac.sagemath.org/ticket/14326#comment:17
<ul>
<li><strong>status</strong>
changed from <em>positive_review</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>duplicate</em>
</li>
</ul>
Ticket