Skip to content

Commit 3583101

Browse files
authored
[NDTensors] Fix diagindices for empty arrays (#1649)
1 parent 69433ec commit 3583101

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

NDTensors/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "NDTensors"
22
uuid = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf"
33
authors = ["Matthew Fishman <[email protected]>"]
4-
version = "0.4.6"
4+
version = "0.4.7"
55

66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"

NDTensors/src/abstractarray/diaginterface.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ function diagstride(a::AbstractArray)
1616
end
1717

1818
function diagindices(a::AbstractArray)
19-
maxdiag = LinearIndices(a)[CartesianIndex(ntuple(Returns(diaglength(a)), ndims(a)))]
19+
maxdiag = if isempty(a)
20+
0
21+
else
22+
LinearIndices(a)[CartesianIndex(ntuple(Returns(diaglength(a)), ndims(a)))]
23+
end
2024
return 1:diagstride(a):maxdiag
2125
end
2226

NDTensors/test/test_diag.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ using NDTensors:
1313
data,
1414
dense,
1515
diaglength,
16+
diagindices,
1617
matrix,
1718
randomTensor,
1819
tensor
@@ -79,6 +80,15 @@ using .NDTensorsTestUtils: devices_list, is_supported_eltype
7980
@test S[i, i] == 2 * D[i, i]
8081
end
8182

83+
a = dev(tensor(Dense(randn(elt, 3)), (3,)))
84+
@test diagindices(a) == 1:1:3
85+
a = dev(tensor(Dense(randn(elt, 9)), (3, 3)))
86+
@test diagindices(a) == 1:4:9
87+
a = dev(tensor(Dense(randn(elt, 36)), (3, 4, 3)))
88+
@test diagindices(a) == 1:16:33
89+
a = dev(tensor(Dense(randn(elt, 0)), (3, 0)))
90+
@test diagindices(a) == 1:1:0
91+
8292
# Regression test for https://github.com/ITensor/ITensors.jl/issues/1199
8393
S = dev(tensor(Diag(randn(elt, 2)), (2, 2)))
8494
## This was creating a `Dense{ReshapedArray{Adjoint{Matrix}}}` which, in mul!, was

0 commit comments

Comments
 (0)