Skip to content

[DQ.cpp] Fix rotation_axis() to match the Matlab implementation - #77

Merged
mmmarinho merged 2 commits into
dqrobotics:masterfrom
juanjqo:hotfix_rotation_axis
Nov 11, 2026
Merged

mmmarinho merged 2 commits into
dqrobotics:masterfrom
juanjqo:hotfix_rotation_axis

Conversation

@juanjqo

@juanjqo juanjqo commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

@dqrobotics/developers

Hi @mmmarinho, I spotted a difference between the C++ and MATLAB implementations of rotation_axis(). The C++ version doesn't check whether sin(phi) is numerically zero when the real part of the rotation is close to -1. MATLAB checks if(sin(phi) <= eps) and returns the conventional axis k_, but C++ only checks if(phi == 0.0).

include_namespace_dq
theta = 1e-8;
r = -(cos(theta/2) + k_*sin(theta/2));   % rotation of 1e-8 rad about k_, written with the negative sign
n = rotation_axis(r)

Output:

n = 

         1k
#include <dqrobotics/DQ.h>
#include <iostream>

int main()
{
    const double theta = 1e-8;
    const DQ_robotics::DQ r = -(cos(theta/2) + DQ_robotics::k_*sin(theta/2));
    std::cout << r.rotation_axis() << std::endl;
}

Output:

 - 40828098.382988k

Since log(), Q4(), and Q8() use rotation_axis(), this also affects them.

This PR adds the same check as the MATLAB version. Please let me know if additional modifications are required.

With this PR, the C++ output matches MATLAB:

 1k

Kind regards,

Juancho

@juanjqo
juanjqo marked this pull request as ready for review November 10, 2026 23:13
Comment thread src/DQ.cpp

double phi = rotation_angle()/2.0;
if(phi == 0.0)
if (sin(phi) <= std::numeric_limits<double>::epsilon())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Indeed implementation seems to match MATLAB literaly.
https://github.com/dqrobotics/matlab/blob/master/%40DQ/rotation_axis.m

@mmmarinho
mmmarinho merged commit 121144e into dqrobotics:master Nov 11, 2026
3 of 6 checks passed
@juanjqo
juanjqo deleted the hotfix_rotation_axis branch November 16, 2026 16:17
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