From 6d7539163020ad54d81a4055e88e547b5e44a199 Mon Sep 17 00:00:00 2001 From: Martin Holters Date: Mon, 20 Nov 2017 09:53:49 +0100 Subject: [PATCH] Add constructors for `Matrix{T}` and `SparseMatrixCSC{T}` from `UniformScaling` --- README.md | 3 +++ src/Compat.jl | 20 ++++++++++++++++++++ test/runtests.jl | 10 ++++++++++ 3 files changed, 33 insertions(+) diff --git a/README.md b/README.md index 3bd29ec01..ca8371f28 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]) +* Constructors for `Matrix{T}` and `SparseMatrixCSC{T}` from `UniformScaling` ([#24372]) + ## Renaming @@ -347,3 +349,4 @@ includes this fix. Find the minimum version from there. [#23931]: https://github.com/JuliaLang/julia/issues/23931 [#24282]: https://github.com/JuliaLang/julia/issues/24282 [#22512]: https://github.com/JuliaLang/julia/issues/22532 +[#24372]: https://github.com/JuliaLang/julia/issues/24372 diff --git a/src/Compat.jl b/src/Compat.jl index 28e7e0e17..0000c85ef 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -729,6 +729,26 @@ end export BitSet end +if VERSION < v"0.7.0-DEV.2377" + (::Type{Matrix{T}}){T}(s::UniformScaling, dims::Dims{2}) = setindex!(zeros(T, dims), T(s.λ), diagind(dims...)) + (::Type{Matrix{T}}){T}(s::UniformScaling, m::Integer, n::Integer) = Matrix{T}(s, Dims((m, n))) + + (::Type{SparseMatrixCSC{Tv,Ti}}){Tv,Ti}(s::UniformScaling, m::Integer, n::Integer) = SparseMatrixCSC{Tv,Ti}(s, Dims((m, n))) + (::Type{SparseMatrixCSC{Tv}}){Tv}(s::UniformScaling, m::Integer, n::Integer) = SparseMatrixCSC{Tv}(s, Dims((m, n))) + (::Type{SparseMatrixCSC{Tv}}){Tv}(s::UniformScaling, dims::Dims{2}) = SparseMatrixCSC{Tv,Int}(s, dims) + function (::Type{SparseMatrixCSC{Tv,Ti}}){Tv,Ti}(s::UniformScaling, dims::Dims{2}) + @boundscheck first(dims) < 0 && throw(ArgumentError("first dimension invalid ($(first(dims)) < 0)")) + @boundscheck last(dims) < 0 && throw(ArgumentError("second dimension invalid ($(last(dims)) < 0)")) + iszero(s.λ) && return spzeros(Tv, Ti, dims...) + m, n, k = dims..., min(dims...) + nzval = fill!(Vector{Tv}(k), Tv(s.λ)) + rowval = copy!(Vector{Ti}(k), 1:k) + colptr = copy!(Vector{Ti}(n + 1), 1:(k + 1)) + for i in (k + 2):(n + 1) colptr[i] = (k + 1) end + SparseMatrixCSC{Tv,Ti}(dims..., colptr, rowval, nzval) + end +end + include("deprecated.jl") end # module Compat diff --git a/test/runtests.jl b/test/runtests.jl index d2f77086d..b7be9fafd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -863,6 +863,16 @@ end # 0.7 @test 1 in BitSet(1:10) +# 0.7 +let a = [1 0 0; 0 1 0; 0 0 1] + @test Matrix{Int}(I, 3, 3) == a + @test Matrix{Float64}(I, 3, 2) == a[:,1:2] + @test SparseMatrixCSC{Int}(I, 3, 3) == a + @test SparseMatrixCSC{Float64}(I, 3, 2) == a[:,1:2] + @test SparseMatrixCSC{Bool,Int16}(I, 3, 3) == a + @test SparseMatrixCSC{Complex128,Int8}(I, 3, 2) == a[:,1:2] +end + if VERSION < v"0.6.0" include("deprecated.jl") end