-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
RFC: Make matrix transpose default for permutedims
#24839
Conversation
The second commit adds |
5a525da
to
9b491d1
Compare
IIUC this won't work for |
That was on purpose - happy to discuss but I worried about making it too magical. |
base/multidimensional.jl
Outdated
""" | ||
permutedims(v::AbstractVector) | ||
|
||
Reshapes vector `v` into a `1 × length(v)` row matrix. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typically we stick to the imperative in docstrings, so this should be "reshape" instead of "reshapes."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks - I always forget this!
base/permuteddimsarray.jl
Outdated
`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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Transposing the elements of a matrix" makes it sound (at least to me) like it recursively calls permutedims
on the elements. Perhaps "transposing the dimensions of a matrix"? Unless that isn't a standard way to say it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps "flips the array cross its diagonal" or "mirrors the array across its diagonal"? Edit: That is, perhaps best to avoid "transpose" in this docstring?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, avoiding the word "transpose" would be ideal.
base/multidimensional.jl
Outdated
@@ -1466,7 +1466,7 @@ end | |||
|
|||
## Permute array dims ## | |||
|
|||
function permutedims(B::StridedArray, perm) | |||
function permutedims(B::StridedArray, perm = (2,1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps better to provide a default (2, 1)
perm only for two-dimensional StridedArray
s? For example, perhaps add the short child
permutedims(B::StridedMatrix) = permutedims(B, (2, 1))
rather than modify this general method's signature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought of that at first, but I note that you get a pretty coherent error message for 3D arrays as this is.
base/multidimensional.jl
Outdated
@@ -1487,7 +1489,7 @@ function checkdims_perm(P::AbstractArray{TP,N}, B::AbstractArray{TB,N}, perm) wh | |||
end | |||
|
|||
for (V, PT, BT) in [((:N,), BitArray, BitArray), ((:T,:N), Array, StridedArray)] | |||
@eval @generated function permutedims!(P::$PT{$(V...)}, B::$BT{$(V...)}, perm) where $(V...) | |||
@eval @generated function permutedims!(P::$PT{$(V...)}, B::$BT{$(V...)}, perm = (2,1)) where $(V...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Likewise here, perhaps better to provide a default (2, 1)
permutation only for two-dimensional BitArray
s?
base/permuteddimsarray.jl
Outdated
@@ -108,11 +108,18 @@ julia> permutedims(A, [3, 2, 1]) | |||
6 8 | |||
``` | |||
""" | |||
function Base.permutedims(A::AbstractArray, perm) | |||
function Base.permutedims(A::AbstractArray, perm = (2,1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And likewise here re. confining the default (2, 1)
permutation to two-dimensional objects? :)
7a91959
to
7ed8ac8
Compare
OK I did another pass, do you think the docstrings are clearer now? |
7ed8ac8
to
c7e2888
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! :) (Potentially modulo the permutedims(v::AbstractVector)
method, about which I lack an opinion, but I imagine others might feel strongly about.)
OK I don't understand the documentation reference system. It's currently complaining that it can't find a reference to |
test/arrayops.jl
Outdated
|
||
m = [1 2; 3 4] | ||
@test permutedims(m) == [1 3; 2 4] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI reports a trailing whitespace failure on this line (583)?
base/permuteddimsarray.jl
Outdated
@@ -77,11 +77,10 @@ end | |||
@inline genperm(I, perm::AbstractVector{Int}) = genperm(I, (perm...,)) | |||
|
|||
""" | |||
permutedims(A, perm) | |||
permutedims(A::AbstractArray, perm]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rogue ]
? :)
base/permuteddimsarray.jl
Outdated
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. | ||
""" | ||
Base.permutedims(A::AbstractMatrix) = permutedims(A, (2,1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Base.
qualification should be unnecessary?
cbc4f80
to
646abce
Compare
646abce
to
0e376d2
Compare
This is working now. |
0e376d2
to
01c8d5c
Compare
Alright, news done. @Sacha0 How do you think this will work in combination with the changes you are making for In particular, what is the best way of getting a left-eigenvector out of an The other way to go is Does anyone else have an opinion if |
* Also allow `permutedims(vector)` to make row matrix * Make clearer the relationships between `transpose`, `adjoint` and `permutedims` in the docstrings.
01c8d5c
to
2ba2556
Compare
Unfortunately beyond the scope of things I can think about for now 😄. Apologies and best! |
No probs, Sacha. I think I may have been overthinking it... I can't think of a proper linear-algebra use of non-recursive transpose other than that, so I think this should be fine. |
Given the lack of nay-saying, I'll go ahead and merge this now. I think this will solve the "data transpose" problem neatly enough for v1.0. We can always improve later on, as opportunities present themselves. |
HOW do I use type annotations/type asserts? Conceptually |
@algorithmx, it's best to ask questions on Discourse. |
There has been a lot of discussion at #20978 about the future of
transpose
(andadjoint
, etc).Basically, we would like a nicer way of doing a "shallow" transpose of an array of data, that has no linear algebra connotations. The current syntax would be
permutedims(matrix, (2,1))
The approach here is to simplify this to
permutedims(matrix)
by making a default argument forperm = (2,1)
.