-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdot.go
More file actions
166 lines (153 loc) · 2.73 KB
/
dot.go
File metadata and controls
166 lines (153 loc) · 2.73 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package num
import (
"fmt"
)
func Dot1D[T Number](a, b []T) T {
return __dot1d_go(a, b)
}
func __dot1d_go[T Number](x, y []T) T {
res := T(0)
for i := 0; i < len(x); i++ {
res += x[i] * y[i]
}
return res
}
// 3 x 3
func __dot2d_go[T Number](a, b [][]T) [][]T {
A := a
B := b
rLen := len(B)
cLen := len(B[0])
c := make([][]T, rLen)
for i := 0; i < rLen; i++ {
col := make([]T, cLen)
for j := 0; j < cLen; j++ {
for k := 0; k < rLen; k++ {
col[j] += A[i][k] * B[k][j]
}
}
c[i] = col
}
return c
}
func __align_go[T Number](x [][]T, n int) [][]T {
//if n < 3 {
// panic("lt 3")
//}
d := make([][]T, n)
rLen := len(x)
cLen := len(x[0])
for i := 0; i < n; i++ {
col := make([]T, n)
for j := 0; j < n; j++ {
col[j] = x[rLen-n+i][cLen-n+j]
}
d[i] = col
}
fmt.Println(d)
return d
}
func Dot2D_V1[T Number](a, b [][]T) [][]T {
W := 3
A := __align_go(a, W)
B := __align_go(b, W)
return __dot2d_go(A, B)
}
// Dot2D 二维矩阵点积
//
// 点积(dot)运算及简单应用 https://www.jianshu.com/p/482abac8798c
func Dot2D[T Number](a, b [][]T) [][]T {
A := a
B := b
rLen := len(A[0])
cLen := len(B[0])
xLen := min(rLen, cLen)
x := make([][]T, xLen)
// 行
for i := 0; i < xLen; i++ {
col := make([]T, cLen)
// 列
for j := 0; j < cLen; j++ {
for k := 0; k < rLen; k++ {
col[j] += A[i][k] * B[k][j]
}
}
x[i] = col
}
return x
}
// Dot 二维点积
func Dot[T Number](a, b [][]T) [][]T {
m, n := Shape[T](a)
k, l := Shape[T](b)
if n != k {
panic("dot 2d a.rows<>b.cols")
}
//fmt.Println("m, n:", m, n)
//fmt.Println("k, l:", k, l)
x := make([][]T, m)
// 行
for i := 0; i < m; i++ {
col := make([]T, l)
// 列
for c := 0; c < l; c++ {
for r := 0; r < k; r++ {
col[c] += a[i][r] * b[r][c]
}
}
x[i] = col
}
return x
}
func Dot_v1[T Number](a, b [][]T) [][]T {
m, n := Shape[T](a)
k, l := Shape[T](b)
if n != k {
panic("dot 2d vs 1d a.rows<>b.cols")
}
//fmt.Println("m, n:", m, n)
//fmt.Println("k, l:", k, l)
x := make([][]T, m)
// 行
for i := 0; i < m; i++ {
col := make([]T, l)
// 列
for c := 0; c < l; c++ {
for r := 0; r < k; r++ {
col[c] += a[i][r] * b[r][c]
}
}
x[i] = col
}
return x
}
// Dot2D1 二维矩阵和一维矩阵计算点积
func Dot2D1[T Number](a [][]T, b []T) []T {
B := [][]T{b}
b1 := Transpose2D(B)
x1 := Dot[T](a, b1)
x2 := Transpose2D(x1)
return x2[0]
}
func Dot2D1_v2[T Number](a [][]T, b []T) []T {
m, n := Shape[T](a)
k, l := Shape[T](b)
if l < 1 {
l = 1
}
fmt.Println("m, n:", m, n)
fmt.Println("k, l:", k, l)
x := make([]T, m)
// 行
for i := 0; i < m; i++ {
col := T(0)
// 列
for c := 0; c < l; c++ {
for r := 0; r < k; r++ {
col += a[i][r] * b[r]
}
}
x[i] = col
}
return x
}