# Push!() giving Method type error

**URL:** https://discourse.julialang.org/t/push-giving-method-type-error/76322
**Category:** New to Julia
**Tags:** question
**Created:** [February 12, 2022, 11:08pm UTC](https://discourse.julialang.org/t/push-giving-method-type-error/76322 "2022-02-12T23:08:33Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Brian\_Becsi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian_becsi/32/19925_2.png) [@Brian\_Becsi](https://discourse.julialang.org/u/Brian_Becsi)
#### Post date: [February 12, 2022, 11:08pm UTC](https://discourse.julialang.org/t/push-giving-method-type-error/76322/1 "2022-02-12T23:08:33Z")

</div>

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.

```julia
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!

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [February 12, 2022, 11:34pm UTC](https://discourse.julialang.org/t/push-giving-method-type-error/76322/2 "2022-02-12T23:34:33Z")

</div>

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

```julia
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.:

```julia
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.

---

<div class="post-metadata">

### Author: ![Brian\_Becsi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian_becsi/32/19925_2.png) [@Brian\_Becsi](https://discourse.julialang.org/u/Brian_Becsi)
#### Post date: [February 13, 2022, 12:01am UTC](https://discourse.julialang.org/t/push-giving-method-type-error/76322/3 "2022-02-13T00:01:16Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Brian\_Becsi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian_becsi/32/19925_2.png) [@Brian\_Becsi](https://discourse.julialang.org/u/Brian_Becsi)
#### Post date: [February 13, 2022, 1:43am UTC](https://discourse.julialang.org/t/push-giving-method-type-error/76322/4 "2022-02-13T01:43:17Z")

</div>

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))`

---

<div class="post-metadata">

### Author: ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)
#### Post date: [February 13, 2022, 8:01am UTC](https://discourse.julialang.org/t/push-giving-method-type-error/76322/5 "2022-02-13T08:01:41Z")

</div>

small suggestion

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

```

can become

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

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [February 13, 2022, 8:52am UTC](https://discourse.julialang.org/t/push-giving-method-type-error/76322/6 "2022-02-13T08:52:07Z")

</div>

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

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

```
