范围生成器`r_`-具有复杂(但不是虚构)步骤的切片;使用幅度 [英] range builder `r_` - slice with complex (but not imaginary) step; magnitude is used

查看:78
本文介绍了范围生成器`r_`-具有复杂(但不是虚构)步骤的切片;使用幅度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用NumPy串联和范围构建对象r_时,我偶然发现了以下行为:显然,无论是实数,虚数还是适当的复数,复杂步骤的绝对值都作为一样的方式.

Playing with the NumPy concatenation and range building object r_ I stumbled over the following behavior: apparently, a complex step no matter whether real, imaginary or proper complex has its absolute value taken as the number of steps in a linspace like way.

>>> import numpy as np
>>> 
>>> np.r_[0:12:4]           # start : stop : step
array([0, 4, 8])            # that's expected
>>> np.r_[0:12:4j]          # start : stop : imaginary step
array([ 0.,  4.,  8., 12.]) # that's in the docs
>>> np.r_[0:12:4+0j]        # real step of complex type ?
array([ 0.,  4.,  8., 12.]) # this is not as far as I can tell
# you can even do stuff like
>>> np.r_[0:12:-4+3j]        # proper complex step ?
array([ 0.,  3.,  6.,  9., 12.])

问题:我只是想知道这是否是一项正式功能,因为我找不到它的记载.

Question: I just wanted to know whether that's an official feature, because I couldn't find it documented.

为什么如此重要?好吧,r_主要是为了节省按键,在某些情况下此功能可以为您节省一些字符.

Why is it relevant? Well, r_ primarily being a keystroke saving convenience there are a few cases where this feature could save you a few characters.

推荐答案

代码确实采用了绝对值:

if isinstance(step, complex):
    size.append(int(abs(step)))

但这不是有据可查的保证. 文档仅保证虚数的行为,而不能保证虚数的行为.任意复数:

but this is not a documented guarantee. The docs only guarantee behavior for imaginary numbers, not arbitrary complex numbers:

如果step是一个虚数(即100j),则将其整数部分解释为所需的点数,并且开始和结束都包括在内

if step is an imaginary number (i.e. 100j) then its integer portion is interpreted as a number-of-points desired and the start and stop are inclusive

您不应该依赖行为来进行非纯想象的复杂输入,因为这不是有据可查的保证.

You should not rely on the behavior for not-purely-imaginary complex inputs, as it is not a documented guarantee.

也就是说,这可能是一种保证.我能够追溯到numpy.r_的代码的最远的地方是此提交. (那不是它的起源-我可以找到对scipy.r_的引用,可以追溯到一样,是正确的提交,只是GitHub似乎只有一个原始的非Git提交.)

That said, it is possible that it was intended to be a guarantee. The furthest back I've been able to trace numpy.r_'s code is this commit. (That's not where it originated - I can find references to scipy.r_ dating back even further - but despite finding references to scipy.r_, I have not been able to locate the code for scipy.r_, and I suspect neither the SciPy nor NumPy GitHub repositories contain the original code. It seems like this would be the right commit, except that GitHub seems to only have a fragment of the original non-Git commit.)

r_,但是在该提交中也存在mgrid,并且在该提交中记录了mgrid对于复数的类似行为

r_ was not documented in the earliest commit I can trace it to, but mgrid was also present in that commit, and mgrid's similar behavior for complex numbers was documented in that commit as

However, if the step length is a COMPLEX NUMBER (e.g. 5j), then the integer
part of it's magnitude is interpreted as specifying the number of points to
create between the start and stop values, where the stop value
IS INCLUSIVE.

我能够追溯到numpy.r_文档的最远的是numpy.r_作者的初衷.

The furthest I've been able to trace numpy.r_'s documentation is this commit 7 years later, labelled "Merge from doc wiki". I believe the doc wiki is now gone, and I have been unable to determine who originally contributed those docs, but it seems likely that those docs were not based on the original intent of numpy.r_'s author.

这篇关于范围生成器`r_`-具有复杂(但不是虚构)步骤的切片;使用幅度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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