Symbolic nth_root()

106 views
Skip to first unread message

Jeroen Demeyer

unread,
Nov 20, 2011, 8:52:29 AM11/20/11
to sage-devel
When searching sage-support, there are several threads about people
complaining they cannot plot x^(1/3) or similar on the negative axis
because (-1)^(1/3) is a complex number, not a real number.

The answer is to plot something like lambda x:RR(x).nth_root(3). This
is not very satisfactory because this is not a symbolic function, so I
can't differentiate it for example. It is also more complicated and
hard to explain to beginners using Sage.

So I think the question still remains: is there something like a
symbolic nth_root() function? Or, alternatively, a way of evaluating
the symbolic expression x^(1/3) yielding only real numbers with real
input? Such functionality would certainly be desirable.

Jeroen.

William Stein

unread,
Nov 20, 2011, 9:16:18 PM11/20/11
to sage-...@googlegroups.com, pynac...@googlegroups.com

I don't think it exists, The symbolic functionality in Sage is
supposed to make it "easy" for users to define a new symbolic function
at runtime, including how that function gets simplified. This is
supposed to not involve any C++ coding with Pynac. So it _should_ be
easy to add what you suggest. Maybe Burcin can pipe up.

-- William

>
> Jeroen.
>
> --
> To post to this group, send an email to sage-...@googlegroups.com
> To unsubscribe from this group, send an email to sage-devel+...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-devel
> URL: http://www.sagemath.org
>

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

Rob Beezer

unread,
Nov 20, 2011, 9:22:11 PM11/20/11
to sage-devel
On Nov 20, 5:52 am, Jeroen Demeyer <jdeme...@cage.ugent.be> wrote:
> So I think the question still remains: is there something like a
> symbolic nth_root() function?  Or, alternatively, a way of evaluating
> the symbolic expression x^(1/3) yielding only real numbers with real
> input?  Such functionality would certainly be desirable.

A huge +1. Definitely extremely desirable. This is one of those
stumbling blocks that makes it harder than it needs to be to use Sage
with calculus students, or with examples worthy of a calculus student.

Rob

Burcin Erocal

unread,
Nov 22, 2011, 6:53:31 AM11/22/11
to sage-...@googlegroups.com, pynac...@googlegroups.com
On Sun, 20 Nov 2011 18:16:18 -0800
William Stein <wst...@gmail.com> wrote:

> On Sun, Nov 20, 2011 at 5:52 AM, Jeroen Demeyer
> <jdem...@cage.ugent.be> wrote:
> > When searching sage-support, there are several threads about people
> > complaining they cannot plot x^(1/3) or similar on the negative axis
> > because (-1)^(1/3) is a complex number, not a real number.
> >
> > The answer is to plot something like lambda x:RR(x).nth_root(3).
> >  This is not very satisfactory because this is not a symbolic
> > function, so I can't differentiate it for example.  It is also more
> > complicated and hard to explain to beginners using Sage.
> >
> > So I think the question still remains: is there something like a
> > symbolic nth_root() function?  Or, alternatively, a way of
> > evaluating the symbolic expression x^(1/3) yielding only real
> > numbers with real input?  Such functionality would certainly be
> > desirable.
>
> I don't think it exists, The symbolic functionality in Sage is
> supposed to make it "easy" for users to define a new symbolic function
> at runtime, including how that function gets simplified. This is
> supposed to not involve any C++ coding with Pynac. So it _should_ be
> easy to add what you suggest. Maybe Burcin can pipe up.

Here is a symbolic function which wraps RR.nth_root():


from sage.symbolic.function import BuiltinFunction, is_inexact
from sage.symbolic.expression import Expression
from sage.structure.coerce import parent

class RealNthRoot(BuiltinFunction):
def __init__(self):
BuiltinFunction.__init__(self, "real_nth_root", nargs=2)

def _eval_(self, base, exp):
if (not isinstance(base, Expression) and is_inexact(base)) or \
(not isinstance(exp, Expression) and is_inexact(exp)):
self._evalf_(base, exp, parent=parent(base))

def _evalf_(self, base, exp, parent=None):
if isinstance(base, float):
return RR(base).nth_root(exp)
try:
return base.nth_root(exp)
except AttributeError:
return base**(1/exp)
return parent(base)**parent(exp)


The code is also here:

http://sage.math.washington.edu/home/burcin/real_nth_root.py

I can plot this without trouble:

sage: attach real_nth_root.py
sage: real_nth_root = RealNthRoot()
sage: v = real_nth_root(x, 3)
sage: plot(v, (x, -1, 1))
<firefox displays the plot>


Cheers,
Burcin

Burcin Erocal

unread,
Nov 23, 2011, 9:48:44 AM11/23/11
to pynac...@googlegroups.com, sage-...@googlegroups.com

> Maybe this would be the best solution. If you put this on a ticket
> with patch I'll try to make sure this gets reviewed properly at Sage
> Days 35.5.

Thanks! I attached a patch to #12074. Still needs documentation and
tests.

http://trac.sagemath.org/sage_trac/ticket/12074


Cheers,
Burcin

Reply all
Reply to author
Forward
0 new messages