_decomp_polar.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import numpy as np
  2. from scipy.linalg import svd
  3. __all__ = ['polar']
  4. def polar(a, side="right"):
  5. """
  6. Compute the polar decomposition.
  7. Returns the factors of the polar decomposition [1]_ `u` and `p` such
  8. that ``a = up`` (if `side` is "right") or ``a = pu`` (if `side` is
  9. "left"), where `p` is positive semidefinite. Depending on the shape
  10. of `a`, either the rows or columns of `u` are orthonormal. When `a`
  11. is a square array, `u` is a square unitary array. When `a` is not
  12. square, the "canonical polar decomposition" [2]_ is computed.
  13. Parameters
  14. ----------
  15. a : (m, n) array_like
  16. The array to be factored.
  17. side : {'left', 'right'}, optional
  18. Determines whether a right or left polar decomposition is computed.
  19. If `side` is "right", then ``a = up``. If `side` is "left", then
  20. ``a = pu``. The default is "right".
  21. Returns
  22. -------
  23. u : (m, n) ndarray
  24. If `a` is square, then `u` is unitary. If m > n, then the columns
  25. of `a` are orthonormal, and if m < n, then the rows of `u` are
  26. orthonormal.
  27. p : ndarray
  28. `p` is Hermitian positive semidefinite. If `a` is nonsingular, `p`
  29. is positive definite. The shape of `p` is (n, n) or (m, m), depending
  30. on whether `side` is "right" or "left", respectively.
  31. References
  32. ----------
  33. .. [1] R. A. Horn and C. R. Johnson, "Matrix Analysis", Cambridge
  34. University Press, 1985.
  35. .. [2] N. J. Higham, "Functions of Matrices: Theory and Computation",
  36. SIAM, 2008.
  37. Examples
  38. --------
  39. >>> import numpy as np
  40. >>> from scipy.linalg import polar
  41. >>> a = np.array([[1, -1], [2, 4]])
  42. >>> u, p = polar(a)
  43. >>> u
  44. array([[ 0.85749293, -0.51449576],
  45. [ 0.51449576, 0.85749293]])
  46. >>> p
  47. array([[ 1.88648444, 1.2004901 ],
  48. [ 1.2004901 , 3.94446746]])
  49. A non-square example, with m < n:
  50. >>> b = np.array([[0.5, 1, 2], [1.5, 3, 4]])
  51. >>> u, p = polar(b)
  52. >>> u
  53. array([[-0.21196618, -0.42393237, 0.88054056],
  54. [ 0.39378971, 0.78757942, 0.4739708 ]])
  55. >>> p
  56. array([[ 0.48470147, 0.96940295, 1.15122648],
  57. [ 0.96940295, 1.9388059 , 2.30245295],
  58. [ 1.15122648, 2.30245295, 3.65696431]])
  59. >>> u.dot(p) # Verify the decomposition.
  60. array([[ 0.5, 1. , 2. ],
  61. [ 1.5, 3. , 4. ]])
  62. >>> u.dot(u.T) # The rows of u are orthonormal.
  63. array([[ 1.00000000e+00, -2.07353665e-17],
  64. [ -2.07353665e-17, 1.00000000e+00]])
  65. Another non-square example, with m > n:
  66. >>> c = b.T
  67. >>> u, p = polar(c)
  68. >>> u
  69. array([[-0.21196618, 0.39378971],
  70. [-0.42393237, 0.78757942],
  71. [ 0.88054056, 0.4739708 ]])
  72. >>> p
  73. array([[ 1.23116567, 1.93241587],
  74. [ 1.93241587, 4.84930602]])
  75. >>> u.dot(p) # Verify the decomposition.
  76. array([[ 0.5, 1.5],
  77. [ 1. , 3. ],
  78. [ 2. , 4. ]])
  79. >>> u.T.dot(u) # The columns of u are orthonormal.
  80. array([[ 1.00000000e+00, -1.26363763e-16],
  81. [ -1.26363763e-16, 1.00000000e+00]])
  82. """
  83. if side not in ['right', 'left']:
  84. raise ValueError("`side` must be either 'right' or 'left'")
  85. a = np.asarray(a)
  86. if a.ndim != 2:
  87. raise ValueError("`a` must be a 2-D array.")
  88. w, s, vh = svd(a, full_matrices=False)
  89. u = w.dot(vh)
  90. if side == 'right':
  91. # a = up
  92. p = (vh.T.conj() * s).dot(vh)
  93. else:
  94. # a = pu
  95. p = (w * s).dot(w.T.conj())
  96. return u, p