JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.68k stars 5.48k forks source link

Bug for transposed vectors of tuples #43329

Closed ParadaCarleton closed 2 years ago

ParadaCarleton commented 2 years ago
julia> z = [(1, 2), (3, 4), (5, 6)]
3-element Vector{Tuple{Int64, Int64}}:
 (1, 2)
 (3, 4)
 (5, 6)

julia> z'
1×3 adjoint(::Vector{Tuple{Int64, Int64}}) with eltype Union{}:
Error showing value of type LinearAlgebra.Adjoint{Union{}, Vector{Tuple{Int64, Int64}}}:
ERROR: MethodError: no method matching adjoint(::Tuple{Int64, Int64})
Closest candidates are:
  adjoint(::Union{LinearAlgebra.QR, LinearAlgebra.QRCompactWY, LinearAlgebra.QRPivoted}) at /usr/share/julia/stdlib/v1.7/LinearAlgebra/src/qr.jl:509
  adjoint(::Union{LinearAlgebra.Cholesky, LinearAlgebra.CholeskyPivoted}) at /usr/share/julia/stdlib/v1.7/LinearAlgebra/src/cholesky.jl:538
  adjoint(::SuiteSparse.CHOLMOD.Factor) at /usr/share/julia/stdlib/v1.7/SuiteSparse/src/cholmod.jl:340
  ...
Stacktrace:
  [1] getindex
    @ /usr/share/julia/stdlib/v1.7/LinearAlgebra/src/adjtrans.jl:178 [inlined]
  [2] _getindex
    @ ./abstractarray.jl:1245 [inlined]
  [3] getindex
    @ ./abstractarray.jl:1218 [inlined]
  [4] isassigned(::LinearAlgebra.Adjoint{Union{}, Vector{Tuple{Int64, Int64}}}, ::Int64, ::Int64)
    @ Base ./abstractarray.jl:553
  [5] alignment(io::IOContext{Base.TTY}, X::AbstractVecOrMat, rows::Vector{Int64}, cols::Vector{Int64}, cols_if_complete::Int64, cols_otherwise::Int64, sep::Int64)
    @ Base ./arrayshow.jl:67
  [6] _print_matrix(io::IOContext{Base.TTY}, X::AbstractVecOrMat, pre::String, sep::String, post::String, hdots::String, vdots::String, ddots::String, hmod::Int64, vmod::Int64, rowsA::UnitRange{Int64}, colsA::UnitRange{Int64})
    @ Base ./arrayshow.jl:204
  [7] print_matrix(io::IOContext{Base.TTY}, X::LinearAlgebra.Adjoint{Union{}, Vector{Tuple{Int64, Int64}}}, pre::String, sep::String, post::String, hdots::String, vdots::String, ddots::String, hmod::Int64, vmod::Int64) (repeats 2 times)
    @ Base ./arrayshow.jl:169
  [8] print_array
    @ ./arrayshow.jl:355 [inlined]
  [9] show(io::IOContext{Base.TTY}, #unused#::MIME{Symbol("text/plain")}, X::LinearAlgebra.Adjoint{Union{}, Vector{Tuple{Int64, Int64}}})
    @ Base ./arrayshow.jl:396
 [10] (::REPL.var"#43#44"{REPL.REPLDisplay{REPL.LineEditREPL}, MIME{Symbol("text/plain")}, Base.RefValue{Any}})(io::Any)
    @ REPL /usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:264
 [11] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
    @ REPL /usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:508
 [12] display(d::REPL.REPLDisplay, mime::MIME{Symbol("text/plain")}, x::Any)
    @ REPL /usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:257
 [13] display(d::REPL.REPLDisplay, x::Any)
    @ REPL /usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:269
 [14] display(x::Any)
    @ Base.Multimedia ./multimedia.jl:328
 [15] #invokelatest#2
    @ ./essentials.jl:716 [inlined]
 [16] invokelatest
    @ ./essentials.jl:714 [inlined]
 [17] print_response(errio::IO, response::Any, show_value::Bool, have_color::Bool, specialdisplay::Union{Nothing, AbstractDisplay})
    @ REPL /usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:291
 [18] (::REPL.var"#45#46"{REPL.LineEditREPL, Pair{Any, Bool}, Bool, Bool})(io::Any)
    @ REPL /usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:275
 [19] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
    @ REPL /usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:508
 [20] print_response(repl::REPL.AbstractREPL, response::Any, show_value::Bool, have_color::Bool)
    @ REPL /usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:273
 [21] (::REPL.var"#do_respond#66"{Bool, Bool, REPL.var"#77#87"{REPL.LineEditREPL, REPL.REPLHistoryProvider}, REPL.LineEditREPL, REPL.LineEdit.Prompt})(s::REPL.LineEdit.MIState, buf::Any, ok::Bool)
    @ REPL /usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:844
 [22] #invokelatest#2
    @ ./essentials.jl:716 [inlined]
 [23] invokelatest
    @ ./essentials.jl:714 [inlined]
 [24] run_interface(terminal::REPL.Terminals.TextTerminal, m::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState)
    @ REPL.LineEdit /usr/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:2493
 [25] run_frontend(repl::REPL.LineEditREPL, backend::REPL.REPLBackendRef)
    @ REPL /usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:1230
 [26] (::REPL.var"#49#54"{REPL.LineEditREPL, REPL.REPLBackendRef})()
    @ REPL ./task.jl:423
DilumAluthge commented 2 years ago

adjoint and transpose are applied recursively, and tuples do not have adjoints or transposes, so this error is the correct behavior.

If you do not want to apply the operation recursively, use permutedims instead.

julia> z = [(1, 2), (3, 4), (5, 6)]
3-element Vector{Tuple{Int64, Int64}}:
 (1, 2)
 (3, 4)
 (5, 6)

julia> permutedims(z)
1×3 Matrix{Tuple{Int64, Int64}}:
 (1, 2)  (3, 4)  (5, 6)
ParadaCarleton commented 2 years ago

adjoint and transpose are applied recursively, and tuples do not have adjoints or transposes, so this error is the correct behavior.

If you do not want to apply the operation recursively, use permutedims instead.

julia> z = [(1, 2), (3, 4), (5, 6)]
3-element Vector{Tuple{Int64, Int64}}:
 (1, 2)
 (3, 4)
 (5, 6)

julia> permutedims(z)
1×3 Matrix{Tuple{Int64, Int64}}:
 (1, 2)  (3, 4)  (5, 6)

I see... the error message confused me and made me think the bug was just a display bug. Thanks!