python - Why can't i raise to a negative power in numpy? -


i'm modelling riemann theta function:

import numpy np def theta(s, n=100):     a_range = np.arange(2, n + 1)     return 1 + sum(1/(a_range ** s)) 

it not work negative s; e.g. theta(-2) leads error:

      1 def theta(s, n=100):       2     a_range = np.arange(1) ----> 3     return 1 + sum(1/(a_range ** s))       4        5 theta(-2)        valueerror: integers negative integer powers not allowed. 

why's that? x^-1 should 1/x if recall math correctly.

in numpy, output dtype of operation a_range ** s determined input dtypes, not values. means raising integers integer powers must give integers, or give floats.

it's important numpy.array([2]) ** numpy.array([2]) give integer output, means numpy.array([2]) ** numpy.array([-2]) has give integers or nothing. picked nothing; raising integers negative integer powers error in numpy.

if want floating-point output, make floating-point input:

a_range = np.arange(2, n + 1, dtype=float) 

or

a_range = np.arange(2, n + 1).astype(float) 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -