Skip to content

Commit

Permalink
Add (sp)diagm pair constructors (#408)
Browse files Browse the repository at this point in the history
* Add (sp)diagm pair constructors
  • Loading branch information
ararslan authored and fredrikekre committed Nov 20, 2017
1 parent 4558247 commit a419b7c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
27 changes: 27 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a419b7c

Please sign in to comment.