cube roots in Sage

320 views
Skip to first unread message

Gregory Bard

unread,
Jun 18, 2014, 2:37:21 AM6/18/14
to sage-...@googlegroups.com
This has been brought up many times before, but I'd like to bring up
the possibility of adding two commands to Sage: cuberoot(x) and
nthroot(x, n)

The reason is that currently plot( x^(1/3), -5, 5) only shows values for x>0,
and not for x<0. The current work-around recommended is

plot(sign(x)*abs(x)^(1/3), -5, 5)
(Track ticket #11458)

which replaced the hideous

plot(lambda x: RR(x).nth_root(3), -5, 5, plot_points=20)

This is true in classes like Precalculus and Calculus 1, where trying to explain
workarounds like these would just really a headache for the instructor and
extremely fragile students.

Also the cube root can occur in many other situations and applications.
This is urgent, because my textbook "Sage for Undergraduates" is due
at the American Mathematical Society on June 30th.

The namespace is so huge, can't we just add two more commands?

I suggest:

def cuberoot(x):
    return sign(x)*((x*sign(x))^(1/3))

Last but not least, links to previous demands for exactly this problem:

https://www.mail-archive.com/sage-s...@googlegroups.com/msg11563.html
https://groups.google.com/forum/#!msg/sage-devel/_JeSMD-Kvfk/xeNstGrcvXQJ
https://groups.google.com/forum/#!topic/sage-support/icZ8ekC_P4Y
https://groups.google.com/forum/#!topic/sage-devel/_JeSMD-Kvfk
http://trac.sagemath.org/ticket/11458
https://sage.uwstout.edu/home/pub/32/
https://groups.google.com/forum/#!topic/sage-support/ZtRWScqMHMM
https://groups.google.com/forum/#!topic/sage-devel/_JeSMD-Kvfk

Jori Mantysalo

unread,
Jun 18, 2014, 3:02:27 AM6/18/14
to sage-...@googlegroups.com
On Tue, 17 Jun 2014, Gregory Bard wrote:

> This has been brought up many times before, but I'd like to bring up
> the possibility of adding two commands to Sage: cuberoot(x) and
> nthroot(x, n)

Could you summarize previos discussion(s)?

For example what should cuberoot(1)^2 be? Different from cuberoot(1)? How
about "realroot(x, n)"?

--
Jori Mäntysalo

Nils Bruin

unread,
Jun 18, 2014, 8:44:22 PM6/18/14
to sage-...@googlegroups.com
On Wednesday, June 18, 2014 2:37:21 AM UTC-4, Gregory Bard wrote:
This has been brought up many times before, but I'd like to bring up
the possibility of adding two commands to Sage: cuberoot(x) and
nthroot(x, n)
+1 for nthroot. Once we have that, I don't think we need cuberoot. The function should only accept (positive?) integer arguments n. For even n it could have the usual behaviour, for odd n it could do the sign thing. It's possibly nicer to implement it as a symbolic function with custom numerical evaluation rather than a python function that produces a relatively cryptic symbolic expression. It should be documented as the "real-valued n-th root of real-valued expressions".

Please look whether we should have nthroot(n,x) or nthroot(x,n), or perhaps even nthroot[n](x) [I hope not].

Vincent Delecroix

unread,
Jun 19, 2014, 2:38:11 AM6/19/14
to sage-...@googlegroups.com
Note that there is already a method "nth_root" on several elements
(ZZ, finite fields, etc). So I would rather go for "real_nth_root"
which makes things clearer.

Vincent

2014-06-19 2:44 UTC+02:00, Nils Bruin <nbr...@sfu.ca>:
> --
> 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+...@googlegroups.com.
> To post to this group, send email to sage-...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sage-devel.
> For more options, visit https://groups.google.com/d/optout.
>

Nils Bruin

unread,
Jun 19, 2014, 9:13:25 AM6/19/14
to sage-...@googlegroups.com
On Thursday, June 19, 2014 2:38:11 AM UTC-4, vdelecroix wrote:
Note that there is already a method "nth_root" on several elements
(ZZ, finite fields, etc). So I would rather go for "real_nth_root"
which makes things clearer.
Perhaps we can then just get away with a "nth_root" symbolic function that  uses nth_root on its argument, making use of the fact that if the numerical evaluation of the argument produces a real number, then the real number itself will know what its nth root is? Something along the lines of:

from sage.symbolic.function import SymbolicFunction
class nth_root(SymbolicFunction):
    def __init__(self):
        SymbolicFunction.__init__(self, 'nth_root', nargs=2)
    def _evalf_(self, n, x, parent=None):
        return parent(x).nth_root(n)

(this needs refinement, but numerical evaluation does seem to be able to produce sometimes real elements and sometimes complex numbers, so perhaps we can utilize that)

Gregory Bard

unread,
Jun 20, 2014, 9:33:55 PM6/20/14
to sage-...@googlegroups.com
It seems that the consensus on both Sage-devel and Sage-edu is to go
with some sort of nth_real_root function. I propose the following,
which I have tested for evaluation, plotting, differentiation, and
integration. Sadly, the derivative has a Dirac delta in it, which is
... perhaps unavoidable because of the vertical tangency of the
cuberoot function at x=0. (Naturally, we can remove the asserts once
testing is completed.
---Greg

def nth_real_root( x, n ):
"""Note: n must be a positive integer. However, x is any real number.
(Otherwise this explanation will make no sense.)
For odd values of n, we return the unique real number y such
that y^n = x.
For even values of n, if x<0, there is no real number y such
that y^n = x and so we throw an exception.
For even values of n, if x=>0, then we return the unique real
number y such that y^n = x."""

if ((n in ZZ)==false):
raise RuntimeError('nth_real_root(x,n) requires n to be a
positive integer. Your n is not an integer.')

if (n<=0):
raise RuntimeError('nth_real_root(x,n) requires n to be a
positive integer. Your n is not positive.')

assert (n in ZZ)
assert (n>0)

if ((n%2) == 0):
# n is even
if (x<0):
raise RuntimeError('There is no nth real root (of a
negative number x) when n is even.')
else:
return x^(1/n)

assert ((n%2)==1)
# n is odd
return sign(x)*(abs(x))^(1/n)
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "sage-devel" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/sage-devel/Q8VLKBypcJk/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

Nicolas M. Thiery

unread,
Jun 22, 2014, 11:27:16 AM6/22/14
to sage-...@googlegroups.com
On Fri, Jun 20, 2014 at 06:33:52PM -0700, Gregory Bard wrote:
> It seems that the consensus on both Sage-devel and Sage-edu is to go
> with some sort of nth_real_root function. I propose the following,
> which I have tested for evaluation, plotting, differentiation, and
> integration. Sadly, the derivative has a Dirac delta in it, which is
> ... perhaps unavoidable because of the vertical tangency of the
> cuberoot function at x=0. (Naturally, we can remove the asserts once
> testing is completed.
> ---Greg
>
> def nth_real_root( x, n ):

Just 2 cents of outsider feedback since I have not followed the
discussion, and am not knowledgeable on the topic. This names suggests
to me that we look at all the real roots of n (for whatever this
means), and then take the n-th one. So maybe real_nth_root instead?

Cheers,
Nicolas
--
Nicolas M. Thiéry "Isil" <nth...@users.sf.net>
http://Nicolas.Thiery.name/

William Stein

unread,
Jun 22, 2014, 12:03:02 PM6/22/14
to sage-devel
On Sun, Jun 22, 2014 at 8:27 AM, Nicolas M. Thiery
<Nicolas...@u-psud.fr> wrote:
> On Fri, Jun 20, 2014 at 06:33:52PM -0700, Gregory Bard wrote:
>> It seems that the consensus on both Sage-devel and Sage-edu is to go
>> with some sort of nth_real_root function. I propose the following,
>> which I have tested for evaluation, plotting, differentiation, and
>> integration. Sadly, the derivative has a Dirac delta in it, which is
>> ... perhaps unavoidable because of the vertical tangency of the
>> cuberoot function at x=0. (Naturally, we can remove the asserts once
>> testing is completed.
>> ---Greg
>>
>> def nth_real_root( x, n ):
>
> Just 2 cents of outsider feedback since I have not followed the
> discussion, and am not knowledgeable on the topic. This names suggests
> to me that we look at all the real roots of n (for whatever this
> means), and then take the n-th one. So maybe real_nth_root instead?

+1

>
> Cheers,
> Nicolas
> --
> Nicolas M. Thiéry "Isil" <nth...@users.sf.net>
> http://Nicolas.Thiery.name/
>
> --
> 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+...@googlegroups.com.
> To post to this group, send email to sage-...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sage-devel.
> For more options, visit https://groups.google.com/d/optout.



--
William Stein
Professor of Mathematics
University of Washington
http://wstein.org

Gregory Bard

unread,
Jun 22, 2014, 4:36:29 PM6/22/14
to sage-...@googlegroups.com
Yes, that is reasonable. Let us call it "real_nth_root" instead, as
suggested by Nicolas Thiery. Any other requests/comments?

It would be superb if this could be resolved by June 30th, when my
book goes to the American Mathematical Society for publication...
---Greg
> You received this message because you are subscribed to a topic in the Google Groups "sage-devel" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/sage-devel/Q8VLKBypcJk/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to sage-devel+...@googlegroups.com.

Dima Pasechnik

unread,
Jun 22, 2014, 5:56:34 PM6/22/14
to sage-...@googlegroups.com
On 2014-06-22, Gregory Bard <gregory....@gmail.com> wrote:
> Yes, that is reasonable. Let us call it "real_nth_root" instead, as
> suggested by Nicolas Thiery. Any other requests/comments?

it's better talk about "n-ic" root rather than "n-th" root, IMHO.

Vincent Delecroix

unread,
Jun 22, 2014, 6:00:37 PM6/22/14
to sage-...@googlegroups.com
As Niles already said it would be better to have it as a symbolic function

sage: f(x) = real_nth_root(x, 5)
sage: f
x |--> real_nth_root(x,5)

2014-06-22 22:36 UTC+02:00, Gregory Bard <gregory....@gmail.com>:

Gregory Bard

unread,
Jun 25, 2014, 1:10:06 AM6/25/14
to sage-...@googlegroups.com
As Vincent and Niles have brought up, there might be advantages to it
being a symbolic function. How does one actually go about making that
happen? Is this an intrusive change, or an easy one? I really have no
idea...
---Greg


On Sun, Jun 22, 2014 at 5:00 PM, Vincent Delecroix

Gregory Bard

unread,
Jul 10, 2014, 6:23:21 PM7/10/14
to sage-...@googlegroups.com
I think we have a consensus that we should do *something* but unless I am very much mistaken, the suggestion from Vincent Delecroix and Nils Bruin that we make a symbolic function has advantages.

I was looking at Nils's code, but I have to confess that I don't understand that code. Actually, I wasn't able to get the following to work, but I have a feeling that I am making a minor and stupid mistake with the syntax.

################

from sage.symbolic.function import SymbolicFunction
class nth_root(SymbolicFunction):
    def __init__(self):
        SymbolicFunction.__init__(self, 'nth_root', nargs=2)
    def _evalf_(self, n, x, parent=None):
        return parent(x).nth_root(n)

plot( nth_root( x, 3), -5, 5 )
################

Maybe someone here could explain exactly what the distinction is or what the pros/cons are for my approach versus Nils's? I have no idea.

I have also posted this on Sage-Edu, but perhaps all future replies should be restricted to Sage-Devel and not Sage-Edu?
---Greg
>>>> To post to this group, send email to sage-...@googlegroups.com.
>>>> Visit this group at http://groups.google.com/group/sage-devel.
>>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>>
>>>
>>> --
>>> William Stein
>>> Professor of Mathematics
>>> University of Washington
>>> http://wstein.org
>>>
>>> --
>>> You received this message because you are subscribed to a topic in the
>>> Google Groups "sage-devel" group.
>>> To unsubscribe from this topic, visit
>>> https://groups.google.com/d/topic/sage-devel/Q8VLKBypcJk/unsubscribe.
>>> To unsubscribe from this group and all its topics, send an email to
>>> To post to this group, send email to sage-...@googlegroups.com.
>>> Visit this group at http://groups.google.com/group/sage-devel.
>>> For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> 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
>> To post to this group, send email to sage-...@googlegroups.com.
>> Visit this group at http://groups.google.com/group/sage-devel.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "sage-devel" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/sage-devel/Q8VLKBypcJk/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to sage-devel+unsubscribe@googlegroups.com.

P Purkayastha

unread,
Jul 10, 2014, 6:47:37 PM7/10/14
to sage-...@googlegroups.com, Gregory Bard
Hi Greg,

Hope the following code helps you in implementing your function:

from sage.symbolic.function import SymbolicFunction
class real_nth_root_class(SymbolicFunction):
def __init__(self):
SymbolicFunction.__init__(self, 'real_nth_root', nargs=2)
def _evalf_(self, x, n, parent=None):
# Handle python floats which do not have the 'nth_root' method
if isinstance(x, float):
return float(RDF(x).nth_root(n))
return parent(x).nth_root(n)

real_nth_root = real_nth_root_class()


In actual usage:

sage: real_nth_root( x, 3)
real_nth_root(x, 3)
sage: f(x) = real_nth_root( x, 3)
sage: f
x |--> real_nth_root(x, 3)
sage: plot( real_nth_root( x, 3), -5, 5 )
# plots the graph.


Regards,
basu.
> <20100.d...@gmail.com <mailto:20100.d...@gmail.com>> wrote:
> > As Niles already said it would be better to have it as a
> symbolic function
> >
> > sage: f(x) = real_nth_root(x, 5)
> > sage: f
> > x |--> real_nth_root(x,5)
> >
> > 2014-06-22 22:36 UTC+02:00, Gregory Bard
> <gregory....@gmail.com <mailto:gregory....@gmail.com>>:
> >> Yes, that is reasonable. Let us call it "real_nth_root"
> instead, as
> >> suggested by Nicolas Thiery. Any other requests/comments?
> >>
> >> It would be superb if this could be resolved by June 30th, when my
> >> book goes to the American Mathematical Society for publication...
> >> ---Greg
> >>
> >> On Sun, Jun 22, 2014 at 9:02 AM, William Stein
> <wst...@gmail.com <mailto:wst...@gmail.com>> wrote:
> >>> On Sun, Jun 22, 2014 at 8:27 AM, Nicolas M. Thiery
> >>> <Nicolas...@u-psud.fr <mailto:Nicolas...@u-psud.fr>>
> <mailto:nth...@users.sf.net>>
> >>>> http://Nicolas.Thiery.name/
> >>>>
> >>>> --
> >>>> 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+...@googlegroups.com
> <mailto:sage-devel%2Bunsu...@googlegroups.com>.
> >>>> To post to this group, send email to
> sage-...@googlegroups.com <mailto:sage-...@googlegroups.com>.
> >>>> Visit this group at http://groups.google.com/group/sage-devel
> <http://groups.google.com/group/sage-devel>.
> >>>> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
> >>>
> >>>
> >>>
> >>> --
> >>> William Stein
> >>> Professor of Mathematics
> >>> University of Washington
> >>> http://wstein.org
> >>>
> >>> --
> >>> You received this message because you are subscribed to a
> topic in the
> >>> Google Groups "sage-devel" group.
> >>> To unsubscribe from this topic, visit
> >>>
> https://groups.google.com/d/topic/sage-devel/Q8VLKBypcJk/unsubscribe
> <https://groups.google.com/d/topic/sage-devel/Q8VLKBypcJk/unsubscribe>.
>
> >>> To unsubscribe from this group and all its topics, send an
> email to
> >>> sage-devel+...@googlegroups.com
> <mailto:sage-devel%2Bunsu...@googlegroups.com>.
> >>> To post to this group, send email to
> sage-...@googlegroups.com <mailto:sage-...@googlegroups.com>.
> <http://groups.google.com/group/sage-devel>.
> >>> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
> >>
> >> --
> >> 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+...@googlegroups.com
> <mailto:sage-devel%2Bunsu...@googlegroups.com>.
> >> To post to this group, send email to
> sage-...@googlegroups.com <mailto:sage-...@googlegroups.com>.
> <http://groups.google.com/group/sage-devel>.
> >> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
> >>
> >
> > --
> > You received this message because you are subscribed to a topic
> in the Google Groups "sage-devel" group.
> > To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/sage-devel/Q8VLKBypcJk/unsubscribe
> <https://groups.google.com/d/topic/sage-devel/Q8VLKBypcJk/unsubscribe>.
>
> > To unsubscribe from this group and all its topics, send an email
> to sage-devel+...@googlegroups.com
> <mailto:sage-devel%2Bunsu...@googlegroups.com>.
> > To post to this group, send email to sage-...@googlegroups.com
> <mailto:sage-...@googlegroups.com>.
> <http://groups.google.com/group/sage-devel>.
> > For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "sage-devel" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/sage-devel/Q8VLKBypcJk/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> sage-devel+...@googlegroups.com
> <mailto:sage-devel+...@googlegroups.com>.
> To post to this group, send email to sage-...@googlegroups.com
> <mailto:sage-...@googlegroups.com>.

Gregory Bard

unread,
Jun 11, 2020, 2:24:01 PM6/11/20
to sage-devel
Dear Sage developers,

I'm currently working on a 2nd edition of my book Sage for Undergraduates,
which adds some new material but mostly updates the book to reflect syntax
changes in Sage since mid-2014, especially relating to the Python2 to Python3
transition.

Some of you might remember a spirited discussion in June of 2014 regarding
cube roots, and real values of nth roots of negative reals, especially as
concerns plotting. For example, compare the two plots:


The eventual consensus was that a command, named real_nth_root( ),
was a good way to resolve this matter by adding only one command. Sadly,
the trac ticket has stalled. https://trac.sagemath.org/ticket/12074

It would be really wonderful if some knowledgeable person could jump on
this and code something up, for possible inclusion in a soon-to-be-released
version of Sage. It would be superb if this could be in Sage by the start of
the Fall 2020 semester in the USA, which is in late August. However, since
the ticket has stalled since June of 2014, I'd be happy with any timeline
of any kind. Of course, I'd do the work myself if I knew how, and if I had time,
but I'm currently up to my eyeballs in typo corrections and updating of code,
plus adding some new examples, challenges, and projects.

I cannot emphasize enough how useful this change would be for those of
us who have to teach pre-calculus and differential calculus. The cube root
function is the best example of a function that is continuous everywhere,
but not differentiable everywhere, perhaps tied with f(x) = abs(x).

Please help,
---Greg

p.s. For those unfamiliar with Sage for Undergraduates, it is available for
free as a pdf file, and the American Mathematical Society has published
a print version at a very reasonable price. Diego Sejas has translated it
recently into Spanish, and the Spanish translation uses the up-to-date
Message has been deleted

Diego Sejas

unread,
Jun 11, 2020, 8:38:59 PM6/11/20
to sage-devel
Hello, Sage Community!

I think the addition of a real_nth_root() function could be very beneficial, specially in the teaching context, where using Sage with odd roots of negative numbers implies the use of some programming tweaks. In my case, I lost the count of how many times I forgot (in the middle of a class) about how Sage treats those roots, and wrote "plot(x^(1/3), -2, 2)", just to discover to my great embarrassment that the plot doesn't look as I told my students. (Of course, this is a minor problem, but a quite annoying one, if you ask me.)

The addition of a "real_nth_root" function has the following benefits:
  1. It would make Sage a little bit more flexible for (pre-)calculus, real analysis, and other classes.
  2. It will definitely make the use of Sage more comfortable for plotting and obtaining results in RR. (Plotting the cube root is a simple school exercise, with a simple statement, and a simple result. It shouldn't be much harder to do in Sage.)
  3. It would amplify a little bit the "ready-to-use" scope of Sage.
  4. In the end, this is a much requested feature, so maybe we should respond in some satisfactory way to the community.
  5. We wouldn't be that behind with respect to Mathematica, which plots x^(1/3) as you would expect it. However, we would also have the current mechanism of obtaining complex roots (if required).
Best regards!

kcrisman

unread,
Jun 12, 2020, 10:04:17 AM6/12/20
to sage-devel
Just want to thank Kwankyu for getting this on a proper branch.  I've made a few comments, and encourage anyone with feedback to do the same.  

Gregory Bard

unread,
Aug 19, 2021, 2:58:00 PM8/19/21
to sage-devel
I'm so grateful that this issue of cube roots (and other real nth roots)
has been solved so successfully. It's a huge help to those of us who
teach calculus, and it's a good story to showcase the cooperative
atmosphere of community developed open-source software.

I'd like to thank those responsible in the acknowledgements of the
2nd edition of Sage for Undergraduates. Would everyone who
contributed a medium or large amount to this effort please email
me, and provide the exact spelling of their name, as they would
like to see it appear in print?

With respect and gratitude,
---Greg

Kwankyu Lee

unread,
Aug 20, 2021, 9:27:46 AM8/20/21
to sage-devel
On Friday, August 20, 2021 at 3:58:00 AM UTC+9 Gregory Bard wrote:
I'm so grateful that this issue of cube roots (and other real nth roots)
has been solved so successfully. It's a huge help to those of us who
teach calculus, and it's a good story to showcase the cooperative
atmosphere of community developed open-source software.

I'd like to thank those responsible in the acknowledgements of the
2nd edition of Sage for Undergraduates. Would everyone who
contributed a medium or large amount to this effort please email
me, and provide the exact spelling of their name, as they would
like to see it appear in print?

Thank you. Some of the names appear in the ticket

Reply all
Reply to author
Forward
0 new messages