Skip to content

Commit 87d2550

Browse files
committed
Extend sparse map[!] to structured matrices by promoting structured matrices to sparse.
1 parent 230fba9 commit 87d2550

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

base/sparse/higherorderfns.jl

+13-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ using ..SparseArrays: SparseVector, SparseMatrixCSC, AbstractSparseArray, indtyp
2121
# (6) Define general _map_[not]zeropres! capable of handling >2 (input) sparse vectors/matrices.
2222
# (7) Define _broadcast_[not]zeropres! specialized for a pair of (input) sparse vectors/matrices.
2323
# (8) Define general _broadcast_[not]zeropres! capable of handling >2 (input) sparse vectors/matrices.
24-
# (9) Define methods handling combinations of broadcast scalars and sparse vectors/matrices.
25-
# (10) Define methods handling combinations of scalars, sparse vectors/matrices, and structured matrices.
24+
# (9) Define (broadcast[!]) methods handling combinations of broadcast scalars and sparse vectors/matrices.
25+
# (10) Define (broadcast[!]) methods handling combinations of scalars, sparse vectors/matrices, and structured matrices.
26+
# (11) Define (map[!]) methods handling combinations of sparse and structured matrices.
2627

2728

2829
# (1) The definitions below provide a common interface to sparse vectors and matrices
@@ -927,4 +928,14 @@ promote_containertype(::Type{Tuple}, ::Type{StructuredArray}) = Array
927928
@inline _sparsifystructured(A::AbstractSparseArray) = A
928929
@inline _sparsifystructured(x) = x
929930

931+
932+
# (11) map[!] over combinations of sparse and structured matrices
933+
StructuredMatrix = Union{Diagonal,Bidiagonal,Tridiagonal,SymTridiagonal}
934+
SparseOrStructuredMatrix = Union{SparseMatrixCSC,StructuredMatrix}
935+
map{Tf}(f::Tf, A::StructuredMatrix) = _noshapecheck_map(f, _sparsifystructured(A))
936+
map{Tf,N}(f::Tf, A::SparseOrStructuredMatrix, Bs::Vararg{SparseOrStructuredMatrix,N}) =
937+
(_checksameshape(A, Bs...); _noshapecheck_map(f, _sparsifystructured(A), map(_sparsifystructured, Bs)...))
938+
map!{Tf,N}(f::Tf, C::SparseMatrixCSC, A::SparseOrStructuredMatrix, Bs::Vararg{SparseOrStructuredMatrix,N}) =
939+
(_checksameshape(C, A, Bs...); _noshapecheck_map!(f, C, _sparsifystructured(A), map(_sparsifystructured, Bs)...))
940+
930941
end

test/sparse/higherorderfns.jl

+28
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,34 @@ end
317317
end
318318
end
319319

320+
@testset "map[!] over combinations of sparse and structured matrices" begin
321+
N, p = 10, 0.4
322+
A = sprand(N, N, p)
323+
Z, fA = copy(A), Array(A)
324+
D = Diagonal(rand(N))
325+
B = Bidiagonal(rand(N), rand(N - 1), true)
326+
T = Tridiagonal(rand(N - 1), rand(N), rand(N - 1))
327+
S = SymTridiagonal(rand(N), rand(N - 1))
328+
structuredarrays = (D, B, T, S)
329+
fstructuredarrays = map(Array, structuredarrays)
330+
for (X, fX) in zip(structuredarrays, fstructuredarrays)
331+
@test (Q = map(sin, X); Q isa SparseMatrixCSC && Q == sparse(map(sin, fX)))
332+
@test map!(sin, Z, X) == sparse(map(sin, fX))
333+
@test (Q = map(cos, X); Q isa SparseMatrixCSC && Q == sparse(map(cos, fX)))
334+
@test map!(cos, Z, X) == sparse(map(cos, fX))
335+
@test (Q = map(+, A, X); Q isa SparseMatrixCSC && Q == sparse(map(+, fA, fX)))
336+
@test map!(+, Z, A, X) == sparse(map(+, fA, fX))
337+
for (Y, fY) in zip(structuredarrays, fstructuredarrays)
338+
@test (Q = map(+, X, Y); Q isa SparseMatrixCSC && Q == sparse(map(+, fX, fY)))
339+
@test map!(+, Z, X, Y) == sparse(map(+, fX, fY))
340+
@test (Q = map(*, X, Y); Q isa SparseMatrixCSC && Q == sparse(map(*, fX, fY)))
341+
@test map!(*, Z, X, Y) == sparse(map(*, fX, fY))
342+
@test (Q = map(+, X, A, Y); Q isa SparseMatrixCSC && Q == sparse(map(+, fX, fA, fY)))
343+
@test map!(+, Z, X, A, Y) == sparse(map(+, fX, fA, fY))
344+
end
345+
end
346+
end
347+
320348
# Older tests of sparse broadcast, now largely covered by the tests above
321349
@testset "assorted tests of sparse broadcast over two input arguments" begin
322350
N, p = 10, 0.3

0 commit comments

Comments
 (0)