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

Add support for RealDot #119

Merged
merged 14 commits into from
Dec 16, 2022
Merged

Conversation

hyrodium
Copy link
Collaborator

@hyrodium hyrodium commented Dec 9, 2022

This PR adds RealDot.realdot(::Quaternion, ::Quaternion) as suggested in #58 (comment).

@hyrodium hyrodium requested a review from sethaxen December 9, 2022 14:40
@codecov
Copy link

codecov bot commented Dec 9, 2022

Codecov Report

Merging #119 (1e3e17c) into main (268cee0) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main      #119   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines          135       136    +1     
=========================================
+ Hits           135       136    +1     
Impacted Files Coverage Δ
src/Quaternion.jl 100.00% <100.00%> (ø)

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

Copy link
Collaborator

@sethaxen sethaxen left a comment

Choose a reason for hiding this comment

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

LGTM, just some minor suggestions.

There are 2 places in the code where we might consider using realdot:

Base.abs2(q::Quaternion) = RealDot.realdot(q, q)
# in slerp
coshalftheta = RealDot.realdot(qa, qb)

Merge with a patch version bump when you feel it's ready.

src/Quaternion.jl Outdated Show resolved Hide resolved
test/Quaternion.jl Outdated Show resolved Hide resolved
@hyrodium
Copy link
Collaborator Author

There are 2 places in the code where we might consider using realdot:

Replacing abs2 causes failures with tests added by #82 (comment) . We have the following choices to fix this problem.

Do you have any thoughts on this?

@sethaxen
Copy link
Collaborator

Fix realdot definition (x-ref:

I think this makes the most sense.

@mikmoore
Copy link
Contributor

mikmoore commented Dec 10, 2022

I am going to suggest that realdot be made to match the calculation of real(p*q) EDIT: real(p'*q) -- i.e., copy the first line of the * definition. I already spent significant effort in #82 making sure that abs2(q) and real(q*q') are exactly equal and as fast as I could manage. Anything but exact equality can cause headaches for users. Any speed difference will be negligible outside of microbenchmarks.

EDIT: Also, make sure that code_native((p,q)->real(p*q),NTuple{2,QuaternionF64}) doesn't succeed in dropping the imaginary parts of the calculation. If it does, then I would define both realdot and abs2 in terms of * so that we don't need to worry about keeping the implementations synchronized any more. The compiler failed to do this a year ago, but it's worth trying again while we're here (having removed the old .norm field might have helped).

test/Quaternion.jl Outdated Show resolved Hide resolved
@sethaxen
Copy link
Collaborator

I am going to suggest that realdot be made to match the calculation of real(p*q) -- i.e., copy the first line of the * definition.

It should be real(conj(p) * q), but otherwise, yes, I agree.

Also, make sure that code_native((p,q)->real(p*q),NTuple{2,QuaternionF64}) doesn't succeed in dropping the imaginary parts of the calculation. If it does, then I would define both realdot and abs2 in terms of * so that we don't need to worry about keeping the implementations synchronized any more. The compiler failed to do this a year ago, but it's worth trying again while we're here (having removed the old .norm field might have helped).

Even if the compiler manages to not calculate the imaginary part, it's still worth having these overloads, because common code transformations, such as automatic differentiation still happen at an earlier step in compilation process; the resulting code is more complex, and the compiler might not be able to eliminate the unused branch. Plus we support older Julia versions as well, and the downsides to implementing realdot and abs2 as we do are only needing to maintain 2 more LOCs, which is negligible.

@hyrodium
Copy link
Collaborator Author

Thank you for the comments! This PR is ready for final review.

Copy link
Collaborator

@sethaxen sethaxen left a comment

Choose a reason for hiding this comment

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

I think we also said that we could use realdot for abs2?

src/Quaternion.jl Show resolved Hide resolved
src/Quaternion.jl Outdated Show resolved Hide resolved
test/Quaternion.jl Show resolved Hide resolved
@hyrodium
Copy link
Collaborator Author

hyrodium commented Dec 15, 2022

I think we also said that we could use realdot for abs2?

Oh, sorry, I was forgetting that. I have updated that:+1:

Copy link
Collaborator

@sethaxen sethaxen left a comment

Choose a reason for hiding this comment

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

LGTM, thanks! Just please increment the patch version and make a patch release.

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.

3 participants