-
Notifications
You must be signed in to change notification settings - Fork 1
/
sparseVector.go
371 lines (295 loc) · 8.18 KB
/
sparseVector.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
367
368
369
370
371
// Copyright (c) 2018 Ross Merrigan
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package graphblas
import (
"context"
"log"
"reflect"
"github.com/rossmerr/graphblas/constraints"
)
// type Ordered interface {
// Integer | Float | ~string
// }
func init() {
RegisterMatrix(reflect.TypeOf((*SparseVector[float64])(nil)).Elem())
}
// SparseVector compressed storage by indices
type SparseVector[T constraints.Number] struct {
l int // length of the sparse vector
values []T
indices []int
}
// NewSparseVector returns a SparseVector
func NewSparseVector[T constraints.Number](l int) *SparseVector[T] {
return newSparseVector[T](l, 0)
}
// NewSparseVectorFromArray returns a SparseVector
func NewSparseVectorFromArray[T constraints.Number](data []T) *SparseVector[T] {
l := len(data)
s := newSparseVector[T](l, 0)
for i := 0; i < l; i++ {
s.SetVec(i, data[i])
}
return s
}
func newSparseVector[T constraints.Number](l, s int) *SparseVector[T] {
return &SparseVector[T]{l: l, values: make([]T, s), indices: make([]int, s)}
}
// Length of the vector
func (s *SparseVector[T]) Length() int {
return s.l
}
// AtVec returns the value of a vector element at i-th
func (s *SparseVector[T]) AtVec(i int) T {
if i < 0 || i >= s.Length() {
log.Panicf("Length '%+v' is invalid", i)
}
pointer, length, _ := s.index(i)
if pointer < length && s.indices[pointer] == i {
return s.values[pointer]
}
return Zero[T]()
}
// SetVec sets the value at i-th of the vector
func (s *SparseVector[T]) SetVec(i int, value T) {
if i < 0 || i >= s.Length() {
log.Panicf("Length '%+v' is invalid", i)
}
pointer, length, _ := s.index(i)
if pointer < length && s.indices[pointer] == i {
if IsZero(value) {
s.remove(pointer)
} else {
s.values[pointer] = value
}
} else {
s.insert(pointer, i, value)
}
}
// Columns the number of columns of the vector
func (s *SparseVector[T]) Columns() int {
return 1
}
// Rows the number of rows of the vector
func (s *SparseVector[T]) Rows() int {
return s.l
}
// Update does a At and Set on the vector element at r-th, c-th
func (s *SparseVector[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)
}
v := s.AtVec(r)
s.SetVec(r, f(v))
}
// At returns the value of a vector element at r-th, c-th
func (s *SparseVector[T]) At(r, c int) (value T) {
s.Update(r, c, func(v T) T {
value = v
return v
})
return
}
// Set sets the value at r-th, c-th of the vector
func (s *SparseVector[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.SetVec(r, value)
}
// ColumnsAt return the columns at c-th
func (s *SparseVector[T]) ColumnsAt(c int) VectorLogial[T] {
if c < 0 || c >= s.Columns() {
log.Panicf("Column '%+v' is invalid", c)
}
return s.copy()
}
// RowsAt return the rows at r-th
func (s *SparseVector[T]) RowsAt(r int) VectorLogial[T] {
if r < 0 || r >= s.Rows() {
log.Panicf("Row '%+v' is invalid", r)
}
rows := NewSparseVector[T](1)
v := s.AtVec(r)
rows.SetVec(0, v)
return rows
}
// RowsAtToArray return the rows at r-th
func (s *SparseVector[T]) RowsAtToArray(r int) []T {
if r < 0 || r >= s.Rows() {
log.Panicf("Row '%+v' is invalid", r)
}
rows := make([]T, 1)
v := s.AtVec(r)
rows[0] = v
return rows
}
func (s *SparseVector[T]) insert(pointer, i int, value T) {
if IsZero(value) {
return
}
s.indices = append(s.indices[:pointer], append([]int{i}, s.indices[pointer:]...)...)
s.values = append(s.values[:pointer], append([]T{value}, s.values[pointer:]...)...)
}
func (s *SparseVector[T]) remove(pointer int) {
s.indices = append(s.indices[:pointer], s.indices[pointer+1:]...)
s.values = append(s.values[:pointer], s.values[pointer+1:]...)
}
func (s *SparseVector[T]) index(i int) (int, int, error) {
length := len(s.indices)
if i > length {
return length, length, nil
}
start := 0
end := length
for start < end {
p := (start + end) / 2
if s.indices[p] > i {
end = p
} else if s.indices[p] < i {
start = p + 1
} else {
return p, length, nil
}
}
return start, length, nil
}
func (s *SparseVector[T]) copy() *SparseVector[T] {
vector := newSparseVector[T](s.l, len(s.indices))
for i := range s.values {
vector.values[i] = s.values[i]
vector.indices[i] = s.indices[i]
}
return vector
}
// Copy copies the vector
func (s *SparseVector[T]) Copy() Matrix[T] {
return s.copy()
}
func (s *SparseVector[T]) CopyLogical() MatrixLogical[T] {
return s.copy()
}
// Scalar multiplication of a vector by alpha
func (s *SparseVector[T]) Scalar(alpha T) Matrix[T] {
return Scalar[T](context.Background(), s, alpha)
}
// Multiply multiplies a vector by another vector
func (s *SparseVector[T]) Multiply(m Matrix[T]) Matrix[T] {
matrix := newMatrixNumber[T](m.Rows(), s.Columns(), nil)
MatrixMatrixMultiply[T](context.Background(), s, m, nil, matrix)
return matrix
}
// Add addition of a metrix by another metrix
func (s *SparseVector[T]) Add(m Matrix[T]) Matrix[T] {
matrix := s.copy()
Add[T](context.Background(), s, m, nil, matrix)
return matrix
}
// Subtract subtracts one metrix from another metrix
func (s *SparseVector[T]) Subtract(m Matrix[T]) Matrix[T] {
matrix := s.copy()
Subtract[T](context.Background(), s, m, nil, matrix)
return matrix
}
// Negative the negative of a metrix
func (s *SparseVector[T]) Negative() MatrixLogical[T] {
matrix := s.copy()
Negative[T](context.Background(), s, nil, matrix)
return matrix
}
// Transpose swaps the rows and columns
func (s *SparseVector[T]) Transpose() MatrixLogical[T] {
matrix := newMatrix[T](s.Columns(), s.Rows(), nil)
Transpose[T](context.Background(), s, nil, &matrix)
return &matrix
}
// Equal the two metrics are equal
func (s *SparseVector[T]) Equal(m MatrixLogical[T]) bool {
return Equal[T](context.Background(), s, m)
}
// NotEqual the two metrix are not equal
func (s *SparseVector[T]) NotEqual(m MatrixLogical[T]) bool {
return NotEqual[T](context.Background(), s, m)
}
// Size of the vector
func (s *SparseVector[T]) Size() int {
return s.l
}
// Values the number of non-zero elements in the Vector
func (s *SparseVector[T]) Values() int {
return len(s.values)
}
// Clear removes all elements from a vector
func (s *SparseVector[T]) Clear() {
s.values = make([]T, 0)
s.indices = make([]int, 0)
}
// Enumerate iterates through all non-zero elements, order is not guaranteed
func (s *SparseVector[T]) Enumerate() Enumerate[T] {
return s.iterator()
}
func (s *SparseVector[T]) iterator() *sparseVectorIterator[T] {
i := &sparseVectorIterator[T]{
matrix: s,
size: len(s.values),
last: 0,
}
return i
}
type sparseVectorIterator[T constraints.Number] struct {
matrix *SparseVector[T]
size int
last int
old int
}
// HasNext checks the iterator has any more values
func (s *sparseVectorIterator[T]) HasNext() bool {
if s.last >= s.size {
return false
}
return true
}
func (s *sparseVectorIterator[T]) next() {
s.old = s.last
s.last++
}
// Next moves the iterator and returns the row, column and value
func (s *sparseVectorIterator[T]) Next() (int, int, T) {
s.next()
return s.matrix.indices[s.old], 0, s.matrix.values[s.old]
}
// Map replace each element with the result of applying a function to its value
func (s *SparseVector[T]) Map() Map[T] {
t := s.iterator()
i := &sparseVectorMap[T]{t}
return i
}
type sparseVectorMap[T constraints.Number] struct {
*sparseVectorIterator[T]
}
// HasNext checks the iterator has any more values
func (s *sparseVectorMap[T]) HasNext() bool {
return s.sparseVectorIterator.HasNext()
}
// Map move the iterator and uses a higher order function to changes the elements current value
func (s *sparseVectorMap[T]) Map(f func(int, int, T) T) {
s.next()
value := f(s.matrix.indices[s.old], 0, s.matrix.values[s.old])
if !IsZero(value) {
s.matrix.values[s.old] = value
} else {
s.matrix.remove(s.old)
}
}
// 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 *SparseVector[T]) Element(r, c int) bool {
return s.AtVec(r) > Default[T]()
}