Skip to content

Constructors #33

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 24 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 4 additions & 2 deletions src/SparseArraysBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export SparseArrayDOK,
oneelement,
storedlength,
storedpairs,
storedvalues
export spzeros, sprand, sprand!
storedvalues,
spzeros,
sprand,
sprand!

include("abstractsparsearrayinterface.jl")
include("sparsearrayinterface.jl")
Expand Down
18 changes: 9 additions & 9 deletions src/abstractsparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,28 @@
The optional `T` argument specifies the element type, which defaults to `Float64`.
""" spzeros

spzeros(dims::Dims) = spzeros(Float64, dims)
spzeros(::Type{T}, dims::Dims) where {T} = SparseArrayDOK{T}(undef, dims)

Check warning on line 69 in src/abstractsparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearray.jl#L68-L69

Added lines #L68 - L69 were not covered by tests

@doc """
sprand([rng], [T::Type], dims; density::Real=0.5, rand_function::Function=rand) -> A::SparseArrayDOK{T}
sprand([rng], [T::Type], dims; density::Real=0.5, randfun::Function=rand) -> A::SparseArrayDOK{T}

Create a random size `dims` sparse array in which the probability of any element being stored is independently given by `density`.
The optional `rng` argument specifies a random number generator, see also `Random`.
The optional `T` argument specifies the element type, which defaults to `Float64`.
The optional `rand_function` argument can be used to control the type of random elements, and should support
the signature `rand_function(rng, T, N)` to generate `N` entries of type `T`.
The optional `randfun` argument can be used to control the type of random elements, and should support
the signature `randfun(rng, T, N)` to generate `N` entries of type `T`.


See also [`sprand!`](@ref).
""" sprand

function sprand(::Type{T}, dims::Dims; kwargs...) where {T}
return sprand(default_rng(), T, dims; kwargs...)

Check warning on line 85 in src/abstractsparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearray.jl#L84-L85

Added lines #L84 - L85 were not covered by tests
end
sprand(dims::Dims; kwargs...) = sprand(default_rng(), Float64, dims; kwargs...)
function sprand(rng::AbstractRNG, dims::Dims; kwargs...)
return sprand(rng, Float64, dims; kwargs...)

Check warning on line 89 in src/abstractsparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearray.jl#L87-L89

Added lines #L87 - L89 were not covered by tests
end
function sprand(rng::AbstractRNG, ::Type{T}, dims::Dims; kwargs...) where {T}
A = SparseArrayDOK{T}(undef, dims)
Expand All @@ -95,24 +95,24 @@
end

@doc """
sprand!([rng], A::AbstractArray; density::Real=0.5, rand_function::Function=rand) -> A
sprand!([rng], A::AbstractArray; density::Real=0.5, randfun::Function=rand) -> A

Overwrite part of an array with random entries, where the probability of overwriting is independently given by `density`.
The optional `rng` argument specifies a random number generator, see also `Random`.
The optional `rand_function` argument can be used to control the type of random elements, and should support
the signature `rand_function(rng, T, N)` to generate `N` entries of type `T`.
The optional `randfun` argument can be used to control the type of random elements, and should support
the signature `randfun(rng, T, N)` to generate `N` entries of type `T`.

See also [`sprand`](@ref).
""" sprand!

sprand!(A::AbstractArray; kwargs...) = sprand!(default_rng(), A; kwargs...)

Check warning on line 108 in src/abstractsparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearray.jl#L108

Added line #L108 was not covered by tests
function sprand!(
rng::AbstractRNG, A::AbstractArray; density::Real=0.5, rand_function::Function=Random.rand
rng::AbstractRNG, A::AbstractArray; density::Real=0.5, randfun::Function=Random.rand
)
ArrayLayouts.zero!(A)
rand_inds = Random.randsubseq(rng, eachindex(A), density)
rand_entries = rand_function(rng, eltype(A), length(rand_inds))
for (I, v) in zip(rand_inds, rand_entries)
rand_entries = randfun(rng, eltype(A), length(rand_inds))
@inbounds for (I, v) in zip(rand_inds, rand_entries)
A[I] = v
end
end
Loading