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

sortrows and sortcols fail on SubArrays #22493

Closed
Hua-Zhou opened this issue Jun 23, 2017 · 1 comment
Closed

sortrows and sortcols fail on SubArrays #22493

Hua-Zhou opened this issue Jun 23, 2017 · 1 comment

Comments

@Hua-Zhou
Copy link

julia> versioninfo()
Julia Version 0.6.0
Commit 903644385b (2017-06-19 13:05 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin13.4.0)
  CPU: Intel(R) Core(TM) i7-6920HQ CPU @ 2.90GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, skylake)

julia> srand(123); A = rand(0:1, 5, 3)
5×3 Array{Int64,2}:
 0  1  1
 0  0  1
 0  1  1
 0  0  0
 1  1  0

julia> Av = view(A, [2; 4; 5], [2; 3])
3×2 SubArray{Int64,2,Array{Int64,2},Tuple{Array{Int64,1},Array{Int64,1}},false}:
 0  1
 0  0
 1  0

julia> sortrows(Av)
ERROR: MethodError: Cannot `convert` an object of type SubArray{Int64,1,Array{Int64,2},Tuple{Int64,Array{Int64,1}},false} to an object of type SubArray{Int64,1,SubArray{Int64,2,Array{Int64,2},Tuple{Array{Int64,1},Array{Int64,1}},false},Tuple{Int64,Base.Slice{Base.OneTo{Int64}}},false}
This may have arisen from a call to the constructor SubArray{Int64,1,SubArray{Int64,2,Array{Int64,2},Tuple{Array{Int64,1},Array{Int64,1}},false},Tuple{Int64,Base.Slice{Base.OneTo{Int64}}},false}(...),
since type constructors fall back to convert methods.
Stacktrace:
 [1] #sortrows#14(::Array{Any,1}, ::Function, ::SubArray{Int64,2,Array{Int64,2},Tuple{Array{Int64,1},Array{Int64,1}},false}) at ./sort.jl:776
 [2] sortrows(::SubArray{Int64,2,Array{Int64,2},Tuple{Array{Int64,1},Array{Int64,1}},false}) at ./sort.jl:772

Similar for sortcols.

In the source, Base.Sort.slicetypeof returns the wrong type for a SubArray so types don't match in rows[i] = view(A, i, :).

@laborg
Copy link
Contributor

laborg commented Feb 4, 2022

sortrows and sortcols have in the meantime been replaced by sortslices and this works as expected:

julia> A = [0  1  1
       0  0  1
       0  1  1
       0  0  0
       1  1  0];

julia> Av = view(A, [2; 4; 5], [2; 3])
3×2 view(::Matrix{Int64}, [2, 4, 5], [2, 3]) with eltype Int64:
 0  1
 0  0
 1  0

julia> sortslices(Av,dims=1)
3×2 Matrix{Int64}:
 0  0
 0  1
 1  0

@laborg laborg closed this as completed Feb 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants