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

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

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个值,从而引发恐慌。

可以通过以下方式解决:

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
}

你可以在这里查看示例代码: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

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

import "math"

func arange(start, stop, step float64) []float64 {
    N := int(math.Ceil((stop - start) / step))
    rnge := make([]float64, N, N)
    i := 0
    for x := start; x < stop; x += step {
        rnge[i] = x
        i += 1
    }
    return rnge
}

这个函数可以返回一个按照指定步长生成的浮点数数组。例如,调用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代码如下:

>>> np.arange(0., 10., 0.5)
array([ 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])

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

英文:

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

import &quot;math&quot;

func arange(start, stop, step float64) []float64 {
    N := int(math.Ceil((stop - start) / step));
    rnge := make([]float64, N, N)
    i := 0
    for x := start; x &lt; stop; x += step {
        rnge[i] = x;
        i += 1
    }
    return rnge
}

which returns:

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]

Corresponding to the Python:

&gt;&gt;&gt; np.arange(0., 10., 0.5)
array([ 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])

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:

确定