Skip to content

Commit f997d6f

Browse files
committed
Extend sparse broadcast! to structured matrices by promoting structured matrices to sparse.
1 parent a630a2f commit f997d6f

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

base/broadcast.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ arguments to `f` unless it is also listed in the `As`,
203203
as in `broadcast!(f, A, A, B)` to perform `A[:] = broadcast(f, A, B)`.
204204
"""
205205
@inline broadcast!{N}(f, C::AbstractArray, A, Bs::Vararg{Any,N}) =
206-
broadcast_c!(f, containertype(C, A, Bs...), C, A, Bs...)
207-
@inline function broadcast_c!{N}(f, ::Type, C::AbstractArray, A, Bs::Vararg{Any,N})
206+
broadcast_c!(f, containertype(C), containertype(A, Bs...), C, A, Bs...)
207+
@inline function broadcast_c!{N}(f, ::Type, ::Type, C, A, Bs::Vararg{Any,N})
208208
shape = indices(C)
209209
@boundscheck check_broadcast_indices(shape, A, Bs...)
210210
keeps, Idefaults = map_newindexer(shape, A, Bs)

base/sparse/higherorderfns.jl

+7-2
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ promote_containertype(::Type{Tuple}, ::Type{AbstractSparseArray}) = Array
854854
promote_containertype(::Type{AbstractSparseArray}, ::Type{Array}) = Array
855855
promote_containertype(::Type{AbstractSparseArray}, ::Type{Tuple}) = Array
856856

857-
# broadcast[!] entry points for combinations of sparse arrays and other types
857+
# broadcast[!] entry points for combinations of sparse arrays and other (scalar) types
858858
@inline function broadcast_c{N}(f, ::Type{AbstractSparseArray}, mixedargs::Vararg{Any,N})
859859
parevalf, passedargstup = capturescalars(f, mixedargs)
860860
return broadcast(parevalf, passedargstup...)
@@ -914,7 +914,12 @@ promote_containertype(::Type{Tuple}, ::Type{StructuredArray}) = Array
914914

915915
# for combinations involving sparse/structured arrays and scalars only,
916916
# promote all structured arguments to sparse and then rebroadcast
917-
broadcast_c{N,Tf}(f::Tf, ::Type{StructuredArray}, As::Vararg{Any,N}) = broadcast(f, map(_sparsifystructured, As)...)
917+
@inline broadcast_c{N}(f, ::Type{StructuredArray}, As::Vararg{Any,N}) =
918+
broadcast(f, map(_sparsifystructured, As)...)
919+
@inline broadcast_c!{N}(f, ::Type{AbstractSparseArray}, ::Type{StructuredArray}, C, B, As::Vararg{Any,N}) =
920+
broadcast!(f, C, _sparsifystructured(B), map(_sparsifystructured, As)...)
921+
@inline broadcast_c!{N}(f, CT::Type, ::Type{StructuredArray}, C, B, As::Vararg{Any,N}) =
922+
broadcast_c!(f, CT, Array, C, B, As...)
918923
@inline _sparsifystructured(S::SymTridiagonal) = SparseMatrixCSC(S)
919924
@inline _sparsifystructured(T::Tridiagonal) = SparseMatrixCSC(T)
920925
@inline _sparsifystructured(B::Bidiagonal) = SparseMatrixCSC(B)

0 commit comments

Comments
 (0)