c++ - Armadillo 保存 double ascii

标签 c++ armadillo

我想用 armadillo 库以 ascii 格式保存一些复杂的 double 矩阵,但不知何故只保存了 6 位有效数字......我知道可以通过以下方式克服这种截断(或舍入,尚未检查)使用二进制格式,但我想使用 ascii 以便我可以手动验证和修改一些值。 在下面提供的 mwe 中,我希望矩阵 A 和 C 对于大约 16 位有效数字是相同的。是否有一个 secret 开关告诉 Armadillo 以更高的精度以ascii格式保存复杂矩阵?谢谢

#include <iostream>
#include <armadillo>

using namespace std;
using namespace arma;

int main()
{
cout.precision(20);
cx_mat A = randu<cx_mat>(2,2);
A(0,0) = 1.2345678901234567890;
A.raw_print(cout);
A.save("A.arma_ascii", arma_ascii);
cout << endl;
cx_mat C;
C.load("A.arma_ascii", arma_ascii);
C.raw_print(cout);
return 0;
}

编辑 这个问题只发生在复杂的矩阵上。对于真实的 Armadillo 在 diskio_meat.hpp 中设置精度。似乎解决此限制的唯一方法是修补 Armadillo 或单独保存复数和虚数部分。

EDIT 2 问题在 armadillo 库 ( git commit ) 中得到解决,在此处发布我的问题仅 2 小时后。干得好!

最佳答案

我认为问题在于 cx_mat 的基础数据类型是complex<double>因此您不能期望超过 15-17 位有效小数位精度,请参阅 https://en.wikipedia.org/wiki/Double-precision_floating-point_format . 在 Armadillo 代码中,他们使用 std::ofstream作为输出,默认显示 6 位数字,就像您在 A.arma_ascii 文件中一样。

关于c++ - Armadillo 保存 double ascii,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42809590/

相关文章:

c++ - push_back/append 或在 C++ Armadillo 中附加带有循环的 vector

c# - 获取错误 CS0031 : Constant value `65535' cannot be converted to a `short'

c++ - 在 gdb 上漂亮地打印 boost::unordered_map

c++ - 如何测试自反、对称或传递

linux - 如何检查是否已经安装了 BLAS 和 ATLAS

c++ - Armadillo C++ : How to modify multiple array elements of a matrix using multiple elements from another matrix, 专门在立方体结构中?

c++ - 遍历 AST 树的特定部分

c++ - 快速搜索以查找事件范围

c++ - 未定义对 Armadillo 的 LAPACK 包装器的引用

c++ - 进程数量增加导致 MPI 性能损失