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

Broadcasting BlockedUnitRange sometimes loses blocking information #334

Open
mtfishman opened this issue Mar 19, 2024 · 2 comments
Open

Comments

@mtfishman
Copy link
Contributor

For example (with BlockArrays v0.16.39):

julia> using BlockArrays

julia> x = blockedrange([2, 4])
2-blocked 6-element BlockedUnitRange{Vector{Int64}}:
 1
 23
 4
 5
 6

julia> sin.(x)
2-blocked 6-element PseudoBlockVector{Float64}:
  0.8414709848078965 
  0.9092974268256817 
 ────────────────────
  0.1411200080598672 
 -0.7568024953079282 
 -0.9589242746631385 
 -0.27941549819892586

julia> x .+ 2
3:8
@jishnub
Copy link
Member

jishnub commented Mar 20, 2024

This happens because of the broadcast implementations in Base that preserves ranges but not their axes. In principle, we may return

julia> BlockArray(x .+ 2, blocksizes(x,1))
2-blocked 6-element BlockVector{Int64, Vector{UnitRange{Int64}}, Tuple{BlockedUnitRange{Vector{Int64}}}}:
 3
 45
 6
 7
 8

but blocksizes(x,1) will need to allocate the result. I wonder if there's a non-allocating way of obtaining the result?

Given that blockedrange necessarily sets the first element of the result to 1, perhaps this should lead to a BlockedOneTo instead of a BlockedUnitRange? In this case, we may be able to avoid the allocation, as a OneTo is its own axis.

@dlfivefifty
Copy link
Member

A BlockedOneTo would be great to have

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants