ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 13 Nov 2013 08:54:11 +0100Is there a simple way to deal with computing real nth roots for n a natural number?https://ask.sagemath.org/question/10730/is-there-a-simple-way-to-deal-with-computing-real-nth-roots-for-n-a-natural-number/I am trying to use the nth root for natural numbers in computations and display the result as a decimal to four places.
I can't find a simple reference for these functions. Is there an nroot(x,n) function?Tue, 12 Nov 2013 19:07:58 +0100https://ask.sagemath.org/question/10730/is-there-a-simple-way-to-deal-with-computing-real-nth-roots-for-n-a-natural-number/Answer by kcrisman for <p>I am trying to use the nth root for natural numbers in computations and display the result as a decimal to four places.
I can't find a simple reference for these functions. Is there an nroot(x,n) function?</p>
https://ask.sagemath.org/question/10730/is-there-a-simple-way-to-deal-with-computing-real-nth-roots-for-n-a-natural-number/?answer=15678#post-id-15678Usually this comes up in plotting. See [the Sage reference](http://sagemath.org/doc/reference/plotting/sage/plot/plot.html) and search the page for "cube root".
sage: a = 2
sage: b = a.n()
sage: b
2.00000000000000
sage: b.nth_root(3)
1.25992104989487
sage: _^3 # _ means the previous output
2.00000000000000
There is an `nth_root` for real numbers like this, but be careful if you want to try this for plain old integers:
sage: a.nth_root(3)
---------------------------------------------------------------------------
<snip>
ValueError: 2 is not a 3rd power
Tue, 12 Nov 2013 21:07:00 +0100https://ask.sagemath.org/question/10730/is-there-a-simple-way-to-deal-with-computing-real-nth-roots-for-n-a-natural-number/?answer=15678#post-id-15678Comment by kcrisman for <p>Usually this comes up in plotting. See <a href="http://sagemath.org/doc/reference/plotting/sage/plot/plot.html">the Sage reference</a> and search the page for "cube root".</p>
<pre><code>sage: a = 2
sage: b = a.n()
sage: b
2.00000000000000
sage: b.nth_root(3)
1.25992104989487
sage: _^3 # _ means the previous output
2.00000000000000
</code></pre>
<p>There is an <code>nth_root</code> for real numbers like this, but be careful if you want to try this for plain old integers:</p>
<pre><code>sage: a.nth_root(3)
---------------------------------------------------------------------------
<snip>
ValueError: 2 is not a 3rd power
</code></pre>
https://ask.sagemath.org/question/10730/is-there-a-simple-way-to-deal-with-computing-real-nth-roots-for-n-a-natural-number/?comment=16712#post-id-16712Yes, output of course - I'll edit that.Wed, 13 Nov 2013 08:54:11 +0100https://ask.sagemath.org/question/10730/is-there-a-simple-way-to-deal-with-computing-real-nth-roots-for-n-a-natural-number/?comment=16712#post-id-16712Comment by gundamlh for <p>Usually this comes up in plotting. See <a href="http://sagemath.org/doc/reference/plotting/sage/plot/plot.html">the Sage reference</a> and search the page for "cube root".</p>
<pre><code>sage: a = 2
sage: b = a.n()
sage: b
2.00000000000000
sage: b.nth_root(3)
1.25992104989487
sage: _^3 # _ means the previous output
2.00000000000000
</code></pre>
<p>There is an <code>nth_root</code> for real numbers like this, but be careful if you want to try this for plain old integers:</p>
<pre><code>sage: a.nth_root(3)
---------------------------------------------------------------------------
<snip>
ValueError: 2 is not a 3rd power
</code></pre>
https://ask.sagemath.org/question/10730/is-there-a-simple-way-to-deal-with-computing-real-nth-roots-for-n-a-natural-number/?comment=16717#post-id-16717"# _ means the previous input" or output? equivalent to "ans" in MATLAB. And we can use a.nth_root(3, truncate_mode = 1). --> (1, False).Wed, 13 Nov 2013 04:04:08 +0100https://ask.sagemath.org/question/10730/is-there-a-simple-way-to-deal-with-computing-real-nth-roots-for-n-a-natural-number/?comment=16717#post-id-16717Answer by Shashank for <p>I am trying to use the nth root for natural numbers in computations and display the result as a decimal to four places.
I can't find a simple reference for these functions. Is there an nroot(x,n) function?</p>
https://ask.sagemath.org/question/10730/is-there-a-simple-way-to-deal-with-computing-real-nth-roots-for-n-a-natural-number/?answer=15691#post-id-15691I don't think there is one but you can define one using a couple of lines of code
def nroot(x,n):
return x**(1/n).n()
nroot(8,3)Tue, 12 Nov 2013 19:52:00 +0100https://ask.sagemath.org/question/10730/is-there-a-simple-way-to-deal-with-computing-real-nth-roots-for-n-a-natural-number/?answer=15691#post-id-15691Comment by Shashank for <p>I don't think there is one but you can define one using a couple of lines of code</p>
<pre><code>def nroot(x,n):
return x**(1/n).n()
nroot(8,3)
</code></pre>
https://ask.sagemath.org/question/10730/is-there-a-simple-way-to-deal-with-computing-real-nth-roots-for-n-a-natural-number/?comment=16718#post-id-16718Sorry about that.Tue, 12 Nov 2013 21:09:46 +0100https://ask.sagemath.org/question/10730/is-there-a-simple-way-to-deal-with-computing-real-nth-roots-for-n-a-natural-number/?comment=16718#post-id-16718Comment by Martin Flashman for <p>I don't think there is one but you can define one using a couple of lines of code</p>
<pre><code>def nroot(x,n):
return x**(1/n).n()
nroot(8,3)
</code></pre>
https://ask.sagemath.org/question/10730/is-there-a-simple-way-to-deal-with-computing-real-nth-roots-for-n-a-natural-number/?comment=16722#post-id-16722
Not so easy when you try
def nroot(x,n):
return x**(1/n).n()
nroot(-8,3)
You don't get the real root- but instead:
1.00000000000000 + 1.73205080756888*I
There's the rub. :( Tue, 12 Nov 2013 20:55:35 +0100https://ask.sagemath.org/question/10730/is-there-a-simple-way-to-deal-with-computing-real-nth-roots-for-n-a-natural-number/?comment=16722#post-id-16722Comment by Shashank for <p>I don't think there is one but you can define one using a couple of lines of code</p>
<pre><code>def nroot(x,n):
return x**(1/n).n()
nroot(8,3)
</code></pre>
https://ask.sagemath.org/question/10730/is-there-a-simple-way-to-deal-with-computing-real-nth-roots-for-n-a-natural-number/?comment=16721#post-id-16721It is supposed to be complex. You cannot have real cube root for -8.Tue, 12 Nov 2013 21:01:39 +0100https://ask.sagemath.org/question/10730/is-there-a-simple-way-to-deal-with-computing-real-nth-roots-for-n-a-natural-number/?comment=16721#post-id-16721Comment by kcrisman for <p>I don't think there is one but you can define one using a couple of lines of code</p>
<pre><code>def nroot(x,n):
return x**(1/n).n()
nroot(8,3)
</code></pre>
https://ask.sagemath.org/question/10730/is-there-a-simple-way-to-deal-with-computing-real-nth-roots-for-n-a-natural-number/?comment=16720#post-id-16720You can't? I always thought -2 cubed was -8 ... :-) But it is true that Sage returns a sort of "primitive nth root" (whatever that means) by default.Tue, 12 Nov 2013 21:08:10 +0100https://ask.sagemath.org/question/10730/is-there-a-simple-way-to-deal-with-computing-real-nth-roots-for-n-a-natural-number/?comment=16720#post-id-16720