Skip to content

Commit d31145b

Browse files
committed
Extend sparse broadcast! to combinations of broadcast scalars and sparse vectors/matrices.
Makes broadcast! dispatch on container type (as broadcast), and inject generic sparse broadcast! for the appropriate container type.
1 parent 18df7c5 commit d31145b

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

base/broadcast.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ Note that `dest` is only used to store the result, and does not supply
190190
arguments to `f` unless it is also listed in the `As`,
191191
as in `broadcast!(f, A, A, B)` to perform `A[:] = broadcast(f, A, B)`.
192192
"""
193-
@inline function broadcast!{nargs}(f, B::AbstractArray, As::Vararg{Any,nargs})
193+
@inline broadcast!(f, B, As...) = broadcast!_c(f, containertype(B, As...), B, As...)
194+
@inline function broadcast!_c{nargs}(f, ::Type, B::AbstractArray, As::Vararg{Any,nargs})
194195
shape = indices(B)
195196
check_broadcast_indices(shape, As...)
196197
keeps, Idefaults = map_newindexer(shape, As)

base/sparse/higherorderfns.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module HigherOrderFns
55
# This module provides higher order functions specialized for sparse arrays,
66
# particularly map[!]/broadcast[!] for SparseVectors and SparseMatrixCSCs at present.
77
import Base: map, map!, broadcast, broadcast!
8-
import Base.Broadcast: containertype, promote_containertype, broadcast_indices, broadcast_c
8+
import Base.Broadcast: containertype, promote_containertype,
9+
broadcast_indices, broadcast_c, broadcast!_c
910

1011
using Base: front, tail, to_shape
1112
using ..SparseArrays: SparseVector, SparseMatrixCSC, AbstractSparseArray, indtype
@@ -850,11 +851,15 @@ promote_containertype(::Type{Tuple}, ::Type{AbstractSparseArray}) = Array
850851
promote_containertype(::Type{AbstractSparseArray}, ::Type{Array}) = Array
851852
promote_containertype(::Type{AbstractSparseArray}, ::Type{Tuple}) = Array
852853

853-
# broadcast entry point for combinations of sparse arrays and other types
854-
function broadcast_c(f, ::Type{AbstractSparseArray}, mixedargs...)
854+
# broadcast[!] entry points for combinations of sparse arrays and other types
855+
@inline function broadcast_c{N}(f, ::Type{AbstractSparseArray}, mixedargs::Vararg{Any,N})
855856
parevalf, passedargstup = capturescalars(f, mixedargs)
856857
return broadcast(parevalf, passedargstup...)
857858
end
859+
@inline function broadcast!_c{N}(f, ::Type{AbstractSparseArray}, dest::SparseVecOrMat, mixedsrcargs::Vararg{Any,N})
860+
parevalf, passedsrcargstup = capturescalars(f, mixedsrcargs)
861+
return broadcast!(parevalf, dest, passedsrcargstup...)
862+
end
858863
# capturescalars takes a function (f) and a tuple of mixed sparse vectors/matrices and
859864
# broadcast scalar arguments (mixedargs), and returns a function (parevalf) and a reduced
860865
# argument tuple (passedargstup) containing only the sparse vectors/matrices in mixedargs

0 commit comments

Comments
 (0)