How does python (or numpy/scipy) do exponentiation? If I do x**p, where p is some positive integer, will it compute x*x*...*x (p times), or will it use logarithms? gideon
On Fri, Jul 25, 2008 at 1:24 PM, Gideon Simpson <grs2103@columbia.edu> wrote:
How does python (or numpy/scipy) do exponentiation? If I do x**p, where p is some positive integer, will it compute x*x*...*x (p times), or will it use logarithms?
Here are some examples:
np.array([[1,2], [3,4]])**2
array([[ 1, 4], [ 9, 16]])
np.matrix([[1,2], [3,4]])**2
matrix([[ 7, 10], [15, 22]])
np.power(np.matrix([[1,2], [3,4]]), 2)
matrix([[ 1, 4], [ 9, 16]])
On Fri, Jul 25, 2008 at 1:32 PM, Keith Goodman <kwgoodman@gmail.com> wrote:
On Fri, Jul 25, 2008 at 1:24 PM, Gideon Simpson <grs2103@columbia.edu> wrote:
How does python (or numpy/scipy) do exponentiation? If I do x**p, where p is some positive integer, will it compute x*x*...*x (p times), or will it use logarithms?
Here are some examples:
np.array([[1,2], [3,4]])**2
array([[ 1, 4], [ 9, 16]])
np.matrix([[1,2], [3,4]])**2
matrix([[ 7, 10], [15, 22]])
np.power(np.matrix([[1,2], [3,4]]), 2)
matrix([[ 1, 4], [ 9, 16]])
Sorry. I see now you're worried about under/overflow.
Fri, 25 Jul 2008 16:24:35 0400, Gideon Simpson wrote:
How does python (or numpy/scipy) do exponentiation? If I do x**p, where p is some positive integer, will it compute x*x*...*x (p times), or will it use logarithms?
For floats it will call operating system's pow, which supposedly is optimized. For integer powers n < 100 of complex numbers it computes the power by repeated squarings and multiplications using a binary decomposition, and for larger n falls back to using a logarithm. For integer powers of integers, it also appears to use repeated squarings and multiplications according to a binary decomposition.  Pauli Virtanen
participants (3)

Gideon Simpson

Keith Goodman

Pauli Virtanen