11# TODO : update to modern op system.
22
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+
315" In place version of `select`."
416function select! (
517 op,
618 C:: GBVecOrMat ,
7- A:: GBArrayOrTranspose ,
8- thunk = nothing ;
19+ A:: GBArrayOrTranspose{T} ,
20+ thunk:: TH = defaultselectthunk (op, T) ;
921 mask = nothing ,
1022 accum = nothing ,
1123 desc = nothing
12- )
24+ ) where {T, TH}
25+ op ∈ (rowindex, colindex, diagindex, tril, triu, diag, offdiag) &&
26+ (thunk = convert (Int64, thunk))
1327 _canbeoutput (C) || throw (ShallowException ())
14- op = SelectOp (op)
28+ op = indexunaryop (op, T, TH )
1529 desc = _handledescriptor (desc; out= C, in1= A)
1630 mask = _handlemask! (desc, mask)
17- thunk === nothing && (thunk = C_NULL )
1831 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)
2333 return C
2434end
2535
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
2740 return select! (op, A, A, thunk; mask, accum, desc)
2841end
2942
3043"""
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
3346
3447Return a `GBArray` whose elements satisfy the predicate defined by `op`.
3548Some SelectOps or functions may require an additional argument `thunk`, for use in
3649 comparison operations such as `C[i,j] = A[i,j] >= thunk ? A[i,j] : nothing`, which is
3750 performed by `select(>, A, thunk)`.
3851
3952# Arguments
40- - `op::Union{ Function, SelectUnion} `: A select operator from the SelectOps submodule.
53+ - `op::Function`: A select operator from the SelectOps submodule.
4154- `A::GBArrayOrTranspose`
4255- `thunk::Union{GBScalar, nothing, valid_union}`: Optional value used to evaluate `op`.
4356
@@ -53,19 +66,19 @@ Some SelectOps or functions may require an additional argument `thunk`, for use
5366"""
5467function select (
5568 op,
56- A:: GBArrayOrTranspose ,
57- thunk = nothing ;
69+ A:: GBArrayOrTranspose{T} ,
70+ thunk:: TH = defaultselectthunk (op, T) ;
5871 mask = nothing ,
5972 accum = nothing ,
6073 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.
6477 select! (op, C, A, thunk; accum, mask, desc)
6578 return C
6679end
6780
6881LinearAlgebra. tril (A:: GBArrayOrTranspose , k:: Integer = 0 ) = select (tril, A, k)
6982LinearAlgebra. 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