How do I do something like numpy's arange in Go?

huangapple go评论134阅读模式
英文:

How do I do something like numpy's arange in Go?

问题

Numpy的arange函数返回一个在给定区间内具有浮点步长的均匀间隔值的列表。在Go语言中是否有一种简洁的方法来创建类似的切片?

英文:

Numpy's arange function returns a list of evenly spaced values within a given interval with float steps. Is there a short and simple way to create a slice like that in Go?

答案1

得分: 4

根据val的解决方案,我建议避免使用"x+=step",因为根据限制和步长,舍入误差会累积,可能会导致最后一个值未定义,甚至更糟的是,尝试定义第N+1个值,从而引发恐慌。

可以通过以下方式解决:

  1. func arange2(start, stop, step float64) []float64 {
  2. N := int(math.Ceil((stop - start) / step))
  3. rnge := make([]float64, N)
  4. for x := range rnge {
  5. rnge[x] = start + step*float64(x)
  6. }
  7. return rnge
  8. }

你可以在这里查看示例代码:http://play.golang.org/p/U67abBaBbv

英文:

Based on val's solution, I would suggest to avoid using "x+=step" because depending on the limits and the step, rounding errors will accumulate and you may leave the last value undefined or even worse, try to define the N+1 value, causing a panic.

This could be solved with:
<pre><code>func arange2(start, stop, step float64) []float64 {
N := int(math.Ceil((stop - start) / step))
rnge := make([]float64, N)
for x := range rnge {
rnge[x] = start + step*float64(x)
}
return rnge
}</code></pre>

http://play.golang.org/p/U67abBaBbv

答案2

得分: 3

我了解的情况下没有现成的翻译。不过你可以自己定义一个函数,实现类似的功能。以下是一个示例代码:

  1. import "math"
  2. func arange(start, stop, step float64) []float64 {
  3. N := int(math.Ceil((stop - start) / step))
  4. rnge := make([]float64, N, N)
  5. i := 0
  6. for x := start; x < stop; x += step {
  7. rnge[i] = x
  8. i += 1
  9. }
  10. return rnge
  11. }

这个函数可以返回一个按照指定步长生成的浮点数数组。例如,调用arange(0., 10., 0.5)会返回[0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5 8 8.5 9 9.5]

对应的Python代码如下:

  1. >>> np.arange(0., 10., 0.5)
  2. array([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. ,
  3. 5.5, 6. , 6.5, 7. , 7.5, 8. , 8.5, 9. , 9.5])

需要注意的是,由于我简化了迭代的方式,我的Go版本中不能使用负步长。但是如果需要的话,你可以定义一个更复杂的函数来实现这个功能。

英文:

None that I am aware of. You can define your own though:

  1. import &quot;math&quot;
  2. func arange(start, stop, step float64) []float64 {
  3. N := int(math.Ceil((stop - start) / step));
  4. rnge := make([]float64, N, N)
  5. i := 0
  6. for x := start; x &lt; stop; x += step {
  7. rnge[i] = x;
  8. i += 1
  9. }
  10. return rnge
  11. }

which returns:

  1. arange(0., 10., 0.5)
  2. [0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5 8 8.5 9 9.5]

Corresponding to the Python:

  1. &gt;&gt;&gt; np.arange(0., 10., 0.5)
  2. array([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. ,
  3. 5.5, 6. , 6.5, 7. , 7.5, 8. , 8.5, 9. , 9.5])

Note that because of the way I defined the iteration, you cannot use negative steps in my simple Go version, but you can define something a bit more robust if you need to.

huangapple
  • 本文由 发表于 2013年11月11日 20:37:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/19906544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定