使用 LAPACK 访问子矩阵
LAPACK 中有一个函数可以给我特定子矩阵的元素吗?如果是的话,C++ 的语法是什么?
或者我需要编码吗?
Is there a function in LAPACK, which will give me the elements of a particular submatrix? If so how what is the syntax in C++?
Or do I need to code it up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有用于访问子矩阵的函数。然而,由于 LAPACK 例程中矩阵数据的存储方式,您不需要这样的例程。这节省了大量的复制工作,并且(部分)选择了数据布局,原因如下:
回想一下,LAPACK 中的密集(即非带状、三角形、厄米特等)矩阵由四个值定义:
大多数时候,大多数人只使用等于行数的前导维度; 3x3 矩阵通常像这样存储:
假设我们想要一个具有前导维度 lda 的巨大矩阵的 3x3 子矩阵。假设我们特别想要左上角位于 a(15,42) 的 3x3 子矩阵:
我们可以将此 3x3 矩阵复制到连续存储中,但如果我们想将其作为输入传递(或输出)矩阵到 LAPACK 例程,我们不需要;我们只需要适当地定义参数即可。我们称这个子矩阵为
b
;然后我们定义:唯一可能令人惊讶的是ldb的值;通过使用“大矩阵”的值
lda
,我们可以在不复制的情况下寻址子矩阵,并就地对其进行操作。但是
我撒了谎(某种程度上)。有时您确实无法就地操作子矩阵,并且确实需要复制它。我不想谈论这一点,因为这种情况很少见,并且您应该尽可能使用就地操作,但如果不告诉您这是可能的,我会感到很难过。例程:
复制左上角为
A
的M
xN
矩阵,并以前导维度LDA 到左上角为
B
并具有前导维度LDB
的M
xN
矩阵。UPLO
参数指示是复制上三角形、下三角形还是整个矩阵。在我上面给出的示例中,您可以像这样使用它(假设使用 clapack 绑定):
There is no function for accessing a submatrix. However, because of the way matrix data is stored in LAPACK routines, you don't need one. This saves a lot of copying, and the data layout was (partially) chosen for this reason:
Recall that a dense (i.e., not banded, triangular, hermitian, etc) matrix in LAPACK is defined by four values:
Most of the time, most people only ever use a leading dimension that is equal to the number of rows; a 3x3 matrix is typically stored like so:
Suppose instead that we wanted a 3x3 submatrix of a huge matrix with leading dimension
lda
. Suppose we specifically want the 3x3 submatrix whose top-left corner is located at a(15,42):We could copy this 3x3 matrix into contiguous storage, but if we want to pass it as an input (or output) matrix to an LAPACK routine, we don't need to; we only need to define the parameters appropriately. Let's call this submatrix
b
; we then define:The only thing that might be surprising is the value of
ldb
; by using the valuelda
of the "big matrix", we can address the submatrix without copying, and operate on it in-place.However
I lied (sort of). Sometimes you really can't operate on a submatrix in place, and genuinely need to copy it. I didn't want to talk about that, because it's rare, and you should use in-place operations whenever possible, but I would feel bad not telling you that it is possible. The routine:
copies the
M
xN
matrix whose top-left corner isA
and is stored with leading dimensionLDA
to theM
xN
matrix whose top-left corner isB
and has leading dimensionLDB
. TheUPLO
parameter indicates whether to copy the upper triangle, lower triangle, or the whole matrix.In the example I gave above, you would use it like this (assuming the clapack bindings):