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

transpose fails on a Matrix of Vectors #15512

Closed
schmrlng opened this issue Mar 15, 2016 · 7 comments
Closed

transpose fails on a Matrix of Vectors #15512

schmrlng opened this issue Mar 15, 2016 · 7 comments
Labels
bug Indicates an unexpected problem or unintended behavior good first issue Indicates a good issue for first-time contributors to Julia linear algebra Linear algebra needs tests Unit tests are required for this change

Comments

@schmrlng
Copy link
Contributor

   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.5.0-dev+3143 (2016-03-13 22:13 UTC)
 _/ |\__'_|_|_|\__'_|  |  Commit 264f856 (1 day old master)
|__/                   |  x86_64-linux-gnu

julia> a = [[i,j] for i in 1:3, j in 1:2]
3x2 Array{Array{Int64,1},2}:
 [1,1]  [1,2]
 [2,1]  [2,2]
 [3,1]  [3,2]

julia> a'
ERROR: MethodError: Cannot `convert` an object of type Array{Int64,2} to an object of type Array{Int64,1}

You might have used a 2d row vector where a 1d column vector was required.
Note the difference between 1d column vector [1,2,3] and 2d row vector [1 2 3].
You can convert to a column vector with the vec() function.
This may have arisen from a call to the constructor Array{Int64,1}(...),
since type constructors fall back to convert methods.
Closest candidates are:
  convert{T}(::Type{Array{T,1}}, ::Range{T})
  convert{T,S,N}(::Type{Array{T,N}}, ::SubArray{S,N,P,I,L})
  convert{T,n}(::Type{Array{T,n}}, ::Array{T,n})
  ...
 in ctranspose!(::Array{Array{Int64,1},2}, ::Array{Array{Int64,1},2}) at ./arraymath.jl:378
 in ctranspose(::Array{Array{Int64,1},2}) at ./arraymath.jl:376
 in eval(::Module, ::Any) at ./boot.jl:267

I think the behaviour should be along the lines of:

julia> eltype(a)[a[i,j] for j in 1:size(a,2), i in 1:size(a,1)]
2x3 Array{Array{Int64,1},2}:
 [1,1]  [2,1]  [3,1]
 [1,2]  [2,2]  [3,2]
@schmrlng
Copy link
Contributor Author

A Vector of Vectors suffers from the same issue.

julia> a = [[i] for i in 1:3]
3-element Array{Array{Int64,1},1}:
 [1]
 [2]
 [3]

julia> a'
ERROR: MethodError: Cannot `convert` an object of type Array{Int64,2} to an object of type Array{Int64,1}

You might have used a 2d row vector where a 1d column vector was required.
Note the difference between 1d column vector [1,2,3] and 2d row vector [1 2 3].
You can convert to a column vector with the vec() function.
This may have arisen from a call to the constructor Array{Int64,1}(...),
since type constructors fall back to convert methods.
Closest candidates are:
  convert{T}(::Type{Array{T,1}}, ::Range{T})
  convert{T,S,N}(::Type{Array{T,N}}, ::SubArray{S,N,P,I,L})
  convert{T,n}(::Type{Array{T,n}}, ::Array{T,n})
  ...
 [inlined code] from ./arraymath.jl:378
 in ctranspose(::Array{Array{Int64,1},1}) at ./arraymath.jl:381
 in eval(::Module, ::Any) at ./boot.jl:267

@KristofferC
Copy link
Member

You are in for a ride #4774

@schmrlng
Copy link
Contributor Author

Hmm. Perhaps not entirely orthogonal to the discussion in #4774 on whether transposition is an operation that interchanges some dimensions or one that turns a vector into a linear functional, is recursive "deeptranspose" really what transpose should mean? I see the linear algebraic appeal (#7244) and there's always permutedims as a backup, so I guess things that need to happen if this is the intended behaviour are:

  1. Update the docs for transpose and permutedims (in particular, destress equivalence in the [2,1] case)
  2. Replace https://github.com/JuliaLang/julia/blob/master/base/arraymath.jl#L371 and similar lines with something like B = Array(transpose(eltype(A)), size(A, 2), size(A, 1)) where transpose(::Type{T}) should be defined for some common types T (at least for the current notion of transpose, as a stopgap until some resolution comes out of Taking vector transposes seriously #4774)

Does that seem right?

@andreasnoack
Copy link
Member

Sorry for the delay here. I think both of your suggestions are good and it would be great if you would prepare a PR.

schmrlng added a commit to schmrlng/julia that referenced this issue Apr 7, 2016
@kshyatt kshyatt added the linear algebra Linear algebra label May 9, 2016
@andyferris
Copy link
Member

Are there some things left to do here?

@andreasnoack
Copy link
Member

I think there is. The original example still fails.

@Sacha0 Sacha0 added the bug Indicates an unexpected problem or unintended behavior label Dec 31, 2017
@Sacha0 Sacha0 added this to the 1.x milestone Dec 31, 2017
@hochB
Copy link

hochB commented Jun 8, 2019

Works on Julia master.

julia> a = [[i,j] for i in 1:3, j in 1:2]
3×2 Array{Array{Int64,1},2}:
 [1, 1]  [1, 2]
 [2, 1]  [2, 2]
 [3, 1]  [3, 2]

julia> a'
2×3 LinearAlgebra.Adjoint{LinearAlgebra.Adjoint{Int64,Array{Int64,1}},Array{Array{Int64,1},2}}:
 [1 1]  [2 1]  [3 1]
 [1 2]  [2 2]  [3 2]

@fredrikekre fredrikekre added the needs tests Unit tests are required for this change label Jun 8, 2019
@ViralBShah ViralBShah added the good first issue Indicates a good issue for first-time contributors to Julia label Jul 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior good first issue Indicates a good issue for first-time contributors to Julia linear algebra Linear algebra needs tests Unit tests are required for this change
Projects
None yet
Development

No branches or pull requests

9 participants