Advertisement
Guest User

Untitled

a guest
Jul 6th, 2011
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.10 KB | None | 0 0
  1. import cvxopt
  2. from cvxopt import lapack
  3.  
  4. def qr(a, overwrite_a=0, lwork=None, mode='qr'):
  5.     """Compute QR decomposition of a matrix.
  6.  
  7.    Calculate the decomposition :lm:`A = Q R` where Q is unitary/orthogonal
  8.    and R upper triangular.
  9.  
  10.    Parameters
  11.    ----------
  12.    a : array, shape (M, N)
  13.        Matrix to be decomposed
  14.    overwrite_a : boolean
  15.        Whether data in a is overwritten (may improve performance)
  16.    lwork : integer
  17.        Work array size, lwork >= a.shape[1]. If None or -1, an optimal size
  18.        is computed.
  19.    mode : {'qr', 'r', 'qrp'}
  20.        Determines what information is to be returned: either both Q and R
  21.        or only R, or Q and R and P, a permutation matrix. Any of these can
  22.        be combined with 'economic' using the '+' sign as a separator.
  23.        Economic mode means the following:
  24.            Compute the economy-size QR decomposition, making shapes
  25.            of Q and R (M, K) and (K, N) instead of (M,M) and (M,N).
  26.            K=min(M,N).
  27.  
  28.    Returns
  29.    -------
  30.    (if mode == 'qr')
  31.    Q : double or complex array, shape (M, M) or (M, K) for econ==True
  32.  
  33.    (for any mode)
  34.    R : double or complex array, shape (M, N) or (K, N) for econ==True
  35.        Size K = min(M, N)
  36.  
  37.    Raises LinAlgError if decomposition fails
  38.  
  39.    Notes
  40.    -----
  41.    This is an interface to the LAPACK routines dgeqrf, zgeqrf,
  42.    dorgqr, and zungqr.
  43.  
  44.    Examples
  45.    --------
  46.    >>> from scipy import random, linalg, dot
  47.    >>> a = random.randn(9, 6)
  48.    >>> q, r = linalg.qr(a)
  49.    >>> allclose(a, dot(q, r))
  50.    True
  51.    >>> q.shape, r.shape
  52.    ((9, 9), (9, 6))
  53.  
  54.    >>> r2 = linalg.qr(a, mode='r')
  55.    >>> allclose(r, r2)
  56.  
  57.    >>> q3, r3 = linalg.qr(a, econ=True)
  58.    >>> q3.shape, r3.shape
  59.    ((9, 6), (6, 6))
  60.  
  61.    """
  62.     mode = mode.split("+")
  63.     if "economic" in mode:
  64.         econ = True
  65.     else:
  66.         econ = False
  67.  
  68.     a1 = asarray_chkfinite(a)
  69.     if len(a1.shape) != 2:
  70.         raise ValueError("expected 2D array")
  71.     M, N = a1.shape
  72.     overwrite_a = overwrite_a or (_datanotshared(a1,a))
  73.  
  74.     if 'qrp' in mode:
  75.         qr = cvxopt.matrix(np.asarray(a1, dtype = float))
  76.  
  77.         tau = cvxopt.matrix(np.zeros(min(M, N), dtype = float))
  78.         jpvt = cvxopt.matrix(np.zeros(N, dtype = int))
  79.  
  80.         lapack.geqp3(qr, jpvt, tau)
  81.  
  82.         qr = np.asarray(qr)
  83.         tau = np.asarray(tau)
  84.         jpvt = (np.asarray(jpvt) - 1).flatten()
  85.     else:
  86.         geqrf, = get_lapack_funcs(('geqrf',),(a1,))
  87.         if lwork is None or lwork == -1:
  88.             # get optimal work array
  89.             qr,tau,work,info = geqrf(a1,lwork=-1,overwrite_a=1)
  90.             lwork = work[0]
  91.  
  92.         qr,tau,work,info = geqrf(a1,lwork=lwork,overwrite_a=overwrite_a)
  93.         if info<0:
  94.             raise ValueError("illegal value in %-th argument of internal geqrf"
  95.                 % -info)
  96.  
  97.     if not econ or M<N:
  98.         R = basic.triu(qr)
  99.     else:
  100.         R = basic.triu(qr[0:N,0:N])
  101.  
  102.     if 'r' in mode:
  103.         return R
  104.  
  105.     if find_best_lapack_type((a1,))[0]=='s' or find_best_lapack_type((a1,))[0]=='d':
  106.         gor_un_gqr, = get_lapack_funcs(('orgqr',),(qr,))
  107.     else:
  108.         gor_un_gqr, = get_lapack_funcs(('ungqr',),(qr,))
  109.  
  110.     if M<N:
  111.         # get optimal work array
  112.         Q,work,info = gor_un_gqr(qr[:,0:M],tau,lwork=-1,overwrite_a=1)
  113.         lwork = work[0]
  114.         Q,work,info = gor_un_gqr(qr[:,0:M],tau,lwork=lwork,overwrite_a=1)
  115.     elif econ:
  116.         # get optimal work array
  117.         Q,work,info = gor_un_gqr(qr,tau,lwork=-1,overwrite_a=1)
  118.         lwork = work[0]
  119.         Q,work,info = gor_un_gqr(qr,tau,lwork=lwork,overwrite_a=1)
  120.     else:
  121.         t = qr.dtype.char
  122.         qqr = numpy.empty((M,M),dtype=t)
  123.         qqr[:,0:N]=qr
  124.         # get optimal work array
  125.         Q,work,info = gor_un_gqr(qqr,tau,lwork=-1,overwrite_a=1)
  126.         lwork = work[0]
  127.         Q,work,info = gor_un_gqr(qqr,tau,lwork=lwork,overwrite_a=1)
  128.  
  129.     if info < 0:
  130.         raise ValueError("illegal value in %-th argument of internal gorgqr"
  131.             % -info)
  132.  
  133.     if 'qrp' in mode:
  134.         return Q, R, jpvt
  135.  
  136.     return Q, R
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement