-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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 pointwise indexing operator #2591
Comments
Very good write-up. Quite compelling. |
This is indeed quite compelling. Keeping syntax aside, I don't like the idea of having a whole new operator for this. Will sleep on this for a bit. |
Can't this already be performed with X[f(inds...)] for suitable |
Yes, it's possible with |
Would a The shape of diagonals = matrix[i,i for i = 1:min(size(matrix))] or else do linear indexing mycopy = matrix[sub2ind(size(matrix), i, j) for i = 1:end, j = 1:end] (actually my usage of |
This proposal is growing on me, thanks to the analogy with dotted function calls and the potential of dot fusion. If I understand it correctly, the original example can in Julia 0.6 be implemented as
and it would participate in dot fusion. It does break down a bit for scalar |
Perhaps we should also deprecate the use of the getindex syntax within Personally, I still find the interplay between non-scalar non-dotted indexing and dot-indexin quite confusing, but perhaps it's something that we'll get used to. |
Re #27563 (comment):
|
I would like to suggest to add a broadcasting pointwise indexing operator to Julia. This a very expressive form of indexing, and quite handy, as remarked in #2247.
Let us call the new operation
pointref(A, inds...) = A.[inds...]
, whereA
and the elements ofinds
are arrays. Then we can computeas follows:
Broadcast all elements of
inds
to the same shape: Extrude each singletonaxis in each
ind
array to the common non-singleton size of thataxis among them. It is an error if the indices cannot be broadcast together.
The shape of
X
is the common broadcast shape of the indices.Each entry of
X
is calculated by indexingA
with the elements ofinds
at the corresponding position. Suppose thatinds
have been broadcast to the same shape. Thenfor each element of the result
X[ks...]
,Example:
Since
size(P) = (1,3)
andsize(Q) = (2,3)
, their broadcast shape is(2,3)
. The broadcast index arrays arewhere
P
has been extruded along the first axis. ThenRelationship to current indexing
Current Julia indexing allows
where the elements of
inds
are vectors and/or scalars. This axiswise indexing can be expressed using pointwise indexing aswhere
swapdims(A,j,k)
swaps axesj
andk
of an array. We see that pointwise and axiswise indexing coincide when there is only one index, sinceswapdims(A,1,1)
is a noop.Julia also supports linear indexing
Analogously, one could consider
i.e.
A.[ind]
looks does a linear-index lookup inA
for each element in the index arrayind
.This seems to be the same behavior as suggested in #2247 for
A[ind]
.Wrap-up
A[B]
, where the two can be said to coincide. (As described in RFC: Linear indexing with multidimensional index. #2247 (comment), numpy does this also in the multi-index case, which becomes messy.)The text was updated successfully, but these errors were encountered: