Skip to content

Commit a0f8445

Browse files
committed
update sprand to take in rfn function
1 parent 8e23309 commit a0f8445

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/abstractsparsearrayinterface.jl

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,40 +43,45 @@ The optional `T` argument specifies the element type, which defualts to `Float64
4343
spzeros(dims::Dims) = spzeros(Float64, dims)
4444
spzeros(::Type{T}, dims::Dims) where {T} = SparseArrayDOK{T}(undef, dims)
4545

46+
@doc """
47+
sprand([rng], [T::Type], dims; density::Real=0.5, rfn::Function=rand) -> A::SparseArrayDOK{T}
48+
4649
Create a random size `dims` sparse array in which the probability of any element being stored is independently given by `density`.
4750
The optional `rng` argument specifies a random number generator, see also `Random`.
4851
The optional `T` argument specifies the element type, which defaults to `Float64`.
4952
5053
See also [`sprand!`](@ref).
5154
""" sprand
5255

53-
function sprand(::Type{T}, dims::Dims; density::Real=0.5) where {T}
54-
return sprand(default_rng(), T, dims; density)
56+
function sprand(::Type{T}, dims::Dims; kwargs...) where {T}
57+
return sprand(default_rng(), T, dims; kwargs...)
5558
end
56-
sprand(dims::Dims; density::Real=0.5) = sprand(default_rng(), Float64, dims; density)
57-
function sprand(rng::AbstractRNG, dims::Dims; density::Real=0.5)
58-
return sprand(rng, Float64, dims; density)
59+
sprand(dims::Dims; kwargs...) = sprand(default_rng(), Float64, dims; kwargs...)
60+
function sprand(rng::AbstractRNG, dims::Dims; kwargs...)
61+
return sprand(rng, Float64, dims; kwargs...)
5962
end
60-
function sprand(rng::AbstractRNG, ::Type{T}, dims::Dims; density::Real=0.5) where {T}
63+
function sprand(rng::AbstractRNG, ::Type{T}, dims::Dims; kwargs...) where {T}
6164
A = SparseArrayDOK{T}(undef, dims)
62-
sprand!(rng, A; density)
65+
sprand!(rng, A; kwargs...)
6366
return A
6467
end
6568

6669
@doc """
67-
sprand!([rng], A::AbstractArray; density::Real=0.5) -> A
70+
sprand!([rng], A::AbstractArray; density::Real=0.5, rfn::Function=rand) -> A
6871
6972
Overwrite part of an array with random entries, where the probability of overwriting is independently given by `density`.
7073
The optional `rng` argument specifies a random number generator, see also `Random`.
7174
7275
See also [`sprand`](@ref).
7376
""" sprand!
7477

75-
sprand!(A::AbstractArray; density::Real=0.5) = sprand!(default_rng(), A; density)
76-
function sprand!(rng::AbstractRNG, A::AbstractArray; density::Real=0.5)
78+
sprand!(A::AbstractArray; kwargs...) = sprand!(default_rng(), A; kwargs...)
79+
function sprand!(
80+
rng::AbstractRNG, A::AbstractArray; density::Real=0.5, rfn::Function=Random.rand
81+
)
82+
ArrayLayouts.zero!(A)
7783
rand_inds = Random.randsubseq(rng, eachindex(A), density)
78-
rand_entries = rand(rng, eltype(A), length(rand_inds))
79-
84+
rand_entries = rfn(rng, eltype(A), length(rand_inds))
8085
for (I, v) in zip(rand_inds, rand_entries)
8186
A[I] = v
8287
end

0 commit comments

Comments
 (0)