从Python调用MKL:DSTEVR

时间:2016-08-29 00:45:16

标签: python scipy eigenvalue intel-mkl

dstevr 计算三角对称矩阵的本征解。凉。除了它不是用SCIPY的包装器移植的例程之一。所以我按照如何直接从Python调用MKL的说明,附件似乎给出了正确的答案。但天哪....有没有办法清理它?!

  import numpy as np 
  from scipy import linalg
  from ctypes import * 

  c_double_p = POINTER(c_double)
  c_int_p = POINTER(c_int)
  c_char_p = POINTER(c_char)

  mkl = CDLL('mkl_rt.dll')
  dstevr = mkl.dstevr

  #SUBROUTINE DSTEVR( JOBZ, RANGE, N, D, E, VL, VU, IL, IU, ABSTOL, M, 
  # *      W, Z, LDZ, ISUPPZ, WORK, LWORK, IWORK, LIWORK, INFO)
  #  CHARACTER * 1 JOBZ, RANGE
  #  INTEGER N, IL, IU, M, LDZ, LWORK, LIWORK, INFO
  #  INTEGER ISUPPZ(*), IWORK(*)
  #  DOUBLE PRECISION VL, VU, ABSTOL
  #  DOUBLE PRECISION D(*), E(*), W(*), Z(LDZ,*), WORK(*)

  dstevr.argtypes = [ c_char_p, c_char_p, c_int_p, c_double_p, c_double_p,  c_double_p, c_double_p, c_int_p, c_int_p, c_double_p,  \
 c_int_p, c_double_p, c_double_p, c_int_p, c_int_p, c_double_p, c_int_p, c_int_p, c_int_p, c_int_p]


  sv = "v"
  eig_j = c_char_p(c_char(sv[0]))
  sr = "a"
  eig_r = c_char_p(c_char(sr[0]))

  vl = c_double(0.0)
  vu = c_double(0.0)
  il = c_int(0)
  iu = c_int(0)
  abstol = c_double(0.0)
  cm = c_int(0)
  eig_info = c_int(0)

  N = 6
  cn = c_int(N)
  ldz = cn 

  lwork = c_int(20*N)
  liwork = c_int(10*N)

  diag = np.ascontiguousarray(np.ones(N)*2)
  diag_p = diag.ctypes.data_as(c_double_p)

  offdiag = np.ascontiguousarray(np.ones(N-1)*(-1))
  offdiag_p = offdiag.ctypes.data_as(c_double_p)

  isuppz = np.ascontiguousarray(np.ones(N*2),dtype=int)
  isuppz_p = isuppz.ctypes.data_as(c_int_p)

  eigw = np.ascontiguousarray(np.zeros(N))
  eigw_p = eigw.ctypes.data_as(c_double_p)

  workz = np.ascontiguousarray(np.ones(20*N))
  workz_p = workz.ctypes.data_as(c_double_p)

  iworkz = np.ascontiguousarray(np.ones(10*N),dtype=int)
  iworkz_p = iworkz.ctypes.data_as(c_int_p)

  eigz = np.ascontiguousarray(np.ones(N*N))
  eigz_p = eigz.ctypes.data_as(c_double_p)


  dstevr( eig_j, eig_r, byref(cn), diag_p, offdiag_p, byref(vl),byref(vu),byref(il),byref(iu),byref(abstol),byref(cm),\
  eigw_p, eigz_p, byref(ldz), isuppz_p, workz_p, byref(lwork), iworkz_p, byref(liwork), byref(eig_info)) 

  print "Eig_Info", eig_info

  print eigz

  A = np.eye(N,N,k=-1)*(-1) + np.eye(N,N)*2 + np.eye(N,N,k=1)*(-1)

  w,v= linalg.eigh(A)

  print v.T

谢谢,

1 个答案:

答案 0 :(得分:0)

您可以在cython_lapack中使用cython包装器,http://docs.scipy.org/doc/scipy/reference/linalg.cython_lapack.html

你需要cython。