Skip to content

Commit 35439c5

Browse files
committed
sparse constructor, zero value to kwarg, more constructors
1 parent d741b7f commit 35439c5

File tree

7 files changed

+227
-193
lines changed

7 files changed

+227
-193
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SparseArraysBase"
22
uuid = "0d5efcca-f356-4864-8770-e1ed8d78f208"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.3.1"
4+
version = "0.4.0"
55

66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"

src/SparseArraysBase.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export SparseArrayDOK,
99
eachstoredindex,
1010
isstored,
1111
oneelement,
12+
sparse,
1213
sparserand,
1314
sparserand!,
1415
sparsezeros,

src/abstractsparsearray.jl

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Dictionaries: AbstractDictionary
2+
13
abstract type AbstractSparseArray{T,N} <: AbstractArray{T,N} end
24

35
using DerivableInterfaces: @array_aliases
@@ -33,17 +35,47 @@ end
3335

3436
# Special-purpose constructors
3537
# ----------------------------
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+
3664
using Random: Random, AbstractRNG, default_rng
3765

3866
@doc """
39-
sparsezeros([T::Type], dims) -> A::SparseArrayDOK{T}
67+
sparsezeros([T::Type], dims[; getunstoredfun]) -> A::SparseArrayDOK{T}
4068
4169
Create an empty size `dims` sparse array.
4270
The optional `T` argument specifies the element type, which defaults to `Float64`.
4371
""" sparsezeros
4472

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...)
4779

4880
@doc """
4981
sparserand([rng], [T::Type], dims; density::Real=0.5, randfun::Function=rand) -> A::SparseArrayDOK{T}
@@ -61,15 +93,25 @@ See also [`sparserand!`](@ref).
6193
function sparserand(::Type{T}, dims::Dims; kwargs...) where {T}
6294
return sparserand(default_rng(), T, dims; kwargs...)
6395
end
96+
function sparserand(::Type{T}, dims::Int...; kwargs...) where {T}
97+
return sparserand(T, dims; kwargs...)
98+
end
6499
sparserand(dims::Dims; kwargs...) = sparserand(default_rng(), Float64, dims; kwargs...)
100+
sparserand(dims::Int...; kwargs...) = sparserand(dims; kwargs...)
65101
function sparserand(rng::AbstractRNG, dims::Dims; kwargs...)
66102
return sparserand(rng, Float64, dims; kwargs...)
67103
end
104+
function sparserand(rng::AbstractRNG, dims::Int...; kwargs...)
105+
return sparserand(rng, dims; kwargs...)
106+
end
68107
function sparserand(rng::AbstractRNG, ::Type{T}, dims::Dims; kwargs...) where {T}
69108
A = SparseArrayDOK{T}(undef, dims)
70109
sparserand!(rng, A; kwargs...)
71110
return A
72111
end
112+
function sparserand(rng::AbstractRNG, ::Type{T}, dims::Int...; kwargs...) where {T}
113+
return sparserand(rng, T, dims; kwargs...)
114+
end
73115

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

0 commit comments

Comments
 (0)