gitee.com/quant1x/num@v0.3.2/dot.go (about) 1 package num 2 3 import ( 4 "fmt" 5 ) 6 7 func Dot1D[T Number](a, b []T) T { 8 return __dot1d_go(a, b) 9 } 10 11 func __dot1d_go[T Number](x, y []T) T { 12 res := T(0) 13 for i := 0; i < len(x); i++ { 14 res += x[i] * y[i] 15 } 16 return res 17 } 18 19 // 3 x 3 20 func __dot2d_go[T Number](a, b [][]T) [][]T { 21 A := a 22 B := b 23 rLen := len(B) 24 cLen := len(B[0]) 25 c := make([][]T, rLen) 26 for i := 0; i < rLen; i++ { 27 col := make([]T, cLen) 28 for j := 0; j < cLen; j++ { 29 for k := 0; k < rLen; k++ { 30 col[j] += A[i][k] * B[k][j] 31 } 32 } 33 c[i] = col 34 } 35 return c 36 } 37 38 func __align_go[T Number](x [][]T, n int) [][]T { 39 //if n < 3 { 40 // panic("lt 3") 41 //} 42 d := make([][]T, n) 43 rLen := len(x) 44 cLen := len(x[0]) 45 for i := 0; i < n; i++ { 46 col := make([]T, n) 47 for j := 0; j < n; j++ { 48 col[j] = x[rLen-n+i][cLen-n+j] 49 } 50 d[i] = col 51 } 52 fmt.Println(d) 53 return d 54 } 55 56 func Dot2D_V1[T Number](a, b [][]T) [][]T { 57 W := 3 58 A := __align_go(a, W) 59 B := __align_go(b, W) 60 61 return __dot2d_go(A, B) 62 } 63 64 // Dot2D 二维矩阵点积 65 // 66 // 点积(dot)运算及简单应用 https://www.jianshu.com/p/482abac8798c 67 func Dot2D[T Number](a, b [][]T) [][]T { 68 A := a 69 B := b 70 rLen := len(A[0]) 71 cLen := len(B[0]) 72 xLen := min(rLen, cLen) 73 x := make([][]T, xLen) 74 // 行 75 for i := 0; i < xLen; i++ { 76 col := make([]T, cLen) 77 // 列 78 for j := 0; j < cLen; j++ { 79 for k := 0; k < rLen; k++ { 80 col[j] += A[i][k] * B[k][j] 81 } 82 } 83 x[i] = col 84 } 85 return x 86 } 87 88 // Dot 二维点积 89 func Dot[T Number](a, b [][]T) [][]T { 90 m, n := Shape[T](a) 91 k, l := Shape[T](b) 92 if n != k { 93 panic("dot 2d a.rows<>b.cols") 94 } 95 //fmt.Println("m, n:", m, n) 96 //fmt.Println("k, l:", k, l) 97 x := make([][]T, m) 98 // 行 99 for i := 0; i < m; i++ { 100 col := make([]T, l) 101 // 列 102 for c := 0; c < l; c++ { 103 for r := 0; r < k; r++ { 104 col[c] += a[i][r] * b[r][c] 105 } 106 } 107 x[i] = col 108 } 109 return x 110 } 111 112 func Dot_v1[T Number](a, b [][]T) [][]T { 113 m, n := Shape[T](a) 114 k, l := Shape[T](b) 115 if n != k { 116 panic("dot 2d vs 1d a.rows<>b.cols") 117 } 118 //fmt.Println("m, n:", m, n) 119 //fmt.Println("k, l:", k, l) 120 x := make([][]T, m) 121 // 行 122 for i := 0; i < m; i++ { 123 col := make([]T, l) 124 // 列 125 for c := 0; c < l; c++ { 126 for r := 0; r < k; r++ { 127 col[c] += a[i][r] * b[r][c] 128 } 129 } 130 x[i] = col 131 } 132 return x 133 } 134 135 // Dot2D1 二维矩阵和一维矩阵计算点积 136 func Dot2D1[T Number](a [][]T, b []T) []T { 137 B := [][]T{b} 138 b1 := Transpose2D(B) 139 x1 := Dot[T](a, b1) 140 x2 := Transpose2D(x1) 141 return x2[0] 142 } 143 144 func Dot2D1_v2[T Number](a [][]T, b []T) []T { 145 m, n := Shape[T](a) 146 k, l := Shape[T](b) 147 if l < 1 { 148 l = 1 149 } 150 151 fmt.Println("m, n:", m, n) 152 fmt.Println("k, l:", k, l) 153 x := make([]T, m) 154 // 行 155 for i := 0; i < m; i++ { 156 col := T(0) 157 // 列 158 for c := 0; c < l; c++ { 159 for r := 0; r < k; r++ { 160 col += a[i][r] * b[r] 161 } 162 } 163 x[i] = col 164 } 165 return x 166 }