-
Notifications
You must be signed in to change notification settings - Fork 66
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
0-length recycling with shape_broadcast()
#299
Comments
I thought about this a bit more, and compared with both numpy and xtensor. I think numpy does the correct thing. It consistently applies broadcasting rules in such a way that having The tl;dr is that vctrs does not quite do the right thing yet, if we want to consistently follow broadcasting rules for the shape. The crux of the problem can be represented in two questions (below, treat the shape in the vctrs sense, all of the dimensions except the first): What should the common dimensions of the two shapes (1, 1) and (0, 1) be? vctrs treats the common dimension as (0, 1) because a 0 is present, and numpy would also say the common dimension is (0, 1), but it does so by applying broadcasting rules consistently (a 1 was present, so the other dimension is used, which happens to be 0). What should the common dimensions of the two shapes (2, 1) and (0, 1) be? vctrs treats the common dimension as (0, 1) because a 0 is present, BUT numpy would error because this breaks broadcasting rules. The first dimensions are Here is the vctrs behavior: library(vctrs)
x <- array(1, c(1, 1, 1))
y <- array(1, c(1, 0, 1))
# shapes of (1, 1) and (0, 1) -> common shape (0, 1)
# this looks right
vctrs:::shape_common(x, y)
#> [1] 0 1
vctrs:::shape_broadcast(x, y)
#> , , 1
#>
#>
#> [1,]
z <- array(1, c(1, 2, 1))
# shapes of (2, 1) and (0, 1) -> error
# this is not what happens
# instead we get shape of (0, 1)
vctrs:::shape_common(z, y)
#> [1] 0 1
vctrs:::shape_broadcast(z, y)
#> , , 1
#>
#>
#> [1,] Created on 2019-05-01 by the reprex package (v0.2.1.9000) If you want to fix this issue, I think it requires changes to It would also require a tweak to |
This issue was originally mentioned in Slack.
Currently, you can
shape_broadcast()
from having 1 column down to having 0 columns. I am unsure if we want this behavior.The text was updated successfully, but these errors were encountered: