Skip to content

Commit 9fc1b65

Browse files
stevengjhaakon-ejishnub
authored
clarify permutedims docs (#52261)
As commented [on discourse](https://discourse.julialang.org/t/how-do-we-julians-win-big-when-the-situation-is-so-unfair/106433/63?u=stevengj), it would be nice if the `permutedims` examples began with something like an array of strings where `transpose` is inapplicable. This PR simply clarifies the docs and adds a few more examples. --------- Co-authored-by: Haakon Ludvig Langeland Ervik <45243236+haakon-e@users.noreply.github.com> Co-authored-by: Jishnu Bhattacharya <jishnub.github@gmail.com>
1 parent d0efc5c commit 9fc1b65

File tree

1 file changed

+100
-37
lines changed

1 file changed

+100
-37
lines changed

base/permuteddimsarray.jl

Lines changed: 100 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,68 @@ end
8787

8888
"""
8989
permutedims(A::AbstractArray, perm)
90+
permutedims(A::AbstractMatrix)
9091
91-
Permute the dimensions of array `A`. `perm` is a vector or a tuple of length `ndims(A)`
92+
Permute the dimensions (axes) of array `A`. `perm` is a tuple or vector of `ndims(A)` integers
9293
specifying the permutation.
9394
95+
If `A` is a 2d array ([`AbstractMatrix`](@ref)), then
96+
`perm` defaults to `(2,1)`, swapping the two axes of `A` (the rows and columns
97+
of the matrix). This differs from [`transpose`](@ref) in that the
98+
operation is not recursive, which is especially useful for arrays of non-numeric values
99+
(where the recursive `transpose` would throw an error) and/or 2d arrays that do not represent
100+
linear operators.
101+
102+
For 1d arrays, see [`permutedims(v::AbstractVector)`](@ref), which returns a 1-row “matrix”.
103+
94104
See also [`permutedims!`](@ref), [`PermutedDimsArray`](@ref), [`transpose`](@ref), [`invperm`](@ref).
95105
96106
# Examples
107+
108+
## 2d arrays:
109+
Unlike `transpose`, `permutedims` can be used to swap rows and columns of 2d arrays of
110+
arbitrary non-numeric elements, such as strings:
111+
```jldoctest
112+
julia> A = ["a" "b" "c"
113+
"d" "e" "f"]
114+
2×3 Matrix{String}:
115+
"a" "b" "c"
116+
"d" "e" "f"
117+
118+
julia> permutedims(A)
119+
3×2 Matrix{String}:
120+
"a" "d"
121+
"b" "e"
122+
"c" "f"
123+
```
124+
And `permutedims` produces results that differ from `transpose`
125+
for matrices whose elements are themselves numeric matrices:
126+
```jldoctest; setup = :(using LinearAlgebra)
127+
julia> a = [1 2; 3 4];
128+
129+
julia> b = [5 6; 7 8];
130+
131+
julia> c = [9 10; 11 12];
132+
133+
julia> d = [13 14; 15 16];
134+
135+
julia> X = [[a] [b]; [c] [d]]
136+
2×2 Matrix{Matrix{Int64}}:
137+
[1 2; 3 4] [5 6; 7 8]
138+
[9 10; 11 12] [13 14; 15 16]
139+
140+
julia> permutedims(X)
141+
2×2 Matrix{Matrix{Int64}}:
142+
[1 2; 3 4] [9 10; 11 12]
143+
[5 6; 7 8] [13 14; 15 16]
144+
145+
julia> transpose(X)
146+
2×2 transpose(::Matrix{Matrix{Int64}}) with eltype Transpose{Int64, Matrix{Int64}}:
147+
[1 3; 2 4] [9 11; 10 12]
148+
[5 7; 6 8] [13 15; 14 16]
149+
```
150+
151+
## Multi-dimensional arrays
97152
```jldoctest
98153
julia> A = reshape(Vector(1:8), (2,2,2))
99154
2×2×2 Array{Int64, 3}:
@@ -143,54 +198,62 @@ function permutedims(A::AbstractArray, perm)
143198
permutedims!(dest, A, perm)
144199
end
145200

146-
"""
147-
permutedims(m::AbstractMatrix)
148-
149-
Permute the dimensions of the matrix `m`, by flipping the elements across the diagonal of
150-
the matrix. Differs from `LinearAlgebra`'s [`transpose`](@ref) in that the
151-
operation is not recursive.
152-
153-
# Examples
154-
```jldoctest; setup = :(using LinearAlgebra)
155-
julia> a = [1 2; 3 4];
156-
157-
julia> b = [5 6; 7 8];
158-
159-
julia> c = [9 10; 11 12];
160-
161-
julia> d = [13 14; 15 16];
162-
163-
julia> X = [[a] [b]; [c] [d]]
164-
2×2 Matrix{Matrix{Int64}}:
165-
[1 2; 3 4] [5 6; 7 8]
166-
[9 10; 11 12] [13 14; 15 16]
167-
168-
julia> permutedims(X)
169-
2×2 Matrix{Matrix{Int64}}:
170-
[1 2; 3 4] [9 10; 11 12]
171-
[5 6; 7 8] [13 14; 15 16]
172-
173-
julia> transpose(X)
174-
2×2 transpose(::Matrix{Matrix{Int64}}) with eltype Transpose{Int64, Matrix{Int64}}:
175-
[1 3; 2 4] [9 11; 10 12]
176-
[5 7; 6 8] [13 15; 14 16]
177-
```
178-
"""
179201
permutedims(A::AbstractMatrix) = permutedims(A, (2,1))
180202

181203
"""
182204
permutedims(v::AbstractVector)
183205
184206
Reshape vector `v` into a `1 × length(v)` row matrix.
185-
Differs from `LinearAlgebra`'s [`transpose`](@ref) in that
186-
the operation is not recursive.
207+
Differs from [`transpose`](@ref) in that
208+
the operation is not recursive, which is especially useful for arrays of non-numeric values
209+
(where the recursive `transpose` might throw an error).
187210
188211
# Examples
212+
Unlike `transpose`, `permutedims` can be used on vectors of
213+
arbitrary non-numeric elements, such as strings:
214+
```jldoctest
215+
julia> permutedims(["a", "b", "c"])
216+
1×3 Matrix{String}:
217+
"a" "b" "c"
218+
```
219+
For vectors of numbers, `permutedims(v)` works much like `transpose(v)`
220+
except that the return type differs (it uses [`reshape`](@ref)
221+
rather than a `LinearAlgebra.Transpose` view, though both
222+
share memory with the original array `v`):
189223
```jldoctest; setup = :(using LinearAlgebra)
190-
julia> permutedims([1, 2, 3, 4])
224+
julia> v = [1, 2, 3, 4]
225+
4-element Vector{Int64}:
226+
1
227+
2
228+
3
229+
4
230+
231+
julia> p = permutedims(v)
191232
1×4 Matrix{Int64}:
192233
1 2 3 4
193234
235+
julia> r = transpose(v)
236+
1×4 transpose(::Vector{Int64}) with eltype Int64:
237+
1 2 3 4
238+
239+
julia> p == r
240+
true
241+
242+
julia> typeof(r)
243+
Transpose{Int64, Vector{Int64}}
244+
245+
julia> p[1] = 5; r[2] = 6; # mutating p or r also changes v
246+
247+
julia> v # shares memory with both p and r
248+
4-element Vector{Int64}:
249+
5
250+
6
251+
3
252+
4
253+
```
254+
However, `permutedims` produces results that differ from `transpose`
255+
for vectors whose elements are themselves numeric matrices:
256+
```jldoctest; setup = :(using LinearAlgebra)
194257
julia> V = [[[1 2; 3 4]]; [[5 6; 7 8]]]
195258
2-element Vector{Matrix{Int64}}:
196259
[1 2; 3 4]

0 commit comments

Comments
 (0)