Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reshape for OneElement when indices lie outside axes #378

Merged
merged 1 commit into from
Aug 27, 2024
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 @@

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)))

Check warning on line 423 in src/oneelement.jl

View check run for this annotation

Codecov / codecov/patch

src/oneelement.jl#L423

Added line #L423 was not covered by tests
# we use the fact that the linear index of the non-zero value is preserved
oldlinind = LinearIndices(A)[A.ind...]
newcartind = CartesianIndices(shape)[oldlinind]

Check warning on line 426 in src/oneelement.jl

View check run for this annotation

Codecov / codecov/patch

src/oneelement.jl#L425-L426

Added lines #L425 - L426 were not covered by tests
else
# arbitrarily set to some value outside the domain
newcartind = shape .+ 1

Check warning on line 429 in src/oneelement.jl

View check run for this annotation

Codecov / codecov/patch

src/oneelement.jl#L429

Added line #L429 was not covered by tests
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
Loading