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

Support indexing Broadcasted objects using Integers #56470

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

jishnub
Copy link
Contributor

@jishnub jishnub commented Nov 6, 2024

This adds support for IndexLinear eachindex, as well as bounds-checking and getindex for a Broadcasted with an Integer index.

Instead of using the number of dimensions in eachindex to determine whether to use CartesianIndices, we may use the IndexStyle. This should not change anything currently, but this adds the possibility of nD broadcasting using linear indexing if the IndexStyle of the Broadcasted object is IndexLinear.

After this,

julia> bc = Broadcast.broadcasted(+, reshape(1:4, 2, 2), 1:2)
Base.Broadcast.Broadcasted(+, ([1 3; 2 4], 1:2))

julia> eachindex(bc)
CartesianIndices((2, 2))

julia> eachindex(IndexLinear(), bc)
Base.OneTo(4)

julia> [bc[I] for I in eachindex(IndexLinear(), bc)]
4-element Vector{Int64}:
 2
 4
 4
 6

julia> vec(collect(bc))
4-element Vector{Int64}:
 2
 4
 4
 6

This PR doesn't add true linear indexing support for IndexCartesian Broadcasted objects. In such cases, an Integer index is converted to a CartesianIndex before it is used in indexing.

@jishnub jishnub added the broadcast Applying a function over a collection label Nov 6, 2024
@jishnub jishnub changed the title Broadcast eachindex using IndexStyles Evaluate eachindex for a Broadcasted object using its IndexStyle Nov 6, 2024
@jishnub jishnub added the feature Indicates new feature / enhancement requests label Nov 6, 2024
@jishnub jishnub marked this pull request as draft November 6, 2024 10:18
@jishnub jishnub marked this pull request as ready for review November 6, 2024 10:58
@jishnub jishnub changed the title Evaluate eachindex for a Broadcasted object using its IndexStyle Index Broadcasted objects using Integers Nov 6, 2024
@jishnub jishnub changed the title Index Broadcasted objects using Integers Support indexing Broadcasted objects using Integers Nov 6, 2024
@jishnub jishnub requested a review from mbauman November 9, 2024 05:24
@jishnub
Copy link
Contributor Author

jishnub commented Nov 20, 2024

Gentle bump

2 similar comments
@jishnub
Copy link
Contributor Author

jishnub commented Nov 27, 2024

Gentle bump

@jishnub
Copy link
Contributor Author

jishnub commented Dec 5, 2024

Gentle bump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
broadcast Applying a function over a collection feature Indicates new feature / enhancement requests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant