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

Use sinc to fix RotationVec near zero #272

Merged
merged 6 commits into from
Nov 12, 2023
Merged

Use sinc to fix RotationVec near zero #272

merged 6 commits into from
Nov 12, 2023

Conversation

timholy
Copy link
Member

@timholy timholy commented Oct 13, 2023

By converting straight from RotationVec->QuatRotation,
and using an implementation that exploits sinc to handle θ ≈ 0,
we get continuous & differentiable behavior.

Requires JuliaDiff/ForwardDiff.jl#669 before tests will pass.

What this fixes: currently, near the origin the Hessian is zero (which is wrong), because *(::RotationVec, ::SVector{3}) expands only to linear order. But the fix is aimed at being broader than this specific case (otherwise, we could have just implemented a second-order approximation).

Note: please check whether the relaxation of the adjoint test is reasonable, perhaps exact equality is necessary? If so I'll need to come up with a fix.

By converting straight from RotationVec->QuatRotation,
and using an implementation that exploits `sinc` to handle θ ≈ 0,
we get continuous & differentiable behavior.
@hyrodium hyrodium self-requested a review October 13, 2023 15:25
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.

Thank you for the PR! I commented two suggestions.

test/derivative_tests.jl Outdated Show resolved Hide resolved
test/rotation_tests.jl Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Oct 20, 2023

Codecov Report

Merging #272 (6811364) into master (149edac) will increase coverage by 0.08%.
Report is 2 commits behind head on master.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #272      +/-   ##
==========================================
+ Coverage   90.42%   90.50%   +0.08%     
==========================================
  Files          17       17              
  Lines        1660     1664       +4     
==========================================
+ Hits         1501     1506       +5     
+ Misses        159      158       -1     
Files Coverage Δ
src/angleaxis_types.jl 99.00% <100.00%> (+1.07%) ⬆️

📣 Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today!

@hyrodium
Copy link
Collaborator

A type instability was introduced in this PR.

Before this PR

julia> using Rotations

julia> QuatRotation(RotationVec(0f0, 0f0, 0f0))
3×3 QuatRotation{Float32} with indices SOneTo(3)×SOneTo(3)(QuaternionF32(1.0, 0.0, 0.0, 0.0)):
 1.0  0.0  0.0
 0.0  1.0  0.0
 0.0  0.0  1.0

julia> QuatRotation(RotationVec(0f0, 0f0, 2f0))
3×3 QuatRotation{Float32} with indices SOneTo(3)×SOneTo(3)(QuaternionF32(0.540302, 0.0, 0.0, 0.841471)):
 -0.416147  -0.909297  0.0
  0.909297  -0.416147  0.0
  0.0        0.0       1.0

After this PR

julia> using Rotations

julia> QuatRotation(RotationVec(0f0, 0f0, 0f0))
3×3 QuatRotation{Float64} with indices SOneTo(3)×SOneTo(3)(QuaternionF64(1.0, 0.0, 0.0, 0.0)):
 1.0  0.0  0.0
 0.0  1.0  0.0
 0.0  0.0  1.0

julia> QuatRotation(RotationVec(0f0, 0f0, 2f0))
3×3 QuatRotation{Float32} with indices SOneTo(3)×SOneTo(3)(QuaternionF32(0.540302, 0.0, 0.0, 0.841471)):
 -0.416147  -0.909297  0.0
  0.909297  -0.416147  0.0
  0.0        0.0       1.0

src/angleaxis_types.jl Outdated Show resolved Hide resolved
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.

I'll merge this PR in a few days if there are no objections.

@hyrodium hyrodium merged commit bbd5980 into master Nov 12, 2023
10 checks passed
@hyrodium hyrodium deleted the teh/sinc branch November 12, 2023 14:44
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.

2 participants