gitee.com/quant1x/num@v0.3.2/range.go (about)

     1  package num
     2  
     3  import (
     4  	"gitee.com/quant1x/num/x32"
     5  	"gitee.com/quant1x/num/x64"
     6  )
     7  
     8  // Arange Return evenly spaced values within a given interval.
     9  //
    10  //	返回给定间隔内的等间距值
    11  func Arange[T Number](start T, end T, argv ...T) []T {
    12  	step := T(1)
    13  	if len(argv) > 0 {
    14  		step = argv[0]
    15  	}
    16  	x := make([]T, 0)
    17  	for i := start; i < end; i += step {
    18  		x = append(x, start)
    19  		start += T(step)
    20  	}
    21  
    22  	return x
    23  }
    24  
    25  // Range 产生从0到n-1的数组
    26  func Range[E Number](n int) []E {
    27  	var dest any
    28  	var start E = 0
    29  	switch v := any(start).(type) {
    30  	case float32:
    31  		dest = x32.Range(v, v+float32(n))
    32  	case float64:
    33  		dest = x64.Range(v, v+float64(n))
    34  	default:
    35  		// 其它类型
    36  		d := make([]E, n)
    37  		for i := 0; i < n; i++ {
    38  			d[i] = start
    39  			start += 1
    40  		}
    41  		dest = d
    42  	}
    43  	return dest.([]E)
    44  }