Skip to content

Commit

Permalink
More doctests for array fun (#24920)
Browse files Browse the repository at this point in the history
* Simple cleanup and doctest for views macro

* Doctests for accumulate! and copy!
  • Loading branch information
kshyatt authored Dec 19, 2017
1 parent 6e0549e commit 1044335
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
72 changes: 69 additions & 3 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ module IteratorsMD
CartesianIndices(A::AbstractArray) -> R
As a convenience, constructing a CartesianIndices from an array makes a
As a convenience, constructing a `CartesianIndices` from an array makes a
range of its indices.
# Examples
Expand Down Expand Up @@ -383,7 +383,7 @@ module IteratorsMD
LinearIndices(sz::Dims) -> R
LinearIndices(istart:istop, jstart:jstop, ...) -> R
Define a mapping between cartesian indices and the corresponding linear index into a CartesianIndices
Define a mapping between cartesian indices and the corresponding linear index into a `CartesianIndices`.
# Example
Expand Down Expand Up @@ -520,7 +520,7 @@ index_shape() = ()
The `LogicalIndex` type is a special vector that simply contains all indices I
where `mask[I]` is true. This specialized type does not support indexing
directly as doing so would require O(n) lookup time. `AbstractArray{Bool}` are
wrapped with `LogicalIndex` upon calling `to_indices`.
wrapped with `LogicalIndex` upon calling [`to_indices`](@ref).
"""
struct LogicalIndex{T, A<:AbstractArray{Bool}} <: AbstractVector{T}
mask::A
Expand Down Expand Up @@ -774,6 +774,7 @@ Cumulative sum along the dimension `dim`. See also [`cumsum!`](@ref)
to use a preallocated output array, both for performance and to control the precision of the
output (e.g. to avoid overflow).
# Examples
```jldoctest
julia> a = [1 2 3; 4 5 6]
2×3 Array{Int64,2}:
Expand Down Expand Up @@ -803,6 +804,7 @@ Cumulative sum a vector. See also [`cumsum!`](@ref)
to use a preallocated output array, both for performance and to control the precision of the
output (e.g. to avoid overflow).
# Examples
```jldoctest
julia> cumsum([1, 1, 1])
3-element Array{Int64,1}:
Expand Down Expand Up @@ -840,6 +842,7 @@ Cumulative product along the dimension `dim`. See also
[`cumprod!`](@ref) to use a preallocated output array, both for performance and
to control the precision of the output (e.g. to avoid overflow).
# Examples
```jldoctest
julia> a = [1 2 3; 4 5 6]
2×3 Array{Int64,2}:
Expand All @@ -866,6 +869,7 @@ Cumulative product of a vector. See also
[`cumprod!`](@ref) to use a preallocated output array, both for performance and
to control the precision of the output (e.g. to avoid overflow).
# Examples
```jldoctest
julia> cumprod(fill(1//2, 3))
3-element Array{Rational{Int64},1}:
Expand Down Expand Up @@ -907,6 +911,7 @@ to control the precision of the output (e.g. to avoid overflow). For common oper
there are specialized variants of `accumulate`, see:
[`cumsum`](@ref), [`cumprod`](@ref)
# Examples
```jldoctest
julia> accumulate(+, fill(1, 3, 3), 1)
3×3 Array{Int64,2}:
Expand Down Expand Up @@ -935,6 +940,7 @@ to control the precision of the output (e.g. to avoid overflow). For common oper
there are specialized variants of `accumulate`, see:
[`cumsum`](@ref), [`cumprod`](@ref)
# Examples
```jldoctest
julia> accumulate(+, [1,2,3])
3-element Array{Int64,1}:
Expand All @@ -956,6 +962,27 @@ accumulate(op, x::AbstractVector) = accumulate(op, x, 1)
Cumulative operation `op` on `A` along the dimension `dim`, storing the result in `B`.
See also [`accumulate`](@ref).
# Examples
```jldoctest
julia> A = [1 2; 3 4];
julia> B = [0 0; 0 0];
julia> accumulate!(-, B, A, 1);
julia> B
2×2 Array{Int64,2}:
1 2
-2 -2
julia> accumulate!(-, B, A, 2);
julia> B
2×2 Array{Int64,2}:
1 -1
3 -1
```
"""
function accumulate!(op, B, A, dim::Integer)
dim > 0 || throw(ArgumentError("dim must be a positive integer"))
Expand Down Expand Up @@ -988,6 +1015,25 @@ end
Cumulative operation `op` on a vector `x`, storing the result in `y`.
See also [`accumulate`](@ref).
# Examples
``jldoctest
julia> x = SparseVector(7, [1, 3, 5], [2., 4., 5.]);
julia> y = zeros(7);
julia> accumulate!(+, y, x);
julia> y
7-element Array{Float64,1}:
2.0
2.0
6.0
6.0
11.0
11.0
11.0
```
"""
function accumulate!(op::Op, y, x::AbstractVector) where Op
isempty(x) && return y
Expand Down Expand Up @@ -1101,9 +1147,29 @@ end
"""
copyto!(dest::AbstractArray, src) -> dest
Copy all elements from collection `src` to array `dest`, whose length must be greater than
or equal to the length `n` of `src`. The first `n` elements of `dest` are overwritten,
the other elements are left untouched.
# Examples
```jldoctest
julia> x = [1., 0., 3., 0., 5.];
julia> y = zeros(7);
julia> copyto!(y, x);
julia> y
7-element Array{Float64,1}:
1.0
0.0
3.0
0.0
5.0
0.0
0.0
```
"""
copyto!(dest, src)

Expand Down
25 changes: 21 additions & 4 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -553,13 +553,30 @@ end
Convert every array-slicing operation in the given expression
(which may be a `begin`/`end` block, loop, function, etc.)
to return a view. Scalar indices, non-array types, and
to return a view. Scalar indices, non-array types, and
explicit `getindex` calls (as opposed to `array[...]`) are
unaffected.
Note that the `@views` macro only affects `array[...]` expressions
that appear explicitly in the given `expression`, not array slicing that
occurs in functions called by that code.
!!! note
The `@views` macro only affects `array[...]` expressions
that appear explicitly in the given `expression`, not array slicing that
occurs in functions called by that code.
# Examples
```jldoctest
julia> A = zeros(3, 3);
julia> @views for row in 1:3
b = A[row, :]
b[:] = row
end
julia> A
3×3 Array{Float64,2}:
1.0 1.0 1.0
2.0 2.0 2.0
3.0 3.0 3.0
```
"""
macro views(x)
esc(_views(replace_ref_end!(x)))
Expand Down

2 comments on commit 1044335

@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.