Skip to content

Commit

Permalink
oneunit of sparse matrix should return sparse matrix (#30228)
Browse files Browse the repository at this point in the history
* added sprandn methods with Type

* oneunit of sparse matrix should return sparse array

(cherry picked from commit 5c5489e)
  • Loading branch information
KlausC authored and KristofferC committed Dec 12, 2018
1 parent ca1b40e commit 2be2ebf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion stdlib/SparseArrays/src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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

0 comments on commit 2be2ebf

Please sign in to comment.