-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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 real matrix sqrt and log for edge cases #40144
Conversation
How does this affect the performance? |
This version of |
With these changes, Updated BenchmarksSetup: using BenchmarkTools, LinearAlgebra
using Random; Random.seed!(1)
F = schur(randn(20, 20)^2)
x = F.T[1:2,1:2] # 2x2 block
y = zero(x) Before: julia> @btime $(LinearAlgebra._sqrt_real_2x2!)($y, $x);
35.130 ns (0 allocations: 0 bytes)
julia> @btime $(LinearAlgebra.sqrt_quasitriu)($(F.T));
16.880 μs (1 allocation: 3.25 KiB) This PR: julia> @btime $(LinearAlgebra._sqrt_real_2x2!)($y, $x);
47.072 ns (0 allocations: 0 bytes)
julia> @btime $(LinearAlgebra.sqrt_quasitriu)($(F.T));
17.599 μs (1 allocation: 3.25 KiB) |
The 2x2 diagonal block computation of |
With the recent changes, julia> @btime $(LinearAlgebra._sqrt_real_2x2!)($y, $x);
38.129 ns (0 allocations: 0 bytes)
julia> @btime $(LinearAlgebra.sqrt_quasitriu)($(F.T));
17.354 μs (1 allocation: 3.25 KiB) |
This is a bugfix so this probably needs to be backported? |
No, this fixes bugs introduced in #39973. The new tests added here, which fail on master, pass on Julia 1.6. |
Merging, as this has been approved by @stevengj and has had enough time for any objections or comments since then. |
* Add failing tests * Improve 2x2 real sqrt for scalar diagonal * Avoid sylvester if zero is a solution * Avoid unnecessary sqrt * Use correct variable names * Avoid erroring due to NaNs * Exactly adapt Higham's algorithm, with hypot * Remove unreachable branch * Add failing tests * Handle overflow/underflow * Invert instead of dividing * Apply d=0 constraint from standardized real schur form * Handle underflow * Avoid underflow/overflow in log diagonal blocks * Add tests for log underflow/overflow
* Add failing tests * Improve 2x2 real sqrt for scalar diagonal * Avoid sylvester if zero is a solution * Avoid unnecessary sqrt * Use correct variable names * Avoid erroring due to NaNs * Exactly adapt Higham's algorithm, with hypot * Remove unreachable branch * Add failing tests * Handle overflow/underflow * Invert instead of dividing * Apply d=0 constraint from standardized real schur form * Handle underflow * Avoid underflow/overflow in log diagonal blocks * Add tests for log underflow/overflow
* Add failing tests * Improve 2x2 real sqrt for scalar diagonal * Avoid sylvester if zero is a solution * Avoid unnecessary sqrt * Use correct variable names * Avoid erroring due to NaNs * Exactly adapt Higham's algorithm, with hypot * Remove unreachable branch * Add failing tests * Handle overflow/underflow * Invert instead of dividing * Apply d=0 constraint from standardized real schur form * Handle underflow * Avoid underflow/overflow in log diagonal blocks * Add tests for log underflow/overflow
Fixes JuliaLang/LinearAlgebra.jl#825, also
eliminates an unnecessary scalartries to reduce potential numerical issues insqrt
_sqrt_real_2x2!
.The added tests fail on master. Test 1 fails unless we make this change to
_sqrt_real_2x2!
. Test 2 also fails unless we make this change to_sqrt_quasitriu_offdiag_block_2x2!
.Edit: Test 3 tests the case where
NaN
s would causeLAPACK.trsyl!
to error.