1
1
# TODO : update to modern op system.
2
2
3
+ function defaultselectthunk (op, T)
4
+ if op ∈ (rowindex, colindex, diagindex)
5
+ return one (Int64)
6
+ elseif op ∈ (tril, triu, diag, offdiag)
7
+ return zero (Int64)
8
+ elseif op === ==
9
+ return zero (T)
10
+ else
11
+ throw (ArgumentError (" You must pass `thunk` to select for this function." ))
12
+ end
13
+ end
14
+
3
15
" In place version of `select`."
4
16
function select! (
5
17
op,
6
18
C:: GBVecOrMat ,
7
- A:: GBArrayOrTranspose ,
8
- thunk = nothing ;
19
+ A:: GBArrayOrTranspose{T} ,
20
+ thunk:: TH = defaultselectthunk (op, T) ;
9
21
mask = nothing ,
10
22
accum = nothing ,
11
23
desc = nothing
12
- )
24
+ ) where {T, TH}
25
+ op ∈ (rowindex, colindex, diagindex, tril, triu, diag, offdiag) &&
26
+ (thunk = convert (Int64, thunk))
13
27
_canbeoutput (C) || throw (ShallowException ())
14
- op = SelectOp (op)
28
+ op = indexunaryop (op, T, TH )
15
29
desc = _handledescriptor (desc; out= C, in1= A)
16
30
mask = _handlemask! (desc, mask)
17
- thunk === nothing && (thunk = C_NULL )
18
31
accum = _handleaccum (accum, storedeltype (C))
19
- if thunk isa Number
20
- thunk = GBScalar (thunk)
21
- end
22
- @wraperror LibGraphBLAS. GxB_Matrix_select (C, mask, accum, op, parent (A), thunk, desc)
32
+ @wraperror LibGraphBLAS. GrB_Matrix_select_Scalar (C, mask, accum, op, parent (A), GBScalar (thunk), desc)
23
33
return C
24
34
end
25
35
26
- function select! (op, A:: GBArrayOrTranspose , thunk = nothing ; mask = nothing , accum = nothing , desc = nothing )
36
+ function select! (
37
+ op, A:: GBArrayOrTranspose{T} , thunk = defaultselectthunk (op, T);
38
+ mask = nothing , accum = nothing , desc = nothing
39
+ ) where T
27
40
return select! (op, A, A, thunk; mask, accum, desc)
28
41
end
29
42
30
43
"""
31
- select(op::Union{ Function, SelectUnion} , A::GBArrayOrTranspose; kwargs...)::GBArrayOrTranspose
32
- select(op::Union{ Function, SelectUnion} , A::GBArrayOrTranspose, thunk; kwargs...)::GBArrayOrTranspose
44
+ select(op::Function, A::GBArrayOrTranspose; kwargs...)::GBArrayOrTranspose
45
+ select(op::Function, A::GBArrayOrTranspose, thunk; kwargs...)::GBArrayOrTranspose
33
46
34
47
Return a `GBArray` whose elements satisfy the predicate defined by `op`.
35
48
Some SelectOps or functions may require an additional argument `thunk`, for use in
36
49
comparison operations such as `C[i,j] = A[i,j] >= thunk ? A[i,j] : nothing`, which is
37
50
performed by `select(>, A, thunk)`.
38
51
39
52
# Arguments
40
- - `op::Union{ Function, SelectUnion} `: A select operator from the SelectOps submodule.
53
+ - `op::Function`: A select operator from the SelectOps submodule.
41
54
- `A::GBArrayOrTranspose`
42
55
- `thunk::Union{GBScalar, nothing, valid_union}`: Optional value used to evaluate `op`.
43
56
@@ -53,19 +66,19 @@ Some SelectOps or functions may require an additional argument `thunk`, for use
53
66
"""
54
67
function select (
55
68
op,
56
- A:: GBArrayOrTranspose ,
57
- thunk = nothing ;
69
+ A:: GBArrayOrTranspose{T} ,
70
+ thunk:: TH = defaultselectthunk (op, T) ;
58
71
mask = nothing ,
59
72
accum = nothing ,
60
73
desc = nothing
61
- )
62
- op = SelectOp (op)
63
- C = similar (A)
74
+ ) where {T, TH}
75
+ op = indexunaryop (op, T, TH )
76
+ C = similar (A) # we keep the same type!! not the ztype of op.
64
77
select! (op, C, A, thunk; accum, mask, desc)
65
78
return C
66
79
end
67
80
68
81
LinearAlgebra. tril (A:: GBArrayOrTranspose , k:: Integer = 0 ) = select (tril, A, k)
69
82
LinearAlgebra. triu (A:: GBArrayOrTranspose , k:: Integer = 0 ) = select (triu, A, k)
70
- SparseArrays. dropzeros (A:: GBArrayOrTranspose ) = select (nonzeros , A)
71
- SparseArrays. dropzeros! (A:: GBArrayOrTranspose ) = select! (nonzeros , A)
83
+ SparseArrays. dropzeros (A:: GBArrayOrTranspose{T} ) where T = select (!= , A, zero (T) )
84
+ SparseArrays. dropzeros! (A:: GBArrayOrTranspose{T} ) where T = select! (!= , A, zero (T) )
0 commit comments