Skip to content

oneunit of sparse matrix should return sparse matrix #30228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion stdlib/SparseArrays/src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1521,7 +1521,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
10 changes: 9 additions & 1 deletion 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 @@ -2335,7 +2336,6 @@ end
@test m2.module == SparseArrays
end


@testset "sprandn with type $T" for T in (Float64, Float32, Float16, ComplexF64, ComplexF32, ComplexF16)
@test sprandn(T, 5, 5, 0.5) isa AbstractSparseMatrix{T}
end
Expand All @@ -2351,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