简体   繁体   English

阵列的差分步长

[英]Differential step size for array

I am working in python. 我在用python工作。 I have an angle quantity for which I want a varying step size for the array instead of a uniform grid that can be created like np.linspace(0, pi, 100) for 100 equal steps. 我有一个角度量,我希望数组的步长变化,而不是像np.linspace(0, pi, 100)一样创建np.linspace(0, pi, 100)个相等步长的均匀网格。 Instead, I want more 'resolution' (ie a smaller step-size) for values close to 0 and pi, with larger step sizes closer to pi/2 radians. 相反,对于接近0和pi的值,我希望有更多的“分辨率”(即较小的步长),而较大的步长则应接近pi / 2弧度。 Is there a simple way to implement this in python using a technique already provided in numpy or otherwise? 有没有简单的方法可以使用numpy或其他方式提供的技术在python中实现此功能?

Here's how to use np.r_ to construct a array with closer spacing at the ends, and wider in the middle: 这是使用np.r_构造一个数组的方法,该数组两端的间距np.r_ ,中间的宽度较大:

In [578]: x=np.r_[0:.09:10j, .1:.9:11j, .91:1:10j]
In [579]: x
Out[579]: 
array([ 0.  ,  0.01,  0.02,  0.03,  0.04,  0.05,  0.06,  0.07,  0.08,
        0.09,  0.1 ,  0.18,  0.26,  0.34,  0.42,  0.5 ,  0.58,  0.66,
        0.74,  0.82,  0.9 ,  0.91,  0.92,  0.93,  0.94,  0.95,  0.96,
        0.97,  0.98,  0.99,  1.  ])

then scale x with np.pi . 然后用np.pi缩放x

This is the kind of thing that np.r_ was created for. 这就是创建np.r_目的。 Not that it's doing anything special. 并不是说它在做任何特别的事情。 It's doing the same as: 它与以下操作相同:

np.concatenate([np.linspace(0,.09,10),
                np.linspace(.1,.9,11),
                np.linspace(.91,1,10)])

For a smoother gradation in spacing, I'd try mapping a single linspace with a curve. 为了使间距渐变更平滑,我尝试使用曲线绘制单个linspace

In [606]: x=np.arctan(np.linspace(-10,10,10))
In [607]: x -= x[0]
In [608]: x /= x[-1]
In [609]: x
Out[609]: 
array([ 0.        ,  0.00958491,  0.02665448,  0.06518406,  0.21519086,
        0.78480914,  0.93481594,  0.97334552,  0.99041509,  1.        ])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM