Skip to content

Commit 554ddf4

Browse files
authored
Fix reshape for OneElement when indices lie outside axes (#378)
1 parent f72e220 commit 554ddf4

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/oneelement.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,14 @@ end
419419

420420
function Base.reshape(A::OneElement, shape::Tuple{Vararg{Int}})
421421
prod(shape) == length(A) || throw(DimensionMismatch("new dimension $shape must be consistent with array size $(length(A))"))
422-
# we use the fact that the linear index of the non-zero value is preserved
423-
oldlinind = LinearIndices(A)[A.ind...]
424-
newcartind = CartesianIndices(shape)[oldlinind]
422+
if all(in.(A.ind, axes(A)))
423+
# we use the fact that the linear index of the non-zero value is preserved
424+
oldlinind = LinearIndices(A)[A.ind...]
425+
newcartind = CartesianIndices(shape)[oldlinind]
426+
else
427+
# arbitrarily set to some value outside the domain
428+
newcartind = shape .+ 1
429+
end
425430
OneElement(A.val, Tuple(newcartind), shape)
426431
end
427432

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2328,6 +2328,10 @@ end
23282328
end
23292329
O = OneElement(2, (), ())
23302330
@test reshape(O, ()) === O
2331+
2332+
O = OneElement(5, 3)
2333+
@test reshape(O, 1, 3) == reshape(Array(O), 1, 3)
2334+
@test reshape(reshape(O, 1, 3), 3) == O
23312335
end
23322336

23332337
@testset "isassigned" begin

0 commit comments

Comments
 (0)