-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdenseMatrix.go
366 lines (293 loc) · 8.06 KB
/
denseMatrix.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright (c) 2018 Ross Merrigan
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package graphblas
import (
"context"
"log"
"github.com/rossmerr/graphblas/constraints"
)
// DenseMatrix a dense matrix
type DenseMatrix[T constraints.Type] struct {
c int // number of rows in the sparse matrix
r int // number of columns in the sparse matrix
data [][]T
}
type DenseMatrixNumber[T constraints.Number] struct {
DenseMatrix[T]
}
// NewDenseMatrix returns a DenseMatrix
func NewDenseMatrix[T constraints.Type](r, c int) *DenseMatrix[T] {
return newMatrixType[T](r, c, nil)
}
func NewDenseMatrixN[T constraints.Number](r, c int) *DenseMatrixNumber[T] {
return newMatrixNumber[T](r, c, nil)
}
// NewDenseMatrixFromArray returns a DenseMatrix
func NewDenseMatrixFromArray[T constraints.Number](data [][]T) *DenseMatrix[T] {
r := len(data)
c := len(data[0])
s := newMatrixType[T](r, c, nil)
s.data = data
return s
}
// NewDenseMatrixFromArray returns a DenseMatrix
func NewDenseMatrixFromArrayN[T constraints.Number](data [][]T) *DenseMatrixNumber[T] {
r := len(data)
c := len(data[0])
s := newMatrixNumber[T](r, c, nil)
s.data = data
return s
}
func newMatrix[T constraints.Type](r, c int, initialise func([]T, int)) DenseMatrix[T] {
s := DenseMatrix[T]{data: make([][]T, r), r: r, c: c}
for i := 0; i < r; i++ {
s.data[i] = make([]T, c)
if initialise != nil {
initialise(s.data[i], i)
}
}
return s
}
func newMatrixType[T constraints.Type](r, c int, initialise func([]T, int)) *DenseMatrix[T] {
d := newMatrix[T](r, c, initialise)
return &d
}
func newMatrixNumber[T constraints.Number](r, c int, initialise func([]T, int)) *DenseMatrixNumber[T] {
d := &DenseMatrixNumber[T]{
DenseMatrix: newMatrix[T](r, c, initialise),
}
return d
}
// Columns the number of columns of the matrix
func (s *DenseMatrix[T]) Columns() int {
return s.c
}
// Rows the number of rows of the matrix
func (s *DenseMatrix[T]) Rows() int {
return s.r
}
// Update does a At and Set on the matrix element at r-th, c-th
func (s *DenseMatrix[T]) Update(r, c int, f func(T) T) {
if r < 0 || r >= s.Rows() {
log.Panicf("Row '%+v' is invalid", r)
}
if c < 0 || c >= s.Columns() {
log.Panicf("Column '%+v' is invalid", c)
}
s.data[r][c] = f(s.data[r][c])
return
}
// At returns the value of a matrix element at r-th, c-th
func (s *DenseMatrix[T]) At(r, c int) T {
if r < 0 || r >= s.Rows() {
log.Panicf("Row '%+v' is invalid", r)
}
if c < 0 || c >= s.Columns() {
log.Panicf("Column '%+v' is invalid", c)
}
return s.data[r][c]
}
// Set sets the value at r-th, c-th of the matrix
func (s *DenseMatrix[T]) Set(r, c int, value T) {
if r < 0 || r >= s.Rows() {
log.Panicf("Row '%+v' is invalid", r)
}
if c < 0 || c >= s.Columns() {
log.Panicf("Column '%+v' is invalid", c)
}
s.data[r][c] = value
}
// ColumnsAt return the columns at c-th
func (s *DenseMatrix[T]) ColumnsAt(c int) VectorLogial[T] {
if c < 0 || c >= s.Columns() {
log.Panicf("Column '%+v' is invalid", c)
}
columns := NewDenseVector[T](s.r)
for r := 0; r < s.r; r++ {
columns.SetVec(r, s.data[r][c])
}
return columns
}
// RowsAt return the rows at r-th
func (s *DenseMatrix[T]) RowsAt(r int) VectorLogial[T] {
if r < 0 || r >= s.Rows() {
log.Panicf("Row '%+v' is invalid", r)
}
rows := NewDenseVector[T](s.c)
for i := 0; i < s.c; i++ {
rows.SetVec(i, s.data[r][i])
}
return rows
}
// RowsAtToArray return the rows at r-th
func (s *DenseMatrix[T]) RowsAtToArray(r int) []T {
if r < 0 || r >= s.Rows() {
log.Panicf("Row '%+v' is invalid", r)
}
rows := make([]T, s.c)
for i := 0; i < s.c; i++ {
rows[i] = s.data[r][i]
}
return rows
}
// Copy copies the matrix
func (s *DenseMatrix[T]) CopyLogical() MatrixLogical[T] {
v := Default[T]()
matrix := newMatrix[T](s.Rows(), s.Columns(), func(row []T, r int) {
for c := 0; c < s.Columns(); c++ {
v = s.data[r][c]
if v != Default[T]() {
row[c] = v
} else {
row[c] = v
}
}
})
return &matrix
}
func (s *DenseMatrixNumber[T]) Copy() Matrix[T] {
v := Default[T]()
matrix := newMatrixNumber[T](s.Rows(), s.Columns(), func(row []T, r int) {
for c := 0; c < s.Columns(); c++ {
v = s.data[r][c]
if v != Default[T]() {
row[c] = v
} else {
row[c] = v
}
}
})
return matrix
}
// Scalar multiplication of a matrix by alpha
func (s *DenseMatrixNumber[T]) Scalar(alpha T) Matrix[T] {
return Scalar[T](context.Background(), s, alpha)
}
// Multiply multiplies a matrix by another matrix
func (s *DenseMatrixNumber[T]) Multiply(m Matrix[T]) Matrix[T] {
matrix := newMatrixNumber[T](s.Rows(), m.Columns(), nil)
MatrixMatrixMultiply[T](context.Background(), s, m, nil, matrix)
return matrix
}
// Add addition of a matrix by another matrix
func (s *DenseMatrixNumber[T]) Add(m Matrix[T]) Matrix[T] {
matrix := s.Copy()
Add[T](context.Background(), s, m, nil, matrix)
return matrix
}
// Subtract subtracts one matrix from another matrix
func (s *DenseMatrixNumber[T]) Subtract(m Matrix[T]) Matrix[T] {
matrix := m.Copy()
Subtract[T](context.Background(), s, m, nil, matrix)
return matrix
}
// Negative the negative of a matrix
func (s *DenseMatrixNumber[T]) Negative() MatrixLogical[T] {
matrix := s.Copy()
Negative[T](context.Background(), s, nil, matrix)
return matrix
}
// Transpose swaps the rows and columns
func (s *DenseMatrix[T]) Transpose() MatrixLogical[T] {
matrix := newMatrix[T](s.Columns(), s.Rows(), nil)
Transpose[T](context.Background(), s, nil, &matrix)
return &matrix
}
// Equal the two matrices are equal
func (s *DenseMatrix[T]) Equal(m MatrixLogical[T]) bool {
return Equal[T](context.Background(), s, m)
}
// NotEqual the two matrices are not equal
func (s *DenseMatrix[T]) NotEqual(m MatrixLogical[T]) bool {
return NotEqual[T](context.Background(), s, m)
}
// Size of the matrix
func (s *DenseMatrix[T]) Size() int {
return s.r * s.c
}
// Values the number of elements in the matrix
func (s *DenseMatrix[T]) Values() int {
return s.r * s.c
}
// Clear removes all elements from a matrix
func (s *DenseMatrix[T]) Clear() {
s.data = make([][]T, s.r)
for i := 0; i < s.r; i++ {
s.data[i] = make([]T, s.c)
}
}
// RawMatrix returns the raw matrix
func (s *DenseMatrix[T]) RawMatrix() [][]T {
return s.data
}
// Enumerate iterates through all non-zero elements, order is not guaranteed
func (s *DenseMatrix[T]) Enumerate() Enumerate[T] {
return s.iterator()
}
func (s *DenseMatrix[T]) iterator() *denseMatrixIterator[T] {
i := &denseMatrixIterator[T]{
matrix: s,
size: s.Values(),
last: 0,
c: 0,
r: 0,
}
return i
}
type denseMatrixIterator[T constraints.Type] struct {
matrix *DenseMatrix[T]
size int
last int
c int
r int
cOld int
rOld int
}
// HasNext checks the iterator has any more values
func (s *denseMatrixIterator[T]) HasNext() bool {
if s.last >= s.size {
return false
}
return true
}
func (s *denseMatrixIterator[T]) next() {
if s.c == s.matrix.Columns() {
s.c = 0
s.r++
}
s.cOld = s.c
s.c++
s.last++
}
// Next moves the iterator and returns the row, column and value
func (s *denseMatrixIterator[T]) Next() (int, int, T) {
s.next()
return s.r, s.cOld, s.matrix.At(s.r, s.cOld)
}
// Map replace each element with the result of applying a function to its value
func (s *DenseMatrixNumber[T]) Map() Map[T] {
t := s.iterator()
i := &denseMatrixMap[T]{t}
return i
}
type denseMatrixMap[T constraints.Number] struct {
*denseMatrixIterator[T]
}
// HasNext checks the iterator has any more values
func (s *denseMatrixMap[T]) HasNext() bool {
return s.denseMatrixIterator.HasNext()
}
// Map move the iterator and uses a higher order function to changes the elements current value
func (s *denseMatrixMap[T]) Map(f func(int, int, T) T) {
s.next()
s.matrix.Set(s.r, s.cOld, f(s.r, s.cOld, s.matrix.At(s.r, s.cOld)))
}
// Element of the mask for each tuple that exists in the matrix for which the value of the tuple cast to Boolean is true
func (s *DenseMatrixNumber[T]) Element(r, c int) bool {
return s.element(r, c)
}
func (s *DenseMatrixNumber[T]) element(r, c int) bool {
return s.At(r, c) > Default[T]()
}