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