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

Move array documentation out of HelpDB, add examples #17795

Merged
merged 3 commits into from
Aug 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
43 changes: 43 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,49 @@ foreach(f, itrs...) = (for z in zip(itrs...); f(z...); end; nothing)
## transform any set of dimensions
## dims specifies which dimensions will be transformed. for example
## dims==1:2 will call f on all slices A[:,:,...]
"""
mapslices(f, A, dims)

Transform the given dimensions of array `A` using function `f`. `f` is called on each slice
of `A` of the form `A[...,:,...,:,...]`. `dims` is an integer vector specifying where the
colons go in this expression. The results are concatenated along the remaining dimensions.
For example, if `dims` is `[1,2]` and `A` is 4-dimensional, `f` is called on `A[:,:,i,j]`
for all `i` and `j`.

```jldoctest
julia> a = reshape(collect(1:16),(2,2,2,2))
2×2×2×2 Array{Int64,4}:
[:, :, 1, 1] =
1 3
2 4
<BLANKLINE>
[:, :, 2, 1] =
5 7
6 8
<BLANKLINE>
[:, :, 1, 2] =
9 11
10 12
<BLANKLINE>
[:, :, 2, 2] =
13 15
14 16

julia> mapslices(sum, a, [1,2])
1×1×2×2 Array{Int64,4}:
[:, :, 1, 1] =
10
<BLANKLINE>
[:, :, 2, 1] =
26
<BLANKLINE>
[:, :, 1, 2] =
42
<BLANKLINE>
[:, :, 2, 2] =
58
```
"""
mapslices(f, A::AbstractArray, dims) = mapslices(f, A, [dims...])
function mapslices(f, A::AbstractArray, dims::AbstractVector)
if isempty(dims)
Expand Down
141 changes: 141 additions & 0 deletions base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,23 @@ function flipdim{T}(A::Array{T}, d::Integer)
return B
end

"""
rotl90(A)

Rotate matrix `A` left 90 degrees.

```jldoctest
julia> a = [1 2; 3 4]
2×2 Array{Int64,2}:
1 2
3 4

julia> rotl90(a)
2×2 Array{Int64,2}:
2 4
1 3
```
"""
function rotl90(A::AbstractMatrix)
ind1, ind2 = indices(A)
B = similar(A, (ind2,ind1))
Expand All @@ -168,6 +185,24 @@ function rotl90(A::AbstractMatrix)
end
return B
end

"""
rotr90(A)

Rotate matrix `A` right 90 degrees.

```jldoctest
julia> a = [1 2; 3 4]
2×2 Array{Int64,2}:
1 2
3 4

julia> rotr90(a)
2×2 Array{Int64,2}:
3 1
4 2
```
"""
function rotr90(A::AbstractMatrix)
ind1, ind2 = indices(A)
B = similar(A, (ind2,ind1))
Expand All @@ -177,6 +212,23 @@ function rotr90(A::AbstractMatrix)
end
return B
end
"""
rot180(A)

Rotate matrix `A` 180 degrees.

```jldoctest
julia> a = [1 2; 3 4]
2×2 Array{Int64,2}:
1 2
3 4

julia> rot180(a)
2×2 Array{Int64,2}:
4 3
2 1
```
"""
function rot180(A::AbstractMatrix)
B = similar(A)
ind1, ind2 = indices(A,1), indices(A,2)
Expand All @@ -186,13 +238,102 @@ function rot180(A::AbstractMatrix)
end
return B
end
"""
rotl90(A, k)

Rotate matrix `A` left 90 degrees an integer `k` number of times.
If `k` is zero or a multiple of four, this is equivalent to a `copy`.

```jldoctest
julia> a = [1 2; 3 4]
2×2 Array{Int64,2}:
1 2
3 4

julia> rotl90(a,1)
2×2 Array{Int64,2}:
2 4
1 3

julia> rotl90(a,2)
2×2 Array{Int64,2}:
4 3
2 1

julia> rotl90(a,3)
2×2 Array{Int64,2}:
3 1
4 2

julia> rotl90(a,4)
2×2 Array{Int64,2}:
1 2
3 4
```
"""
function rotl90(A::AbstractMatrix, k::Integer)
k = mod(k, 4)
k == 1 ? rotl90(A) :
k == 2 ? rot180(A) :
k == 3 ? rotr90(A) : copy(A)
end
"""
rotr90(A, k)

Rotate matrix `A` right 90 degrees an integer `k` number of times. If `k` is zero or a
multiple of four, this is equivalent to a `copy`.

```jldoctest
julia> a = [1 2; 3 4]
2×2 Array{Int64,2}:
1 2
3 4

julia> rotr90(a,1)
2×2 Array{Int64,2}:
3 1
4 2

julia> rotr90(a,2)
2×2 Array{Int64,2}:
4 3
2 1

julia> rotr90(a,3)
2×2 Array{Int64,2}:
2 4
1 3

julia> rotr90(a,4)
2×2 Array{Int64,2}:
1 2
3 4
```
"""
rotr90(A::AbstractMatrix, k::Integer) = rotl90(A,-k)
"""
rot180(A, k)

Rotate matrix `A` 180 degrees an integer `k` number of times.
If `k` is even, this is equivalent to a `copy`.

```jldoctest
julia> a = [1 2; 3 4]
2×2 Array{Int64,2}:
1 2
3 4

julia> rot180(a,1)
2×2 Array{Int64,2}:
4 3
2 1

julia> rot180(a,2)
2×2 Array{Int64,2}:
1 2
3 4
```
"""
rot180(A::AbstractMatrix, k::Integer) = mod(k, 2) == 1 ? rot180(A) : copy(A)

## Transpose ##
Expand Down
95 changes: 0 additions & 95 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -504,19 +504,6 @@ Get a backtrace object for the current program point.
"""
backtrace

"""
reducedim(f, A, dims[, initial])

Reduce 2-argument function `f` along dimensions of `A`. `dims` is a vector specifying the
dimensions to reduce, and `initial` is the initial value to use in the reductions. For `+`, `*`,
`max` and `min` the `initial` argument is optional.

The associativity of the reduction is implementation-dependent; if you need a particular
associativity, e.g. left-to-right, you should write your own loop. See documentation for
`reduce`.
"""
reducedim

"""
-(x)

Expand Down Expand Up @@ -1119,17 +1106,6 @@ representable.
"""
ceil

"""
mapslices(f, A, dims)

Transform the given dimensions of array `A` using function `f`. `f` is called on each slice
of `A` of the form `A[...,:,...,:,...]`. `dims` is an integer vector specifying where the
colons go in this expression. The results are concatenated along the remaining dimensions.
For example, if `dims` is `[1,2]` and `A` is 4-dimensional, `f` is called on `A[:,:,i,j]`
for all `i` and `j`.
"""
mapslices

"""
issocket(path) -> Bool

Expand Down Expand Up @@ -2350,15 +2326,6 @@ Bessel function of the second kind of order 1, ``Y_1(x)``.
"""
bessely1

"""
cumprod(A, [dim])

Cumulative product along a dimension `dim` (defaults to 1). See also
[`cumprod!`](:func:`cumprod!`) to use a preallocated output array, both for performance and
to control the precision of the output (e.g. to avoid overflow).
"""
cumprod

"""
besseljx(nu, x)

Expand Down Expand Up @@ -3105,21 +3072,6 @@ block to create a new scope with copies of all variables referenced in the expre
"""
:@async

"""
rotr90(A)

Rotate matrix `A` right 90 degrees.
"""
rotr90(A)

"""
rotr90(A, k)

Rotate matrix `A` right 90 degrees an integer `k` number of times. If `k` is zero or a
multiple of four, this is equivalent to a `copy`.
"""
rotr90(A, k)

"""
readdir([dir]) -> Vector{String}

Expand Down Expand Up @@ -3591,21 +3543,6 @@ Number of ways to choose `k` out of `n` items.
"""
binomial

"""
rot180(A)

Rotate matrix `A` 180 degrees.
"""
rot180(A)

"""
rot180(A, k)

Rotate matrix `A` 180 degrees an integer `k` number of times. If `k` is even, this is
equivalent to a `copy`.
"""
rot180(A, k)

"""
.<=(x, y)
.≤(x,y)
Expand Down Expand Up @@ -3979,21 +3916,6 @@ x == div(x,y)*y + rem(x,y)
"""
rem

"""
rotl90(A)

Rotate matrix `A` left 90 degrees.
"""
rotl90(A)

"""
rotl90(A, k)

Rotate matrix `A` left 90 degrees an integer `k` number of times. If `k` is zero or a
multiple of four, this is equivalent to a `copy`.
"""
rotl90(A, k)

"""
info(msg)

Expand Down Expand Up @@ -5762,14 +5684,6 @@ handle comparison to other types via promotion rules where possible.
"""
Base.:(==)

"""
mapreducedim(f, op, A, dims[, initial])

Evaluates to the same as `reducedim(op, map(f, A), dims, f(initial))`, but is generally
faster because the intermediate array is avoided.
"""
mapreducedim

"""
seekstart(s)

Expand Down Expand Up @@ -6554,15 +6468,6 @@ the group owning the file
"""
operm

"""
cumsum(A, [dim])

Cumulative sum along a dimension `dim` (defaults to 1). See also [`cumsum!`](:func:`cumsum!`)
to use a preallocated output array, both for performance and to control the precision of the
output (e.g. to avoid overflow).
"""
cumsum

"""
rpad(string, n, p)

Expand Down
Loading