-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.py
143 lines (81 loc) · 2.91 KB
/
features.py
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
from GraphBLAS import *
def ops():
A = Matrix(
(
[4213, 234, 242, 1123, 3342],
([1, 2, 2, 3, 3], [0, 1, 2, 1, 3])
), shape=(4,4)
)
v = Vector([1, 2, 3, 4])
# element-wise operators
B = A + A # eWiseAdd (matrix)
u = v + v # eWiseAdd (vector)
C = A * A # eWiseMult (matrix)
w = v * v # eWiseMult (vector)
# matrix products
u = A @ v # mxv
w = v @ A # vxm
C = A @ A # mxm
# with masking
from scipy import sparse
# construct empty matrix to assign into
B = Matrix(shape=A.shape, dtype=A.dtype)
# construct random matrix from scipy.sparse matrix
M = Matrix(sparse.random(*A.shape, density=0.5, dtype='float'))
B[:] = A + A # no mask
B[M] = A * A # matrix mask
B[~M] = A @ A # matrix complement mask
B[M.T] = A @ A # matrix transpose mask
# with accumulators
C = Matrix(shape=A.shape, dtype=A.dtype)
C[:] = 1 # assign 1 to every element of C
C[:] += A + A # default accumulator (arithmetic)
C[M] += A * A # accumulator with mask
C[:,True] += A @ A # replace_flag = True
# using non-default operators
# built-in boolean accumulate
from GraphBLAS.operators import BooleanAccumulate
with BooleanAccumulate:
C[:] += A + A
# binary operator definition
from GraphBLAS.operators import BinaryOp
Max = BinaryOp("Max")
with Max:
B = A + A
# built-in semiring operator
from GraphBLAS.operators import MaxSelect2ndSemiring
with MaxSelect2ndSemiring:
C = A @ A
# assign operator
A[2, 3] = 3 # assign single element
# assign with slice
A[:,:] = B # assign to entire matrix A
D = Matrix(([1, 2, 3, 4], ([0, 1, 0, 1], [0, 1, 0, 1])))
A[2:,:2] = D # assign to upper-right 2x2 submatrix
# assign with list/np.array
i = list(range(A.shape[0]))
j = list(range(A.shape[1]))
A[i, j] = B
##### TODO #####
# A[1,:] = v # assign to row of A
# A[:,2] = v # assign to column of A
##### END TODO #####
A[2:,3] = 4 # assign constant to submatrix
# extract operator
D = A[:] # extract entire matrix A (essentially copies)
D = A[:2,2:] # extract lower left 2x2 submatrix of A
# TODO D[:] = A[::2,::2] # extract into existing matrix D (no mask)
D = A[:,1] # extract column of A
# apply operator
from GraphBLAS.operators import apply, UnaryOp
A = apply(operators.AdditiveInverse, A) # apply unary operator
plus1 = UnaryOp("Plus", 1)
A = apply(plus1, A) # apply binary operator with bound constant
A[M] = apply(plus1, A) # apply with mask
# reduce operator
from GraphBLAS.operators import reduce, Monoid
plus = Monoid("Plus", 0)
c = reduce(plus, A) # reduce to constant
v = Vector(shape=A.shape[0], dtype=A.dtype)
v[:] = reduce(plus, A) # reduce to vector
ops()