_decomp_ldl.py 12 KB

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