skcuda.linalg.qr

skcuda.linalg.qr(a_gpu, mode='reduced', handle=None, lib='cusolver')[source]

QR Decomposition.

Factor the real/complex matrix a as QR, where Q is an orthonormal/unitary matrix and R is an upper triangular matrix.

Parameters:
  • a_gpu (pycuda.gpuarray.GPUArray) – Real/complex input matrix a with dimensions (m, n). a is assumed to be m>=`n`.
  • mode ({'reduced', 'economic', 'r'}) – ‘reduced’ : returns Q, R with dimensions (m, k) and (k, n) (default). ‘economic’ : returns Q only with dimensions (m, k). ‘r’ : returns R only with dimensions (k, n) with k`=min`(m,n).
  • handle (int) – CUBLAS context. If no context is specified, the default handle from skcuda.misc._global_cublas_handle is used.
  • lib (str) – Library to use. May be either ‘cula’ or ‘cusolver’.
Returns:

  • q_gpu (pycuda.gpuarray.GPUArray) – Orthonormal/unitary matrix (depending on whether or not A is real/complex).
  • r_gpu (pycuda.gpuarray.GPUArray) – The upper-triangular matrix.

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 assumed to be stored in column-major order, i.e., order=’F’.

Examples

>>> import pycuda.autoinit
>>> import pycuda.gpuarray as gpuarray
>>> import numpy as np
>>> import skcuda.linalg as linalg
>>> linalg.init()
>>> # Rectangular matrix A, np.float32
>>> A = np.array(np.random.randn(9, 7), np.float32, order='F')
>>> A_gpu = gpuarray.to_gpu(A)
>>> Q_gpu, R_gpu = linalg.qr(A_gpu, 'reduced')
>>> np.allclose(A, np.dot(Q_gpu.get(), R_gpu.get()), 1e-4)
True
>>> # Square matrix A, np.complex128
>>> A = np.random.randn(9, 9) + 1j*np.random.randn(9, 9)
>>> A = np.asarray(A, np.complex128, order='F')
>>> A_gpu = gpuarray.to_gpu(A)
>>> Q_gpu, R_gpu = linalg.qr(A_gpu, 'reduced')
>>> np.allclose(A, np.dot(Q_gpu.get(), R_gpu.get()), 1e-4)
True
>>> np.allclose(np.identity(Q_gpu.shape[0]) + 1j*0, np.dot(Q_gpu.get().conj().T, Q_gpu.get()), 1e-4)
True
>>> # Numpy QR and CULA QR
>>> A = np.array(np.random.randn(9, 7), np.float32, order='F')
>>> Q, R = np.linalg.qr(A, 'reduced')
>>> a_gpu = gpuarray.to_gpu(A)
>>> Q_gpu, R_gpu = linalg.qr(a_gpu, 'reduced')
>>> np.allclose(Q, Q_gpu.get(), 1e-4)
True
>>> np.allclose(R, R_gpu.get(), 1e-4)
True