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 20 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
63 changes: 63 additions & 0 deletions src/abstractsparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,66 @@
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, rand_function::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 `rand_function` argument can be used to control the type of random elements, and should support
the signature `rand_function(rng, T, N)` to generate `N` entries of type `T`.


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 85 in src/abstractsparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearray.jl#L84-L85

Added lines #L84 - L85 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 89 in src/abstractsparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearray.jl#L87-L89

Added lines #L87 - L89 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, rand_function::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 `rand_function` argument can be used to control the type of random elements, and should support
the signature `rand_function(rng, T, N)` to generate `N` entries of type `T`.

See also [`sprand`](@ref).
""" sprand!

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

Check warning on line 108 in src/abstractsparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearray.jl#L108

Added line #L108 was not covered by tests
function sprand!(
rng::AbstractRNG, A::AbstractArray; density::Real=0.5, rand_function::Function=Random.rand
)
ArrayLayouts.zero!(A)
rand_inds = Random.randsubseq(rng, eachindex(A), density)
rand_entries = rand_function(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
Loading
Loading