# Push!() giving Method type error

I am getting a Method error, specifically a type conversion error, specifically Array{Float64,2} to type Int64.

I don’t understand types very well yet, and definitely don’t understand why push!() would be trying to convert to an integer. Here’s my code, “principal_minor.jl” is working well in the REPL.

``````using Symbolics
using Latexify
using SymPy
using LinearAlgebra

include("principal_minor.jl")

function characteristic_polynomial(A)
z = size(A)
n = z[1]
p = [1]
for i = 1:n
p_0 = (-1)^i*sum(principal_minor(A,i))
#print(typeof(p_0))
push!(p, p_0)
end
return p
end
``````

I’ve tried casting a few of the arguments but didn’t work. Any feedback is appreciated, especially quick tips about dealing with these type errors as they have been the roadblock I come across the most.

Thanks for reading!

p is of type Vector{Int} and you try to push! something like a Array{Float64,2} (p_0) into this array.

``````julia> p = [1]
1-element Vector{Int64}:
1
``````

Your code is not a MWE (minimal working example) so it’s not clear what p_0 is.
What is the output of `print(typeof(p_0))`?
For now a naiv solution can be not to initialize `p` as you do it, but e.g.:

``````function characteristic_polynomial(A)
z = size(A)
n = z[1]
for i = 1:n
p_0 = (-1)^i*sum(principal_minor(A,i))
if ! @isdefined p
p=Array{typeof(p_0)}(undef,0)
end
push!(p, p_0)
end
return p
end
``````

This assumes that the type of p_0 is always the same.

1 Like

thanks. I will look into the details of your response and follow up.

2 Likes

exactly as you said. I fixed it by changing `sum(A)` to `sum(sum(principal_subminor(A,i)))`.
while this works for arrays with entries that are matrices, I know it won’t work for tensors of higher dimension, though I feel like I could write a function to do so. regardless, not relevant in this characteristic polynomial computation at the moment.

thanks for the response.

### Edited that is, `sum(sum(principal_subminor(A,i))`

1 Like

small suggestion

``````z = size(A)
n = z[1]
for i = 1:n
``````

can become

`for i in 1:size(A, 1)`

3 Likes

If you want there’s always the option of playing the broadcasting game:

``````p = (-1).^(1:size(A, 1)) .* sum.(principal_minor.(Ref(A), 1:size(A, 1)))
``````
1 Like