__init__.py 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import torch
  4. from torch._C import _add_docstr, _linalg # type: ignore[attr-defined]
  5. LinAlgError = torch._C._LinAlgError # type: ignore[attr-defined]
  6. Tensor = torch.Tensor
  7. common_notes = {
  8. "experimental_warning": """This function is "experimental" and it may change in a future PyTorch release.""",
  9. "sync_note": "When inputs are on a CUDA device, this function synchronizes that device with the CPU.",
  10. "sync_note_ex": r"When the inputs are on a CUDA device, this function synchronizes only when :attr:`check_errors`\ `= True`.",
  11. "sync_note_has_ex": ("When inputs are on a CUDA device, this function synchronizes that device with the CPU. "
  12. "For a version of this function that does not synchronize, see :func:`{}`.")
  13. }
  14. # Note: This not only adds doc strings for functions in the linalg namespace, but
  15. # also connects the torch.linalg Python namespace to the torch._C._linalg builtins.
  16. cross = _add_docstr(_linalg.linalg_cross, r"""
  17. linalg.cross(input, other, *, dim=-1, out=None) -> Tensor
  18. Computes the cross product of two 3-dimensional vectors.
  19. Supports input of float, double, cfloat and cdouble dtypes. Also supports batches
  20. of vectors, for which it computes the product along the dimension :attr:`dim`.
  21. It broadcasts over the batch dimensions.
  22. Args:
  23. input (Tensor): the first input tensor.
  24. other (Tensor): the second input tensor.
  25. dim (int, optional): the dimension along which to take the cross-product. Default: `-1`.
  26. Keyword args:
  27. out (Tensor, optional): the output tensor. Ignored if `None`. Default: `None`.
  28. Example:
  29. >>> a = torch.randn(4, 3)
  30. >>> a
  31. tensor([[-0.3956, 1.1455, 1.6895],
  32. [-0.5849, 1.3672, 0.3599],
  33. [-1.1626, 0.7180, -0.0521],
  34. [-0.1339, 0.9902, -2.0225]])
  35. >>> b = torch.randn(4, 3)
  36. >>> b
  37. tensor([[-0.0257, -1.4725, -1.2251],
  38. [-1.1479, -0.7005, -1.9757],
  39. [-1.3904, 0.3726, -1.1836],
  40. [-0.9688, -0.7153, 0.2159]])
  41. >>> torch.linalg.cross(a, b)
  42. tensor([[ 1.0844, -0.5281, 0.6120],
  43. [-2.4490, -1.5687, 1.9792],
  44. [-0.8304, -1.3037, 0.5650],
  45. [-1.2329, 1.9883, 1.0551]])
  46. >>> a = torch.randn(1, 3) # a is broadcast to match shape of b
  47. >>> a
  48. tensor([[-0.9941, -0.5132, 0.5681]])
  49. >>> torch.linalg.cross(a, b)
  50. tensor([[ 1.4653, -1.2325, 1.4507],
  51. [ 1.4119, -2.6163, 0.1073],
  52. [ 0.3957, -1.9666, -1.0840],
  53. [ 0.2956, -0.3357, 0.2139]])
  54. """)
  55. cholesky = _add_docstr(_linalg.linalg_cholesky, r"""
  56. linalg.cholesky(A, *, upper=False, out=None) -> Tensor
  57. Computes the Cholesky decomposition of a complex Hermitian or real symmetric positive-definite matrix.
  58. Letting :math:`\mathbb{K}` be :math:`\mathbb{R}` or :math:`\mathbb{C}`,
  59. the **Cholesky decomposition** of a complex Hermitian or real symmetric positive-definite matrix
  60. :math:`A \in \mathbb{K}^{n \times n}` is defined as
  61. .. math::
  62. A = LL^{\text{H}}\mathrlap{\qquad L \in \mathbb{K}^{n \times n}}
  63. where :math:`L` is a lower triangular matrix with real positive diagonal (even in the complex case) and
  64. :math:`L^{\text{H}}` is the conjugate transpose when :math:`L` is complex, and the transpose when :math:`L` is real-valued.
  65. Supports input of float, double, cfloat and cdouble dtypes.
  66. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  67. the output has the same batch dimensions.
  68. """ + fr"""
  69. .. note:: {common_notes["sync_note"]}
  70. """ + r"""
  71. .. seealso::
  72. :func:`torch.linalg.cholesky_ex` for a version of this operation that
  73. skips the (slow) error checking by default and instead returns the debug
  74. information. This makes it a faster way to check if a matrix is
  75. positive-definite.
  76. :func:`torch.linalg.eigh` for a different decomposition of a Hermitian matrix.
  77. The eigenvalue decomposition gives more information about the matrix but it
  78. slower to compute than the Cholesky decomposition.
  79. Args:
  80. A (Tensor): tensor of shape `(*, n, n)` where `*` is zero or more batch dimensions
  81. consisting of symmetric or Hermitian positive-definite matrices.
  82. Keyword args:
  83. upper (bool, optional): whether to return an upper triangular matrix.
  84. The tensor returned with upper=True is the conjugate transpose of the tensor
  85. returned with upper=False.
  86. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  87. Raises:
  88. RuntimeError: if the :attr:`A` matrix or any matrix in a batched :attr:`A` is not Hermitian
  89. (resp. symmetric) positive-definite. If :attr:`A` is a batch of matrices,
  90. the error message will include the batch index of the first matrix that fails
  91. to meet this condition.
  92. Examples::
  93. >>> A = torch.randn(2, 2, dtype=torch.complex128)
  94. >>> A = A @ A.T.conj() + torch.eye(2) # creates a Hermitian positive-definite matrix
  95. >>> A
  96. tensor([[2.5266+0.0000j, 1.9586-2.0626j],
  97. [1.9586+2.0626j, 9.4160+0.0000j]], dtype=torch.complex128)
  98. >>> L = torch.linalg.cholesky(A)
  99. >>> L
  100. tensor([[1.5895+0.0000j, 0.0000+0.0000j],
  101. [1.2322+1.2976j, 2.4928+0.0000j]], dtype=torch.complex128)
  102. >>> torch.dist(L @ L.T.conj(), A)
  103. tensor(4.4692e-16, dtype=torch.float64)
  104. >>> A = torch.randn(3, 2, 2, dtype=torch.float64)
  105. >>> A = A @ A.mT + torch.eye(2) # batch of symmetric positive-definite matrices
  106. >>> L = torch.linalg.cholesky(A)
  107. >>> torch.dist(L @ L.mT, A)
  108. tensor(5.8747e-16, dtype=torch.float64)
  109. """)
  110. cholesky_ex = _add_docstr(_linalg.linalg_cholesky_ex, r"""
  111. linalg.cholesky_ex(A, *, upper=False, check_errors=False, out=None) -> (Tensor, Tensor)
  112. Computes the Cholesky decomposition of a complex Hermitian or real
  113. symmetric positive-definite matrix.
  114. This function skips the (slow) error checking and error message construction
  115. of :func:`torch.linalg.cholesky`, instead directly returning the LAPACK
  116. error codes as part of a named tuple ``(L, info)``. This makes this function
  117. a faster way to check if a matrix is positive-definite, and it provides an
  118. opportunity to handle decomposition errors more gracefully or performantly
  119. than :func:`torch.linalg.cholesky` does.
  120. Supports input of float, double, cfloat and cdouble dtypes.
  121. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  122. the output has the same batch dimensions.
  123. If :attr:`A` is not a Hermitian positive-definite matrix, or if it's a batch of matrices
  124. and one or more of them is not a Hermitian positive-definite matrix,
  125. then ``info`` stores a positive integer for the corresponding matrix.
  126. The positive integer indicates the order of the leading minor that is not positive-definite,
  127. and the decomposition could not be completed.
  128. ``info`` filled with zeros indicates that the decomposition was successful.
  129. If ``check_errors=True`` and ``info`` contains positive integers, then a RuntimeError is thrown.
  130. """ + fr"""
  131. .. note:: {common_notes["sync_note_ex"]}
  132. .. warning:: {common_notes["experimental_warning"]}
  133. """ + r"""
  134. .. seealso::
  135. :func:`torch.linalg.cholesky` is a NumPy compatible variant that always checks for errors.
  136. Args:
  137. A (Tensor): the Hermitian `n \times n` matrix or the batch of such matrices of size
  138. `(*, n, n)` where `*` is one or more batch dimensions.
  139. Keyword args:
  140. upper (bool, optional): whether to return an upper triangular matrix.
  141. The tensor returned with upper=True is the conjugate transpose of the tensor
  142. returned with upper=False.
  143. check_errors (bool, optional): controls whether to check the content of ``infos``. Default: `False`.
  144. out (tuple, optional): tuple of two tensors to write the output to. Ignored if `None`. Default: `None`.
  145. Examples::
  146. >>> A = torch.randn(2, 2, dtype=torch.complex128)
  147. >>> A = A @ A.t().conj() # creates a Hermitian positive-definite matrix
  148. >>> L, info = torch.linalg.cholesky_ex(A)
  149. >>> A
  150. tensor([[ 2.3792+0.0000j, -0.9023+0.9831j],
  151. [-0.9023-0.9831j, 0.8757+0.0000j]], dtype=torch.complex128)
  152. >>> L
  153. tensor([[ 1.5425+0.0000j, 0.0000+0.0000j],
  154. [-0.5850-0.6374j, 0.3567+0.0000j]], dtype=torch.complex128)
  155. >>> info
  156. tensor(0, dtype=torch.int32)
  157. """)
  158. inv = _add_docstr(_linalg.linalg_inv, r"""
  159. linalg.inv(A, *, out=None) -> Tensor
  160. Computes the inverse of a square matrix if it exists.
  161. Throws a `RuntimeError` if the matrix is not invertible.
  162. Letting :math:`\mathbb{K}` be :math:`\mathbb{R}` or :math:`\mathbb{C}`,
  163. for a matrix :math:`A \in \mathbb{K}^{n \times n}`,
  164. its **inverse matrix** :math:`A^{-1} \in \mathbb{K}^{n \times n}` (if it exists) is defined as
  165. .. math::
  166. A^{-1}A = AA^{-1} = \mathrm{I}_n
  167. where :math:`\mathrm{I}_n` is the `n`-dimensional identity matrix.
  168. The inverse matrix exists if and only if :math:`A` is `invertible`_. In this case,
  169. the inverse is unique.
  170. Supports input of float, double, cfloat and cdouble dtypes.
  171. Also supports batches of matrices, and if :attr:`A` is a batch of matrices
  172. then the output has the same batch dimensions.
  173. """ + fr"""
  174. .. note:: {common_notes["sync_note"]}
  175. """ + r"""
  176. .. note::
  177. Consider using :func:`torch.linalg.solve` if possible for multiplying a matrix on the left by
  178. the inverse, as::
  179. linalg.solve(A, B) == linalg.inv(A) @ B # When B is a matrix
  180. It is always preferred to use :func:`~solve` when possible, as it is faster and more
  181. numerically stable than computing the inverse explicitly.
  182. .. seealso::
  183. :func:`torch.linalg.pinv` computes the pseudoinverse (Moore-Penrose inverse) of matrices
  184. of any shape.
  185. :func:`torch.linalg.solve` computes :attr:`A`\ `.inv() @ \ `:attr:`B` with a
  186. numerically stable algorithm.
  187. Args:
  188. A (Tensor): tensor of shape `(*, n, n)` where `*` is zero or more batch dimensions
  189. consisting of invertible matrices.
  190. Keyword args:
  191. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  192. Raises:
  193. RuntimeError: if the matrix :attr:`A` or any matrix in the batch of matrices :attr:`A` is not invertible.
  194. Examples::
  195. >>> A = torch.randn(4, 4)
  196. >>> Ainv = torch.linalg.inv(A)
  197. >>> torch.dist(A @ Ainv, torch.eye(4))
  198. tensor(1.1921e-07)
  199. >>> A = torch.randn(2, 3, 4, 4) # Batch of matrices
  200. >>> Ainv = torch.linalg.inv(A)
  201. >>> torch.dist(A @ Ainv, torch.eye(4))
  202. tensor(1.9073e-06)
  203. >>> A = torch.randn(4, 4, dtype=torch.complex128) # Complex matrix
  204. >>> Ainv = torch.linalg.inv(A)
  205. >>> torch.dist(A @ Ainv, torch.eye(4))
  206. tensor(7.5107e-16, dtype=torch.float64)
  207. .. _invertible:
  208. https://en.wikipedia.org/wiki/Invertible_matrix#The_invertible_matrix_theorem
  209. """)
  210. solve_ex = _add_docstr(_linalg.linalg_solve_ex, r"""
  211. linalg.solve_ex(A, B, *, left=True, check_errors=False, out=None) -> (Tensor, Tensor)
  212. A version of :func:`~solve` that does not perform error checks unless :attr:`check_errors`\ `= True`.
  213. It also returns the :attr:`info` tensor returned by `LAPACK's getrf`_.
  214. """ + fr"""
  215. .. note:: {common_notes["sync_note_ex"]}
  216. .. warning:: {common_notes["experimental_warning"]}
  217. """ + r"""
  218. Args:
  219. A (Tensor): tensor of shape `(*, n, n)` where `*` is zero or more batch dimensions.
  220. Keyword args:
  221. left (bool, optional): whether to solve the system :math:`AX=B` or :math:`XA = B`. Default: `True`.
  222. check_errors (bool, optional): controls whether to check the content of ``infos`` and raise
  223. an error if it is non-zero. Default: `False`.
  224. out (tuple, optional): tuple of two tensors to write the output to. Ignored if `None`. Default: `None`.
  225. Returns:
  226. A named tuple `(result, info)`.
  227. Examples::
  228. >>> A = torch.randn(3, 3)
  229. >>> Ainv, info = torch.linalg.solve_ex(A)
  230. >>> torch.dist(torch.linalg.inv(A), Ainv)
  231. tensor(0.)
  232. >>> info
  233. tensor(0, dtype=torch.int32)
  234. .. _LAPACK's getrf:
  235. https://www.netlib.org/lapack/explore-html/dd/d9a/group__double_g_ecomputational_ga0019443faea08275ca60a734d0593e60.html
  236. """)
  237. inv_ex = _add_docstr(_linalg.linalg_inv_ex, r"""
  238. linalg.inv_ex(A, *, check_errors=False, out=None) -> (Tensor, Tensor)
  239. Computes the inverse of a square matrix if it is invertible.
  240. Returns a namedtuple ``(inverse, info)``. ``inverse`` contains the result of
  241. inverting :attr:`A` and ``info`` stores the LAPACK error codes.
  242. If :attr:`A` is not an invertible matrix, or if it's a batch of matrices
  243. and one or more of them is not an invertible matrix,
  244. then ``info`` stores a positive integer for the corresponding matrix.
  245. The positive integer indicates the diagonal element of the LU decomposition of
  246. the input matrix that is exactly zero.
  247. ``info`` filled with zeros indicates that the inversion was successful.
  248. If ``check_errors=True`` and ``info`` contains positive integers, then a RuntimeError is thrown.
  249. Supports input of float, double, cfloat and cdouble dtypes.
  250. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  251. the output has the same batch dimensions.
  252. """ + fr"""
  253. .. note:: {common_notes["sync_note_ex"]}
  254. .. warning:: {common_notes["experimental_warning"]}
  255. """ + r"""
  256. .. seealso::
  257. :func:`torch.linalg.inv` is a NumPy compatible variant that always checks for errors.
  258. Args:
  259. A (Tensor): tensor of shape `(*, n, n)` where `*` is zero or more batch dimensions
  260. consisting of square matrices.
  261. check_errors (bool, optional): controls whether to check the content of ``info``. Default: `False`.
  262. Keyword args:
  263. out (tuple, optional): tuple of two tensors to write the output to. Ignored if `None`. Default: `None`.
  264. Examples::
  265. >>> A = torch.randn(3, 3)
  266. >>> Ainv, info = torch.linalg.inv_ex(A)
  267. >>> torch.dist(torch.linalg.inv(A), Ainv)
  268. tensor(0.)
  269. >>> info
  270. tensor(0, dtype=torch.int32)
  271. """)
  272. det = _add_docstr(_linalg.linalg_det, r"""
  273. linalg.det(A, *, out=None) -> Tensor
  274. Computes the determinant of a square matrix.
  275. Supports input of float, double, cfloat and cdouble dtypes.
  276. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  277. the output has the same batch dimensions.
  278. .. seealso::
  279. :func:`torch.linalg.slogdet` computes the sign and natural logarithm of the absolute
  280. value of the determinant of square matrices.
  281. Args:
  282. A (Tensor): tensor of shape `(*, n, n)` where `*` is zero or more batch dimensions.
  283. Keyword args:
  284. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  285. Examples::
  286. >>> A = torch.randn(3, 3)
  287. >>> torch.linalg.det(A)
  288. tensor(0.0934)
  289. >>> A = torch.randn(3, 2, 2)
  290. >>> torch.linalg.det(A)
  291. tensor([1.1990, 0.4099, 0.7386])
  292. """)
  293. slogdet = _add_docstr(_linalg.linalg_slogdet, r"""
  294. linalg.slogdet(A, *, out=None) -> (Tensor, Tensor)
  295. Computes the sign and natural logarithm of the absolute value of the determinant of a square matrix.
  296. For complex :attr:`A`, it returns the sign and the natural logarithm of the modulus of the
  297. determinant, that is, a logarithmic polar decomposition of the determinant.
  298. The determinant can be recovered as `sign * exp(logabsdet)`.
  299. When a matrix has a determinant of zero, it returns `(0, -inf)`.
  300. Supports input of float, double, cfloat and cdouble dtypes.
  301. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  302. the output has the same batch dimensions.
  303. .. seealso::
  304. :func:`torch.linalg.det` computes the determinant of square matrices.
  305. Args:
  306. A (Tensor): tensor of shape `(*, n, n)` where `*` is zero or more batch dimensions.
  307. Keyword args:
  308. out (tuple, optional): output tuple of two tensors. Ignored if `None`. Default: `None`.
  309. Returns:
  310. A named tuple `(sign, logabsdet)`.
  311. `sign` will have the same dtype as :attr:`A`.
  312. `logabsdet` will always be real-valued, even when :attr:`A` is complex.
  313. Examples::
  314. >>> A = torch.randn(3, 3)
  315. >>> A
  316. tensor([[ 0.0032, -0.2239, -1.1219],
  317. [-0.6690, 0.1161, 0.4053],
  318. [-1.6218, -0.9273, -0.0082]])
  319. >>> torch.linalg.det(A)
  320. tensor(-0.7576)
  321. >>> torch.logdet(A)
  322. tensor(nan)
  323. >>> torch.linalg.slogdet(A)
  324. torch.return_types.linalg_slogdet(sign=tensor(-1.), logabsdet=tensor(-0.2776))
  325. """)
  326. eig = _add_docstr(_linalg.linalg_eig, r"""
  327. linalg.eig(A, *, out=None) -> (Tensor, Tensor)
  328. Computes the eigenvalue decomposition of a square matrix if it exists.
  329. Letting :math:`\mathbb{K}` be :math:`\mathbb{R}` or :math:`\mathbb{C}`,
  330. the **eigenvalue decomposition** of a square matrix
  331. :math:`A \in \mathbb{K}^{n \times n}` (if it exists) is defined as
  332. .. math::
  333. A = V \operatorname{diag}(\Lambda) V^{-1}\mathrlap{\qquad V \in \mathbb{C}^{n \times n}, \Lambda \in \mathbb{C}^n}
  334. This decomposition exists if and only if :math:`A` is `diagonalizable`_.
  335. This is the case when all its eigenvalues are different.
  336. Supports input of float, double, cfloat and cdouble dtypes.
  337. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  338. the output has the same batch dimensions.
  339. .. note:: The eigenvalues and eigenvectors of a real matrix may be complex.
  340. """ + fr"""
  341. .. note:: {common_notes["sync_note"]}
  342. """ + r"""
  343. .. warning:: This function assumes that :attr:`A` is `diagonalizable`_ (for example, when all the
  344. eigenvalues are different). If it is not diagonalizable, the returned
  345. eigenvalues will be correct but :math:`A \neq V \operatorname{diag}(\Lambda)V^{-1}`.
  346. .. warning:: The returned eigenvectors are normalized to have norm `1`.
  347. Even then, the eigenvectors of a matrix are not unique, nor are they continuous with respect to
  348. :attr:`A`. Due to this lack of uniqueness, different hardware and software may compute
  349. different eigenvectors.
  350. This non-uniqueness is caused by the fact that multiplying an eigenvector by
  351. by :math:`e^{i \phi}, \phi \in \mathbb{R}` produces another set of valid eigenvectors
  352. of the matrix. For this reason, the loss function shall not depend on the phase of the
  353. eigenvectors, as this quantity is not well-defined.
  354. This is checked when computing the gradients of this function. As such,
  355. when inputs are on a CUDA device, this function synchronizes that device with the CPU
  356. when computing the gradients.
  357. This is checked when computing the gradients of this function. As such,
  358. when inputs are on a CUDA device, the computation of the gradients
  359. of this function synchronizes that device with the CPU.
  360. .. warning:: Gradients computed using the `eigenvectors` tensor will only be finite when
  361. :attr:`A` has distinct eigenvalues.
  362. Furthermore, if the distance between any two eigenvalues is close to zero,
  363. the gradient will be numerically unstable, as it depends on the eigenvalues
  364. :math:`\lambda_i` through the computation of
  365. :math:`\frac{1}{\min_{i \neq j} \lambda_i - \lambda_j}`.
  366. .. seealso::
  367. :func:`torch.linalg.eigvals` computes only the eigenvalues.
  368. Unlike :func:`torch.linalg.eig`, the gradients of :func:`~eigvals` are always
  369. numerically stable.
  370. :func:`torch.linalg.eigh` for a (faster) function that computes the eigenvalue decomposition
  371. for Hermitian and symmetric matrices.
  372. :func:`torch.linalg.svd` for a function that computes another type of spectral
  373. decomposition that works on matrices of any shape.
  374. :func:`torch.linalg.qr` for another (much faster) decomposition that works on matrices of
  375. any shape.
  376. Args:
  377. A (Tensor): tensor of shape `(*, n, n)` where `*` is zero or more batch dimensions
  378. consisting of diagonalizable matrices.
  379. Keyword args:
  380. out (tuple, optional): output tuple of two tensors. Ignored if `None`. Default: `None`.
  381. Returns:
  382. A named tuple `(eigenvalues, eigenvectors)` which corresponds to :math:`\Lambda` and :math:`V` above.
  383. `eigenvalues` and `eigenvectors` will always be complex-valued, even when :attr:`A` is real. The eigenvectors
  384. will be given by the columns of `eigenvectors`.
  385. Examples::
  386. >>> A = torch.randn(2, 2, dtype=torch.complex128)
  387. >>> A
  388. tensor([[ 0.9828+0.3889j, -0.4617+0.3010j],
  389. [ 0.1662-0.7435j, -0.6139+0.0562j]], dtype=torch.complex128)
  390. >>> L, V = torch.linalg.eig(A)
  391. >>> L
  392. tensor([ 1.1226+0.5738j, -0.7537-0.1286j], dtype=torch.complex128)
  393. >>> V
  394. tensor([[ 0.9218+0.0000j, 0.1882-0.2220j],
  395. [-0.0270-0.3867j, 0.9567+0.0000j]], dtype=torch.complex128)
  396. >>> torch.dist(V @ torch.diag(L) @ torch.linalg.inv(V), A)
  397. tensor(7.7119e-16, dtype=torch.float64)
  398. >>> A = torch.randn(3, 2, 2, dtype=torch.float64)
  399. >>> L, V = torch.linalg.eig(A)
  400. >>> torch.dist(V @ torch.diag_embed(L) @ torch.linalg.inv(V), A)
  401. tensor(3.2841e-16, dtype=torch.float64)
  402. .. _diagonalizable:
  403. https://en.wikipedia.org/wiki/Diagonalizable_matrix#Definition
  404. """)
  405. eigvals = _add_docstr(_linalg.linalg_eigvals, r"""
  406. linalg.eigvals(A, *, out=None) -> Tensor
  407. Computes the eigenvalues of a square matrix.
  408. Letting :math:`\mathbb{K}` be :math:`\mathbb{R}` or :math:`\mathbb{C}`,
  409. the **eigenvalues** of a square matrix :math:`A \in \mathbb{K}^{n \times n}` are defined
  410. as the roots (counted with multiplicity) of the polynomial `p` of degree `n` given by
  411. .. math::
  412. p(\lambda) = \operatorname{det}(A - \lambda \mathrm{I}_n)\mathrlap{\qquad \lambda \in \mathbb{C}}
  413. where :math:`\mathrm{I}_n` is the `n`-dimensional identity matrix.
  414. Supports input of float, double, cfloat and cdouble dtypes.
  415. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  416. the output has the same batch dimensions.
  417. .. note:: The eigenvalues of a real matrix may be complex, as the roots of a real polynomial may be complex.
  418. The eigenvalues of a matrix are always well-defined, even when the matrix is not diagonalizable.
  419. """ + fr"""
  420. .. note:: {common_notes["sync_note"]}
  421. """ + r"""
  422. .. seealso::
  423. :func:`torch.linalg.eig` computes the full eigenvalue decomposition.
  424. Args:
  425. A (Tensor): tensor of shape `(*, n, n)` where `*` is zero or more batch dimensions.
  426. Keyword args:
  427. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  428. Returns:
  429. A complex-valued tensor containing the eigenvalues even when :attr:`A` is real.
  430. Examples::
  431. >>> A = torch.randn(2, 2, dtype=torch.complex128)
  432. >>> L = torch.linalg.eigvals(A)
  433. >>> L
  434. tensor([ 1.1226+0.5738j, -0.7537-0.1286j], dtype=torch.complex128)
  435. >>> torch.dist(L, torch.linalg.eig(A).eigenvalues)
  436. tensor(2.4576e-07)
  437. """)
  438. eigh = _add_docstr(_linalg.linalg_eigh, r"""
  439. linalg.eigh(A, UPLO='L', *, out=None) -> (Tensor, Tensor)
  440. Computes the eigenvalue decomposition of a complex Hermitian or real symmetric matrix.
  441. Letting :math:`\mathbb{K}` be :math:`\mathbb{R}` or :math:`\mathbb{C}`,
  442. the **eigenvalue decomposition** of a complex Hermitian or real symmetric matrix
  443. :math:`A \in \mathbb{K}^{n \times n}` is defined as
  444. .. math::
  445. A = Q \operatorname{diag}(\Lambda) Q^{\text{H}}\mathrlap{\qquad Q \in \mathbb{K}^{n \times n}, \Lambda \in \mathbb{R}^n}
  446. where :math:`Q^{\text{H}}` is the conjugate transpose when :math:`Q` is complex, and the transpose when :math:`Q` is real-valued.
  447. :math:`Q` is orthogonal in the real case and unitary in the complex case.
  448. Supports input of float, double, cfloat and cdouble dtypes.
  449. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  450. the output has the same batch dimensions.
  451. :attr:`A` is assumed to be Hermitian (resp. symmetric), but this is not checked internally, instead:
  452. - If :attr:`UPLO`\ `= 'L'` (default), only the lower triangular part of the matrix is used in the computation.
  453. - If :attr:`UPLO`\ `= 'U'`, only the upper triangular part of the matrix is used.
  454. The eigenvalues are returned in ascending order.
  455. """ + fr"""
  456. .. note:: {common_notes["sync_note"]}
  457. """ + r"""
  458. .. note:: The eigenvalues of real symmetric or complex Hermitian matrices are always real.
  459. .. warning:: The eigenvectors of a symmetric matrix are not unique, nor are they continuous with
  460. respect to :attr:`A`. Due to this lack of uniqueness, different hardware and
  461. software may compute different eigenvectors.
  462. This non-uniqueness is caused by the fact that multiplying an eigenvector by
  463. `-1` in the real case or by :math:`e^{i \phi}, \phi \in \mathbb{R}` in the complex
  464. case produces another set of valid eigenvectors of the matrix.
  465. For this reason, the loss function shall not depend on the phase of the eigenvectors, as
  466. this quantity is not well-defined.
  467. This is checked for complex inputs when computing the gradients of this function. As such,
  468. when inputs are complex and are on a CUDA device, the computation of the gradients
  469. of this function synchronizes that device with the CPU.
  470. .. warning:: Gradients computed using the `eigenvectors` tensor will only be finite when
  471. :attr:`A` has distinct eigenvalues.
  472. Furthermore, if the distance between any two eigenvalues is close to zero,
  473. the gradient will be numerically unstable, as it depends on the eigenvalues
  474. :math:`\lambda_i` through the computation of
  475. :math:`\frac{1}{\min_{i \neq j} \lambda_i - \lambda_j}`.
  476. .. seealso::
  477. :func:`torch.linalg.eigvalsh` computes only the eigenvalues of a Hermitian matrix.
  478. Unlike :func:`torch.linalg.eigh`, the gradients of :func:`~eigvalsh` are always
  479. numerically stable.
  480. :func:`torch.linalg.cholesky` for a different decomposition of a Hermitian matrix.
  481. The Cholesky decomposition gives less information about the matrix but is much faster
  482. to compute than the eigenvalue decomposition.
  483. :func:`torch.linalg.eig` for a (slower) function that computes the eigenvalue decomposition
  484. of a not necessarily Hermitian square matrix.
  485. :func:`torch.linalg.svd` for a (slower) function that computes the more general SVD
  486. decomposition of matrices of any shape.
  487. :func:`torch.linalg.qr` for another (much faster) decomposition that works on general
  488. matrices.
  489. Args:
  490. A (Tensor): tensor of shape `(*, n, n)` where `*` is zero or more batch dimensions
  491. consisting of symmetric or Hermitian matrices.
  492. UPLO ('L', 'U', optional): controls whether to use the upper or lower triangular part
  493. of :attr:`A` in the computations. Default: `'L'`.
  494. Keyword args:
  495. out (tuple, optional): output tuple of two tensors. Ignored if `None`. Default: `None`.
  496. Returns:
  497. A named tuple `(eigenvalues, eigenvectors)` which corresponds to :math:`\Lambda` and :math:`Q` above.
  498. `eigenvalues` will always be real-valued, even when :attr:`A` is complex.
  499. It will also be ordered in ascending order.
  500. `eigenvectors` will have the same dtype as :attr:`A` and will contain the eigenvectors as its columns.
  501. Examples::
  502. >>> A = torch.randn(2, 2, dtype=torch.complex128)
  503. >>> A = A + A.T.conj() # creates a Hermitian matrix
  504. >>> A
  505. tensor([[2.9228+0.0000j, 0.2029-0.0862j],
  506. [0.2029+0.0862j, 0.3464+0.0000j]], dtype=torch.complex128)
  507. >>> L, Q = torch.linalg.eigh(A)
  508. >>> L
  509. tensor([0.3277, 2.9415], dtype=torch.float64)
  510. >>> Q
  511. tensor([[-0.0846+-0.0000j, -0.9964+0.0000j],
  512. [ 0.9170+0.3898j, -0.0779-0.0331j]], dtype=torch.complex128)
  513. >>> torch.dist(Q @ torch.diag(L.cdouble()) @ Q.T.conj(), A)
  514. tensor(6.1062e-16, dtype=torch.float64)
  515. >>> A = torch.randn(3, 2, 2, dtype=torch.float64)
  516. >>> A = A + A.mT # creates a batch of symmetric matrices
  517. >>> L, Q = torch.linalg.eigh(A)
  518. >>> torch.dist(Q @ torch.diag_embed(L) @ Q.mH, A)
  519. tensor(1.5423e-15, dtype=torch.float64)
  520. """)
  521. eigvalsh = _add_docstr(_linalg.linalg_eigvalsh, r"""
  522. linalg.eigvalsh(A, UPLO='L', *, out=None) -> Tensor
  523. Computes the eigenvalues of a complex Hermitian or real symmetric matrix.
  524. Letting :math:`\mathbb{K}` be :math:`\mathbb{R}` or :math:`\mathbb{C}`,
  525. the **eigenvalues** of a complex Hermitian or real symmetric matrix :math:`A \in \mathbb{K}^{n \times n}`
  526. are defined as the roots (counted with multiplicity) of the polynomial `p` of degree `n` given by
  527. .. math::
  528. p(\lambda) = \operatorname{det}(A - \lambda \mathrm{I}_n)\mathrlap{\qquad \lambda \in \mathbb{R}}
  529. where :math:`\mathrm{I}_n` is the `n`-dimensional identity matrix.
  530. The eigenvalues of a real symmetric or complex Hermitian matrix are always real.
  531. Supports input of float, double, cfloat and cdouble dtypes.
  532. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  533. the output has the same batch dimensions.
  534. The eigenvalues are returned in ascending order.
  535. :attr:`A` is assumed to be Hermitian (resp. symmetric), but this is not checked internally, instead:
  536. - If :attr:`UPLO`\ `= 'L'` (default), only the lower triangular part of the matrix is used in the computation.
  537. - If :attr:`UPLO`\ `= 'U'`, only the upper triangular part of the matrix is used.
  538. """ + fr"""
  539. .. note:: {common_notes["sync_note"]}
  540. """ + r"""
  541. .. seealso::
  542. :func:`torch.linalg.eigh` computes the full eigenvalue decomposition.
  543. Args:
  544. A (Tensor): tensor of shape `(*, n, n)` where `*` is zero or more batch dimensions
  545. consisting of symmetric or Hermitian matrices.
  546. UPLO ('L', 'U', optional): controls whether to use the upper or lower triangular part
  547. of :attr:`A` in the computations. Default: `'L'`.
  548. Keyword args:
  549. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  550. Returns:
  551. A real-valued tensor containing the eigenvalues even when :attr:`A` is complex.
  552. The eigenvalues are returned in ascending order.
  553. Examples::
  554. >>> A = torch.randn(2, 2, dtype=torch.complex128)
  555. >>> A = A + A.T.conj() # creates a Hermitian matrix
  556. >>> A
  557. tensor([[2.9228+0.0000j, 0.2029-0.0862j],
  558. [0.2029+0.0862j, 0.3464+0.0000j]], dtype=torch.complex128)
  559. >>> torch.linalg.eigvalsh(A)
  560. tensor([0.3277, 2.9415], dtype=torch.float64)
  561. >>> A = torch.randn(3, 2, 2, dtype=torch.float64)
  562. >>> A = A + A.mT # creates a batch of symmetric matrices
  563. >>> torch.linalg.eigvalsh(A)
  564. tensor([[ 2.5797, 3.4629],
  565. [-4.1605, 1.3780],
  566. [-3.1113, 2.7381]], dtype=torch.float64)
  567. """)
  568. householder_product = _add_docstr(_linalg.linalg_householder_product, r"""
  569. householder_product(A, tau, *, out=None) -> Tensor
  570. Computes the first `n` columns of a product of Householder matrices.
  571. Let :math:`\mathbb{K}` be :math:`\mathbb{R}` or :math:`\mathbb{C}`, and
  572. let :math:`V \in \mathbb{K}^{m \times n}` be a matrix with columns :math:`v_i \in \mathbb{K}^m`
  573. for :math:`i=1,\ldots,m` with :math:`m \geq n`. Denote by :math:`w_i` the vector resulting from
  574. zeroing out the first :math:`i-1` components of :math:`v_i` and setting to `1` the :math:`i`-th.
  575. For a vector :math:`\tau \in \mathbb{K}^k` with :math:`k \leq n`, this function computes the
  576. first :math:`n` columns of the matrix
  577. .. math::
  578. H_1H_2 ... H_k \qquad\text{with}\qquad H_i = \mathrm{I}_m - \tau_i w_i w_i^{\text{H}}
  579. where :math:`\mathrm{I}_m` is the `m`-dimensional identity matrix and :math:`w^{\text{H}}` is the
  580. conjugate transpose when :math:`w` is complex, and the transpose when :math:`w` is real-valued.
  581. The output matrix is the same size as the input matrix :attr:`A`.
  582. See `Representation of Orthogonal or Unitary Matrices`_ for further details.
  583. Supports inputs of float, double, cfloat and cdouble dtypes.
  584. Also supports batches of matrices, and if the inputs are batches of matrices then
  585. the output has the same batch dimensions.
  586. .. seealso::
  587. :func:`torch.geqrf` can be used together with this function to form the `Q` from the
  588. :func:`~qr` decomposition.
  589. :func:`torch.ormqr` is a related function that computes the matrix multiplication
  590. of a product of Householder matrices with another matrix.
  591. However, that function is not supported by autograd.
  592. .. warning::
  593. Gradient computations are only well-defined if :math:`tau_i \neq \frac{1}{||v_i||^2}`.
  594. If this condition is not met, no error will be thrown, but the gradient produced may contain `NaN`.
  595. Args:
  596. A (Tensor): tensor of shape `(*, m, n)` where `*` is zero or more batch dimensions.
  597. tau (Tensor): tensor of shape `(*, k)` where `*` is zero or more batch dimensions.
  598. Keyword args:
  599. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  600. Raises:
  601. RuntimeError: if :attr:`A` doesn't satisfy the requirement `m >= n`,
  602. or :attr:`tau` doesn't satisfy the requirement `n >= k`.
  603. Examples::
  604. >>> A = torch.randn(2, 2)
  605. >>> h, tau = torch.geqrf(A)
  606. >>> Q = torch.linalg.householder_product(h, tau)
  607. >>> torch.dist(Q, torch.linalg.qr(A).Q)
  608. tensor(0.)
  609. >>> h = torch.randn(3, 2, 2, dtype=torch.complex128)
  610. >>> tau = torch.randn(3, 1, dtype=torch.complex128)
  611. >>> Q = torch.linalg.householder_product(h, tau)
  612. >>> Q
  613. tensor([[[ 1.8034+0.4184j, 0.2588-1.0174j],
  614. [-0.6853+0.7953j, 2.0790+0.5620j]],
  615. [[ 1.4581+1.6989j, -1.5360+0.1193j],
  616. [ 1.3877-0.6691j, 1.3512+1.3024j]],
  617. [[ 1.4766+0.5783j, 0.0361+0.6587j],
  618. [ 0.6396+0.1612j, 1.3693+0.4481j]]], dtype=torch.complex128)
  619. .. _Representation of Orthogonal or Unitary Matrices:
  620. https://www.netlib.org/lapack/lug/node128.html
  621. """)
  622. ldl_factor = _add_docstr(_linalg.linalg_ldl_factor, r"""
  623. linalg.ldl_factor(A, *, hermitian=False, out=None) -> (Tensor, Tensor)
  624. Computes a compact representation of the LDL factorization of a Hermitian or symmetric (possibly indefinite) matrix.
  625. When :attr:`A` is complex valued it can be Hermitian (:attr:`hermitian`\ `= True`)
  626. or symmetric (:attr:`hermitian`\ `= False`).
  627. The factorization is of the form the form :math:`A = L D L^T`.
  628. If :attr:`hermitian` is `True` then transpose operation is the conjugate transpose.
  629. :math:`L` (or :math:`U`) and :math:`D` are stored in compact form in ``LD``.
  630. They follow the format specified by `LAPACK's sytrf`_ function.
  631. These tensors may be used in :func:`torch.linalg.ldl_solve` to solve linear systems.
  632. Supports input of float, double, cfloat and cdouble dtypes.
  633. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  634. the output has the same batch dimensions.
  635. """ + fr"""
  636. .. note:: {common_notes["sync_note_has_ex"].format("torch.linalg.ldl_factor_ex")}
  637. """ + r"""
  638. Args:
  639. A (Tensor): tensor of shape (*, n, n) where * is zero or more batch dimensions consisting of symmetric or Hermitian matrices.
  640. `(*, n, n)` where `*` is one or more batch dimensions.
  641. Keyword args:
  642. hermitian (bool, optional): whether to consider the input to be Hermitian or symmetric.
  643. For real-valued matrices, this switch has no effect. Default: `False`.
  644. out (tuple, optional): tuple of two tensors to write the output to. Ignored if `None`. Default: `None`.
  645. Returns:
  646. A named tuple `(LD, pivots)`.
  647. Examples::
  648. >>> A = torch.randn(3, 3)
  649. >>> A = A @ A.mT # make symmetric
  650. >>> A
  651. tensor([[7.2079, 4.2414, 1.9428],
  652. [4.2414, 3.4554, 0.3264],
  653. [1.9428, 0.3264, 1.3823]])
  654. >>> LD, pivots = torch.linalg.ldl_factor(A)
  655. >>> LD
  656. tensor([[ 7.2079, 0.0000, 0.0000],
  657. [ 0.5884, 0.9595, 0.0000],
  658. [ 0.2695, -0.8513, 0.1633]])
  659. >>> pivots
  660. tensor([1, 2, 3], dtype=torch.int32)
  661. .. _LAPACK's sytrf:
  662. https://www.netlib.org/lapack/explore-html/d3/db6/group__double_s_ycomputational_gad91bde1212277b3e909eb6af7f64858a.html
  663. """)
  664. ldl_factor_ex = _add_docstr(_linalg.linalg_ldl_factor_ex, r"""
  665. linalg.ldl_factor_ex(A, *, hermitian=False, check_errors=False, out=None) -> (Tensor, Tensor, Tensor)
  666. This is a version of :func:`~ldl_factor` that does not perform error checks unless :attr:`check_errors`\ `= True`.
  667. It also returns the :attr:`info` tensor returned by `LAPACK's sytrf`_.
  668. ``info`` stores integer error codes from the backend library.
  669. A positive integer indicates the diagonal element of :math:`D` that is zero.
  670. Division by 0 will occur if the result is used for solving a system of linear equations.
  671. ``info`` filled with zeros indicates that the factorization was successful.
  672. If ``check_errors=True`` and ``info`` contains positive integers, then a `RuntimeError` is thrown.
  673. """ + fr"""
  674. .. note:: {common_notes["sync_note_ex"]}
  675. .. warning:: {common_notes["experimental_warning"]}
  676. """ + r"""
  677. Args:
  678. A (Tensor): tensor of shape (*, n, n) where * is zero or more batch dimensions consisting of symmetric or Hermitian matrices.
  679. `(*, n, n)` where `*` is one or more batch dimensions.
  680. Keyword args:
  681. hermitian (bool, optional): whether to consider the input to be Hermitian or symmetric.
  682. For real-valued matrices, this switch has no effect. Default: `False`.
  683. check_errors (bool, optional): controls whether to check the content of ``info`` and raise
  684. an error if it is non-zero. Default: `False`.
  685. out (tuple, optional): tuple of three tensors to write the output to. Ignored if `None`. Default: `None`.
  686. Returns:
  687. A named tuple `(LD, pivots, info)`.
  688. Examples::
  689. >>> A = torch.randn(3, 3)
  690. >>> A = A @ A.mT # make symmetric
  691. >>> A
  692. tensor([[7.2079, 4.2414, 1.9428],
  693. [4.2414, 3.4554, 0.3264],
  694. [1.9428, 0.3264, 1.3823]])
  695. >>> LD, pivots, info = torch.linalg.ldl_factor_ex(A)
  696. >>> LD
  697. tensor([[ 7.2079, 0.0000, 0.0000],
  698. [ 0.5884, 0.9595, 0.0000],
  699. [ 0.2695, -0.8513, 0.1633]])
  700. >>> pivots
  701. tensor([1, 2, 3], dtype=torch.int32)
  702. >>> info
  703. tensor(0, dtype=torch.int32)
  704. .. _LAPACK's sytrf:
  705. https://www.netlib.org/lapack/explore-html/d3/db6/group__double_s_ycomputational_gad91bde1212277b3e909eb6af7f64858a.html
  706. """)
  707. ldl_solve = _add_docstr(_linalg.linalg_ldl_solve, r"""
  708. linalg.ldl_solve(LD, pivots, B, *, hermitian=False, out=None) -> Tensor
  709. Computes the solution of a system of linear equations using the LDL factorization.
  710. :attr:`LD` and :attr:`pivots` are the compact representation of the LDL factorization and
  711. are expected to be computed by :func:`torch.linalg.ldl_factor_ex`.
  712. :attr:`hermitian` argument to this function should be the same
  713. as the corresponding arguments in :func:`torch.linalg.ldl_factor_ex`.
  714. Supports input of float, double, cfloat and cdouble dtypes.
  715. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  716. the output has the same batch dimensions.
  717. """ + fr"""
  718. .. warning:: {common_notes["experimental_warning"]}
  719. """ + r"""
  720. Args:
  721. LD (Tensor): the `n \times n` matrix or the batch of such matrices of size
  722. `(*, n, n)` where `*` is one or more batch dimensions.
  723. pivots (Tensor): the pivots corresponding to the LDL factorization of :attr:`LD`.
  724. B (Tensor): right-hand side tensor of shape `(*, n, k)`.
  725. Keyword args:
  726. hermitian (bool, optional): whether to consider the decomposed matrix to be Hermitian or symmetric.
  727. For real-valued matrices, this switch has no effect. Default: `False`.
  728. out (tuple, optional): output tensor. `B` may be passed as `out` and the result is computed in-place on `B`.
  729. Ignored if `None`. Default: `None`.
  730. Examples::
  731. >>> A = torch.randn(2, 3, 3)
  732. >>> A = A @ A.mT # make symmetric
  733. >>> LD, pivots, info = torch.linalg.ldl_factor_ex(A)
  734. >>> B = torch.randn(2, 3, 4)
  735. >>> X = torch.linalg.ldl_solve(LD, pivots, B)
  736. >>> torch.linalg.norm(A @ X - B)
  737. >>> tensor(0.0001)
  738. """)
  739. lstsq = _add_docstr(_linalg.linalg_lstsq, r"""
  740. torch.linalg.lstsq(A, B, rcond=None, *, driver=None) -> (Tensor, Tensor, Tensor, Tensor)
  741. Computes a solution to the least squares problem of a system of linear equations.
  742. Letting :math:`\mathbb{K}` be :math:`\mathbb{R}` or :math:`\mathbb{C}`,
  743. the **least squares problem** for a linear system :math:`AX = B` with
  744. :math:`A \in \mathbb{K}^{m \times n}, B \in \mathbb{K}^{m \times k}` is defined as
  745. .. math::
  746. \min_{X \in \mathbb{K}^{n \times k}} \|AX - B\|_F
  747. where :math:`\|-\|_F` denotes the Frobenius norm.
  748. Supports inputs of float, double, cfloat and cdouble dtypes.
  749. Also supports batches of matrices, and if the inputs are batches of matrices then
  750. the output has the same batch dimensions.
  751. :attr:`driver` chooses the backend function that will be used.
  752. For CPU inputs the valid values are `'gels'`, `'gelsy'`, `'gelsd`, `'gelss'`.
  753. To choose the best driver on CPU consider:
  754. - If :attr:`A` is well-conditioned (its `condition number`_ is not too large), or you do not mind some precision loss.
  755. - For a general matrix: `'gelsy'` (QR with pivoting) (default)
  756. - If :attr:`A` is full-rank: `'gels'` (QR)
  757. - If :attr:`A` is not well-conditioned.
  758. - `'gelsd'` (tridiagonal reduction and SVD)
  759. - But if you run into memory issues: `'gelss'` (full SVD).
  760. For CUDA input, the only valid driver is `'gels'`, which assumes that :attr:`A` is full-rank.
  761. See also the `full description of these drivers`_
  762. :attr:`rcond` is used to determine the effective rank of the matrices in :attr:`A`
  763. when :attr:`driver` is one of (`'gelsy'`, `'gelsd'`, `'gelss'`).
  764. In this case, if :math:`\sigma_i` are the singular values of `A` in decreasing order,
  765. :math:`\sigma_i` will be rounded down to zero if :math:`\sigma_i \leq \text{rcond} \cdot \sigma_1`.
  766. If :attr:`rcond`\ `= None` (default), :attr:`rcond` is set to the machine precision of the dtype of :attr:`A` times `max(m, n)`.
  767. This function returns the solution to the problem and some extra information in a named tuple of
  768. four tensors `(solution, residuals, rank, singular_values)`. For inputs :attr:`A`, :attr:`B`
  769. of shape `(*, m, n)`, `(*, m, k)` respectively, it contains
  770. - `solution`: the least squares solution. It has shape `(*, n, k)`.
  771. - `residuals`: the squared residuals of the solutions, that is, :math:`\|AX - B\|_F^2`.
  772. It has shape equal to the batch dimensions of :attr:`A`.
  773. It is computed when `m > n` and every matrix in :attr:`A` is full-rank,
  774. otherwise, it is an empty tensor.
  775. If :attr:`A` is a batch of matrices and any matrix in the batch is not full rank,
  776. then an empty tensor is returned. This behavior may change in a future PyTorch release.
  777. - `rank`: tensor of ranks of the matrices in :attr:`A`.
  778. It has shape equal to the batch dimensions of :attr:`A`.
  779. It is computed when :attr:`driver` is one of (`'gelsy'`, `'gelsd'`, `'gelss'`),
  780. otherwise it is an empty tensor.
  781. - `singular_values`: tensor of singular values of the matrices in :attr:`A`.
  782. It has shape `(*, min(m, n))`.
  783. It is computed when :attr:`driver` is one of (`'gelsd'`, `'gelss'`),
  784. otherwise it is an empty tensor.
  785. .. note::
  786. This function computes `X = \ `:attr:`A`\ `.pinverse() @ \ `:attr:`B` in a faster and
  787. more numerically stable way than performing the computations separately.
  788. .. warning::
  789. The default value of :attr:`rcond` may change in a future PyTorch release.
  790. It is therefore recommended to use a fixed value to avoid potential
  791. breaking changes.
  792. Args:
  793. A (Tensor): lhs tensor of shape `(*, m, n)` where `*` is zero or more batch dimensions.
  794. B (Tensor): rhs tensor of shape `(*, m, k)` where `*` is zero or more batch dimensions.
  795. rcond (float, optional): used to determine the effective rank of :attr:`A`.
  796. If :attr:`rcond`\ `= None`, :attr:`rcond` is set to the machine
  797. precision of the dtype of :attr:`A` times `max(m, n)`. Default: `None`.
  798. Keyword args:
  799. driver (str, optional): name of the LAPACK/MAGMA method to be used.
  800. If `None`, `'gelsy'` is used for CPU inputs and `'gels'` for CUDA inputs.
  801. Default: `None`.
  802. Returns:
  803. A named tuple `(solution, residuals, rank, singular_values)`.
  804. Examples::
  805. >>> A = torch.randn(1,3,3)
  806. >>> A
  807. tensor([[[-1.0838, 0.0225, 0.2275],
  808. [ 0.2438, 0.3844, 0.5499],
  809. [ 0.1175, -0.9102, 2.0870]]])
  810. >>> B = torch.randn(2,3,3)
  811. >>> B
  812. tensor([[[-0.6772, 0.7758, 0.5109],
  813. [-1.4382, 1.3769, 1.1818],
  814. [-0.3450, 0.0806, 0.3967]],
  815. [[-1.3994, -0.1521, -0.1473],
  816. [ 1.9194, 1.0458, 0.6705],
  817. [-1.1802, -0.9796, 1.4086]]])
  818. >>> X = torch.linalg.lstsq(A, B).solution # A is broadcasted to shape (2, 3, 3)
  819. >>> torch.dist(X, torch.linalg.pinv(A) @ B)
  820. tensor(1.5152e-06)
  821. >>> S = torch.linalg.lstsq(A, B, driver='gelsd').singular_values
  822. >>> torch.dist(S, torch.linalg.svdvals(A))
  823. tensor(2.3842e-07)
  824. >>> A[:, 0].zero_() # Decrease the rank of A
  825. >>> rank = torch.linalg.lstsq(A, B).rank
  826. >>> rank
  827. tensor([2])
  828. .. _condition number:
  829. https://pytorch.org/docs/master/linalg.html#torch.linalg.cond
  830. .. _full description of these drivers:
  831. https://www.netlib.org/lapack/lug/node27.html
  832. """)
  833. matrix_power = _add_docstr(_linalg.linalg_matrix_power, r"""
  834. matrix_power(A, n, *, out=None) -> Tensor
  835. Computes the `n`-th power of a square matrix for an integer `n`.
  836. Supports input of float, double, cfloat and cdouble dtypes.
  837. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  838. the output has the same batch dimensions.
  839. If :attr:`n`\ `= 0`, it returns the identity matrix (or batch) of the same shape
  840. as :attr:`A`. If :attr:`n` is negative, it returns the inverse of each matrix
  841. (if invertible) raised to the power of `abs(n)`.
  842. .. note::
  843. Consider using :func:`torch.linalg.solve` if possible for multiplying a matrix on the left by
  844. a negative power as, if :attr:`n`\ `> 0`::
  845. matrix_power(torch.linalg.solve(A, B), n) == matrix_power(A, -n) @ B
  846. It is always preferred to use :func:`~solve` when possible, as it is faster and more
  847. numerically stable than computing :math:`A^{-n}` explicitly.
  848. .. seealso::
  849. :func:`torch.linalg.solve` computes :attr:`A`\ `.inverse() @ \ `:attr:`B` with a
  850. numerically stable algorithm.
  851. Args:
  852. A (Tensor): tensor of shape `(*, m, m)` where `*` is zero or more batch dimensions.
  853. n (int): the exponent.
  854. Keyword args:
  855. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  856. Raises:
  857. RuntimeError: if :attr:`n`\ `< 0` and the matrix :attr:`A` or any matrix in the
  858. batch of matrices :attr:`A` is not invertible.
  859. Examples::
  860. >>> A = torch.randn(3, 3)
  861. >>> torch.linalg.matrix_power(A, 0)
  862. tensor([[1., 0., 0.],
  863. [0., 1., 0.],
  864. [0., 0., 1.]])
  865. >>> torch.linalg.matrix_power(A, 3)
  866. tensor([[ 1.0756, 0.4980, 0.0100],
  867. [-1.6617, 1.4994, -1.9980],
  868. [-0.4509, 0.2731, 0.8001]])
  869. >>> torch.linalg.matrix_power(A.expand(2, -1, -1), -2)
  870. tensor([[[ 0.2640, 0.4571, -0.5511],
  871. [-1.0163, 0.3491, -1.5292],
  872. [-0.4899, 0.0822, 0.2773]],
  873. [[ 0.2640, 0.4571, -0.5511],
  874. [-1.0163, 0.3491, -1.5292],
  875. [-0.4899, 0.0822, 0.2773]]])
  876. """)
  877. matrix_rank = _add_docstr(_linalg.linalg_matrix_rank, r"""
  878. linalg.matrix_rank(A, *, atol=None, rtol=None, hermitian=False, out=None) -> Tensor
  879. Computes the numerical rank of a matrix.
  880. The matrix rank is computed as the number of singular values
  881. (or eigenvalues in absolute value when :attr:`hermitian`\ `= True`)
  882. that are greater than :math:`\max(\text{atol}, \sigma_1 * \text{rtol})` threshold,
  883. where :math:`\sigma_1` is the largest singular value (or eigenvalue).
  884. Supports input of float, double, cfloat and cdouble dtypes.
  885. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  886. the output has the same batch dimensions.
  887. If :attr:`hermitian`\ `= True`, :attr:`A` is assumed to be Hermitian if complex or
  888. symmetric if real, but this is not checked internally. Instead, just the lower
  889. triangular part of the matrix is used in the computations.
  890. If :attr:`rtol` is not specified and :attr:`A` is a matrix of dimensions `(m, n)`,
  891. the relative tolerance is set to be :math:`\text{rtol} = \max(m, n) \varepsilon`
  892. and :math:`\varepsilon` is the epsilon value for the dtype of :attr:`A` (see :class:`.finfo`).
  893. If :attr:`rtol` is not specified and :attr:`atol` is specified to be larger than zero then
  894. :attr:`rtol` is set to zero.
  895. If :attr:`atol` or :attr:`rtol` is a :class:`torch.Tensor`, its shape must be broadcastable to that
  896. of the singular values of :attr:`A` as returned by :func:`torch.linalg.svdvals`.
  897. .. note::
  898. This function has NumPy compatible variant `linalg.matrix_rank(A, tol, hermitian=False)`.
  899. However, use of the positional argument :attr:`tol` is deprecated in favor of :attr:`atol` and :attr:`rtol`.
  900. """ + fr"""
  901. .. note:: The matrix rank is computed using a singular value decomposition
  902. :func:`torch.linalg.svdvals` if :attr:`hermitian`\ `= False` (default) and the eigenvalue
  903. decomposition :func:`torch.linalg.eigvalsh` when :attr:`hermitian`\ `= True`.
  904. {common_notes["sync_note"]}
  905. """ + r"""
  906. Args:
  907. A (Tensor): tensor of shape `(*, m, n)` where `*` is zero or more batch dimensions.
  908. tol (float, Tensor, optional): [NumPy Compat] Alias for :attr:`atol`. Default: `None`.
  909. Keyword args:
  910. atol (float, Tensor, optional): the absolute tolerance value. When `None` it's considered to be zero.
  911. Default: `None`.
  912. rtol (float, Tensor, optional): the relative tolerance value. See above for the value it takes when `None`.
  913. Default: `None`.
  914. hermitian(bool): indicates whether :attr:`A` is Hermitian if complex
  915. or symmetric if real. Default: `False`.
  916. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  917. Examples::
  918. >>> A = torch.eye(10)
  919. >>> torch.linalg.matrix_rank(A)
  920. tensor(10)
  921. >>> B = torch.eye(10)
  922. >>> B[0, 0] = 0
  923. >>> torch.linalg.matrix_rank(B)
  924. tensor(9)
  925. >>> A = torch.randn(4, 3, 2)
  926. >>> torch.linalg.matrix_rank(A)
  927. tensor([2, 2, 2, 2])
  928. >>> A = torch.randn(2, 4, 2, 3)
  929. >>> torch.linalg.matrix_rank(A)
  930. tensor([[2, 2, 2, 2],
  931. [2, 2, 2, 2]])
  932. >>> A = torch.randn(2, 4, 3, 3, dtype=torch.complex64)
  933. >>> torch.linalg.matrix_rank(A)
  934. tensor([[3, 3, 3, 3],
  935. [3, 3, 3, 3]])
  936. >>> torch.linalg.matrix_rank(A, hermitian=True)
  937. tensor([[3, 3, 3, 3],
  938. [3, 3, 3, 3]])
  939. >>> torch.linalg.matrix_rank(A, atol=1.0, rtol=0.0)
  940. tensor([[3, 2, 2, 2],
  941. [1, 2, 1, 2]])
  942. >>> torch.linalg.matrix_rank(A, atol=1.0, rtol=0.0, hermitian=True)
  943. tensor([[2, 2, 2, 1],
  944. [1, 2, 2, 2]])
  945. """)
  946. norm = _add_docstr(_linalg.linalg_norm, r"""
  947. linalg.norm(A, ord=None, dim=None, keepdim=False, *, out=None, dtype=None) -> Tensor
  948. Computes a vector or matrix norm.
  949. Supports input of float, double, cfloat and cdouble dtypes.
  950. Whether this function computes a vector or matrix norm is determined as follows:
  951. - If :attr:`dim` is an `int`, the vector norm will be computed.
  952. - If :attr:`dim` is a `2`-`tuple`, the matrix norm will be computed.
  953. - If :attr:`dim`\ `= None` and :attr:`ord`\ `= None`,
  954. :attr:`A` will be flattened to 1D and the `2`-norm of the resulting vector will be computed.
  955. - If :attr:`dim`\ `= None` and :attr:`ord` `!= None`, :attr:`A` must be 1D or 2D.
  956. :attr:`ord` defines the norm that is computed. The following norms are supported:
  957. ====================== ========================= ========================================================
  958. :attr:`ord` norm for matrices norm for vectors
  959. ====================== ========================= ========================================================
  960. `None` (default) Frobenius norm `2`-norm (see below)
  961. `'fro'` Frobenius norm -- not supported --
  962. `'nuc'` nuclear norm -- not supported --
  963. `inf` `max(sum(abs(x), dim=1))` `max(abs(x))`
  964. `-inf` `min(sum(abs(x), dim=1))` `min(abs(x))`
  965. `0` -- not supported -- `sum(x != 0)`
  966. `1` `max(sum(abs(x), dim=0))` as below
  967. `-1` `min(sum(abs(x), dim=0))` as below
  968. `2` largest singular value as below
  969. `-2` smallest singular value as below
  970. other `int` or `float` -- not supported -- `sum(abs(x)^{ord})^{(1 / ord)}`
  971. ====================== ========================= ========================================================
  972. where `inf` refers to `float('inf')`, NumPy's `inf` object, or any equivalent object.
  973. .. seealso::
  974. :func:`torch.linalg.vector_norm` computes a vector norm.
  975. :func:`torch.linalg.matrix_norm` computes a matrix norm.
  976. The above functions are often clearer and more flexible than using :func:`torch.linalg.norm`.
  977. For example, `torch.linalg.norm(A, ord=1, dim=(0, 1))` always
  978. computes a matrix norm, but with `torch.linalg.vector_norm(A, ord=1, dim=(0, 1))` it is possible
  979. to compute a vector norm over the two dimensions.
  980. Args:
  981. A (Tensor): tensor of shape `(*, n)` or `(*, m, n)` where `*` is zero or more batch dimensions
  982. ord (int, float, inf, -inf, 'fro', 'nuc', optional): order of norm. Default: `None`
  983. dim (int, Tuple[int], optional): dimensions over which to compute
  984. the vector or matrix norm. See above for the behavior when :attr:`dim`\ `= None`.
  985. Default: `None`
  986. keepdim (bool, optional): If set to `True`, the reduced dimensions are retained
  987. in the result as dimensions with size one. Default: `False`
  988. Keyword args:
  989. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  990. dtype (:class:`torch.dtype`, optional): If specified, the input tensor is cast to
  991. :attr:`dtype` before performing the operation, and the returned tensor's type
  992. will be :attr:`dtype`. Default: `None`
  993. Returns:
  994. A real-valued tensor, even when :attr:`A` is complex.
  995. Examples::
  996. >>> from torch import linalg as LA
  997. >>> a = torch.arange(9, dtype=torch.float) - 4
  998. >>> a
  999. tensor([-4., -3., -2., -1., 0., 1., 2., 3., 4.])
  1000. >>> B = a.reshape((3, 3))
  1001. >>> B
  1002. tensor([[-4., -3., -2.],
  1003. [-1., 0., 1.],
  1004. [ 2., 3., 4.]])
  1005. >>> LA.norm(a)
  1006. tensor(7.7460)
  1007. >>> LA.norm(B)
  1008. tensor(7.7460)
  1009. >>> LA.norm(B, 'fro')
  1010. tensor(7.7460)
  1011. >>> LA.norm(a, float('inf'))
  1012. tensor(4.)
  1013. >>> LA.norm(B, float('inf'))
  1014. tensor(9.)
  1015. >>> LA.norm(a, -float('inf'))
  1016. tensor(0.)
  1017. >>> LA.norm(B, -float('inf'))
  1018. tensor(2.)
  1019. >>> LA.norm(a, 1)
  1020. tensor(20.)
  1021. >>> LA.norm(B, 1)
  1022. tensor(7.)
  1023. >>> LA.norm(a, -1)
  1024. tensor(0.)
  1025. >>> LA.norm(B, -1)
  1026. tensor(6.)
  1027. >>> LA.norm(a, 2)
  1028. tensor(7.7460)
  1029. >>> LA.norm(B, 2)
  1030. tensor(7.3485)
  1031. >>> LA.norm(a, -2)
  1032. tensor(0.)
  1033. >>> LA.norm(B.double(), -2)
  1034. tensor(1.8570e-16, dtype=torch.float64)
  1035. >>> LA.norm(a, 3)
  1036. tensor(5.8480)
  1037. >>> LA.norm(a, -3)
  1038. tensor(0.)
  1039. Using the :attr:`dim` argument to compute vector norms::
  1040. >>> c = torch.tensor([[1., 2., 3.],
  1041. ... [-1, 1, 4]])
  1042. >>> LA.norm(c, dim=0)
  1043. tensor([1.4142, 2.2361, 5.0000])
  1044. >>> LA.norm(c, dim=1)
  1045. tensor([3.7417, 4.2426])
  1046. >>> LA.norm(c, ord=1, dim=1)
  1047. tensor([6., 6.])
  1048. Using the :attr:`dim` argument to compute matrix norms::
  1049. >>> A = torch.arange(8, dtype=torch.float).reshape(2, 2, 2)
  1050. >>> LA.norm(A, dim=(1,2))
  1051. tensor([ 3.7417, 11.2250])
  1052. >>> LA.norm(A[0, :, :]), LA.norm(A[1, :, :])
  1053. (tensor(3.7417), tensor(11.2250))
  1054. """)
  1055. vector_norm = _add_docstr(_linalg.linalg_vector_norm, r"""
  1056. linalg.vector_norm(x, ord=2, dim=None, keepdim=False, *, dtype=None, out=None) -> Tensor
  1057. Computes a vector norm.
  1058. If :attr:`x` is complex valued, it computes the norm of :attr:`x`\ `.abs()`
  1059. Supports input of float, double, cfloat and cdouble dtypes.
  1060. This function does not necessarily treat multidimensional :attr:`x` as a batch of
  1061. vectors, instead:
  1062. - If :attr:`dim`\ `= None`, :attr:`x` will be flattened before the norm is computed.
  1063. - If :attr:`dim` is an `int` or a `tuple`, the norm will be computed over these dimensions
  1064. and the other dimensions will be treated as batch dimensions.
  1065. This behavior is for consistency with :func:`torch.linalg.norm`.
  1066. :attr:`ord` defines the vector norm that is computed. The following norms are supported:
  1067. ====================== ===============================
  1068. :attr:`ord` vector norm
  1069. ====================== ===============================
  1070. `2` (default) `2`-norm (see below)
  1071. `inf` `max(abs(x))`
  1072. `-inf` `min(abs(x))`
  1073. `0` `sum(x != 0)`
  1074. other `int` or `float` `sum(abs(x)^{ord})^{(1 / ord)}`
  1075. ====================== ===============================
  1076. where `inf` refers to `float('inf')`, NumPy's `inf` object, or any equivalent object.
  1077. :attr:`dtype` may be used to perform the computation in a more precise dtype.
  1078. It is semantically equivalent to calling ``linalg.vector_norm(x.to(dtype))``
  1079. but it is faster in some cases.
  1080. .. seealso::
  1081. :func:`torch.linalg.matrix_norm` computes a matrix norm.
  1082. Args:
  1083. x (Tensor): tensor, flattened by default, but this behavior can be
  1084. controlled using :attr:`dim`.
  1085. ord (int, float, inf, -inf, 'fro', 'nuc', optional): order of norm. Default: `2`
  1086. dim (int, Tuple[int], optional): dimensions over which to compute
  1087. the norm. See above for the behavior when :attr:`dim`\ `= None`.
  1088. Default: `None`
  1089. keepdim (bool, optional): If set to `True`, the reduced dimensions are retained
  1090. in the result as dimensions with size one. Default: `False`
  1091. Keyword args:
  1092. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  1093. dtype (:class:`torch.dtype`, optional): type used to perform the accumulation and the return.
  1094. If specified, :attr:`x` is cast to :attr:`dtype` before performing the operation,
  1095. and the returned tensor’s type will be :attr:`dtype` if real and of its real counterpart if complex.
  1096. :attr:`dtype` may be complex if :attr:`x` is complex, otherwise it must be real.
  1097. :attr:`x` should be convertible without narrowing to :attr:`dtype`. Default: None
  1098. Returns:
  1099. A real-valued tensor, even when :attr:`x` is complex.
  1100. Examples::
  1101. >>> from torch import linalg as LA
  1102. >>> a = torch.arange(9, dtype=torch.float) - 4
  1103. >>> a
  1104. tensor([-4., -3., -2., -1., 0., 1., 2., 3., 4.])
  1105. >>> B = a.reshape((3, 3))
  1106. >>> B
  1107. tensor([[-4., -3., -2.],
  1108. [-1., 0., 1.],
  1109. [ 2., 3., 4.]])
  1110. >>> LA.vector_norm(a, ord=3.5)
  1111. tensor(5.4345)
  1112. >>> LA.vector_norm(B, ord=3.5)
  1113. tensor(5.4345)
  1114. """)
  1115. matrix_norm = _add_docstr(_linalg.linalg_matrix_norm, r"""
  1116. linalg.matrix_norm(A, ord='fro', dim=(-2, -1), keepdim=False, *, dtype=None, out=None) -> Tensor
  1117. Computes a matrix norm.
  1118. If :attr:`A` is complex valued, it computes the norm of :attr:`A`\ `.abs()`
  1119. Support input of float, double, cfloat and cdouble dtypes.
  1120. Also supports batches of matrices: the norm will be computed over the
  1121. dimensions specified by the 2-tuple :attr:`dim` and the other dimensions will
  1122. be treated as batch dimensions. The output will have the same batch dimensions.
  1123. :attr:`ord` defines the matrix norm that is computed. The following norms are supported:
  1124. ====================== ========================================================
  1125. :attr:`ord` matrix norm
  1126. ====================== ========================================================
  1127. `'fro'` (default) Frobenius norm
  1128. `'nuc'` nuclear norm
  1129. `inf` `max(sum(abs(x), dim=1))`
  1130. `-inf` `min(sum(abs(x), dim=1))`
  1131. `1` `max(sum(abs(x), dim=0))`
  1132. `-1` `min(sum(abs(x), dim=0))`
  1133. `2` largest singular value
  1134. `-2` smallest singular value
  1135. ====================== ========================================================
  1136. where `inf` refers to `float('inf')`, NumPy's `inf` object, or any equivalent object.
  1137. Args:
  1138. A (Tensor): tensor with two or more dimensions. By default its
  1139. shape is interpreted as `(*, m, n)` where `*` is zero or more
  1140. batch dimensions, but this behavior can be controlled using :attr:`dim`.
  1141. ord (int, inf, -inf, 'fro', 'nuc', optional): order of norm. Default: `'fro'`
  1142. dim (Tuple[int, int], optional): dimensions over which to compute the norm. Default: `(-2, -1)`
  1143. keepdim (bool, optional): If set to `True`, the reduced dimensions are retained
  1144. in the result as dimensions with size one. Default: `False`
  1145. Keyword args:
  1146. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  1147. dtype (:class:`torch.dtype`, optional): If specified, the input tensor is cast to
  1148. :attr:`dtype` before performing the operation, and the returned tensor's type
  1149. will be :attr:`dtype`. Default: `None`
  1150. Returns:
  1151. A real-valued tensor, even when :attr:`A` is complex.
  1152. Examples::
  1153. >>> from torch import linalg as LA
  1154. >>> A = torch.arange(9, dtype=torch.float).reshape(3, 3)
  1155. >>> A
  1156. tensor([[0., 1., 2.],
  1157. [3., 4., 5.],
  1158. [6., 7., 8.]])
  1159. >>> LA.matrix_norm(A)
  1160. tensor(14.2829)
  1161. >>> LA.matrix_norm(A, ord=-1)
  1162. tensor(9.)
  1163. >>> B = A.expand(2, -1, -1)
  1164. >>> B
  1165. tensor([[[0., 1., 2.],
  1166. [3., 4., 5.],
  1167. [6., 7., 8.]],
  1168. [[0., 1., 2.],
  1169. [3., 4., 5.],
  1170. [6., 7., 8.]]])
  1171. >>> LA.matrix_norm(B)
  1172. tensor([14.2829, 14.2829])
  1173. >>> LA.matrix_norm(B, dim=(0, 2))
  1174. tensor([ 3.1623, 10.0000, 17.2627])
  1175. """)
  1176. matmul = _add_docstr(_linalg.linalg_matmul, r"""
  1177. linalg.matmul(input, other, *, out=None) -> Tensor
  1178. Alias for :func:`torch.matmul`
  1179. """)
  1180. diagonal = _add_docstr(_linalg.linalg_diagonal, r"""
  1181. linalg.diagonal(A, *, offset=0, dim1=-2, dim2=-1) -> Tensor
  1182. Alias for :func:`torch.diagonal` with defaults :attr:`dim1`\ `= -2`, :attr:`dim2`\ `= -1`.
  1183. """)
  1184. multi_dot = _add_docstr(_linalg.linalg_multi_dot, r"""
  1185. linalg.multi_dot(tensors, *, out=None)
  1186. Efficiently multiplies two or more matrices by reordering the multiplications so that
  1187. the fewest arithmetic operations are performed.
  1188. Supports inputs of float, double, cfloat and cdouble dtypes.
  1189. This function does not support batched inputs.
  1190. Every tensor in :attr:`tensors` must be 2D, except for the first and last which
  1191. may be 1D. If the first tensor is a 1D vector of shape `(n,)` it is treated as a row vector
  1192. of shape `(1, n)`, similarly if the last tensor is a 1D vector of shape `(n,)` it is treated
  1193. as a column vector of shape `(n, 1)`.
  1194. If the first and last tensors are matrices, the output will be a matrix.
  1195. However, if either is a 1D vector, then the output will be a 1D vector.
  1196. Differences with `numpy.linalg.multi_dot`:
  1197. - Unlike `numpy.linalg.multi_dot`, the first and last tensors must either be 1D or 2D
  1198. whereas NumPy allows them to be nD
  1199. .. warning:: This function does not broadcast.
  1200. .. note:: This function is implemented by chaining :func:`torch.mm` calls after
  1201. computing the optimal matrix multiplication order.
  1202. .. note:: The cost of multiplying two matrices with shapes `(a, b)` and `(b, c)` is
  1203. `a * b * c`. Given matrices `A`, `B`, `C` with shapes `(10, 100)`,
  1204. `(100, 5)`, `(5, 50)` respectively, we can calculate the cost of different
  1205. multiplication orders as follows:
  1206. .. math::
  1207. \begin{align*}
  1208. \operatorname{cost}((AB)C) &= 10 \times 100 \times 5 + 10 \times 5 \times 50 = 7500 \\
  1209. \operatorname{cost}(A(BC)) &= 10 \times 100 \times 50 + 100 \times 5 \times 50 = 75000
  1210. \end{align*}
  1211. In this case, multiplying `A` and `B` first followed by `C` is 10 times faster.
  1212. Args:
  1213. tensors (Sequence[Tensor]): two or more tensors to multiply. The first and last
  1214. tensors may be 1D or 2D. Every other tensor must be 2D.
  1215. Keyword args:
  1216. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  1217. Examples::
  1218. >>> from torch.linalg import multi_dot
  1219. >>> multi_dot([torch.tensor([1, 2]), torch.tensor([2, 3])])
  1220. tensor(8)
  1221. >>> multi_dot([torch.tensor([[1, 2]]), torch.tensor([2, 3])])
  1222. tensor([8])
  1223. >>> multi_dot([torch.tensor([[1, 2]]), torch.tensor([[2], [3]])])
  1224. tensor([[8]])
  1225. >>> A = torch.arange(2 * 3).view(2, 3)
  1226. >>> B = torch.arange(3 * 2).view(3, 2)
  1227. >>> C = torch.arange(2 * 2).view(2, 2)
  1228. >>> multi_dot((A, B, C))
  1229. tensor([[ 26, 49],
  1230. [ 80, 148]])
  1231. """)
  1232. svd = _add_docstr(_linalg.linalg_svd, r"""
  1233. linalg.svd(A, full_matrices=True, *, driver=None, out=None) -> (Tensor, Tensor, Tensor)
  1234. Computes the singular value decomposition (SVD) of a matrix.
  1235. Letting :math:`\mathbb{K}` be :math:`\mathbb{R}` or :math:`\mathbb{C}`,
  1236. the **full SVD** of a matrix
  1237. :math:`A \in \mathbb{K}^{m \times n}`, if `k = min(m,n)`, is defined as
  1238. .. math::
  1239. A = U \operatorname{diag}(S) V^{\text{H}}
  1240. \mathrlap{\qquad U \in \mathbb{K}^{m \times m}, S \in \mathbb{R}^k, V \in \mathbb{K}^{n \times n}}
  1241. where :math:`\operatorname{diag}(S) \in \mathbb{K}^{m \times n}`,
  1242. :math:`V^{\text{H}}` is the conjugate transpose when :math:`V` is complex, and the transpose when :math:`V` is real-valued.
  1243. The matrices :math:`U`, :math:`V` (and thus :math:`V^{\text{H}}`) are orthogonal in the real case, and unitary in the complex case.
  1244. When `m > n` (resp. `m < n`) we can drop the last `m - n` (resp. `n - m`) columns of `U` (resp. `V`) to form the **reduced SVD**:
  1245. .. math::
  1246. A = U \operatorname{diag}(S) V^{\text{H}}
  1247. \mathrlap{\qquad U \in \mathbb{K}^{m \times k}, S \in \mathbb{R}^k, V \in \mathbb{K}^{k \times n}}
  1248. where :math:`\operatorname{diag}(S) \in \mathbb{K}^{k \times k}`.
  1249. In this case, :math:`U` and :math:`V` also have orthonormal columns.
  1250. Supports input of float, double, cfloat and cdouble dtypes.
  1251. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  1252. the output has the same batch dimensions.
  1253. The returned decomposition is a named tuple `(U, S, Vh)`
  1254. which corresponds to :math:`U`, :math:`S`, :math:`V^{\text{H}}` above.
  1255. The singular values are returned in descending order.
  1256. The parameter :attr:`full_matrices` chooses between the full (default) and reduced SVD.
  1257. The :attr:`driver` kwarg may be used in CUDA with a cuSOLVER backend to choose the algorithm used to compute the SVD.
  1258. The choice of a driver is a trade-off between accuracy and speed.
  1259. - If :attr:`A` is well-conditioned (its `condition number`_ is not too large), or you do not mind some precision loss.
  1260. - For a general matrix: `'gesvdj'` (Jacobi method)
  1261. - If :attr:`A` is tall or wide (`m >> n` or `m << n`): `'gesvda'` (Approximate method)
  1262. - If :attr:`A` is not well-conditioned or precision is relevant: `'gesvd'` (QR based)
  1263. By default (:attr:`driver`\ `= None`), we call `'gesvdj'` and, if it fails, we fallback to `'gesvd'`.
  1264. Differences with `numpy.linalg.svd`:
  1265. - Unlike `numpy.linalg.svd`, this function always returns a tuple of three tensors
  1266. and it doesn't support `compute_uv` argument.
  1267. Please use :func:`torch.linalg.svdvals`, which computes only the singular values,
  1268. instead of `compute_uv=False`.
  1269. .. note:: When :attr:`full_matrices`\ `= True`, the gradients with respect to `U[..., :, min(m, n):]`
  1270. and `Vh[..., min(m, n):, :]` will be ignored, as those vectors can be arbitrary bases
  1271. of the corresponding subspaces.
  1272. .. warning:: The returned tensors `U` and `V` are not unique, nor are they continuous with
  1273. respect to :attr:`A`.
  1274. Due to this lack of uniqueness, different hardware and software may compute
  1275. different singular vectors.
  1276. This non-uniqueness is caused by the fact that multiplying any pair of singular
  1277. vectors :math:`u_k, v_k` by `-1` in the real case or by
  1278. :math:`e^{i \phi}, \phi \in \mathbb{R}` in the complex case produces another two
  1279. valid singular vectors of the matrix.
  1280. For this reason, the loss function shall not depend on this :math:`e^{i \phi}` quantity,
  1281. as it is not well-defined.
  1282. This is checked for complex inputs when computing the gradients of this function. As such,
  1283. when inputs are complex and are on a CUDA device, the computation of the gradients
  1284. of this function synchronizes that device with the CPU.
  1285. .. warning:: Gradients computed using `U` or `Vh` will only be finite when
  1286. :attr:`A` does not have repeated singular values. If :attr:`A` is rectangular,
  1287. additionally, zero must also not be one of its singular values.
  1288. Furthermore, if the distance between any two singular values is close to zero,
  1289. the gradient will be numerically unstable, as it depends on the singular values
  1290. :math:`\sigma_i` through the computation of
  1291. :math:`\frac{1}{\min_{i \neq j} \sigma_i^2 - \sigma_j^2}`.
  1292. In the rectangular case, the gradient will also be numerically unstable when
  1293. :attr:`A` has small singular values, as it also depends on the computation of
  1294. :math:`\frac{1}{\sigma_i}`.
  1295. .. seealso::
  1296. :func:`torch.linalg.svdvals` computes only the singular values.
  1297. Unlike :func:`torch.linalg.svd`, the gradients of :func:`~svdvals` are always
  1298. numerically stable.
  1299. :func:`torch.linalg.eig` for a function that computes another type of spectral
  1300. decomposition of a matrix. The eigendecomposition works just on square matrices.
  1301. :func:`torch.linalg.eigh` for a (faster) function that computes the eigenvalue decomposition
  1302. for Hermitian and symmetric matrices.
  1303. :func:`torch.linalg.qr` for another (much faster) decomposition that works on general
  1304. matrices.
  1305. Args:
  1306. A (Tensor): tensor of shape `(*, m, n)` where `*` is zero or more batch dimensions.
  1307. full_matrices (bool, optional): controls whether to compute the full or reduced
  1308. SVD, and consequently,
  1309. the shape of the returned tensors
  1310. `U` and `Vh`. Default: `True`.
  1311. Keyword args:
  1312. driver (str, optional): name of the cuSOLVER method to be used. This keyword argument only works on CUDA inputs.
  1313. Available options are: `None`, `gesvd`, `gesvdj`, and `gesvda`.
  1314. Default: `None`.
  1315. out (tuple, optional): output tuple of three tensors. Ignored if `None`.
  1316. Returns:
  1317. A named tuple `(U, S, Vh)` which corresponds to :math:`U`, :math:`S`, :math:`V^{\text{H}}` above.
  1318. `S` will always be real-valued, even when :attr:`A` is complex.
  1319. It will also be ordered in descending order.
  1320. `U` and `Vh` will have the same dtype as :attr:`A`. The left / right singular vectors will be given by
  1321. the columns of `U` and the rows of `Vh` respectively.
  1322. Examples::
  1323. >>> A = torch.randn(5, 3)
  1324. >>> U, S, Vh = torch.linalg.svd(A, full_matrices=False)
  1325. >>> U.shape, S.shape, Vh.shape
  1326. (torch.Size([5, 3]), torch.Size([3]), torch.Size([3, 3]))
  1327. >>> torch.dist(A, U @ torch.diag(S) @ Vh)
  1328. tensor(1.0486e-06)
  1329. >>> U, S, Vh = torch.linalg.svd(A)
  1330. >>> U.shape, S.shape, Vh.shape
  1331. (torch.Size([5, 5]), torch.Size([3]), torch.Size([3, 3]))
  1332. >>> torch.dist(A, U[:, :3] @ torch.diag(S) @ Vh)
  1333. tensor(1.0486e-06)
  1334. >>> A = torch.randn(7, 5, 3)
  1335. >>> U, S, Vh = torch.linalg.svd(A, full_matrices=False)
  1336. >>> torch.dist(A, U @ torch.diag_embed(S) @ Vh)
  1337. tensor(3.0957e-06)
  1338. .. _condition number:
  1339. https://pytorch.org/docs/master/linalg.html#torch.linalg.cond
  1340. .. _the resulting vectors will span the same subspace:
  1341. https://en.wikipedia.org/wiki/Singular_value_decomposition#Singular_values,_singular_vectors,_and_their_relation_to_the_SVD
  1342. """)
  1343. svdvals = _add_docstr(_linalg.linalg_svdvals, r"""
  1344. linalg.svdvals(A, *, driver=None, out=None) -> Tensor
  1345. Computes the singular values of a matrix.
  1346. Supports input of float, double, cfloat and cdouble dtypes.
  1347. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  1348. the output has the same batch dimensions.
  1349. The singular values are returned in descending order.
  1350. .. note:: This function is equivalent to NumPy's `linalg.svd(A, compute_uv=False)`.
  1351. """ + fr"""
  1352. .. note:: {common_notes["sync_note"]}
  1353. """ + r"""
  1354. .. seealso::
  1355. :func:`torch.linalg.svd` computes the full singular value decomposition.
  1356. Args:
  1357. A (Tensor): tensor of shape `(*, m, n)` where `*` is zero or more batch dimensions.
  1358. Keyword args:
  1359. driver (str, optional): name of the cuSOLVER method to be used. This keyword argument only works on CUDA inputs.
  1360. Available options are: `None`, `gesvd`, `gesvdj`, and `gesvda`.
  1361. Check :func:`torch.linalg.svd` for details.
  1362. Default: `None`.
  1363. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  1364. Returns:
  1365. A real-valued tensor, even when :attr:`A` is complex.
  1366. Examples::
  1367. >>> A = torch.randn(5, 3)
  1368. >>> S = torch.linalg.svdvals(A)
  1369. >>> S
  1370. tensor([2.5139, 2.1087, 1.1066])
  1371. >>> torch.dist(S, torch.linalg.svd(A, full_matrices=False).S)
  1372. tensor(2.4576e-07)
  1373. """)
  1374. cond = _add_docstr(_linalg.linalg_cond, r"""
  1375. linalg.cond(A, p=None, *, out=None) -> Tensor
  1376. Computes the condition number of a matrix with respect to a matrix norm.
  1377. Letting :math:`\mathbb{K}` be :math:`\mathbb{R}` or :math:`\mathbb{C}`,
  1378. the **condition number** :math:`\kappa` of a matrix
  1379. :math:`A \in \mathbb{K}^{n \times n}` is defined as
  1380. .. math::
  1381. \kappa(A) = \|A\|_p\|A^{-1}\|_p
  1382. The condition number of :attr:`A` measures the numerical stability of the linear system `AX = B`
  1383. with respect to a matrix norm.
  1384. Supports input of float, double, cfloat and cdouble dtypes.
  1385. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  1386. the output has the same batch dimensions.
  1387. :attr:`p` defines the matrix norm that is computed. The following norms are supported:
  1388. ========= =================================
  1389. :attr:`p` matrix norm
  1390. ========= =================================
  1391. `None` `2`-norm (largest singular value)
  1392. `'fro'` Frobenius norm
  1393. `'nuc'` nuclear norm
  1394. `inf` `max(sum(abs(x), dim=1))`
  1395. `-inf` `min(sum(abs(x), dim=1))`
  1396. `1` `max(sum(abs(x), dim=0))`
  1397. `-1` `min(sum(abs(x), dim=0))`
  1398. `2` largest singular value
  1399. `-2` smallest singular value
  1400. ========= =================================
  1401. where `inf` refers to `float('inf')`, NumPy's `inf` object, or any equivalent object.
  1402. For :attr:`p` is one of `('fro', 'nuc', inf, -inf, 1, -1)`, this function uses
  1403. :func:`torch.linalg.norm` and :func:`torch.linalg.inv`.
  1404. As such, in this case, the matrix (or every matrix in the batch) :attr:`A` has to be square
  1405. and invertible.
  1406. For :attr:`p` in `(2, -2)`, this function can be computed in terms of the singular values
  1407. :math:`\sigma_1 \geq \ldots \geq \sigma_n`
  1408. .. math::
  1409. \kappa_2(A) = \frac{\sigma_1}{\sigma_n}\qquad \kappa_{-2}(A) = \frac{\sigma_n}{\sigma_1}
  1410. In these cases, it is computed using :func:`torch.linalg.svdvals`. For these norms, the matrix
  1411. (or every matrix in the batch) :attr:`A` may have any shape.
  1412. .. note :: When inputs are on a CUDA device, this function synchronizes that device with the CPU
  1413. if :attr:`p` is one of `('fro', 'nuc', inf, -inf, 1, -1)`.
  1414. .. seealso::
  1415. :func:`torch.linalg.solve` for a function that solves linear systems of square matrices.
  1416. :func:`torch.linalg.lstsq` for a function that solves linear systems of general matrices.
  1417. Args:
  1418. A (Tensor): tensor of shape `(*, m, n)` where `*` is zero or more batch dimensions
  1419. for :attr:`p` in `(2, -2)`, and of shape `(*, n, n)` where every matrix
  1420. is invertible for :attr:`p` in `('fro', 'nuc', inf, -inf, 1, -1)`.
  1421. p (int, inf, -inf, 'fro', 'nuc', optional):
  1422. the type of the matrix norm to use in the computations (see above). Default: `None`
  1423. Keyword args:
  1424. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  1425. Returns:
  1426. A real-valued tensor, even when :attr:`A` is complex.
  1427. Raises:
  1428. RuntimeError:
  1429. if :attr:`p` is one of `('fro', 'nuc', inf, -inf, 1, -1)`
  1430. and the :attr:`A` matrix or any matrix in the batch :attr:`A` is not square
  1431. or invertible.
  1432. Examples::
  1433. >>> A = torch.randn(3, 4, 4, dtype=torch.complex64)
  1434. >>> torch.linalg.cond(A)
  1435. >>> A = torch.tensor([[1., 0, -1], [0, 1, 0], [1, 0, 1]])
  1436. >>> torch.linalg.cond(A)
  1437. tensor([1.4142])
  1438. >>> torch.linalg.cond(A, 'fro')
  1439. tensor(3.1623)
  1440. >>> torch.linalg.cond(A, 'nuc')
  1441. tensor(9.2426)
  1442. >>> torch.linalg.cond(A, float('inf'))
  1443. tensor(2.)
  1444. >>> torch.linalg.cond(A, float('-inf'))
  1445. tensor(1.)
  1446. >>> torch.linalg.cond(A, 1)
  1447. tensor(2.)
  1448. >>> torch.linalg.cond(A, -1)
  1449. tensor(1.)
  1450. >>> torch.linalg.cond(A, 2)
  1451. tensor([1.4142])
  1452. >>> torch.linalg.cond(A, -2)
  1453. tensor([0.7071])
  1454. >>> A = torch.randn(2, 3, 3)
  1455. >>> torch.linalg.cond(A)
  1456. tensor([[9.5917],
  1457. [3.2538]])
  1458. >>> A = torch.randn(2, 3, 3, dtype=torch.complex64)
  1459. >>> torch.linalg.cond(A)
  1460. tensor([[4.6245],
  1461. [4.5671]])
  1462. """)
  1463. pinv = _add_docstr(_linalg.linalg_pinv, r"""
  1464. linalg.pinv(A, *, atol=None, rtol=None, hermitian=False, out=None) -> Tensor
  1465. Computes the pseudoinverse (Moore-Penrose inverse) of a matrix.
  1466. The pseudoinverse may be `defined algebraically`_
  1467. but it is more computationally convenient to understand it `through the SVD`_
  1468. Supports input of float, double, cfloat and cdouble dtypes.
  1469. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  1470. the output has the same batch dimensions.
  1471. If :attr:`hermitian`\ `= True`, :attr:`A` is assumed to be Hermitian if complex or
  1472. symmetric if real, but this is not checked internally. Instead, just the lower
  1473. triangular part of the matrix is used in the computations.
  1474. The singular values (or the norm of the eigenvalues when :attr:`hermitian`\ `= True`)
  1475. that are below :math:`\max(\text{atol}, \sigma_1 \cdot \text{rtol})` threshold are
  1476. treated as zero and discarded in the computation,
  1477. where :math:`\sigma_1` is the largest singular value (or eigenvalue).
  1478. If :attr:`rtol` is not specified and :attr:`A` is a matrix of dimensions `(m, n)`,
  1479. the relative tolerance is set to be :math:`\text{rtol} = \max(m, n) \varepsilon`
  1480. and :math:`\varepsilon` is the epsilon value for the dtype of :attr:`A` (see :class:`.finfo`).
  1481. If :attr:`rtol` is not specified and :attr:`atol` is specified to be larger than zero then
  1482. :attr:`rtol` is set to zero.
  1483. If :attr:`atol` or :attr:`rtol` is a :class:`torch.Tensor`, its shape must be broadcastable to that
  1484. of the singular values of :attr:`A` as returned by :func:`torch.linalg.svd`.
  1485. .. note:: This function uses :func:`torch.linalg.svd` if :attr:`hermitian`\ `= False` and
  1486. :func:`torch.linalg.eigh` if :attr:`hermitian`\ `= True`.
  1487. For CUDA inputs, this function synchronizes that device with the CPU.
  1488. .. note::
  1489. Consider using :func:`torch.linalg.lstsq` if possible for multiplying a matrix on the left by
  1490. the pseudoinverse, as::
  1491. torch.linalg.lstsq(A, B).solution == A.pinv() @ B
  1492. It is always preferred to use :func:`~lstsq` when possible, as it is faster and more
  1493. numerically stable than computing the pseudoinverse explicitly.
  1494. .. note::
  1495. This function has NumPy compatible variant `linalg.pinv(A, rcond, hermitian=False)`.
  1496. However, use of the positional argument :attr:`rcond` is deprecated in favor of :attr:`rtol`.
  1497. .. warning::
  1498. This function uses internally :func:`torch.linalg.svd` (or :func:`torch.linalg.eigh`
  1499. when :attr:`hermitian`\ `= True`), so its derivative has the same problems as those of these
  1500. functions. See the warnings in :func:`torch.linalg.svd` and :func:`torch.linalg.eigh` for
  1501. more details.
  1502. .. seealso::
  1503. :func:`torch.linalg.inv` computes the inverse of a square matrix.
  1504. :func:`torch.linalg.lstsq` computes :attr:`A`\ `.pinv() @ \ `:attr:`B` with a
  1505. numerically stable algorithm.
  1506. Args:
  1507. A (Tensor): tensor of shape `(*, m, n)` where `*` is zero or more batch dimensions.
  1508. rcond (float, Tensor, optional): [NumPy Compat]. Alias for :attr:`rtol`. Default: `None`.
  1509. Keyword args:
  1510. atol (float, Tensor, optional): the absolute tolerance value. When `None` it's considered to be zero.
  1511. Default: `None`.
  1512. rtol (float, Tensor, optional): the relative tolerance value. See above for the value it takes when `None`.
  1513. Default: `None`.
  1514. hermitian(bool, optional): indicates whether :attr:`A` is Hermitian if complex
  1515. or symmetric if real. Default: `False`.
  1516. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  1517. Examples::
  1518. >>> A = torch.randn(3, 5)
  1519. >>> A
  1520. tensor([[ 0.5495, 0.0979, -1.4092, -0.1128, 0.4132],
  1521. [-1.1143, -0.3662, 0.3042, 1.6374, -0.9294],
  1522. [-0.3269, -0.5745, -0.0382, -0.5922, -0.6759]])
  1523. >>> torch.linalg.pinv(A)
  1524. tensor([[ 0.0600, -0.1933, -0.2090],
  1525. [-0.0903, -0.0817, -0.4752],
  1526. [-0.7124, -0.1631, -0.2272],
  1527. [ 0.1356, 0.3933, -0.5023],
  1528. [-0.0308, -0.1725, -0.5216]])
  1529. >>> A = torch.randn(2, 6, 3)
  1530. >>> Apinv = torch.linalg.pinv(A)
  1531. >>> torch.dist(Apinv @ A, torch.eye(3))
  1532. tensor(8.5633e-07)
  1533. >>> A = torch.randn(3, 3, dtype=torch.complex64)
  1534. >>> A = A + A.T.conj() # creates a Hermitian matrix
  1535. >>> Apinv = torch.linalg.pinv(A, hermitian=True)
  1536. >>> torch.dist(Apinv @ A, torch.eye(3))
  1537. tensor(1.0830e-06)
  1538. .. _defined algebraically:
  1539. https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse#Existence_and_uniqueness
  1540. .. _through the SVD:
  1541. https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse#Singular_value_decomposition_(SVD)
  1542. """)
  1543. matrix_exp = _add_docstr(_linalg.linalg_matrix_exp, r"""
  1544. linalg.matrix_exp(A) -> Tensor
  1545. Computes the matrix exponential of a square matrix.
  1546. Letting :math:`\mathbb{K}` be :math:`\mathbb{R}` or :math:`\mathbb{C}`,
  1547. this function computes the **matrix exponential** of :math:`A \in \mathbb{K}^{n \times n}`, which is defined as
  1548. .. math::
  1549. \mathrm{matrix_exp}(A) = \sum_{k=0}^\infty \frac{1}{k!}A^k \in \mathbb{K}^{n \times n}.
  1550. If the matrix :math:`A` has eigenvalues :math:`\lambda_i \in \mathbb{C}`,
  1551. the matrix :math:`\mathrm{matrix_exp}(A)` has eigenvalues :math:`e^{\lambda_i} \in \mathbb{C}`.
  1552. Supports input of bfloat16, float, double, cfloat and cdouble dtypes.
  1553. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  1554. the output has the same batch dimensions.
  1555. Args:
  1556. A (Tensor): tensor of shape `(*, n, n)` where `*` is zero or more batch dimensions.
  1557. Example::
  1558. >>> A = torch.empty(2, 2, 2)
  1559. >>> A[0, :, :] = torch.eye(2, 2)
  1560. >>> A[1, :, :] = 2 * torch.eye(2, 2)
  1561. >>> A
  1562. tensor([[[1., 0.],
  1563. [0., 1.]],
  1564. [[2., 0.],
  1565. [0., 2.]]])
  1566. >>> torch.linalg.matrix_exp(A)
  1567. tensor([[[2.7183, 0.0000],
  1568. [0.0000, 2.7183]],
  1569. [[7.3891, 0.0000],
  1570. [0.0000, 7.3891]]])
  1571. >>> import math
  1572. >>> A = torch.tensor([[0, math.pi/3], [-math.pi/3, 0]]) # A is skew-symmetric
  1573. >>> torch.linalg.matrix_exp(A) # matrix_exp(A) = [[cos(pi/3), sin(pi/3)], [-sin(pi/3), cos(pi/3)]]
  1574. tensor([[ 0.5000, 0.8660],
  1575. [-0.8660, 0.5000]])
  1576. """)
  1577. solve = _add_docstr(_linalg.linalg_solve, r"""
  1578. linalg.solve(A, B, *, left=True, out=None) -> Tensor
  1579. Computes the solution of a square system of linear equations with a unique solution.
  1580. Letting :math:`\mathbb{K}` be :math:`\mathbb{R}` or :math:`\mathbb{C}`,
  1581. this function computes the solution :math:`X \in \mathbb{K}^{n \times k}` of the **linear system** associated to
  1582. :math:`A \in \mathbb{K}^{n \times n}, B \in \mathbb{K}^{n \times k}`, which is defined as
  1583. .. math:: AX = B
  1584. If :attr:`left`\ `= False`, this function returns the matrix :math:`X \in \mathbb{K}^{n \times k}` that solves the system
  1585. .. math::
  1586. XA = B\mathrlap{\qquad A \in \mathbb{K}^{k \times k}, B \in \mathbb{K}^{n \times k}.}
  1587. This system of linear equations has one solution if and only if :math:`A` is `invertible`_.
  1588. This function assumes that :math:`A` is invertible.
  1589. Supports inputs of float, double, cfloat and cdouble dtypes.
  1590. Also supports batches of matrices, and if the inputs are batches of matrices then
  1591. the output has the same batch dimensions.
  1592. Letting `*` be zero or more batch dimensions,
  1593. - If :attr:`A` has shape `(*, n, n)` and :attr:`B` has shape `(*, n)` (a batch of vectors) or shape
  1594. `(*, n, k)` (a batch of matrices or "multiple right-hand sides"), this function returns `X` of shape
  1595. `(*, n)` or `(*, n, k)` respectively.
  1596. - Otherwise, if :attr:`A` has shape `(*, n, n)` and :attr:`B` has shape `(n,)` or `(n, k)`, :attr:`B`
  1597. is broadcasted to have shape `(*, n)` or `(*, n, k)` respectively.
  1598. This function then returns the solution of the resulting batch of systems of linear equations.
  1599. .. note::
  1600. This function computes `X = \ `:attr:`A`\ `.inverse() @ \ `:attr:`B` in a faster and
  1601. more numerically stable way than performing the computations separately.
  1602. .. note::
  1603. It is possible to compute the solution of the system :math:`XA = B` by passing the inputs
  1604. :attr:`A` and :attr:`B` transposed and transposing the output returned by this function.
  1605. """ + fr"""
  1606. .. note:: {common_notes["sync_note"]}
  1607. """ + r"""
  1608. .. seealso::
  1609. :func:`torch.linalg.solve_triangular` computes the solution of a triangular system of linear
  1610. equations with a unique solution.
  1611. Args:
  1612. A (Tensor): tensor of shape `(*, n, n)` where `*` is zero or more batch dimensions.
  1613. B (Tensor): right-hand side tensor of shape `(*, n)` or `(*, n, k)` or `(n,)` or `(n, k)`
  1614. according to the rules described above
  1615. Keyword args:
  1616. left (bool, optional): whether to solve the system :math:`AX=B` or :math:`XA = B`. Default: `True`.
  1617. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  1618. Raises:
  1619. RuntimeError: if the :attr:`A` matrix is not invertible or any matrix in a batched :attr:`A`
  1620. is not invertible.
  1621. Examples::
  1622. >>> A = torch.randn(3, 3)
  1623. >>> b = torch.randn(3)
  1624. >>> x = torch.linalg.solve(A, b)
  1625. >>> torch.allclose(A @ x, b)
  1626. True
  1627. >>> A = torch.randn(2, 3, 3)
  1628. >>> B = torch.randn(2, 3, 4)
  1629. >>> X = torch.linalg.solve(A, B)
  1630. >>> X.shape
  1631. torch.Size([2, 3, 4])
  1632. >>> torch.allclose(A @ X, B)
  1633. True
  1634. >>> A = torch.randn(2, 3, 3)
  1635. >>> b = torch.randn(3, 1)
  1636. >>> x = torch.linalg.solve(A, b) # b is broadcasted to size (2, 3, 1)
  1637. >>> x.shape
  1638. torch.Size([2, 3, 1])
  1639. >>> torch.allclose(A @ x, b)
  1640. True
  1641. >>> b = torch.randn(3)
  1642. >>> x = torch.linalg.solve(A, b) # b is broadcasted to size (2, 3)
  1643. >>> x.shape
  1644. torch.Size([2, 3])
  1645. >>> Ax = A @ x.unsqueeze(-1)
  1646. >>> torch.allclose(Ax, b.unsqueeze(-1).expand_as(Ax))
  1647. True
  1648. .. _invertible:
  1649. https://en.wikipedia.org/wiki/Invertible_matrix#The_invertible_matrix_theorem
  1650. """)
  1651. solve_triangular = _add_docstr(_linalg.linalg_solve_triangular, r"""
  1652. linalg.solve_triangular(A, B, *, upper, left=True, unitriangular=False, out=None) -> Tensor
  1653. Computes the solution of a triangular system of linear equations with a unique solution.
  1654. Letting :math:`\mathbb{K}` be :math:`\mathbb{R}` or :math:`\mathbb{C}`,
  1655. this function computes the solution :math:`X \in \mathbb{K}^{n \times k}` of the **linear system**
  1656. associated to the triangular matrix :math:`A \in \mathbb{K}^{n \times n}` without zeros on the diagonal
  1657. (that is, it is `invertible`_) and the rectangular matrix , :math:`B \in \mathbb{K}^{n \times k}`,
  1658. which is defined as
  1659. .. math:: AX = B
  1660. The argument :attr:`upper` signals whether :math:`A` is upper or lower triangular.
  1661. If :attr:`left`\ `= False`, this function returns the matrix :math:`X \in \mathbb{K}^{n \times k}` that
  1662. solves the system
  1663. .. math::
  1664. XA = B\mathrlap{\qquad A \in \mathbb{K}^{k \times k}, B \in \mathbb{K}^{n \times k}.}
  1665. If :attr:`upper`\ `= True` (resp. `False`) just the upper (resp. lower) triangular half of :attr:`A`
  1666. will be accessed. The elements below the main diagonal will be considered to be zero and will not be accessed.
  1667. If :attr:`unitriangular`\ `= True`, the diagonal of :attr:`A` is assumed to be ones and will not be accessed.
  1668. The result may contain `NaN` s if the diagonal of :attr:`A` contains zeros or elements that
  1669. are very close to zero and :attr:`unitriangular`\ `= False` (default) or if the input matrix
  1670. has very small eigenvalues.
  1671. Supports inputs of float, double, cfloat and cdouble dtypes.
  1672. Also supports batches of matrices, and if the inputs are batches of matrices then
  1673. the output has the same batch dimensions.
  1674. .. seealso::
  1675. :func:`torch.linalg.solve` computes the solution of a general square system of linear
  1676. equations with a unique solution.
  1677. Args:
  1678. A (Tensor): tensor of shape `(*, n, n)` (or `(*, k, k)` if :attr:`left`\ `= True`)
  1679. where `*` is zero or more batch dimensions.
  1680. B (Tensor): right-hand side tensor of shape `(*, n, k)`.
  1681. Keyword args:
  1682. upper (bool): whether :attr:`A` is an upper or lower triangular matrix.
  1683. left (bool, optional): whether to solve the system :math:`AX=B` or :math:`XA = B`. Default: `True`.
  1684. unitriangular (bool, optional): if `True`, the diagonal elements of :attr:`A` are assumed to be
  1685. all equal to `1`. Default: `False`.
  1686. out (Tensor, optional): output tensor. `B` may be passed as `out` and the result is computed in-place on `B`.
  1687. Ignored if `None`. Default: `None`.
  1688. Examples::
  1689. >>> A = torch.randn(3, 3).triu_()
  1690. >>> b = torch.randn(3, 4)
  1691. >>> X = torch.linalg.solve_triangular(A, B, upper=True)
  1692. >>> torch.allclose(A @ X, B)
  1693. True
  1694. >>> A = torch.randn(2, 3, 3).tril_()
  1695. >>> B = torch.randn(2, 3, 4)
  1696. >>> X = torch.linalg.solve_triangular(A, B, upper=False)
  1697. >>> torch.allclose(A @ X, B)
  1698. True
  1699. >>> A = torch.randn(2, 4, 4).tril_()
  1700. >>> B = torch.randn(2, 3, 4)
  1701. >>> X = torch.linalg.solve_triangular(A, B, upper=False, left=False)
  1702. >>> torch.allclose(X @ A, B)
  1703. True
  1704. .. _invertible:
  1705. https://en.wikipedia.org/wiki/Invertible_matrix#The_invertible_matrix_theorem
  1706. """)
  1707. lu_factor = _add_docstr(_linalg.linalg_lu_factor, r"""
  1708. linalg.lu_factor(A, *, bool pivot=True, out=None) -> (Tensor, Tensor)
  1709. Computes a compact representation of the LU factorization with partial pivoting of a matrix.
  1710. This function computes a compact representation of the decomposition given by :func:`torch.linalg.lu`.
  1711. If the matrix is square, this representation may be used in :func:`torch.linalg.lu_solve`
  1712. to solve system of linear equations that share the matrix :attr:`A`.
  1713. The returned decomposition is represented as a named tuple `(LU, pivots)`.
  1714. The ``LU`` matrix has the same shape as the input matrix ``A``. Its upper and lower triangular
  1715. parts encode the non-constant elements of ``L`` and ``U`` of the LU decomposition of ``A``.
  1716. The returned permutation matrix is represented by a 1-indexed vector. `pivots[i] == j` represents
  1717. that in the `i`-th step of the algorithm, the `i`-th row was permuted with the `j-1`-th row.
  1718. On CUDA, one may use :attr:`pivot`\ `= False`. In this case, this function returns the LU
  1719. decomposition without pivoting if it exists.
  1720. Supports inputs of float, double, cfloat and cdouble dtypes.
  1721. Also supports batches of matrices, and if the inputs are batches of matrices then
  1722. the output has the same batch dimensions.
  1723. """ + fr"""
  1724. .. note:: {common_notes["sync_note_has_ex"].format("torch.linalg.lu_factor_ex")}
  1725. """ + r"""
  1726. .. warning:: The LU decomposition is almost never unique, as often there are different permutation
  1727. matrices that can yield different LU decompositions.
  1728. As such, different platforms, like SciPy, or inputs on different devices,
  1729. may produce different valid decompositions.
  1730. Gradient computations are only supported if the input matrix is full-rank.
  1731. If this condition is not met, no error will be thrown, but the gradient may not be finite.
  1732. This is because the LU decomposition with pivoting is not differentiable at these points.
  1733. .. seealso::
  1734. :func:`torch.linalg.lu_solve` solves a system of linear equations given the output of this
  1735. function provided the input matrix was square and invertible.
  1736. :func:`torch.lu_unpack` unpacks the tensors returned by :func:`~lu_factor` into the three
  1737. matrices `P, L, U` that form the decomposition.
  1738. :func:`torch.linalg.lu` computes the LU decomposition with partial pivoting of a possibly
  1739. non-square matrix. It is a composition of :func:`~lu_factor` and :func:`torch.lu_unpack`.
  1740. :func:`torch.linalg.solve` solves a system of linear equations. It is a composition
  1741. of :func:`~lu_factor` and :func:`~lu_solve`.
  1742. Args:
  1743. A (Tensor): tensor of shape `(*, m, n)` where `*` is zero or more batch dimensions.
  1744. Keyword args:
  1745. pivot (bool, optional): Whether to compute the LU decomposition with partial pivoting, or the regular LU
  1746. decomposition. :attr:`pivot`\ `= False` not supported on CPU. Default: `True`.
  1747. out (tuple, optional): tuple of two tensors to write the output to. Ignored if `None`. Default: `None`.
  1748. Returns:
  1749. A named tuple `(LU, pivots)`.
  1750. Raises:
  1751. RuntimeError: if the :attr:`A` matrix is not invertible or any matrix in a batched :attr:`A`
  1752. is not invertible.
  1753. Examples::
  1754. >>> A = torch.randn(2, 3, 3)
  1755. >>> B1 = torch.randn(2, 3, 4)
  1756. >>> B2 = torch.randn(2, 3, 7)
  1757. >>> A_factor = torch.linalg.lu_factor(A)
  1758. >>> X1 = torch.linalg.lu_solve(A_factor, B1)
  1759. >>> X2 = torch.linalg.lu_solve(A_factor, B2)
  1760. >>> torch.allclose(A @ X1, B1)
  1761. True
  1762. >>> torch.allclose(A @ X2, B2)
  1763. True
  1764. .. _invertible:
  1765. https://en.wikipedia.org/wiki/Invertible_matrix#The_invertible_matrix_theorem
  1766. """)
  1767. lu_factor_ex = _add_docstr(_linalg.linalg_lu_factor_ex, r"""
  1768. linalg.lu_factor_ex(A, *, pivot=True, check_errors=False, out=None) -> (Tensor, Tensor, Tensor)
  1769. This is a version of :func:`~lu_factor` that does not perform error checks unless :attr:`check_errors`\ `= True`.
  1770. It also returns the :attr:`info` tensor returned by `LAPACK's getrf`_.
  1771. """ + fr"""
  1772. .. note:: {common_notes["sync_note_ex"]}
  1773. .. warning:: {common_notes["experimental_warning"]}
  1774. """ + r"""
  1775. Args:
  1776. A (Tensor): tensor of shape `(*, m, n)` where `*` is zero or more batch dimensions.
  1777. Keyword args:
  1778. pivot (bool, optional): Whether to compute the LU decomposition with partial pivoting, or the regular LU
  1779. decomposition. :attr:`pivot`\ `= False` not supported on CPU. Default: `True`.
  1780. check_errors (bool, optional): controls whether to check the content of ``infos`` and raise
  1781. an error if it is non-zero. Default: `False`.
  1782. out (tuple, optional): tuple of three tensors to write the output to. Ignored if `None`. Default: `None`.
  1783. Returns:
  1784. A named tuple `(LU, pivots, info)`.
  1785. .. _LAPACK's getrf:
  1786. https://www.netlib.org/lapack/explore-html/dd/d9a/group__double_g_ecomputational_ga0019443faea08275ca60a734d0593e60.html
  1787. """)
  1788. lu_solve = _add_docstr(_linalg.linalg_lu_solve, r"""
  1789. linalg.lu_solve(LU, pivots, B, *, left=True, adjoint=False, out=None) -> Tensor
  1790. Computes the solution of a square system of linear equations with a unique solution given an LU decomposition.
  1791. Letting :math:`\mathbb{K}` be :math:`\mathbb{R}` or :math:`\mathbb{C}`,
  1792. this function computes the solution :math:`X \in \mathbb{K}^{n \times k}` of the **linear system** associated to
  1793. :math:`A \in \mathbb{K}^{n \times n}, B \in \mathbb{K}^{n \times k}`, which is defined as
  1794. .. math:: AX = B
  1795. where :math:`A` is given factorized as returned by :func:`~lu_factor`.
  1796. If :attr:`left`\ `= False`, this function returns the matrix :math:`X \in \mathbb{K}^{n \times k}` that solves the system
  1797. .. math::
  1798. XA = B\mathrlap{\qquad A \in \mathbb{K}^{k \times k}, B \in \mathbb{K}^{n \times k}.}
  1799. If :attr:`adjoint`\ `= True` (and :attr:`left`\ `= True), given an LU factorization of :math:`A`
  1800. this function function returns the :math:`X \in \mathbb{K}^{n \times k}` that solves the system
  1801. .. math::
  1802. A^{\text{H}}X = B\mathrlap{\qquad A \in \mathbb{K}^{k \times k}, B \in \mathbb{K}^{n \times k}.}
  1803. where :math:`A^{\text{H}}` is the conjugate transpose when :math:`A` is complex, and the
  1804. transpose when :math:`A` is real-valued. The :attr:`left`\ `= False` case is analogous.
  1805. Supports inputs of float, double, cfloat and cdouble dtypes.
  1806. Also supports batches of matrices, and if the inputs are batches of matrices then
  1807. the output has the same batch dimensions.
  1808. Args:
  1809. LU (Tensor): tensor of shape `(*, n, n)` (or `(*, k, k)` if :attr:`left`\ `= True`)
  1810. where `*` is zero or more batch dimensions as returned by :func:`~lu_factor`.
  1811. pivots (Tensor): tensor of shape `(*, n)` (or `(*, k)` if :attr:`left`\ `= True`)
  1812. where `*` is zero or more batch dimensions as returned by :func:`~lu_factor`.
  1813. B (Tensor): right-hand side tensor of shape `(*, n, k)`.
  1814. Keyword args:
  1815. left (bool, optional): whether to solve the system :math:`AX=B` or :math:`XA = B`. Default: `True`.
  1816. adjoint (bool, optional): whether to solve the system :math:`AX=B` or :math:`A^{\text{H}}X = B`. Default: `False`.
  1817. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  1818. Examples::
  1819. >>> A = torch.randn(3, 3)
  1820. >>> LU, pivots = torch.linalg.lu_factor(A)
  1821. >>> B = torch.randn(3, 2)
  1822. >>> X = torch.linalg.lu_solve(LU, pivots, B)
  1823. >>> torch.allclose(A @ X, B)
  1824. True
  1825. >>> B = torch.randn(3, 3, 2) # Broadcasting rules apply: A is broadcasted
  1826. >>> X = torch.linalg.lu_solve(LU, pivots, B)
  1827. >>> torch.allclose(A @ X, B)
  1828. True
  1829. >>> B = torch.randn(3, 5, 3)
  1830. >>> X = torch.linalg.lu_solve(LU, pivots, B, left=False)
  1831. >>> torch.allclose(X @ A, B)
  1832. True
  1833. >>> B = torch.randn(3, 3, 4) # Now solve for A^T
  1834. >>> X = torch.linalg.lu_solve(LU, pivots, B, adjoint=True)
  1835. >>> torch.allclose(A.mT @ X, B)
  1836. True
  1837. .. _invertible:
  1838. https://en.wikipedia.org/wiki/Invertible_matrix#The_invertible_matrix_theorem
  1839. """)
  1840. lu = _add_docstr(_linalg.linalg_lu, r"""
  1841. lu(A, *, pivot=True, out=None) -> (Tensor, Tensor, Tensor)
  1842. Computes the LU decomposition with partial pivoting of a matrix.
  1843. Letting :math:`\mathbb{K}` be :math:`\mathbb{R}` or :math:`\mathbb{C}`,
  1844. the **LU decomposition with partial pivoting** of a matrix
  1845. :math:`A \in \mathbb{K}^{m \times n}` is defined as
  1846. .. math::
  1847. A = PLU\mathrlap{\qquad P \in \mathbb{K}^{m \times m}, L \in \mathbb{K}^{m \times k}, U \in \mathbb{K}^{k \times n}}
  1848. where `k = min(m,n)`, :math:`P` is a `permutation matrix`_, :math:`L` is lower triangular with ones on the diagonal
  1849. and :math:`U` is upper triangular.
  1850. If :attr:`pivot`\ `= False` and :attr:`A` is on GPU, then the **LU decomposition without pivoting** is computed
  1851. .. math::
  1852. A = LU\mathrlap{\qquad L \in \mathbb{K}^{m \times k}, U \in \mathbb{K}^{k \times n}}
  1853. When :attr:`pivot`\ `= False`, the returned matrix :attr:`P` will be empty.
  1854. The LU decomposition without pivoting `may not exist`_ if any of the principal minors of :attr:`A` is singular.
  1855. In this case, the output matrix may contain `inf` or `NaN`.
  1856. Supports input of float, double, cfloat and cdouble dtypes.
  1857. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  1858. the output has the same batch dimensions.
  1859. .. seealso::
  1860. :func:`torch.linalg.solve` solves a system of linear equations using the LU decomposition
  1861. with partial pivoting.
  1862. .. warning:: The LU decomposition is almost never unique, as often there are different permutation
  1863. matrices that can yield different LU decompositions.
  1864. As such, different platforms, like SciPy, or inputs on different devices,
  1865. may produce different valid decompositions.
  1866. .. warning:: Gradient computations are only supported if the input matrix is full-rank.
  1867. If this condition is not met, no error will be thrown, but the gradient
  1868. may not be finite.
  1869. This is because the LU decomposition with pivoting is not differentiable at these points.
  1870. Args:
  1871. A (Tensor): tensor of shape `(*, m, n)` where `*` is zero or more batch dimensions.
  1872. pivot (bool, optional): Controls whether to compute the LU decomposition with partial pivoting or
  1873. no pivoting. Default: `True`.
  1874. Keyword args:
  1875. out (tuple, optional): output tuple of three tensors. Ignored if `None`. Default: `None`.
  1876. Returns:
  1877. A named tuple `(P, L, U)`.
  1878. Examples::
  1879. >>> A = torch.randn(3, 2)
  1880. >>> P, L, U = torch.linalg.lu(A)
  1881. >>> P
  1882. tensor([[0., 1., 0.],
  1883. [0., 0., 1.],
  1884. [1., 0., 0.]])
  1885. >>> L
  1886. tensor([[1.0000, 0.0000],
  1887. [0.5007, 1.0000],
  1888. [0.0633, 0.9755]])
  1889. >>> U
  1890. tensor([[0.3771, 0.0489],
  1891. [0.0000, 0.9644]])
  1892. >>> torch.dist(A, P @ L @ U)
  1893. tensor(5.9605e-08)
  1894. >>> A = torch.randn(2, 5, 7, device="cuda")
  1895. >>> P, L, U = torch.linalg.lu(A, pivot=False)
  1896. >>> P
  1897. tensor([], device='cuda:0')
  1898. >>> torch.dist(A, L @ U)
  1899. tensor(1.0376e-06, device='cuda:0')
  1900. .. _permutation matrix:
  1901. https://en.wikipedia.org/wiki/Permutation_matrix
  1902. .. _may not exist:
  1903. https://en.wikipedia.org/wiki/LU_decomposition#Definitions
  1904. """)
  1905. tensorinv = _add_docstr(_linalg.linalg_tensorinv, r"""
  1906. linalg.tensorinv(A, ind=2, *, out=None) -> Tensor
  1907. Computes the multiplicative inverse of :func:`torch.tensordot`.
  1908. If `m` is the product of the first :attr:`ind` dimensions of :attr:`A` and `n` is the product of
  1909. the rest of the dimensions, this function expects `m` and `n` to be equal.
  1910. If this is the case, it computes a tensor `X` such that
  1911. `tensordot(\ `:attr:`A`\ `, X, \ `:attr:`ind`\ `)` is the identity matrix in dimension `m`.
  1912. `X` will have the shape of :attr:`A` but with the first :attr:`ind` dimensions pushed back to the end
  1913. .. code:: text
  1914. X.shape == A.shape[ind:] + A.shape[:ind]
  1915. Supports input of float, double, cfloat and cdouble dtypes.
  1916. .. note:: When :attr:`A` is a `2`-dimensional tensor and :attr:`ind`\ `= 1`,
  1917. this function computes the (multiplicative) inverse of :attr:`A`
  1918. (see :func:`torch.linalg.inv`).
  1919. .. note::
  1920. Consider using :func:`torch.linalg.tensorsolve` if possible for multiplying a tensor on the left
  1921. by the tensor inverse, as::
  1922. linalg.tensorsolve(A, B) == torch.tensordot(linalg.tensorinv(A), B) # When B is a tensor with shape A.shape[:B.ndim]
  1923. It is always preferred to use :func:`~tensorsolve` when possible, as it is faster and more
  1924. numerically stable than computing the pseudoinverse explicitly.
  1925. .. seealso::
  1926. :func:`torch.linalg.tensorsolve` computes
  1927. `torch.tensordot(tensorinv(\ `:attr:`A`\ `), \ `:attr:`B`\ `)`.
  1928. Args:
  1929. A (Tensor): tensor to invert. Its shape must satisfy
  1930. `prod(\ `:attr:`A`\ `.shape[:\ `:attr:`ind`\ `]) ==
  1931. prod(\ `:attr:`A`\ `.shape[\ `:attr:`ind`\ `:])`.
  1932. ind (int): index at which to compute the inverse of :func:`torch.tensordot`. Default: `2`.
  1933. Keyword args:
  1934. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  1935. Raises:
  1936. RuntimeError: if the reshaped :attr:`A` is not invertible or the product of the first
  1937. :attr:`ind` dimensions is not equal to the product of the rest.
  1938. Examples::
  1939. >>> A = torch.eye(4 * 6).reshape((4, 6, 8, 3))
  1940. >>> Ainv = torch.linalg.tensorinv(A, ind=2)
  1941. >>> Ainv.shape
  1942. torch.Size([8, 3, 4, 6])
  1943. >>> B = torch.randn(4, 6)
  1944. >>> torch.allclose(torch.tensordot(Ainv, B), torch.linalg.tensorsolve(A, B))
  1945. True
  1946. >>> A = torch.randn(4, 4)
  1947. >>> Atensorinv = torch.linalg.tensorinv(A, ind=1)
  1948. >>> Ainv = torch.linalg.inverse(A)
  1949. >>> torch.allclose(Atensorinv, Ainv)
  1950. True
  1951. """)
  1952. tensorsolve = _add_docstr(_linalg.linalg_tensorsolve, r"""
  1953. linalg.tensorsolve(A, B, dims=None, *, out=None) -> Tensor
  1954. Computes the solution `X` to the system `torch.tensordot(A, X) = B`.
  1955. If `m` is the product of the first :attr:`B`\ `.ndim` dimensions of :attr:`A` and
  1956. `n` is the product of the rest of the dimensions, this function expects `m` and `n` to be equal.
  1957. The returned tensor `x` satisfies
  1958. `tensordot(\ `:attr:`A`\ `, x, dims=x.ndim) == \ `:attr:`B`.
  1959. `x` has shape :attr:`A`\ `[B.ndim:]`.
  1960. If :attr:`dims` is specified, :attr:`A` will be reshaped as
  1961. .. code:: text
  1962. A = movedim(A, dims, range(len(dims) - A.ndim + 1, 0))
  1963. Supports inputs of float, double, cfloat and cdouble dtypes.
  1964. .. seealso::
  1965. :func:`torch.linalg.tensorinv` computes the multiplicative inverse of
  1966. :func:`torch.tensordot`.
  1967. Args:
  1968. A (Tensor): tensor to solve for. Its shape must satisfy
  1969. `prod(\ `:attr:`A`\ `.shape[:\ `:attr:`B`\ `.ndim]) ==
  1970. prod(\ `:attr:`A`\ `.shape[\ `:attr:`B`\ `.ndim:])`.
  1971. B (Tensor): tensor of shape :attr:`A`\ `.shape[:\ `:attr:`B`\ `.ndim]`.
  1972. dims (Tuple[int], optional): dimensions of :attr:`A` to be moved.
  1973. If `None`, no dimensions are moved. Default: `None`.
  1974. Keyword args:
  1975. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  1976. Raises:
  1977. RuntimeError: if the reshaped :attr:`A`\ `.view(m, m)` with `m` as above is not
  1978. invertible or the product of the first :attr:`ind` dimensions is not equal
  1979. to the product of the rest of the dimensions.
  1980. Examples::
  1981. >>> A = torch.eye(2 * 3 * 4).reshape((2 * 3, 4, 2, 3, 4))
  1982. >>> B = torch.randn(2 * 3, 4)
  1983. >>> X = torch.linalg.tensorsolve(A, B)
  1984. >>> X.shape
  1985. torch.Size([2, 3, 4])
  1986. >>> torch.allclose(torch.tensordot(A, X, dims=X.ndim), B)
  1987. True
  1988. >>> A = torch.randn(6, 4, 4, 3, 2)
  1989. >>> B = torch.randn(4, 3, 2)
  1990. >>> X = torch.linalg.tensorsolve(A, B, dims=(0, 2))
  1991. >>> X.shape
  1992. torch.Size([6, 4])
  1993. >>> A = A.permute(1, 3, 4, 0, 2)
  1994. >>> A.shape[B.ndim:]
  1995. torch.Size([6, 4])
  1996. >>> torch.allclose(torch.tensordot(A, X, dims=X.ndim), B, atol=1e-6)
  1997. True
  1998. """)
  1999. qr = _add_docstr(_linalg.linalg_qr, r"""
  2000. qr(A, mode='reduced', *, out=None) -> (Tensor, Tensor)
  2001. Computes the QR decomposition of a matrix.
  2002. Letting :math:`\mathbb{K}` be :math:`\mathbb{R}` or :math:`\mathbb{C}`,
  2003. the **full QR decomposition** of a matrix
  2004. :math:`A \in \mathbb{K}^{m \times n}` is defined as
  2005. .. math::
  2006. A = QR\mathrlap{\qquad Q \in \mathbb{K}^{m \times m}, R \in \mathbb{K}^{m \times n}}
  2007. where :math:`Q` is orthogonal in the real case and unitary in the complex case,
  2008. and :math:`R` is upper triangular with real diagonal (even in the complex case).
  2009. When `m > n` (tall matrix), as `R` is upper triangular, its last `m - n` rows are zero.
  2010. In this case, we can drop the last `m - n` columns of `Q` to form the
  2011. **reduced QR decomposition**:
  2012. .. math::
  2013. A = QR\mathrlap{\qquad Q \in \mathbb{K}^{m \times n}, R \in \mathbb{K}^{n \times n}}
  2014. The reduced QR decomposition agrees with the full QR decomposition when `n >= m` (wide matrix).
  2015. Supports input of float, double, cfloat and cdouble dtypes.
  2016. Also supports batches of matrices, and if :attr:`A` is a batch of matrices then
  2017. the output has the same batch dimensions.
  2018. The parameter :attr:`mode` chooses between the full and reduced QR decomposition.
  2019. If :attr:`A` has shape `(*, m, n)`, denoting `k = min(m, n)`
  2020. - :attr:`mode`\ `= 'reduced'` (default): Returns `(Q, R)` of shapes `(*, m, k)`, `(*, k, n)` respectively.
  2021. It is always differentiable.
  2022. - :attr:`mode`\ `= 'complete'`: Returns `(Q, R)` of shapes `(*, m, m)`, `(*, m, n)` respectively.
  2023. It is differentiable for `m <= n`.
  2024. - :attr:`mode`\ `= 'r'`: Computes only the reduced `R`. Returns `(Q, R)` with `Q` empty and `R` of shape `(*, k, n)`.
  2025. It is never differentiable.
  2026. Differences with `numpy.linalg.qr`:
  2027. - :attr:`mode`\ `= 'raw'` is not implemented.
  2028. - Unlike `numpy.linalg.qr`, this function always returns a tuple of two tensors.
  2029. When :attr:`mode`\ `= 'r'`, the `Q` tensor is an empty tensor.
  2030. .. warning:: The elements in the diagonal of `R` are not necessarily positive.
  2031. As such, the returned QR decomposition is only unique up to the sign of the diagonal of `R`.
  2032. Therefore, different platforms, like NumPy, or inputs on different devices,
  2033. may produce different valid decompositions.
  2034. .. warning:: The QR decomposition is only well-defined if the first `k = min(m, n)` columns
  2035. of every matrix in :attr:`A` are linearly independent.
  2036. If this condition is not met, no error will be thrown, but the QR produced
  2037. may be incorrect and its autodiff may fail or produce incorrect results.
  2038. Args:
  2039. A (Tensor): tensor of shape `(*, m, n)` where `*` is zero or more batch dimensions.
  2040. mode (str, optional): one of `'reduced'`, `'complete'`, `'r'`.
  2041. Controls the shape of the returned tensors. Default: `'reduced'`.
  2042. Keyword args:
  2043. out (tuple, optional): output tuple of two tensors. Ignored if `None`. Default: `None`.
  2044. Returns:
  2045. A named tuple `(Q, R)`.
  2046. Examples::
  2047. >>> A = torch.tensor([[12., -51, 4], [6, 167, -68], [-4, 24, -41]])
  2048. >>> Q, R = torch.linalg.qr(A)
  2049. >>> Q
  2050. tensor([[-0.8571, 0.3943, 0.3314],
  2051. [-0.4286, -0.9029, -0.0343],
  2052. [ 0.2857, -0.1714, 0.9429]])
  2053. >>> R
  2054. tensor([[ -14.0000, -21.0000, 14.0000],
  2055. [ 0.0000, -175.0000, 70.0000],
  2056. [ 0.0000, 0.0000, -35.0000]])
  2057. >>> (Q @ R).round()
  2058. tensor([[ 12., -51., 4.],
  2059. [ 6., 167., -68.],
  2060. [ -4., 24., -41.]])
  2061. >>> (Q.T @ Q).round()
  2062. tensor([[ 1., 0., 0.],
  2063. [ 0., 1., -0.],
  2064. [ 0., -0., 1.]])
  2065. >>> Q2, R2 = torch.linalg.qr(A, mode='r')
  2066. >>> Q2
  2067. tensor([])
  2068. >>> torch.equal(R, R2)
  2069. True
  2070. >>> A = torch.randn(3, 4, 5)
  2071. >>> Q, R = torch.linalg.qr(A, mode='complete')
  2072. >>> torch.dist(Q @ R, A)
  2073. tensor(1.6099e-06)
  2074. >>> torch.dist(Q.mT @ Q, torch.eye(4))
  2075. tensor(6.2158e-07)
  2076. """)
  2077. vander = _add_docstr(_linalg.linalg_vander, r"""
  2078. vander(x, N=None) -> Tensor
  2079. Generates a Vandermonde matrix.
  2080. Returns the Vandermonde matrix :math:`V`
  2081. .. math::
  2082. V = \begin{pmatrix}
  2083. 1 & x_1 & x_1^2 & \dots & x_1^{N-1}\\
  2084. 1 & x_2 & x_2^2 & \dots & x_2^{N-1}\\
  2085. 1 & x_3 & x_3^2 & \dots & x_3^{N-1}\\
  2086. \vdots & \vdots & \vdots & \ddots &\vdots \\
  2087. 1 & x_n & x_n^2 & \dots & x_n^{N-1}
  2088. \end{pmatrix}.
  2089. for `N > 1`.
  2090. If :attr:`N`\ `= None`, then `N = x.size(-1)` so that the output is a square matrix.
  2091. Supports inputs of float, double, cfloat, cdouble, and integral dtypes.
  2092. Also supports batches of vectors, and if :attr:`x` is a batch of vectors then
  2093. the output has the same batch dimensions.
  2094. Differences with `numpy.vander`:
  2095. - Unlike `numpy.vander`, this function returns the powers of :attr:`x` in ascending order.
  2096. To get them in the reverse order call ``linalg.vander(x, N).flip(-1)``.
  2097. Args:
  2098. x (Tensor): tensor of shape `(*, n)` where `*` is zero or more batch dimensions
  2099. consisting of vectors.
  2100. Keyword args:
  2101. N (int, optional): Number of columns in the output. Default: `x.size(-1)`
  2102. Example::
  2103. >>> x = torch.tensor([1, 2, 3, 5])
  2104. >>> linalg.vander(x)
  2105. tensor([[ 1, 1, 1, 1],
  2106. [ 1, 2, 4, 8],
  2107. [ 1, 3, 9, 27],
  2108. [ 1, 5, 25, 125]])
  2109. >>> linalg.vander(x, N=3)
  2110. tensor([[ 1, 1, 1],
  2111. [ 1, 2, 4],
  2112. [ 1, 3, 9],
  2113. [ 1, 5, 25]])
  2114. """)
  2115. vecdot = _add_docstr(_linalg.linalg_vecdot, r"""
  2116. linalg.vecdot(x, y, *, dim=-1, out=None) -> Tensor
  2117. Computes the dot product of two batches of vectors along a dimension.
  2118. In symbols, this function computes
  2119. .. math::
  2120. \sum_{i=1}^n \overline{x_i}y_i.
  2121. over the dimension :attr:`dim` where :math:`\overline{x_i}` denotes the conjugate for complex
  2122. vectors, and it is the identity for real vectors.
  2123. Supports input of half, bfloat16, float, double, cfloat, cdouble and integral dtypes.
  2124. It also supports broadcasting.
  2125. Args:
  2126. x (Tensor): first batch of vectors of shape `(*, n)`.
  2127. y (Tensor): second batch of vectors of shape `(*, n)`.
  2128. Keyword args:
  2129. dim (int): Dimension along which to compute the dot product. Default: `-1`.
  2130. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  2131. Examples::
  2132. >>> v1 = torch.randn(3, 2)
  2133. >>> v2 = torch.randn(3, 2)
  2134. >>> linalg.vecdot(v1, v2)
  2135. tensor([ 0.3223, 0.2815, -0.1944])
  2136. >>> torch.vdot(v1[0], v2[0])
  2137. tensor(0.3223)
  2138. """)