StaticArrays.jl icon indicating copy to clipboard operation
StaticArrays.jl copied to clipboard

Inverse of symmetric static arrays returns errors

Open Boxylmer opened this issue 3 years ago • 3 comments

MWE:

using LinearAlgebra
using StaticArrays

my_matrix = [
    23.839174539658693 243552.4933521217 1.8416855269559405e9 7262.007338486136 -0.15093214378094674 -0.08311979038696746; 
    243552.4933521217 2.962601669841655e9 1.565059094802e13 7.454589007612145e7 -1822.7229289215243 -703.6834309479851; 
    1.8416855269559405e9 1.565059094802e13 1.852842171814476e17 5.52741246224e11 -9.820212598493524e6 -8.437622282150278e6; 
    7262.007338486136 7.454589007612145e7 5.52741246224e11 2.214925230782032e6 -46.13837676487657 -24.926993518175713; 
    -0.15093214378094674 -1822.7229289215243 -9.820212598493524e6 -46.13837676487657 0.0011236787686097864 0.00044192488231854755; 
    -0.08311979038696746 -703.6834309479851 -8.437622282150278e6 -24.926993518175713 0.00044192488231854755 0.0003843024068388386
    ]
my_static_matrix = @SMatrix[
    23.839174539658693 243552.4933521217 1.8416855269559405e9 7262.007338486136 -0.15093214378094674 -0.08311979038696746; 
    243552.4933521217 2.962601669841655e9 1.565059094802e13 7.454589007612145e7 -1822.7229289215243 -703.6834309479851; 
    1.8416855269559405e9 1.565059094802e13 1.852842171814476e17 5.52741246224e11 -9.820212598493524e6 -8.437622282150278e6; 
    7262.007338486136 7.454589007612145e7 5.52741246224e11 2.214925230782032e6 -46.13837676487657 -24.926993518175713; 
    -0.15093214378094674 -1822.7229289215243 -9.820212598493524e6 -46.13837676487657 0.0011236787686097864 0.00044192488231854755; 
    -0.08311979038696746 -703.6834309479851 -8.437622282150278e6 -24.926993518175713 0.00044192488231854755 0.0003843024068388386
    ]

my_symmetric_matrix = Symmetric(my_matrix)
my_symmetric_static_matrix = Symmetric(my_static_matrix)

myinversion = inv(my_symmetric_matrix)
mystaticinversion = inv(my_symmetric_static_matrix)

Returns

ERROR: LoadError: MethodError: no method matching bunchkaufman!(::Symmetric{Float64, SMatrix{6, 6, Float64, 36}}, ::Bool; check=true)

On Julia 1.6.0

Apologies for the unnecessarily large matrix, I'm copying this from some work I was originally trying to do for a model I'm getting the covariance matric of through the inverse Hessian.

Boxylmer avatar Aug 25 '21 22:08 Boxylmer

LinearAlgebra is trying to factorize symmetric matrices for inversion using the Bunch-Kafuman algorithm which is not available in StaticArrays.jl. You can either just use LinearAlgebra and accept the non-ideal performance, drop the Symmetric wrapper and use StaticArrays.jl solver with LU decomposition, or implement Bunch-Kafuman for static arrays.

mateuszbaran avatar Aug 26 '21 09:08 mateuszbaran

Implement Bunch-Kafuman for static arrays

Presumably this is preferred?

Drop the Symmetric wrapper and use StaticArrays.jl solver with LU decomposition

Is there a way to get StaticArrays to do this, so it doesn't throw an error? Don't you want StaticArrays to support all operations in the Standard Library without throwing an error?

ojwoodford avatar Mar 23 '23 10:03 ojwoodford

Implement Bunch-Kafuman for static arrays

Presumably this is preferred?

Yes, probably, though so far no one has made a PR.

Drop the Symmetric wrapper and use StaticArrays.jl solver with LU decomposition

Is there a way to get StaticArrays to do this, so it doesn't throw an error? Don't you want StaticArrays to support all operations in the Standard Library without throwing an error?

Supporting all LinearAlgebra operations would be ideal but that's a lot of work, and people tend to focus on what's important to them. I can review a PR for this issue in case someone wants to make one.

mateuszbaran avatar Mar 23 '23 11:03 mateuszbaran