Stream: helpdesk (published)

Topic: Transpose matrix


view this post on Zulip Ben Gear (May 12 2021 at 21:44):

Just a quick one. Is there a quicker (as in fewer characters) way to get the transpose of a matrix, other than just typing transpose(A)?

view this post on Zulip David Weingut (May 12 2021 at 21:45):

there is A', which I think is identical

view this post on Zulip Ben Gear (May 12 2021 at 21:46):

I thought that was the conjugate transpose? Similar to Matlab

view this post on Zulip Mason Protter (May 12 2021 at 21:49):

' is the conjugate transpose.

view this post on Zulip David Weingut (May 12 2021 at 21:50):

Yeah, you're right, sorry. Realized that I was using A' like transpose(A)...

view this post on Zulip Mason Protter (May 12 2021 at 21:51):

There's a couple options. One would be to write:

julia> struct  end; Base.:(*)(A, ::Type{}) = transpose(A)

julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
 1  2
 3  4

julia> (A)
2×2 transpose(::Matrix{Int64}) with eltype Int64:
 1  3
 2  4

view this post on Zulip Mason Protter (May 12 2021 at 21:52):

Here's another option if you don't like unicode:

julia> struct T end; Base.:(^)(A, ::Type{T}) = transpose(A)

julia> A^T
2×2 transpose(::Matrix{Int64}) with eltype Int64:
 1  3
 2  4

view this post on Zulip Ben Gear (May 12 2021 at 21:52):

Okay, I see. what's the \xxx command for getting the T superscript?

view this post on Zulip Mason Protter (May 12 2021 at 21:53):

It's \^T<TAB>. BTW, you can always find these by pasting them into the help> mode repl:

help?> 
"ᵀ" can be typed by \^T<tab>

search: 

  No documentation found.

  Summary
  ≡≡≡≡≡≡≡≡≡

  struct  <: Any

view this post on Zulip Ben Gear (May 12 2021 at 21:54):

Great. That's really helpful. Thanks

view this post on Zulip Mason Protter (May 12 2021 at 21:55):

If you're on version 1.6 or later, another option is to locally shadow the ' variable in a let block or whatever.

julia> let var"'" = transpose
           A'
       end
2×2 transpose(::Matrix{Int64}) with eltype Int64:
 1  3
 2  4

This can be handy when you have like a function body or something that has a few transposes in it and you want to be explicit about what means what

view this post on Zulip Mason Protter (May 12 2021 at 21:55):

Happy to help!


Last updated: Nov 22 2024 at 04:41 UTC