gitee.com/quant1x/num@v0.3.2/argmin.go (about) 1 package num 2 3 import ( 4 "gitee.com/quant1x/num/x32" 5 "gitee.com/quant1x/num/x64" 6 ) 7 8 // ArgMin Returns the indices of the minimum values along an axis. 9 // 10 // 返回轴上最小值的索引 11 func ArgMin[T Number](x []T) int { 12 ret := UnaryOperations2[T, int](x, x32.ArgMin, x64.ArgMin, __go_arg_min[T]) 13 return ret 14 } 15 16 func ArgMin2[T BaseType](x []T) int { 17 var d int 18 switch vs := any(x).(type) { 19 case []float32: 20 d = ArgMin(vs) 21 case []float64: 22 d = ArgMin(vs) 23 case []int: 24 d = ArgMin(vs) 25 case []int8: 26 d = ArgMin(vs) 27 case []int16: 28 d = ArgMin(vs) 29 case []int32: 30 d = ArgMin(vs) 31 case []int64: 32 d = ArgMin(vs) 33 case []uint: 34 d = ArgMin(vs) 35 case []uint8: 36 d = ArgMin(vs) 37 case []uint16: 38 d = ArgMin(vs) 39 case []uint32: 40 d = ArgMin(vs) 41 case []uint64: 42 d = ArgMin(vs) 43 case []uintptr: 44 d = ArgMin(vs) 45 case []string: 46 d = __go_arg_min(vs) 47 case []bool: 48 d = __go_arg_min_bool(vs) 49 default: 50 // 其它类型原样返回 51 panic(TypeError(any(x))) 52 } 53 54 return d 55 } 56 57 func __go_arg_min[T Ordered](x []T) int { 58 minValue := x[0] 59 idx := 0 60 for i, v := range x[1:] { 61 if v < minValue { 62 minValue = v 63 idx = 1 + i 64 } 65 } 66 return idx 67 } 68 69 func __go_arg_min_bool(x []bool) int { 70 minValue := BoolToInt(x[0]) 71 idx := 0 72 for i, v := range x[1:] { 73 if BoolToInt(v) < minValue { 74 minValue = BoolToInt(v) 75 idx = 1 + i 76 } 77 } 78 return idx 79 }