Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix precision of 2x2 eigenvectors #1191

Merged
merged 6 commits into from
Sep 8, 2023
Merged

Fix precision of 2x2 eigenvectors #1191

merged 6 commits into from
Sep 8, 2023

Conversation

KnutAM
Copy link
Contributor

@KnutAM KnutAM commented Sep 4, 2023

Fixes #956 by subtracting mean diagonal (draft because for now only for A.uplo == 'U', see question below)

a = SMatrix{2,2}((1.0, 1e-100, 1e-100, 1.0))
Master (fast, but incorrect)

@btime eigen($a)
9.510 ns (0 allocations: 0 bytes)
eigen(a)
Eigen{Float64, Float64, SMatrix{2, 2, Float64, 4}, SVector{2, Float64}}
values:
2-element SVector{2, Float64} with indices SOneTo(2):
 1.0
 1.0
vectors:
2×2 SMatrix{2, 2, Float64, 4} with indices SOneTo(2)×SOneTo(2):
 0.0  0.0
 1.0  1.0

PR (slower, but correct)

julia> @btime eigen($a)
  11.011 ns (0 allocations: 0 bytes)

julia> eigen(a)
Eigen{Float64, Float64, SMatrix{2, 2, Float64, 4}, SVector{2, Float64}}
values:
2-element SVector{2, Float64} with indices SOneTo(2):
 1.0
 1.0
vectors:
2×2 SMatrix{2, 2, Float64, 4} with indices SOneTo(2)×SOneTo(2):
 -0.707107  0.707107
  0.707107  0.707107

Question

Why is the algorithm split between upper and lower diagonal, should be easier to do

a21 = A.uplo == 'U' ? a[3]' : a[2]

And then the algorithms are equal.

For now, I have only changed the algorithm for upper diagonal entry (hence draft), + no tests are added yet. Once I understand this choice, I can finish it up!

@mateuszbaran
Copy link
Collaborator

I think the added accuracy is worth sacrificing a bit of performance. The implementation is probably split because no one noticed before that defining a21 like you did would be enough -- I think this would be a good improvement.

@mateuszbaran
Copy link
Collaborator

BTW, could you fix the issue on Julia 1.10? You just need to replace

@test_throws ArgumentError SA[1;2;;3]

with

if VERSION < v"1.10-DEV"
    @test_throws ArgumentError SA[1;2;;3]
else
    @test_throws DimensionMismatch SA[1;2;;3]
end

in initializers.jl.

@KnutAM
Copy link
Contributor Author

KnutAM commented Sep 6, 2023

Thanks!

@KnutAM KnutAM marked this pull request as ready for review September 6, 2023 22:29
Copy link
Collaborator

@mateuszbaran mateuszbaran left a comment

Choose a reason for hiding this comment

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

LGTM 👍 . Could you just bump patch version?

@mateuszbaran mateuszbaran merged commit 084e215 into JuliaArrays:master Sep 8, 2023
20 of 27 checks passed
@KnutAM KnutAM deleted the kam/2x2eigenprec branch September 8, 2023 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

eigen not working properly for 2x2 Hermitian SMatrices
2 participants