1
+ using Dictionaries: AbstractDictionary
2
+
1
3
abstract type AbstractSparseArray{T,N} <: AbstractArray{T,N} end
2
4
3
5
using DerivableInterfaces: @array_aliases
33
35
34
36
# Special-purpose constructors
35
37
# ----------------------------
38
+
39
+ """
40
+ sparse(storage::Union{AbstractDict,AbstractDictionary}, dims...[; getunstoredfun])
41
+
42
+ Construct an `N`-dimensional [`SparseArrayDOK`](@ref) containing elements of type `T`. Both
43
+ `T` and `N` can either be supplied explicitly or be determined by the `storage` and the
44
+ length or number of `dims`.
45
+
46
+ This constructor does not take ownership of the supplied storage, and will result in an
47
+ independent container.
48
+ """
49
+ sparse (:: Union{AbstractDict,AbstractDictionary} , dims... ; kwargs... )
50
+
51
+ const AbstractDictOrDictionary = Union{AbstractDict,AbstractDictionary}
52
+ # checked constructor from data: use `setindex!` to validate/convert input
53
+ function sparse (storage:: AbstractDictOrDictionary , dims:: Dims ; kwargs... )
54
+ A = SparseArrayDOK {eltype(storage)} (undef, dims; kwargs... )
55
+ for (i, v) in pairs (storage)
56
+ A[i] = v
57
+ end
58
+ return A
59
+ end
60
+ function sparse (storage:: AbstractDictOrDictionary , dims:: Int... ; kwargs... )
61
+ return sparse (storage, dims; kwargs... )
62
+ end
63
+
36
64
using Random: Random, AbstractRNG, default_rng
37
65
38
66
@doc """
39
- sparsezeros([T::Type], dims) -> A::SparseArrayDOK{T}
67
+ sparsezeros([T::Type], dims[; getunstoredfun] ) -> A::SparseArrayDOK{T}
40
68
41
69
Create an empty size `dims` sparse array.
42
70
The optional `T` argument specifies the element type, which defaults to `Float64`.
43
71
""" sparsezeros
44
72
45
- sparsezeros (dims:: Dims ) = sparsezeros (Float64, dims)
46
- sparsezeros (:: Type{T} , dims:: Dims ) where {T} = SparseArrayDOK {T} (undef, dims)
73
+ function sparsezeros (:: Type{T} , dims:: Dims ; kwargs... ) where {T}
74
+ return SparseArrayDOK {T} (undef, dims; kwargs... )
75
+ end
76
+ sparsezeros (:: Type{T} , dims:: Int... ; kwargs... ) where {T} = sparsezeros (T, dims; kwargs... )
77
+ sparsezeros (dims:: Dims ; kwargs... ) = sparsezeros (Float64, dims; kwargs... )
78
+ sparsezeros (dims:: Int... ; kwargs... ) = sparsezeros (Float64, dims; kwargs... )
47
79
48
80
@doc """
49
81
sparserand([rng], [T::Type], dims; density::Real=0.5, randfun::Function=rand) -> A::SparseArrayDOK{T}
@@ -61,15 +93,25 @@ See also [`sparserand!`](@ref).
61
93
function sparserand (:: Type{T} , dims:: Dims ; kwargs... ) where {T}
62
94
return sparserand (default_rng (), T, dims; kwargs... )
63
95
end
96
+ function sparserand (:: Type{T} , dims:: Int... ; kwargs... ) where {T}
97
+ return sparserand (T, dims; kwargs... )
98
+ end
64
99
sparserand (dims:: Dims ; kwargs... ) = sparserand (default_rng (), Float64, dims; kwargs... )
100
+ sparserand (dims:: Int... ; kwargs... ) = sparserand (dims; kwargs... )
65
101
function sparserand (rng:: AbstractRNG , dims:: Dims ; kwargs... )
66
102
return sparserand (rng, Float64, dims; kwargs... )
67
103
end
104
+ function sparserand (rng:: AbstractRNG , dims:: Int... ; kwargs... )
105
+ return sparserand (rng, dims; kwargs... )
106
+ end
68
107
function sparserand (rng:: AbstractRNG , :: Type{T} , dims:: Dims ; kwargs... ) where {T}
69
108
A = SparseArrayDOK {T} (undef, dims)
70
109
sparserand! (rng, A; kwargs... )
71
110
return A
72
111
end
112
+ function sparserand (rng:: AbstractRNG , :: Type{T} , dims:: Int... ; kwargs... ) where {T}
113
+ return sparserand (rng, T, dims; kwargs... )
114
+ end
73
115
74
116
@doc """
75
117
sparserand!([rng], A::AbstractArray; density::Real=0.5, randfun::Function=rand) -> A
0 commit comments