_decomp_ldl.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. from warnings import warn
  2. import numpy as np
  3. from numpy import (atleast_2d, ComplexWarning, arange, zeros_like, imag, diag,
  4. iscomplexobj, tril, triu, argsort, empty_like)
  5. from ._decomp import _asarray_validated
  6. from .lapack import get_lapack_funcs, _compute_lwork
  7. __all__ = ['ldl']
  8. def ldl(A, lower=True, hermitian=True, overwrite_a=False, check_finite=True):
  9. """ Computes the LDLt or Bunch-Kaufman factorization of a symmetric/
  10. hermitian matrix.
  11. This function returns a block diagonal matrix D consisting blocks of size
  12. at most 2x2 and also a possibly permuted unit lower triangular matrix
  13. ``L`` such that the factorization ``A = L D L^H`` or ``A = L D L^T``
  14. holds. If `lower` is False then (again possibly permuted) upper
  15. triangular matrices are returned as outer factors.
  16. The permutation array can be used to triangularize the outer factors
  17. simply by a row shuffle, i.e., ``lu[perm, :]`` is an upper/lower
  18. triangular matrix. This is also equivalent to multiplication with a
  19. permutation matrix ``P.dot(lu)``, where ``P`` is a column-permuted
  20. identity matrix ``I[:, perm]``.
  21. Depending on the value of the boolean `lower`, only upper or lower
  22. triangular part of the input array is referenced. Hence, a triangular
  23. matrix on entry would give the same result as if the full matrix is
  24. supplied.
  25. Parameters
  26. ----------
  27. A : array_like
  28. Square input array
  29. lower : bool, optional
  30. This switches between the lower and upper triangular outer factors of
  31. the factorization. Lower triangular (``lower=True``) is the default.
  32. hermitian : bool, optional
  33. For complex-valued arrays, this defines whether ``A = A.conj().T`` or
  34. ``A = A.T`` is assumed. For real-valued arrays, this switch has no
  35. effect.
  36. overwrite_a : bool, optional
  37. Allow overwriting data in `A` (may enhance performance). The default
  38. is False.
  39. check_finite : bool, optional
  40. Whether to check that the input matrices contain only finite numbers.
  41. Disabling may give a performance gain, but may result in problems
  42. (crashes, non-termination) if the inputs do contain infinities or NaNs.
  43. Returns
  44. -------
  45. lu : ndarray
  46. The (possibly) permuted upper/lower triangular outer factor of the
  47. factorization.
  48. d : ndarray
  49. The block diagonal multiplier of the factorization.
  50. perm : ndarray
  51. The row-permutation index array that brings lu into triangular form.
  52. Raises
  53. ------
  54. ValueError
  55. If input array is not square.
  56. ComplexWarning
  57. If a complex-valued array with nonzero imaginary parts on the
  58. diagonal is given and hermitian is set to True.
  59. See Also
  60. --------
  61. cholesky, lu
  62. Notes
  63. -----
  64. This function uses ``?SYTRF`` routines for symmetric matrices and
  65. ``?HETRF`` routines for Hermitian matrices from LAPACK. See [1]_ for
  66. the algorithm details.
  67. Depending on the `lower` keyword value, only lower or upper triangular
  68. part of the input array is referenced. Moreover, this keyword also defines
  69. the structure of the outer factors of the factorization.
  70. .. versionadded:: 1.1.0
  71. References
  72. ----------
  73. .. [1] J.R. Bunch, L. Kaufman, Some stable methods for calculating
  74. inertia and solving symmetric linear systems, Math. Comput. Vol.31,
  75. 1977. :doi:`10.2307/2005787`
  76. Examples
  77. --------
  78. Given an upper triangular array ``a`` that represents the full symmetric
  79. array with its entries, obtain ``l``, 'd' and the permutation vector `perm`:
  80. >>> import numpy as np
  81. >>> from scipy.linalg import ldl
  82. >>> a = np.array([[2, -1, 3], [0, 2, 0], [0, 0, 1]])
  83. >>> lu, d, perm = ldl(a, lower=0) # Use the upper part
  84. >>> lu
  85. array([[ 0. , 0. , 1. ],
  86. [ 0. , 1. , -0.5],
  87. [ 1. , 1. , 1.5]])
  88. >>> d
  89. array([[-5. , 0. , 0. ],
  90. [ 0. , 1.5, 0. ],
  91. [ 0. , 0. , 2. ]])
  92. >>> perm
  93. array([2, 1, 0])
  94. >>> lu[perm, :]
  95. array([[ 1. , 1. , 1.5],
  96. [ 0. , 1. , -0.5],
  97. [ 0. , 0. , 1. ]])
  98. >>> lu.dot(d).dot(lu.T)
  99. array([[ 2., -1., 3.],
  100. [-1., 2., 0.],
  101. [ 3., 0., 1.]])
  102. """
  103. a = atleast_2d(_asarray_validated(A, check_finite=check_finite))
  104. if a.shape[0] != a.shape[1]:
  105. raise ValueError('The input array "a" should be square.')
  106. # Return empty arrays for empty square input
  107. if a.size == 0:
  108. return empty_like(a), empty_like(a), np.array([], dtype=int)
  109. n = a.shape[0]
  110. r_or_c = complex if iscomplexobj(a) else float
  111. # Get the LAPACK routine
  112. if r_or_c is complex and hermitian:
  113. s, sl = 'hetrf', 'hetrf_lwork'
  114. if np.any(imag(diag(a))):
  115. warn('scipy.linalg.ldl():\nThe imaginary parts of the diagonal'
  116. 'are ignored. Use "hermitian=False" for factorization of'
  117. 'complex symmetric arrays.', ComplexWarning, stacklevel=2)
  118. else:
  119. s, sl = 'sytrf', 'sytrf_lwork'
  120. solver, solver_lwork = get_lapack_funcs((s, sl), (a,))
  121. lwork = _compute_lwork(solver_lwork, n, lower=lower)
  122. ldu, piv, info = solver(a, lwork=lwork, lower=lower,
  123. overwrite_a=overwrite_a)
  124. if info < 0:
  125. raise ValueError('{} exited with the internal error "illegal value '
  126. 'in argument number {}". See LAPACK documentation '
  127. 'for the error codes.'.format(s.upper(), -info))
  128. swap_arr, pivot_arr = _ldl_sanitize_ipiv(piv, lower=lower)
  129. d, lu = _ldl_get_d_and_l(ldu, pivot_arr, lower=lower, hermitian=hermitian)
  130. lu, perm = _ldl_construct_tri_factor(lu, swap_arr, pivot_arr, lower=lower)
  131. return lu, d, perm
  132. def _ldl_sanitize_ipiv(a, lower=True):
  133. """
  134. This helper function takes the rather strangely encoded permutation array
  135. returned by the LAPACK routines ?(HE/SY)TRF and converts it into
  136. regularized permutation and diagonal pivot size format.
  137. Since FORTRAN uses 1-indexing and LAPACK uses different start points for
  138. upper and lower formats there are certain offsets in the indices used
  139. below.
  140. Let's assume a result where the matrix is 6x6 and there are two 2x2
  141. and two 1x1 blocks reported by the routine. To ease the coding efforts,
  142. we still populate a 6-sized array and fill zeros as the following ::
  143. pivots = [2, 0, 2, 0, 1, 1]
  144. This denotes a diagonal matrix of the form ::
  145. [x x ]
  146. [x x ]
  147. [ x x ]
  148. [ x x ]
  149. [ x ]
  150. [ x]
  151. In other words, we write 2 when the 2x2 block is first encountered and
  152. automatically write 0 to the next entry and skip the next spin of the
  153. loop. Thus, a separate counter or array appends to keep track of block
  154. sizes are avoided. If needed, zeros can be filtered out later without
  155. losing the block structure.
  156. Parameters
  157. ----------
  158. a : ndarray
  159. The permutation array ipiv returned by LAPACK
  160. lower : bool, optional
  161. The switch to select whether upper or lower triangle is chosen in
  162. the LAPACK call.
  163. Returns
  164. -------
  165. swap_ : ndarray
  166. The array that defines the row/column swap operations. For example,
  167. if row two is swapped with row four, the result is [0, 3, 2, 3].
  168. pivots : ndarray
  169. The array that defines the block diagonal structure as given above.
  170. """
  171. n = a.size
  172. swap_ = arange(n)
  173. pivots = zeros_like(swap_, dtype=int)
  174. skip_2x2 = False
  175. # Some upper/lower dependent offset values
  176. # range (s)tart, r(e)nd, r(i)ncrement
  177. x, y, rs, re, ri = (1, 0, 0, n, 1) if lower else (-1, -1, n-1, -1, -1)
  178. for ind in range(rs, re, ri):
  179. # If previous spin belonged already to a 2x2 block
  180. if skip_2x2:
  181. skip_2x2 = False
  182. continue
  183. cur_val = a[ind]
  184. # do we have a 1x1 block or not?
  185. if cur_val > 0:
  186. if cur_val != ind+1:
  187. # Index value != array value --> permutation required
  188. swap_[ind] = swap_[cur_val-1]
  189. pivots[ind] = 1
  190. # Not.
  191. elif cur_val < 0 and cur_val == a[ind+x]:
  192. # first neg entry of 2x2 block identifier
  193. if -cur_val != ind+2:
  194. # Index value != array value --> permutation required
  195. swap_[ind+x] = swap_[-cur_val-1]
  196. pivots[ind+y] = 2
  197. skip_2x2 = True
  198. else: # Doesn't make sense, give up
  199. raise ValueError('While parsing the permutation array '
  200. 'in "scipy.linalg.ldl", invalid entries '
  201. 'found. The array syntax is invalid.')
  202. return swap_, pivots
  203. def _ldl_get_d_and_l(ldu, pivs, lower=True, hermitian=True):
  204. """
  205. Helper function to extract the diagonal and triangular matrices for
  206. LDL.T factorization.
  207. Parameters
  208. ----------
  209. ldu : ndarray
  210. The compact output returned by the LAPACK routing
  211. pivs : ndarray
  212. The sanitized array of {0, 1, 2} denoting the sizes of the pivots. For
  213. every 2 there is a succeeding 0.
  214. lower : bool, optional
  215. If set to False, upper triangular part is considered.
  216. hermitian : bool, optional
  217. If set to False a symmetric complex array is assumed.
  218. Returns
  219. -------
  220. d : ndarray
  221. The block diagonal matrix.
  222. lu : ndarray
  223. The upper/lower triangular matrix
  224. """
  225. is_c = iscomplexobj(ldu)
  226. d = diag(diag(ldu))
  227. n = d.shape[0]
  228. blk_i = 0 # block index
  229. # row/column offsets for selecting sub-, super-diagonal
  230. x, y = (1, 0) if lower else (0, 1)
  231. lu = tril(ldu, -1) if lower else triu(ldu, 1)
  232. diag_inds = arange(n)
  233. lu[diag_inds, diag_inds] = 1
  234. for blk in pivs[pivs != 0]:
  235. # increment the block index and check for 2s
  236. # if 2 then copy the off diagonals depending on uplo
  237. inc = blk_i + blk
  238. if blk == 2:
  239. d[blk_i+x, blk_i+y] = ldu[blk_i+x, blk_i+y]
  240. # If Hermitian matrix is factorized, the cross-offdiagonal element
  241. # should be conjugated.
  242. if is_c and hermitian:
  243. d[blk_i+y, blk_i+x] = ldu[blk_i+x, blk_i+y].conj()
  244. else:
  245. d[blk_i+y, blk_i+x] = ldu[blk_i+x, blk_i+y]
  246. lu[blk_i+x, blk_i+y] = 0.
  247. blk_i = inc
  248. return d, lu
  249. def _ldl_construct_tri_factor(lu, swap_vec, pivs, lower=True):
  250. """
  251. Helper function to construct explicit outer factors of LDL factorization.
  252. If lower is True the permuted factors are multiplied as L(1)*L(2)*...*L(k).
  253. Otherwise, the permuted factors are multiplied as L(k)*...*L(2)*L(1). See
  254. LAPACK documentation for more details.
  255. Parameters
  256. ----------
  257. lu : ndarray
  258. The triangular array that is extracted from LAPACK routine call with
  259. ones on the diagonals.
  260. swap_vec : ndarray
  261. The array that defines the row swapping indices. If the kth entry is m
  262. then rows k,m are swapped. Notice that the mth entry is not necessarily
  263. k to avoid undoing the swapping.
  264. pivs : ndarray
  265. The array that defines the block diagonal structure returned by
  266. _ldl_sanitize_ipiv().
  267. lower : bool, optional
  268. The boolean to switch between lower and upper triangular structure.
  269. Returns
  270. -------
  271. lu : ndarray
  272. The square outer factor which satisfies the L * D * L.T = A
  273. perm : ndarray
  274. The permutation vector that brings the lu to the triangular form
  275. Notes
  276. -----
  277. Note that the original argument "lu" is overwritten.
  278. """
  279. n = lu.shape[0]
  280. perm = arange(n)
  281. # Setup the reading order of the permutation matrix for upper/lower
  282. rs, re, ri = (n-1, -1, -1) if lower else (0, n, 1)
  283. for ind in range(rs, re, ri):
  284. s_ind = swap_vec[ind]
  285. if s_ind != ind:
  286. # Column start and end positions
  287. col_s = ind if lower else 0
  288. col_e = n if lower else ind+1
  289. # If we stumble upon a 2x2 block include both cols in the perm.
  290. if pivs[ind] == (0 if lower else 2):
  291. col_s += -1 if lower else 0
  292. col_e += 0 if lower else 1
  293. lu[[s_ind, ind], col_s:col_e] = lu[[ind, s_ind], col_s:col_e]
  294. perm[[s_ind, ind]] = perm[[ind, s_ind]]
  295. return lu, argsort(perm)