diff --git a/.github/workflows/IntegrationTestRequest.yml b/.github/workflows/IntegrationTestRequest.yml new file mode 100644 index 0000000..d42fcca --- /dev/null +++ b/.github/workflows/IntegrationTestRequest.yml @@ -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 diff --git a/.github/workflows/Register.yml b/.github/workflows/Register.yml deleted file mode 100644 index 5b7cd3b..0000000 --- a/.github/workflows/Register.yml +++ /dev/null @@ -1,16 +0,0 @@ -name: Register Package -on: - workflow_dispatch: - inputs: - version: - description: Version to register or component to bump - required: true -jobs: - register: - runs-on: ubuntu-latest - permissions: - contents: write - steps: - - uses: julia-actions/RegisterAction@latest - with: - token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/Registrator.yml b/.github/workflows/Registrator.yml new file mode 100644 index 0000000..255e2af --- /dev/null +++ b/.github/workflows/Registrator.yml @@ -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 }} diff --git a/LICENSE b/LICENSE index 7f5c8c6..89cf357 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/Project.toml b/Project.toml index 68d0814..b7acbad 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "SparseArraysBase" uuid = "0d5efcca-f356-4864-8770-e1ed8d78f208" authors = ["ITensor developers and contributors"] -version = "0.2.11" +version = "0.3.0" [deps] Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697" @@ -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" @@ -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" diff --git a/README.md b/README.md index bdf69d5..68a60d3 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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) diff --git a/docs/make.jl b/docs/make.jl index 8d4c368..991d772 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -16,7 +16,7 @@ makedocs(; edit_link="main", assets=String[], ), - pages=["Home" => "index.md"], + pages=["Home" => "index.md", "Reference" => "reference.md"], ) deploydocs(; diff --git a/docs/src/reference.md b/docs/src/reference.md new file mode 100644 index 0000000..c1eadba --- /dev/null +++ b/docs/src/reference.md @@ -0,0 +1,5 @@ +# Reference + +```@autodocs +Modules = [SparseArraysBase] +``` diff --git a/examples/README.jl b/examples/README.jl index da64f99..eb42f94 100644 --- a/examples/README.jl +++ b/examples/README.jl @@ -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: @@ -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) diff --git a/src/SparseArraysBase.jl b/src/SparseArraysBase.jl index 0fa0ad2..02b5970 100644 --- a/src/SparseArraysBase.jl +++ b/src/SparseArraysBase.jl @@ -8,7 +8,10 @@ export SparseArrayDOK, OneElementVector, eachstoredindex, isstored, - oneelementarray, + oneelement, + sparserand, + sparserand!, + sparsezeros, storedlength, storedpairs, storedvalues diff --git a/src/abstractsparsearray.jl b/src/abstractsparsearray.jl index 14e01d1..c427c4d 100644 --- a/src/abstractsparsearray.jl +++ b/src/abstractsparsearray.jl @@ -53,3 +53,66 @@ function getunstoredindex(a::ReplacedUnstoredSparseArray, I::Int...) end eachstoredindex(a::ReplacedUnstoredSparseArray) = eachstoredindex(parent(a)) @derive ReplacedUnstoredSparseArray AbstractArrayOps + +# Special-purpose constructors +# ---------------------------- +using Random: Random, AbstractRNG, default_rng + +@doc """ + sparsezeros([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`. +""" sparsezeros + +sparsezeros(dims::Dims) = sparsezeros(Float64, dims) +sparsezeros(::Type{T}, dims::Dims) where {T} = SparseArrayDOK{T}(undef, dims) + +@doc """ + sparserand([rng], [T::Type], dims; density::Real=0.5, randfun::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 `randfun` argument can be used to control the type of random elements, and should support +the signature `randfun(rng, T, N)` to generate `N` entries of type `T`. + + +See also [`sparserand!`](@ref). +""" sparserand + +function sparserand(::Type{T}, dims::Dims; kwargs...) where {T} + return sparserand(default_rng(), T, dims; kwargs...) +end +sparserand(dims::Dims; kwargs...) = sparserand(default_rng(), Float64, dims; kwargs...) +function sparserand(rng::AbstractRNG, dims::Dims; kwargs...) + return sparserand(rng, Float64, 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 + +@doc """ + sparserand!([rng], A::AbstractArray; density::Real=0.5, randfun::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 `randfun` argument can be used to control the type of random elements, and should support +the signature `randfun(rng, T, N)` to generate `N` entries of type `T`. + +See also [`sparserand`](@ref). +""" sparserand! + +sparserand!(A::AbstractArray; kwargs...) = sparserand!(default_rng(), A; kwargs...) +function sparserand!( + rng::AbstractRNG, A::AbstractArray; density::Real=0.5, randfun::Function=Random.rand +) + ArrayLayouts.zero!(A) + rand_inds = Random.randsubseq(rng, eachindex(A), density) + rand_entries = randfun(rng, eltype(A), length(rand_inds)) + @inbounds for (I, v) in zip(rand_inds, rand_entries) + A[I] = v + end +end diff --git a/src/abstractsparsearrayinterface.jl b/src/abstractsparsearrayinterface.jl index f8bd0e0..82d984e 100644 --- a/src/abstractsparsearrayinterface.jl +++ b/src/abstractsparsearrayinterface.jl @@ -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. diff --git a/src/sparsearraydok.jl b/src/sparsearraydok.jl index 3161f95..12f5112 100644 --- a/src/sparsearraydok.jl +++ b/src/sparsearraydok.jl @@ -1,39 +1,111 @@ 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} + +""" + SparseArrayDOK{T,N,F} <: AbstractSparseArray{T,N} + +`N`-dimensional sparse Dictionary-of-keys (DOK) array with elements of type `T`, +optionally with a function of type `F` to instantiate non-stored elements. +""" struct SparseArrayDOK{T,N,F} <: AbstractSparseArray{T,N} - storage::Dictionary{CartesianIndex{N},T} + storage::DOKStorage{T,N} size::NTuple{N,Int} getunstoredindex::F -end -function set_getunstoredindex(a::SparseArrayDOK, f) - @set a.getunstoredindex = f - return a + # 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}( + storage::DOKStorage{T,N}, size::Dims{N}, getunstoredindex::F + ) where {T,N,F} + return new{T,N,F}(storage, size, getunstoredindex) + end end -using DerivableInterfaces: DerivableInterfaces -# This defines the destination type of various operations in DerivableInterfaces.jl. -DerivableInterfaces.arraytype(::AbstractSparseArrayInterface, T::Type) = SparseArrayDOK{T} +# Constructors +# ------------ +""" + SparseArrayDOK{T}(undef, dims, unstored...) + SparseArrayDOK{T}(undef, dims...) + SparseArrayDOK{T,N}(undef, dims, unstored...) + SparseArrayDOK{T,N}(undef, dims...) -function SparseArrayDOK{T,N}(size::Vararg{Int,N}) where {T,N} - getunstoredindex = default_getunstoredindex +Construct an uninitialized `N`-dimensional [`SparseArrayDOK`](@ref) containing +elements of type `T`. `N` can either be supplied explicitly, or be determined by +the length or number of `dims`. +""" +SparseArrayDOK{T,N}(::UndefInitializer, dims, unstored...) + +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}(Dictionary{CartesianIndex{N},T}(), size, getunstoredindex) + return SparseArrayDOK{T,N,F}(undef, dims, getunstoredindex) +end +function SparseArrayDOK{T}(::UndefInitializer, dims::Dims{N}, unstored...) where {T,N} + return SparseArrayDOK{T,N}(undef, dims, unstored...) +end +function SparseArrayDOK{T}(::UndefInitializer, dims::Vararg{Int,N}) where {T,N} + return SparseArrayDOK{T,N}(undef, dims) end -function SparseArrayDOK{T}(::UndefInitializer, size::Tuple{Vararg{Int}}) where {T} - return SparseArrayDOK{T,length(size)}(size...) +""" + SparseArrayDOK(storage::Union{AbstractDict,AbstractDictionary}, dims, unstored...) + SparseArrayDOK{T}(storage::Union{AbstractDict,AbstractDictionary}, dims, unstored...) + SparseArrayDOK{T,N}(storage::Union{AbstractDict,AbstractDictionary}, dims, unstored...) + +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`. + +This constructor does not take ownership of the supplied storage, and will result in an +independent container. +""" +SparseArrayDOK{T,N}(::Union{AbstractDict,AbstractDictionary}, dims, unstored...) + +const AbstractDictOrDictionary = Union{AbstractDict,AbstractDictionary} +# checked constructor from data: use `setindex!` to validate/convert input +function SparseArrayDOK{T,N}( + storage::AbstractDictOrDictionary, dims::Dims, unstored... +) where {T,N} + A = SparseArrayDOK{T,N}(undef, dims, unstored...) + for (i, v) in pairs(storage) + A[i] = v + end + return A +end +function SparseArrayDOK{T}( + storage::AbstractDictOrDictionary, dims::Dims, unstored... +) where {T} + return SparseArrayDOK{T,length(dims)}(storage, dims, unstored...) +end +function SparseArrayDOK(storage::AbstractDictOrDictionary, dims::Dims, unstored...) + return SparseArrayDOK{valtype(storage)}(storage, dims, unstored...) 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 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 diff --git a/test/runtests.jl b/test/runtests.jl index bd97441..1c52c3e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 diff --git a/test/test_aqua.jl b/test/test_aqua.jl index 0379869..0bff54a 100644 --- a/test/test_aqua.jl +++ b/test/test_aqua.jl @@ -3,6 +3,5 @@ using Aqua: Aqua using Test: @testset @testset "Code quality (Aqua.jl)" begin - # TODO: Fix Aqua issues and add this back. - # Aqua.test_all(SparseArraysBase) + Aqua.test_all(SparseArraysBase; ambiguities=false) end diff --git a/test/test_exports.jl b/test/test_exports.jl index 56bde16..952e478 100644 --- a/test/test_exports.jl +++ b/test/test_exports.jl @@ -11,7 +11,10 @@ using Test: @test, @testset :OneElementVector, :eachstoredindex, :isstored, - :oneelementarray, + :oneelement, + :sparserand, + :sparserand!, + :sparsezeros, :storedlength, :storedpairs, :storedvalues, diff --git a/test/test_linalg.jl b/test/test_linalg.jl index f48585c..7c67354 100644 --- a/test/test_linalg.jl +++ b/test/test_linalg.jl @@ -1,31 +1,19 @@ -using SparseArraysBase: SparseArrayDOK +using SparseArraysBase: sparserand using LinearAlgebra: mul! -using Random: Random using StableRNGs: StableRNG const rng = StableRNG(123) -# TODO: add this to main package -function sprand(rng::Random.AbstractRNG, ::Type{T}, sz::Base.Dims; p::Real=0.5) where {T} - A = SparseArrayDOK{T}(undef, sz) - for I in eachindex(A) - if rand(rng) < p - A[I] = rand(rng, T) - end - end - return A -end - @testset "mul!" begin T = Float64 szA = (2, 2) szB = (2, 2) szC = (szA[1], szB[2]) - for p in 0.0:0.25:1 - C = sprand(rng, T, szC; p) - A = sprand(rng, T, szA; p) - B = sprand(rng, T, szB; p) + for density in 0.0:0.25:1 + C = sparserand(rng, T, szC; density) + A = sparserand(rng, T, szA; density) + B = sparserand(rng, T, szB; density) check1 = mul!(Array(C), Array(A), Array(B)) @test mul!(copy(C), A, B) ≈ check1 diff --git a/test/test_sparsearraydok.jl b/test/test_sparsearraydok.jl index 08cadf4..6d9f42e 100644 --- a/test/test_sparsearraydok.jl +++ b/test/test_sparsearraydok.jl @@ -23,7 +23,7 @@ arrayts = (Array,) dev(x) = adapt(arrayt, x) - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a[1, 2] = 12 @test a isa SparseArrayDOK{elt,2} @test size(a) == (2, 2) @@ -33,7 +33,7 @@ arrayts = (Array,) @test a[1, 2, 1] == 12 @test storedlength(a) == 1 - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a[1, 2] = 12 for b in (similar(a, Float32, (3, 3)), similar(a, Float32, Base.OneTo.((3, 3)))) @test b isa SparseArrayDOK{Float32,2} @@ -41,7 +41,7 @@ arrayts = (Array,) @test size(b) == (3, 3) end - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a[1, 2] = 12 b = similar(a) bc = Broadcast.Broadcasted(x -> 2x, (a,)) @@ -50,49 +50,49 @@ arrayts = (Array,) @test b == [0 24; 0 0] @test storedlength(b) == 1 - a = SparseArrayDOK{elt}(3, 3, 3) + a = SparseArrayDOK{elt}(undef, 3, 3, 3) a[1, 2, 3] = 123 b = permutedims(a, (2, 3, 1)) @test b isa SparseArrayDOK{elt,3} @test b[2, 3, 1] == 123 @test storedlength(b) == 1 - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a[1, 2] = 12 b = copy(a') @test b isa SparseArrayDOK{elt,2} @test b == [0 0; 12 0] @test storedlength(b) == 1 - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a[1, 2] = 12 b = map(x -> 2x, a) @test b isa SparseArrayDOK{elt,2} @test b == [0 24; 0 0] @test storedlength(b) == 1 - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a[1, 2] = 12 b = a * a' @test b isa SparseArrayDOK{elt,2} @test b == [144 0; 0 0] @test storedlength(b) == 1 - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a[1, 2] = 12 b = a .+ 2 .* a' @test b isa SparseArrayDOK{elt,2} @test b == [0 12; 24 0] @test storedlength(b) == 2 - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a[1, 2] = 12 b = a[1:2, 2] @test b isa SparseArrayDOK{elt,1} @test b == [12, 0] @test storedlength(b) == 1 - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) @test iszero(a) a[2, 1] = 21 a[1, 2] = 12 @@ -101,7 +101,7 @@ arrayts = (Array,) @test sum(a) == 33 @test mapreduce(x -> 2x, +, a) == 66 - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a[1, 2] = 12 b = similar(a) copyto!(b, a) @@ -110,58 +110,58 @@ arrayts = (Array,) @test b[1, 2] == 12 @test storedlength(b) == 1 - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a .= 2 @test storedlength(a) == length(a) for I in eachindex(a) @test a[I] == 2 end - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) fill!(a, 2) @test storedlength(a) == length(a) for I in eachindex(a) @test a[I] == 2 end - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a[1, 2] = 12 zero!(a) @test iszero(a) @test iszero(storedlength(a)) - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a[1, 2] = 12 b = zero(a) @test b isa SparseArrayDOK{elt,2} @test iszero(b) @test iszero(storedlength(b)) - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a[1, 2] = 12 - b = SparseArrayDOK{elt}(4, 4) + b = SparseArrayDOK{elt}(undef, 4, 4) b[2:3, 2:3] .= a @test isone(storedlength(b)) @test b[2, 3] == 12 - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a[1, 2] = 12 - b = SparseArrayDOK{elt}(4, 4) + b = SparseArrayDOK{elt}(undef, 4, 4) b[2:3, 2:3] = a @test isone(storedlength(b)) @test b[2, 3] == 12 - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a[1, 2] = 12 - b = SparseArrayDOK{elt}(4, 4) + b = SparseArrayDOK{elt}(undef, 4, 4) c = @view b[2:3, 2:3] c .= a @test isone(storedlength(b)) @test b[2, 3] == 12 - a1 = SparseArrayDOK{elt}(2, 2) + a1 = SparseArrayDOK{elt}(undef, 2, 2) a1[1, 2] = 12 - a2 = SparseArrayDOK{elt}(2, 2) + a2 = SparseArrayDOK{elt}(undef, 2, 2) a2[2, 1] = 21 b = cat(a1, a2; dims=(1, 2)) @test b isa SparseArrayDOK{elt,2} @@ -173,21 +173,21 @@ arrayts = (Array,) # Printing # Not testing other element types since they change the # spacing so it isn't easy to make the test general. - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a[1, 2] = 12 @test sprint(show, "text/plain", a) == "$(summary(a)):\n ⋅ $(eltype(a)(12))\n ⋅ ⋅" end # Regression test for: # https://github.com/ITensor/SparseArraysBase.jl/issues/19 - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a[1, 1] = 1 a .*= 2 @test a == [2 0; 0 0] @test storedlength(a) == 1 # Test aliasing behavior. - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a[1, 1] = 11 a[1, 2] = 12 a[2, 2] = 22 @@ -202,7 +202,7 @@ arrayts = (Array,) @test storedlength(a) == 3 # Test aliasing behavior. - a = SparseArrayDOK{elt}(2, 2) + a = SparseArrayDOK{elt}(undef, 2, 2) a[1, 1] = 11 a[1, 2] = 12 a[2, 2] = 22