[LANGUAGE INCOMPATIBILITY] Constructing Pure DQ With Double Values As Arguments
Describe the Missing/Unexpected Functionality
This problem is somewhat related to #24, but using a different constructor for pure quaternions or pure dual quaternions. Namely, when constructing them using double values, the C++ version of the code does not return pure quaternions or pure dual quaternions. In contrast, this construction is possible in MATLAB.
Understanding the Issue
The issue at hand is the difference in behavior between the C++ and MATLAB versions of the code when constructing pure quaternions or pure dual quaternions using double values. The C++ version does not return the expected pure quaternions or pure dual quaternions, whereas the MATLAB version does.
Matlab Behavior
Matlab Script
DQ([1., 2., 3.])
Matlab Output
ans =
1i + 2j + 3k
As we can see from the MATLAB output, the DQ
constructor in MATLAB returns a pure quaternion when given a vector of three double values.
C++ Behavior
C++ Code
#include <iostream>
#include <dqrobotics/DQ.h>
using namespace std;
int main()
{
DQ_robotics::DQ should_be_pure = DQ_robotics::DQ(1., 2., 3.);
cout << "should_be_pure = " << should_be_pure << endl;
return 0;
}
CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 3.1)
include(${CMAKE_CURRENT_SOURCE_DIR}/../dqrobotics_dependencies.cmake)
project(simple_class_tests)
set (CMAKE_CXX_STANDARD 11)
FIND_PACKAGE(Eigen3 REQUIRED)
INCLUDE_DIRECTORIES(${EIGEN3_INCLUDE_DIR})
ADD_COMPILE_OPTIONS(-Werror=return-type -Wall -Wextra -Wmissing-declarations -Wredundant-decls -Woverloaded-virtual)
# DQ Dynamics library is installed here when using the regular
# cmake ., make, sudo make install
LINK_DIRECTORIES(
/usr/local/lib/)
add_executable(test_dq_pure
test_dq_pure.cpp
)
TARGET_LINK_LIBRARIES(test_dq_pure
dqrobotics
)
C++ Output
p = 1 + 2i + 3j
As we can see from the C++ output, the DQ
constructor in C++ returns a quaternion when given a vector of three double values, but it is not a pure quaternion.
Environment
- OS: Ubuntu 22.04.5 LTS
- dqrobotics version: development
- MATLAB version: R2023b
Additional Context
One possible solution would be to overload the constructor to behave similarly to the one in MATLAB, which throws an error when given a vector with the wrong number of elements.
explicit DQ(const double& q0=0.0,
const double& q1=0.0,
const double& q2=0.0,
const double& q3=0.0,
const double& q4=0.0,
const double& q5=0.0,
const double& q6=0.0,
const double& q7=0.0) noexcept;
This constructor would throw an error when given a vector with the wrong number of elements, similar to the MATLAB version.
Possible Solutions
- Overload the Constructor: Overload the
DQ
constructor to behave similarly to the one in MATLAB, which throws an error when given a vector with the wrong number of elements. - Add a Check: Add a check in the
DQ
constructor to ensure that the given vector has the correct number of elements before constructing the pure quaternion or pure dual quaternion. - Modify the C++ Code: Modify the C++ code to return the expected pure quaternion or pure dual quaternion when given a vector of double values.
Conclusion
Q&A: Understanding the Issue and Possible Solutions
Q: What is the issue with constructing pure quaternions or pure dual quaternions using double values in C++?
A: The issue is that the C++ version of the code does not return pure quaternions or pure dual quaternions when given a vector of double values, whereas the MATLAB version does.
Q: Why is this a problem?
A: This is a problem because it can lead to unexpected behavior and errors in the code. For example, if the code is expecting a pure quaternion or pure dual quaternion, but instead gets a quaternion or dual quaternion, it can cause the code to fail or produce incorrect results.
Q: What are the possible solutions to this issue?
A: There are several possible solutions to this issue:
- Overload the Constructor: Overload the
DQ
constructor to behave similarly to the one in MATLAB, which throws an error when given a vector with the wrong number of elements. - Add a Check: Add a check in the
DQ
constructor to ensure that the given vector has the correct number of elements before constructing the pure quaternion or pure dual quaternion. - Modify the C++ Code: Modify the C++ code to return the expected pure quaternion or pure dual quaternion when given a vector of double values.
Q: How can I overload the constructor to fix this issue?
A: To overload the constructor, you can add a new constructor to the DQ
class that takes a vector of double values as an argument. This constructor should check that the vector has the correct number of elements before constructing the pure quaternion or pure dual quaternion. If the vector has the wrong number of elements, the constructor should throw an error.
Q: What is the correct number of elements for a pure quaternion or pure dual quaternion?
A: The correct number of elements for a pure quaternion or pure dual quaternion is 3 or 6, respectively.
Q: How can I add a check to ensure that the vector has the correct number of elements?
A: You can add a check by using the size()
function to get the number of elements in the vector, and then comparing it to the expected number of elements. If the number of elements is not correct, you can throw an error.
Q: How can I modify the C++ code to return the expected pure quaternion or pure dual quaternion?
A: To modify the C++ code, you can change the DQ
constructor to return the expected pure quaternion or pure dual quaternion when given a vector of double values. This may involve modifying the internal representation of the DQ
class to store the pure quaternion or pure dual quaternion correctly.
Q: What are the benefits of using a pure quaternion or pure dual quaternion?
A: The benefits of using a pure quaternion or pure dual quaternion include:
- Improved accuracy: Pure quaternions and dual quaternions can provide more representations of rotations and transformations.
- Simplified code: Using pure quaternions and dual quaternions can simplify the code and reduce the risk of errors.
- Improved performance: Pure quaternions and dual quaternions can provide faster and more efficient computations.
Q: How can I get started with using pure quaternions and dual quaternions in my code?
A: To get started with using pure quaternions and dual quaternions, you can:
- Read the documentation: Read the documentation for the
DQ
class and the relevant libraries to understand how to use pure quaternions and dual quaternions. - Experiment with examples: Experiment with examples and test cases to see how pure quaternions and dual quaternions work in practice.
- Consult with experts: Consult with experts and experienced developers who have used pure quaternions and dual quaternions in their code.