@@ -7,7 +7,7 @@ module HigherOrderFns
77import Base: map, map!, broadcast, copy, copyto!
88
99using Base: front, tail, to_shape
10- using .. SparseArrays: SparseVector, SparseMatrixCSC, AbstractSparseVector,
10+ using .. SparseArrays: SparseVector, SparseMatrixCSC, AbstractSparseVector, AbstractSparseMatrixCSC,
1111 AbstractSparseMatrix, AbstractSparseArray, indtype, nnz, nzrange,
1212 SparseVectorUnion, AdjOrTransSparseVectorUnion, nonzeroinds, nonzeros, rowvals, getcolptr
1313using Base. Broadcast: BroadcastStyle, Broadcasted, flatten
@@ -33,13 +33,13 @@ using LinearAlgebra
3333
3434# (0) BroadcastStyle rules and convenience types for dispatch
3535
36- SparseVecOrMat = Union{SparseVector,SparseMatrixCSC }
36+ SparseVecOrMat = Union{SparseVector,AbstractSparseMatrixCSC }
3737
3838# broadcast container type promotion for combinations of sparse arrays and other types
3939struct SparseVecStyle <: Broadcast.AbstractArrayStyle{1} end
4040struct SparseMatStyle <: Broadcast.AbstractArrayStyle{2} end
4141Broadcast. BroadcastStyle (:: Type{<:SparseVector} ) = SparseVecStyle ()
42- Broadcast. BroadcastStyle (:: Type{<:SparseMatrixCSC } ) = SparseMatStyle ()
42+ Broadcast. BroadcastStyle (:: Type{<:AbstractSparseMatrixCSC } ) = SparseMatStyle ()
4343const SPVM = Union{SparseVecStyle,SparseMatStyle}
4444
4545# SparseVecStyle handles 0-1 dimensions, SparseMatStyle 0-2 dimensions.
@@ -109,23 +109,23 @@ const SpBroadcasted2{Style<:SPVM,Axes,F,Args<:Tuple{SparseVecOrMat,SparseVecOrMa
109109# as n-by-one sparse matrices which, though technically incorrect, is how broacast[!] views
110110# sparse vectors in practice.
111111@inline numrows (A:: SparseVector ) = length (A)
112- @inline numrows (A:: SparseMatrixCSC ) = size (A, 1 )
112+ @inline numrows (A:: AbstractSparseMatrixCSC ) = size (A, 1 )
113113@inline numcols (A:: SparseVector ) = 1
114- @inline numcols (A:: SparseMatrixCSC ) = size (A, 2 )
114+ @inline numcols (A:: AbstractSparseMatrixCSC ) = size (A, 2 )
115115# numrows and numcols respectively yield size(A, 1) and size(A, 2), but avoid a branch
116116@inline columns (A:: SparseVector ) = 1
117- @inline columns (A:: SparseMatrixCSC ) = 1 : size (A, 2 )
117+ @inline columns (A:: AbstractSparseMatrixCSC ) = 1 : size (A, 2 )
118118@inline colrange (A:: SparseVector , j) = 1 : length (nonzeroinds (A))
119- @inline colrange (A:: SparseMatrixCSC , j) = nzrange (A, j)
119+ @inline colrange (A:: AbstractSparseMatrixCSC , j) = nzrange (A, j)
120120@inline colstartind (A:: SparseVector , j) = one (indtype (A))
121121@inline colboundind (A:: SparseVector , j) = convert (indtype (A), length (nonzeroinds (A)) + 1 )
122- @inline colstartind (A:: SparseMatrixCSC , j) = getcolptr (A)[j]
123- @inline colboundind (A:: SparseMatrixCSC , j) = getcolptr (A)[j + 1 ]
122+ @inline colstartind (A:: AbstractSparseMatrixCSC , j) = getcolptr (A)[j]
123+ @inline colboundind (A:: AbstractSparseMatrixCSC , j) = getcolptr (A)[j + 1 ]
124124@inline storedinds (A:: SparseVector ) = nonzeroinds (A)
125- @inline storedinds (A:: SparseMatrixCSC ) = rowvals (A)
125+ @inline storedinds (A:: AbstractSparseMatrixCSC ) = rowvals (A)
126126@inline storedvals (A:: SparseVecOrMat ) = nonzeros (A)
127127@inline setcolptr! (A:: SparseVector , j, val) = val
128- @inline setcolptr! (A:: SparseMatrixCSC , j, val) = getcolptr (A)[j] = val
128+ @inline setcolptr! (A:: AbstractSparseMatrixCSC , j, val) = getcolptr (A)[j] = val
129129function trimstorage! (A:: SparseVecOrMat , maxstored)
130130 resize! (storedinds (A), maxstored)
131131 resize! (storedvals (A), maxstored)
@@ -140,12 +140,12 @@ end
140140
141141# (2) map[!] entry points
142142map (f:: Tf , A:: SparseVector ) where {Tf} = _noshapecheck_map (f, A)
143- map (f:: Tf , A:: SparseMatrixCSC ) where {Tf} = _noshapecheck_map (f, A)
144- map (f:: Tf , A:: SparseMatrixCSC , Bs:: Vararg{SparseMatrixCSC,N} ) where {Tf,N} =
143+ map (f:: Tf , A:: AbstractSparseMatrixCSC ) where {Tf} = _noshapecheck_map (f, A)
144+ map (f:: Tf , A:: AbstractSparseMatrixCSC , Bs:: Vararg{SparseMatrixCSC,N} ) where {Tf,N} =
145145 (_checksameshape (A, Bs... ); _noshapecheck_map (f, A, Bs... ))
146146map (f:: Tf , A:: SparseVecOrMat , Bs:: Vararg{SparseVecOrMat,N} ) where {Tf,N} =
147147 (_checksameshape (A, Bs... ); _noshapecheck_map (f, A, Bs... ))
148- map! (f:: Tf , C:: SparseMatrixCSC , A:: SparseMatrixCSC , Bs:: Vararg{SparseMatrixCSC,N} ) where {Tf,N} =
148+ map! (f:: Tf , C:: AbstractSparseMatrixCSC , A:: AbstractSparseMatrixCSC , Bs:: Vararg{SparseMatrixCSC,N} ) where {Tf,N} =
149149 (_checksameshape (C, A, Bs... ); _noshapecheck_map! (f, C, A, Bs... ))
150150map! (f:: Tf , C:: SparseVecOrMat , A:: SparseVecOrMat , Bs:: Vararg{SparseVecOrMat,N} ) where {Tf,N} =
151151 (_checksameshape (C, A, Bs... ); _noshapecheck_map! (f, C, A, Bs... ))
216216@inline _densennz (shape:: NTuple{2} ) = shape[1 ] * shape[2 ]
217217_maxnnzfrom (shape:: NTuple{1} , A:: SparseVector ) = nnz (A) * div (shape[1 ], length (A))
218218_maxnnzfrom (shape:: NTuple{2} , A:: SparseVector ) = nnz (A) * div (shape[1 ], length (A)) * shape[2 ]
219- _maxnnzfrom (shape:: NTuple{2} , A:: SparseMatrixCSC ) = nnz (A) * div (shape[1 ], size (A, 1 )) * div (shape[2 ], size (A, 2 ))
219+ _maxnnzfrom (shape:: NTuple{2} , A:: AbstractSparseMatrixCSC ) = nnz (A) * div (shape[1 ], size (A, 1 )) * div (shape[2 ], size (A, 2 ))
220220@inline _maxnnzfrom_each (shape, :: Tuple{} ) = ()
221221@inline _maxnnzfrom_each (shape, As) = (_maxnnzfrom (shape, first (As)), _maxnnzfrom_each (shape, tail (As))... )
222222@inline _unchecked_maxnnzbcres (shape, As:: Tuple ) = min (_densennz (shape), sum (_maxnnzfrom_each (shape, As)))
@@ -277,13 +277,13 @@ function _map_notzeropres!(f::Tf, fillvalue, C::SparseVecOrMat, A::SparseVecOrMa
277277end
278278# helper functions for these methods and some of those below
279279@inline _densecoloffsets (A:: SparseVector ) = 0
280- @inline _densecoloffsets (A:: SparseMatrixCSC ) = 0 : size (A, 1 ): (size (A, 1 )* (size (A, 2 ) - 1 ))
280+ @inline _densecoloffsets (A:: AbstractSparseMatrixCSC ) = 0 : size (A, 1 ): (size (A, 1 )* (size (A, 2 ) - 1 ))
281281function _densestructure! (A:: SparseVector )
282282 expandstorage! (A, length (A))
283283 copyto! (nonzeroinds (A), 1 : length (A))
284284 return A
285285end
286- function _densestructure! (A:: SparseMatrixCSC )
286+ function _densestructure! (A:: AbstractSparseMatrixCSC )
287287 nnzA = size (A, 1 ) * size (A, 2 )
288288 expandstorage! (A, nnzA)
289289 copyto! (getcolptr (A), 1 : size (A, 1 ): (nnzA + 1 ))
@@ -812,7 +812,7 @@ function _broadcast_notzeropres!(f::Tf, fillvalue, C::SparseVecOrMat, A::SparseV
812812 return C
813813end
814814_finishempty! (C:: SparseVector ) = C
815- _finishempty! (C:: SparseMatrixCSC ) = (fill! (getcolptr (C), 1 ); C)
815+ _finishempty! (C:: AbstractSparseMatrixCSC ) = (fill! (getcolptr (C), 1 ); C)
816816
817817# special case - vector outer product
818818_copy (f:: typeof (* ), x:: SparseVectorUnion , y:: AdjOrTransSparseVectorUnion ) = _outer (x, y)
@@ -1012,7 +1012,7 @@ end
10121012end
10131013
10141014_copy (f, args:: SparseVector... ) = _shapecheckbc (f, args... )
1015- _copy (f, args:: SparseMatrixCSC ... ) = _shapecheckbc (f, args... )
1015+ _copy (f, args:: AbstractSparseMatrixCSC ... ) = _shapecheckbc (f, args... )
10161016_copy (f, args:: SparseVecOrMat... ) = _diffshape_broadcast (f, args... )
10171017# Otherwise, we incorporate scalars into the function and re-dispatch
10181018function _copy (f, args... )
@@ -1112,8 +1112,8 @@ end
11121112end
11131113
11141114# NOTE: The following two method definitions work around #19096.
1115- broadcast (f:: Tf , :: Type{T} , A:: SparseMatrixCSC ) where {Tf,T} = broadcast (y -> f (T, y), A)
1116- broadcast (f:: Tf , A:: SparseMatrixCSC , :: Type{T} ) where {Tf,T} = broadcast (x -> f (x, T), A)
1115+ broadcast (f:: Tf , :: Type{T} , A:: AbstractSparseMatrixCSC ) where {Tf,T} = broadcast (y -> f (T, y), A)
1116+ broadcast (f:: Tf , A:: AbstractSparseMatrixCSC , :: Type{T} ) where {Tf,T} = broadcast (x -> f (x, T), A)
11171117
11181118
11191119# (11) broadcast[!] over combinations of scalars, sparse vectors/matrices, structured matrices,
@@ -1151,7 +1151,7 @@ _sparsifystructured(x) = x
11511151SparseOrStructuredMatrix = Union{SparseMatrixCSC,LinearAlgebra. StructuredMatrix}
11521152map (f:: Tf , A:: SparseOrStructuredMatrix , Bs:: Vararg{SparseOrStructuredMatrix,N} ) where {Tf,N} =
11531153 (_checksameshape (A, Bs... ); _noshapecheck_map (f, _sparsifystructured (A), map (_sparsifystructured, Bs)... ))
1154- map! (f:: Tf , C:: SparseMatrixCSC , A:: SparseOrStructuredMatrix , Bs:: Vararg{SparseOrStructuredMatrix,N} ) where {Tf,N} =
1154+ map! (f:: Tf , C:: AbstractSparseMatrixCSC , A:: SparseOrStructuredMatrix , Bs:: Vararg{SparseOrStructuredMatrix,N} ) where {Tf,N} =
11551155 (_checksameshape (C, A, Bs... ); _noshapecheck_map! (f, C, _sparsifystructured (A), map (_sparsifystructured, Bs)... ))
11561156
11571157end
0 commit comments