嗨,我正在使用armadillo
用于线性代数和矩阵的库将脚本从 MATLAB 重写为 C++。
为了获得或多或少相同的输出,我调用了 cout 方法:
cout.precision(4);
cout.setf(ios::fixed);
但我得到了不同的结果:
Matlab结果:
0.0000 0.0000 0.0000 0.0000 0.0000
0.0012 0.0014 0.0016 0.0020 0.0281
0.0396 0.0297 0.0297 0.0495 0.0976
犰狳 c++ 结果:
0.0000 0.0000 0.0000 0.0000 0.0000
0.0012 0.0014 0.0016 0.0020 0.0282
0.0416 0.0312 0.0312 0.0520 0.1027
现在,我不知道这些小不精确(0.039
接近0.041
)是否是由我翻译的 C++ 代码中的一些错误引起的,或者它们应该被视为 g++ 和 MATLAB 中双精度之间的正常差异
在我的代码中,我使用了很多这样的循环:
xi_summed = xi_summed + normalise((trans % (alpha.col(t) * b.t())));
其中xi_summed
, trans
, alpha
,b
是arma::mat
并且%
是逐元素乘法 并且mat::t()
是 transpose 和 normalize 是使矩阵A
数组的条目总和为的函数1
。