# Problem with eltype of a non-numeric Symmetric matrix

**URL:** <https://discourse.julialang.org/t/problem-with-eltype-of-a-non-numeric-symmetric-matrix/23211>\
**Category:** General Usage\
**Created:** [April 16, 2019, 4:44pm UTC](https://discourse.julialang.org/t/problem-with-eltype-of-a-non-numeric-symmetric-matrix/23211 "2019-04-16T16:44:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lhatzis](https://avatars.discourse-cdn.com/v4/letter/l/a9a28c/32.png) [@lhatzis](https://discourse.julialang.org/u/lhatzis)\
**Post date:** [April 16, 2019, 4:44pm UTC](https://discourse.julialang.org/t/problem-with-eltype-of-a-non-numeric-symmetric-matrix/23211/1 "2019-04-16T16:44:18Z")

</div>

Hi everyone,

I have defined a custom type to wrap a Python library similarly to what [SymPy.jl](https://github.com/JuliaPy/SymPy.jl) does. So I will focus on a custom defined for SymPy but essentially the problem is the same for my custom type too.

If I define a symmetric matrix as:

```julia
using LinearAlgebra, SymPy

x = Matrix{Sym}(undef, 2, 2)
Symmetric(x)

```

the type of the output is `2×2 Symmetric{Any,Array{Sym,2}}`, while if I defined as

```julia
Symmetric{eltype(x),typeof(x)}(x, 'L')

```

the return type is `2×2 Symmetric{Sym,Array{Sym,2}}`.

That means that the `eltype` in the [definition of Symmetric](https://github.com/JuliaLang/julia/blob/d386e40c17d43b79fc89d3e579fc04547241787c/base/linalg/symmetric.jl#L44) returns type `Any`. Why is this happening and how can I resolve that? Thanks!

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [April 16, 2019, 5:10pm UTC](https://discourse.julialang.org/t/problem-with-eltype-of-a-non-numeric-symmetric-matrix/23211/2 "2019-04-16T17:10:58Z")

</div>

You’re looking at a really old version of Julia there. You’re probably running 1.1 or somesuch — the function that’s now called is this:

[https://github.com/JuliaLang/julia/blob/release-1.1/stdlib/LinearAlgebra/src/symmetric.jl#L48-L51](https://github.com/JuliaLang/julia/blob/release-1.1/stdlib/LinearAlgebra/src/symmetric.jl#L48-L51)

Symmetric is now recursive so it needs to reason about the transpose of things — and if it can’t then it just gives up and says `Any`. I’m not sure what all you need to do for a custom type.

---

<div class="post-metadata">

**Author:** ![lhatzis](https://avatars.discourse-cdn.com/v4/letter/l/a9a28c/32.png) [@lhatzis](https://discourse.julialang.org/u/lhatzis)\
**Post date:** [April 16, 2019, 7:10pm UTC](https://discourse.julialang.org/t/problem-with-eltype-of-a-non-numeric-symmetric-matrix/23211/3 "2019-04-16T19:10:53Z")

</div>

I haven’t noticed that I’m looking into an older version. Thanks for pointing it out!

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [April 16, 2019, 8:12pm UTC](https://discourse.julialang.org/t/problem-with-eltype-of-a-non-numeric-symmetric-matrix/23211/4 "2019-04-16T20:12:52Z")

</div>

I think the main problem is that `transpose(::Sym)` is not type-stable, so that `Base.promote_op(transpose, Sym) == Any`. `promote_op` is used here:

[https://github.com/JuliaLang/julia/blob/7132c635ab8da723c0177a9d6617bf01f4e7460a/stdlib/LinearAlgebra/src/symmetric.jl#L75](https://github.com/JuliaLang/julia/blob/7132c635ab8da723c0177a9d6617bf01f4e7460a/stdlib/LinearAlgebra/src/symmetric.jl#L75)

and `promote_op` uses `Core.Compiler.return_type` in its implementation.

This one case is easy enough to fix with a PR, but I’m thinking this is likely a pretty pervasive problem throughout SymPy.

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [April 16, 2019, 10:08pm UTC](https://discourse.julialang.org/t/problem-with-eltype-of-a-non-numeric-symmetric-matrix/23211/5 "2019-04-16T22:08:48Z")

</div>

Thanks for pointing this out. I have a PR to fix this particular issue, but how widely it needs doing remains to be seen.
