Skip to content

Commit 248dd87

Browse files
authored
sparse constructor, zero value to kwarg, constructors with splatted dims (#43)
1 parent 4e94eea commit 248dd87

11 files changed

+301
-196
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.2"
4+
version = "0.4.0"
55

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

docs/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ SparseArraysBase = "0d5efcca-f356-4864-8770-e1ed8d78f208"
88
Dictionaries = "0.4.4"
99
Documenter = "1.8.1"
1010
Literate = "2.20.1"
11-
SparseArraysBase = "0.3.0"
11+
SparseArraysBase = "0.4.0"

examples/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
55

66
[compat]
77
Dictionaries = "0.4.4"
8-
SparseArraysBase = "0.3.0"
8+
SparseArraysBase = "0.4.0"
99
Test = "<0.0.1, 1"

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: 54 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,56 @@ end
3335

3436
# Special-purpose constructors
3537
# ----------------------------
38+
39+
"""
40+
sparse(storage::Union{AbstractDict,AbstractDictionary}, dims...[; getunstored])
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`. If `dims` aren't specified, the size will be determined automatically
45+
from the input indices.
46+
47+
This constructor does not take ownership of the supplied storage, and will result in an
48+
independent container.
49+
"""
50+
sparse(::Union{AbstractDict,AbstractDictionary}, dims...; kwargs...)
51+
52+
const AbstractDictOrDictionary = Union{AbstractDict,AbstractDictionary}
53+
# checked constructor from data: use `setindex!` to validate/convert input
54+
function sparse(storage::AbstractDictOrDictionary, dims::Dims; kwargs...)
55+
A = SparseArrayDOK{valtype(storage)}(undef, dims; kwargs...)
56+
for (i, v) in pairs(storage)
57+
A[i] = v
58+
end
59+
return A
60+
end
61+
function sparse(storage::AbstractDictOrDictionary, dims::Int...; kwargs...)
62+
return sparse(storage, dims; kwargs...)
63+
end
64+
# Determine the size automatically.
65+
function sparse(storage::AbstractDictOrDictionary; kwargs...)
66+
dims = ntuple(Returns(0), length(keytype(storage)))
67+
for I in keys(storage)
68+
dims = map(max, dims, Tuple(I))
69+
end
70+
return sparse(storage, dims; kwargs...)
71+
end
72+
3673
using Random: Random, AbstractRNG, default_rng
3774

3875
@doc """
39-
sparsezeros([T::Type], dims) -> A::SparseArrayDOK{T}
76+
sparsezeros([T::Type], dims[; getunstored]) -> A::SparseArrayDOK{T}
4077
4178
Create an empty size `dims` sparse array.
4279
The optional `T` argument specifies the element type, which defaults to `Float64`.
4380
""" sparsezeros
4481

45-
sparsezeros(dims::Dims) = sparsezeros(Float64, dims)
46-
sparsezeros(::Type{T}, dims::Dims) where {T} = SparseArrayDOK{T}(undef, dims)
82+
function sparsezeros(::Type{T}, dims::Dims; kwargs...) where {T}
83+
return SparseArrayDOK{T}(undef, dims; kwargs...)
84+
end
85+
sparsezeros(::Type{T}, dims::Int...; kwargs...) where {T} = sparsezeros(T, dims; kwargs...)
86+
sparsezeros(dims::Dims; kwargs...) = sparsezeros(Float64, dims; kwargs...)
87+
sparsezeros(dims::Int...; kwargs...) = sparsezeros(Float64, dims; kwargs...)
4788

4889
@doc """
4990
sparserand([rng], [T::Type], dims; density::Real=0.5, randfun::Function=rand) -> A::SparseArrayDOK{T}
@@ -61,15 +102,25 @@ See also [`sparserand!`](@ref).
61102
function sparserand(::Type{T}, dims::Dims; kwargs...) where {T}
62103
return sparserand(default_rng(), T, dims; kwargs...)
63104
end
105+
function sparserand(::Type{T}, dims::Int...; kwargs...) where {T}
106+
return sparserand(T, dims; kwargs...)
107+
end
64108
sparserand(dims::Dims; kwargs...) = sparserand(default_rng(), Float64, dims; kwargs...)
109+
sparserand(dims::Int...; kwargs...) = sparserand(dims; kwargs...)
65110
function sparserand(rng::AbstractRNG, dims::Dims; kwargs...)
66111
return sparserand(rng, Float64, dims; kwargs...)
67112
end
113+
function sparserand(rng::AbstractRNG, dims::Int...; kwargs...)
114+
return sparserand(rng, dims; kwargs...)
115+
end
68116
function sparserand(rng::AbstractRNG, ::Type{T}, dims::Dims; kwargs...) where {T}
69117
A = SparseArrayDOK{T}(undef, dims)
70118
sparserand!(rng, A; kwargs...)
71119
return A
72120
end
121+
function sparserand(rng::AbstractRNG, ::Type{T}, dims::Int...; kwargs...) where {T}
122+
return sparserand(rng, T, dims; kwargs...)
123+
end
73124

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

0 commit comments

Comments
 (0)