From 5c5489ea7d19ea93bb9239cec65644c6b95882b5 Mon Sep 17 00:00:00 2001 From: Klaus Crusius Date: Tue, 11 Dec 2018 12:56:49 +0100 Subject: [PATCH] oneunit of sparse matrix should return sparse matrix (#30228) * added sprandn methods with Type * oneunit of sparse matrix should return sparse array --- stdlib/SparseArrays/src/sparsematrix.jl | 3 ++- stdlib/SparseArrays/test/sparse.jl | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/stdlib/SparseArrays/src/sparsematrix.jl b/stdlib/SparseArrays/src/sparsematrix.jl index c4b335701400d..fe73825db4d18 100644 --- a/stdlib/SparseArrays/src/sparsematrix.jl +++ b/stdlib/SparseArrays/src/sparsematrix.jl @@ -1524,7 +1524,8 @@ function spzeros(::Type{Tv}, ::Type{Ti}, sz::Tuple{Integer,Integer}) where {Tv, spzeros(Tv, Ti, sz[1], sz[2]) end -function one(S::SparseMatrixCSC{T}) where T +import Base._one +function Base._one(unit::T, S::SparseMatrixCSC) where T S.m == S.n || throw(DimensionMismatch("multiplicative identity only defined for square matrices")) return SparseMatrixCSC{T}(I, S.m, S.n) end diff --git a/stdlib/SparseArrays/test/sparse.jl b/stdlib/SparseArrays/test/sparse.jl index 60bd97f23da87..4328d59592ce9 100644 --- a/stdlib/SparseArrays/test/sparse.jl +++ b/stdlib/SparseArrays/test/sparse.jl @@ -9,6 +9,7 @@ using Base.Printf: @printf using Random using Test: guardseed using InteractiveUtils: @which +using Dates @testset "issparse" begin @test issparse(sparse(fill(1,5,5))) @@ -2350,4 +2351,12 @@ end @test success(pipeline(cmd; stdout=stdout, stderr=stderr)) end +@testset "oneunit of sparse matrix" begin + A = sparse([Second(0) Second(0); Second(0) Second(0)]) + @test oneunit(sprand(2, 2, 0.5)) isa SparseMatrixCSC{Float64} + @test oneunit(A) isa SparseMatrixCSC{Second} + @test one(sprand(2, 2, 0.5)) isa SparseMatrixCSC{Float64} + @test one(A) isa SparseMatrixCSC{Int} +end + end # module