Skip to content

Commit cbc4f80

Browse files
author
Andy Ferris
committed
Make shallow matrix transpose default for permutedims
* Also allow `permutedims(vector)` to make row matrix * Make clearer the relationships between `transpose`, `adjoint` and `permutedims` in the docstrings.
1 parent d9e73c5 commit cbc4f80

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

base/linalg/transpose.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ end
147147
"""
148148
transpose(A::AbstractMatrix)
149149
150-
The transposition operator (`.'`).
150+
The transposition operator (`.'`). Note that the transposition is applied recursively to
151+
elements.
152+
153+
This operation is intended for linear algebra usage - for general data manipulation see
154+
[`permutedims`](@ref), which is non-recursive.
151155
152156
# Examples
153157
```jldoctest

base/operators.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,11 @@ fldmod1(x::T, y::T) where {T<:Integer} = (fld1(x,y), mod1(x,y))
744744
"""
745745
adjoint(A)
746746
747-
The conjugate transposition operator (`'`).
747+
The conjugate transposition operator (`'`). Note that `adjoint` is applied recursively to
748+
elements.
749+
750+
This operation is intended for linear algebra usage - for general data manipulation see
751+
[`permutedims`](@ref).
748752
749753
# Examples
750754
```jldoctest

base/permuteddimsarray.jl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,10 @@ end
7777
@inline genperm(I, perm::AbstractVector{Int}) = genperm(I, (perm...,))
7878

7979
"""
80-
permutedims(A, perm)
80+
permutedims(A::AbstractArray, perm])
8181
8282
Permute the dimensions of array `A`. `perm` is a vector specifying a permutation of length
83-
`ndims(A)`. This is a generalization of transpose for multi-dimensional arrays. Transpose is
84-
equivalent to `permutedims(A, [2,1])`.
83+
`ndims(A)`.
8584
8685
See also: [`PermutedDimsArray`](@ref).
8786
@@ -113,6 +112,21 @@ function Base.permutedims(A::AbstractArray, perm)
113112
permutedims!(dest, A, perm)
114113
end
115114

115+
"""
116+
permutedims(m::AbstractMatrix)
117+
118+
Permute the dimensions of the matrix `m`, by flipping the elements across the diagonal of
119+
the matrix. Differs from [`transpose`](@ref) in that the operation is not recursive.
120+
"""
121+
Base.permutedims(A::AbstractMatrix) = permutedims(A, (2,1))
122+
123+
"""
124+
permutedims(v::AbstractVector)
125+
126+
Reshape vector `v` into a `1 × length(v)` row matrix.
127+
"""
128+
permutedims(v::AbstractVector) = reshape(v, (1, length(v)))
129+
116130
"""
117131
permutedims!(dest, src, perm)
118132

test/arrayops.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,12 @@ end
577577
@test isequal(A,permutedims(permutedims(A,perm),invperm(perm)))
578578
@test isequal(A,permutedims(permutedims(A,invperm(perm)),perm))
579579
end
580+
581+
m = [1 2; 3 4]
582+
@test permutedims(m) == [1 3; 2 4]
583+
584+
v = [1,2,3]
585+
@test permutedims(v) == [1 2 3]
580586
end
581587

582588
@testset "circshift" begin

0 commit comments

Comments
 (0)