-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgo.go
65 lines (55 loc) · 1.12 KB
/
go.go
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
// +build !avx,!sse
package vecf32
import "github.com/chewxy/math32"
// Add performs a̅ + b̅. a̅ will be clobbered
func Add(a, b []float32) {
b = b[:len(a)]
for i, v := range a {
a[i] = v + b[i]
}
}
// Sub performs a̅ - b̅. a̅ will be clobbered
func Sub(a, b []float32) {
b = b[:len(a)]
for i, v := range a {
a[i] = v - b[i]
}
}
// Mul performs a̅ × b̅. a̅ will be clobbered
func Mul(a, b []float32) {
b = b[:len(a)]
for i, v := range a {
a[i] = v * b[i]
}
}
// Div performs a̅ ÷ b̅. a̅ will be clobbered
func Div(a, b []float32) {
b = b[:len(a)]
for i, v := range a {
if b[i] == 0 {
a[i] = math32.Inf(0)
continue
}
a[i] = v / b[i]
}
}
// Sqrt performs √a̅ elementwise. a̅ will be clobbered
func Sqrt(a []float32) {
for i, v := range a {
a[i] = math32.Sqrt(v)
}
}
// InvSqrt performs 1/√a̅ elementwise. a̅ will be clobbered
func InvSqrt(a []float32) {
for i, v := range a {
a[i] = float32(1) / math32.Sqrt(v)
}
}
// Sum sums a slice of float32 and returns a float32
func Sum(a []float32) float32 {
var retVal float32
for _, v := range a {
retVal += v
}
return retVal
}