I get a MethodError in my code and I’m trying to understand the behaviour. Functions of tuples of matrices work and functions of vectors of tuples of matrices give a MethodError. A minimum working example would be the following:
julia> A=([[1,2] [3,4]], [[5,6] [7,8]])
([1 3; 2 4], [5 7; 6 8])
julia> function f(P::Tuple{Matrix, Matrix}) return P[1]+P[2] end
f (generic function with 1 method)
julia> f(A)
2×2 Matrix{Int64}:
6 10
8 12
julia> function f(P::Vector{Tuple{Matrix, Matrix}}) return [Q[1]+Q[2] for Q in P] end
f (generic function with 2 methods)
julia> f([A])
ERROR: MethodError: no method matching f(::Vector{Tuple{Matrix{Int64}, Matrix{Int64}}})
Closest candidates are:
f(::Tuple{Matrix, Matrix})
@ Main REPL[3]:1
f(::Vector{Tuple{Matrix, Matrix}})
@ Main REPL[5]:1
Stacktrace:
[1] top-level scope
@ REPL[6]:1
I fixed this as follows:
julia> function g(P::Vector{Tuple{Matrix{T}, Matrix{T}}}) where T return [Q[1]+Q[2] for Q in P] end
g (generic function with 1 method)
julia> g([A])
1-element Vector{Matrix{Int64}}:
[6 10; 8 12]
To avoid getting this error in the future, my question is why the first thing doesn’t work and the second thing does and in what cases I will encounter this.