Skip to content

Commit 2be2ebf

Browse files
KlausCKristofferC
authored andcommitted
oneunit of sparse matrix should return sparse matrix (#30228)
* added sprandn methods with Type * oneunit of sparse matrix should return sparse array (cherry picked from commit 5c5489e)
1 parent ca1b40e commit 2be2ebf

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

stdlib/SparseArrays/src/sparsematrix.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,8 @@ function spzeros(::Type{Tv}, ::Type{Ti}, sz::Tuple{Integer,Integer}) where {Tv,
15241524
spzeros(Tv, Ti, sz[1], sz[2])
15251525
end
15261526

1527-
function one(S::SparseMatrixCSC{T}) where T
1527+
import Base._one
1528+
function Base._one(unit::T, S::SparseMatrixCSC) where T
15281529
S.m == S.n || throw(DimensionMismatch("multiplicative identity only defined for square matrices"))
15291530
return SparseMatrixCSC{T}(I, S.m, S.n)
15301531
end

stdlib/SparseArrays/test/sparse.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ using Base.Printf: @printf
99
using Random
1010
using Test: guardseed
1111
using InteractiveUtils: @which
12+
using Dates
1213

1314
@testset "issparse" begin
1415
@test issparse(sparse(fill(1,5,5)))
@@ -2350,4 +2351,12 @@ end
23502351
@test success(pipeline(cmd; stdout=stdout, stderr=stderr))
23512352
end
23522353

2354+
@testset "oneunit of sparse matrix" begin
2355+
A = sparse([Second(0) Second(0); Second(0) Second(0)])
2356+
@test oneunit(sprand(2, 2, 0.5)) isa SparseMatrixCSC{Float64}
2357+
@test oneunit(A) isa SparseMatrixCSC{Second}
2358+
@test one(sprand(2, 2, 0.5)) isa SparseMatrixCSC{Float64}
2359+
@test one(A) isa SparseMatrixCSC{Int}
2360+
end
2361+
23532362
end # module

0 commit comments

Comments
 (0)