Leading dimension
Leading dimension
如果你用LAPACK解过矩阵本征值问题,你一定会接触到这样一个名词,“leading dimension”,比如在函数zheev中。我想绝大部分人在第一次接触这个词的时候都不明白它到底是什么意思。以前我也不明白,今天索性搜了一把,在下面找到了答案。
http://icl.cs.utk.edu/lapack-forum/viewtopic.php?p=661&sid=67c66465dedfcbb6e0612cca7647698f
Suppose that you have a matrix A of size 100x100 which is stored in an array 100x100. In this case LDA is the same as N. Now suppose that you want to work only on the submatrix A(91:100 , 1:100); in this case the number of rows is 10 but LDA=100. Assuming the fortran column-major ordering (which is the case in LAPACK), the LDA is used to define the distance in memory between elements of two consecutive columns which have the same row index. If you call B = A(91:100 , 1:100) then B(1,1) and B(1,2) are 100 memory locations far from each other.
其实之所以设LDA(leading dimension)这个参数主要是考虑到fortran是“列优先”存储数组的原因。这里要解本征值的矩阵是NxN大小的,但是存储这个矩阵的数组A却并不一定非得是NxN大小,可以是M1xM2大小,其中 M1≥N,M2≥N,NxN矩阵要存放在M1xM2数组的左上角,即A(1:N,1:N)部分。这样,当把数组A传给zheev时,zheev通过N来知道要解的矩阵是多大的,通过LDA来知道同一行中相邻两列的元素在内存中相距多远,可见,LDA=M2,其实就是数组A的一列的元素个数,也就是“实际存储时的第一维”的大小。正是由于fortran列优先存储数组,才使得概念上的第一维(行)与实际存储时的第一维(列)不一样。
注1:如果参数A的位置就用数组片段来调用的话则令当别论。例如同样A的大小为M1xM2,那么若调用zheev时,A参数位置处的实参是A(1:N,1:N)的话,则LDA位置处的实参应该是N,而不是M2!
注2:zheev的输出本征矢时,A的每一列代表一个本征矢。
http://icl.cs.utk.edu/lapack-forum/viewtopic.php?p=661&sid=67c66465dedfcbb6e0612cca7647698f
Suppose that you have a matrix A of size 100x100 which is stored in an array 100x100. In this case LDA is the same as N. Now suppose that you want to work only on the submatrix A(91:100 , 1:100); in this case the number of rows is 10 but LDA=100. Assuming the fortran column-major ordering (which is the case in LAPACK), the LDA is used to define the distance in memory between elements of two consecutive columns which have the same row index. If you call B = A(91:100 , 1:100) then B(1,1) and B(1,2) are 100 memory locations far from each other.
其实之所以设LDA(leading dimension)这个参数主要是考虑到fortran是“列优先”存储数组的原因。这里要解本征值的矩阵是NxN大小的,但是存储这个矩阵的数组A却并不一定非得是NxN大小,可以是M1xM2大小,其中 M1≥N,M2≥N,NxN矩阵要存放在M1xM2数组的左上角,即A(1:N,1:N)部分。这样,当把数组A传给zheev时,zheev通过N来知道要解的矩阵是多大的,通过LDA来知道同一行中相邻两列的元素在内存中相距多远,可见,LDA=M2,其实就是数组A的一列的元素个数,也就是“实际存储时的第一维”的大小。正是由于fortran列优先存储数组,才使得概念上的第一维(行)与实际存储时的第一维(列)不一样。
注1:如果参数A的位置就用数组片段来调用的话则令当别论。例如同样A的大小为M1xM2,那么若调用zheev时,A参数位置处的实参是A(1:N,1:N)的话,则LDA位置处的实参应该是N,而不是M2!
注2:zheev的输出本征矢时,A的每一列代表一个本征矢。