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

Make abs, abs_imag, inv, and / resistant to under/overflow #14

Merged
merged 5 commits into from
Jan 24, 2023
Merged

Conversation

sethaxen
Copy link
Collaborator

This PR is the companion of JuliaGeometry/Quaternions.jl#122 and fixes #4. It primarily ensures abs, abs_imag, inv, and / do not under/overflow unnecessarily, following the implementations of the corresponding functions for Complex. It also implements RealDot.realdot as in JuliaGeometry/Quaternions.jl#119.

A quick before/after benchmark demonstrating the peformance cost of these changes.

julia> using BenchmarkTools, Octonions

julia> q, w = rand(OctonionF64, 2);

Before:

julia> @btime abs2($q);
  1.923 ns (0 allocations: 0 bytes)

julia> @btime abs($q);
  1.924 ns (0 allocations: 0 bytes)

julia> @btime $(Octonions.abs_imag)($q);
  1.926 ns (0 allocations: 0 bytes)

julia> @btime inv($q);
  12.884 ns (0 allocations: 0 bytes)

julia> @btime $q / $w;
  30.243 ns (0 allocations: 0 bytes)

After:

julia> @btime abs2($q);
  1.687 ns (0 allocations: 0 bytes)

julia> @btime abs($q);
  18.967 ns (0 allocations: 0 bytes)

julia> @btime $(Octonions.abs_imag)($q);
  16.890 ns (0 allocations: 0 bytes)

julia> @btime inv($q);
  26.269 ns (0 allocations: 0 bytes)

julia> @btime $q / $w;
  49.940 ns (0 allocations: 0 bytes)

@codecov
Copy link

codecov bot commented Jan 23, 2023

Codecov Report

Merging #14 (8760cf0) into main (ae650c6) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main       #14   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            2         2           
  Lines           68        92   +24     
=========================================
+ Hits            68        92   +24     
Impacted Files Coverage Δ
src/Octonions.jl 100.00% <ø> (ø)
src/octonion.jl 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@sethaxen
Copy link
Collaborator Author

sethaxen commented Jan 23, 2023

Note that the above benchmarks are for v1.9-beta3. For v1.8 and earlier, abs and abs_imag are much slower, because hypot was much slower. We should probably do I did what we did in JuliaGeometry/Quaternions.jl#122 and basically ported modern _hypot.

@sethaxen sethaxen requested a review from hyrodium January 23, 2023 23:38
Copy link
Collaborator

@hyrodium hyrodium left a comment

Choose a reason for hiding this comment

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

LGTM!

@sethaxen sethaxen merged commit 69b12a8 into main Jan 24, 2023
@sethaxen sethaxen deleted the numstable branch January 24, 2023 10:29
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.

Improving numerical stability
2 participants