zheev给出错误的特征值(检查zgeev和numpy.linalg.eig)

时间:2017-06-03 08:33:53

标签: c++ fortran lapack lapacke

修补linalg库,我尝试使用LAPACKE获得使用c ++运行的Hermitian矩阵的对角化例程

我按照this示例使用ZHEEV,然后检查其他一些方法,特别是numpy的eig和LAPACK(E)的zgeev。我不想使用英特尔自有品牌的东西,所以我避免使用MKL而只是直接使用LAPACKE,但大多数代码与示例中的相同。

为清楚起见,我认为 ge neral z ge ev不应该能够处理他 rmitian矩阵,即使z ev被优化。

这是c ++

#include <stdlib.h>
#include <stdio.h>
#include <lapacke.h>

//Parameters 
#define N 4
#define LDA N
#define lint lapack_int
#define ldcmplex lapack_complex_double

//Auxiliary routines prototypes 
extern void print_matrix( char* desc, lint m, lint n, ldcmplex* a, lint lda         );
extern void print_rmatrix( char* desc, lint m, lint n, double* a, lint lda         );

//Main program 
int main()
  {
    //Locals 
    lint n = N, lda = LDA, info;
  ;
    //Local arrays 
    double wr[N];
    ldcmplex ah[LDA*N] = { 
           { 9.14,  0.00}, { 0.00,  0.00}, { 0.00,  0.00}, { 0.00,  0.00},
           {-4.37,  9.22}, {-3.35,  0.00}, { 0.00,  0.00}, { 0.00,  0.00},
           {-1.98,  1.72}, { 2.25,  9.51}, {-4.82,  0.00}, { 0.00,  0.00},
           {-8.96,  9.50}, { 2.57, -2.40}, {-3.24, -2.04}, { 8.44,  0.00}
    };
  ; 
    //Executable statements 
    printf( "LAPACKE_zheev (row-major, high-level) Example Program                         Results\n" )    ;
  ; 
    //Print martix 
    print_matrix( "Input Matrix", n, n, ah, lda );
  ; 
    //Solve eigenproblem  
    info = LAPACKE_zheev( LAPACK_ROW_MAJOR, 'V', 'L', n, ah, lda, wr );
  ; 
    //Check for convergence 
    if( info > 0 ) {
            printf( "zheev algorithm failed to compute eigenvalues.\n" );
            exit( 1 );
    }
  ; 
    //Print eigenvalues 
    print_rmatrix( "zheev Eigenvalues", 1, n, wr, 1 );
  ; 
    //Print eigenvectors 
    print_matrix( "Eigenvectors (stored columnwise)", n, n, ah, lda );
  ; 
    //Local arrays 
    ldcmplex wc[N];
    ldcmplex ag[LDA*N] = {
      { 9.14,  0.00}, {-4.37, -9.22}, {-1.98, -1.72}, {-8.96, -9.50},
      {-4.37,  9.22}, {-3.35,  0.00}, { 2.25, -9.51}, { 2.57,  2.40},
      {-1.98,  1.72}, { 2.25,  9.51}, {-4.82,  0.00}, {-3.24,  2.04},
      {-8.96,  9.50}, { 2.57, -2.40}, {-3.24, -2.04}, { 8.44,  0.00},
    };
  ; 
    //Executable statements 
    printf( "LAPACKE_zgeev (row-major, high-level) Example Program Results\n" );
  ; 
    //Print martix 
    print_matrix( "Input Matrix", n, n, ag, lda );
  ; 
    //Solve eigenproblem  
    info = LAPACKE_zgeev( LAPACK_ROW_MAJOR, 'N', 'V', n, ag, lda, wc, 0, lda, 0, lda);
  ; 
    //Check for convergence 
    if( info > 0 ) {
            printf( "zgeev algorithm failed to compute eigenvalues.\n" );
            exit( 1 );
    }
  ; 
    //Print eigenvalues 
    print_matrix( "zgeev Eigenvalues", 1, n, wc, 1);
  ;
    //Print eigenvectors 
    print_matrix( "Eigenvectors (stored columnwise)", n, n, ag, lda );
    exit( 0 );
  }

//Auxiliary routine: printing a matrix 
void print_matrix( char* desc, lint m, lint n, ldcmplex* a, lint lda ) {
        lint i, j;
        printf( "\n %s\n", desc );
        for( i = 0; i < m; i++ ) {
                for( j = 0; j < n; j++ )
                        printf( " (%6.2f,%6.2f)", creal(a[i*lda+j]),     cimag(a[i*lda+j]) );
                printf( "\n" );
        }
}

//Auxiliary routine: printing a real matrix 
void print_rmatrix( char* desc, lint m, lint n, double* a, lint lda ) {
        lint i, j;
        printf( "\n %s\n", desc );
        for( i = 0; i < m; i++ ) {
                for( j = 0; j < n; j++ ) printf( " %6.2f", a[i*lda+j] );
                printf( "\n" );
        }
}

编译
g++ diag.cc -L /usr/lib/lapack/ -llapacke -lcblas -o diag.out

唯一通过liblapacke-dev安装的非标准软件包libcblas-devapt-get install。什么可能出错?

输出

LAPACKE_zheev (row-major, high-level) Example Program Results

 Input Matrix
 (  9.14,  0.00) (  0.00,  0.00) (  0.00,  0.00) (  0.00,  0.00)
 ( -4.37,  9.22) ( -3.35,  0.00) (  0.00,  0.00) (  0.00,  0.00)
 ( -1.98,  1.72) (  2.25,  9.51) ( -4.82,  0.00) (  0.00,  0.00)
 ( -8.96,  9.50) (  2.57, -2.40) ( -3.24, -2.04) (  8.44,  0.00)

 zheev Eigenvalues
 -18.96 -12.85  18.78  30.71

 Eigenvectors (stored columnwise)
 (  0.16,  0.00) (  0.57,  0.00) ( -0.73,  0.00) (  0.35,  0.00)
 (  0.26, -0.81) (  0.17, -0.25) (  0.22, -0.38) (  0.06, -0.02)
 (  0.29,  0.27) ( -0.11, -0.30) ( -0.26, -0.42) ( -0.50, -0.50)
 ( -0.21,  0.23) (  0.50, -0.49) (  0.18, -0.09) ( -0.33,  0.51)

LAPACKE_zgeev (row-major, high-level) Example Program Results

 Input Matrix
 (  9.14,  0.00) ( -4.37, -9.22) ( -1.98, -1.72) ( -8.96, -9.50)
 ( -4.37,  9.22) ( -3.35,  0.00) (  2.25, -9.51) (  2.57,  2.40)
 ( -1.98,  1.72) (  2.25,  9.51) ( -4.82,  0.00) ( -3.24,  2.04)
 ( -8.96,  9.50) (  2.57, -2.40) ( -3.24, -2.04) (  8.44,  0.00)

 zgeev Eigenvalues
 ( 25.51,  0.00) (-16.00, -0.00) ( -6.76,  0.00) (  6.67,  0.00)

 Eigenvectors (stored columnwise)
 ( 25.51,  0.00) ( -0.00,  0.00) (  0.00,  0.00) (  0.00, -0.00)
 (  0.00,  0.00) (-16.00, -0.00) (  0.00, -0.00) (  0.00, -0.00)
 (  0.00,  0.00) (  0.00,  0.00) ( -6.76,  0.00) ( -0.00, -0.00)
 (  0.00,  0.00) (  0.00,  0.00) (  0.00,  0.00) (  6.67,  0.00)

我尝试使用上三角形,填充矩阵和其他各种修正。每次都有相同的结果。

我怀疑#define ldcmplex lapack_complex_double宏,但我能找到的所有documentation都说我应该使用双重复合体,所以我有点迷失了。无论如何,如果那是问题,为什么zgeev会工作?

无论如何,这是python检查脚本:

#!/usr/bin/env python
from numpy import linalg as li
import numpy as np

mat=np.array([
[   9.14 + 0.00j,  0.00 + 0.00j,  0.00 + 0.00j,  0.00 +0.00j],
[  -4.37 + 9.22j, -3.35 + 0.00j,  0.00 + 0.00j,  0.00 +0.00j],
[  -1.98 + 1.72j,  2.25 + 9.51j, -4.82 + 0.00j,  0.00 +0.00j],
[  -8.96 + 9.50j,  2.57 - 2.40j, -3.24 - 2.04j,  8.44 +0.00j]])
mat[0]=np.conj(mat[:,0])
mat[1]=np.conj(mat[:,1])
mat[2]=np.conj(mat[:,2])
mat[3]=np.conj(mat[:,3])

mat=np.matrix(mat)

w, v = li.eig(mat)

print w
print v

它同意zgeev(最多一些舍入/机器错误)。上面链接的intel教程也证实了结果。 zheev方法显然属于少数,我只是不知道为什么。

我在几台机器上试过这个:

Linux parabox 4.8.0-52-generic #55-Ubuntu SMP Fri Apr 28 13:28:50 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
Linux glass 4.10.0-21-generic #23-Ubuntu SMP Fri Apr 28 16:14:22 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

任何和所有帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

这是我运行你的python脚本时得到的结果:

$ ./diag.py
[ 25.51400517 +1.20330583e-15j -16.00474647 -2.91871119e-15j
  -6.76497015 -6.59630730e-16j   6.66571145 +1.54590036e-16j]
[[ 0.69747489+0.j          0.21857873+0.26662122j  0.47736933+0.26449375j     -0.02829581-0.30988089j]
 [-0.21578745+0.28003172j  0.69688890+0.j         -0.14143627-0.2852389j       0.24437193-0.47778739j]
 [-0.14607303-0.08302697j -0.01445974-0.60818924j 0.44678181+0.26546077j       0.57583205+0.j        ]
 [-0.34133591+0.49376693j  0.15930699-0.00061647j  0.57507627+0.j             -0.45823952+0.2713093j ]]

我不知道应该匹配什么。特征值匹配,但不匹配特征向量。

答案 1 :(得分:0)

在编译行中用-cblas替换-blas解决了这个问题。

cblas包必须有错误。