-
Notifications
You must be signed in to change notification settings - Fork 14
/
denseBinaryMatrix.go
310 lines (259 loc) · 6.55 KB
/
denseBinaryMatrix.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package htm
import (
"bytes"
"github.com/nupic-community/htm/utils"
//"math"
)
//Sparse binary matrix stores indexes of non-zero entries in matrix
//to conserve space
type DenseBinaryMatrix struct {
Width int
Height int
entries []bool
}
//Create new sparse binary matrix of specified size
func NewDenseBinaryMatrix(height, width int) *DenseBinaryMatrix {
m := &DenseBinaryMatrix{}
m.Height = height
m.Width = width
m.entries = make([]bool, width*height)
return m
}
//Create sparse binary matrix from specified dense matrix
func NewDenseBinaryMatrixFromDense(values [][]bool) *DenseBinaryMatrix {
if len(values) < 1 {
panic("No values specified.")
}
m := NewDenseBinaryMatrix(len(values), len(values[0]))
for r := 0; r < m.Height; r++ {
m.SetRowFromDense(r, values[r])
}
return m
}
//Create sparse binary matrix from specified dense matrix
func NewDenseBinaryMatrixFromDense1D(values []bool, rows, cols int) *DenseBinaryMatrix {
if len(values) < 1 {
panic("No values specified.")
}
if len(values) != rows*cols {
panic("Invalid size")
}
m := NewDenseBinaryMatrix(rows, cols)
for r := 0; r < m.Height; r++ {
m.SetRowFromDense(r, values[r*cols:(r*cols)+cols])
}
return m
}
// Creates a sparse binary matrix from specified integer array
// (any values greater than 0 are true)
func NewDenseBinaryMatrixFromInts(values [][]int) *DenseBinaryMatrix {
if len(values) < 1 {
panic("No values specified.")
}
m := NewDenseBinaryMatrix(len(values), len(values[0]))
for r := 0; r < m.Height; r++ {
for c := 0; c < m.Width; c++ {
if values[r][c] > 0 {
m.Set(r, c, true)
}
}
}
return m
}
//Converts index to col/row
func (sm *DenseBinaryMatrix) toIndex(index int) (row int, col int) {
row = index / sm.Width
col = index % sm.Width
return
}
//Returns all true/on indices
func (sm *DenseBinaryMatrix) Entries() []SparseEntry {
result := make([]SparseEntry, 0, int(float64(len(sm.entries))*0.3))
for idx, val := range sm.entries {
if val {
i, j := sm.toIndex(idx)
result = append(result, SparseEntry{i, j})
}
}
return result
}
//Returns flattend dense represenation
func (sm *DenseBinaryMatrix) Flatten() []bool {
result := make([]bool, sm.Height*sm.Width)
for _, val := range sm.Entries() {
result[(val.Row*sm.Width)+val.Col] = true
}
return result
}
//Get value at col,row position
func (sm *DenseBinaryMatrix) Get(row int, col int) bool {
row = row % sm.Height
if row < 0 {
row = sm.Height - row
}
col = col % sm.Width
if col < 0 {
col = sm.Width - col
}
return sm.entries[row*sm.Width+col]
}
//Set value at row,col position
func (sm *DenseBinaryMatrix) Set(row int, col int, value bool) {
row = row % sm.Height
if row < 0 {
row = sm.Height - row
}
col = col % sm.Width
if col < 0 {
col = sm.Width - col
}
sm.entries[row*sm.Width+col] = value
}
//Replaces specified row with values, assumes values is ordered
//correctly
func (sm *DenseBinaryMatrix) ReplaceRow(row int, values []bool) {
sm.validateRowCol(row, len(values))
for i := 0; i < sm.Width; i++ {
sm.Set(row, i, values[i])
}
}
//Replaces row with true values at specified indices
func (sm *DenseBinaryMatrix) ReplaceRowByIndices(row int, indices []int) {
sm.validateRow(row)
start := row * sm.Width
for i := 0; i < sm.Width; i++ {
sm.entries[start+i] = utils.ContainsInt(i, indices)
}
}
//Returns dense row
func (sm *DenseBinaryMatrix) GetDenseRow(row int) []bool {
sm.validateRow(row)
result := make([]bool, sm.Width)
start := row * sm.Width
for i := 0; i < sm.Width; i++ {
result[i] = sm.entries[start+i]
}
return result
}
//Returns a rows "on" indices
func (sm *DenseBinaryMatrix) GetRowIndices(row int) []int {
result := make([]int, 0, sm.Width)
start := row * sm.Width
for i := 0; i < sm.Width; i++ {
if sm.entries[start+i] {
result = append(result, i)
}
}
return result
}
//Sets a sparse row from dense representation
func (sm *DenseBinaryMatrix) SetRowFromDense(row int, denseRow []bool) {
//TODO: speed this up
sm.validateRowCol(row, len(denseRow))
for i := 0; i < sm.Width; i++ {
sm.Set(row, i, denseRow[i])
}
}
//In a normal matrix this would be multiplication in binary terms
//we just and then sum the true entries
func (sm *DenseBinaryMatrix) RowAndSum(row []bool) []int {
sm.validateCol(len(row))
result := make([]int, sm.Height)
for idx, val := range sm.entries {
if val {
r, c := sm.toIndex(idx)
if row[c] {
result[r]++
}
}
}
return result
}
//Returns row indexes with at least 1 true column
func (sm *DenseBinaryMatrix) NonZeroRows() []int {
counts := make(map[int]int, sm.Height)
for idx, val := range sm.entries {
if val {
r, _ := sm.toIndex(idx)
counts[r]++
}
}
result := make([]int, 0, sm.Height)
for k, v := range counts {
if v > 0 && !utils.ContainsInt(k, result) {
result = append(result, k)
}
}
return result
}
//Returns # of rows with at least 1 true value
func (sm *DenseBinaryMatrix) TotalTrueRows() int {
return len(sm.NonZeroRows())
}
//Returns total true entries
func (sm *DenseBinaryMatrix) TotalNonZeroCount() int {
return len(sm.Entries())
}
// Ors 2 matrices
func (sm *DenseBinaryMatrix) Or(sm2 *DenseBinaryMatrix) *DenseBinaryMatrix {
result := NewDenseBinaryMatrix(sm.Height, sm.Width)
for _, val := range sm.Entries() {
result.Set(val.Row, val.Col, true)
}
for _, val := range sm2.Entries() {
result.Set(val.Row, val.Col, true)
}
return result
}
//Clears all entries
func (sm *DenseBinaryMatrix) Clear() {
utils.FillSliceBool(sm.entries, false)
}
//Fills specified row with specified value
func (sm *DenseBinaryMatrix) FillRow(row int, val bool) {
for j := 0; j < sm.Width; j++ {
sm.Set(row, j, val)
}
}
//Copys a matrix
func (sm *DenseBinaryMatrix) Copy() *DenseBinaryMatrix {
if sm == nil {
return nil
}
result := new(DenseBinaryMatrix)
result.Width = sm.Width
result.Height = sm.Height
result.entries = make([]bool, len(sm.entries))
for idx, val := range sm.entries {
result.entries[idx] = val
}
return result
}
func (sm *DenseBinaryMatrix) ToString() string {
var buffer bytes.Buffer
for r := 0; r < sm.Height; r++ {
for c := 0; c < sm.Width; c++ {
if sm.Get(r, c) {
buffer.WriteByte('1')
} else {
buffer.WriteByte('0')
}
}
buffer.WriteByte('\n')
}
return buffer.String()
}
func (sm *DenseBinaryMatrix) validateCol(col int) {
if col > sm.Width {
panic("Specified row is wider than matrix.")
}
}
func (sm *DenseBinaryMatrix) validateRow(row int) {
if row > sm.Height {
panic("Specified row is out of bounds.")
}
}
func (sm *DenseBinaryMatrix) validateRowCol(row int, col int) {
sm.validateCol(col)
sm.validateRow(row)
}