Skip to content

Commit 7959946

Browse files
committed
move functions
1 parent e4b3ae4 commit 7959946

File tree

2 files changed

+60
-57
lines changed

2 files changed

+60
-57
lines changed

src/abstractsparsearray.jl

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,63 @@ function getunstoredindex(a::ReplacedUnstoredSparseArray, I::Int...)
5353
end
5454
eachstoredindex(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+
spzeros([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+
""" spzeros
67+
68+
spzeros(dims::Dims) = spzeros(Float64, dims)
69+
spzeros(::Type{T}, dims::Dims) where {T} = SparseArrayDOK{T}(undef, dims)
70+
71+
@doc """
72+
sprand([rng], [T::Type], dims; density::Real=0.5, rfn::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 `rfn` argument can be used to control the type of random elements.
78+
79+
See also [`sprand!`](@ref).
80+
""" sprand
81+
82+
function sprand(::Type{T}, dims::Dims; kwargs...) where {T}
83+
return sprand(default_rng(), T, dims; kwargs...)
84+
end
85+
sprand(dims::Dims; kwargs...) = sprand(default_rng(), Float64, dims; kwargs...)
86+
function sprand(rng::AbstractRNG, dims::Dims; kwargs...)
87+
return sprand(rng, Float64, dims; kwargs...)
88+
end
89+
function sprand(rng::AbstractRNG, ::Type{T}, dims::Dims; kwargs...) where {T}
90+
A = SparseArrayDOK{T}(undef, dims)
91+
sprand!(rng, A; kwargs...)
92+
return A
93+
end
94+
95+
@doc """
96+
sprand!([rng], A::AbstractArray; density::Real=0.5, rfn::Function=rand) -> A
97+
98+
Overwrite part of an array with random entries, where the probability of overwriting is independently given by `density`.
99+
The optional `rng` argument specifies a random number generator, see also `Random`.
100+
The optional `rfn` argument can be used to control the type of random elements.
101+
102+
See also [`sprand`](@ref).
103+
""" sprand!
104+
105+
sprand!(A::AbstractArray; kwargs...) = sprand!(default_rng(), A; kwargs...)
106+
function sprand!(
107+
rng::AbstractRNG, A::AbstractArray; density::Real=0.5, rfn::Function=Random.rand
108+
)
109+
ArrayLayouts.zero!(A)
110+
rand_inds = Random.randsubseq(rng, eachindex(A), density)
111+
rand_entries = rfn(rng, eltype(A), length(rand_inds))
112+
for (I, v) in zip(rand_inds, rand_entries)
113+
A[I] = v
114+
end
115+
end

src/abstractsparsearrayinterface.jl

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ using DerivableInterfaces: DerivableInterfaces, @derive, @interface, AbstractArr
33
# This is to bring `ArrayLayouts.zero!` into the namespace
44
# since it is considered part of the sparse array interface.
55
using ArrayLayouts: zero!
6-
using Random: Random, AbstractRNG, default_rng
76

87
function eachstoredindex end
98
function getstoredindex end
@@ -33,62 +32,6 @@ function densearray(a::AbstractArray)
3332
return Array(a)
3433
end
3534

36-
@doc """
37-
spzeros([T::Type], dims) -> A::SparseArrayDOK{T}
38-
39-
Create an empty size `dims` sparse array.
40-
The optional `T` argument specifies the element type, which defaults to `Float64`.
41-
""" spzeros
42-
43-
spzeros(dims::Dims) = spzeros(Float64, dims)
44-
spzeros(::Type{T}, dims::Dims) where {T} = SparseArrayDOK{T}(undef, dims)
45-
46-
@doc """
47-
sprand([rng], [T::Type], dims; density::Real=0.5, rfn::Function=rand) -> A::SparseArrayDOK{T}
48-
49-
Create a random size `dims` sparse array in which the probability of any element being stored is independently given by `density`.
50-
The optional `rng` argument specifies a random number generator, see also `Random`.
51-
The optional `T` argument specifies the element type, which defaults to `Float64`.
52-
The optional `rfn` argument can be used to control the type of random elements.
53-
54-
See also [`sprand!`](@ref).
55-
""" sprand
56-
57-
function sprand(::Type{T}, dims::Dims; kwargs...) where {T}
58-
return sprand(default_rng(), T, dims; kwargs...)
59-
end
60-
sprand(dims::Dims; kwargs...) = sprand(default_rng(), Float64, dims; kwargs...)
61-
function sprand(rng::AbstractRNG, dims::Dims; kwargs...)
62-
return sprand(rng, Float64, dims; kwargs...)
63-
end
64-
function sprand(rng::AbstractRNG, ::Type{T}, dims::Dims; kwargs...) where {T}
65-
A = SparseArrayDOK{T}(undef, dims)
66-
sprand!(rng, A; kwargs...)
67-
return A
68-
end
69-
70-
@doc """
71-
sprand!([rng], A::AbstractArray; density::Real=0.5, rfn::Function=rand) -> A
72-
73-
Overwrite part of an array with random entries, where the probability of overwriting is independently given by `density`.
74-
The optional `rng` argument specifies a random number generator, see also `Random`.
75-
The optional `rfn` argument can be used to control the type of random elements.
76-
77-
See also [`sprand`](@ref).
78-
""" sprand!
79-
80-
sprand!(A::AbstractArray; kwargs...) = sprand!(default_rng(), A; kwargs...)
81-
function sprand!(
82-
rng::AbstractRNG, A::AbstractArray; density::Real=0.5, rfn::Function=Random.rand
83-
)
84-
ArrayLayouts.zero!(A)
85-
rand_inds = Random.randsubseq(rng, eachindex(A), density)
86-
rand_entries = rfn(rng, eltype(A), length(rand_inds))
87-
for (I, v) in zip(rand_inds, rand_entries)
88-
A[I] = v
89-
end
90-
end
91-
9235
# Minimal interface for `SparseArrayInterface`.
9336
# Fallbacks for dense/non-sparse arrays.
9437
@interface ::AbstractArrayInterface isstored(a::AbstractArray, I::Int...) = true

0 commit comments

Comments
 (0)