Skip to content

[Language Incompatibility] Fix bug in the dec_mult function - #78

Merged
mmmarinho merged 1 commit into
dqrobotics:masterfrom
juanjqo:hotfix_dec_mult
Nov 20, 2026
Merged

mmmarinho merged 1 commit into
dqrobotics:masterfrom
juanjqo:hotfix_dec_mult

Conversation

@juanjqo

@juanjqo juanjqo commented Nov 16, 2026

Copy link
Copy Markdown
Member

@dqrobotics/developers

Hi, @mmmarinho

I believe there is a bug related to language incompatibility in the C++ version. The results for a .* b in MATLAB are different from dec_mult(a,b) in C++. They match when I reverse the order in C++

a .* b != dec_mult(a,b)
a .* b = dec_mult(b,a)

This PR fixes the function to match the MATLAB version.

Kind regards,

Juancho

Minimal example

#include <cmath>
#include <iostream>
using namespace DQ_robotics;

int main()
{
    const DQ a = (1 + 0.5*E_*(1*i_))*(cos(M_PI/4) + i_*sin(M_PI/4));
    const DQ b = (1 + 0.5*E_*(2*j_))*(cos(M_PI/4) + j_*sin(M_PI/4));

    /// Decompositional multiplication (Adorno and Fraisse, 2017, Definition 1 (Eq. 10)):
    /// a (x) b = T(a)*T(b)*P(a)*P(b)

    const DQ expected = T(a)*T(b)*P(a)*P(b);
    std::cout << "expected      = " << expected << std::endl;
    std::cout << "dec_mult(a,b) = " << dec_mult(a, b) << std::endl;
    std::cout << "error         = " << vec8(dec_mult(a, b) - expected).norm() << std::endl;
}

Output

expected      = 0.5 + 0.5i + 0.5j + 0.5k + E*( - 0.75 + 0.75i + 0.25j - 0.25k)
dec_mult(a,b) = 0.5 + 0.5i + 0.5j - 0.5k + E*( - 0.75 - 0.25i + 0.75j - 0.25k)
error         = 1.5

Minimal example (MATLAB)

% Decompositional multiplication (Adorno and Fraisse, 2017, Definition 1 (Eq. 10)):
% a (x) b = T(a)*T(b)*P(a)*P(b)
include_namespace_dq

a = (1 + 0.5*E_*(1*i_))*(cos(pi/4) + i_*sin(pi/4));  % 90 deg about x, then 1 m along x
b = (1 + 0.5*E_*(2*j_))*(cos(pi/4) + j_*sin(pi/4));  % 90 deg about y, then 2 m along y

expected = T(a)*T(b)*P(a)*P(b)
matlab   = a .* b

norm(vec8(matlab - expected))

Output

expected = 

         (0.5 + 0.5i + 0.5j + 0.5k) + E*( - 0.75 + 0.75i + 0.25j - 0.25k)

matlab = 

         (0.5 + 0.5i + 0.5j + 0.5k) + E*( - 0.75 + 0.75i + 0.25j - 0.25k)

ans =

     0

@mmmarinho

Copy link
Copy Markdown
Member

Hi @juanjqo ,

Thanks. Surprisingly, this seems to have been the case for the entire life of the cpp version.

Could you please add the operator .* in cpp as part of this PR? Otherwise, the language incompatibility will persist because dec_mult doesn't seem to exist in MATLAB.

Kind regards,
Murilo

@juanjqo

juanjqo commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

@mmmarinho Sure thing!

@juanjqo
juanjqo marked this pull request as draft November 16, 2026 17:53
@mmmarinho
mmmarinho marked this pull request as ready for review November 20, 2026 09:23
@mmmarinho

Copy link
Copy Markdown
Member

@juanjqo Unfortunately that operator isn't overloadable, so we have our hands tied. I will approve this one than perhaps you might want to open one on MATLAB to implement dec_mult so that there is as much compatibility as possible.

@mmmarinho
mmmarinho merged commit 386dd74 into dqrobotics:master Nov 20, 2026
3 of 6 checks passed
@bvadorno

Copy link
Copy Markdown
Member

Hi, @juanjqo and @mmmarinho.

How about overloading another operator, such as %? I don't think that a % b is stranger than a .*b.

Since the remainder operator doesn't make much sense for dual quaternions, I don't see any issues using it.

Kind regards,
Bruno

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