# "dispatch" on whether or not a matrix is symmetric? (aka should I use a function barrier?)

**URL:** https://discourse.julialang.org/t/dispatch-on-whether-or-not-a-matrix-is-symmetric-aka-should-i-use-a-function-barrier/33086
**Category:** New to Julia
**Created:** [January 8, 2020, 4:56am UTC](https://discourse.julialang.org/t/dispatch-on-whether-or-not-a-matrix-is-symmetric-aka-should-i-use-a-function-barrier/33086 "2020-01-08T04:56:22Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)
#### Post date: [January 8, 2020, 4:56am UTC](https://discourse.julialang.org/t/dispatch-on-whether-or-not-a-matrix-is-symmetric-aka-should-i-use-a-function-barrier/33086/1 "2020-01-08T04:56:22Z")

</div>

Short version: is a pattern like `issymmetric(x) && (x = Symmetric(x))` type-unstable, and will it have performance consequences? Details below.

I have a user-facing function that looks something like

```julia
function f(x::Matrix)
    for i in 1:big_number
        helper(x)
    end
end

```

And `helper` has a fast version and a slow version:

```julia
helper(x::AbstractMatrix) = ... # slow
helper(x::Symmetric) = ... # fast

```

I’d like to efficiently use the version of `helper` appropriate to whether or not `x` is symmetric, _without_ requiring the user to themselves wrap `x` in a `Symmetric` view. That is, I don’t want to have two methods `f(x::Symmetric)` and `f(x::AbstractMatrix)`.

So I have two ideas:

**1. Use a function barrier:**

```julia
function f(x)
    issymmetric(x) && (x = Symmetric(x))
    return _f_logic(x)
end
function _f_logic(x)
    for i in 1:big_number
        helper(x)
    end
end

```

**2. Simply wrap `x` in `Symmetric` within the main function:**

```julia
function f(x::Matrix)
    issymmetric(x) && (x = Symmetric(x))
    for i in 1:big_number
        helper(x)
    end
end

```

Now, I’d expect - and `@code_warntype` seems to confirm - that option 2 introduces type instability since the compiler can’t know the type of `x` within the big loop; that type depends on the `issymmetric` call which depends on runtime values. In practice, however, there’s no meaningful performance difference between these two approaches and option 2 seems rather more readable to me.

Am I missing something? Is there a more idiomatic way to deal with this use case?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 8, 2020, 7:36am UTC](https://discourse.julialang.org/t/dispatch-on-whether-or-not-a-matrix-is-symmetric-aka-should-i-use-a-function-barrier/33086/2 "2020-01-08T07:36:19Z")

</div>

> [@evanfields](#):
>
> efficiently use the version of `helper` appropriate to whether or not `x` is symmetric, _without_ requiring the user to themselves wrap `x` in a `Symmetric` view

Why? Using `Symmetric` is the perfect way to signal that the matrix is symmetric.

The function barrier approach is your best option if symmetry needs to be checked in runtime. This is what all polyalgorithms do, eg `\`.

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [January 8, 2020, 8:07am UTC](https://discourse.julialang.org/t/dispatch-on-whether-or-not-a-matrix-is-symmetric-aka-should-i-use-a-function-barrier/33086/3 "2020-01-08T08:07:51Z")

</div>

To supplement what @Tamas_Papp said, check out the runtime checks for `\` here:

[https://github.com/JuliaLang/julia/blob/master/stdlib/LinearAlgebra/src/generic.jl#L1089](https://github.com/JuliaLang/julia/blob/master/stdlib/LinearAlgebra/src/generic.jl#L1089)

---

<div class="post-metadata">

### Author: ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)
#### Post date: [January 8, 2020, 1:37pm UTC](https://discourse.julialang.org/t/dispatch-on-whether-or-not-a-matrix-is-symmetric-aka-should-i-use-a-function-barrier/33086/4 "2020-01-08T13:37:56Z")

</div>

> Why? Using `Symmetric` is the perfect way to signal that the matrix is symmetric

In this case, the user may generate the matrix without knowing or caring that it’s symmetric; it’s just a convenient property. I don’t want to offload the work of wondering whether a matrix has certain convenient properties onto the user.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 8, 2020, 2:25pm UTC](https://discourse.julialang.org/t/dispatch-on-whether-or-not-a-matrix-is-symmetric-aka-should-i-use-a-function-barrier/33086/5 "2020-01-08T14:25:00Z")

</div>

Unfortunately in practice, matrices which “should be” symmetric may not end up as one because of floating error.

Whether this is important depends on your problem.

---

<div class="post-metadata">

### Author: ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)
#### Post date: [January 8, 2020, 6:47pm UTC](https://discourse.julialang.org/t/dispatch-on-whether-or-not-a-matrix-is-symmetric-aka-should-i-use-a-function-barrier/33086/6 "2020-01-08T18:47:50Z")

</div>

Yeah, that’s a great point. The use case here is that matrices are distance matrices defining instances of the traveling salesman problem. A matrix that should be symmetric but isn’t due to floating point error should still produce a valid problem instance, albeit one that solves more slowly.

One of my main goals with TravelingSalesmanHeuristics.jl is ease of use, so I’m willing to accept “occasional slower-than-needed algorithms because a matrix is barely asymmetric” as the cost of “not making the user explicitly indicate whether or not their problem is symmetric.”

---

<div class="post-metadata">

### Author: ![MichelJuillard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/micheljuillard/32/10555_2.png) [@MichelJuillard](https://discourse.julialang.org/u/MichelJuillard)
#### Post date: [January 8, 2020, 8:29pm UTC](https://discourse.julialang.org/t/dispatch-on-whether-or-not-a-matrix-is-symmetric-aka-should-i-use-a-function-barrier/33086/7 "2020-01-08T20:29:40Z")

</div>

You may want to consider forcing exact symmetry with

```julia
AS .= 0.5*(A .+ A')

```

that may be cheap in comparison with solving a non symmetric problem
