Skip to content

Commit

Permalink
Document recursive (c)transpose and fix #15512
Browse files Browse the repository at this point in the history
  • Loading branch information
schmrlng committed Apr 7, 2016
1 parent a92c7ff commit 7e77145
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
7 changes: 4 additions & 3 deletions base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -367,18 +367,19 @@ function ccopy!(B, A)
end
end

transpose{T<:AbstractVector}(::Type{T}) = Matrix{eltype(T)}
function transpose(A::AbstractMatrix)
B = similar(A, size(A, 2), size(A, 1))
B = Array(transpose(eltype(A)), size(A, 2), size(A, 1))
transpose!(B, A)
end
function ctranspose(A::AbstractMatrix)
B = similar(A, size(A, 2), size(A, 1))
B = Array(transpose(eltype(A)), size(A, 2), size(A, 1))
ctranspose!(B, A)
end
ctranspose{T<:Real}(A::AbstractVecOrMat{T}) = transpose(A)

transpose(x::AbstractVector) = [ transpose(v) for i=1, v in x ]
ctranspose{T}(x::AbstractVector{T}) = T[ ctranspose(v) for i=1, v in x ] #Fixme comprehension
ctranspose{T}(x::AbstractVector{T}) = transpose(T)[ ctranspose(v) for i=1, v in x ] #Fixme comprehension

_cumsum_type{T<:Number}(v::AbstractArray{T}) = typeof(+zero(T))
_cumsum_type(v) = typeof(v[1]+v[1])
Expand Down
7 changes: 5 additions & 2 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2818,7 +2818,8 @@ find(f, A)
"""
ctranspose(A)
The conjugate transposition operator (`'`).
The conjugate transposition operator (`'`). Operates recursively on arrays with vector or
matrix element type (e.g., block matrices).
"""
ctranspose

Expand Down Expand Up @@ -5115,7 +5116,9 @@ lock
"""
transpose(A)
The transposition operator (`.'`).
The transposition operator (`.'`). Operates recursively on arrays with vector or matrix
element type (e.g., block matrices). Non-recursive behavior may be obtained in such cases
by calling `permutedims(A, [2,1])`.
"""
transpose

Expand Down
4 changes: 2 additions & 2 deletions doc/stdlib/linalg.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ Linear algebra functions in Julia are largely implemented by calling functions f

.. Docstring generated from Julia source
The transposition operator (``.'``\ ).
The transposition operator (``.'``\ ). Operates recursively on arrays with vector or matrix element type (e.g., block matrices). Non-recursive behavior may be obtained in such cases by calling ``permutedims(A, [2,1])``\ .

.. function:: transpose!(dest,src)

Expand All @@ -1140,7 +1140,7 @@ Linear algebra functions in Julia are largely implemented by calling functions f

.. Docstring generated from Julia source
The conjugate transposition operator (``'``\ ).
The conjugate transposition operator (``'``\ ). Operates recursively on arrays with vector or matrix element type (e.g., block matrices).

.. function:: ctranspose!(dest,src)

Expand Down
14 changes: 14 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,20 @@ a = zeros(Complex,1,5)
ctranspose!(a,b)
@test a == ones(Complex,1,5)

# block matrix/vector transposes
unblock(x::AbstractMatrix) = hvcat(size(x,2), permutedims(x, [2,1])...)
unblock(x::AbstractVector) = vcat(x...)
a = [rand(3,3) for i=1:3, j=1:3]
b = [rand(3) for i=1:3]
ua = unblock(a)
ub = unblock(b)
@test (b'*a*b)[1][1] (ub'*ua*ub)[1] (b'*a'*b)[1][1] (ub'*ua'*ub)[1]
a = [rand(3,3) + rand(3,3)*im for i=1:3, j=1:3]
b = [rand(3) + rand(3)*im for i=1:3]
ua = unblock(a)
ub = unblock(b)
@test (b'*a*b)[1][1] (ub'*ua*ub)[1] conj((b'*a'*b)[1][1]) conj((ub'*ua'*ub)[1])

# flipdim
a = rand(5,3)
@test flipdim(flipdim(a,2),2) == a
Expand Down

0 comments on commit 7e77145

Please sign in to comment.