@@ -53,3 +53,66 @@ function getunstoredindex(a::ReplacedUnstoredSparseArray, I::Int...)
5353end
5454eachstoredindex (a:: ReplacedUnstoredSparseArray ) = eachstoredindex (parent (a))
5555@derive ReplacedUnstoredSparseArray AbstractArrayOps
56+
57+ # Special-purpose constructors
58+ # ----------------------------
59+ using Random: Random, AbstractRNG, default_rng
60+
61+ @doc """
62+ sparsezeros([T::Type], dims) -> A::SparseArrayDOK{T}
63+
64+ Create an empty size `dims` sparse array.
65+ The optional `T` argument specifies the element type, which defaults to `Float64`.
66+ """ sparsezeros
67+
68+ sparsezeros (dims:: Dims ) = sparsezeros (Float64, dims)
69+ sparsezeros (:: Type{T} , dims:: Dims ) where {T} = SparseArrayDOK {T} (undef, dims)
70+
71+ @doc """
72+ sparserand([rng], [T::Type], dims; density::Real=0.5, randfun::Function=rand) -> A::SparseArrayDOK{T}
73+
74+ Create a random size `dims` sparse array in which the probability of any element being stored is independently given by `density`.
75+ The optional `rng` argument specifies a random number generator, see also `Random`.
76+ The optional `T` argument specifies the element type, which defaults to `Float64`.
77+ The optional `randfun` argument can be used to control the type of random elements, and should support
78+ the signature `randfun(rng, T, N)` to generate `N` entries of type `T`.
79+
80+
81+ See also [`sparserand!`](@ref).
82+ """ sparserand
83+
84+ function sparserand (:: Type{T} , dims:: Dims ; kwargs... ) where {T}
85+ return sparserand (default_rng (), T, dims; kwargs... )
86+ end
87+ sparserand (dims:: Dims ; kwargs... ) = sparserand (default_rng (), Float64, dims; kwargs... )
88+ function sparserand (rng:: AbstractRNG , dims:: Dims ; kwargs... )
89+ return sparserand (rng, Float64, dims; kwargs... )
90+ end
91+ function sparserand (rng:: AbstractRNG , :: Type{T} , dims:: Dims ; kwargs... ) where {T}
92+ A = SparseArrayDOK {T} (undef, dims)
93+ sparserand! (rng, A; kwargs... )
94+ return A
95+ end
96+
97+ @doc """
98+ sparserand!([rng], A::AbstractArray; density::Real=0.5, randfun::Function=rand) -> A
99+
100+ Overwrite part of an array with random entries, where the probability of overwriting is independently given by `density`.
101+ The optional `rng` argument specifies a random number generator, see also `Random`.
102+ The optional `randfun` argument can be used to control the type of random elements, and should support
103+ the signature `randfun(rng, T, N)` to generate `N` entries of type `T`.
104+
105+ See also [`sparserand`](@ref).
106+ """ sparserand!
107+
108+ sparserand! (A:: AbstractArray ; kwargs... ) = sparserand! (default_rng (), A; kwargs... )
109+ function sparserand! (
110+ rng:: AbstractRNG , A:: AbstractArray ; density:: Real = 0.5 , randfun:: Function = Random. rand
111+ )
112+ ArrayLayouts. zero! (A)
113+ rand_inds = Random. randsubseq (rng, eachindex (A), density)
114+ rand_entries = randfun (rng, eltype (A), length (rand_inds))
115+ @inbounds for (I, v) in zip (rand_inds, rand_entries)
116+ A[I] = v
117+ end
118+ end
0 commit comments