【问题标题】:Testing symmetry on symmetric matrix fails在对称矩阵上测试对称性失败
【发布时间】:2011-10-05 14:54:22
【问题描述】:

我在内存中有一个对称矩阵 (4x4)。不幸的是, sum( originalMatrix != t( originalMatrix ) ) = 6。所以我输入了那个矩阵并将其读回内存。现在 sum( dputMatrix!= t( dputMatrix) ) = 0。这怎么可能?如何测试内存中矩阵的对称性?

originalMatrix = ...
sum( originalMatrix != t( originalMatrix ) )  # results in 6
dput( originalMatrix )
dputMatrix = structure(c(0.00117771346463494, -1.57864028664711e-05, 0.000293652924987303, 
-9.85387333663002e-05, -1.57864028664711e-05, 0.000194782042576016, 
6.9133672862693e-05, 4.23792732612071e-05, 0.000293652924987303, 
6.9133672862693e-05, 0.00046216043028767, 2.70172523991749e-05, 
-9.85387333663002e-05, 4.23792732612071e-05, 2.70172523991749e-05, 
0.00017694896679169), .Dim = c(4L, 4L), .Dimnames = list(c("a00088630", 
"a0091n", "a01010", "a01307810"), c("a00088630", "a0091n", "a01010", 
"a01307810")))
sum( dputMatrix != t( dputMatrix ) )  # results in 0!!!

【问题讨论】:

  • 我发现 all.equal 对于 originalMatrix 和 dputMatrix 测试都返回 TRUE
  • 看起来 R FAQ 7.31 适用。 cran.r-project.org/doc/FAQ/R-FAQ.html
  • 我知道这个问题,但无法理解为什么 dput 版本有效。感谢您指向常见问题解答!我认为这句话解释了它:“......除非它们是由相同的算法计算的”。 dput 版本让 R 有机会以我猜想的方式将数字读入内存。
  • 是的,dput 只写入低精度。使用 save() 准确地写出 R 对象。
  • 谢谢!您能否发布您的两个 cmets 作为答案,以便我接受?

标签: r


【解决方案1】:

这是FAQ 7.31的体现。请注意,如果要将 R 对象保存为全精度,则应使用 save()。

请打勾!

【讨论】:

  • 有没有办法以高精度转储 R 对象,例如无损翻译到另一个程序?
  • writeBin 可以使用内部二进制表示来保存浮点数据。您可以将其读回到 C 程序中,但可能存在字节序问题。
猜你喜欢
  • 2016-10-05
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多