使用LAPACK访问子矩阵 [英] Accessing submatrices using LAPACK
问题描述
在LAPACK中有一个函数,它会给我一个特定子矩阵的元素吗?
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++?
或者我需要编写它吗?
推荐答案
没有访问子矩阵的功能。但是,由于矩阵数据存储在LAPACK例程中的方式,您不需要一个。这节省了大量的复制,并且由于这个原因(部分地)选择了数据布局:
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:
- 指向矩阵左上角的指针
- 矩阵中的行数
- 矩阵中的列数
- 矩阵的通常这是内存中相邻元素之间的距离。
- a pointer to the top left corner of the matrix
- the number of rows in the matrix
- the number of columns in the matrix
- the "leading dimension" of the matrix; typically this is the distance in memory between adjacent elements of a row.
大多数时候,大多数人只使用领先维度等于行数; 3×3矩阵通常存储如下:
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:
a[0] a[3] a[6]
a[1] a[4] a[7]
a[2] a[5] a[8]
$ b b
假设我们想要一个3x3的子矩阵,该矩阵的前导维 lda
。假设我们特别想要左上角位于(15,42)的3x3子矩阵:
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):
. . .
. . .
... a[15+42*lda] a[15+43*lda] a[15+44*lda] ...
... a[16+42*lda] a[16+43*lda] a[16+44*lda] ...
... a[17+42*lda] a[17+43*lda] a[17+44*lda] ...
. . .
. . .
我们可以将此3x3矩阵复制到连续的存储空间,将它作为输入(或输出)矩阵传递给LAPACK例程,我们不需要;我们只需要适当地定义参数。让我们把这个子矩阵称为 b
;我们然后定义:
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:
// pointer to the top-left corner of b:
float *b = &a[15 + 42*lda];
// number of rows in b:
const int nb = 3;
// number of columns in b:
const int mb = 3;
// leading dimension of b:
const int ldb = lda;
唯一可能令人惊讶的是 ldb
;通过使用大矩阵的值 lda
,我们可以在不复制的情况下寻址子矩阵,并在其上就地操作。
The only thing that might be surprising is the value of ldb
; by using the value lda
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:
SLACPY(UPLO,M,N,A,LDA,B,LDB)
复制 M
x N
矩阵,其左上角为 A
,并以前导维 LDA
存储到<$ c $其左上角为 B
且具有前导维度的矩阵C> M x N
LDB 。 UPLO
参数指示是否复制上三角形,下三角形或整个矩阵。
copies the M
xN
matrix whose top-left corner is A
and is stored with leading dimension LDA
to the M
xN
matrix whose top-left corner is B
and has leading dimension LDB
. The UPLO
parameter indicates whether to copy the upper triangle, lower triangle, or the whole matrix.
我给上面的,你会使用像这样(假设clapack绑定):
In the example I gave above, you would use it like this (assuming the clapack bindings):
...
const int m = 3;
const int n = 3;
float b[9];
const int ldb = 3;
slacpy("A", // anything except "U" or "L" means "copy everything"
&m, // number of rows to copy
&n, // number of columns to copy
&a[15 + 42*lda], // pointer to top-left element to copy
lda, // leading dimension of a (something huge)
b, // pointer to top-left element of destination
ldb); // leading dimension of b (== m, so storage is dense)
...
这篇关于使用LAPACK访问子矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!