Skip to content

Commit

Permalink
Document more details on array assignment (#30092)
Browse files Browse the repository at this point in the history
* Followup to #29167.

More carefully explain the scalar case, and lead with the shape of the indices to drive the discussion
  • Loading branch information
mbauman authored Dec 6, 2018
1 parent 55e95ad commit fcd031b
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions doc/src/manual/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ julia> [(i,j) for i=1:3 for j=1:i if i+j == 4]

## [Indexing](@id man-array-indexing)

The general syntax for indexing into an n-dimensional array A is:
The general syntax for indexing into an n-dimensional array `A` is:

```
X = A[I_1, I_2, ..., I_n]
Expand All @@ -295,8 +295,8 @@ If all the indices are scalars, then the result `X` is a single element from the
`X` is an array with the same number of dimensions as the sum of the dimensionalities of all the
indices.

If all indices are vectors, for example, then the shape of `X` would be `(length(I_1), length(I_2), ..., length(I_n))`,
with location `(i_1, i_2, ..., i_n)` of `X` containing the value `A[I_1[i_1], I_2[i_2], ..., I_n[i_n]]`.
If all indices `I_k` are vectors, for example, then the shape of `X` would be `(length(I_1), length(I_2), ..., length(I_n))`,
with location `i_1, i_2, ..., i_n` of `X` containing the value `A[I_1[i_1], I_2[i_2], ..., I_n[i_n]]`.

Example:

Expand Down Expand Up @@ -364,9 +364,9 @@ julia> A[[1 2; 1 2], 1, 2, 1]
5 6
```

The location `(i_1, i_2, i_3, ..., i_{n+1})` contains the value at `A[I_1[i_1, i_2], I_2[i_3], ..., I_n[i_{n+1}]]`.
All dimensions 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]`.
The location `i_1, i_2, i_3, ..., i_{n+1}` contains the value at `A[I_1[i_1, i_2], I_2[i_3], ..., I_n[i_{n+1}]]`.
All dimensions indexed with scalars are dropped. For example, if `J` is an array of indices, then the result of `A[2, J, 3]` is an
array with size `size(J)`. Its `j`th element is populated by `A[2, J[j], 3]`.

As a special part of this syntax, the `end` keyword may be used to represent the last index of
each dimension within the indexing brackets, as determined by the size of the innermost array
Expand Down Expand Up @@ -410,7 +410,7 @@ julia> searchsorted(a, 4)

## Assignment

The general syntax for assigning values in an n-dimensional array A is:
The general syntax for assigning values in an n-dimensional array `A` is:

```
A[I_1, I_2, ..., I_n] = X
Expand All @@ -422,10 +422,18 @@ where each `I_k` may be a scalar integer, an array of integers, or any other
ranges of the form `a:c` or `a:b:c` to select contiguous or strided
subsections, and arrays of booleans to select elements at their `true` indices.

If `X` is an array, it must have the same number of elements as the product of the lengths of
the indices: `prod(length(I_1), length(I_2), ..., length(I_n))`. The value in location `I_1[i_1], I_2[i_2], ..., I_n[i_n]`
of `A` is overwritten with the value `X[i_1, i_2, ..., i_n]`. If `X` is a scalar, use the
element-wise assignment operator `.=` to write the value to all referenced locations of `A`:
If all indices `I_k` are integers, then the value in location `I_1, I_2, ..., I_n` of `A` is
overwritten with the value of `X`, [`convert`](@ref)ing to the
[`eltype`](@ref) of `A` if necessary.


If any index `I_k` selects more than one location, then the right hand side `X` must be an
array with the same shape as the result of indexing `A[I_1, I_2, ..., I_n]` or a vector with
the same number of elements. The value in location `I_1[i_1], I_2[i_2], ..., I_n[i_n]` of
`A` is overwritten with the value `X[I_1, I_2, ..., I_n]`, converting if necessary. The
element-wise assignment operator `.=` may be used to [broadcast](@ref Broadcasting) `X`
across the selected locations:


```
A[I_1, I_2, ..., I_n] .= X
Expand Down

2 comments on commit fcd031b

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

Please sign in to comment.