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)



On Thu, Jun 19, 2014 at 6:13 AM, Nils Bruin <nbr...@sfu.ca> wrote:
> 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)
>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to sage-devel@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 email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to