@@ -43,40 +43,45 @@ The optional `T` argument specifies the element type, which defualts to `Float64
43
43
spzeros (dims:: Dims ) = spzeros (Float64, dims)
44
44
spzeros (:: Type{T} , dims:: Dims ) where {T} = SparseArrayDOK {T} (undef, dims)
45
45
46
+ @doc """
47
+ sprand([rng], [T::Type], dims; density::Real=0.5, rfn::Function=rand) -> A::SparseArrayDOK{T}
48
+
46
49
Create a random size `dims` sparse array in which the probability of any element being stored is independently given by `density`.
47
50
The optional `rng` argument specifies a random number generator, see also `Random`.
48
51
The optional `T` argument specifies the element type, which defaults to `Float64`.
49
52
50
53
See also [`sprand!`](@ref).
51
54
""" sprand
52
55
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 ... )
55
58
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 ... )
59
62
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}
61
64
A = SparseArrayDOK {T} (undef, dims)
62
- sprand!(rng, A; density )
65
+ sprand! (rng, A; kwargs ... )
63
66
return A
64
67
end
65
68
66
69
@doc """
67
- sprand! ([rng], A:: AbstractArray ; density:: Real = 0.5 ) -> A
70
+ sprand!([rng], A::AbstractArray; density::Real=0.5, rfn::Function=rand ) -> A
68
71
69
72
Overwrite part of an array with random entries, where the probability of overwriting is independently given by `density`.
70
73
The optional `rng` argument specifies a random number generator, see also `Random`.
71
74
72
75
See also [`sprand`](@ref).
73
76
""" sprand!
74
77
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)
77
83
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))
80
85
for (I, v) in zip (rand_inds, rand_entries)
81
86
A[I] = v
82
87
end
0 commit comments