四元数的错误乘法

Wrong multiplication of quaternions

我实现了四元数并使用了 boost 来测试它。我循环测试它,我总是在乘法和除法中出错。然后我用Wolfram找出谁错了,我发现是boost错了。

我不确定boost库是Wong,也许有人能知道是怎么回事?

控制台输出示例:

0
Operation : [ * ]
First vector =  [ -41.4168   -92.2373     -33.0126   -42.2364 ]
Second vector = [ -67.8087   -60.3523     58.8705   36.3265 ]
My multiplication =    [ 719.45   10041.3   5700.03   -6062.97 ]
Boost multiplication = [ 719.45   10041.3   10041.3   5700.03 ]

main.cpp

#include "quaternion.h"
#include <boost/math/quaternion.hpp>
#include <random>
#include <time.h>
#include <QDebug>

using boost::math::quaternion;

double fRand(double fMin, double fMax)
{
    double f = (double)rand() / RAND_MAX;
    return fMin + f * (fMax - fMin);
}

bool isEqual(double a, double b){
    return std::abs(a-b) < std::numeric_limits<double>::epsilon();
}

bool isEqual(Quaternion& quat1, quaternion<double> quat2){
    if (!isEqual(quat2.R_component_1(), quat1.real)) return false;
    if (!isEqual(quat2.R_component_2(), quat1.imagine.data.at(0))) return false;
    if (!isEqual(quat2.R_component_3(), quat1.imagine.data.at(1))) return false;
    if (!isEqual(quat2.R_component_4(), quat1.imagine.data.at(2))) return false;
    return true;
}

int main () {

    std::srand(std::time(nullptr));
    Quaternion compR;
    quaternion<double> boost;

    double a1, a2, a3, a4,
            b1, b2, b3, b4;
try {
    for (unsigned int i=0; i<10000000; ++i){
        a1 = fRand(-100, 100);
        a2 = fRand(-100, 100);
        a3 = fRand(-100, 100);
        a4 = fRand(-100, 100);

        b1 = fRand(-100, 100);
        b2 = fRand(-100, 100);
        b3 = fRand(-100, 100);
        b4 = fRand(-100, 100);

        Quaternion comp1{a1,a2,a3,a4};
        Quaternion comp2{b1,b2,b3,b4};
        quaternion<double> a(a1,a2,a3,a4);
        quaternion<double> b(b1,b2,b3,b4);


        //if (i%50000==0)
            qDebug() << i;

        compR = comp1+comp2;
        boost = a+b;
        if (!isEqual(compR, boost))
            throw std::runtime_error("+");

        compR = comp1-comp2;
        boost = a-b;
        if (!isEqual(compR, boost))
            throw std::runtime_error("-");

        compR = comp1*comp2;
        boost = a*b;
        if (!isEqual(compR, boost))
            throw std::runtime_error("*");

        compR = comp1/comp2;
        boost = a/b;
        if (!isEqual(compR, boost))
            throw std::runtime_error("/");

    }
    } catch (const std::runtime_error& error) {
        qDebug() << "Operation : [" << error.what() << "]";
        qDebug() << " First vector =  [" << a1 << " " << a2 << " " << " " << a3 << " " << a4 << "]\n" <<
                    "Second vector = [" << b1 << " " << b2 << " " << " " << b3 << " " << b4 << "]";
        qDebug() << " My multiplication =    [" << compR.real << " " << compR.imagine.data.at(0) << " " << compR.imagine.data.at(1)<< " " << compR.imagine.data.at(2) << "]\n" <<
                    "Boost multiplication = [" << boost.R_component_1() << " " << boost.R_component_2() << " " << boost.R_component_2() << " " << boost.R_component_3() << "]";
    }

    return 0;
}

完整项目:https://github.com/Yegres5/quaternion

您有两个问题:

  • 你在打印 boost 四元数时遇到了错误(你打印了两次索引 2)
  • 使用更大的 epsilon。如果您的数字在 1 左右,则按原样使用 epsilon 是合适的。您的结果约为 10,000,因此您需要使用至少 10,000 倍大的 epsilon。这仍然可能不够大,因为四元数乘法使用多个运算,每个运算都会增加一些额外的误差。因此,为了安全起见,将 epsilon 进一步乘以 ~10 左右。