为什么我不能在numpy中提升为负数? [英] Why can't I raise to a negative power in numpy?

查看:397
本文介绍了为什么我不能在numpy中提升为负数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对Riemann theta函数建模:

I'm modelling the Riemann theta function:

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

不适用于负数s;例如theta(-2)导致此错误:

It does not work for negative s; e.g. theta(-2) leads to this 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 to negative integer powers are not allowed.

那是为什么?如果我没记错我的数学,x^-1应该只是1/x.

Why's that? x^-1 should just be 1/x if i recall my math correctly.

推荐答案

在NumPy中,用于选择a_range ** s之类的操作的输出dtype的逻辑基于dtype,而不是值.这意味着a_range ** -2必须具有与a_range ** 2相同的输出dtype.

In NumPy, the logic used to choose the output dtype of an operation like a_range ** s is based on dtypes, not values. That means that a_range ** -2 has to have the same output dtype as a_range ** 2.

重要的是,像numpy.array([2]) ** 2这样的东西给出整数输出,所以这意味着numpy.array([2]) ** -2必须给出整数或什么都不给出.他们什么也没捡.将整数提高为负整数幂是NumPy中的错误.

It's important that something like numpy.array([2]) ** 2 give integer output, so that means that numpy.array([2]) ** -2 has to give integers or nothing. They picked nothing; raising integers to negative integer powers is an error in NumPy.

如果要浮点输出,请进行浮点输入:

If you want floating-point output, make floating-point input:

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

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


在上面的描述中,您可能不会想到NumPy的类型规则的一些奇怪方面.一种是对于涉及标量和数组的操作,标量的dtype 实际上可能被降级"了根据使用输入dtypes选择结果dtype之前的值:


There are a few weird aspects of NumPy's type rules you might not expect from the above description. One is that for operations involving both scalars and arrays, the scalar's dtype may actually be "demoted" based on its value before the input dtypes are used to choose the result dtype:

>>> (numpy.array([1], dtype='int8') + numpy.int32(1)).dtype
dtype('int8')
>>> (numpy.array([1], dtype='int8') + numpy.array([1], dtype='int32')).dtype
dtype('int32')

在这里,标量numpy.int32(1)被降级"到int8,但是数组没有降级. (实际上,降级不仅仅是降级为int8,尤其是对于有符号/无符号处理;请参阅

Here, the scalar numpy.int32(1) gets "demoted" to int8, but the arrays don't get demoted. (It's actually a bit more complex than just demoting to int8, particularly for signed/unsigned handling; see the implementation for the full details.)

第二,当涉及uint64s时,NumPy突然看起来可以接受负指数:

Second, when uint64s are involved, NumPy may suddenly appear to be okay with negative exponents:

>>> numpy.arange(5, dtype='uint64') ** -2
__main__:1: RuntimeWarning: divide by zero encountered in power
array([       inf, 1.        , 0.25      , 0.11111111, 0.0625    ])

这是因为NumPy无法找到足够大的整数dtype来容纳uint64值和负值,因此它放弃并强制将输入强制为浮点型.只要避免使用标量类型降级",对带符号的dtype的正指数也可以看到相同的结果:

This is because NumPy cannot find an integer dtype big enough for both uint64 values and negative values, so it gives up and coerces the inputs to floats. The same can be seen with a positive exponent of signed dtype, as long as you avoid scalar type "demotion":

>>> numpy.arange(5, dtype='uint64') ** numpy.array([2], dtype='int32')
array([ 0.,  1.,  4.,  9., 16.])

这篇关于为什么我不能在numpy中提升为负数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆