Raymond Hettinger added the comment:
If this ends up going forward (and I'm don't believe a good case has been
made), I would prefer the function to be called "nth_root" which is unequivocal
and readable (it is also close to what Matlab uses:
http://www.mathworks.com/help/matlab/ref/nthroot.html )
It seems that Matlab's reason for inclusion doesn't have anything to do with
precision; instead, they say "While power is a more efficient function for
computing the roots of numbers, in cases where both real and complex roots
exist, power returns only the complex roots. In these cases, use nthroot to
obtain the real roots." Mathematica uses Surd[n, x] for that purpose.
Outside of Matlab and Mathematica, I'm not seeing this function elsewhere (on
my calculator, in Java math, etc.). This suggests that the need is minimal.
As an alternative, we could follow the model used in the itertools module and
include "recipes" for functions that don't meet that bar for inclusion in the
standard library. Here's one recipe I found after a few seconds of googling:
from decimal import Decimal, getcontext
def nthroot (n, A, precision):
getcontext().prec = precision
n = Decimal(n)
x_0 = A / n #step 1: make a while guess.
x_1 = 1 #need it to exist before step 2
while True:
#step 2:
x_0, x_1 = x_1, (1 / n)*((n - 1)*x_0 + (A / (x_0 ** (n - 1))))
if x_0 == x_1:
return x_1
Out of Steven's original suggestions, I most prefer "leave nth_root in
statistics as a private function".
----------
nosy: +rhettinger, skrah
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue27353>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com