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

APL indexing #15431

Merged
merged 4 commits into from
May 1, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Only allow logical indexing with vectors or...
a single index that matches the size of the array it indexes into
  • Loading branch information
mbauman committed May 1, 2016
commit ddedbdb7a2736b6b89b216171dd9c53102541116
2 changes: 1 addition & 1 deletion base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function checkbounds(::Type{Bool}, sz::Integer, r::Range)
@_propagate_inbounds_meta
isempty(r) | (checkbounds(Bool, sz, first(r)) & checkbounds(Bool, sz, last(r)))
end
checkbounds(::Type{Bool}, sz::Integer, I::AbstractArray{Bool}) = length(I) == sz
checkbounds{N}(::Type{Bool}, sz::Integer, I::AbstractArray{Bool,N}) = N == 1 && length(I) == sz
function checkbounds(::Type{Bool}, sz::Integer, I::AbstractArray)
@_inline_meta
b = true
Expand Down
2 changes: 1 addition & 1 deletion base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ using .IteratorsMD
# improvement over the general definitions in abstractarray.jl
for N = 1:5
args = [:($(symbol(:I, d))) for d = 1:N]
targs = [:($(symbol(:I, d))::Union{Colon,Number,AbstractVector}) for d = 1:N] # prevent co-opting the CartesianIndex version
targs = [:($(symbol(:I, d))::Union{Colon,Number,AbstractArray}) for d = 1:N] # prevent co-opting the CartesianIndex version
exs = [:(checkbounds(Bool, size(A, $d), $(args[d]))) for d = 1:N]
cbexpr = exs[1]
for d = 2:N
Expand Down
15 changes: 9 additions & 6 deletions doc/manual/arrays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ where each ``I_k`` may be:
2. A ``Range`` of the form ``a:b``, or ``a:b:c``
3. A ``:`` or ``Colon()`` to select entire dimensions
4. An arbitrary integer array, including the empty array ``[]``
5. A boolean array to select elements at its ``true`` indices
5. A boolean array to select a vector of elements at its ``true`` indices

If all the indices are scalars, then the result ``X`` is a single element from
the array ``A``. Otherwise, ``X`` is an array with the same number of
Expand All @@ -282,11 +282,14 @@ indexed with scalars are dropped. For example, the result of ``A[2, I, 3]`` is
an array with size ``size(I)``. Its ``i``\ th element is populated by
``A[2, I[i], 3]``.

Boolean arrays must be the same length as the dimension they are indexing into.
Indexing by a boolean array ``B`` is the same as indexing by the vector that is
returned by ``find(B)``; the size of a dimension indexed by a boolean array will
be the number of true values in the vector. It is generally more efficient to
use boolean arrays as indices directly instead of first calling ``find``.
Indexing by a boolean array ``B`` is effectively the same as indexing by the
vector that is returned by :func:`find(B) <find>`. Often referred to as logical
indexing, this selects elements at the indices where the values are ``true``,
akin to a mask. A logical index must be a vector of the same length as the
dimension it indexes into, or it must be the only index provided and match the
size and dimensionality of the array it indexes into. It is generally more
efficient to use boolean arrays as indices directly instead of first calling
:func:`find`.

Additionally, single elements of a multidimensional array can be indexed as
``x = A[I]``, where ``I`` is a ``CartesianIndex``. It effectively behaves like
Expand Down