Skip to content

Commit 6d75391

Browse files
committed
Add constructors for Matrix{T} and SparseMatrixCSC{T} from UniformScaling
1 parent fbd9cb2 commit 6d75391

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ Currently, the `@compat` macro supports the following syntaxes:
180180

181181
* `*(::Union{Char,AbstractString},::Union{Char,AbstractString})` concatenation. ([#22512])
182182

183+
* Constructors for `Matrix{T}` and `SparseMatrixCSC{T}` from `UniformScaling` ([#24372])
184+
183185
## Renaming
184186

185187

@@ -347,3 +349,4 @@ includes this fix. Find the minimum version from there.
347349
[#23931]: https://github.com/JuliaLang/julia/issues/23931
348350
[#24282]: https://github.com/JuliaLang/julia/issues/24282
349351
[#22512]: https://github.com/JuliaLang/julia/issues/22532
352+
[#24372]: https://github.com/JuliaLang/julia/issues/24372

src/Compat.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,26 @@ end
729729
export BitSet
730730
end
731731

732+
if VERSION < v"0.7.0-DEV.2377"
733+
(::Type{Matrix{T}}){T}(s::UniformScaling, dims::Dims{2}) = setindex!(zeros(T, dims), T(s.λ), diagind(dims...))
734+
(::Type{Matrix{T}}){T}(s::UniformScaling, m::Integer, n::Integer) = Matrix{T}(s, Dims((m, n)))
735+
736+
(::Type{SparseMatrixCSC{Tv,Ti}}){Tv,Ti}(s::UniformScaling, m::Integer, n::Integer) = SparseMatrixCSC{Tv,Ti}(s, Dims((m, n)))
737+
(::Type{SparseMatrixCSC{Tv}}){Tv}(s::UniformScaling, m::Integer, n::Integer) = SparseMatrixCSC{Tv}(s, Dims((m, n)))
738+
(::Type{SparseMatrixCSC{Tv}}){Tv}(s::UniformScaling, dims::Dims{2}) = SparseMatrixCSC{Tv,Int}(s, dims)
739+
function (::Type{SparseMatrixCSC{Tv,Ti}}){Tv,Ti}(s::UniformScaling, dims::Dims{2})
740+
@boundscheck first(dims) < 0 && throw(ArgumentError("first dimension invalid ($(first(dims)) < 0)"))
741+
@boundscheck last(dims) < 0 && throw(ArgumentError("second dimension invalid ($(last(dims)) < 0)"))
742+
iszero(s.λ) && return spzeros(Tv, Ti, dims...)
743+
m, n, k = dims..., min(dims...)
744+
nzval = fill!(Vector{Tv}(k), Tv(s.λ))
745+
rowval = copy!(Vector{Ti}(k), 1:k)
746+
colptr = copy!(Vector{Ti}(n + 1), 1:(k + 1))
747+
for i in (k + 2):(n + 1) colptr[i] = (k + 1) end
748+
SparseMatrixCSC{Tv,Ti}(dims..., colptr, rowval, nzval)
749+
end
750+
end
751+
732752
include("deprecated.jl")
733753

734754
end # module Compat

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,16 @@ end
863863
# 0.7
864864
@test 1 in BitSet(1:10)
865865

866+
# 0.7
867+
let a = [1 0 0; 0 1 0; 0 0 1]
868+
@test Matrix{Int}(I, 3, 3) == a
869+
@test Matrix{Float64}(I, 3, 2) == a[:,1:2]
870+
@test SparseMatrixCSC{Int}(I, 3, 3) == a
871+
@test SparseMatrixCSC{Float64}(I, 3, 2) == a[:,1:2]
872+
@test SparseMatrixCSC{Bool,Int16}(I, 3, 3) == a
873+
@test SparseMatrixCSC{Complex128,Int8}(I, 3, 2) == a[:,1:2]
874+
end
875+
866876
if VERSION < v"0.6.0"
867877
include("deprecated.jl")
868878
end

0 commit comments

Comments
 (0)