Skip to content

Commit

Permalink
Make shallow matrix transpose default for permutedims
Browse files Browse the repository at this point in the history
 * Also allow `permutedims(vector)` to make row matrix
 * Make clearer the relationships between `transpose`, `adjoint` and
   `permutedims` in the docstrings.
  • Loading branch information
Andy Ferris committed Dec 3, 2017
1 parent d9e73c5 commit 646abce
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
6 changes: 5 additions & 1 deletion base/linalg/transpose.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ end
"""
transpose(A::AbstractMatrix)
The transposition operator (`.'`).
The transposition operator (`.'`). Note that the transposition is applied recursively to
elements.
This operation is intended for linear algebra usage - for general data manipulation see
[`permutedims`](@ref), which is non-recursive.
# Examples
```jldoctest
Expand Down
6 changes: 5 additions & 1 deletion base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,11 @@ fldmod1(x::T, y::T) where {T<:Integer} = (fld1(x,y), mod1(x,y))
"""
adjoint(A)
The conjugate transposition operator (`'`).
The conjugate transposition operator (`'`). Note that `adjoint` is applied recursively to
elements.
This operation is intended for linear algebra usage - for general data manipulation see
[`permutedims`](@ref).
# Examples
```jldoctest
Expand Down
22 changes: 18 additions & 4 deletions base/permuteddimsarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ end
@inline genperm(I, perm::AbstractVector{Int}) = genperm(I, (perm...,))

"""
permutedims(A, perm)
permutedims(A::AbstractArray, perm)
Permute the dimensions of array `A`. `perm` is a vector specifying a permutation of length
`ndims(A)`. This is a generalization of transpose for multi-dimensional arrays. Transpose is
equivalent to `permutedims(A, [2,1])`.
`ndims(A)`.
See also: [`PermutedDimsArray`](@ref).
Expand All @@ -108,11 +107,26 @@ julia> permutedims(A, [3, 2, 1])
6 8
```
"""
function Base.permutedims(A::AbstractArray, perm)
function permutedims(A::AbstractArray, perm)
dest = similar(A, genperm(indices(A), perm))
permutedims!(dest, A, perm)
end

"""
permutedims(m::AbstractMatrix)
Permute the dimensions of the matrix `m`, by flipping the elements across the diagonal of
the matrix. Differs from [`transpose`](@ref) in that the operation is not recursive.
"""
permutedims(A::AbstractMatrix) = permutedims(A, (2,1))

"""
permutedims(v::AbstractVector)
Reshape vector `v` into a `1 × length(v)` row matrix.
"""
permutedims(v::AbstractVector) = reshape(v, (1, length(v)))

"""
permutedims!(dest, src, perm)
Expand Down
6 changes: 6 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,12 @@ end
@test isequal(A,permutedims(permutedims(A,perm),invperm(perm)))
@test isequal(A,permutedims(permutedims(A,invperm(perm)),perm))
end

m = [1 2; 3 4]
@test permutedims(m) == [1 3; 2 4]

v = [1,2,3]
@test permutedims(v) == [1 2 3]
end

@testset "circshift" begin
Expand Down

0 comments on commit 646abce

Please sign in to comment.