/
normalize.go
61 lines (52 loc) · 1.11 KB
/
normalize.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
package main
import (
"math"
"github.com/mumax/3/data"
)
// normalize vector data to given length
func normalize(f *data.Slice, length float64) {
a := f.Vectors()
for i := range a[0] {
for j := range a[0][i] {
for k := range a[0][i][j] {
x, y, z := a[0][i][j][k], a[1][i][j][k], a[2][i][j][k]
norm := math.Sqrt(float64(x*x + y*y + z*z))
invnorm := float32(1)
if norm != 0 {
invnorm = float32(length / norm)
}
a[0][i][j][k] *= invnorm
a[1][i][j][k] *= invnorm
a[2][i][j][k] *= invnorm
}
}
}
}
func normpeak(f *data.Slice) {
a := f.Vectors()
maxnorm := 0.
for i := range a[0] {
for j := range a[0][i] {
for k := range a[0][i][j] {
x, y, z := a[0][i][j][k], a[1][i][j][k], a[2][i][j][k]
norm := math.Sqrt(float64(x*x + y*y + z*z))
if norm > maxnorm {
maxnorm = norm
}
}
}
}
scale(f, float32(1/maxnorm))
}
func scale(f *data.Slice, factor float32) {
a := f.Vectors()
for i := range a[0] {
for j := range a[0][i] {
for k := range a[0][i][j] {
a[0][i][j][k] *= factor
a[1][i][j][k] *= factor
a[2][i][j][k] *= factor
}
}
}
}