Matlab和c ++矩阵产品:相同的输入,不同的输出

时间:2013-02-28 17:24:09

标签: c++ matlab double-precision

我在x86计算机上的ubuntu 12.04中使用Matlab 2010,以及g ++ 4.6.3。 这就是我制作和输入的方式:

   #include <Src/Tools/Math/Matrix_nxn.h>
   #include <iostream>
   using namespace std;
   int main()
   {
      Matrix_nxn<double,4> A1,A2,Tb,aa;
      A1[0][0] = 0.99958087959447828;   A1[0][1] = 1.7725781974830023e-18;A1[0][2] = 0.028949354900049871;  A1[0][3] = 0;
      A1[1][0] = -0.028949354900049871; A1[1][1] = 6.1204654815537932e-17;A1[1][2] = 0.99958087959447828;   A1[1][3] = 0;
      A1[2][0] = 0,           A1[2][1] = -1;            A1[2][2] = 6.1230317691118863e-17;A1[2][3] = 0.21129000000000001;
      A1[3][0] = 0,           A1[3][1] = 0;         A1[3][2] = 0;             A1[3][3] = 1;

      A2[0][0] = 0.90634806393366396;   A2[0][1] = -0.42253187690835708;A2[0][2] = 0;A2[0][3] = 0;
      A2[1][0] = 0.42253187690835708;   A2[1][1] = 0.90634806393366396; A2[1][2] = 0;A2[1][3] = 0;
      A2[2][0] = 0;           A2[2][1] = 0;           A2[2][2] = 1;A2[2][3] = 0;
      A2[3][0] = 0;           A2[3][1] = 0;           A2[3][2] = 0;A2[3][3] = 1;

      Tb[0][0] = 0.99956387949834924; Tb[0][1] = -0.00016363183229951183;   Tb[0][2] = -0.029530052943282908; Tb[0][3] = 0;
      Tb[1][0] = 0;         Tb[1][1] = 0.99998464792303143; Tb[1][2] = -0.0055411116439683869;Tb[1][3] = 0;
      Tb[2][0] = 0.029530506297888514;Tb[2][1] = 0.0055386950515785164; Tb[2][2] = 0.99954853411673616;   Tb[2][3] = 0;
      Tb[3][0] = 0;         Tb[3][1] = 0;           Tb[3][2] = 0;             Tb[3][3] = 1;


   aa = Tb*A1*A2;
   cout.precision(25);
   cout <<aa[0][0]<<'   '<<aa[0][1]<<'  '<<aa[0][2]<<'  '<<aa[0][3]<<endl
        <<aa[1][0]<<'   '<<aa[1][1]<<'  '<<aa[1][2]<<'  '<<aa[1][3]<<endl
        <<aa[2][0]<<'   '<<aa[2][1]<<'  '<<aa[2][2]<<'  '<<aa[2][3]<<endl
        <<aa[3][0]<<'   '<<aa[3][1]<<'  '<<aa[3][2]<<'  '<<aa[3][3]<<endl;
}

这是operator*

的定义
Matrix_nxn<T, N> res;
size_t i, j, k;
for (i = 0; i < N; ++i)
{
  for (j = 0; j < N; ++j)
  {
    for (k = 0; k < N; ++k)
    {
      res[i][j] += m1[i][k] * m2[k][j];
    }
    if (MVTools::isNearInf(res[i][j]))
    {
      if (MVTools::isNearPosInf(res[i][j]))
        throw MVException(MVException::PosInfValue);
      else
        throw MVException(MVException::NegInfValue);
    }
  }
}
return res;

奇怪的是我在Matlab中使用相同的值制作相同的矩阵,并得到不同的结果。这是Matlab代码:

Tb = [0.99956387949834924,-0.00016363183229951183,-0.029530052943282908,0;0,0.99998464792303143,-0.0055411116439683869,0;0.029530506297888514,0.0055386950515785164,0.99954853411673616,0;0,0,0,1];
A1 = [0.99958087959447828,1.7725781974830023e-18,0.028949354900049871,0;-0.028949354900049871,6.1204654815537932e-17,0.99958087959447828,0;0,-1,6.1230317691118863e-17,0.21129000000000001;0,0,0,1];
A2 = [0.90634806393366396,-0.42253187690835708,0,0;0.42253187690835708,0.90634806393366396,0,0;0,0,1,0;0,0,0,1];
aa = Tb*A1*A2;
aa - aaa

ans =

   1.0e-16 *

               0  -0.555111512312578                   0                   0
               0                   0                   0                   0
               0                   0                   0                   0
               0                   0                   0                   0

而aaa是c ++实现的输出。 我知道错误很少,但我想知道导致问题的原因!我想调试很多代码,我需要零差异才能进行良好的调试。

2 个答案:

答案 0 :(得分:2)

不同值的原因(无论多么微不足道)是matlab使用的算法与相同。

您的算法很简单O(N^3)矩阵乘法。有效地计算小尺寸矩阵的特殊算法,以及比O(N^3)具有更好渐近性能的复杂算法。

如果您有兴趣,请参阅:

答案 1 :(得分:1)

我看到你期望C ++代码有25位精度。使用double类型的可能性很小。您可以使用long double获得更好的精确度,但可能不会像25位数那样。

请参阅:What is the precision of long double in C++?