设 $A$ 是一个 $m imes n$ 矩阵,不一定是对称的,$D$ 是一个对角线 $n imes n$ 矩阵。在我的问题中,$m < n$ but both $m$ and $n$ have the potential to be quite large, so the computation of $ADA^T$ can take a few seconds as the dimensions get really high. The program I'm writing will carry out this computation numerous times for different variations of $A$ and $D$, so I want to find a way to speed it up, as that could save minutes over the course of the script.
目前,我只是在做
tcrossprod(A %*% D, A)
。但我觉得应该有一种更快的方法来计算它,因为 $D$ 是对角矩阵。在代数或计算方面,有人在这里看到任何好的捷径吗?
只是为了枚举底层C代码所采用的路径
tcrossprod
...
A
,隐式类matrix
:如果
D
是非负的,那么你可以得到BLAS例程DSYRK
,它利用了对称性,具有:
tcrossprod(A * rep(sqrt(diag(D, names = FALSE)), each = nrow(A)))
否则,你将陷入 BLAS 例程
DGEMM
:
tcrossprod(A * rep(diag(D, names = FALSE), each = nrow(A)), A)
如果您知道 A
和
D
是有限的,不包含 IEEE 特殊值 Inf
或 NaN
(包括 R 的 NA_real_
),您可以使用 external BLAS进行实验。如果 R 检测到任一矩阵因子是非有限的,R 将不会使用外部 BLAS。
A
,正式类dgCMatrix
:如果
D
是非负的,那么你可以得到CHOLMOD例程cholmod_aat
,它利用了对称性,具有:
A@x <- A@x * rep.int(sqrt(diag(D, names = FALSE)), A@p[-1L] - A@p[-ncol(A)])
tcrossprod(A)
否则,您将陷入 CHOLMOD 例程
cholmod_transpose
和cholmod_ssmult
:
AD <- A
AD@x <- A@x * rep.int(diag(D, names = FALSE), A@p[-1L] - A@p[-ncol(A)])
tcrossprod(AD, A)
在任何情况下,因为
D
是对角线,你应该只需要一个矩阵乘法。