Sage: Ticket #12074: real nth root function
https://trac.sagemath.org/ticket/12074
<p>
See sage-devel threads at:
</p>
<p>
<a class="ext-link" href="http://groups.google.com/group/sage-devel/t/cea9b562ea49c9c1"><span class="icon"></span>http://groups.google.com/group/sage-devel/t/cea9b562ea49c9c1</a>
</p>
<p>
and
</p>
<p>
<a class="ext-link" href="https://groups.google.com/forum/#!topic/sage-devel/Q8VLKBypcJk"><span class="icon"></span>https://groups.google.com/forum/#!topic/sage-devel/Q8VLKBypcJk</a>
</p>
en-usSagehttps://trac.sagemath.org/chrome/site/logo_sagemath_trac.png
https://trac.sagemath.org/ticket/12074
Trac 1.1.6burcinWed, 23 Nov 2011 14:44:50 GMTcc, work_issues set
https://trac.sagemath.org/ticket/12074#comment:1
https://trac.sagemath.org/ticket/12074#comment:1
<ul>
<li><strong>cc</strong>
<em>kcrisman</em> added
</li>
<li><strong>work_issues</strong>
set to <em>needs tests and documentation</em>
</li>
</ul>
TicketjdemeyerWed, 23 Nov 2011 19:32:23 GMT
https://trac.sagemath.org/ticket/12074#comment:2
https://trac.sagemath.org/ticket/12074#comment:2
<p>
Is there any chance this could be made into a function that we can do calculus with, like computing derivatives, integrals, solving equations... (I'm afraid the answer will be no though because we need maxima).
</p>
TicketburcinThu, 24 Nov 2011 11:52:46 GMTattachment set
https://trac.sagemath.org/ticket/12074
https://trac.sagemath.org/ticket/12074
<ul>
<li><strong>attachment</strong>
set to <em>trac_12074-nth_root.patch</em>
</li>
</ul>
TicketburcinThu, 24 Nov 2011 12:06:14 GMT
https://trac.sagemath.org/ticket/12074#comment:3
https://trac.sagemath.org/ticket/12074#comment:3
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/12074#comment:2" title="Comment 2">jdemeyer</a>:
</p>
<blockquote class="citation">
<p>
Is there any chance this could be made into a function that we can do calculus with, like computing derivatives, integrals, solving equations... (I'm afraid the answer will be no though because we need maxima).
</p>
</blockquote>
<p>
This is already a symbolic function, so it plays well with symbolics generally (as opposed to piecewise functions for instance):
</p>
<pre class="wiki">sage: v = nth_root(x,3)
sage: v*sin(x) + x^2
x^2 + real_nth_root(x, 3)*sin(x)
</pre><p>
I updated the patch to add custom exponentiation and derivative methods as well:
</p>
<pre class="wiki">sage: v^2
real_nth_root(x, 3/2)
sage: v*v
real_nth_root(x, 3/2)
sage: v.diff(x)
1/3*real_nth_root(x, -3/2)
</pre><p>
This all needs a lot of work of course.
</p>
<p>
For integration and solving equations we call out to maxima. One way to get sensible results from these calls would be to convert this function to a regular <code>(base)^(exp)</code> representation when passing it to maxima. I don't think there is any way to read it back from the maxima result though.
</p>
TicketjdemeyerThu, 24 Nov 2011 19:04:23 GMTcc changed
https://trac.sagemath.org/ticket/12074#comment:4
https://trac.sagemath.org/ticket/12074#comment:4
<ul>
<li><strong>cc</strong>
<em>karsten.naert@…</em> added
</li>
</ul>
TicketeviatarbachSat, 22 Jun 2013 18:27:33 GMTcc changed
https://trac.sagemath.org/ticket/12074#comment:5
https://trac.sagemath.org/ticket/12074#comment:5
<ul>
<li><strong>cc</strong>
<em>eviatarbach</em> added
</li>
</ul>
TicketjdemeyerTue, 13 Aug 2013 15:35:53 GMTmilestone changed
https://trac.sagemath.org/ticket/12074#comment:6
https://trac.sagemath.org/ticket/12074#comment:6
<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/12074#comment:7
https://trac.sagemath.org/ticket/12074#comment:7
<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/12074#comment:8
https://trac.sagemath.org/ticket/12074#comment:8
<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/12074#comment:9
https://trac.sagemath.org/ticket/12074#comment:9
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.3</em> to <em>sage-6.4</em>
</li>
</ul>
TicketslelievreTue, 11 Jun 2019 03:21:03 GMTcc, milestone changed
https://trac.sagemath.org/ticket/12074#comment:10
https://trac.sagemath.org/ticket/12074#comment:10
<ul>
<li><strong>cc</strong>
<em>slelievre</em> added
</li>
<li><strong>milestone</strong>
changed from <em>sage-6.4</em> to <em>sage-wishlist</em>
</li>
</ul>
<p>
See also sympy's <code>real_root</code>
</p>
<ul><li><a class="ext-link" href="https://docs.sympy.org/latest/modules/functions/elementary.html#real-root"><span class="icon"></span>https://docs.sympy.org/latest/modules/functions/elementary.html#real-root</a>
</li></ul>
TicketkcrismanThu, 11 Jun 2020 17:54:42 GMT
https://trac.sagemath.org/ticket/12074#comment:11
https://trac.sagemath.org/ticket/12074#comment:11
<p>
There is some interest in this again, due to an upcoming book's 2nd edition.
</p>
<p>
Sympy's solution may be more elegant in some ways, because it uses <code>Abs(x)**(1/3)*sign(x)</code> for <code>real_root(x,3)</code>, apparently. If that solution (in Sage, of course, with translations to Sympy) has better support overall in Sage we could just do that instead (for odd roots).
</p>
<p>
Here's the full Sympy code. They have better handling of complicated piecewise things, I guess.
</p>
<pre class="wiki"> from sympy.functions.elementary.complexes import Abs, im, sign
from sympy.functions.elementary.piecewise import Piecewise
if n is not None:
return Piecewise(
(root(arg, n, evaluate=evaluate), Or(Eq(n, S.One), Eq(n, S.NegativeOne))),
(Mul(sign(arg), root(Abs(arg), n, evaluate=evaluate), evaluate=evaluate),
And(Eq(im(arg), S.Zero), Eq(Mod(n, 2), S.One))),
(root(arg, n, evaluate=evaluate), True))
rv = sympify(arg)
n1pow = Transform(lambda x: -(-x.base)**x.exp,
lambda x:
x.is_Pow and
x.base.is_negative and
x.exp.is_Rational and
x.exp.p == 1 and x.exp.q % 2)
return rv.xreplace(n1pow)
</pre>
TicketkleeFri, 12 Jun 2020 13:35:17 GMTauthor changed; branch set
https://trac.sagemath.org/ticket/12074#comment:12
https://trac.sagemath.org/ticket/12074#comment:12
<ul>
<li><strong>branch</strong>
set to <em>public/12074</em>
</li>
<li><strong>author</strong>
changed from <em>Burcin Erocal</em> to <em>Burcin Erocal; Kwankyu Lee</em>
</li>
</ul>
TicketgitFri, 12 Jun 2020 13:35:58 GMTcommit set
https://trac.sagemath.org/ticket/12074#comment:13
https://trac.sagemath.org/ticket/12074#comment:13
<ul>
<li><strong>commit</strong>
set to <em>5028e22e1d4bbd112a14983efd13f7aa43adf168</em>
</li>
</ul>
<p>
Branch pushed to git repo; I updated commit sha1. New commits:
</p>
<table class="wiki">
<tr><td><a class="ext-link" href="https://git.sagemath.org/sage.git/commit/?id=5028e22e1d4bbd112a14983efd13f7aa43adf168"><span class="icon"></span>5028e22</a></td><td><code>Add real_nth_root symbolic function</code>
</td></tr></table>
TicketkleeFri, 12 Jun 2020 13:37:48 GMT
https://trac.sagemath.org/ticket/12074#comment:14
https://trac.sagemath.org/ticket/12074#comment:14
<p>
Revived Burcin's implementation of <code>real_nth_root</code> symbolic function.
</p>
<p>
It is in public. Feel free to improve.
</p>
TicketkleeFri, 12 Jun 2020 13:38:22 GMTmilestone changed; keywords, work_issues deleted
https://trac.sagemath.org/ticket/12074#comment:15
https://trac.sagemath.org/ticket/12074#comment:15
<ul>
<li><strong>keywords</strong>
<em>nth_root</em> removed
</li>
<li><strong>work_issues</strong>
<em>needs tests and documentation</em> deleted
</li>
<li><strong>milestone</strong>
changed from <em>sage-wishlist</em> to <em>sage-9.2</em>
</li>
</ul>
TicketkleeFri, 12 Jun 2020 13:38:40 GMTauthor changed
https://trac.sagemath.org/ticket/12074#comment:16
https://trac.sagemath.org/ticket/12074#comment:16
<ul>
<li><strong>author</strong>
changed from <em>Burcin Erocal; Kwankyu Lee</em> to <em>Burcin Erocal, Kwankyu Lee</em>
</li>
</ul>
TicketkleeFri, 12 Jun 2020 13:38:58 GMTstatus changed
https://trac.sagemath.org/ticket/12074#comment:17
https://trac.sagemath.org/ticket/12074#comment:17
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>needs_review</em>
</li>
</ul>
TicketkleeFri, 12 Jun 2020 13:40:11 GMTdescription changed
https://trac.sagemath.org/ticket/12074#comment:18
https://trac.sagemath.org/ticket/12074#comment:18
<ul>
<li><strong>description</strong>
modified (<a href="/ticket/12074?action=diff&version=18">diff</a>)
</li>
</ul>
TicketkcrismanFri, 12 Jun 2020 14:00:32 GMT
https://trac.sagemath.org/ticket/12074#comment:19
https://trac.sagemath.org/ticket/12074#comment:19
<p>
Kwankyu (관규?), thanks so much for getting that ball rolling, and the doctests/doc looks good so far.
</p>
<p>
Here are some questions/comments (for anyone, not necessarily for klee):
</p>
<ul><li>See <a class="ext-link" href="https://github.com/sagemath/sage/blob/develop/src/sage/plot/plot.py#L1502"><span class="icon"></span>this list of plot methods for this</a> in the plot doc. We should add this, perhaps as the first-order approximation.
</li><li>The file <a class="ext-link" href="https://github.com/sagemath/sage/blob/develop/src/doc/en/faq/faq-usage.rst"><span class="icon"></span>faq-usage.rst</a> also should be amended since it mentions the same issue.
</li><li>Maybe even there should be a plot example in this file, and then a link to this function from the plot doc?
</li><li>Does this one work with derivatives/integrals? Maybe in the meantime we don't need the custom method.
</li><li>Apparently Mathematica now has something like this, called <a class="ext-link" href="https://reference.wolfram.com/language/ref/Surd.html"><span class="icon"></span>Surd</a> which, while perhaps technically a correct name, is not exactly inspiring to anyone who has taken math since 1950 ... Perhaps this is because Maple <a class="ext-link" href="https://www.maplesoft.com/support/help/Maple/view.aspx?path=surd"><span class="icon"></span>also has the same name</a> though not capitalized. I like the Sage/Sympy name <em>much</em> better, but anyway these can be added to the conversions. (I don't think Maxima has an equivalent for now.)
</li></ul>
TicketmjoFri, 12 Jun 2020 15:56:18 GMTcc changed
https://trac.sagemath.org/ticket/12074#comment:20
https://trac.sagemath.org/ticket/12074#comment:20
<ul>
<li><strong>cc</strong>
<em>mjo</em> added
</li>
</ul>
<p>
The current behavior of integer/float exponentiation is due to python, I guess?
</p>
<pre class="wiki">$ python
Python 3.7.7 (default, May 8 2020, 10:21:17)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> (-1)**(1/3)
(0.5000000000000001+0.8660254037844386j)
</pre><p>
Otherwise the question can be asked: why not make that return real numbers when the argument is real? Which leads me to this question: would it make sense to call this function <code>nth_root</code>, and to have it return the real/complex answer depending on whether or not the argument is real/complex?
</p>
<p>
(I also like having the codomain of a function be explicit, so I'm not really advocating for this, just wondering out loud.)
</p>
TicketnbruinFri, 12 Jun 2020 16:08:48 GMT
https://trac.sagemath.org/ticket/12074#comment:21
https://trac.sagemath.org/ticket/12074#comment:21
<p>
Some comments:
</p>
<ul><li>I think evalf needs some care to work properly on real ball fields and real interval fields.
</li><li>I think we need a derivative implementation. I'd say just the formal rule <code>d/dx real_nth_root(x,n) = 1/n*sgn(x)*real_nth_root(x,n)^(1/n-1)</code>. Naturally, we don't have to support differentiation with respect to the second variable.
</li><li>I think <code>real_nth_root(x,n)^m</code> should not simplify to <code>real_nth_root(x^m,n)</code>. For instance, for <code>real_nth_root(-1,2)^2</code> this goes wrong.
</li></ul>
TicketkcrismanFri, 12 Jun 2020 16:28:35 GMT
https://trac.sagemath.org/ticket/12074#comment:22
https://trac.sagemath.org/ticket/12074#comment:22
<p>
More concretely, then, for this ticket:
</p>
<ul><li>Current branch + things that are probably necessary just to use:
<ul><li>Derivative and evaluation (can follow Sympy or Nils with the former, perhaps). Can we just use the syntax in Burcin's patch for the derivative, or do we no longer have that underscore method?
</li><li>I can't remember if we support a custom integration but could include something in the documentation saying to use Sympy if you have to integrate?
</li><li>Possibly fix the simplification - is the one in the branch the same as Burcin's, or Nils'? (Indeed, should we have this function work for <code>n</code> even and negative <code>x</code>?)
</li><li>Might as well add the extra conversions.
</li></ul></li></ul><p>
After all, the point of this function (note how Sympy and the Ms handle this) is for something we know doesn't really behave well with respect to branch cuts etc., intentionally. So no one should necessarily be using it for production symbolics, but rather really mostly just for classroom usage in situations (which are <em>legion</em>) where we have to explicitly exclude complex stuff. Luckily for the logarithm this is a little easier to hide from the student, except for in an antiderivative of 1/x, but here it's not as "nice".
</p>
<p>
Then a separate ticket could do:
</p>
<ul><li>Improve things like simplification/codomains from the most necessary.
</li></ul>
TicketgitWed, 17 Jun 2020 13:01:33 GMTcommit changed
https://trac.sagemath.org/ticket/12074#comment:23
https://trac.sagemath.org/ticket/12074#comment:23
<ul>
<li><strong>commit</strong>
changed from <em>5028e22e1d4bbd112a14983efd13f7aa43adf168</em> to <em>e188fa24b1857152b234551950eab526870811ca</em>
</li>
</ul>
<p>
Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:
</p>
<table class="wiki">
<tr><td><a class="ext-link" href="https://git.sagemath.org/sage.git/commit/?id=e188fa24b1857152b234551950eab526870811ca"><span class="icon"></span>e188fa2</a></td><td><code>Add real_nth_root symbolic function</code>
</td></tr></table>
TicketkleeWed, 17 Jun 2020 13:02:30 GMT
https://trac.sagemath.org/ticket/12074#comment:24
https://trac.sagemath.org/ticket/12074#comment:24
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/12074#comment:19" title="Comment 19">kcrisman</a>:
</p>
<blockquote class="citation">
<p>
Kwankyu (관규?)
</p>
</blockquote>
<p>
Correct!
</p>
<blockquote class="citation">
<p>
Here are some questions/comments (for anyone, not necessarily for klee):
</p>
<ul><li>See <a class="ext-link" href="https://github.com/sagemath/sage/blob/develop/src/sage/plot/plot.py#L1502"><span class="icon"></span>this list of plot methods for this</a> in the plot doc. We should add this, perhaps as the first-order approximation.
</li></ul></blockquote>
<p>
Done.
</p>
<blockquote class="citation">
<ul><li>The file <a class="ext-link" href="https://github.com/sagemath/sage/blob/develop/src/doc/en/faq/faq-usage.rst"><span class="icon"></span>faq-usage.rst</a> also should be amended since it mentions the same issue.
</li></ul></blockquote>
<p>
Done.
</p>
<blockquote class="citation">
<ul><li>Maybe even there should be a plot example in this file, and then a link to this function from the plot doc?
</li></ul></blockquote>
<p>
Made a link from the plot doc.
</p>
<blockquote class="citation">
<ul><li>Does this one work with derivatives/integrals? Maybe in the meantime we don't need the custom method.
</li></ul></blockquote>
<p>
Almost.
</p>
<pre class="wiki">sage: f = real_nth_root(x,3)
sage: f.diff()
1/3*real_nth_root(x^(-2), 3)
sage: f.integrate(x)
integrate(abs(x)^(1/3)*sgn(x), x)
sage: _.diff()
abs(x)^(1/3)*sgn(x)
</pre><blockquote class="citation">
<ul><li>Apparently Mathematica now has something like this, called <a class="ext-link" href="https://reference.wolfram.com/language/ref/Surd.html"><span class="icon"></span>Surd</a> which, while perhaps technically a correct name, is not exactly inspiring to anyone who has taken math since 1950 ... Perhaps this is because Maple <a class="ext-link" href="https://www.maplesoft.com/support/help/Maple/view.aspx?path=surd"><span class="icon"></span>also has the same name</a> though not capitalized. I like the Sage/Sympy name <em>much</em> better, but anyway these can be added to the conversions. (I don't think Maxima has an equivalent for now.)
</li></ul></blockquote>
<p>
Done. But I could not test them as I have no Mathematica nor Maple.
</p>
TicketkleeWed, 17 Jun 2020 13:10:36 GMT
https://trac.sagemath.org/ticket/12074#comment:25
https://trac.sagemath.org/ticket/12074#comment:25
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/12074#comment:20" title="Comment 20">mjo</a>:
</p>
<blockquote class="citation">
<p>
The current behavior of integer/float exponentiation is due to python, I guess?
</p>
<pre class="wiki">$ python
Python 3.7.7 (default, May 8 2020, 10:21:17)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> (-1)**(1/3)
(0.5000000000000001+0.8660254037844386j)
</pre></blockquote>
<p>
This convention is shared by other symbolic algebra systems.
</p>
TicketkleeWed, 17 Jun 2020 13:22:21 GMT
https://trac.sagemath.org/ticket/12074#comment:26
https://trac.sagemath.org/ticket/12074#comment:26
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/12074#comment:21" title="Comment 21">nbruin</a>:
</p>
<blockquote class="citation">
<p>
Some comments:
</p>
<ul><li>I think evalf needs some care to work properly on real ball fields and real interval fields.
</li></ul></blockquote>
<p>
Now it works for real ball fields:
</p>
<pre class="wiki">sage: a = RBF(-2)
sage: real_nth_root(a, 3)
[-1.259921049894873 +/- 3.92e-16]
</pre><p>
and for real interval fields:
</p>
<pre class="wiki">sage: a = RIF(-2)
sage: real_nth_root(a, 3)
-1.259921049894873?
</pre><blockquote class="citation">
<ul><li>I think we need a derivative implementation. I'd say just the formal rule <code>d/dx real_nth_root(x,n) = 1/n*sgn(x)*real_nth_root(x,n)^(1/n-1)</code>. Naturally, we don't have to support differentiation with respect to the second variable.
</li></ul></blockquote>
<p>
I guess you meant <code>d/dx real_nth_root(x,n) = 1/n*real_nth_root(x,n)^(1-n)</code>. Done.
</p>
<blockquote class="citation">
<ul><li>I think <code>real_nth_root(x,n)^m</code> should not simplify to <code>real_nth_root(x^m,n)</code>. For instance, for <code>real_nth_root(-1,2)^2</code> this goes wrong.
</li></ul></blockquote>
<p>
<code>real_nth_root(-1,2)</code> now raises <code>ValueError</code>. The simplification is valid for other cases.
</p>
TicketkleeWed, 17 Jun 2020 13:41:37 GMT
https://trac.sagemath.org/ticket/12074#comment:27
https://trac.sagemath.org/ticket/12074#comment:27
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/12074#comment:22" title="Comment 22">kcrisman</a>:
</p>
<blockquote class="citation">
<p>
After all, the point of this function (note how Sympy and the Ms handle this) is for something we know doesn't really behave well with respect to branch cuts etc., intentionally. So no one should necessarily be using it for production symbolics, but rather really mostly just for classroom usage in situations (which are <em>legion</em>) where we have to explicitly exclude complex stuff. Luckily for the logarithm this is a little easier to hide from the student, except for in an antiderivative of 1/x, but here it's not as "nice".
</p>
</blockquote>
<p>
School math just works:
</p>
<pre class="wiki">sage: f = real_nth_root(x^2, 2)
sage: g = f.diff()
sage: f.plot()
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage: g.plot()
Launched png viewer for Graphics object consisting of 1 graphics primitive
</pre>
TicketgitWed, 17 Jun 2020 14:06:37 GMTcommit changed
https://trac.sagemath.org/ticket/12074#comment:28
https://trac.sagemath.org/ticket/12074#comment:28
<ul>
<li><strong>commit</strong>
changed from <em>e188fa24b1857152b234551950eab526870811ca</em> to <em>7cef50b2542c2b90e8a60b88beb6fc6d09d0d380</em>
</li>
</ul>
<p>
Branch pushed to git repo; I updated commit sha1. New commits:
</p>
<table class="wiki">
<tr><td><a class="ext-link" href="https://git.sagemath.org/sage.git/commit/?id=7cef50b2542c2b90e8a60b88beb6fc6d09d0d380"><span class="icon"></span>7cef50b</a></td><td><code>Fix a typo in FAQ</code>
</td></tr></table>
TicketkcrismanWed, 17 Jun 2020 14:14:36 GMT
https://trac.sagemath.org/ticket/12074#comment:29
https://trac.sagemath.org/ticket/12074#comment:29
<p>
Okay, this is ready enough that I will (try to) upgrade my Sage develop branch just for this ticket. I still have a few thoughts - notably about seeing whether we can get Sympy to give us an integral for this, and about the derivative. Maybe we can get someone to even test out the M* conversion... Thanks for this work. Only took us how many years?
</p>
TicketgitThu, 18 Jun 2020 00:11:55 GMTcommit changed
https://trac.sagemath.org/ticket/12074#comment:30
https://trac.sagemath.org/ticket/12074#comment:30
<ul>
<li><strong>commit</strong>
changed from <em>7cef50b2542c2b90e8a60b88beb6fc6d09d0d380</em> to <em>d12ab31283e8bf8905fb1534d653df97619ba575</em>
</li>
</ul>
<p>
Branch pushed to git repo; I updated commit sha1. New commits:
</p>
<table class="wiki">
<tr><td><a class="ext-link" href="https://git.sagemath.org/sage.git/commit/?id=d12ab31283e8bf8905fb1534d653df97619ba575"><span class="icon"></span>d12ab31</a></td><td><code>Fix for a pyflakes warning</code>
</td></tr></table>
TicketkleeThu, 18 Jun 2020 05:49:16 GMT
https://trac.sagemath.org/ticket/12074#comment:31
https://trac.sagemath.org/ticket/12074#comment:31
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/12074#comment:20" title="Comment 20">mjo</a>:
</p>
<blockquote class="citation">
<p>
Otherwise the question can be asked: why not make that return real numbers when the argument is real? Which leads me to this question: would it make sense to call this function <code>nth_root</code>, and to have it return the real/complex answer depending on whether or not the argument is real/complex?
</p>
</blockquote>
<p>
It is possible to get this behavior
</p>
<pre class="wiki">sage: f = x^(1/3)
sage: f(-8)
2*(-1)^(1/3)
sage: f(-2.)
0.629960524947437 + 1.09112363597172*I
sage: assume(x, 'real')
sage: g = x^(1/3)
sage: g(-2.)
-1.25992104989487
sage: g
real_nth_root(x, 3)
sage: g.diff()
1/3*real_nth_root(x^(-2), 3)
sage: g.plot()
Launched png viewer for Graphics object consisting of 1 graphics primitive
</pre><p>
by injecting some code into <code>expression.pyx</code>. The question is whether we want this.
</p>
TicketkcrismanThu, 18 Jun 2020 11:36:35 GMT
https://trac.sagemath.org/ticket/12074#comment:32
https://trac.sagemath.org/ticket/12074#comment:32
<blockquote class="citation">
<p>
by injecting some code into <code>expression.pyx</code>. The question is whether we want this.
</p>
</blockquote>
<p>
I'm gonna say no on that - not without some healthy sage-devel discussion. I do think some people use the assume command for precisely this purpose, though.
</p>
<p>
I'm going to look at this branch later today, thank you again for putting it in "modern" shape!
</p>
TicketkcrismanThu, 18 Jun 2020 17:16:57 GMT
https://trac.sagemath.org/ticket/12074#comment:33
https://trac.sagemath.org/ticket/12074#comment:33
<p>
Here come a bunch of questions now that I have played around with this. They don't necessarily all need changes, but I think they are reasonable to ask even if they end up not needing change. But I'm not letting the perfect be the enemy of the good; as long as we document and open new tickets, that would be the important part.
</p>
<hr />
<ul><li>Do we want a latex method? E.g. <code>latex_name=r"\mathrm{abs}"</code> used elsewhere in this file. This is obviously not ideal:
<pre class="wiki">sage: latex(F)
{\rm real}_{{\rm nth}_{{\rm root}}}\left(x, 5\right)
</pre>But I'm not sure whether just using the usual root syntax is "correct" either since we have a different function now. Using <code>abs(x)^(1/3)*sgn(x)</code> doesn't work for even <code>n</code>, else I'd recommend that for latex.
</li><li>Integration, perhaps surprisingly, works, as you mention <a class="ticket" href="https://trac.sagemath.org/ticket/12074#comment:24" title="Comment 24">above</a>:
<pre class="wiki">sage: f = real_nth_root(x,3)
sage: f.diff()
1/3*real_nth_root(x^(-2), 3)
sage: f.integrate(x)
integrate(abs(x)^(1/3)*sgn(x), x)
sage: _.diff()
abs(x)^(1/3)*sgn(x)
</pre>Apparently integration now tries Maxima, Giac, and then Sympy, so that explains it. We should probably document this behavior.
</li><li>Upon further consideration, maybe the derivative can be a little better. Because aren't we sort of assuming this is real input? Maybe we aren't. Here is Sympy.
<pre class="wiki">>>> from sympy import root, real_root, Rational
>>> from sympy.abc import x, n
>>> real_root(x,5)
Piecewise((Abs(x)**(1/5)*sign(x), Eq(im(x), 0)), (x**(1/5), True))
>>> diff(real_root(x,5),x)
Piecewise((Abs(x)**(1/5)*Derivative(sign(x), x) + (re(x)*Derivative(re(x), x) + im(x)*Derivative(im(x), x))*sign(x)**2/(5*x*Abs(x)**(4/5)), Eq(im(x), 0)), (1/(5*x**(4/5)), True))
>>> x = Symbol('x', real=True)
>>> diff(real_root(x,5),x)
2*Abs(x)**(1/5)*DiracDelta(x) + sign(x)**2/(5*Abs(x)**(4/5))
</pre>and here is Sage
<pre class="wiki">sage: G = abs(x)^(1/5)*sgn(x)
sage: G
abs(x)^(1/5)*sgn(x)
sage: G.diff(x)
2*abs(x)^(1/5)*dirac_delta(x) + 1/10*(x + conjugate(x))*sgn(x)/abs(x)^(9/5)
sage: G.diff(x).simplify()
2*abs(x)^(1/5)*dirac_delta(x) + 1/5*x*sgn(x)/abs(x)^(9/5)
</pre>at least
<pre class="wiki">sage: assume(x,'real')
sage: F.diff(x)
1/5*real_nth_root(x^(-4), 5)
</pre>should mimic this, if not perhaps always. Any thoughts on this?
</li><li>To elaborate, Sympy's function somehow allows complex input, but do we want to?
<pre class="wiki">sage: real_nth_root(3.*I,5)
1.18476052767182 + 0.384952030759866*I # should this happen?
</pre>That's of course related to Nils question regarding negative input too.
<pre class="wiki">sage: H = real_nth_root(x,2)
sage: (H.subs(x=-1))^2
---------------------------------------------------------------------------
...
ValueError: no real nth root of negative real number with even n
sage: ((H)^2).subs(x=-1)
1
</pre>I'm not sure the latter should be allowed or not.
</li></ul>
TicketkcrismanThu, 18 Jun 2020 18:29:29 GMT
https://trac.sagemath.org/ticket/12074#comment:34
https://trac.sagemath.org/ticket/12074#comment:34
<pre class="wiki">sage -t src/doc/en/faq/faq-usage.rst
**********************************************************************
File "src/doc/en/faq/faq-usage.rst", line 692, in doc.en.faq.faq-usage
Failed example:
plot(x^(1/3), (x, -1, 1))
Expected nothing
Got:
verbose 0 (3834: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 100 points.
verbose 0 (3834: plot.py, generate_plot_points) Last error message: 'can't convert complex to float'
Graphics object consisting of 1 graphics primitive
**********************************************************************
</pre><p>
So you'll have to make that not tested (I think there is a way to mark it as such other than <code># not tested</code> but anyway...).
</p>
TicketkcrismanThu, 18 Jun 2020 18:29:37 GMTstatus changed
https://trac.sagemath.org/ticket/12074#comment:35
https://trac.sagemath.org/ticket/12074#comment:35
<ul>
<li><strong>status</strong>
changed from <em>needs_review</em> to <em>needs_work</em>
</li>
</ul>
TicketkleeFri, 19 Jun 2020 01:53:52 GMT
https://trac.sagemath.org/ticket/12074#comment:36
https://trac.sagemath.org/ticket/12074#comment:36
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/12074#comment:32" title="Comment 32">kcrisman</a>:
</p>
<blockquote class="citation">
<blockquote class="citation">
<p>
by injecting some code into <code>expression.pyx</code>. The question is whether we want this.
</p>
</blockquote>
<p>
I'm gonna say no on that - not without some healthy sage-devel discussion. I do think some people use the assume command for precisely this purpose, though.
</p>
</blockquote>
<p>
Injecting the code incur many doctest failures in other places. So I give up this idea.
</p>
TicketkleeFri, 19 Jun 2020 02:23:59 GMT
https://trac.sagemath.org/ticket/12074#comment:37
https://trac.sagemath.org/ticket/12074#comment:37
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/12074#comment:33" title="Comment 33">kcrisman</a>:
</p>
<blockquote class="citation">
<ul><li>Do we want a latex method? E.g. <code>latex_name=r"\mathrm{abs}"</code> used elsewhere in this file. This is obviously not ideal:
<pre class="wiki">sage: latex(F)
{\rm real}_{{\rm nth}_{{\rm root}}}\left(x, 5\right)
</pre>But I'm not sure whether just using the usual root syntax is "correct" either since we have a different function now. Using <code>abs(x)^(1/3)*sgn(x)</code> doesn't work for even <code>n</code>, else I'd recommend that for latex.
</li></ul></blockquote>
<p>
I see <code>real_nth_root</code> as representing the real function x<sup>1/n</sup> with variable x taking real numbers (possibly negative for odd n). The latex print is for humans. So why not just print x<sup>1/n</sup>?
</p>
<blockquote class="citation">
<ul><li>Integration, perhaps surprisingly, works, as you mention <a class="ticket" href="https://trac.sagemath.org/ticket/12074#comment:24" title="Comment 24">above</a>:
<pre class="wiki">sage: f = real_nth_root(x,3)
sage: f.diff()
1/3*real_nth_root(x^(-2), 3)
sage: f.integrate(x)
integrate(abs(x)^(1/3)*sgn(x), x)
sage: _.diff()
abs(x)^(1/3)*sgn(x)
</pre>Apparently integration now tries Maxima, Giac, and then Sympy, so that explains it. We should probably document this behavior.
</li></ul></blockquote>
<p>
Done.
</p>
<blockquote class="citation">
<ul><li>Upon further consideration, maybe the derivative can be a little better. Because aren't we sort of assuming this is real input? Maybe we aren't. Here is Sympy.
<pre class="wiki">>>> from sympy import root, real_root, Rational
>>> from sympy.abc import x, n
>>> real_root(x,5)
Piecewise((Abs(x)**(1/5)*sign(x), Eq(im(x), 0)), (x**(1/5), True))
>>> diff(real_root(x,5),x)
Piecewise((Abs(x)**(1/5)*Derivative(sign(x), x) + (re(x)*Derivative(re(x), x) + im(x)*Derivative(im(x), x))*sign(x)**2/(5*x*Abs(x)**(4/5)), Eq(im(x), 0)), (1/(5*x**(4/5)), True))
>>> x = Symbol('x', real=True)
>>> diff(real_root(x,5),x)
2*Abs(x)**(1/5)*DiracDelta(x) + sign(x)**2/(5*Abs(x)**(4/5))
</pre>and here is Sage
<pre class="wiki">sage: G = abs(x)^(1/5)*sgn(x)
sage: G
abs(x)^(1/5)*sgn(x)
sage: G.diff(x)
2*abs(x)^(1/5)*dirac_delta(x) + 1/10*(x + conjugate(x))*sgn(x)/abs(x)^(9/5)
sage: G.diff(x).simplify()
2*abs(x)^(1/5)*dirac_delta(x) + 1/5*x*sgn(x)/abs(x)^(9/5)
</pre>at least
<pre class="wiki">sage: assume(x,'real')
sage: F.diff(x)
1/5*real_nth_root(x^(-4), 5)
</pre>should mimic this, if not perhaps always. Any thoughts on this?
</li></ul></blockquote>
<p>
I don't get your concern. I think there is nothing wrong with
</p>
<pre class="wiki">sage: f = real_nth_root(x,3)
sage: f.diff()
1/3*real_nth_root(x^(-2), 3)
</pre><blockquote class="citation">
<ul><li>To elaborate, Sympy's function somehow allows complex input, but do we want to?
<pre class="wiki">sage: real_nth_root(3.*I,5)
1.18476052767182 + 0.384952030759866*I # should this happen?
</pre>That's of course related to Nils question regarding negative input too.
<pre class="wiki">sage: H = real_nth_root(x,2)
sage: (H.subs(x=-1))^2
---------------------------------------------------------------------------
...
ValueError: no real nth root of negative real number with even n
sage: ((H)^2).subs(x=-1)
1
</pre>I'm not sure the latter should be allowed or not.
</li></ul></blockquote>
<p>
No. <code>real_nth_root</code> takes real input and outputs real. For the symbolic function <code>1/x</code>, we assume <code>x</code> takes nonzero value. Likewise, we assume real value x for <code>real_nth_root(x, 3)</code>.
</p>
TicketgitFri, 19 Jun 2020 02:27:43 GMTcommit changed
https://trac.sagemath.org/ticket/12074#comment:38
https://trac.sagemath.org/ticket/12074#comment:38
<ul>
<li><strong>commit</strong>
changed from <em>d12ab31283e8bf8905fb1534d653df97619ba575</em> to <em>00380b09135e8171593108059f17490a23f554ad</em>
</li>
</ul>
<p>
Branch pushed to git repo; I updated commit sha1. New commits:
</p>
<table class="wiki">
<tr><td><a class="ext-link" href="https://git.sagemath.org/sage.git/commit/?id=00380b09135e8171593108059f17490a23f554ad"><span class="icon"></span>00380b0</a></td><td><code>Fixes for reviewer comments</code>
</td></tr></table>
TicketmjoFri, 19 Jun 2020 02:41:14 GMT
https://trac.sagemath.org/ticket/12074#comment:39
https://trac.sagemath.org/ticket/12074#comment:39
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/12074#comment:36" title="Comment 36">klee</a>:
</p>
<blockquote class="citation">
<p>
Injecting the code incur many doctest failures in other places. So I give up this idea.
</p>
</blockquote>
<p>
I was thinking of something simpler, like
</p>
<pre class="wiki">sage: nth_root(RR(-1), 3)
-1.00000000000000
sage: nth_root(CC(-1), 3)
0.500000000000000 + 0.866025403784439*I
</pre>
TicketkcrismanFri, 19 Jun 2020 14:16:06 GMT
https://trac.sagemath.org/ticket/12074#comment:40
https://trac.sagemath.org/ticket/12074#comment:40
<p>
Thanks for all these fixes - we are converging :-) Couple more questions/comments:
</p>
<hr />
<ul><li>I don't think you needed to make this one not tested; it should have passed fine. Only the subsequent one would have thrown an error.
<div class="wiki-code"><div class="code"><pre><span class="gd">- sage: plot(real_nth_root(x, 3), (x, -1, 1))
- Graphics object consisting of 1 graphics primitive
</span><span class="gi">+ sage: plot(real_nth_root(x, 3), (x, -1, 1)) # not tested
</span></pre></div></div></li></ul><ul><li>latex:
<blockquote class="citation">
<p>
I see <code>real_nth_root</code> as representing the real function x<sup>1/n</sup> with variable x taking real numbers (possibly negative for odd n). The latex print is for humans. So why not just print x<sup>1/n</sup>?
</p>
</blockquote>
It's a little tricky, because the humans it is intended for might not realize it's not quite the same. mjo or Nils, any thoughts? I can go with it for now.
</li></ul><ul><li>calculus:
<blockquote class="citation">
<p>
Done.
</p>
</blockquote>
Thanks. However, that brings up the related issue that the online documentation is only going to show the <code>_init_</code> method doc (and apparently not the tests?).
</li></ul><blockquote>
<p>
If you are up for it, I'd recommend making a more "narrative" structure which includes examples of calculus, plotting, etc., more like <a class="ext-link" href="https://git.sagemath.org/sage.git/tree/src/sage/functions/other.py?id=00380b09135e8171593108059f17490a23f554ad#n1208"><span class="icon"></span>real_part</a> does. If you'd prefer me to do that, I can - might just take a little longer as I recall how to add to a public branch :-)
</p>
</blockquote>
<ul><li>diff output:
<blockquote class="citation">
<blockquote class="citation">
<pre class="wiki">sage: assume(x,'real')
sage: F.diff(x)
1/5*real_nth_root(x^(-4), 5)
</pre><blockquote>
<p>
should mimic this, if not perhaps always. Any thoughts on this?
</p>
</blockquote>
</blockquote>
<p>
I don't get your concern. I think there is nothing wrong with
</p>
<pre class="wiki">sage: f = real_nth_root(x,3)
sage: f.diff()
1/3*real_nth_root(x^(-2), 3)
</pre></blockquote>
If <code>n</code> is odd, though, <code>2*abs(x)^(1/5)*dirac_delta(x) + 1/5*x*sgn(x)/abs(x)^(9/5)</code> is somehow "better", and available. Just thinking out loud; could be for a followup ticket.
</li></ul><ul><li>input:
<blockquote class="citation">
<blockquote class="citation">
<pre class="wiki">sage: H = real_nth_root(x,2)
sage: (H.subs(x=-1))^2
---------------------------------------------------------------------------
...
ValueError: no real nth root of negative real number with even n
sage: ((H)^2).subs(x=-1)
1
</pre><blockquote>
<p>
I'm not sure the latter should be allowed or not.
</p>
</blockquote>
</blockquote>
<p>
No. <code>real_nth_root</code> takes real input and outputs real. For the symbolic function <code>1/x</code>, we assume <code>x</code> takes nonzero value. Likewise, we assume real value x for <code>real_nth_root(x, 3)</code>.
</p>
</blockquote>
Wait, so are you saying that we should disallow that function with <code>x=-1</code> to be evaluated? Because <code>1/x</code> blows up if we inject <code>x=0</code> - though then again Sage simplifies <code>1/x*x =1</code> which isn't technically correct. Anyway, I think this is Nils' point.
</li></ul>
TicketkcrismanFri, 19 Jun 2020 14:20:01 GMT
https://trac.sagemath.org/ticket/12074#comment:41
https://trac.sagemath.org/ticket/12074#comment:41
<blockquote class="citation">
<p>
I was thinking of something simpler, like
</p>
<pre class="wiki">sage: nth_root(RR(-1), 3)
-1.00000000000000
sage: nth_root(CC(-1), 3)
0.500000000000000 + 0.866025403784439*I
</pre></blockquote>
<p>
I feel like this would be another ticket, and need sage-devel discussion. This ticket is about adding something to aid in plotting, and then making sure it has enough bells and whistles that it doesn't end up causing confusion. I'm a little uncomfortable with this since
</p>
<pre class="wiki">sage: RR(-1) == CC(-1)
True
</pre><p>
and the point of the function is strictly for pedagogical purposes (which should also be made clear in the documentation).
</p>
TicketmjoFri, 19 Jun 2020 14:44:37 GMT
https://trac.sagemath.org/ticket/12074#comment:42
https://trac.sagemath.org/ticket/12074#comment:42
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/12074#comment:41" title="Comment 41">kcrisman</a>:
</p>
<blockquote class="citation">
<p>
I feel like this would be another ticket, and need sage-devel discussion. This ticket is about adding something to aid in plotting, and then making sure it has enough bells and whistles that it doesn't end up causing confusion. I'm a little uncomfortable with this since
</p>
<pre class="wiki">sage: RR(-1) == CC(-1)
True
</pre><p>
and the point of the function is strictly for pedagogical purposes (which should also be made clear in the documentation).
</p>
</blockquote>
<p>
We run into that same problem whenever we have a subclass method that does something different from its superclass method. I was only trying to clear up my previous suggestion. If the authors/reviewers don't think this is a good idea, feel free to ignore it.
</p>
TicketkcrismanFri, 19 Jun 2020 14:57:18 GMT
https://trac.sagemath.org/ticket/12074#comment:43
https://trac.sagemath.org/ticket/12074#comment:43
<blockquote class="citation">
<p>
I was only trying to clear up my previous suggestion.
</p>
</blockquote>
<p>
Fair enough.
</p>
<blockquote class="citation">
<p>
If the authors/reviewers don't think this is a good idea, feel free to ignore it.
</p>
</blockquote>
<p>
If this gets merged, why not open a new ticket for an <code>nth_root</code> function if you think it's more generally useful? SymPy has some explicit warnings about these things for its function but we could follow that model (its function is intended for more than the current ticket, I believe).
</p>
TicketmjoFri, 19 Jun 2020 15:06:13 GMT
https://trac.sagemath.org/ticket/12074#comment:44
https://trac.sagemath.org/ticket/12074#comment:44
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/12074#comment:43" title="Comment 43">kcrisman</a>:
</p>
<blockquote class="citation">
<p>
If this gets merged, why not open a new ticket for an <code>nth_root</code> function if you think it's more generally useful? SymPy has some explicit warnings about these things for its function but we could follow that model (its function is intended for more than the current ticket, I believe).
</p>
</blockquote>
<p>
I'm not 100% convinced it's the right thing to do either. Especially if we already have <code>real_nth_root</code>, then we have <code>^(1/n)</code> for the complex root, and... presumably you know which one you want? I dunno. But until someone finds the existing methods lacking, adding them proactively just increases the chances that we'll have gotten it wrong when the need does arise.
</p>
TicketgitMon, 22 Jun 2020 01:47:08 GMTcommit changed
https://trac.sagemath.org/ticket/12074#comment:45
https://trac.sagemath.org/ticket/12074#comment:45
<ul>
<li><strong>commit</strong>
changed from <em>00380b09135e8171593108059f17490a23f554ad</em> to <em>f8cb7a0429c2e696dc69db8466ecc077b0bf0f32</em>
</li>
</ul>
<p>
Branch pushed to git repo; I updated commit sha1. New commits:
</p>
<table class="wiki">
<tr><td><a class="ext-link" href="https://git.sagemath.org/sage.git/commit/?id=f8cb7a0429c2e696dc69db8466ecc077b0bf0f32"><span class="icon"></span>f8cb7a0</a></td><td><code>Refactored docs</code>
</td></tr></table>
TicketkleeMon, 22 Jun 2020 02:07:41 GMT
https://trac.sagemath.org/ticket/12074#comment:46
https://trac.sagemath.org/ticket/12074#comment:46
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/12074#comment:40" title="Comment 40">kcrisman</a>:
</p>
<blockquote class="citation">
<p>
However, that brings up the related issue that the online documentation is only going to show the <code>_init_</code> method doc (and apparently not the tests?).
</p>
<blockquote>
<p>
If you are up for it, I'd recommend making a more "narrative" structure which includes examples of calculus, plotting, etc., more like <a class="ext-link" href="https://git.sagemath.org/sage.git/tree/src/sage/functions/other.py?id=00380b09135e8171593108059f17490a23f554ad#n1208"><span class="icon"></span>real_part</a> does. If you'd prefer me to do that, I can - might just take a little longer as I recall how to add to a public branch :-)
</p>
</blockquote>
</blockquote>
<p>
Done.
</p>
<blockquote class="citation">
<ul><li>input:
<blockquote class="citation">
<blockquote class="citation">
<pre class="wiki">sage: H = real_nth_root(x,2)
sage: (H.subs(x=-1))^2
---------------------------------------------------------------------------
...
ValueError: no real nth root of negative real number with even n
sage: ((H)^2).subs(x=-1)
1
</pre><blockquote>
<p>
I'm not sure the latter should be allowed or not.
</p>
</blockquote>
</blockquote>
<p>
No. <code>real_nth_root</code> takes real input and outputs real. For the symbolic function <code>1/x</code>, we assume <code>x</code> takes nonzero value. Likewise, we assume real value x for <code>real_nth_root(x, 3)</code>.
</p>
</blockquote>
Wait, so are you saying that we should disallow that function with <code>x=-1</code> to be evaluated?
</li></ul></blockquote>
<p>
Yes. What should <code>real_nth_root(-1, 2)</code> evaluate to?
</p>
<blockquote class="citation">
<p>
Sage simplifies <code>1/x*x =1</code> which isn't technically correct.
</p>
</blockquote>
<p>
Why is it not correct? It is correct but you should not evaluate 1/x at x=0.
</p>
TicketkcrismanMon, 22 Jun 2020 12:49:36 GMT
https://trac.sagemath.org/ticket/12074#comment:47
https://trac.sagemath.org/ticket/12074#comment:47
<blockquote class="citation">
<p>
Done.
</p>
</blockquote>
<p>
Thanks, I'll check that out momentarily to confirm it builds properly. Looks good so far.
</p>
<blockquote class="citation">
<p>
</p>
<blockquote class="citation">
<ul><li>input:
<blockquote class="citation">
<blockquote class="citation">
<pre class="wiki">sage: H = real_nth_root(x,2)
sage: (H.subs(x=-1))^2
---------------------------------------------------------------------------
...
ValueError: no real nth root of negative real number with even n
sage: ((H)^2).subs(x=-1)
1
</pre><blockquote>
<p>
I'm not sure the latter should be allowed or not.
</p>
</blockquote>
</blockquote>
<p>
No. <code>real_nth_root</code> takes real input and outputs real. For the symbolic function <code>1/x</code>, we assume <code>x</code> takes nonzero value. Likewise, we assume real value x for <code>real_nth_root(x, 3)</code>.
</p>
</blockquote>
Wait, so are you saying that we should disallow that function with <code>x=-1</code> to be evaluated?
</li></ul></blockquote>
<p>
Yes. What should <code>real_nth_root(-1, 2)</code> evaluate to?
</p>
</blockquote>
<p>
I meant <code>((real_nth_root(x, 2))^2).subs(x=-1)</code>. Are you saying this should be allowed or not allowed?
</p>
<blockquote class="citation">
<blockquote class="citation">
<p>
Sage simplifies <code>1/x*x =1</code> which isn't technically correct.
</p>
</blockquote>
<p>
Why is it not correct? It is correct but you should not evaluate 1/x at x=0.
</p>
</blockquote>
<p>
Depends on whether such expressions are meant to include domains - but anyway that is not directly relevant.
</p>
TicketkcrismanMon, 22 Jun 2020 18:44:13 GMTreviewer set
https://trac.sagemath.org/ticket/12074#comment:48
https://trac.sagemath.org/ticket/12074#comment:48
<ul>
<li><strong>reviewer</strong>
set to <em>Karl-Dieter Crisman, Nils Bruin, Kwanyku Lee</em>
</li>
</ul>
<p>
Thank you for all the good work - this is really <em>long</em> since overdue.
</p>
<blockquote class="citation">
<blockquote class="citation">
<p>
Yes. What should <code>real_nth_root(-1, 2)</code> evaluate to?
</p>
</blockquote>
<p>
I meant <code>((real_nth_root(x, 2))^2).subs(x=-1)</code>. Are you saying this should be allowed or not allowed?
</p>
</blockquote>
<p>
Again, see <a class="ticket" href="https://trac.sagemath.org/ticket/12074#comment:21" title="Comment 21">comment:21</a>. It is good that <code>real_nth_root(-1, 2)</code> does not evaluate, but the question is whether the <em>symbolic</em> simplification should do this when <code>((real_nth_root(x, 2))^2)</code> doesn't really make sense for <code>x=-1</code>. Here is another example.
</p>
<pre class="wiki">sage: ((real_nth_root(x, 2))^2)
real_nth_root(x^2, 2)
sage: f(x) = _
sage: f
x |--> real_nth_root(x^2, 2)
sage: f(-1)
1
</pre><p>
I think if in <code>_power_</code> we had something like
</p>
<pre class="wiki">if exp % 2 == 1:
return self(base**power_param, exp)
else:
....
</pre><p>
that might solve it (suitable tests included). What do you think? Unfortunately the most naive things I tried for the <code>else</code> clause led to nasty errors; I wasted about 20 minutes on abort errors, so I have a feeling that what I did leads to Pynac problems. (Anyone else interested in this particular question other than me or Kwankyu?)
</p>
<p>
Probably we don't need to <code>assert</code> that <code>n</code> is a positive integer since the documentation clearly says so and since why would anyone use it any other way? (Famous last words.)
</p>
TicketkleeTue, 23 Jun 2020 00:40:09 GMT
https://trac.sagemath.org/ticket/12074#comment:49
https://trac.sagemath.org/ticket/12074#comment:49
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/12074#comment:48" title="Comment 48">kcrisman</a>:
</p>
<blockquote class="citation">
<p>
Thank you for all the good work - this is really <em>long</em> since overdue.
</p>
<blockquote class="citation">
<blockquote class="citation">
<p>
Yes. What should <code>real_nth_root(-1, 2)</code> evaluate to?
</p>
</blockquote>
<p>
I meant <code>((real_nth_root(x, 2))^2).subs(x=-1)</code>. Are you saying this should be allowed or not allowed?
</p>
</blockquote>
</blockquote>
<p>
Allowed. This is exactly the same problem with
</p>
<pre class="wiki">sage: (1/x)*x
1
sage: _.subs(x=0)
1
</pre><p>
which we don't bother to fix in any way. If you argue the cases are different because the difference of the domains of definition is of measure zero, then how about this:
</p>
<pre class="wiki">sage: f = 1/floor(abs(x))*floor(abs(x))
sage: f.subs(x=1/2)
1
</pre><p>
Perhaps it is important that these things do not cause real problems because the domain of definition never gets smaller from the initial domain of definition that one starts with.
</p>
TicketkcrismanTue, 23 Jun 2020 01:23:29 GMTstatus changed
https://trac.sagemath.org/ticket/12074#comment:50
https://trac.sagemath.org/ticket/12074#comment:50
<ul>
<li><strong>status</strong>
changed from <em>needs_work</em> to <em>positive_review</em>
</li>
</ul>
<blockquote class="citation">
<p>
Allowed. This is exactly the same problem with
</p>
</blockquote>
<p>
Okay, I'll roll with that. Haven't heard from any of the other people regarding that here, I don't have a big commitment here since this isn't really switchable to "regular" root functions. Thank you!
</p>
TicketkleeTue, 23 Jun 2020 06:10:50 GMT
https://trac.sagemath.org/ticket/12074#comment:51
https://trac.sagemath.org/ticket/12074#comment:51
<p>
Thank you!
</p>
TicketegourgoulhonMon, 29 Jun 2020 22:24:50 GMT
https://trac.sagemath.org/ticket/12074#comment:52
https://trac.sagemath.org/ticket/12074#comment:52
<p>
Thank you very much for making this happen!
</p>
TicketvbraunWed, 08 Jul 2020 19:32:43 GMTstatus, branch changed; resolution set
https://trac.sagemath.org/ticket/12074#comment:53
https://trac.sagemath.org/ticket/12074#comment:53
<ul>
<li><strong>status</strong>
changed from <em>positive_review</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>fixed</em>
</li>
<li><strong>branch</strong>
changed from <em>public/12074</em> to <em>f8cb7a0429c2e696dc69db8466ecc077b0bf0f32</em>
</li>
</ul>
TicketmkoeppeSat, 24 Oct 2020 20:37:10 GMTreviewer changed; commit deleted
https://trac.sagemath.org/ticket/12074#comment:54
https://trac.sagemath.org/ticket/12074#comment:54
<ul>
<li><strong>reviewer</strong>
changed from <em>Karl-Dieter Crisman, Nils Bruin, Kwanyku Lee</em> to <em>Karl-Dieter Crisman, Nils Bruin, Kwankyu Lee</em>
</li>
<li><strong>commit</strong>
<em>f8cb7a0429c2e696dc69db8466ecc077b0bf0f32</em> deleted
</li>
</ul>
Ticket