R 当对数函数应用于同一个正则矩阵和稀疏矩阵时,为什么结果不同?

R 当对数函数应用于同一个正则矩阵和稀疏矩阵时,为什么结果不同?,r,matrix,sparse-matrix,R,Matrix,Sparse Matrix,当我试图将log函数应用于相同的正则和稀疏矩阵时,得到的结果是不同的。在应用这些函数时,有什么我应该记住的吗?下面是一个可复制的示例 TestMatrix = matrix(c(3,1,0,0,0,4,0,1,0,0,2,1,1,2,0,6,1,0,1,0,1,0,0,0,0),5,byrow = TRUE) TestSparseMatrix = Matrix(TestMatrix,sparse = TRUE) # Logarithmic function when applied to reg

当我试图将
log
函数应用于相同的正则和稀疏矩阵时,得到的结果是不同的。在应用这些函数时,有什么我应该记住的吗?下面是一个可复制的示例

TestMatrix = matrix(c(3,1,0,0,0,4,0,1,0,0,2,1,1,2,0,6,1,0,1,0,1,0,0,0,0),5,byrow = TRUE)
TestSparseMatrix = Matrix(TestMatrix,sparse = TRUE)
# Logarithmic function when applied to regular matrix
 -log(TestMatrix / rowSums(TestMatrix), 2)


#Output
#          [,1]     [,2]     [,3]     [,4] [,5]
#[1,] 0.4150375 2.000000      Inf      Inf  Inf
#[2,] 0.3219281      Inf 2.321928      Inf  Inf
#[3,] 1.5849625 2.584963 2.584963 1.584963  Inf
#[4,] 0.4150375 3.000000      Inf 3.000000  Inf
#[5,] 0.0000000      Inf      Inf      Inf  Inf


# Logarithmic function when applied to Sparse matrix
-log(TestSparseMatrix / rowSums(TestSparseMatrix), 2)


# Output
#          [,1]     [,2]     [,3]     [,4] [,5]
#[1,] 0.2876821 1.386294      Inf      Inf  Inf
#[2,] 0.2231436      Inf 1.609438      Inf  Inf
#[3,] 1.0986123 1.791759 1.791759 1.098612  Inf
#[4,] 0.2876821 2.079442      Inf 2.079442  Inf
#[5,] 0.0000000      Inf      Inf      Inf  Inf
log()
正在忽略稀疏矩阵(一个“S4”对象)的
base
。使用
log2
可以消除以下问题:

-log2(TestSparseMatrix / rowSums(TestSparseMatrix))
阅读
日志的“S4方法”部分。我相信这是不为人知的

Note that this means that the S4 generic for ‘log’ has a signature
with only one argument, ‘x’, but that ‘base’ can be passed to
methods (but will not be used for method selection).  On the other
hand, if you only set a method for the ‘Math’ group generic then
‘base’ argument of ‘log’ will be ignored for your class.
如果您想知道,您可以进一步阅读:

?groupGeneric
?S4groupGeneric
或同等方式(您将被重定向到上述手册页面):

这实际上与“数学”组的定义有关。特别引用自
?S4groupGeneric

Note that two members of the ‘Math’ group, ‘log’ and ‘trunc’, have
... as an extra formal argument.  Since methods for ‘Math’ will
have only one formal argument, you must set a specific method for
these functions in order to call them with the extra argument(s).

所以,如果你想用任意有效的
base
取对数,比如
base=3
?使用公式:

log(x, base = 3) = log(x) / log(3)
例如,使用
TestSparseMatrix
,您还可以执行以下操作:

-log(TestSparseMatrix / rowSums(TestSparseMatrix)) / log(2)
log()
正在忽略稀疏矩阵(一个“S4”对象)的
base
。使用
log2
可以消除以下问题:

-log2(TestSparseMatrix / rowSums(TestSparseMatrix))
阅读
日志的“S4方法”部分。我相信这是不为人知的

Note that this means that the S4 generic for ‘log’ has a signature
with only one argument, ‘x’, but that ‘base’ can be passed to
methods (but will not be used for method selection).  On the other
hand, if you only set a method for the ‘Math’ group generic then
‘base’ argument of ‘log’ will be ignored for your class.
如果您想知道,您可以进一步阅读:

?groupGeneric
?S4groupGeneric
或同等方式(您将被重定向到上述手册页面):

这实际上与“数学”组的定义有关。特别引用自
?S4groupGeneric

Note that two members of the ‘Math’ group, ‘log’ and ‘trunc’, have
... as an extra formal argument.  Since methods for ‘Math’ will
have only one formal argument, you must set a specific method for
these functions in order to call them with the extra argument(s).

所以,如果你想用任意有效的
base
取对数,比如
base=3
?使用公式:

log(x, base = 3) = log(x) / log(3)
例如,使用
TestSparseMatrix
,您还可以执行以下操作:

-log(TestSparseMatrix / rowSums(TestSparseMatrix)) / log(2)