skcuda.linalg.eig

skcuda.linalg.eig(a_gpu, jobvl='N', jobvr='V', imag='F', lib='cusolver')[source]

Eigendecomposition of a matrix.

Compute the eigenvalues w for a real/complex square matrix a and (optionally) the real left and right eigenvectors vl, vr.

Parameters:
  • a_gpu (pycuda.gpuarray.GPUArray) – Real/complex input matrix a with dimensions (m, n).
  • jobvl ({'V', 'N'}) – ‘V’ : returns vl, the left eigenvectors of a with dimensions (m, m). ‘N’ : left eigenvectors are not computed.
  • jobvr ({'V', 'N'}) – ‘V’ : returns vr, the right eigenvectors of a with dimensions (m, m), (default). ‘N’ : right eigenvectors are not computed.
  • imag ({'F', 'T'}) – ‘F’ : imaginary parts of a real matrix are not returned (default). ‘T’ : returns the imaginary parts of a real matrix (only relevant in the case of single/double precision ).
  • lib (str) – Library to use. May be either ‘cula’ or ‘cusolver’. If using ‘cusolver’, only symmetric/Hermitian matrices are supported.
Returns:

  • vr_gpu (pycuda.gpuarray.GPUArray) – The normalized (Euclidean norm equal to 1) right eigenvectors, such that the column vr[:,i] is the eigenvector corresponding to the eigenvalue w[i].
  • w_gpu (pycuda.gpuarray.GPUArray) – Array containing the real/complex eigenvalues, not necessarily ordered. w is of length m.
  • vl_gpu (pycuda.gpuarray.GPUArray) – The normalized (Euclidean norm equal to 1) left eigenvectors, such that the column vl[:,i] is the eigenvector corresponding to the eigenvalue w[i].

Notes

Double precision is only supported if the standard version of the CULA Dense toolkit is installed.

This function destroys the contents of the input matrix.

Arrays are expected to be stored in column-major order, i.e., order=’F’.

Examples

>>> import pycuda.gpuarray as gpuarray
>>> import pycuda.autoinit
>>> import numpy as np
>>> from skcuda import linalg
>>> linalg.init()
>>> # Compute right eigenvectors of a symmetric matrix A and verify A*vr = vr*w
>>> a = np.array(([1,3],[3,5]), np.float32, order='F')
>>> a_gpu = gpuarray.to_gpu(a)
>>> vr_gpu, w_gpu = linalg.eig(a_gpu, 'N', 'V')
>>> np.allclose(np.dot(a, vr_gpu.get()), np.dot(vr_gpu.get(), np.diag(w_gpu.get())), 1e-4)
True
>>> # Compute left eigenvectors of a symmetric matrix A and verify vl.T*A = w*vl.T
>>> a = np.array(([1,3],[3,5]), np.float32, order='F')
>>> a_gpu = gpuarray.to_gpu(a)
>>> w_gpu, vl_gpu = linalg.eig(a_gpu, 'V', 'N')
>>> np.allclose(np.dot(vl_gpu.get().T, a), np.dot(np.diag(w_gpu.get()), vl_gpu.get().T), 1e-4)
True
>>> # Compute left/right eigenvectors of a symmetric matrix A and verify A = vr*w*vl.T
>>> a = np.array(([1,3],[3,5]), np.float32, order='F')
>>> a_gpu = gpuarray.to_gpu(a)
>>> vr_gpu, w_gpu, vl_gpu = linalg.eig(a_gpu, 'V', 'V')
>>> np.allclose(a, np.dot(vr_gpu.get(), np.dot(np.diag(w_gpu.get()), vl_gpu.get().T)), 1e-4)
True
>>> # Compute eigenvalues of a square matrix A and verify that trace(A)=sum(w)
>>> a = np.array(np.random.rand(9,9), np.float32, order='F')
>>> a_gpu = gpuarray.to_gpu(a)
>>> w_gpu = linalg.eig(a_gpu, 'N', 'N')
>>> np.allclose(np.trace(a), sum(w_gpu.get()), 1e-4)
True
>>> # Compute eigenvalues of a real valued matrix A possessing complex e-valuesand
>>> a = np.array(np.array(([1, -2], [1, 3])), np.float32, order='F')
>>> a_gpu = gpuarray.to_gpu(a)
>>> w_gpu = linalg.eig(a_gpu, 'N', 'N', imag='T')
True
>>> # Compute eigenvalues of a complex valued matrix A and verify that trace(A)=sum(w)
>>> a = np.array(np.random.rand(2,2) + 1j*np.random.rand(2,2), np.complex64, order='F')
>>> a_gpu = gpuarray.to_gpu(a)
>>> w_gpu = linalg.eig(a_gpu, 'N', 'N')
>>> np.allclose(np.trace(a), sum(w_gpu.get()), 1e-4)
True