Skip to content
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 <support@itensor.org> and contributors"]
version = "0.7.5"
version = "0.7.6"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down
58 changes: 29 additions & 29 deletions test/test_dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,36 @@ using Test: @test, @testset
elts = (Float32, ComplexF64)
arrayts = (Array, JLArray)
@testset "dense (arraytype=$arrayt, eltype=$elt)" for arrayt in arrayts, elt in elts
dev(x) = adapt(arrayt, x)
dev(x) = adapt(arrayt, x)

@testset "SparseArrayDOK" begin
s = sparsezeros(elt, 3, 4)
s[1, 2] = 2
d = dense(s)
@test d isa Matrix{elt}
@test d == [0 2 0 0; 0 0 0 0; 0 0 0 0]
end

@testset "Custom sparse array" begin
struct MySparseArrayDOK{T,N,S<:AbstractVector{T}} <: AbstractArray{T,N}
storedvalues::S
storedindices::Dict{CartesianIndex{N},Int}
size::NTuple{N,Int}
end
Base.size(a::MySparseArrayDOK) = a.size
function Base.getindex(a::MySparseArrayDOK{<:Any,N}, I::Vararg{Int,N}) where {N}
storageindex = get(a.storedindices, CartesianIndex(I), nothing)
isnothing(storageindex) && return zero(eltype(a))
return a.storedvalues[storageindex]
@testset "SparseArrayDOK" begin
s = sparsezeros(elt, 3, 4)
s[1, 2] = 2
d = dense(s)
@test d isa Matrix{elt}
@test d == [0 2 0 0; 0 0 0 0; 0 0 0 0]
end
Base.parent(a::MySparseArrayDOK) = a.storedvalues

s = MySparseArrayDOK(
dev(elt[2, 4]), Dict([CartesianIndex(1, 2) => 1, CartesianIndex(3, 4) => 2]), (3, 4)
)
d = dense(s)
@show typeof(d)
@test d isa arrayt{elt,2}
@test d == dev(elt[0 2 0 0; 0 0 0 0; 0 0 0 4])
end
@testset "Custom sparse array" begin
struct MySparseArrayDOK{T, N, S <: AbstractVector{T}} <: AbstractArray{T, N}
storedvalues::S
storedindices::Dict{CartesianIndex{N}, Int}
size::NTuple{N, Int}
end
Base.size(a::MySparseArrayDOK) = a.size
function Base.getindex(a::MySparseArrayDOK{<:Any, N}, I::Vararg{Int, N}) where {N}
storageindex = get(a.storedindices, CartesianIndex(I), nothing)
isnothing(storageindex) && return zero(eltype(a))
return a.storedvalues[storageindex]
end
Base.parent(a::MySparseArrayDOK) = a.storedvalues

s = MySparseArrayDOK(
dev(elt[2, 4]), Dict([CartesianIndex(1, 2) => 1, CartesianIndex(3, 4) => 2]), (3, 4)
)
d = dense(s)
@show typeof(d)
@test d isa arrayt{elt, 2}
@test d == dev(elt[0 2 0 0; 0 0 0 0; 0 0 0 4])
end
end
Loading