_svds_doc.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. def _svds_arpack_doc(A, k=6, ncv=None, tol=0, which='LM', v0=None,
  2. maxiter=None, return_singular_vectors=True,
  3. solver='arpack', random_state=None):
  4. """
  5. Partial singular value decomposition of a sparse matrix using ARPACK.
  6. Compute the largest or smallest `k` singular values and corresponding
  7. singular vectors of a sparse matrix `A`. The order in which the singular
  8. values are returned is not guaranteed.
  9. In the descriptions below, let ``M, N = A.shape``.
  10. Parameters
  11. ----------
  12. A : sparse matrix or LinearOperator
  13. Matrix to decompose.
  14. k : int, optional
  15. Number of singular values and singular vectors to compute.
  16. Must satisfy ``1 <= k <= min(M, N) - 1``.
  17. Default is 6.
  18. ncv : int, optional
  19. The number of Lanczos vectors generated.
  20. The default is ``min(n, max(2*k + 1, 20))``.
  21. If specified, must satistify ``k + 1 < ncv < min(M, N)``; ``ncv > 2*k``
  22. is recommended.
  23. tol : float, optional
  24. Tolerance for singular values. Zero (default) means machine precision.
  25. which : {'LM', 'SM'}
  26. Which `k` singular values to find: either the largest magnitude ('LM')
  27. or smallest magnitude ('SM') singular values.
  28. v0 : ndarray, optional
  29. The starting vector for iteration:
  30. an (approximate) left singular vector if ``N > M`` and a right singular
  31. vector otherwise. Must be of length ``min(M, N)``.
  32. Default: random
  33. maxiter : int, optional
  34. Maximum number of Arnoldi update iterations allowed;
  35. default is ``min(M, N) * 10``.
  36. return_singular_vectors : {True, False, "u", "vh"}
  37. Singular values are always computed and returned; this parameter
  38. controls the computation and return of singular vectors.
  39. - ``True``: return singular vectors.
  40. - ``False``: do not return singular vectors.
  41. - ``"u"``: if ``M <= N``, compute only the left singular vectors and
  42. return ``None`` for the right singular vectors. Otherwise, compute
  43. all singular vectors.
  44. - ``"vh"``: if ``M > N``, compute only the right singular vectors and
  45. return ``None`` for the left singular vectors. Otherwise, compute
  46. all singular vectors.
  47. solver : {'arpack', 'propack', 'lobpcg'}, optional
  48. This is the solver-specific documentation for ``solver='arpack'``.
  49. :ref:`'lobpcg' <sparse.linalg.svds-lobpcg>` and
  50. :ref:`'propack' <sparse.linalg.svds-propack>`
  51. are also supported.
  52. random_state : {None, int, `numpy.random.Generator`,
  53. `numpy.random.RandomState`}, optional
  54. Pseudorandom number generator state used to generate resamples.
  55. If `random_state` is ``None`` (or `np.random`), the
  56. `numpy.random.RandomState` singleton is used.
  57. If `random_state` is an int, a new ``RandomState`` instance is used,
  58. seeded with `random_state`.
  59. If `random_state` is already a ``Generator`` or ``RandomState``
  60. instance then that instance is used.
  61. options : dict, optional
  62. A dictionary of solver-specific options. No solver-specific options
  63. are currently supported; this parameter is reserved for future use.
  64. Returns
  65. -------
  66. u : ndarray, shape=(M, k)
  67. Unitary matrix having left singular vectors as columns.
  68. s : ndarray, shape=(k,)
  69. The singular values.
  70. vh : ndarray, shape=(k, N)
  71. Unitary matrix having right singular vectors as rows.
  72. Notes
  73. -----
  74. This is a naive implementation using ARPACK as an eigensolver
  75. on ``A.conj().T @ A`` or ``A @ A.conj().T``, depending on which one is more
  76. efficient.
  77. Examples
  78. --------
  79. Construct a matrix ``A`` from singular values and vectors.
  80. >>> from scipy.stats import ortho_group
  81. >>> from scipy.sparse import csc_matrix, diags
  82. >>> from scipy.sparse.linalg import svds
  83. >>> rng = np.random.default_rng()
  84. >>> orthogonal = csc_matrix(ortho_group.rvs(10, random_state=rng))
  85. >>> s = [0.0001, 0.001, 3, 4, 5] # singular values
  86. >>> u = orthogonal[:, :5] # left singular vectors
  87. >>> vT = orthogonal[:, 5:].T # right singular vectors
  88. >>> A = u @ diags(s) @ vT
  89. With only three singular values/vectors, the SVD approximates the original
  90. matrix.
  91. >>> u2, s2, vT2 = svds(A, k=3, solver='arpack')
  92. >>> A2 = u2 @ np.diag(s2) @ vT2
  93. >>> np.allclose(A2, A.toarray(), atol=1e-3)
  94. True
  95. With all five singular values/vectors, we can reproduce the original
  96. matrix.
  97. >>> u3, s3, vT3 = svds(A, k=5, solver='arpack')
  98. >>> A3 = u3 @ np.diag(s3) @ vT3
  99. >>> np.allclose(A3, A.toarray())
  100. True
  101. The singular values match the expected singular values, and the singular
  102. vectors are as expected up to a difference in sign.
  103. >>> (np.allclose(s3, s) and
  104. ... np.allclose(np.abs(u3), np.abs(u.toarray())) and
  105. ... np.allclose(np.abs(vT3), np.abs(vT.toarray())))
  106. True
  107. The singular vectors are also orthogonal.
  108. >>> (np.allclose(u3.T @ u3, np.eye(5)) and
  109. ... np.allclose(vT3 @ vT3.T, np.eye(5)))
  110. True
  111. """
  112. pass
  113. def _svds_lobpcg_doc(A, k=6, ncv=None, tol=0, which='LM', v0=None,
  114. maxiter=None, return_singular_vectors=True,
  115. solver='lobpcg', random_state=None):
  116. """
  117. Partial singular value decomposition of a sparse matrix using LOBPCG.
  118. Compute the largest or smallest `k` singular values and corresponding
  119. singular vectors of a sparse matrix `A`. The order in which the singular
  120. values are returned is not guaranteed.
  121. In the descriptions below, let ``M, N = A.shape``.
  122. Parameters
  123. ----------
  124. A : sparse matrix or LinearOperator
  125. Matrix to decompose.
  126. k : int, default: 6
  127. Number of singular values and singular vectors to compute.
  128. Must satisfy ``1 <= k <= min(M, N) - 1``.
  129. ncv : int, optional
  130. Ignored.
  131. tol : float, optional
  132. Tolerance for singular values. Zero (default) means machine precision.
  133. which : {'LM', 'SM'}
  134. Which `k` singular values to find: either the largest magnitude ('LM')
  135. or smallest magnitude ('SM') singular values.
  136. v0 : ndarray, optional
  137. If `k` is 1, the starting vector for iteration:
  138. an (approximate) left singular vector if ``N > M`` and a right singular
  139. vector otherwise. Must be of length ``min(M, N)``.
  140. Ignored otherwise.
  141. Default: random
  142. maxiter : int, default: 20
  143. Maximum number of iterations.
  144. return_singular_vectors : {True, False, "u", "vh"}
  145. Singular values are always computed and returned; this parameter
  146. controls the computation and return of singular vectors.
  147. - ``True``: return singular vectors.
  148. - ``False``: do not return singular vectors.
  149. - ``"u"``: if ``M <= N``, compute only the left singular vectors and
  150. return ``None`` for the right singular vectors. Otherwise, compute
  151. all singular vectors.
  152. - ``"vh"``: if ``M > N``, compute only the right singular vectors and
  153. return ``None`` for the left singular vectors. Otherwise, compute
  154. all singular vectors.
  155. solver : {'arpack', 'propack', 'lobpcg'}, optional
  156. This is the solver-specific documentation for ``solver='lobpcg'``.
  157. :ref:`'arpack' <sparse.linalg.svds-arpack>` and
  158. :ref:`'propack' <sparse.linalg.svds-propack>`
  159. are also supported.
  160. random_state : {None, int, `numpy.random.Generator`,
  161. `numpy.random.RandomState`}, optional
  162. Pseudorandom number generator state used to generate resamples.
  163. If `random_state` is ``None`` (or `np.random`), the
  164. `numpy.random.RandomState` singleton is used.
  165. If `random_state` is an int, a new ``RandomState`` instance is used,
  166. seeded with `random_state`.
  167. If `random_state` is already a ``Generator`` or ``RandomState``
  168. instance then that instance is used.
  169. options : dict, optional
  170. A dictionary of solver-specific options. No solver-specific options
  171. are currently supported; this parameter is reserved for future use.
  172. Returns
  173. -------
  174. u : ndarray, shape=(M, k)
  175. Unitary matrix having left singular vectors as columns.
  176. s : ndarray, shape=(k,)
  177. The singular values.
  178. vh : ndarray, shape=(k, N)
  179. Unitary matrix having right singular vectors as rows.
  180. Notes
  181. -----
  182. This is a naive implementation using LOBPCG as an eigensolver
  183. on ``A.conj().T @ A`` or ``A @ A.conj().T``, depending on which one is more
  184. efficient.
  185. Examples
  186. --------
  187. Construct a matrix ``A`` from singular values and vectors.
  188. >>> from scipy.stats import ortho_group
  189. >>> from scipy.sparse import csc_matrix, diags
  190. >>> from scipy.sparse.linalg import svds
  191. >>> rng = np.random.default_rng()
  192. >>> orthogonal = csc_matrix(ortho_group.rvs(10, random_state=rng))
  193. >>> s = [0.0001, 0.001, 3, 4, 5] # singular values
  194. >>> u = orthogonal[:, :5] # left singular vectors
  195. >>> vT = orthogonal[:, 5:].T # right singular vectors
  196. >>> A = u @ diags(s) @ vT
  197. With only three singular values/vectors, the SVD approximates the original
  198. matrix.
  199. >>> u2, s2, vT2 = svds(A, k=3, solver='lobpcg')
  200. >>> A2 = u2 @ np.diag(s2) @ vT2
  201. >>> np.allclose(A2, A.toarray(), atol=1e-3)
  202. True
  203. With all five singular values/vectors, we can reproduce the original
  204. matrix.
  205. >>> u3, s3, vT3 = svds(A, k=5, solver='lobpcg')
  206. >>> A3 = u3 @ np.diag(s3) @ vT3
  207. >>> np.allclose(A3, A.toarray())
  208. True
  209. The singular values match the expected singular values, and the singular
  210. vectors are as expected up to a difference in sign.
  211. >>> (np.allclose(s3, s) and
  212. ... np.allclose(np.abs(u3), np.abs(u.todense())) and
  213. ... np.allclose(np.abs(vT3), np.abs(vT.todense())))
  214. True
  215. The singular vectors are also orthogonal.
  216. >>> (np.allclose(u3.T @ u3, np.eye(5)) and
  217. ... np.allclose(vT3 @ vT3.T, np.eye(5)))
  218. True
  219. """
  220. pass
  221. def _svds_propack_doc(A, k=6, ncv=None, tol=0, which='LM', v0=None,
  222. maxiter=None, return_singular_vectors=True,
  223. solver='propack', random_state=None):
  224. """
  225. Partial singular value decomposition of a sparse matrix using PROPACK.
  226. Compute the largest or smallest `k` singular values and corresponding
  227. singular vectors of a sparse matrix `A`. The order in which the singular
  228. values are returned is not guaranteed.
  229. In the descriptions below, let ``M, N = A.shape``.
  230. Parameters
  231. ----------
  232. A : sparse matrix or LinearOperator
  233. Matrix to decompose. If `A` is a ``LinearOperator``
  234. object, it must define both ``matvec`` and ``rmatvec`` methods.
  235. k : int, default: 6
  236. Number of singular values and singular vectors to compute.
  237. Must satisfy ``1 <= k <= min(M, N)``.
  238. ncv : int, optional
  239. Ignored.
  240. tol : float, optional
  241. The desired relative accuracy for computed singular values.
  242. Zero (default) means machine precision.
  243. which : {'LM', 'SM'}
  244. Which `k` singular values to find: either the largest magnitude ('LM')
  245. or smallest magnitude ('SM') singular values. Note that choosing
  246. ``which='SM'`` will force the ``irl`` option to be set ``True``.
  247. v0 : ndarray, optional
  248. Starting vector for iterations: must be of length ``A.shape[0]``.
  249. If not specified, PROPACK will generate a starting vector.
  250. maxiter : int, optional
  251. Maximum number of iterations / maximal dimension of the Krylov
  252. subspace. Default is ``10 * k``.
  253. return_singular_vectors : {True, False, "u", "vh"}
  254. Singular values are always computed and returned; this parameter
  255. controls the computation and return of singular vectors.
  256. - ``True``: return singular vectors.
  257. - ``False``: do not return singular vectors.
  258. - ``"u"``: compute only the left singular vectors; return ``None`` for
  259. the right singular vectors.
  260. - ``"vh"``: compute only the right singular vectors; return ``None``
  261. for the left singular vectors.
  262. solver : {'arpack', 'propack', 'lobpcg'}, optional
  263. This is the solver-specific documentation for ``solver='propack'``.
  264. :ref:`'arpack' <sparse.linalg.svds-arpack>` and
  265. :ref:`'lobpcg' <sparse.linalg.svds-lobpcg>`
  266. are also supported.
  267. random_state : {None, int, `numpy.random.Generator`,
  268. `numpy.random.RandomState`}, optional
  269. Pseudorandom number generator state used to generate resamples.
  270. If `random_state` is ``None`` (or `np.random`), the
  271. `numpy.random.RandomState` singleton is used.
  272. If `random_state` is an int, a new ``RandomState`` instance is used,
  273. seeded with `random_state`.
  274. If `random_state` is already a ``Generator`` or ``RandomState``
  275. instance then that instance is used.
  276. options : dict, optional
  277. A dictionary of solver-specific options. No solver-specific options
  278. are currently supported; this parameter is reserved for future use.
  279. Returns
  280. -------
  281. u : ndarray, shape=(M, k)
  282. Unitary matrix having left singular vectors as columns.
  283. s : ndarray, shape=(k,)
  284. The singular values.
  285. vh : ndarray, shape=(k, N)
  286. Unitary matrix having right singular vectors as rows.
  287. Notes
  288. -----
  289. This is an interface to the Fortran library PROPACK [1]_.
  290. The current default is to run with IRL mode disabled unless seeking the
  291. smallest singular values/vectors (``which='SM'``).
  292. References
  293. ----------
  294. .. [1] Larsen, Rasmus Munk. "PROPACK-Software for large and sparse SVD
  295. calculations." Available online. URL
  296. http://sun.stanford.edu/~rmunk/PROPACK (2004): 2008-2009.
  297. Examples
  298. --------
  299. Construct a matrix ``A`` from singular values and vectors.
  300. >>> from scipy.stats import ortho_group
  301. >>> from scipy.sparse import csc_matrix, diags
  302. >>> from scipy.sparse.linalg import svds
  303. >>> rng = np.random.default_rng()
  304. >>> orthogonal = csc_matrix(ortho_group.rvs(10, random_state=rng))
  305. >>> s = [0.0001, 0.001, 3, 4, 5] # singular values
  306. >>> u = orthogonal[:, :5] # left singular vectors
  307. >>> vT = orthogonal[:, 5:].T # right singular vectors
  308. >>> A = u @ diags(s) @ vT
  309. With only three singular values/vectors, the SVD approximates the original
  310. matrix.
  311. >>> u2, s2, vT2 = svds(A, k=3, solver='propack')
  312. >>> A2 = u2 @ np.diag(s2) @ vT2
  313. >>> np.allclose(A2, A.todense(), atol=1e-3)
  314. True
  315. With all five singular values/vectors, we can reproduce the original
  316. matrix.
  317. >>> u3, s3, vT3 = svds(A, k=5, solver='propack')
  318. >>> A3 = u3 @ np.diag(s3) @ vT3
  319. >>> np.allclose(A3, A.todense())
  320. True
  321. The singular values match the expected singular values, and the singular
  322. vectors are as expected up to a difference in sign.
  323. >>> (np.allclose(s3, s) and
  324. ... np.allclose(np.abs(u3), np.abs(u.toarray())) and
  325. ... np.allclose(np.abs(vT3), np.abs(vT.toarray())))
  326. True
  327. The singular vectors are also orthogonal.
  328. >>> (np.allclose(u3.T @ u3, np.eye(5)) and
  329. ... np.allclose(vT3 @ vT3.T, np.eye(5)))
  330. True
  331. """
  332. pass