Inverse of a view of a symmetric matrix


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

using LinearAlgebra
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:

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?

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 UnitRanges), so Julia can’t dispatch to a specialized method for computing the inverse of a symmetric matrix.


Thank you.

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

It’s the same:

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.