From 8fad80887a6a5ae182c13eea13146f68544d0198 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 3 Apr 2025 15:41:19 -0400 Subject: [PATCH] Define zeros --- Project.toml | 2 +- src/gradedarray.jl | 6 ++++++ test/test_gradedarray.jl | 21 ++++++++++++++++++--- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Project.toml b/Project.toml index 1c0e18b..56b1b8c 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "GradedArrays" uuid = "bc96ca6e-b7c8-4bb6-888e-c93f838762c2" authors = ["ITensor developers and contributors"] -version = "0.2.2" +version = "0.2.3" [deps] BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" diff --git a/src/gradedarray.jl b/src/gradedarray.jl index 7108b76..4d23d72 100644 --- a/src/gradedarray.jl +++ b/src/gradedarray.jl @@ -87,6 +87,12 @@ function Base.similar( return similar_blocksparse(a, elt, axes) end +function Base.zeros( + elt::Type, ax::Tuple{AbstractGradedUnitRange,Vararg{AbstractGradedUnitRange}} +) + return BlockSparseArray{elt}(undef, ax) +end + function getindex_blocksparse(a::AbstractArray, I::AbstractUnitRange...) a′ = similar(a, only.(axes.(I))...) a′ .= a diff --git a/test/test_gradedarray.jl b/test/test_gradedarray.jl index cab9301..233c386 100644 --- a/test/test_gradedarray.jl +++ b/test/test_gradedarray.jl @@ -57,9 +57,24 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64}) @test 2 * Array(a) == b end - d1 = gradedrange([U1(0) => 2, U1(1) => 2]) - d2 = gradedrange([U1(0) => 2, U1(1) => 2]) - a = randn_blockdiagonal(elt, (d1, d2, d1, d2)) + r = gradedrange([U1(0) => 2, U1(1) => 2]) + a = zeros(r, r, r, r) + @test a isa BlockSparseArray{Float64} + @test eltype(a) === Float64 + @test size(a) == (4, 4, 4, 4) + @test iszero(a) + @test iszero(blockstoredlength(a)) + + r = gradedrange([U1(0) => 2, U1(1) => 2]) + a = zeros(elt, r, r, r, r) + @test a isa BlockSparseArray{elt} + @test eltype(a) === elt + @test size(a) == (4, 4, 4, 4) + @test iszero(a) + @test iszero(blockstoredlength(a)) + + r = gradedrange([U1(0) => 2, U1(1) => 2]) + a = randn_blockdiagonal(elt, (r, r, r, r)) b = similar(a, ComplexF64) @test b isa BlockSparseArray{ComplexF64} @test eltype(b) === ComplexF64