-
Notifications
You must be signed in to change notification settings - Fork 52
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
vcat
/hcat
/hvcat
-related method ambiguities
#431
Comments
there are a lot of reported issues about this problem. |
It looks like Julia itself needs something like LazyArrays.jl and InfiniteArrays.jl would really benefit from such a thing too. |
Should this be fixed soon, possibly removing the method/making it more specific not to cause ambiguity? Sparse arrays themselves aren't used too often, but SparseArrays.jl is a dependency of huge amount of Julia code because Statistics.jl depends on it. The
method makes any
ambiguous for the case of
specifically, in addition to eltype-agnostic Recent julia repo issues: JuliaLang/julia#52386 JuliaLang/julia#52263 |
If you can fix this in a good way doing this it would be helpful. To me, it just seems that the whole |
In case others find this issue and want a simple reliable workaround – here it is: Base.delete_method.(filter(
m -> nameof(m.module) == :SparseArrays && m.sig == Tuple{typeof(vcat), Union{Number,AbstractVecOrMat{<:Number}}, Vararg{Union{Number,AbstractVecOrMat{<:Number}}}},
methods(vcat)
))
Base.delete_method.(filter(
m -> nameof(m.module) == :SparseArrays && m.sig == Tuple{typeof(hcat), Union{Number,AbstractVecOrMat{<:Number}}, Vararg{Union{Number,AbstractVecOrMat{<:Number}}}},
methods(hcat)
)) Put this code into your script/code anywhere after package imports. It deletes those pirating methods defined in SparseArrays, getting rid of such ambiguities. At the same time, it doesn't interfere with julia> vcat(sparse([0,1]), sparse([0,1]))
4-element SparseVector{Int64, Int64} with 2 stored entries:
[2] = 1
[4] = 1 |
Concretely, julia> struct MyArray{N, T} <: AbstractArray{N, T}
data::Array{N, T}
end
julia> Base.IndexStyle(::Type{<:MyArray}) = IndexLinear()
julia> Base.setindex(x::MyArray, v, i) = (Base.setindex(x.data, v, i); x)
julia> Base.getindex(x::MyArray, i) = x.data[i]
julia> Base.size(x::MyArray) = size(x.data)
julia> Base.vcat(x::AbstractMatrix, y::MyArray) = MyArray(vcat(x, y.data))
julia> vcat(MyArray(rand(2,2)), MyArray(rand(2,2)))
4×2 MyArray{Float64, 2}:
0.244229 0.612965
0.743984 0.46191
0.321956 0.62292
0.937136 0.0216649
julia> using SparseArrays
julia> vcat(MyArray(rand(2,2)), MyArray(rand(2,2)))
ERROR: MethodError: vcat(::MyArray{Float64, 2}, ::MyArray{Float64, 2}) is ambiguous.
Candidates:
vcat(x::AbstractMatrix, y::MyArray)
@ Main REPL[6]:1
vcat(X1::Union{Number, AbstractVecOrMat{<:Number}}, X::Union{Number, AbstractVecOrMat{<:Number}}...)
@ SparseArrays ~/.julia/juliaup/julia-1.10.3+0.aarch64.linux.gnu/share/julia/stdlib/v1.10/SparseArrays/src/sparsevector.jl:1235
Possible fix, define
vcat(::AbstractMatrix{<:Number}, ::Union{MyArray{<:Number, 1}, MyArray{<:Number, 2}})
Stacktrace:
[1] top-level scope
@ REPL[9]:1 However, note that
|
That's totally reasonable and expected: in such a scenario, it is indeed not clear how to |
This effects [same intro as above]
julia> Base.hvcat(rows::Tuple{Vararg{Int64}}, x::AbstractMatrix, y::MyArray) = MyArray(hvcat(rows, x, y.data))
julia> hvcat((1,1), MyArray(rand(2,2)), MyArray(rand(2,2)))
4×2 MyArray{Float64, 2}:
0.989625 0.272548
0.577782 0.730749
0.884814 0.436665
0.202579 0.490402
julia> using SparseArrays
julia> hvcat((1,1), MyArray(rand(2,2)), MyArray(rand(2,2)))
ERROR: MethodError: hvcat(::Tuple{Int64, Int64}, ::MyArray{Float64, 2}, ::MyArray{Float64, 2}) is ambiguous.
Candidates:
hvcat(rows::Tuple{Vararg{Int64}}, x::AbstractMatrix, y::MyArray)
@ Main REPL[6]:1
hvcat(rows::Tuple{Vararg{Int64}}, X1::Union{Number, AbstractVecOrMat{<:Number}}, X::Union{Number, AbstractVecOrMat{<:Number}}...)
@ SparseArrays ~/.julia/juliaup/julia-1.11.0-beta1+0.aarch64.linux.gnu/share/julia/stdlib/v1.11/SparseArrays/src/sparsevector.jl:1279
Possible fix, define
hvcat(::Tuple{Vararg{Int64}}, ::AbstractMatrix{<:Number}, ::Union{MyArray{<:Number, 1}, MyArray{<:Number, 2}})
Stacktrace:
[1] top-level scope
@ REPL[10]:1 |
vcat
-related method ambiguitiesvcat
/hcat
/hvcat
-related method ambiguities
It does change the behavior when concatenating non-homogeneous arrays: julia> using SparseArrays
julia> vcat([0 1; 1 0], sparse([0 1; 1 0]))
4×2 SparseMatrixCSC{Int64, Int64} with 4 stored entries:
⋅ 1
1 ⋅
⋅ 1
1 ⋅
julia> Base.delete_method.(filter(
m -> nameof(m.module) == :SparseArrays && m.sig == Tuple{typeof(vcat), Union{Number,AbstractVecOrMat{<:Number}}, Vararg{Union{Number,AbstractVecOrMat{<:Number}}}},
methods(vcat)
))
1-element Vector{Nothing}:
nothing
julia> vcat([0 1; 1 0], sparse([0 1; 1 0]))
4×2 Matrix{Int64}:
0 1
1 0
0 1
1 0 |
The type-pirated
vcat
methods introduced here are leading to method ambiguities on Julia v1.10, of the form:This test passes on v1.9.3, but it fails on v1.10.0-beta2 because of the new method added here.
The text was updated successfully, but these errors were encountered: