Skip to content

sparse constructor, zero value to kwarg, constructors with splatted dims #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SparseArraysBase"
uuid = "0d5efcca-f356-4864-8770-e1ed8d78f208"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.3.2"
version = "0.4.0"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ SparseArraysBase = "0d5efcca-f356-4864-8770-e1ed8d78f208"
Dictionaries = "0.4.4"
Documenter = "1.8.1"
Literate = "2.20.1"
SparseArraysBase = "0.3.0"
SparseArraysBase = "0.4.0"
2 changes: 1 addition & 1 deletion examples/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
Dictionaries = "0.4.4"
SparseArraysBase = "0.3.0"
SparseArraysBase = "0.4.0"
Test = "<0.0.1, 1"
1 change: 1 addition & 0 deletions src/SparseArraysBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export SparseArrayDOK,
eachstoredindex,
isstored,
oneelement,
sparse,
sparserand,
sparserand!,
sparsezeros,
Expand Down
57 changes: 54 additions & 3 deletions src/abstractsparsearray.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Dictionaries: AbstractDictionary

abstract type AbstractSparseArray{T,N} <: AbstractArray{T,N} end

using DerivableInterfaces: @array_aliases
Expand Down Expand Up @@ -33,17 +35,56 @@

# Special-purpose constructors
# ----------------------------

"""
sparse(storage::Union{AbstractDict,AbstractDictionary}, dims...[; getunstored])

Construct an `N`-dimensional [`SparseArrayDOK`](@ref) containing elements of type `T`. Both
`T` and `N` can either be supplied explicitly or be determined by the `storage` and the
length or number of `dims`. If `dims` aren't specified, the size will be determined automatically
from the input indices.

This constructor does not take ownership of the supplied storage, and will result in an
independent container.
"""
sparse(::Union{AbstractDict,AbstractDictionary}, dims...; kwargs...)

const AbstractDictOrDictionary = Union{AbstractDict,AbstractDictionary}
# checked constructor from data: use `setindex!` to validate/convert input
function sparse(storage::AbstractDictOrDictionary, dims::Dims; kwargs...)
A = SparseArrayDOK{valtype(storage)}(undef, dims; kwargs...)
for (i, v) in pairs(storage)
A[i] = v
end
return A
end
function sparse(storage::AbstractDictOrDictionary, dims::Int...; kwargs...)
return sparse(storage, dims; kwargs...)
end
# Determine the size automatically.
function sparse(storage::AbstractDictOrDictionary; kwargs...)
dims = ntuple(Returns(0), length(keytype(storage)))
for I in keys(storage)
dims = map(max, dims, Tuple(I))
end
return sparse(storage, dims; kwargs...)
end

using Random: Random, AbstractRNG, default_rng

@doc """
sparsezeros([T::Type], dims) -> A::SparseArrayDOK{T}
sparsezeros([T::Type], dims[; getunstored]) -> A::SparseArrayDOK{T}

Create an empty size `dims` sparse array.
The optional `T` argument specifies the element type, which defaults to `Float64`.
""" sparsezeros

sparsezeros(dims::Dims) = sparsezeros(Float64, dims)
sparsezeros(::Type{T}, dims::Dims) where {T} = SparseArrayDOK{T}(undef, dims)
function sparsezeros(::Type{T}, dims::Dims; kwargs...) where {T}
return SparseArrayDOK{T}(undef, dims; kwargs...)
end
sparsezeros(::Type{T}, dims::Int...; kwargs...) where {T} = sparsezeros(T, dims; kwargs...)
sparsezeros(dims::Dims; kwargs...) = sparsezeros(Float64, dims; kwargs...)
sparsezeros(dims::Int...; kwargs...) = sparsezeros(Float64, dims; kwargs...)

@doc """
sparserand([rng], [T::Type], dims; density::Real=0.5, randfun::Function=rand) -> A::SparseArrayDOK{T}
Expand All @@ -61,15 +102,25 @@
function sparserand(::Type{T}, dims::Dims; kwargs...) where {T}
return sparserand(default_rng(), T, dims; kwargs...)
end
function sparserand(::Type{T}, dims::Int...; kwargs...) where {T}
return sparserand(T, dims; kwargs...)

Check warning on line 106 in src/abstractsparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearray.jl#L105-L106

Added lines #L105 - L106 were not covered by tests
end
sparserand(dims::Dims; kwargs...) = sparserand(default_rng(), Float64, dims; kwargs...)
sparserand(dims::Int...; kwargs...) = sparserand(dims; kwargs...)

Check warning on line 109 in src/abstractsparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearray.jl#L109

Added line #L109 was not covered by tests
function sparserand(rng::AbstractRNG, dims::Dims; kwargs...)
return sparserand(rng, Float64, dims; kwargs...)
end
function sparserand(rng::AbstractRNG, dims::Int...; kwargs...)
return sparserand(rng, dims; kwargs...)
end
function sparserand(rng::AbstractRNG, ::Type{T}, dims::Dims; kwargs...) where {T}
A = SparseArrayDOK{T}(undef, dims)
sparserand!(rng, A; kwargs...)
return A
end
function sparserand(rng::AbstractRNG, ::Type{T}, dims::Int...; kwargs...) where {T}
return sparserand(rng, T, dims; kwargs...)
end

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