Skip to content

Commit 3e00217

Browse files
authored
Move array documentation out of HelpDB, add examples (#17795)
Lots of examples for things that are probably confusing for new Julia users. Made `reducedim` and `mapreducedim` arguments more similar to the ones for `reduce` and `mapreduce`.
1 parent 21480a6 commit 3e00217

File tree

6 files changed

+506
-103
lines changed

6 files changed

+506
-103
lines changed

base/abstractarray.jl

+43
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,49 @@ foreach(f, itrs...) = (for z in zip(itrs...); f(z...); end; nothing)
13581358
## transform any set of dimensions
13591359
## dims specifies which dimensions will be transformed. for example
13601360
## dims==1:2 will call f on all slices A[:,:,...]
1361+
"""
1362+
mapslices(f, A, dims)
1363+
1364+
Transform the given dimensions of array `A` using function `f`. `f` is called on each slice
1365+
of `A` of the form `A[...,:,...,:,...]`. `dims` is an integer vector specifying where the
1366+
colons go in this expression. The results are concatenated along the remaining dimensions.
1367+
For example, if `dims` is `[1,2]` and `A` is 4-dimensional, `f` is called on `A[:,:,i,j]`
1368+
for all `i` and `j`.
1369+
1370+
```jldoctest
1371+
julia> a = reshape(collect(1:16),(2,2,2,2))
1372+
2×2×2×2 Array{Int64,4}:
1373+
[:, :, 1, 1] =
1374+
1 3
1375+
2 4
1376+
<BLANKLINE>
1377+
[:, :, 2, 1] =
1378+
5 7
1379+
6 8
1380+
<BLANKLINE>
1381+
[:, :, 1, 2] =
1382+
9 11
1383+
10 12
1384+
<BLANKLINE>
1385+
[:, :, 2, 2] =
1386+
13 15
1387+
14 16
1388+
1389+
julia> mapslices(sum, a, [1,2])
1390+
1×1×2×2 Array{Int64,4}:
1391+
[:, :, 1, 1] =
1392+
10
1393+
<BLANKLINE>
1394+
[:, :, 2, 1] =
1395+
26
1396+
<BLANKLINE>
1397+
[:, :, 1, 2] =
1398+
42
1399+
<BLANKLINE>
1400+
[:, :, 2, 2] =
1401+
58
1402+
```
1403+
"""
13611404
mapslices(f, A::AbstractArray, dims) = mapslices(f, A, [dims...])
13621405
function mapslices(f, A::AbstractArray, dims::AbstractVector)
13631406
if isempty(dims)

base/arraymath.jl

+141
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,23 @@ function flipdim{T}(A::Array{T}, d::Integer)
159159
return B
160160
end
161161

162+
"""
163+
rotl90(A)
164+
165+
Rotate matrix `A` left 90 degrees.
166+
167+
```jldoctest
168+
julia> a = [1 2; 3 4]
169+
2×2 Array{Int64,2}:
170+
1 2
171+
3 4
172+
173+
julia> rotl90(a)
174+
2×2 Array{Int64,2}:
175+
2 4
176+
1 3
177+
```
178+
"""
162179
function rotl90(A::AbstractMatrix)
163180
ind1, ind2 = indices(A)
164181
B = similar(A, (ind2,ind1))
@@ -168,6 +185,24 @@ function rotl90(A::AbstractMatrix)
168185
end
169186
return B
170187
end
188+
189+
"""
190+
rotr90(A)
191+
192+
Rotate matrix `A` right 90 degrees.
193+
194+
```jldoctest
195+
julia> a = [1 2; 3 4]
196+
2×2 Array{Int64,2}:
197+
1 2
198+
3 4
199+
200+
julia> rotr90(a)
201+
2×2 Array{Int64,2}:
202+
3 1
203+
4 2
204+
```
205+
"""
171206
function rotr90(A::AbstractMatrix)
172207
ind1, ind2 = indices(A)
173208
B = similar(A, (ind2,ind1))
@@ -177,6 +212,23 @@ function rotr90(A::AbstractMatrix)
177212
end
178213
return B
179214
end
215+
"""
216+
rot180(A)
217+
218+
Rotate matrix `A` 180 degrees.
219+
220+
```jldoctest
221+
julia> a = [1 2; 3 4]
222+
2×2 Array{Int64,2}:
223+
1 2
224+
3 4
225+
226+
julia> rot180(a)
227+
2×2 Array{Int64,2}:
228+
4 3
229+
2 1
230+
```
231+
"""
180232
function rot180(A::AbstractMatrix)
181233
B = similar(A)
182234
ind1, ind2 = indices(A,1), indices(A,2)
@@ -186,13 +238,102 @@ function rot180(A::AbstractMatrix)
186238
end
187239
return B
188240
end
241+
"""
242+
rotl90(A, k)
243+
244+
Rotate matrix `A` left 90 degrees an integer `k` number of times.
245+
If `k` is zero or a multiple of four, this is equivalent to a `copy`.
246+
247+
```jldoctest
248+
julia> a = [1 2; 3 4]
249+
2×2 Array{Int64,2}:
250+
1 2
251+
3 4
252+
253+
julia> rotl90(a,1)
254+
2×2 Array{Int64,2}:
255+
2 4
256+
1 3
257+
258+
julia> rotl90(a,2)
259+
2×2 Array{Int64,2}:
260+
4 3
261+
2 1
262+
263+
julia> rotl90(a,3)
264+
2×2 Array{Int64,2}:
265+
3 1
266+
4 2
267+
268+
julia> rotl90(a,4)
269+
2×2 Array{Int64,2}:
270+
1 2
271+
3 4
272+
```
273+
"""
189274
function rotl90(A::AbstractMatrix, k::Integer)
190275
k = mod(k, 4)
191276
k == 1 ? rotl90(A) :
192277
k == 2 ? rot180(A) :
193278
k == 3 ? rotr90(A) : copy(A)
194279
end
280+
"""
281+
rotr90(A, k)
282+
283+
Rotate matrix `A` right 90 degrees an integer `k` number of times. If `k` is zero or a
284+
multiple of four, this is equivalent to a `copy`.
285+
286+
```jldoctest
287+
julia> a = [1 2; 3 4]
288+
2×2 Array{Int64,2}:
289+
1 2
290+
3 4
291+
292+
julia> rotr90(a,1)
293+
2×2 Array{Int64,2}:
294+
3 1
295+
4 2
296+
297+
julia> rotr90(a,2)
298+
2×2 Array{Int64,2}:
299+
4 3
300+
2 1
301+
302+
julia> rotr90(a,3)
303+
2×2 Array{Int64,2}:
304+
2 4
305+
1 3
306+
307+
julia> rotr90(a,4)
308+
2×2 Array{Int64,2}:
309+
1 2
310+
3 4
311+
```
312+
"""
195313
rotr90(A::AbstractMatrix, k::Integer) = rotl90(A,-k)
314+
"""
315+
rot180(A, k)
316+
317+
Rotate matrix `A` 180 degrees an integer `k` number of times.
318+
If `k` is even, this is equivalent to a `copy`.
319+
320+
```jldoctest
321+
julia> a = [1 2; 3 4]
322+
2×2 Array{Int64,2}:
323+
1 2
324+
3 4
325+
326+
julia> rot180(a,1)
327+
2×2 Array{Int64,2}:
328+
4 3
329+
2 1
330+
331+
julia> rot180(a,2)
332+
2×2 Array{Int64,2}:
333+
1 2
334+
3 4
335+
```
336+
"""
196337
rot180(A::AbstractMatrix, k::Integer) = mod(k, 2) == 1 ? rot180(A) : copy(A)
197338

198339
## Transpose ##

base/docs/helpdb/Base.jl

-95
Original file line numberDiff line numberDiff line change
@@ -490,19 +490,6 @@ Get a backtrace object for the current program point.
490490
"""
491491
backtrace
492492

493-
"""
494-
reducedim(f, A, dims[, initial])
495-
496-
Reduce 2-argument function `f` along dimensions of `A`. `dims` is a vector specifying the
497-
dimensions to reduce, and `initial` is the initial value to use in the reductions. For `+`, `*`,
498-
`max` and `min` the `initial` argument is optional.
499-
500-
The associativity of the reduction is implementation-dependent; if you need a particular
501-
associativity, e.g. left-to-right, you should write your own loop. See documentation for
502-
`reduce`.
503-
"""
504-
reducedim
505-
506493
"""
507494
-(x)
508495
@@ -1105,17 +1092,6 @@ representable.
11051092
"""
11061093
ceil
11071094

1108-
"""
1109-
mapslices(f, A, dims)
1110-
1111-
Transform the given dimensions of array `A` using function `f`. `f` is called on each slice
1112-
of `A` of the form `A[...,:,...,:,...]`. `dims` is an integer vector specifying where the
1113-
colons go in this expression. The results are concatenated along the remaining dimensions.
1114-
For example, if `dims` is `[1,2]` and `A` is 4-dimensional, `f` is called on `A[:,:,i,j]`
1115-
for all `i` and `j`.
1116-
"""
1117-
mapslices
1118-
11191095
"""
11201096
issocket(path) -> Bool
11211097
@@ -2321,15 +2297,6 @@ Bessel function of the second kind of order 1, ``Y_1(x)``.
23212297
"""
23222298
bessely1
23232299

2324-
"""
2325-
cumprod(A, [dim])
2326-
2327-
Cumulative product along a dimension `dim` (defaults to 1). See also
2328-
[`cumprod!`](:func:`cumprod!`) to use a preallocated output array, both for performance and
2329-
to control the precision of the output (e.g. to avoid overflow).
2330-
"""
2331-
cumprod
2332-
23332300
"""
23342301
besseljx(nu, x)
23352302
@@ -3054,21 +3021,6 @@ block to create a new scope with copies of all variables referenced in the expre
30543021
"""
30553022
:@async
30563023

3057-
"""
3058-
rotr90(A)
3059-
3060-
Rotate matrix `A` right 90 degrees.
3061-
"""
3062-
rotr90(A)
3063-
3064-
"""
3065-
rotr90(A, k)
3066-
3067-
Rotate matrix `A` right 90 degrees an integer `k` number of times. If `k` is zero or a
3068-
multiple of four, this is equivalent to a `copy`.
3069-
"""
3070-
rotr90(A, k)
3071-
30723024
"""
30733025
readdir([dir]) -> Vector{String}
30743026
@@ -3523,21 +3475,6 @@ Number of ways to choose `k` out of `n` items.
35233475
"""
35243476
binomial
35253477

3526-
"""
3527-
rot180(A)
3528-
3529-
Rotate matrix `A` 180 degrees.
3530-
"""
3531-
rot180(A)
3532-
3533-
"""
3534-
rot180(A, k)
3535-
3536-
Rotate matrix `A` 180 degrees an integer `k` number of times. If `k` is even, this is
3537-
equivalent to a `copy`.
3538-
"""
3539-
rot180(A, k)
3540-
35413478
"""
35423479
.<=(x, y)
35433480
.≤(x,y)
@@ -3903,21 +3840,6 @@ x == div(x,y)*y + rem(x,y)
39033840
"""
39043841
rem
39053842

3906-
"""
3907-
rotl90(A)
3908-
3909-
Rotate matrix `A` left 90 degrees.
3910-
"""
3911-
rotl90(A)
3912-
3913-
"""
3914-
rotl90(A, k)
3915-
3916-
Rotate matrix `A` left 90 degrees an integer `k` number of times. If `k` is zero or a
3917-
multiple of four, this is equivalent to a `copy`.
3918-
"""
3919-
rotl90(A, k)
3920-
39213843
"""
39223844
info(msg)
39233845
@@ -5623,14 +5545,6 @@ handle comparison to other types via promotion rules where possible.
56235545
"""
56245546
Base.:(==)
56255547

5626-
"""
5627-
mapreducedim(f, op, A, dims[, initial])
5628-
5629-
Evaluates to the same as `reducedim(op, map(f, A), dims, f(initial))`, but is generally
5630-
faster because the intermediate array is avoided.
5631-
"""
5632-
mapreducedim
5633-
56345548
"""
56355549
seekstart(s)
56365550
@@ -6415,15 +6329,6 @@ the group owning the file
64156329
"""
64166330
operm
64176331

6418-
"""
6419-
cumsum(A, [dim])
6420-
6421-
Cumulative sum along a dimension `dim` (defaults to 1). See also [`cumsum!`](:func:`cumsum!`)
6422-
to use a preallocated output array, both for performance and to control the precision of the
6423-
output (e.g. to avoid overflow).
6424-
"""
6425-
cumsum
6426-
64276332
"""
64286333
rpad(string, n, p)
64296334

0 commit comments

Comments
 (0)