-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (60 loc) · 1.54 KB
/
main.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
66
67
68
69
70
71
72
73
74
// Package linear provides a linear-algebra toolbox.
package linear
// TensorFloat64 computes the tensor product of a number of vectors.
func TensorFloat64(vectors ...[]float64) []float64 {
nd := len(vectors)
dimensions := make([]int, nd)
for i := 0; i < nd; i++ {
dimensions[i] = len(vectors[i])
}
ascend, descend := prepareTensor(dimensions)
np := dimensions[0] * descend[0]
tensor := make([]float64, np*nd)
for i := 0; i < nd; i++ {
for j, z := 0, i; j < descend[i]; j++ {
for k := 0; k < dimensions[i]; k++ {
for l := 0; l < ascend[i]; l++ {
tensor[z] = vectors[i][k]
z += nd
}
}
}
}
return tensor
}
// TensorUint64 computes the tensor product of a number of vectors.
func TensorUint64(vectors ...[]uint64) []uint64 {
nd := len(vectors)
dimensions := make([]int, nd)
for i := 0; i < nd; i++ {
dimensions[i] = len(vectors[i])
}
ascend, descend := prepareTensor(dimensions)
np := dimensions[0] * descend[0]
tensor := make([]uint64, np*nd)
for i := 0; i < nd; i++ {
for j, z := 0, i; j < descend[i]; j++ {
for k := 0; k < dimensions[i]; k++ {
for l := 0; l < ascend[i]; l++ {
tensor[z] = vectors[i][k]
z += nd
}
}
}
}
return tensor
}
func prepareTensor(dimensions []int) ([]int, []int) {
nd := len(dimensions)
ascend := make([]int, nd)
ascend[0] = 1
for i := 1; i < nd; i++ {
ascend[i] = dimensions[i-1] * ascend[i-1]
}
descend := make([]int, nd)
descend[nd-1] = 1
for i := nd - 2; i >= 0; i-- {
descend[i] = dimensions[i+1] * descend[i+1]
}
return ascend, descend
}