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
11 changes: 8 additions & 3 deletions src/oneelement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,14 @@ end

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

Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2326,6 +2326,10 @@ end
end
O = OneElement(2, (), ())
@test reshape(O, ()) === O

O = OneElement(5, 3)
@test reshape(O, 1, 3) == reshape(Array(O), 1, 3)
@test reshape(reshape(O, 1, 3), 3) == O
end

@testset "isassigned" begin
Expand Down