Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support (c)transpose #42

Merged
merged 1 commit into from
Sep 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ function Base.permutedims(A::AxisArray, perm)
p = permutation(perm, axisnames(A))
AxisArray(permutedims(A.data, p), axes(A)[[p...]])
end

Base.transpose{T}(A::AxisArray{T,2}) = AxisArray(transpose(A.data), A.axes[2], A.axes[1])
Base.ctranspose{T}(A::AxisArray{T,2}) = AxisArray(ctranspose(A.data), A.axes[2], A.axes[1])
Base.transpose{T}(A::AxisArray{T,1}) = AxisArray(transpose(A.data), Axis{:transpose}(Base.OneTo(1)), A.axes[1])
Base.ctranspose{T}(A::AxisArray{T,1}) = AxisArray(ctranspose(A.data), Axis{:transpose}(Base.OneTo(1)), A.axes[1])

permutation(to::Union{AbstractVector{Int},Tuple{Int,Vararg{Int}}}, from::Symbols) = to

"""
Expand Down
13 changes: 13 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ C = similar(A, 0)
D = similar(A)
@test size(A) == size(D)
@test eltype(A) == eltype(D)

# permutedims and transpose
@test axisnames(permutedims(A, (2,1,3))) == (:col, :row, :page)
@test axisnames(permutedims(A, (2,3,1))) == (:col, :page, :row)
@test axisnames(permutedims(A, (3,2,1))) == (:page, :col, :row)
Expand All @@ -35,6 +37,17 @@ for perm in ((:col, :row, :page), (:col, :page, :row),
(:row, :page, :col), (:row, :col, :page))
@test axisnames(permutedims(A, perm)) == perm
end
@test axisnames(permutedims(A, (:col,))) == (:col, :row, :page)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this test and the next to highlight a somewhat non-obvious feature of the permutation algorithm: any dimensions that aren't listed explicitly get appended, in the order in which they appear. This was useful for me at one point in Images (that's where this was effectively copied from), but I'm not sure it's necessary now and in any event I recognize it could be a bit controversial. So I wanted to air the (potentially) dirty laundry; if you don't like it, I can pull these tests and modify the algorithm to be more strict about its inputs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not terribly different than the way indexing works. If you index with named axes, the other dimensions are filled with :. I agree it's a little marginal, but it doesn't seem objectionable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, let's keep it then.

@test axisnames(permutedims(A, (:page,))) == (:page, :row, :col)
A2 = AxisArray(reshape(1:15, 3, 5))
A1 = AxisArray(1:5, :t)
for f in (transpose, ctranspose)
@test f(A2).data == f(A2.data)
@test axisnames(f(A2)) == (:col, :row)
@test f(A1).data == f(A1.data)
@test axisnames(f(A1)) == (:transpose, :t)
end

# Test modifying a particular axis
E = similar(A, Float64, Axis{:col}(1:2))
@test size(E) == (2,2,4)
Expand Down