使用 NumPy RClass 中的步长解释复数(`np.r_`)

2024-04-20 01:25:03 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是一段代码:

np.concatenate(([3], [0]*5, np.arange(-1, 1.002, 2/9.0)))

# the above outputs 
array([ 3.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        , -1.        , -0.77777778, -0.55555556, -0.33333333,
       -0.11111111,  0.11111111,  0.33333333,  0.55555556,  0.77777778,
        1.        ])

虽然这是冗长的,它是相当可以理解的。这里有另一种方法来获得相同的输出,使用(ab)表示法,用复数作为步长。你知道吗

np.r_[3, [0]*5, -1:1:10j]

# the above outputs
array([ 3.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        , -1.        , -0.77777778, -0.55555556, -0.33333333,
       -0.11111111,  0.11111111,  0.33333333,  0.55555556,  0.77777778,
        1.        ])

我试图理解第一种方法中的步长是如何与第二种方法中的复数步长(10j)等价的。你知道吗

2/9.0  == 10j  # how?

我在^{}中读到,-1:1:10j意味着我们要在-1:1之间产生10个值,包括两边。但是,那10j怎么翻译成0.2222?你知道吗

  • 有什么直观的想法或解释吗?你知道吗
  • 另外,我们还可以用这种表达式做哪些有用的NumPy示例?你知道吗

另外,我已经看过range-builder-r-slice-with-complex-but-not-imaginary-step-magnitude了,但这并不能提供太多的想法。你知道吗


Tags: the方法代码abnpoutputsarrayabove
2条回答

文档的相关部分是:

However, 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. In other words start:stop:stepj is interpreted as np.linspace(start, stop, step, endpoint=1) inside of the brackets.

这是一个在numpy/lib/index_tricks.pymgrid是另一个)中的几个类中使用的符号技巧。这不是一般的numpy或python技巧。使用class定义(不是函数)和自定义__getitem__方法是关键。你知道吗

至于数字细节,请检查代码中的np.linspace。(MATLAB有一个同名函数)。你知道吗

也许这种与arange的比较会给人一种直观的感觉。你知道吗

In [65]: np.arange(-1,1.01,.2)                                                  
Out[65]: 
array([-1.00000000e+00, -8.00000000e-01, -6.00000000e-01, -4.00000000e-01,
       -2.00000000e-01, -2.22044605e-16,  2.00000000e-01,  4.00000000e-01,
        6.00000000e-01,  8.00000000e-01,  1.00000000e+00])
In [66]: _.shape                                                                
Out[66]: (11,)
In [67]: np.linspace(-1,1,11)                                                   
Out[67]: array([-1. , -0.8, -0.6, -0.4, -0.2,  0. ,  0.2,  0.4,  0.6,  0.8,  1. ])

arange产生了11个值,因此我们必须在linspace中使用相同的大小。注意,linspace对端点的处理更好,从而更清晰地显示浮点值(这两种情况都不精确)。你知道吗

如果改用10,则间距正确(.2222…*9=1.9999…)。要得到10个值,我们必须步进9次。或者把范围分成9个区间。你知道吗

In [68]: np.linspace(-1,1,10)                                                   
Out[68]: 
array([-1.        , -0.77777778, -0.55555556, -0.33333333, -0.11111111,
        0.11111111,  0.33333333,  0.55555556,  0.77777778,  1.        ])

arange带浮点数比以整数开头的等价值更混乱:

In [70]: np.arange(-10,11,2)/10                                                 
Out[70]: array([-1. , -0.8, -0.6, -0.4, -0.2,  0. ,  0.2,  0.4,  0.6,  0.8,  1. ])

当您有startstop(两者都包括在内)和size时,step的计算如下:

step = (stop - start) / (size - 1)

使用start = -1stop = 1size = 2您将获得step = 2和数组[-1, 1]

使用start = -1stop = 1size = 3您将获得step = 1和数组[-1, 0, 1]

使用切片start = -1stop = 1size = 10中的-1:1:10j可以得到step = 2/9和数组

[-1.        , -0.77777778, -0.55555556, -0.33333333, -0.11111111,
  0.11111111,  0.33333333,  0.55555556,  0.77777778,  1.        ]

:对于复数,如10j,np.abs公司(10j)将首先计算得到一个实数。你知道吗

相关问题 更多 >