Skip to content

Commit 3a99888

Browse files
committed
awfulstack with docstrings
1 parent 8545ac5 commit 3a99888

File tree

2 files changed

+243
-0
lines changed

2 files changed

+243
-0
lines changed

base/abstractarray.jl

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2599,6 +2599,248 @@ end
25992599
Ai
26002600
end
26012601

2602+
"""
2603+
awfulstack(array_of_arrays)
2604+
2605+
Concatenates a multi-dimensional array of arrays into a single array, seeing
2606+
the input as a block array. The dimensions of the sub-arrays must match accordingly.
2607+
2608+
# Examples
2609+
2610+
Simple concatenation of vectors
2611+
2612+
```jldoctest
2613+
julia> hcat([1,2,3], [5,6,7])
2614+
3×2 Matrix{Int64}:
2615+
1 5
2616+
2 6
2617+
3 7
2618+
2619+
julia> reduce(hcat, [[1,2,3], [5,6,7]])
2620+
3×2 Matrix{Int64}:
2621+
1 5
2622+
2 6
2623+
3 7
2624+
2625+
julia> awfulstack([[1,2,3] [5,6,7]])
2626+
6-element Vector{Int64}:
2627+
1 5
2628+
2 6
2629+
3 7
2630+
2631+
julia> awfulstack([[1,2,3], [5,6,7]])
2632+
6-element Vector{Int64}:
2633+
1
2634+
2
2635+
3
2636+
5
2637+
6
2638+
7
2639+
2640+
julia> awfulstack([[1,2,3], [5,6,7]]')
2641+
1×6 Matrix{Int64}:
2642+
1 2 3 5 6 7
2643+
2644+
julia> awfulstack(permutedims.([[1,2,3], [5,6,7]]))
2645+
2×3 Matrix{Int64}:
2646+
1 2 3
2647+
5 6 7
2648+
```
2649+
2650+
"Flatmap" behavior.
2651+
```jldoctest
2652+
julia> awfulstack(n -> -n:2:n, 1:3)
2653+
9-element Vector{Int64}:
2654+
-1
2655+
1
2656+
-2
2657+
0
2658+
2
2659+
-3
2660+
-1
2661+
1
2662+
3
2663+
```
2664+
2665+
Image montage.
2666+
2667+
```
2668+
using TestImages, ImageView
2669+
myimages = ["cameraman","resolution_test_512","plastic_bubbles_he_512", "pirate", "woman_darkhair", "walkbridge"]
2670+
imagearray = testimage.(reshape(myimages,2,:))
2671+
imshow(awfulstack(imagearray))
2672+
```
2673+
2674+
# Extended Help
2675+
2676+
## More Examples
2677+
2678+
Higher-dimension concatenation.
2679+
2680+
```jldoctest
2681+
julia> awfulstack([1 2]) do n reshape(n*4-3:n*4,2,2) end
2682+
2×4 Matrix{Int64}:
2683+
1 3 5 7
2684+
2 4 6 8
2685+
2686+
julia> awfulstack([1,2]) do n reshape(n*4-3:n*4,2,2) end
2687+
4×2 Matrix{Int64}:
2688+
1 3
2689+
2 4
2690+
5 7
2691+
6 8
2692+
2693+
julia> awfulstack([1;;;2;;;]) do n reshape(n*4-3:n*4,2,2) end
2694+
2×2×2 Array{Int64, 3}:
2695+
[:, :, 1] =
2696+
1 3
2697+
2 4
2698+
2699+
[:, :, 2] =
2700+
5 7
2701+
6 8
2702+
```
2703+
2704+
Relationship to `eachcol` and `eachrow`.
2705+
2706+
```jldoctest
2707+
julia> m = reshape(1:15,3,5)
2708+
3×5 reshape(::UnitRange{Int64}, 3, 5) with eltype Int64:
2709+
1 4 7 10 13
2710+
2 5 8 11 14
2711+
3 6 9 12 15
2712+
2713+
julia> lol = eachcol(m) |> collect
2714+
5-element Vector{SubArray{Int64, 1, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, true}}:
2715+
[1, 2, 3]
2716+
[4, 5, 6]
2717+
[7, 8, 9]
2718+
[10, 11, 12]
2719+
[13, 14, 15]
2720+
2721+
julia> awfulstack(lol)
2722+
15-element Vector{Int64}:
2723+
1
2724+
2
2725+
3
2726+
4
2727+
5
2728+
6
2729+
7
2730+
8
2731+
9
2732+
10
2733+
11
2734+
12
2735+
13
2736+
14
2737+
15
2738+
2739+
julia> awfulstack(lol')
2740+
1×15 Matrix{Int64}:
2741+
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2742+
2743+
julia> awfulstack(permutedims(lol))
2744+
3×5 Matrix{Int64}:
2745+
1 4 7 10 13
2746+
2 5 8 11 14
2747+
3 6 9 12 15
2748+
2749+
julia> lol = eachrow(m) |> collect
2750+
3-element Vector{SubArray{Int64, 1, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}, Tuple{Int64, Base.Slice{Base.OneTo{Int64}}}, true}}:
2751+
[1, 4, 7, 10, 13]
2752+
[2, 5, 8, 11, 14]
2753+
[3, 6, 9, 12, 15]
2754+
2755+
julia> awfulstack(permutedims(lol))
2756+
5×3 Matrix{Int64}:
2757+
1 2 3
2758+
4 5 6
2759+
7 8 9
2760+
10 11 12
2761+
13 14 15
2762+
2763+
julia> awfulstack(permutedims.(lol))
2764+
3×5 Matrix{Int64}:
2765+
1 4 7 10 13
2766+
2 5 8 11 14
2767+
3 6 9 12 15
2768+
```
2769+
2770+
Non-uniform array shapes with 3 dimensions.
2771+
2772+
```jldoctest
2773+
julia> myarrays = map([(j,k,l) for j in 1:2, k in 1:2, l in 1:2]) do (jkl)
2774+
reshape(1:prod((jkl)), jkl...)
2775+
end
2776+
2×2×2 Array{Base.ReshapedArray{Int64, 3, UnitRange{Int64}, Tuple{}}, 3}:
2777+
[:, :, 1] =
2778+
[1;;;] [1 2;;;]
2779+
[1; 2;;;] [1 3; 2 4;;;]
2780+
2781+
[:, :, 2] =
2782+
[1;;; 2] [1 2;;; 3 4]
2783+
[1; 2;;; 3; 4] [1 3; 2 4;;; 5 7; 6 8]
2784+
2785+
julia> arr = awfulstack(myarrays)
2786+
3×3×3 Array{Int64, 3}:
2787+
[:, :, 1] =
2788+
1 1 2
2789+
1 1 3
2790+
2 2 4
2791+
2792+
[:, :, 2] =
2793+
1 1 2
2794+
1 1 3
2795+
2 2 4
2796+
2797+
[:, :, 3] =
2798+
2 3 4
2799+
3 5 7
2800+
4 6 8
2801+
2802+
julia> arr == awfulstack([(j,k,l) for j in 1:2, k in 1:2, l in 1:2]) do (jkl)
2803+
reshape(1:prod((jkl)), jkl...)
2804+
end
2805+
true
2806+
```
2807+
"""
2808+
awfulstack(array_of_arrays) = awfulstack_(array_of_arrays)
2809+
2810+
"""
2811+
awfulstack(f, c...)
2812+
2813+
Equivalent to awfulstact(map(f, c...)). Implements flatmap behavior.
2814+
2815+
# Example
2816+
```jldoctest
2817+
julia> Znm = [x for n in 1:3 for x in -n:2:n]
2818+
9-element Vector{Int64}:
2819+
-1
2820+
1
2821+
-2
2822+
0
2823+
2
2824+
-3
2825+
-1
2826+
1
2827+
3
2828+
2829+
julia> awfulstack(n -> -n:2:n, 1:3) == Znm
2830+
true
2831+
```
2832+
"""
2833+
awfulstack(f, c...) = awfulstack(map(f, c...))
2834+
2835+
function awfulstack_(aoa; indices=(), mydim=ndims(aoa))
2836+
if mydim==1
2837+
reduce(catdim_(mydim), view(aoa,:,indices...))
2838+
else
2839+
reduce(catdim_(mydim), (awfulstack_(aoa, indices=(n, indices...), mydim=mydim-1) for n in 1:size(aoa, mydim)))
2840+
end
2841+
end
2842+
catdim_(dims) = (a,b) -> cat(a,b,dims=dims)
2843+
26022844
## Reductions and accumulates ##
26032845

26042846
function isequal(A::AbstractArray, B::AbstractArray)

base/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ export
445445
sortperm!,
446446
sortslices,
447447
dropdims,
448+
awfulstack,
448449
step,
449450
stride,
450451
strides,

0 commit comments

Comments
 (0)