Skip to content

Constructors #33

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 24 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from 8 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
25 changes: 13 additions & 12 deletions .github/workflows/Register.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
name: Register Package
on:
workflow_dispatch:
inputs:
version:
description: Version to register or component to bump
required: true
pull_request:
types:
- closed
paths:
- 'Project.toml'
branches:
- 'master'
- 'main'
jobs:
register:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: julia-actions/RegisterAction@latest
with:
token: ${{ secrets.GITHUB_TOKEN }}
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
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
59 changes: 58 additions & 1 deletion src/abstractsparsearrayinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# This is to bring `ArrayLayouts.zero!` into the namespace
# since it is considered part of the sparse array interface.
using ArrayLayouts: zero!
using Random: Random, AbstractRNG, default_rng

function eachstoredindex end
function getstoredindex end
Expand Down Expand Up @@ -32,6 +33,62 @@
return Array(a)
end

@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 44 in src/abstractsparsearrayinterface.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearrayinterface.jl#L43-L44

Added lines #L43 - L44 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 58 in src/abstractsparsearrayinterface.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearrayinterface.jl#L57-L58

Added lines #L57 - L58 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 62 in src/abstractsparsearrayinterface.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearrayinterface.jl#L60-L62

Added lines #L60 - L62 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

Check warning on line 67 in src/abstractsparsearrayinterface.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearrayinterface.jl#L64-L67

Added lines #L64 - L67 were not covered by tests
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...)
function sprand!(

Check warning on line 81 in src/abstractsparsearrayinterface.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearrayinterface.jl#L80-L81

Added lines #L80 - L81 were not covered by tests
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

Check warning on line 89 in src/abstractsparsearrayinterface.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearrayinterface.jl#L84-L89

Added lines #L84 - L89 were not covered by tests
end

# Minimal interface for `SparseArrayInterface`.
# Fallbacks for dense/non-sparse arrays.
@interface ::AbstractArrayInterface isstored(a::AbstractArray, I::Int...) = true
Expand Down Expand Up @@ -200,7 +257,7 @@
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
60 changes: 44 additions & 16 deletions src/sparsearraydok.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,56 @@
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

# undef constructors
function SparseArrayDOK{T}(
::UndefInitializer, dims::Dims, getunstoredindex=default_getunstoredindex
) where {T}
all(≥(0), dims) || throw(ArgumentError("Invalid dimensions: $dims"))
N = length(dims)
F = typeof(getunstoredindex)
return SparseArrayDOK{T,N,F}(undef, dims, getunstoredindex)
end
function SparseArrayDOK{T}(::UndefInitializer, dims::Int...) where {T}
return SparseArrayDOK{T}(undef, dims)
end

# checked constructor from data: use `setindex!` to validate input
# does not take ownership of `storage`!
function SparseArrayDOK(

Check warning on line 46 in src/sparsearraydok.jl

View check run for this annotation

Codecov / codecov/patch

src/sparsearraydok.jl#L46

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

Check warning on line 53 in src/sparsearraydok.jl

View check run for this annotation

Codecov / codecov/patch

src/sparsearraydok.jl#L49-L53

Added lines #L49 - L53 were not covered by tests
end

function set_getunstoredindex(a::SparseArrayDOK, f)
Expand All @@ -20,20 +62,6 @@
# This defines the destination type of various operations in DerivableInterfaces.jl.
DerivableInterfaces.arraytype(::AbstractSparseArrayInterface, T::Type) = SparseArrayDOK{T}

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)
end

function SparseArrayDOK{T}(::UndefInitializer, size::Tuple{Vararg{Int}}) where {T}
return SparseArrayDOK{T,length(size)}(size...)
end

function SparseArrayDOK{T}(size::Int...) where {T}
return SparseArrayDOK{T,length(size)}(size...)
end

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