diff --git a/README.md b/README.md index 74dcd7978e822..d5bc83133417c 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,8 @@ Currently, the `@compat` macro supports the following syntaxes: * `*(::Union{Char,AbstractString},::Union{Char,AbstractString})` concatenation. ([#22512]) +* `diagm` and `spdiagm` accept pairs mapping diagonals to vectors ([#24047], [#23757]) + ## Renaming @@ -347,6 +349,8 @@ includes this fix. Find the minimum version from there. [#23570]: https://github.com/JuliaLang/julia/issues/23570 [#23666]: https://github.com/JuliaLang/julia/issues/23666 [#23667]: https://github.com/JuliaLang/julia/issues/23667 +[#23757]: https://github.com/JuliaLang/julia/issues/23757 [#23812]: https://github.com/JuliaLang/julia/issues/23812 [#23931]: https://github.com/JuliaLang/julia/issues/23931 +[#24047]: https://github.com/JuliaLang/julia/issues/24047 [#24282]: https://github.com/JuliaLang/julia/issues/24282 diff --git a/src/Compat.jl b/src/Compat.jl index 4f8f4179d938e..2f71fd4924215 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -736,6 +736,33 @@ end export textwidth end +# 0.7.0-DEV.2116 +@static if VERSION < v"0.7.0-DEV.2116" + import Base: spdiagm + function spdiagm(kv::Pair...) + I, J, V = Base.SparseArrays.spdiagm_internal(last.(kv), first.(kv)) + m = max(Base.SparseArrays.dimlub(I), Base.SparseArrays.dimlub(J)) + return sparse(I, J, V, m, m) + end +end + +# 0.7.0-DEV.2161 +@static if VERSION < v"0.7.0-DEV.2161" + import Base: diagm + function diagm(kv::Pair...) + T = promote_type(map(x -> eltype(x.second), kv)...) + n = mapreduce(x -> length(x.second) + abs(x.first), max, kv) + A = zeros(T, n, n) + for p in kv + inds = diagind(A, p.first) + for (i, val) in enumerate(p.second) + A[inds[i]] += val + end + end + return A + end +end + include("deprecated.jl") end # module Compat diff --git a/test/runtests.jl b/test/runtests.jl index e044f7624acd0..d39b31280f4dc 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -867,6 +867,12 @@ end @test textwidth("A") == 1 @test textwidth('A') == 1 +# 0.7 +@test diagm(0 => ones(2), -1 => ones(2)) == [1.0 0.0 0.0; 1.0 1.0 0.0; 0.0 1.0 0.0] +@test diagm(0 => ones(2), 1 => ones(2)) == [1.0 1.0 0.0; 0.0 1.0 1.0; 0.0 0.0 0.0] +@test spdiagm(0 => ones(2), -1 => ones(2)) == [1.0 0.0 0.0; 1.0 1.0 0.0; 0.0 1.0 0.0] +@test spdiagm(0 => ones(2), 1 => ones(2)) == [1.0 1.0 0.0; 0.0 1.0 1.0; 0.0 0.0 0.0] + if VERSION < v"0.6.0" include("deprecated.jl") end