是否有任何方法可以从计算的角度简化 $ADA^T$,其中 $D$ 是对角矩阵?

问题描述 投票:0回答:1

设 $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$ 是对角矩阵。在代数或计算方面,有人在这里看到任何好的捷径吗?

r matrix linear-algebra
1个回答
1
投票

只是为了枚举底层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
是对角线,你应该只需要一个矩阵乘法。

© www.soinside.com 2019 - 2024. All rights reserved.