Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 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
14 changes: 14 additions & 0 deletions .github/workflows/IntegrationTestRequest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: "Integration Test Request"

on:
issue_comment:
types: [created]

jobs:
integrationrequest:
if: |
github.event.issue.pull_request &&
contains(fromJSON('["OWNER", "COLLABORATOR", "MEMBER"]'), github.event.comment.author_association)
uses: ITensor/ITensorActions/.github/workflows/IntegrationTestRequest.yml@main
with:
localregistry: https://github.com/ITensor/ITensorRegistry.git
16 changes: 0 additions & 16 deletions .github/workflows/Register.yml

This file was deleted.

24 changes: 24 additions & 0 deletions .github/workflows/Registrator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Register Package
on:
workflow_dispatch:
pull_request:
types:
- closed
paths:
- 'Project.toml'
branches:
- 'master'
- 'main'

permissions:
contents: write
pull-requests: write

jobs:
Register:
if: github.event_name == 'workflow_dispatch' || github.event.pull_request.merged == true
uses: "ITensor/ITensorActions/.github/workflows/Registrator.yml@main"
with:
localregistry: ITensor/ITensorRegistry
secrets:
REGISTRATOR_KEY: ${{ secrets.REGISTRATOR_KEY }}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 ITensor developers
Copyright (c) 2025 ITensor developers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 3 additions & 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.2.11"
version = "0.3.0"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand All @@ -11,6 +11,7 @@ Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MapBroadcast = "ebd9b9da-f48d-417c-9660-449667d60261"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
Accessors = "0.1.41"
Expand All @@ -21,6 +22,7 @@ Dictionaries = "0.4.3"
FillArrays = "1.13.0"
LinearAlgebra = "1.10"
MapBroadcast = "0.1.5"
Random = "1.10.0"
SafeTestsets = "0.1"
Suppressor = "0.2"
Test = "1.10"
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ using SparseArraysBase:
zero!
using Test: @test, @test_throws

a = SparseArrayDOK{Float64}(2, 2)
a = SparseArrayDOK{Float64}(undef, 2, 2)
````

AbstractArray interface:
Expand Down Expand Up @@ -122,32 +122,32 @@ b = a[1:2, 2]
@test b == [12, 0]
@test storedlength(b) == 1

a = SparseArrayDOK{Float64}(2, 2)
a = SparseArrayDOK{Float64}(undef, 2, 2)
a .= 2
for I in eachindex(a)
@test a[I] == 2
end
@test storedlength(a) == length(a)

a = SparseArrayDOK{Float64}(2, 2)
a = SparseArrayDOK{Float64}(undef, 2, 2)
fill!(a, 2)
for I in eachindex(a)
@test a[I] == 2
end
@test storedlength(a) == length(a)

a = SparseArrayDOK{Float64}(2, 2)
a = SparseArrayDOK{Float64}(undef, 2, 2)
fill!(a, 0)
@test iszero(a)
@test iszero(storedlength(a))

a = SparseArrayDOK{Float64}(2, 2)
a = SparseArrayDOK{Float64}(undef, 2, 2)
a[1, 2] = 12
zero!(a)
@test iszero(a)
@test iszero(storedlength(a))

a = SparseArrayDOK{Float64}(2, 2)
a = SparseArrayDOK{Float64}(undef, 2, 2)
a[1, 2] = 12
b = zero(a)
@test iszero(b)
Expand Down
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ makedocs(;
edit_link="main",
assets=String[],
),
pages=["Home" => "index.md"],
pages=["Home" => "index.md", "Reference" => "reference.md"],
)

deploydocs(;
Expand Down
5 changes: 5 additions & 0 deletions docs/src/reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Reference

```@autodocs
Modules = [SparseArraysBase]
```
12 changes: 6 additions & 6 deletions examples/README.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ using SparseArraysBase:
zero!
using Test: @test, @test_throws

a = SparseArrayDOK{Float64}(2, 2)
a = SparseArrayDOK{Float64}(undef, 2, 2)

# AbstractArray interface:

Expand Down Expand Up @@ -118,32 +118,32 @@ b = a[1:2, 2]
@test b == [12, 0]
@test storedlength(b) == 1

a = SparseArrayDOK{Float64}(2, 2)
a = SparseArrayDOK{Float64}(undef, 2, 2)
a .= 2
for I in eachindex(a)
@test a[I] == 2
end
@test storedlength(a) == length(a)

a = SparseArrayDOK{Float64}(2, 2)
a = SparseArrayDOK{Float64}(undef, 2, 2)
fill!(a, 2)
for I in eachindex(a)
@test a[I] == 2
end
@test storedlength(a) == length(a)

a = SparseArrayDOK{Float64}(2, 2)
a = SparseArrayDOK{Float64}(undef, 2, 2)
fill!(a, 0)
@test iszero(a)
@test iszero(storedlength(a))

a = SparseArrayDOK{Float64}(2, 2)
a = SparseArrayDOK{Float64}(undef, 2, 2)
a[1, 2] = 12
zero!(a)
@test iszero(a)
@test iszero(storedlength(a))

a = SparseArrayDOK{Float64}(2, 2)
a = SparseArrayDOK{Float64}(undef, 2, 2)
a[1, 2] = 12
b = zero(a)
@test iszero(b)
Expand Down
3 changes: 2 additions & 1 deletion src/SparseArraysBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ export SparseArrayDOK,
OneElementVector,
eachstoredindex,
isstored,
oneelementarray,
oneelement,
storedlength,
storedpairs,
storedvalues
export spzeros, sprand, sprand!

include("abstractsparsearrayinterface.jl")
include("sparsearrayinterface.jl")
Expand Down
60 changes: 60 additions & 0 deletions src/abstractsparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,63 @@
end
eachstoredindex(a::ReplacedUnstoredSparseArray) = eachstoredindex(parent(a))
@derive ReplacedUnstoredSparseArray AbstractArrayOps

# Special-purpose constructors
# ----------------------------
using Random: Random, AbstractRNG, default_rng

@doc """
spzeros([T::Type], dims) -> A::SparseArrayDOK{T}
Create an empty size `dims` sparse array.
The optional `T` argument specifies the element type, which defaults to `Float64`.
""" spzeros

spzeros(dims::Dims) = spzeros(Float64, dims)
spzeros(::Type{T}, dims::Dims) where {T} = SparseArrayDOK{T}(undef, dims)

Check warning on line 69 in src/abstractsparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearray.jl#L68-L69

Added lines #L68 - L69 were not covered by tests

@doc """
sprand([rng], [T::Type], dims; density::Real=0.5, rfn::Function=rand) -> A::SparseArrayDOK{T}
Create a random size `dims` sparse array in which the probability of any element being stored is independently given by `density`.
The optional `rng` argument specifies a random number generator, see also `Random`.
The optional `T` argument specifies the element type, which defaults to `Float64`.
The optional `rfn` argument can be used to control the type of random elements.
See also [`sprand!`](@ref).
""" sprand

function sprand(::Type{T}, dims::Dims; kwargs...) where {T}
return sprand(default_rng(), T, dims; kwargs...)

Check warning on line 83 in src/abstractsparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearray.jl#L82-L83

Added lines #L82 - L83 were not covered by tests
end
sprand(dims::Dims; kwargs...) = sprand(default_rng(), Float64, dims; kwargs...)
function sprand(rng::AbstractRNG, dims::Dims; kwargs...)
return sprand(rng, Float64, dims; kwargs...)

Check warning on line 87 in src/abstractsparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearray.jl#L85-L87

Added lines #L85 - L87 were not covered by tests
end
function sprand(rng::AbstractRNG, ::Type{T}, dims::Dims; kwargs...) where {T}
A = SparseArrayDOK{T}(undef, dims)
sprand!(rng, A; kwargs...)
return A
end

@doc """
sprand!([rng], A::AbstractArray; density::Real=0.5, rfn::Function=rand) -> A
Overwrite part of an array with random entries, where the probability of overwriting is independently given by `density`.
The optional `rng` argument specifies a random number generator, see also `Random`.
The optional `rfn` argument can be used to control the type of random elements.
See also [`sprand`](@ref).
""" sprand!

sprand!(A::AbstractArray; kwargs...) = sprand!(default_rng(), A; kwargs...)

Check warning on line 105 in src/abstractsparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearray.jl#L105

Added line #L105 was not covered by tests
function sprand!(
rng::AbstractRNG, A::AbstractArray; density::Real=0.5, rfn::Function=Random.rand
)
ArrayLayouts.zero!(A)
rand_inds = Random.randsubseq(rng, eachindex(A), density)
rand_entries = rfn(rng, eltype(A), length(rand_inds))
for (I, v) in zip(rand_inds, rand_entries)
A[I] = v
end
end
2 changes: 1 addition & 1 deletion src/abstractsparsearrayinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ end
a::AbstractArray, T::Type, size::Tuple{Vararg{Int}}
)
# TODO: Define `default_similartype` or something like that?
return SparseArrayDOK{T}(size...)
return SparseArrayDOK{T}(undef, size)
end

# map over a specified subset of indices of the inputs.
Expand Down
74 changes: 58 additions & 16 deletions src/sparsearraydok.jl
Original file line number Diff line number Diff line change
@@ -1,39 +1,81 @@
using Accessors: @set
using Dictionaries: Dictionary, IndexError, set!
using Dictionaries: AbstractDictionary, Dictionary, IndexError, set!

function default_getunstoredindex(a::AbstractArray, I::Int...)
return zero(eltype(a))
end

const DOKStorage{T,N} = Dictionary{CartesianIndex{N},T}

struct SparseArrayDOK{T,N,F} <: AbstractSparseArray{T,N}
storage::Dictionary{CartesianIndex{N},T}
storage::DOKStorage{T,N}
size::NTuple{N,Int}
getunstoredindex::F

# bare constructor
function SparseArrayDOK{T,N,F}(
::UndefInitializer, size::Dims{N}, getunstoredindex::F
) where {T,N,F}
storage = DOKStorage{T,N}()
return new{T,N,F}(storage, size, getunstoredindex)
end

# unchecked constructor from data
function SparseArrayDOK{T,N,F}(

Check warning on line 24 in src/sparsearraydok.jl

View check run for this annotation

Codecov / codecov/patch

src/sparsearraydok.jl#L24

Added line #L24 was not covered by tests
storage::DOKStorage{T,N}, size::Dims{N}, getunstoredindex::F
) where {T,N,F}
return new{T,N,F}(storage, size, getunstoredindex)

Check warning on line 27 in src/sparsearraydok.jl

View check run for this annotation

Codecov / codecov/patch

src/sparsearraydok.jl#L27

Added line #L27 was not covered by tests
end
end

function set_getunstoredindex(a::SparseArrayDOK, f)
@set a.getunstoredindex = f
return a
## constructors with T and N
# -> make SparseMatrix{T}(undef, ...) work
function SparseArrayDOK{T,N}(
::UndefInitializer, dims::Dims, getunstoredindex=default_getunstoredindex
) where {T,N}
(length(dims) == N && all(≥(0), dims)) ||
throw(ArgumentError("Invalid dimensions: $dims"))
F = typeof(getunstoredindex)
return SparseArrayDOK{T,N,F}(undef, dims, getunstoredindex)
end

using DerivableInterfaces: DerivableInterfaces
# This defines the destination type of various operations in DerivableInterfaces.jl.
DerivableInterfaces.arraytype(::AbstractSparseArrayInterface, T::Type) = SparseArrayDOK{T}
## constructors with T
function SparseArrayDOK{T}(::UndefInitializer, dims::Dims{N}, unstored...) where {T,N}
return SparseArrayDOK{T,N}(undef, dims, unstored...)
end

function SparseArrayDOK{T,N}(size::Vararg{Int,N}) where {T,N}
getunstoredindex = default_getunstoredindex
F = typeof(getunstoredindex)
return SparseArrayDOK{T,N,F}(Dictionary{CartesianIndex{N},T}(), size, getunstoredindex)
function SparseArrayDOK{T}(::UndefInitializer, dims::Vararg{Int,N}) where {T,N}
return SparseArrayDOK{T,N}(undef, dims)
end

# checked constructor from data: use `setindex!` to validate/convert input
function SparseArrayDOK{T}(

Check warning on line 52 in src/sparsearraydok.jl

View check run for this annotation

Codecov / codecov/patch

src/sparsearraydok.jl#L52

Added line #L52 was not covered by tests
storage::Union{AbstractDictionary,AbstractDict}, dims::Dims, unstored...
) where {T}
A = SparseArrayDOK{T}(undef, dims, unstored...)
for (i, v) in pairs(storage)
A[i] = v
end
return A

Check warning on line 59 in src/sparsearraydok.jl

View check run for this annotation

Codecov / codecov/patch

src/sparsearraydok.jl#L55-L59

Added lines #L55 - L59 were not covered by tests
end

function SparseArrayDOK{T}(::UndefInitializer, size::Tuple{Vararg{Int}}) where {T}
return SparseArrayDOK{T,length(size)}(size...)
## constructors without type parameters
function SparseArrayDOK(

Check warning on line 63 in src/sparsearraydok.jl

View check run for this annotation

Codecov / codecov/patch

src/sparsearraydok.jl#L63

Added line #L63 was not covered by tests
storage::Union{AbstractDictionary,AbstractDict}, dims::Dims, unstored...
)
T = valtype(storage)
return SparseArrayDOK{T}(storage, dims, unstored...)

Check warning on line 67 in src/sparsearraydok.jl

View check run for this annotation

Codecov / codecov/patch

src/sparsearraydok.jl#L66-L67

Added lines #L66 - L67 were not covered by tests
end

function SparseArrayDOK{T}(size::Int...) where {T}
return SparseArrayDOK{T,length(size)}(size...)
function set_getunstoredindex(a::SparseArrayDOK, f)
@set a.getunstoredindex = f
return a

Check warning on line 72 in src/sparsearraydok.jl

View check run for this annotation

Codecov / codecov/patch

src/sparsearraydok.jl#L70-L72

Added lines #L70 - L72 were not covered by tests
end

using DerivableInterfaces: DerivableInterfaces
# This defines the destination type of various operations in DerivableInterfaces.jl.
DerivableInterfaces.arraytype(::AbstractSparseArrayInterface, T::Type) = SparseArrayDOK{T}

using DerivableInterfaces: @array_aliases
# Define `SparseMatrixDOK`, `AnySparseArrayDOK`, etc.
@array_aliases SparseArrayDOK
Expand Down
6 changes: 4 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ isexamplefile(fn) =
# tests in groups based on folder structure
for testgroup in filter(isdir, readdir(@__DIR__))
if GROUP == "ALL" || GROUP == uppercase(testgroup)
for file in filter(istestfile, readdir(joinpath(@__DIR__, testgroup); join=true))
groupdir = joinpath(@__DIR__, testgroup)
for file in filter(istestfile, readdir(groupdir))
filename = joinpath(groupdir, file)
@eval @safetestset $file begin
include($file)
include($filename)
end
end
end
Expand Down
Loading
Loading