-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrocketMatrix.go
More file actions
176 lines (159 loc) · 4.06 KB
/
rocketMatrix.go
File metadata and controls
176 lines (159 loc) · 4.06 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
167
168
169
170
171
172
173
174
175
176
package rocketc
// Matrix : Basic numerical container (2D array), core datatype.
type Matrix [][]float32
// Init : Initializes a Matrix to 1 X 1 Matrix containing zero.
func (m *Matrix) Init() {
*m = make(Matrix, 1)
(*m)[0] = make([]float32, 1)
}
// Rows : Returns number of rows in Matrix.
func (m Matrix) Rows() int {
return len(m)
}
// Cols : Returns number of columns in Matrix.
func (m Matrix) Cols() int {
if len(m) != 0 {
return len(m[0])
}
return 0
}
// Shape : Returns a int array []int containing the dimensions of Matrix.
func (m Matrix) Shape() [2]int {
var s [2]int
s[0] = m.Rows()
s[1] = m.Cols()
return s
}
// MakeMatrixUniform : Makes Matrix rows of same size,
// by filling rest of the row with zero value of float32
// if different size rows are present in Matrix.
func (m *Matrix) MakeMatrixUniform() {
nRows := m.Rows()
sizeMaxRows := 0
for i := 0; i < nRows; i++ {
if len((*m)[i]) > sizeMaxRows {
sizeMaxRows = len((*m)[i])
}
}
for i := 0; i < nRows; i++ {
if len((*m)[i]) < sizeMaxRows {
temp := make([]float32, sizeMaxRows-len((*m)[i]))
(*m)[i] = append((*m)[i], temp...)
}
}
}
// Fill : fills the Matrix with a given number i.
func (m *Matrix) Fill(i float32) {
for row := range *m {
for col := range (*m)[row] {
(*m)[row][col] = i
}
}
}
// Transpose : Transpose the Matrix, takes a boolean inplace if True
// transposes Matrix inplace.
func (m *Matrix) Transpose(inplace bool) Matrix {
var s = Zeros(m.Cols(), m.Rows())
for row := range *m {
for col := range (*m)[row] {
s[col][row] = (*m)[row][col]
}
}
if inplace {
*m = s
}
return s
}
// Ones : Fills the Matrix with all zeros.
func (m *Matrix) Ones() {
(*m).Fill(1)
}
// Zeros : Fills the Matrix with all ones.
func (m *Matrix) Zeros() {
(*m).Fill(0)
}
// Add : Adds a given number to every element of Matrix, takes a boolean inplace if True
// performs addition inplace.
func (m *Matrix) Add(i float32, inplace bool) Matrix {
var s = Zeros(m.Rows(), m.Cols())
s.Fill(i)
s = AddElementwise(s, *m)
if inplace {
*m = s
}
return s
}
// Sub : Subtracts a given number from every element of Matrix, takes a boolean inplace if True
// performs subtraction inplace.
func (m *Matrix) Sub(i float32, inplace bool) Matrix {
var s = Zeros((*m).Rows(), (*m).Cols())
s.Fill(i)
s = AddElementwise(s, *m)
if inplace {
*m = s
}
return s
}
// Mul : Multiplies i to every element of the Matrix, takes a boolean inplace if True
// performs multiplication inplace.
func (m *Matrix) Mul(i float32, inplace bool) Matrix {
var s = Zeros((*m).Rows(), (*m).Cols())
s.Fill(i)
s = MulElementwise(*m, s)
if inplace {
*m = s
}
return s
}
// Div : Divides every element of Matrix by i, takes a boolean inplace if True
// performs division inplace.
func (m *Matrix) Div(i float32, inplace bool) Matrix {
var s = Zeros((*m).Rows(), (*m).Cols())
s.Fill(i)
s = DivElementwise(*m, s)
if inplace {
*m = s
}
return s
}
// ReciproElementwise : Elementwise reciprocal of Matrix elements, takes a boolean inplace if True
// performs reciprocal inplace.
func (m *Matrix) ReciproElementwise(inplace bool) Matrix {
var s = Zeros((*m).Rows(), (*m).Cols())
for row := range *m {
for col := range (*m)[row] {
s[row][col] = 1 / (*m)[row][col]
}
}
if inplace {
*m = s
}
return s
}
// Map : Applies given function to every element of Matrix,
// takes a boolean inplace if True performs mapping inplace.
func (m *Matrix) Map(f func(v float32) float32, inplace bool) Matrix {
var temp = Zeros(m.Rows(), m.Cols())
for i := 0; i < len(temp); i++ {
for j := 0; j < len(temp[i]); j++ {
temp[i][j] = f((*m)[i][j])
}
}
if inplace {
*m = temp
}
return temp
}
// Filter : Takes a function f as argument which in turn takes a float32
// and returns bool. Filter function applies f to every element in m and
// returns the result as a boolean matrix.
func (m Matrix) Filter(f func(v float32) bool) [][]bool {
result := make([][]bool, m.Rows())
for i := 0; i < m.Rows(); i++ {
result[i] = make([]bool, len(m[i]))
for j := 0; j < len(m[i]); j++ {
result[i][j] = f(m[i][j])
}
}
return result
}