问题
I am confused about how parametric methods work in Julia. I also hope that parametric is the right word to use here. I've read the docs on methods but it is still not clear why the following error occurs. If I define a function as below
function Bazinga{T<:Real}(mat ::Union{Array{T,2},SparseMatrixCSC})
mat^4
end
and run
Penny = sparse(randn(10,10))
Bazinga(Penny)
I get
ERROR: MethodError: `Bazinga` has no method matching Bazinga(::SparseMatrixCSC{Float64,Int64})
Closest candidates are:
Bazinga{T<:Real}(::Union{Array{T<:Real,2},SparseMatrixCSC{Tv,Ti<:Integer}})
But Int64<:Int is true so what is the problem here?
If I redefine the function as
function Bazinga(mat ::Union{Array{Real,2},SparseMatrixCSC})
mat^4
end
no error is produced and the function works. Also there are now two methods of the function
Bazinga(mat::Union{Array{Real,2},SparseMatrixCSC{Tv,Ti<:Integer}}) at none:2
Bazinga{T<:Real}(mat::Union{Array{T<:Real,2},SparseMatrixCSC{Tv,Ti<:Integer}}) at none:2
in which both SparseMatrixCSC{Tv,Ti<:Integer} is in, but only in the first case it causes an error.
Any input is wellcome!
Edit1: And why is Array{Float64,1} <:Array{Real,1} false?
Edit2: The question in the Edit1 is solved by Arrays break string types in Julia . But I don't think it explains what happens with the method. Or at least I don't see it.
回答1:
I don't quite know the reason under the hood, but using a TypeVar may be a workaround for your use case:
T = TypeVar(:T, Union{}, Real, false)
function Bazinga(mat::Union{Array{T,2},SparseMatrixCSC})
mat^4
end
julia> Bazinga(sparse(randn(2,2)))
2x2 sparse matrix with 4 Float64 entries:
[1, 1] = 0.840151
[2, 1] = -0.503551
[1, 2] = -0.437787
[2, 2] = 1.28652
julia> Bazinga(randn(2,2))
2x2 Array{Float64,2}:
0.203916 -0.261589
0.0107211 -0.0137373
UPDATE:
After some investigation, I find the "canonical" way to solve this problem is to define a typealias:
typealias RealMatrix{T<:Real} Array{T, 2}
function Bazinga(mat::Union{RealMatrix, SparseMatrixCSC})
mat^4
end
I guess the reason is Julia can't complete method matching tasks without knowing what on earth all of the parametric types are. Here is another example:
Foo{R<:Real,C<:Complex}(x::Union{Array{R}, SparseMatrixCSC{C}}) = x
Neither Foo(sparse([1+im 2; 3 4])) nor Foo(rand(2,2)) will work. So the correct way to write a parametric-union method is to constrain those different parameters via typealias:
julia> typealias Bar{T<:Real} Array{T,2}
Array{T<:Real,2}
julia> typealias Baz{T<:Complex} SparseMatrixCSC{T}
SparseMatrixCSC{T<:Complex{T<:Real},Ti<:Integer}
julia> Foo(x::Union{Bar, Baz}) = x
Foo (generic function with 1 method)
julia> Foo(rand(2,2))
2x2 Array{Float64,2}:
0.000739447 0.713386
0.32024 0.705593
julia> Foo(sparse([1+im; 2im]))
2x1 sparse matrix with 2 Complex{Int64} entries:
[1, 1] = 1+1im
[2, 1] = 0+2im
If argument types of the union have the same type parameter T, we can also use typealias as follows:
julia> typealias MyUnion{T,N<:Integer} Union{Array{T,2}, SparseMatrixCSC{T,N}}
Union{Array{T,2},SparseMatrixCSC{T,N<:Integer}}
julia> Foo{T<:Real}(x::MyUnion{T}) = x
Foo (generic function with 1 method)
julia> Foo(rand(2,2))
2x2 Array{Float64,2}:
0.0172915 0.587518
0.234148 0.00543953
julia> Foo(rand(2))
ERROR: MethodError: `Foo` has no method matching Foo(::Array{Float64,1})
julia> Foo(sparse(rand(2)))
2x1 sparse matrix with 2 Float64 entries:
[1, 1] = 0.900029
[2, 1] = 0.0634815
julia> Foo(sparse(rand(2,2)))
2x2 sparse matrix with 4 Float64 entries:
[1, 1] = 0.592632
[2, 1] = 0.066563
[1, 2] = 0.805307
[2, 2] = 0.923422
julia> Foo(rand(2,2))
2x2 Array{Float64,2}:
0.167078 0.673194
0.681418 0.316017
UPDATE 2:
In Julia-v0.6+, defining the typealias is not necessary:
function Bazinga(mat::Union{Array{<:Real,2},SparseMatrixCSC})
mat^4
end
Please refer to this answer for further details.
来源:https://stackoverflow.com/questions/39079213/sparse-matrices-and-type-constraints-in-julia