Skip to content

Commit

Permalink
Improve accuracy of 2x2 symmetric/Hermitian eigendecomposition (#1158)
Browse files Browse the repository at this point in the history
* Improve accuracy of 2x2 symmetric/Hermitian eigendecomposition

* bump version

* Update src/eigen.jl

Co-authored-by: Yuto Horikawa <[email protected]>

---------

Co-authored-by: Yuto Horikawa <[email protected]>
  • Loading branch information
mateuszbaran and yuto-horikawa-rist committed Apr 30, 2023
1 parent b4e49ce commit dce8c1c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StaticArrays"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.5.23"
version = "1.5.24"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
11 changes: 2 additions & 9 deletions src/eigen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,10 @@ end
@inline function _eig(::Size{(2,2)}, A::LinearAlgebra.RealHermSymComplexHerm{T}, permute, scale) where {T <: Real}
a = A.data
TA = eltype(A)

@inbounds if A.uplo == 'U'
if !iszero(a[3]) # A is not diagonal
t_half = real(a[1] + a[4]) / 2
d = real(a[1] * a[4] - a[3]' * a[3]) # Should be real

tmp2 = t_half * t_half - d
tmp = tmp2 < 0 ? zero(tmp2) : sqrt(tmp2) # Numerically stable for identity matrices, etc.
tmp = norm(SVector((a[1] - a[4])/2, a[3]'))
vals = SVector(t_half - tmp, t_half + tmp)

v11 = vals[1] - a[4]
Expand All @@ -187,10 +183,7 @@ end
else # A.uplo == 'L'
if !iszero(a[2]) # A is not diagonal
t_half = real(a[1] + a[4]) / 2
d = real(a[1] * a[4] - a[2]' * a[2]) # Should be real

tmp2 = t_half * t_half - d
tmp = tmp2 < 0 ? zero(tmp2) : sqrt(tmp2) # Numerically stable for identity matrices, etc.
tmp = norm(SVector((a[1] - a[4])/2, a[2]))
vals = SVector(t_half - tmp, t_half + tmp)

v11 = vals[1] - a[4]
Expand Down
5 changes: 5 additions & 0 deletions test/eigen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ using StaticArrays, Test, LinearAlgebra
@test (@inferred SVector{2,ComplexF64} eigvals(Symmetric(m1), Symmetric(m2))) eigvals(Symmetric(m1_a), Symmetric(m2_a))
end

@testset "2×2, difficult case" begin
m1 = SA[1.6590891025248637 -2.7087777909606814e-7; -2.7087777909606814e-7 1.659089317183428]
@test norm(m1 - Array(eigen(m1))) < 1e-15
end

@test_throws DimensionMismatch eigvals(SA[1 2 3; 4 5 6], SA[1 2 3; 4 5 5])
@test_throws DimensionMismatch eigvals(SA[1 2; 4 5], SA[1 2 3; 4 5 5; 3 4 5])

Expand Down

2 comments on commit dce8c1c

@mateuszbaran
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/82598

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.5.24 -m "<description of version>" dce8c1c2869178cef26ac581e52bc6d7406ad19e
git push origin v1.5.24

Please sign in to comment.