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

reshape with zero dimensions #45589

Open
cossio opened this issue Jun 5, 2022 · 11 comments
Open

reshape with zero dimensions #45589

cossio opened this issue Jun 5, 2022 · 11 comments
Labels
arrays [a, r, r, a, y, s] bug Indicates an unexpected problem or unintended behavior

Comments

@cossio
Copy link
Contributor

cossio commented Jun 5, 2022

I think this should be able to return without error.

julia> reshape(randn(0,4),0,:)
ERROR: DivideError: integer division error
Stacktrace:
 [1] div
   @ ./int.jl:284 [inlined]
 [2] divrem
   @ ./div.jl:162 [inlined]
 [3] divrem
   @ ./div.jl:158 [inlined]
 [4] _reshape_uncolon
   @ ./reshapedarray.jl:127 [inlined]
 [5] reshape(parent::Matrix{Float64}, dims::Tuple{Int64, Colon})
   @ Base ./reshapedarray.jl:118
 [6] reshape(::Matrix{Float64}, ::Int64, ::Colon)
   @ Base ./reshapedarray.jl:117
 [7] top-level scope
   @ REPL[1]:1
@giordano
Copy link
Contributor

giordano commented Jun 5, 2022

What shape do you expect it to return?

@cossio
Copy link
Contributor Author

cossio commented Jun 5, 2022

@giordano (0, 4) ?

@giordano
Copy link
Contributor

giordano commented Jun 5, 2022

Why not (0, 6829)?

@cossio
Copy link
Contributor Author

cossio commented Jun 5, 2022

@giordano `(0,4) is the original size. I'd say it should prefer that?

@giordano
Copy link
Contributor

giordano commented Jun 5, 2022

Then my next question would be why you're reshaping it if you want to keep the same shape 🙂

@cossio
Copy link
Contributor Author

cossio commented Jun 5, 2022

Okay. How about reshape(zeros(0,4,3),0,:)? I think it could return zeros(0,12).

@jishnub
Copy link
Contributor

jishnub commented Jun 6, 2022

What shape do you expect it to return?

I think it's reasonable to expect it to be consistent with this:

julia> reshape(zeros(0,4), Val(2))
0×4 Matrix{Float64}

@JeffBezanson JeffBezanson added arrays [a, r, r, a, y, s] bug Indicates an unexpected problem or unintended behavior labels Jun 10, 2022
@JeffBezanson
Copy link
Member

Heh, never realized this works, though of course it makes sense:

julia> reshape(zeros(0,4),0,5)
0×5 Matrix{Float64}

Still it seems to me the example in the OP might as well work.

@nlw0
Copy link
Contributor

nlw0 commented Jul 12, 2022

Why not (0, 6829)?

Anything goes, so it should be able to return without an error...

@frankwswang
Copy link

frankwswang commented Jul 29, 2022

This discussion in the Julia Discourse might be relevant.

I think if the returned array's shape is the same as the input array's shape and the input array is a container of plain data (e.g. Float64) at the same time, reshape should just return the input array regardless of its dimensions, like the identity.

@nsajko
Copy link
Contributor

nsajko commented Aug 16, 2024

In the meantime I opened an issue that turns out to partially duplicate this one: #54245

Then I fixed it in #54261 (not in v1.11).

Perhaps the sizing for empty arrays should be considered again? The linked PR implements this behavior:

julia> a = randn(0,4)
0×4 Matrix{Float64}

julia> size(reshape(a, 0, :))
(0, 0)

julia> ans == size(reshape(a, Val(2)))
false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s] bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

7 participants