-
Notifications
You must be signed in to change notification settings - Fork 0
/
rocketMatrixFunctions.go
295 lines (276 loc) · 6.48 KB
/
rocketMatrixFunctions.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package rocketc
import (
"fmt"
"math/rand"
"time"
)
// PrintMatrix : Prints Matrix in a pretty manner.
func PrintMatrix(m ...Matrix) {
lambda := func(m Matrix) {
for i := 0; i < len(m[0]); i++ {
fmt.Printf("%10d ", i)
}
fmt.Println()
for i := 0; i < len(m[0])*6; i++ {
fmt.Printf("--")
}
fmt.Println()
for row := range m {
fmt.Printf("%3d |", row)
for col := range m[row] {
fmt.Printf("%10.6f ", m[row][col])
}
fmt.Println()
}
}
for _, value := range m {
lambda(value)
fmt.Println()
}
}
// CopyMatrix : Returns the copy of src Matrix.
func CopyMatrix(src Matrix) Matrix {
m := Zeros(src.Rows(), src.Cols())
copy(m, src)
return m
}
// DimensionEqual : Checks if m1 and m2 has same dimension, returns boolean.
func DimensionEqual(m1, m2 Matrix) bool {
if (m1.Rows() == m2.Rows()) && (m1.Cols() == m2.Cols()) {
return true
}
return false
}
// AddElementwise : Adds two Matrix m1 and m2.
func AddElementwise(m1, m2 Matrix) Matrix {
if !DimensionEqual(m1, m2) {
panic("Dimensions are not equal.")
}
var s = Zeros(m1.Rows(), m1.Cols())
for row := range s {
for col := range s[row] {
s[row][col] = m1[row][col] + m2[row][col]
}
}
return s
}
// SubElementwise : Subtracts two Matrix m1 and m2.
func SubElementwise(m1, m2 Matrix) Matrix {
if !DimensionEqual(m1, m2) {
panic("Dimensions are not equal.")
}
var s = Zeros(m1.Rows(), m1.Cols())
for row := range s {
for col := range s[row] {
s[row][col] = m1[row][col] - m2[row][col]
}
}
return s
}
// MulElementwise : Elementwise multiplication of Matrix m1 and m2.
func MulElementwise(m1, m2 Matrix) Matrix {
if !DimensionEqual(m1, m2) {
panic("Dimensions are not equal.")
}
var s = Zeros(m1.Rows(), m2.Cols())
for row := 0; row < m1.Rows(); row++ {
for col := 0; col < m2.Cols(); col++ {
s[row][col] = m2[row][col] * m1[row][col]
}
}
return s
}
// DivElementwise : Elementwise division of Matric m1 and m2.
func DivElementwise(m1, m2 Matrix) Matrix {
var m = m2.ReciproElementwise(false)
return MulElementwise(m1, m)
}
// Multiply : Returns the result of Matrix Multiplication of m1 and m2.
func Multiply(m1, m2 Matrix) Matrix {
isPossible := func(m1, m2 Matrix) bool {
if m1.Cols() == m2.Rows() {
return true
}
return false
}
if !isPossible(m1, m2) {
panic("Dimension Mismatch for matrix multiplication.")
}
var s = Zeros(m1.Rows(), m2.Cols())
for row, value := range s {
var sum float32
for col := range value {
for i := 0; i < m1.Cols(); i++ {
sum += m1[row][i] * m2[i][col]
}
s[row][col] = sum
sum = 0
}
}
return s
}
// Zeros : Returns a Matrix of zeros of given rows and cols.
func Zeros(rows, cols int) Matrix {
var m = make(Matrix, rows)
for index := range m {
m[index] = make([]float32, cols)
}
return m
}
// Ones : Returns a Matrix of ones of given rows and cols.
func Ones(rows, cols int) Matrix {
var m = Zeros(rows, cols)
m.Fill(1)
return m
}
// Sum : Returns a Matrix containing sum of elements of Matrix m according to given axis.
// If axis = 0, along the rows and if axis = 1, along the columns.
func Sum(m Matrix, axis int) Matrix {
if axis > 1 || axis < 0 {
panic("Error in Axis")
}
if axis == 1 {
var s = Zeros(1, m.Cols())
for i := 0; i < len(s[0]); i++ {
for _, value := range m {
s[0][i] += value[i]
}
}
return s
}
var s = Zeros(m.Rows(), 1)
lambda := func(l []float32) float32 {
var s float32
for _, value := range l {
s += value
}
return s
}
for index, value := range m {
s[index][0] = lambda(value)
}
return s
}
// Random : Generates and returns a Matrix of given rows and cols
// containing random numbers.
func Random(rows, cols int) Matrix {
if (rows <= 0) || (cols <= 0) {
return nil
}
rand.Seed(time.Now().UnixNano())
var s = Zeros(rows, cols)
for row, value := range s {
for col := range value {
s[row][col] = rand.Float32()
}
}
return s
}
// VStack : Returns a Matrix which is constructed by vertically stacking
// every Matrix passed in argument. Takes variadic number of arguments i.e Matrix.
func VStack(m ...Matrix) Matrix {
var s Matrix
for _, value := range m {
for _, row := range value {
s = append(s, row)
}
}
return s
}
// HStack : Returns a Matrix which is constructed by horizontally stacking
// every Matrix passed in argument. Takes variadic number of arguments i.e Matrix.
func HStack(m ...Matrix) Matrix {
var s = make(Matrix, m[0].Rows())
for _, value := range m {
for index, row := range value {
s[index] = append(s[index], row...)
}
}
return s
}
// Max : Returns a Matrix containing the maximum of elements of Matrix m according to given axis.
// If axis = 0, along the rows and if axis = 1, along the columns.
func Max(m Matrix, axis int) Matrix {
if axis > 1 || axis < 0 {
panic("Error in Axis")
}
if axis == 1 {
var s = Zeros(1, m.Cols())
for row, value := range m {
for col := range value {
if m[row][col] > s[0][col] {
s[0][col] = m[row][col]
}
}
}
return s
}
var s = Zeros(m.Rows(), 1)
for row, value := range m {
for col := range value {
if m[row][col] > s[row][0] {
s[row][0] = m[row][col]
}
}
}
return s
}
// Min : Returns a Matrix containing the minimum of elements of Matrix m according to given axis.
// If axis = 0, along the rows and if axis = 1, along the columns.
func Min(m Matrix, axis int) Matrix {
minimum := func(m []float32) float32 {
min := m[0]
for _, value := range m {
if value < min {
min = value
}
}
return min
}
if axis == 1 {
var s = Zeros(1, m.Cols())
s[0] = m[0]
for row, value := range m {
for col := range value {
if m[row][col] < s[0][col] {
s[0][col] = m[row][col]
}
}
}
return s
}
var s = Zeros(m.Rows(), 1)
for row, value := range m {
s[row][0] = minimum(value)
}
return s
}
// Mean : Returns a Matrix containing the mean of elements of Matrix m according to given axis.
// If axis = 0, along the rows and if axis = 1, along the columns.
func Mean(m Matrix, axis int) Matrix {
if axis == 0 {
var l = m.Cols()
var s = Sum(m, axis)
for col := range s {
s[col][0] /= float32(l)
}
return s
}
var l = m.Rows()
var s = Sum(m, 1)
for col := range s[0] {
s[0][col] /= float32(l)
}
return s
}
// GetColumnsMatrix : Returns a Matrix only containing columns whose index is passed.
// columns are 0 indexed. Takes variadic arguments i.e index.
func GetColumnsMatrix(m Matrix, i ...int) Matrix {
var c = Zeros(m.Rows(), len(i))
for row, value := range m {
for index, v := range i {
c[row][index] = value[v]
}
}
return c
}