Skip to content

Commit

Permalink
Added permutedims docs and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Ferris committed Nov 29, 2017
1 parent b9a1a6c commit 5a525da
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
5 changes: 5 additions & 0 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,11 @@ end

## Permute array dims ##

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

function permutedims(B::StridedArray, perm = (2,1))
Expand Down
8 changes: 4 additions & 4 deletions base/permuteddimsarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ end
@inline genperm(I, perm::AbstractVector{Int}) = genperm(I, (perm...,))

"""
permutedims(A, perm)
permutedims(A::AbstractArray, [perm = (2,1)])
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)`. This is a generalization of matrix transposition for multi-dimensional arrays.
The value of `perm` defaults to `(2,1)` for transposing the elements of a matrix
See also: [`PermutedDimsArray`](@ref).
Expand All @@ -108,7 +108,7 @@ julia> permutedims(A, [3, 2, 1])
6 8
```
"""
function Base.permutedims(A::AbstractArray, perm)
function Base.permutedims(A::AbstractArray, perm = (2,1))
dest = similar(A, genperm(indices(A), perm))
permutedims!(dest, A, perm)
end
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 5a525da

Please sign in to comment.