From 83ab91d32e8dc9ee548fe529d1382fdb1ad3ef3d Mon Sep 17 00:00:00 2001 From: Martin Holters Date: Mon, 20 Nov 2017 09:53:49 +0100 Subject: [PATCH 1/2] 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 d5bc83133..e8f48ddd2 100644 --- a/README.md +++ b/README.md @@ -182,6 +182,8 @@ Currently, the `@compat` macro supports the following syntaxes: * `diagm` and `spdiagm` accept pairs mapping diagonals to vectors ([#24047], [#23757]) +* Constructors for `Matrix{T}` and `SparseMatrixCSC{T}` from `UniformScaling` ([#24372]) + ## Renaming @@ -354,3 +356,4 @@ includes this fix. Find the minimum version from there. [#23931]: https://github.com/JuliaLang/julia/issues/23931 [#24047]: https://github.com/JuliaLang/julia/issues/24047 [#24282]: https://github.com/JuliaLang/julia/issues/24282 +[#24372]: https://github.com/JuliaLang/julia/issues/24372 diff --git a/src/Compat.jl b/src/Compat.jl index 2f71fd492..c48f3b291 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -763,6 +763,26 @@ end end 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 d39b31280..3ef38d9ac 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -873,6 +873,16 @@ end @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] +# 0.7 +let a = [1 0 0; 0 1 0; 0 0 1] + @test Matrix{Int}(I, 3, 3)::Matrix{Int} == a + @test Matrix{Float64}(I, (3, 2))::Matrix{Float64} == a[:,1:2] + @test SparseMatrixCSC{Int}(I, 3, 3)::SparseMatrixCSC{Int,Int} == a + @test SparseMatrixCSC{Float64}(I, (3, 2))::SparseMatrixCSC{Float64,Int} == a[:,1:2] + @test SparseMatrixCSC{Bool,Int16}(I, (3, 3))::SparseMatrixCSC{Bool,Int16} == a + @test SparseMatrixCSC{Complex128,Int8}(I, 3, 2)::SparseMatrixCSC{Complex128,Int8} == a[:,1:2] +end + if VERSION < v"0.6.0" include("deprecated.jl") end From 060d58331f7a7f9377f9f2c125c92f39cabb1b92 Mon Sep 17 00:00:00 2001 From: Martin Holters Date: Tue, 21 Nov 2017 08:31:50 +0100 Subject: [PATCH 2/2] Add constructors for `Array{T}` from `UniformScaling` --- README.md | 3 ++- src/Compat.jl | 4 ++++ test/runtests.jl | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e8f48ddd2..ff906169e 100644 --- a/README.md +++ b/README.md @@ -182,7 +182,7 @@ Currently, the `@compat` macro supports the following syntaxes: * `diagm` and `spdiagm` accept pairs mapping diagonals to vectors ([#24047], [#23757]) -* Constructors for `Matrix{T}` and `SparseMatrixCSC{T}` from `UniformScaling` ([#24372]) +* Constructors for `Matrix{T}`, `Array{T}`, and `SparseMatrixCSC{T}` from `UniformScaling` ([#24372], [#24657]) ## Renaming @@ -357,3 +357,4 @@ includes this fix. Find the minimum version from there. [#24047]: https://github.com/JuliaLang/julia/issues/24047 [#24282]: https://github.com/JuliaLang/julia/issues/24282 [#24372]: https://github.com/JuliaLang/julia/issues/24372 +[#24657]: https://github.com/JuliaLang/julia/issues/24657 diff --git a/src/Compat.jl b/src/Compat.jl index c48f3b291..f3ce0992f 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -782,6 +782,10 @@ if VERSION < v"0.7.0-DEV.2377" SparseMatrixCSC{Tv,Ti}(dims..., colptr, rowval, nzval) end end +if VERSION < v"0.7.0-DEV.2543" + (::Type{Array{T}}){T}(s::UniformScaling, dims::Dims{2}) = Matrix{T}(s, dims) + (::Type{Array{T}}){T}(s::UniformScaling, m::Integer, n::Integer) = Matrix{T}(s, m, n) +end include("deprecated.jl") diff --git a/test/runtests.jl b/test/runtests.jl index 3ef38d9ac..608ae182c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -877,6 +877,8 @@ end let a = [1 0 0; 0 1 0; 0 0 1] @test Matrix{Int}(I, 3, 3)::Matrix{Int} == a @test Matrix{Float64}(I, (3, 2))::Matrix{Float64} == a[:,1:2] + @test Array{Int}(I, (3, 3))::Matrix{Int} == a + @test Array{Float64}(I, 3, 2)::Matrix{Float64} == a[:,1:2] @test SparseMatrixCSC{Int}(I, 3, 3)::SparseMatrixCSC{Int,Int} == a @test SparseMatrixCSC{Float64}(I, (3, 2))::SparseMatrixCSC{Float64,Int} == a[:,1:2] @test SparseMatrixCSC{Bool,Int16}(I, (3, 3))::SparseMatrixCSC{Bool,Int16} == a