# Inverse of a view of a symmetric matrix

**URL:** <https://discourse.julialang.org/t/inverse-of-a-view-of-a-symmetric-matrix/25877>\
**Category:** Numerics\
**Tags:** linearalgebra\
**Created:** [July 1, 2019, 12:32am UTC](https://discourse.julialang.org/t/inverse-of-a-view-of-a-symmetric-matrix/25877 "2019-07-01T00:32:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipelle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fipelle/32/4772_2.png) [@fipelle](https://discourse.julialang.org/u/fipelle)\
**Post date:** [July 1, 2019, 12:32am UTC](https://discourse.julialang.org/t/inverse-of-a-view-of-a-symmetric-matrix/25877/1 "2019-07-01T00:32:35Z")

</div>

Hi,

I am trying to compute the inverse of a view of a symmetric matrix via:

```julia
using LinearAlgebra
V=Symmetric(rand(10,10)+50*I);
V_view = @view V[1:5,1:5];
V_view_inv = inv(V_view);

```

However, it does not work and I get the following error message:

```julia
ERROR: MethodError: no method matching factorize(::SubArray{Float64,2,Symmetric{Float64,Array{Float64,2}},Tuple{UnitRange{Int64},UnitRange{Int64}},false})

```

I wonder whether I should use a different (specialised) inverse function to perform this operation. What would you suggest to do?

---

<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:** [July 1, 2019, 1:24am UTC](https://discourse.julialang.org/t/inverse-of-a-view-of-a-symmetric-matrix/25877/2 "2019-07-01T01:24:39Z")

</div>

You could use `V_view = Symmetric(@view parent(V)[1:5, 1:5])`, so that the `Symmetric` wrapper is on the outside rather than on the inside. There should probably be a fallback that allows you to do what you tried, but I think I think this is better anyway, since what you’re doing causes there to be a loss of compile time information regarding the structure of the matrix: a view of a symmetric matrix is not necessarily symmetric (for arbitrary `UnitRange`s), so Julia can’t dispatch to a specialized method for computing the inverse of a symmetric matrix.

---

<div class="post-metadata">

**Author:** ![fipelle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fipelle/32/4772_2.png) [@fipelle](https://discourse.julialang.org/u/fipelle)\
**Post date:** [July 1, 2019, 1:31am UTC](https://discourse.julialang.org/t/inverse-of-a-view-of-a-symmetric-matrix/25877/3 "2019-07-01T01:31:18Z")

</div>

Thank you.

I am not familiar with the `parent(.)` syntax. Would you mind giving me more details on that? Is there any difference between `V.data` and `parent(V)`?

---

<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:** [July 1, 2019, 1:35am UTC](https://discourse.julialang.org/t/inverse-of-a-view-of-a-symmetric-matrix/25877/4 "2019-07-01T01:35:09Z")

</div>

It’s the same:

[https://github.com/JuliaLang/julia/blob/8d4f6d24c0fa87509fcf40788ca2c83cdfecd9af/stdlib/LinearAlgebra/src/symmetric.jl#L270](https://github.com/JuliaLang/julia/blob/8d4f6d24c0fa87509fcf40788ca2c83cdfecd9af/stdlib/LinearAlgebra/src/symmetric.jl#L270)

But directly accessing a field of the `Symmetric` type could be seen as relying on an implementation detail. It’s not likely that the `data` field will be renamed in the future, but if there’s a documented function you can use to achieve the same, it’s generally preferable to use it.
