matrices.py 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233
  1. import mpmath as mp
  2. from collections.abc import Callable
  3. from sympy.core.add import Add
  4. from sympy.core.basic import Basic
  5. from sympy.core.function import diff
  6. from sympy.core.expr import Expr
  7. from sympy.core.kind import _NumberKind, UndefinedKind
  8. from sympy.core.mul import Mul
  9. from sympy.core.power import Pow
  10. from sympy.core.singleton import S
  11. from sympy.core.symbol import Dummy, Symbol, uniquely_named_symbol
  12. from sympy.core.sympify import sympify, _sympify
  13. from sympy.functions.combinatorial.factorials import binomial, factorial
  14. from sympy.functions.elementary.complexes import re
  15. from sympy.functions.elementary.exponential import exp, log
  16. from sympy.functions.elementary.miscellaneous import Max, Min, sqrt
  17. from sympy.functions.special.tensor_functions import KroneckerDelta, LeviCivita
  18. from sympy.polys import cancel
  19. from sympy.printing import sstr
  20. from sympy.printing.defaults import Printable
  21. from sympy.printing.str import StrPrinter
  22. from sympy.utilities.iterables import flatten, NotIterable, is_sequence, reshape
  23. from sympy.utilities.misc import as_int, filldedent
  24. from .common import (
  25. MatrixCommon, MatrixError, NonSquareMatrixError, NonInvertibleMatrixError,
  26. ShapeError, MatrixKind, a2idx)
  27. from .utilities import _iszero, _is_zero_after_expand_mul, _simplify
  28. from .determinant import (
  29. _find_reasonable_pivot, _find_reasonable_pivot_naive,
  30. _adjugate, _charpoly, _cofactor, _cofactor_matrix, _per,
  31. _det, _det_bareiss, _det_berkowitz, _det_LU, _minor, _minor_submatrix)
  32. from .reductions import _is_echelon, _echelon_form, _rank, _rref
  33. from .subspaces import _columnspace, _nullspace, _rowspace, _orthogonalize
  34. from .eigen import (
  35. _eigenvals, _eigenvects,
  36. _bidiagonalize, _bidiagonal_decomposition,
  37. _is_diagonalizable, _diagonalize,
  38. _is_positive_definite, _is_positive_semidefinite,
  39. _is_negative_definite, _is_negative_semidefinite, _is_indefinite,
  40. _jordan_form, _left_eigenvects, _singular_values)
  41. from .decompositions import (
  42. _rank_decomposition, _cholesky, _LDLdecomposition,
  43. _LUdecomposition, _LUdecomposition_Simple, _LUdecompositionFF,
  44. _singular_value_decomposition, _QRdecomposition, _upper_hessenberg_decomposition)
  45. from .graph import (
  46. _connected_components, _connected_components_decomposition,
  47. _strongly_connected_components, _strongly_connected_components_decomposition)
  48. from .solvers import (
  49. _diagonal_solve, _lower_triangular_solve, _upper_triangular_solve,
  50. _cholesky_solve, _LDLsolve, _LUsolve, _QRsolve, _gauss_jordan_solve,
  51. _pinv_solve, _solve, _solve_least_squares)
  52. from .inverse import (
  53. _pinv, _inv_mod, _inv_ADJ, _inv_GE, _inv_LU, _inv_CH, _inv_LDL, _inv_QR,
  54. _inv, _inv_block)
  55. class DeferredVector(Symbol, NotIterable):
  56. """A vector whose components are deferred (e.g. for use with lambdify).
  57. Examples
  58. ========
  59. >>> from sympy import DeferredVector, lambdify
  60. >>> X = DeferredVector( 'X' )
  61. >>> X
  62. X
  63. >>> expr = (X[0] + 2, X[2] + 3)
  64. >>> func = lambdify( X, expr)
  65. >>> func( [1, 2, 3] )
  66. (3, 6)
  67. """
  68. def __getitem__(self, i):
  69. if i == -0:
  70. i = 0
  71. if i < 0:
  72. raise IndexError('DeferredVector index out of range')
  73. component_name = '%s[%d]' % (self.name, i)
  74. return Symbol(component_name)
  75. def __str__(self):
  76. return sstr(self)
  77. def __repr__(self):
  78. return "DeferredVector('%s')" % self.name
  79. class MatrixDeterminant(MatrixCommon):
  80. """Provides basic matrix determinant operations. Should not be instantiated
  81. directly. See ``determinant.py`` for their implementations."""
  82. def _eval_det_bareiss(self, iszerofunc=_is_zero_after_expand_mul):
  83. return _det_bareiss(self, iszerofunc=iszerofunc)
  84. def _eval_det_berkowitz(self):
  85. return _det_berkowitz(self)
  86. def _eval_det_lu(self, iszerofunc=_iszero, simpfunc=None):
  87. return _det_LU(self, iszerofunc=iszerofunc, simpfunc=simpfunc)
  88. def _eval_determinant(self): # for expressions.determinant.Determinant
  89. return _det(self)
  90. def adjugate(self, method="berkowitz"):
  91. return _adjugate(self, method=method)
  92. def charpoly(self, x='lambda', simplify=_simplify):
  93. return _charpoly(self, x=x, simplify=simplify)
  94. def cofactor(self, i, j, method="berkowitz"):
  95. return _cofactor(self, i, j, method=method)
  96. def cofactor_matrix(self, method="berkowitz"):
  97. return _cofactor_matrix(self, method=method)
  98. def det(self, method="bareiss", iszerofunc=None):
  99. return _det(self, method=method, iszerofunc=iszerofunc)
  100. def per(self):
  101. return _per(self)
  102. def minor(self, i, j, method="berkowitz"):
  103. return _minor(self, i, j, method=method)
  104. def minor_submatrix(self, i, j):
  105. return _minor_submatrix(self, i, j)
  106. _find_reasonable_pivot.__doc__ = _find_reasonable_pivot.__doc__
  107. _find_reasonable_pivot_naive.__doc__ = _find_reasonable_pivot_naive.__doc__
  108. _eval_det_bareiss.__doc__ = _det_bareiss.__doc__
  109. _eval_det_berkowitz.__doc__ = _det_berkowitz.__doc__
  110. _eval_det_lu.__doc__ = _det_LU.__doc__
  111. _eval_determinant.__doc__ = _det.__doc__
  112. adjugate.__doc__ = _adjugate.__doc__
  113. charpoly.__doc__ = _charpoly.__doc__
  114. cofactor.__doc__ = _cofactor.__doc__
  115. cofactor_matrix.__doc__ = _cofactor_matrix.__doc__
  116. det.__doc__ = _det.__doc__
  117. per.__doc__ = _per.__doc__
  118. minor.__doc__ = _minor.__doc__
  119. minor_submatrix.__doc__ = _minor_submatrix.__doc__
  120. class MatrixReductions(MatrixDeterminant):
  121. """Provides basic matrix row/column operations. Should not be instantiated
  122. directly. See ``reductions.py`` for some of their implementations."""
  123. def echelon_form(self, iszerofunc=_iszero, simplify=False, with_pivots=False):
  124. return _echelon_form(self, iszerofunc=iszerofunc, simplify=simplify,
  125. with_pivots=with_pivots)
  126. @property
  127. def is_echelon(self):
  128. return _is_echelon(self)
  129. def rank(self, iszerofunc=_iszero, simplify=False):
  130. return _rank(self, iszerofunc=iszerofunc, simplify=simplify)
  131. def rref(self, iszerofunc=_iszero, simplify=False, pivots=True,
  132. normalize_last=True):
  133. return _rref(self, iszerofunc=iszerofunc, simplify=simplify,
  134. pivots=pivots, normalize_last=normalize_last)
  135. echelon_form.__doc__ = _echelon_form.__doc__
  136. is_echelon.__doc__ = _is_echelon.__doc__
  137. rank.__doc__ = _rank.__doc__
  138. rref.__doc__ = _rref.__doc__
  139. def _normalize_op_args(self, op, col, k, col1, col2, error_str="col"):
  140. """Validate the arguments for a row/column operation. ``error_str``
  141. can be one of "row" or "col" depending on the arguments being parsed."""
  142. if op not in ["n->kn", "n<->m", "n->n+km"]:
  143. raise ValueError("Unknown {} operation '{}'. Valid col operations "
  144. "are 'n->kn', 'n<->m', 'n->n+km'".format(error_str, op))
  145. # define self_col according to error_str
  146. self_cols = self.cols if error_str == 'col' else self.rows
  147. # normalize and validate the arguments
  148. if op == "n->kn":
  149. col = col if col is not None else col1
  150. if col is None or k is None:
  151. raise ValueError("For a {0} operation 'n->kn' you must provide the "
  152. "kwargs `{0}` and `k`".format(error_str))
  153. if not 0 <= col < self_cols:
  154. raise ValueError("This matrix does not have a {} '{}'".format(error_str, col))
  155. elif op == "n<->m":
  156. # we need two cols to swap. It does not matter
  157. # how they were specified, so gather them together and
  158. # remove `None`
  159. cols = {col, k, col1, col2}.difference([None])
  160. if len(cols) > 2:
  161. # maybe the user left `k` by mistake?
  162. cols = {col, col1, col2}.difference([None])
  163. if len(cols) != 2:
  164. raise ValueError("For a {0} operation 'n<->m' you must provide the "
  165. "kwargs `{0}1` and `{0}2`".format(error_str))
  166. col1, col2 = cols
  167. if not 0 <= col1 < self_cols:
  168. raise ValueError("This matrix does not have a {} '{}'".format(error_str, col1))
  169. if not 0 <= col2 < self_cols:
  170. raise ValueError("This matrix does not have a {} '{}'".format(error_str, col2))
  171. elif op == "n->n+km":
  172. col = col1 if col is None else col
  173. col2 = col1 if col2 is None else col2
  174. if col is None or col2 is None or k is None:
  175. raise ValueError("For a {0} operation 'n->n+km' you must provide the "
  176. "kwargs `{0}`, `k`, and `{0}2`".format(error_str))
  177. if col == col2:
  178. raise ValueError("For a {0} operation 'n->n+km' `{0}` and `{0}2` must "
  179. "be different.".format(error_str))
  180. if not 0 <= col < self_cols:
  181. raise ValueError("This matrix does not have a {} '{}'".format(error_str, col))
  182. if not 0 <= col2 < self_cols:
  183. raise ValueError("This matrix does not have a {} '{}'".format(error_str, col2))
  184. else:
  185. raise ValueError('invalid operation %s' % repr(op))
  186. return op, col, k, col1, col2
  187. def _eval_col_op_multiply_col_by_const(self, col, k):
  188. def entry(i, j):
  189. if j == col:
  190. return k * self[i, j]
  191. return self[i, j]
  192. return self._new(self.rows, self.cols, entry)
  193. def _eval_col_op_swap(self, col1, col2):
  194. def entry(i, j):
  195. if j == col1:
  196. return self[i, col2]
  197. elif j == col2:
  198. return self[i, col1]
  199. return self[i, j]
  200. return self._new(self.rows, self.cols, entry)
  201. def _eval_col_op_add_multiple_to_other_col(self, col, k, col2):
  202. def entry(i, j):
  203. if j == col:
  204. return self[i, j] + k * self[i, col2]
  205. return self[i, j]
  206. return self._new(self.rows, self.cols, entry)
  207. def _eval_row_op_swap(self, row1, row2):
  208. def entry(i, j):
  209. if i == row1:
  210. return self[row2, j]
  211. elif i == row2:
  212. return self[row1, j]
  213. return self[i, j]
  214. return self._new(self.rows, self.cols, entry)
  215. def _eval_row_op_multiply_row_by_const(self, row, k):
  216. def entry(i, j):
  217. if i == row:
  218. return k * self[i, j]
  219. return self[i, j]
  220. return self._new(self.rows, self.cols, entry)
  221. def _eval_row_op_add_multiple_to_other_row(self, row, k, row2):
  222. def entry(i, j):
  223. if i == row:
  224. return self[i, j] + k * self[row2, j]
  225. return self[i, j]
  226. return self._new(self.rows, self.cols, entry)
  227. def elementary_col_op(self, op="n->kn", col=None, k=None, col1=None, col2=None):
  228. """Performs the elementary column operation `op`.
  229. `op` may be one of
  230. * ``"n->kn"`` (column n goes to k*n)
  231. * ``"n<->m"`` (swap column n and column m)
  232. * ``"n->n+km"`` (column n goes to column n + k*column m)
  233. Parameters
  234. ==========
  235. op : string; the elementary row operation
  236. col : the column to apply the column operation
  237. k : the multiple to apply in the column operation
  238. col1 : one column of a column swap
  239. col2 : second column of a column swap or column "m" in the column operation
  240. "n->n+km"
  241. """
  242. op, col, k, col1, col2 = self._normalize_op_args(op, col, k, col1, col2, "col")
  243. # now that we've validated, we're all good to dispatch
  244. if op == "n->kn":
  245. return self._eval_col_op_multiply_col_by_const(col, k)
  246. if op == "n<->m":
  247. return self._eval_col_op_swap(col1, col2)
  248. if op == "n->n+km":
  249. return self._eval_col_op_add_multiple_to_other_col(col, k, col2)
  250. def elementary_row_op(self, op="n->kn", row=None, k=None, row1=None, row2=None):
  251. """Performs the elementary row operation `op`.
  252. `op` may be one of
  253. * ``"n->kn"`` (row n goes to k*n)
  254. * ``"n<->m"`` (swap row n and row m)
  255. * ``"n->n+km"`` (row n goes to row n + k*row m)
  256. Parameters
  257. ==========
  258. op : string; the elementary row operation
  259. row : the row to apply the row operation
  260. k : the multiple to apply in the row operation
  261. row1 : one row of a row swap
  262. row2 : second row of a row swap or row "m" in the row operation
  263. "n->n+km"
  264. """
  265. op, row, k, row1, row2 = self._normalize_op_args(op, row, k, row1, row2, "row")
  266. # now that we've validated, we're all good to dispatch
  267. if op == "n->kn":
  268. return self._eval_row_op_multiply_row_by_const(row, k)
  269. if op == "n<->m":
  270. return self._eval_row_op_swap(row1, row2)
  271. if op == "n->n+km":
  272. return self._eval_row_op_add_multiple_to_other_row(row, k, row2)
  273. class MatrixSubspaces(MatrixReductions):
  274. """Provides methods relating to the fundamental subspaces of a matrix.
  275. Should not be instantiated directly. See ``subspaces.py`` for their
  276. implementations."""
  277. def columnspace(self, simplify=False):
  278. return _columnspace(self, simplify=simplify)
  279. def nullspace(self, simplify=False, iszerofunc=_iszero):
  280. return _nullspace(self, simplify=simplify, iszerofunc=iszerofunc)
  281. def rowspace(self, simplify=False):
  282. return _rowspace(self, simplify=simplify)
  283. # This is a classmethod but is converted to such later in order to allow
  284. # assignment of __doc__ since that does not work for already wrapped
  285. # classmethods in Python 3.6.
  286. def orthogonalize(cls, *vecs, **kwargs):
  287. return _orthogonalize(cls, *vecs, **kwargs)
  288. columnspace.__doc__ = _columnspace.__doc__
  289. nullspace.__doc__ = _nullspace.__doc__
  290. rowspace.__doc__ = _rowspace.__doc__
  291. orthogonalize.__doc__ = _orthogonalize.__doc__
  292. orthogonalize = classmethod(orthogonalize) # type:ignore
  293. class MatrixEigen(MatrixSubspaces):
  294. """Provides basic matrix eigenvalue/vector operations.
  295. Should not be instantiated directly. See ``eigen.py`` for their
  296. implementations."""
  297. def eigenvals(self, error_when_incomplete=True, **flags):
  298. return _eigenvals(self, error_when_incomplete=error_when_incomplete, **flags)
  299. def eigenvects(self, error_when_incomplete=True, iszerofunc=_iszero, **flags):
  300. return _eigenvects(self, error_when_incomplete=error_when_incomplete,
  301. iszerofunc=iszerofunc, **flags)
  302. def is_diagonalizable(self, reals_only=False, **kwargs):
  303. return _is_diagonalizable(self, reals_only=reals_only, **kwargs)
  304. def diagonalize(self, reals_only=False, sort=False, normalize=False):
  305. return _diagonalize(self, reals_only=reals_only, sort=sort,
  306. normalize=normalize)
  307. def bidiagonalize(self, upper=True):
  308. return _bidiagonalize(self, upper=upper)
  309. def bidiagonal_decomposition(self, upper=True):
  310. return _bidiagonal_decomposition(self, upper=upper)
  311. @property
  312. def is_positive_definite(self):
  313. return _is_positive_definite(self)
  314. @property
  315. def is_positive_semidefinite(self):
  316. return _is_positive_semidefinite(self)
  317. @property
  318. def is_negative_definite(self):
  319. return _is_negative_definite(self)
  320. @property
  321. def is_negative_semidefinite(self):
  322. return _is_negative_semidefinite(self)
  323. @property
  324. def is_indefinite(self):
  325. return _is_indefinite(self)
  326. def jordan_form(self, calc_transform=True, **kwargs):
  327. return _jordan_form(self, calc_transform=calc_transform, **kwargs)
  328. def left_eigenvects(self, **flags):
  329. return _left_eigenvects(self, **flags)
  330. def singular_values(self):
  331. return _singular_values(self)
  332. eigenvals.__doc__ = _eigenvals.__doc__
  333. eigenvects.__doc__ = _eigenvects.__doc__
  334. is_diagonalizable.__doc__ = _is_diagonalizable.__doc__
  335. diagonalize.__doc__ = _diagonalize.__doc__
  336. is_positive_definite.__doc__ = _is_positive_definite.__doc__
  337. is_positive_semidefinite.__doc__ = _is_positive_semidefinite.__doc__
  338. is_negative_definite.__doc__ = _is_negative_definite.__doc__
  339. is_negative_semidefinite.__doc__ = _is_negative_semidefinite.__doc__
  340. is_indefinite.__doc__ = _is_indefinite.__doc__
  341. jordan_form.__doc__ = _jordan_form.__doc__
  342. left_eigenvects.__doc__ = _left_eigenvects.__doc__
  343. singular_values.__doc__ = _singular_values.__doc__
  344. bidiagonalize.__doc__ = _bidiagonalize.__doc__
  345. bidiagonal_decomposition.__doc__ = _bidiagonal_decomposition.__doc__
  346. class MatrixCalculus(MatrixCommon):
  347. """Provides calculus-related matrix operations."""
  348. def diff(self, *args, **kwargs):
  349. """Calculate the derivative of each element in the matrix.
  350. ``args`` will be passed to the ``integrate`` function.
  351. Examples
  352. ========
  353. >>> from sympy import Matrix
  354. >>> from sympy.abc import x, y
  355. >>> M = Matrix([[x, y], [1, 0]])
  356. >>> M.diff(x)
  357. Matrix([
  358. [1, 0],
  359. [0, 0]])
  360. See Also
  361. ========
  362. integrate
  363. limit
  364. """
  365. # XXX this should be handled here rather than in Derivative
  366. from sympy.tensor.array.array_derivatives import ArrayDerivative
  367. kwargs.setdefault('evaluate', True)
  368. deriv = ArrayDerivative(self, *args, evaluate=True)
  369. if not isinstance(self, Basic):
  370. return deriv.as_mutable()
  371. else:
  372. return deriv
  373. def _eval_derivative(self, arg):
  374. return self.applyfunc(lambda x: x.diff(arg))
  375. def integrate(self, *args, **kwargs):
  376. """Integrate each element of the matrix. ``args`` will
  377. be passed to the ``integrate`` function.
  378. Examples
  379. ========
  380. >>> from sympy import Matrix
  381. >>> from sympy.abc import x, y
  382. >>> M = Matrix([[x, y], [1, 0]])
  383. >>> M.integrate((x, ))
  384. Matrix([
  385. [x**2/2, x*y],
  386. [ x, 0]])
  387. >>> M.integrate((x, 0, 2))
  388. Matrix([
  389. [2, 2*y],
  390. [2, 0]])
  391. See Also
  392. ========
  393. limit
  394. diff
  395. """
  396. return self.applyfunc(lambda x: x.integrate(*args, **kwargs))
  397. def jacobian(self, X):
  398. """Calculates the Jacobian matrix (derivative of a vector-valued function).
  399. Parameters
  400. ==========
  401. ``self`` : vector of expressions representing functions f_i(x_1, ..., x_n).
  402. X : set of x_i's in order, it can be a list or a Matrix
  403. Both ``self`` and X can be a row or a column matrix in any order
  404. (i.e., jacobian() should always work).
  405. Examples
  406. ========
  407. >>> from sympy import sin, cos, Matrix
  408. >>> from sympy.abc import rho, phi
  409. >>> X = Matrix([rho*cos(phi), rho*sin(phi), rho**2])
  410. >>> Y = Matrix([rho, phi])
  411. >>> X.jacobian(Y)
  412. Matrix([
  413. [cos(phi), -rho*sin(phi)],
  414. [sin(phi), rho*cos(phi)],
  415. [ 2*rho, 0]])
  416. >>> X = Matrix([rho*cos(phi), rho*sin(phi)])
  417. >>> X.jacobian(Y)
  418. Matrix([
  419. [cos(phi), -rho*sin(phi)],
  420. [sin(phi), rho*cos(phi)]])
  421. See Also
  422. ========
  423. hessian
  424. wronskian
  425. """
  426. if not isinstance(X, MatrixBase):
  427. X = self._new(X)
  428. # Both X and ``self`` can be a row or a column matrix, so we need to make
  429. # sure all valid combinations work, but everything else fails:
  430. if self.shape[0] == 1:
  431. m = self.shape[1]
  432. elif self.shape[1] == 1:
  433. m = self.shape[0]
  434. else:
  435. raise TypeError("``self`` must be a row or a column matrix")
  436. if X.shape[0] == 1:
  437. n = X.shape[1]
  438. elif X.shape[1] == 1:
  439. n = X.shape[0]
  440. else:
  441. raise TypeError("X must be a row or a column matrix")
  442. # m is the number of functions and n is the number of variables
  443. # computing the Jacobian is now easy:
  444. return self._new(m, n, lambda j, i: self[j].diff(X[i]))
  445. def limit(self, *args):
  446. """Calculate the limit of each element in the matrix.
  447. ``args`` will be passed to the ``limit`` function.
  448. Examples
  449. ========
  450. >>> from sympy import Matrix
  451. >>> from sympy.abc import x, y
  452. >>> M = Matrix([[x, y], [1, 0]])
  453. >>> M.limit(x, 2)
  454. Matrix([
  455. [2, y],
  456. [1, 0]])
  457. See Also
  458. ========
  459. integrate
  460. diff
  461. """
  462. return self.applyfunc(lambda x: x.limit(*args))
  463. # https://github.com/sympy/sympy/pull/12854
  464. class MatrixDeprecated(MatrixCommon):
  465. """A class to house deprecated matrix methods."""
  466. def berkowitz_charpoly(self, x=Dummy('lambda'), simplify=_simplify):
  467. return self.charpoly(x=x)
  468. def berkowitz_det(self):
  469. """Computes determinant using Berkowitz method.
  470. See Also
  471. ========
  472. det
  473. berkowitz
  474. """
  475. return self.det(method='berkowitz')
  476. def berkowitz_eigenvals(self, **flags):
  477. """Computes eigenvalues of a Matrix using Berkowitz method.
  478. See Also
  479. ========
  480. berkowitz
  481. """
  482. return self.eigenvals(**flags)
  483. def berkowitz_minors(self):
  484. """Computes principal minors using Berkowitz method.
  485. See Also
  486. ========
  487. berkowitz
  488. """
  489. sign, minors = self.one, []
  490. for poly in self.berkowitz():
  491. minors.append(sign * poly[-1])
  492. sign = -sign
  493. return tuple(minors)
  494. def berkowitz(self):
  495. from sympy.matrices import zeros
  496. berk = ((1,),)
  497. if not self:
  498. return berk
  499. if not self.is_square:
  500. raise NonSquareMatrixError()
  501. A, N = self, self.rows
  502. transforms = [0] * (N - 1)
  503. for n in range(N, 1, -1):
  504. T, k = zeros(n + 1, n), n - 1
  505. R, C = -A[k, :k], A[:k, k]
  506. A, a = A[:k, :k], -A[k, k]
  507. items = [C]
  508. for i in range(0, n - 2):
  509. items.append(A * items[i])
  510. for i, B in enumerate(items):
  511. items[i] = (R * B)[0, 0]
  512. items = [self.one, a] + items
  513. for i in range(n):
  514. T[i:, i] = items[:n - i + 1]
  515. transforms[k - 1] = T
  516. polys = [self._new([self.one, -A[0, 0]])]
  517. for i, T in enumerate(transforms):
  518. polys.append(T * polys[i])
  519. return berk + tuple(map(tuple, polys))
  520. def cofactorMatrix(self, method="berkowitz"):
  521. return self.cofactor_matrix(method=method)
  522. def det_bareis(self):
  523. return _det_bareiss(self)
  524. def det_LU_decomposition(self):
  525. """Compute matrix determinant using LU decomposition.
  526. Note that this method fails if the LU decomposition itself
  527. fails. In particular, if the matrix has no inverse this method
  528. will fail.
  529. TODO: Implement algorithm for sparse matrices (SFF),
  530. http://www.eecis.udel.edu/~saunders/papers/sffge/it5.ps.
  531. See Also
  532. ========
  533. det
  534. det_bareiss
  535. berkowitz_det
  536. """
  537. return self.det(method='lu')
  538. def jordan_cell(self, eigenval, n):
  539. return self.jordan_block(size=n, eigenvalue=eigenval)
  540. def jordan_cells(self, calc_transformation=True):
  541. P, J = self.jordan_form()
  542. return P, J.get_diag_blocks()
  543. def minorEntry(self, i, j, method="berkowitz"):
  544. return self.minor(i, j, method=method)
  545. def minorMatrix(self, i, j):
  546. return self.minor_submatrix(i, j)
  547. def permuteBkwd(self, perm):
  548. """Permute the rows of the matrix with the given permutation in reverse."""
  549. return self.permute_rows(perm, direction='backward')
  550. def permuteFwd(self, perm):
  551. """Permute the rows of the matrix with the given permutation."""
  552. return self.permute_rows(perm, direction='forward')
  553. @Mul._kind_dispatcher.register(_NumberKind, MatrixKind)
  554. def num_mat_mul(k1, k2):
  555. """
  556. Return MatrixKind. The element kind is selected by recursive dispatching.
  557. Do not need to dispatch in reversed order because KindDispatcher
  558. searches for this automatically.
  559. """
  560. # Deal with Mul._kind_dispatcher's commutativity
  561. # XXX: this function is called with either k1 or k2 as MatrixKind because
  562. # the Mul kind dispatcher is commutative. Maybe it shouldn't be. Need to
  563. # swap the args here because NumberKind does not have an element_kind
  564. # attribute.
  565. if not isinstance(k2, MatrixKind):
  566. k1, k2 = k2, k1
  567. elemk = Mul._kind_dispatcher(k1, k2.element_kind)
  568. return MatrixKind(elemk)
  569. @Mul._kind_dispatcher.register(MatrixKind, MatrixKind)
  570. def mat_mat_mul(k1, k2):
  571. """
  572. Return MatrixKind. The element kind is selected by recursive dispatching.
  573. """
  574. elemk = Mul._kind_dispatcher(k1.element_kind, k2.element_kind)
  575. return MatrixKind(elemk)
  576. class MatrixBase(MatrixDeprecated,
  577. MatrixCalculus,
  578. MatrixEigen,
  579. MatrixCommon,
  580. Printable):
  581. """Base class for matrix objects."""
  582. # Added just for numpy compatibility
  583. __array_priority__ = 11
  584. is_Matrix = True
  585. _class_priority = 3
  586. _sympify = staticmethod(sympify)
  587. zero = S.Zero
  588. one = S.One
  589. @property
  590. def kind(self) -> MatrixKind:
  591. elem_kinds = {e.kind for e in self.flat()}
  592. if len(elem_kinds) == 1:
  593. elemkind, = elem_kinds
  594. else:
  595. elemkind = UndefinedKind
  596. return MatrixKind(elemkind)
  597. def flat(self):
  598. return [self[i, j] for i in range(self.rows) for j in range(self.cols)]
  599. def __array__(self, dtype=object):
  600. from .dense import matrix2numpy
  601. return matrix2numpy(self, dtype=dtype)
  602. def __len__(self):
  603. """Return the number of elements of ``self``.
  604. Implemented mainly so bool(Matrix()) == False.
  605. """
  606. return self.rows * self.cols
  607. def _matrix_pow_by_jordan_blocks(self, num):
  608. from sympy.matrices import diag, MutableMatrix
  609. def jordan_cell_power(jc, n):
  610. N = jc.shape[0]
  611. l = jc[0,0]
  612. if l.is_zero:
  613. if N == 1 and n.is_nonnegative:
  614. jc[0,0] = l**n
  615. elif not (n.is_integer and n.is_nonnegative):
  616. raise NonInvertibleMatrixError("Non-invertible matrix can only be raised to a nonnegative integer")
  617. else:
  618. for i in range(N):
  619. jc[0,i] = KroneckerDelta(i, n)
  620. else:
  621. for i in range(N):
  622. bn = binomial(n, i)
  623. if isinstance(bn, binomial):
  624. bn = bn._eval_expand_func()
  625. jc[0,i] = l**(n-i)*bn
  626. for i in range(N):
  627. for j in range(1, N-i):
  628. jc[j,i+j] = jc [j-1,i+j-1]
  629. P, J = self.jordan_form()
  630. jordan_cells = J.get_diag_blocks()
  631. # Make sure jordan_cells matrices are mutable:
  632. jordan_cells = [MutableMatrix(j) for j in jordan_cells]
  633. for j in jordan_cells:
  634. jordan_cell_power(j, num)
  635. return self._new(P.multiply(diag(*jordan_cells))
  636. .multiply(P.inv()))
  637. def __str__(self):
  638. if S.Zero in self.shape:
  639. return 'Matrix(%s, %s, [])' % (self.rows, self.cols)
  640. return "Matrix(%s)" % str(self.tolist())
  641. def _format_str(self, printer=None):
  642. if not printer:
  643. printer = StrPrinter()
  644. # Handle zero dimensions:
  645. if S.Zero in self.shape:
  646. return 'Matrix(%s, %s, [])' % (self.rows, self.cols)
  647. if self.rows == 1:
  648. return "Matrix([%s])" % self.table(printer, rowsep=',\n')
  649. return "Matrix([\n%s])" % self.table(printer, rowsep=',\n')
  650. @classmethod
  651. def irregular(cls, ntop, *matrices, **kwargs):
  652. """Return a matrix filled by the given matrices which
  653. are listed in order of appearance from left to right, top to
  654. bottom as they first appear in the matrix. They must fill the
  655. matrix completely.
  656. Examples
  657. ========
  658. >>> from sympy import ones, Matrix
  659. >>> Matrix.irregular(3, ones(2,1), ones(3,3)*2, ones(2,2)*3,
  660. ... ones(1,1)*4, ones(2,2)*5, ones(1,2)*6, ones(1,2)*7)
  661. Matrix([
  662. [1, 2, 2, 2, 3, 3],
  663. [1, 2, 2, 2, 3, 3],
  664. [4, 2, 2, 2, 5, 5],
  665. [6, 6, 7, 7, 5, 5]])
  666. """
  667. ntop = as_int(ntop)
  668. # make sure we are working with explicit matrices
  669. b = [i.as_explicit() if hasattr(i, 'as_explicit') else i
  670. for i in matrices]
  671. q = list(range(len(b)))
  672. dat = [i.rows for i in b]
  673. active = [q.pop(0) for _ in range(ntop)]
  674. cols = sum([b[i].cols for i in active])
  675. rows = []
  676. while any(dat):
  677. r = []
  678. for a, j in enumerate(active):
  679. r.extend(b[j][-dat[j], :])
  680. dat[j] -= 1
  681. if dat[j] == 0 and q:
  682. active[a] = q.pop(0)
  683. if len(r) != cols:
  684. raise ValueError(filldedent('''
  685. Matrices provided do not appear to fill
  686. the space completely.'''))
  687. rows.append(r)
  688. return cls._new(rows)
  689. @classmethod
  690. def _handle_ndarray(cls, arg):
  691. # NumPy array or matrix or some other object that implements
  692. # __array__. So let's first use this method to get a
  693. # numpy.array() and then make a Python list out of it.
  694. arr = arg.__array__()
  695. if len(arr.shape) == 2:
  696. rows, cols = arr.shape[0], arr.shape[1]
  697. flat_list = [cls._sympify(i) for i in arr.ravel()]
  698. return rows, cols, flat_list
  699. elif len(arr.shape) == 1:
  700. flat_list = [cls._sympify(i) for i in arr]
  701. return arr.shape[0], 1, flat_list
  702. else:
  703. raise NotImplementedError(
  704. "SymPy supports just 1D and 2D matrices")
  705. @classmethod
  706. def _handle_creation_inputs(cls, *args, **kwargs):
  707. """Return the number of rows, cols and flat matrix elements.
  708. Examples
  709. ========
  710. >>> from sympy import Matrix, I
  711. Matrix can be constructed as follows:
  712. * from a nested list of iterables
  713. >>> Matrix( ((1, 2+I), (3, 4)) )
  714. Matrix([
  715. [1, 2 + I],
  716. [3, 4]])
  717. * from un-nested iterable (interpreted as a column)
  718. >>> Matrix( [1, 2] )
  719. Matrix([
  720. [1],
  721. [2]])
  722. * from un-nested iterable with dimensions
  723. >>> Matrix(1, 2, [1, 2] )
  724. Matrix([[1, 2]])
  725. * from no arguments (a 0 x 0 matrix)
  726. >>> Matrix()
  727. Matrix(0, 0, [])
  728. * from a rule
  729. >>> Matrix(2, 2, lambda i, j: i/(j + 1) )
  730. Matrix([
  731. [0, 0],
  732. [1, 1/2]])
  733. See Also
  734. ========
  735. irregular - filling a matrix with irregular blocks
  736. """
  737. from sympy.matrices import SparseMatrix
  738. from sympy.matrices.expressions.matexpr import MatrixSymbol
  739. from sympy.matrices.expressions.blockmatrix import BlockMatrix
  740. flat_list = None
  741. if len(args) == 1:
  742. # Matrix(SparseMatrix(...))
  743. if isinstance(args[0], SparseMatrix):
  744. return args[0].rows, args[0].cols, flatten(args[0].tolist())
  745. # Matrix(Matrix(...))
  746. elif isinstance(args[0], MatrixBase):
  747. return args[0].rows, args[0].cols, args[0].flat()
  748. # Matrix(MatrixSymbol('X', 2, 2))
  749. elif isinstance(args[0], Basic) and args[0].is_Matrix:
  750. return args[0].rows, args[0].cols, args[0].as_explicit().flat()
  751. elif isinstance(args[0], mp.matrix):
  752. M = args[0]
  753. flat_list = [cls._sympify(x) for x in M]
  754. return M.rows, M.cols, flat_list
  755. # Matrix(numpy.ones((2, 2)))
  756. elif hasattr(args[0], "__array__"):
  757. return cls._handle_ndarray(args[0])
  758. # Matrix([1, 2, 3]) or Matrix([[1, 2], [3, 4]])
  759. elif is_sequence(args[0]) \
  760. and not isinstance(args[0], DeferredVector):
  761. dat = list(args[0])
  762. ismat = lambda i: isinstance(i, MatrixBase) and (
  763. evaluate or
  764. isinstance(i, BlockMatrix) or
  765. isinstance(i, MatrixSymbol))
  766. raw = lambda i: is_sequence(i) and not ismat(i)
  767. evaluate = kwargs.get('evaluate', True)
  768. if evaluate:
  769. def make_explicit(x):
  770. """make Block and Symbol explicit"""
  771. if isinstance(x, BlockMatrix):
  772. return x.as_explicit()
  773. elif isinstance(x, MatrixSymbol) and all(_.is_Integer for _ in x.shape):
  774. return x.as_explicit()
  775. else:
  776. return x
  777. def make_explicit_row(row):
  778. # Could be list or could be list of lists
  779. if isinstance(row, (list, tuple)):
  780. return [make_explicit(x) for x in row]
  781. else:
  782. return make_explicit(row)
  783. if isinstance(dat, (list, tuple)):
  784. dat = [make_explicit_row(row) for row in dat]
  785. if dat in ([], [[]]):
  786. rows = cols = 0
  787. flat_list = []
  788. elif not any(raw(i) or ismat(i) for i in dat):
  789. # a column as a list of values
  790. flat_list = [cls._sympify(i) for i in dat]
  791. rows = len(flat_list)
  792. cols = 1 if rows else 0
  793. elif evaluate and all(ismat(i) for i in dat):
  794. # a column as a list of matrices
  795. ncol = {i.cols for i in dat if any(i.shape)}
  796. if ncol:
  797. if len(ncol) != 1:
  798. raise ValueError('mismatched dimensions')
  799. flat_list = [_ for i in dat for r in i.tolist() for _ in r]
  800. cols = ncol.pop()
  801. rows = len(flat_list)//cols
  802. else:
  803. rows = cols = 0
  804. flat_list = []
  805. elif evaluate and any(ismat(i) for i in dat):
  806. ncol = set()
  807. flat_list = []
  808. for i in dat:
  809. if ismat(i):
  810. flat_list.extend(
  811. [k for j in i.tolist() for k in j])
  812. if any(i.shape):
  813. ncol.add(i.cols)
  814. elif raw(i):
  815. if i:
  816. ncol.add(len(i))
  817. flat_list.extend([cls._sympify(ij) for ij in i])
  818. else:
  819. ncol.add(1)
  820. flat_list.append(i)
  821. if len(ncol) > 1:
  822. raise ValueError('mismatched dimensions')
  823. cols = ncol.pop()
  824. rows = len(flat_list)//cols
  825. else:
  826. # list of lists; each sublist is a logical row
  827. # which might consist of many rows if the values in
  828. # the row are matrices
  829. flat_list = []
  830. ncol = set()
  831. rows = cols = 0
  832. for row in dat:
  833. if not is_sequence(row) and \
  834. not getattr(row, 'is_Matrix', False):
  835. raise ValueError('expecting list of lists')
  836. if hasattr(row, '__array__'):
  837. if 0 in row.shape:
  838. continue
  839. elif not row:
  840. continue
  841. if evaluate and all(ismat(i) for i in row):
  842. r, c, flatT = cls._handle_creation_inputs(
  843. [i.T for i in row])
  844. T = reshape(flatT, [c])
  845. flat = \
  846. [T[i][j] for j in range(c) for i in range(r)]
  847. r, c = c, r
  848. else:
  849. r = 1
  850. if getattr(row, 'is_Matrix', False):
  851. c = 1
  852. flat = [row]
  853. else:
  854. c = len(row)
  855. flat = [cls._sympify(i) for i in row]
  856. ncol.add(c)
  857. if len(ncol) > 1:
  858. raise ValueError('mismatched dimensions')
  859. flat_list.extend(flat)
  860. rows += r
  861. cols = ncol.pop() if ncol else 0
  862. elif len(args) == 3:
  863. rows = as_int(args[0])
  864. cols = as_int(args[1])
  865. if rows < 0 or cols < 0:
  866. raise ValueError("Cannot create a {} x {} matrix. "
  867. "Both dimensions must be positive".format(rows, cols))
  868. # Matrix(2, 2, lambda i, j: i+j)
  869. if len(args) == 3 and isinstance(args[2], Callable):
  870. op = args[2]
  871. flat_list = []
  872. for i in range(rows):
  873. flat_list.extend(
  874. [cls._sympify(op(cls._sympify(i), cls._sympify(j)))
  875. for j in range(cols)])
  876. # Matrix(2, 2, [1, 2, 3, 4])
  877. elif len(args) == 3 and is_sequence(args[2]):
  878. flat_list = args[2]
  879. if len(flat_list) != rows * cols:
  880. raise ValueError(
  881. 'List length should be equal to rows*columns')
  882. flat_list = [cls._sympify(i) for i in flat_list]
  883. # Matrix()
  884. elif len(args) == 0:
  885. # Empty Matrix
  886. rows = cols = 0
  887. flat_list = []
  888. if flat_list is None:
  889. raise TypeError(filldedent('''
  890. Data type not understood; expecting list of lists
  891. or lists of values.'''))
  892. return rows, cols, flat_list
  893. def _setitem(self, key, value):
  894. """Helper to set value at location given by key.
  895. Examples
  896. ========
  897. >>> from sympy import Matrix, I, zeros, ones
  898. >>> m = Matrix(((1, 2+I), (3, 4)))
  899. >>> m
  900. Matrix([
  901. [1, 2 + I],
  902. [3, 4]])
  903. >>> m[1, 0] = 9
  904. >>> m
  905. Matrix([
  906. [1, 2 + I],
  907. [9, 4]])
  908. >>> m[1, 0] = [[0, 1]]
  909. To replace row r you assign to position r*m where m
  910. is the number of columns:
  911. >>> M = zeros(4)
  912. >>> m = M.cols
  913. >>> M[3*m] = ones(1, m)*2; M
  914. Matrix([
  915. [0, 0, 0, 0],
  916. [0, 0, 0, 0],
  917. [0, 0, 0, 0],
  918. [2, 2, 2, 2]])
  919. And to replace column c you can assign to position c:
  920. >>> M[2] = ones(m, 1)*4; M
  921. Matrix([
  922. [0, 0, 4, 0],
  923. [0, 0, 4, 0],
  924. [0, 0, 4, 0],
  925. [2, 2, 4, 2]])
  926. """
  927. from .dense import Matrix
  928. is_slice = isinstance(key, slice)
  929. i, j = key = self.key2ij(key)
  930. is_mat = isinstance(value, MatrixBase)
  931. if isinstance(i, slice) or isinstance(j, slice):
  932. if is_mat:
  933. self.copyin_matrix(key, value)
  934. return
  935. if not isinstance(value, Expr) and is_sequence(value):
  936. self.copyin_list(key, value)
  937. return
  938. raise ValueError('unexpected value: %s' % value)
  939. else:
  940. if (not is_mat and
  941. not isinstance(value, Basic) and is_sequence(value)):
  942. value = Matrix(value)
  943. is_mat = True
  944. if is_mat:
  945. if is_slice:
  946. key = (slice(*divmod(i, self.cols)),
  947. slice(*divmod(j, self.cols)))
  948. else:
  949. key = (slice(i, i + value.rows),
  950. slice(j, j + value.cols))
  951. self.copyin_matrix(key, value)
  952. else:
  953. return i, j, self._sympify(value)
  954. return
  955. def add(self, b):
  956. """Return self + b."""
  957. return self + b
  958. def condition_number(self):
  959. """Returns the condition number of a matrix.
  960. This is the maximum singular value divided by the minimum singular value
  961. Examples
  962. ========
  963. >>> from sympy import Matrix, S
  964. >>> A = Matrix([[1, 0, 0], [0, 10, 0], [0, 0, S.One/10]])
  965. >>> A.condition_number()
  966. 100
  967. See Also
  968. ========
  969. singular_values
  970. """
  971. if not self:
  972. return self.zero
  973. singularvalues = self.singular_values()
  974. return Max(*singularvalues) / Min(*singularvalues)
  975. def copy(self):
  976. """
  977. Returns the copy of a matrix.
  978. Examples
  979. ========
  980. >>> from sympy import Matrix
  981. >>> A = Matrix(2, 2, [1, 2, 3, 4])
  982. >>> A.copy()
  983. Matrix([
  984. [1, 2],
  985. [3, 4]])
  986. """
  987. return self._new(self.rows, self.cols, self.flat())
  988. def cross(self, b):
  989. r"""
  990. Return the cross product of ``self`` and ``b`` relaxing the condition
  991. of compatible dimensions: if each has 3 elements, a matrix of the
  992. same type and shape as ``self`` will be returned. If ``b`` has the same
  993. shape as ``self`` then common identities for the cross product (like
  994. `a \times b = - b \times a`) will hold.
  995. Parameters
  996. ==========
  997. b : 3x1 or 1x3 Matrix
  998. See Also
  999. ========
  1000. dot
  1001. multiply
  1002. multiply_elementwise
  1003. """
  1004. from sympy.matrices.expressions.matexpr import MatrixExpr
  1005. if not isinstance(b, (MatrixBase, MatrixExpr)):
  1006. raise TypeError(
  1007. "{} must be a Matrix, not {}.".format(b, type(b)))
  1008. if not (self.rows * self.cols == b.rows * b.cols == 3):
  1009. raise ShapeError("Dimensions incorrect for cross product: %s x %s" %
  1010. ((self.rows, self.cols), (b.rows, b.cols)))
  1011. else:
  1012. return self._new(self.rows, self.cols, (
  1013. (self[1] * b[2] - self[2] * b[1]),
  1014. (self[2] * b[0] - self[0] * b[2]),
  1015. (self[0] * b[1] - self[1] * b[0])))
  1016. @property
  1017. def D(self):
  1018. """Return Dirac conjugate (if ``self.rows == 4``).
  1019. Examples
  1020. ========
  1021. >>> from sympy import Matrix, I, eye
  1022. >>> m = Matrix((0, 1 + I, 2, 3))
  1023. >>> m.D
  1024. Matrix([[0, 1 - I, -2, -3]])
  1025. >>> m = (eye(4) + I*eye(4))
  1026. >>> m[0, 3] = 2
  1027. >>> m.D
  1028. Matrix([
  1029. [1 - I, 0, 0, 0],
  1030. [ 0, 1 - I, 0, 0],
  1031. [ 0, 0, -1 + I, 0],
  1032. [ 2, 0, 0, -1 + I]])
  1033. If the matrix does not have 4 rows an AttributeError will be raised
  1034. because this property is only defined for matrices with 4 rows.
  1035. >>> Matrix(eye(2)).D
  1036. Traceback (most recent call last):
  1037. ...
  1038. AttributeError: Matrix has no attribute D.
  1039. See Also
  1040. ========
  1041. sympy.matrices.common.MatrixCommon.conjugate: By-element conjugation
  1042. sympy.matrices.common.MatrixCommon.H: Hermite conjugation
  1043. """
  1044. from sympy.physics.matrices import mgamma
  1045. if self.rows != 4:
  1046. # In Python 3.2, properties can only return an AttributeError
  1047. # so we can't raise a ShapeError -- see commit which added the
  1048. # first line of this inline comment. Also, there is no need
  1049. # for a message since MatrixBase will raise the AttributeError
  1050. raise AttributeError
  1051. return self.H * mgamma(0)
  1052. def dot(self, b, hermitian=None, conjugate_convention=None):
  1053. """Return the dot or inner product of two vectors of equal length.
  1054. Here ``self`` must be a ``Matrix`` of size 1 x n or n x 1, and ``b``
  1055. must be either a matrix of size 1 x n, n x 1, or a list/tuple of length n.
  1056. A scalar is returned.
  1057. By default, ``dot`` does not conjugate ``self`` or ``b``, even if there are
  1058. complex entries. Set ``hermitian=True`` (and optionally a ``conjugate_convention``)
  1059. to compute the hermitian inner product.
  1060. Possible kwargs are ``hermitian`` and ``conjugate_convention``.
  1061. If ``conjugate_convention`` is ``"left"``, ``"math"`` or ``"maths"``,
  1062. the conjugate of the first vector (``self``) is used. If ``"right"``
  1063. or ``"physics"`` is specified, the conjugate of the second vector ``b`` is used.
  1064. Examples
  1065. ========
  1066. >>> from sympy import Matrix
  1067. >>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  1068. >>> v = Matrix([1, 1, 1])
  1069. >>> M.row(0).dot(v)
  1070. 6
  1071. >>> M.col(0).dot(v)
  1072. 12
  1073. >>> v = [3, 2, 1]
  1074. >>> M.row(0).dot(v)
  1075. 10
  1076. >>> from sympy import I
  1077. >>> q = Matrix([1*I, 1*I, 1*I])
  1078. >>> q.dot(q, hermitian=False)
  1079. -3
  1080. >>> q.dot(q, hermitian=True)
  1081. 3
  1082. >>> q1 = Matrix([1, 1, 1*I])
  1083. >>> q.dot(q1, hermitian=True, conjugate_convention="maths")
  1084. 1 - 2*I
  1085. >>> q.dot(q1, hermitian=True, conjugate_convention="physics")
  1086. 1 + 2*I
  1087. See Also
  1088. ========
  1089. cross
  1090. multiply
  1091. multiply_elementwise
  1092. """
  1093. from .dense import Matrix
  1094. if not isinstance(b, MatrixBase):
  1095. if is_sequence(b):
  1096. if len(b) != self.cols and len(b) != self.rows:
  1097. raise ShapeError(
  1098. "Dimensions incorrect for dot product: %s, %s" % (
  1099. self.shape, len(b)))
  1100. return self.dot(Matrix(b))
  1101. else:
  1102. raise TypeError(
  1103. "`b` must be an ordered iterable or Matrix, not %s." %
  1104. type(b))
  1105. if (1 not in self.shape) or (1 not in b.shape):
  1106. raise ShapeError
  1107. if len(self) != len(b):
  1108. raise ShapeError(
  1109. "Dimensions incorrect for dot product: %s, %s" % (self.shape, b.shape))
  1110. mat = self
  1111. n = len(mat)
  1112. if mat.shape != (1, n):
  1113. mat = mat.reshape(1, n)
  1114. if b.shape != (n, 1):
  1115. b = b.reshape(n, 1)
  1116. # Now ``mat`` is a row vector and ``b`` is a column vector.
  1117. # If it so happens that only conjugate_convention is passed
  1118. # then automatically set hermitian to True. If only hermitian
  1119. # is true but no conjugate_convention is not passed then
  1120. # automatically set it to ``"maths"``
  1121. if conjugate_convention is not None and hermitian is None:
  1122. hermitian = True
  1123. if hermitian and conjugate_convention is None:
  1124. conjugate_convention = "maths"
  1125. if hermitian == True:
  1126. if conjugate_convention in ("maths", "left", "math"):
  1127. mat = mat.conjugate()
  1128. elif conjugate_convention in ("physics", "right"):
  1129. b = b.conjugate()
  1130. else:
  1131. raise ValueError("Unknown conjugate_convention was entered."
  1132. " conjugate_convention must be one of the"
  1133. " following: math, maths, left, physics or right.")
  1134. return (mat * b)[0]
  1135. def dual(self):
  1136. """Returns the dual of a matrix.
  1137. A dual of a matrix is:
  1138. ``(1/2)*levicivita(i, j, k, l)*M(k, l)`` summed over indices `k` and `l`
  1139. Since the levicivita method is anti_symmetric for any pairwise
  1140. exchange of indices, the dual of a symmetric matrix is the zero
  1141. matrix. Strictly speaking the dual defined here assumes that the
  1142. 'matrix' `M` is a contravariant anti_symmetric second rank tensor,
  1143. so that the dual is a covariant second rank tensor.
  1144. """
  1145. from sympy.matrices import zeros
  1146. M, n = self[:, :], self.rows
  1147. work = zeros(n)
  1148. if self.is_symmetric():
  1149. return work
  1150. for i in range(1, n):
  1151. for j in range(1, n):
  1152. acum = 0
  1153. for k in range(1, n):
  1154. acum += LeviCivita(i, j, 0, k) * M[0, k]
  1155. work[i, j] = acum
  1156. work[j, i] = -acum
  1157. for l in range(1, n):
  1158. acum = 0
  1159. for a in range(1, n):
  1160. for b in range(1, n):
  1161. acum += LeviCivita(0, l, a, b) * M[a, b]
  1162. acum /= 2
  1163. work[0, l] = -acum
  1164. work[l, 0] = acum
  1165. return work
  1166. def _eval_matrix_exp_jblock(self):
  1167. """A helper function to compute an exponential of a Jordan block
  1168. matrix
  1169. Examples
  1170. ========
  1171. >>> from sympy import Symbol, Matrix
  1172. >>> l = Symbol('lamda')
  1173. A trivial example of 1*1 Jordan block:
  1174. >>> m = Matrix.jordan_block(1, l)
  1175. >>> m._eval_matrix_exp_jblock()
  1176. Matrix([[exp(lamda)]])
  1177. An example of 3*3 Jordan block:
  1178. >>> m = Matrix.jordan_block(3, l)
  1179. >>> m._eval_matrix_exp_jblock()
  1180. Matrix([
  1181. [exp(lamda), exp(lamda), exp(lamda)/2],
  1182. [ 0, exp(lamda), exp(lamda)],
  1183. [ 0, 0, exp(lamda)]])
  1184. References
  1185. ==========
  1186. .. [1] https://en.wikipedia.org/wiki/Matrix_function#Jordan_decomposition
  1187. """
  1188. size = self.rows
  1189. l = self[0, 0]
  1190. exp_l = exp(l)
  1191. bands = {i: exp_l / factorial(i) for i in range(size)}
  1192. from .sparsetools import banded
  1193. return self.__class__(banded(size, bands))
  1194. def analytic_func(self, f, x):
  1195. """
  1196. Computes f(A) where A is a Square Matrix
  1197. and f is an analytic function.
  1198. Examples
  1199. ========
  1200. >>> from sympy import Symbol, Matrix, S, log
  1201. >>> x = Symbol('x')
  1202. >>> m = Matrix([[S(5)/4, S(3)/4], [S(3)/4, S(5)/4]])
  1203. >>> f = log(x)
  1204. >>> m.analytic_func(f, x)
  1205. Matrix([
  1206. [ 0, log(2)],
  1207. [log(2), 0]])
  1208. Parameters
  1209. ==========
  1210. f : Expr
  1211. Analytic Function
  1212. x : Symbol
  1213. parameter of f
  1214. """
  1215. f, x = _sympify(f), _sympify(x)
  1216. if not self.is_square:
  1217. raise NonSquareMatrixError
  1218. if not x.is_symbol:
  1219. raise ValueError("{} must be a symbol.".format(x))
  1220. if x not in f.free_symbols:
  1221. raise ValueError(
  1222. "{} must be a parameter of {}.".format(x, f))
  1223. if x in self.free_symbols:
  1224. raise ValueError(
  1225. "{} must not be a parameter of {}.".format(x, self))
  1226. eigen = self.eigenvals()
  1227. max_mul = max(eigen.values())
  1228. derivative = {}
  1229. dd = f
  1230. for i in range(max_mul - 1):
  1231. dd = diff(dd, x)
  1232. derivative[i + 1] = dd
  1233. n = self.shape[0]
  1234. r = self.zeros(n)
  1235. f_val = self.zeros(n, 1)
  1236. row = 0
  1237. for i in eigen:
  1238. mul = eigen[i]
  1239. f_val[row] = f.subs(x, i)
  1240. if f_val[row].is_number and not f_val[row].is_complex:
  1241. raise ValueError(
  1242. "Cannot evaluate the function because the "
  1243. "function {} is not analytic at the given "
  1244. "eigenvalue {}".format(f, f_val[row]))
  1245. val = 1
  1246. for a in range(n):
  1247. r[row, a] = val
  1248. val *= i
  1249. if mul > 1:
  1250. coe = [1 for ii in range(n)]
  1251. deri = 1
  1252. while mul > 1:
  1253. row = row + 1
  1254. mul -= 1
  1255. d_i = derivative[deri].subs(x, i)
  1256. if d_i.is_number and not d_i.is_complex:
  1257. raise ValueError(
  1258. "Cannot evaluate the function because the "
  1259. "derivative {} is not analytic at the given "
  1260. "eigenvalue {}".format(derivative[deri], d_i))
  1261. f_val[row] = d_i
  1262. for a in range(n):
  1263. if a - deri + 1 <= 0:
  1264. r[row, a] = 0
  1265. coe[a] = 0
  1266. continue
  1267. coe[a] = coe[a]*(a - deri + 1)
  1268. r[row, a] = coe[a]*pow(i, a - deri)
  1269. deri += 1
  1270. row += 1
  1271. c = r.solve(f_val)
  1272. ans = self.zeros(n)
  1273. pre = self.eye(n)
  1274. for i in range(n):
  1275. ans = ans + c[i]*pre
  1276. pre *= self
  1277. return ans
  1278. def exp(self):
  1279. """Return the exponential of a square matrix.
  1280. Examples
  1281. ========
  1282. >>> from sympy import Symbol, Matrix
  1283. >>> t = Symbol('t')
  1284. >>> m = Matrix([[0, 1], [-1, 0]]) * t
  1285. >>> m.exp()
  1286. Matrix([
  1287. [ exp(I*t)/2 + exp(-I*t)/2, -I*exp(I*t)/2 + I*exp(-I*t)/2],
  1288. [I*exp(I*t)/2 - I*exp(-I*t)/2, exp(I*t)/2 + exp(-I*t)/2]])
  1289. """
  1290. if not self.is_square:
  1291. raise NonSquareMatrixError(
  1292. "Exponentiation is valid only for square matrices")
  1293. try:
  1294. P, J = self.jordan_form()
  1295. cells = J.get_diag_blocks()
  1296. except MatrixError:
  1297. raise NotImplementedError(
  1298. "Exponentiation is implemented only for matrices for which the Jordan normal form can be computed")
  1299. blocks = [cell._eval_matrix_exp_jblock() for cell in cells]
  1300. from sympy.matrices import diag
  1301. eJ = diag(*blocks)
  1302. # n = self.rows
  1303. ret = P.multiply(eJ, dotprodsimp=None).multiply(P.inv(), dotprodsimp=None)
  1304. if all(value.is_real for value in self.values()):
  1305. return type(self)(re(ret))
  1306. else:
  1307. return type(self)(ret)
  1308. def _eval_matrix_log_jblock(self):
  1309. """Helper function to compute logarithm of a jordan block.
  1310. Examples
  1311. ========
  1312. >>> from sympy import Symbol, Matrix
  1313. >>> l = Symbol('lamda')
  1314. A trivial example of 1*1 Jordan block:
  1315. >>> m = Matrix.jordan_block(1, l)
  1316. >>> m._eval_matrix_log_jblock()
  1317. Matrix([[log(lamda)]])
  1318. An example of 3*3 Jordan block:
  1319. >>> m = Matrix.jordan_block(3, l)
  1320. >>> m._eval_matrix_log_jblock()
  1321. Matrix([
  1322. [log(lamda), 1/lamda, -1/(2*lamda**2)],
  1323. [ 0, log(lamda), 1/lamda],
  1324. [ 0, 0, log(lamda)]])
  1325. """
  1326. size = self.rows
  1327. l = self[0, 0]
  1328. if l.is_zero:
  1329. raise MatrixError(
  1330. 'Could not take logarithm or reciprocal for the given '
  1331. 'eigenvalue {}'.format(l))
  1332. bands = {0: log(l)}
  1333. for i in range(1, size):
  1334. bands[i] = -((-l) ** -i) / i
  1335. from .sparsetools import banded
  1336. return self.__class__(banded(size, bands))
  1337. def log(self, simplify=cancel):
  1338. """Return the logarithm of a square matrix.
  1339. Parameters
  1340. ==========
  1341. simplify : function, bool
  1342. The function to simplify the result with.
  1343. Default is ``cancel``, which is effective to reduce the
  1344. expression growing for taking reciprocals and inverses for
  1345. symbolic matrices.
  1346. Examples
  1347. ========
  1348. >>> from sympy import S, Matrix
  1349. Examples for positive-definite matrices:
  1350. >>> m = Matrix([[1, 1], [0, 1]])
  1351. >>> m.log()
  1352. Matrix([
  1353. [0, 1],
  1354. [0, 0]])
  1355. >>> m = Matrix([[S(5)/4, S(3)/4], [S(3)/4, S(5)/4]])
  1356. >>> m.log()
  1357. Matrix([
  1358. [ 0, log(2)],
  1359. [log(2), 0]])
  1360. Examples for non positive-definite matrices:
  1361. >>> m = Matrix([[S(3)/4, S(5)/4], [S(5)/4, S(3)/4]])
  1362. >>> m.log()
  1363. Matrix([
  1364. [ I*pi/2, log(2) - I*pi/2],
  1365. [log(2) - I*pi/2, I*pi/2]])
  1366. >>> m = Matrix(
  1367. ... [[0, 0, 0, 1],
  1368. ... [0, 0, 1, 0],
  1369. ... [0, 1, 0, 0],
  1370. ... [1, 0, 0, 0]])
  1371. >>> m.log()
  1372. Matrix([
  1373. [ I*pi/2, 0, 0, -I*pi/2],
  1374. [ 0, I*pi/2, -I*pi/2, 0],
  1375. [ 0, -I*pi/2, I*pi/2, 0],
  1376. [-I*pi/2, 0, 0, I*pi/2]])
  1377. """
  1378. if not self.is_square:
  1379. raise NonSquareMatrixError(
  1380. "Logarithm is valid only for square matrices")
  1381. try:
  1382. if simplify:
  1383. P, J = simplify(self).jordan_form()
  1384. else:
  1385. P, J = self.jordan_form()
  1386. cells = J.get_diag_blocks()
  1387. except MatrixError:
  1388. raise NotImplementedError(
  1389. "Logarithm is implemented only for matrices for which "
  1390. "the Jordan normal form can be computed")
  1391. blocks = [
  1392. cell._eval_matrix_log_jblock()
  1393. for cell in cells]
  1394. from sympy.matrices import diag
  1395. eJ = diag(*blocks)
  1396. if simplify:
  1397. ret = simplify(P * eJ * simplify(P.inv()))
  1398. ret = self.__class__(ret)
  1399. else:
  1400. ret = P * eJ * P.inv()
  1401. return ret
  1402. def is_nilpotent(self):
  1403. """Checks if a matrix is nilpotent.
  1404. A matrix B is nilpotent if for some integer k, B**k is
  1405. a zero matrix.
  1406. Examples
  1407. ========
  1408. >>> from sympy import Matrix
  1409. >>> a = Matrix([[0, 0, 0], [1, 0, 0], [1, 1, 0]])
  1410. >>> a.is_nilpotent()
  1411. True
  1412. >>> a = Matrix([[1, 0, 1], [1, 0, 0], [1, 1, 0]])
  1413. >>> a.is_nilpotent()
  1414. False
  1415. """
  1416. if not self:
  1417. return True
  1418. if not self.is_square:
  1419. raise NonSquareMatrixError(
  1420. "Nilpotency is valid only for square matrices")
  1421. x = uniquely_named_symbol('x', self, modify=lambda s: '_' + s)
  1422. p = self.charpoly(x)
  1423. if p.args[0] == x ** self.rows:
  1424. return True
  1425. return False
  1426. def key2bounds(self, keys):
  1427. """Converts a key with potentially mixed types of keys (integer and slice)
  1428. into a tuple of ranges and raises an error if any index is out of ``self``'s
  1429. range.
  1430. See Also
  1431. ========
  1432. key2ij
  1433. """
  1434. islice, jslice = [isinstance(k, slice) for k in keys]
  1435. if islice:
  1436. if not self.rows:
  1437. rlo = rhi = 0
  1438. else:
  1439. rlo, rhi = keys[0].indices(self.rows)[:2]
  1440. else:
  1441. rlo = a2idx(keys[0], self.rows)
  1442. rhi = rlo + 1
  1443. if jslice:
  1444. if not self.cols:
  1445. clo = chi = 0
  1446. else:
  1447. clo, chi = keys[1].indices(self.cols)[:2]
  1448. else:
  1449. clo = a2idx(keys[1], self.cols)
  1450. chi = clo + 1
  1451. return rlo, rhi, clo, chi
  1452. def key2ij(self, key):
  1453. """Converts key into canonical form, converting integers or indexable
  1454. items into valid integers for ``self``'s range or returning slices
  1455. unchanged.
  1456. See Also
  1457. ========
  1458. key2bounds
  1459. """
  1460. if is_sequence(key):
  1461. if not len(key) == 2:
  1462. raise TypeError('key must be a sequence of length 2')
  1463. return [a2idx(i, n) if not isinstance(i, slice) else i
  1464. for i, n in zip(key, self.shape)]
  1465. elif isinstance(key, slice):
  1466. return key.indices(len(self))[:2]
  1467. else:
  1468. return divmod(a2idx(key, len(self)), self.cols)
  1469. def normalized(self, iszerofunc=_iszero):
  1470. """Return the normalized version of ``self``.
  1471. Parameters
  1472. ==========
  1473. iszerofunc : Function, optional
  1474. A function to determine whether ``self`` is a zero vector.
  1475. The default ``_iszero`` tests to see if each element is
  1476. exactly zero.
  1477. Returns
  1478. =======
  1479. Matrix
  1480. Normalized vector form of ``self``.
  1481. It has the same length as a unit vector. However, a zero vector
  1482. will be returned for a vector with norm 0.
  1483. Raises
  1484. ======
  1485. ShapeError
  1486. If the matrix is not in a vector form.
  1487. See Also
  1488. ========
  1489. norm
  1490. """
  1491. if self.rows != 1 and self.cols != 1:
  1492. raise ShapeError("A Matrix must be a vector to normalize.")
  1493. norm = self.norm()
  1494. if iszerofunc(norm):
  1495. out = self.zeros(self.rows, self.cols)
  1496. else:
  1497. out = self.applyfunc(lambda i: i / norm)
  1498. return out
  1499. def norm(self, ord=None):
  1500. """Return the Norm of a Matrix or Vector.
  1501. In the simplest case this is the geometric size of the vector
  1502. Other norms can be specified by the ord parameter
  1503. ===== ============================ ==========================
  1504. ord norm for matrices norm for vectors
  1505. ===== ============================ ==========================
  1506. None Frobenius norm 2-norm
  1507. 'fro' Frobenius norm - does not exist
  1508. inf maximum row sum max(abs(x))
  1509. -inf -- min(abs(x))
  1510. 1 maximum column sum as below
  1511. -1 -- as below
  1512. 2 2-norm (largest sing. value) as below
  1513. -2 smallest singular value as below
  1514. other - does not exist sum(abs(x)**ord)**(1./ord)
  1515. ===== ============================ ==========================
  1516. Examples
  1517. ========
  1518. >>> from sympy import Matrix, Symbol, trigsimp, cos, sin, oo
  1519. >>> x = Symbol('x', real=True)
  1520. >>> v = Matrix([cos(x), sin(x)])
  1521. >>> trigsimp( v.norm() )
  1522. 1
  1523. >>> v.norm(10)
  1524. (sin(x)**10 + cos(x)**10)**(1/10)
  1525. >>> A = Matrix([[1, 1], [1, 1]])
  1526. >>> A.norm(1) # maximum sum of absolute values of A is 2
  1527. 2
  1528. >>> A.norm(2) # Spectral norm (max of |Ax|/|x| under 2-vector-norm)
  1529. 2
  1530. >>> A.norm(-2) # Inverse spectral norm (smallest singular value)
  1531. 0
  1532. >>> A.norm() # Frobenius Norm
  1533. 2
  1534. >>> A.norm(oo) # Infinity Norm
  1535. 2
  1536. >>> Matrix([1, -2]).norm(oo)
  1537. 2
  1538. >>> Matrix([-1, 2]).norm(-oo)
  1539. 1
  1540. See Also
  1541. ========
  1542. normalized
  1543. """
  1544. # Row or Column Vector Norms
  1545. vals = list(self.values()) or [0]
  1546. if S.One in self.shape:
  1547. if ord in (2, None): # Common case sqrt(<x, x>)
  1548. return sqrt(Add(*(abs(i) ** 2 for i in vals)))
  1549. elif ord == 1: # sum(abs(x))
  1550. return Add(*(abs(i) for i in vals))
  1551. elif ord is S.Infinity: # max(abs(x))
  1552. return Max(*[abs(i) for i in vals])
  1553. elif ord is S.NegativeInfinity: # min(abs(x))
  1554. return Min(*[abs(i) for i in vals])
  1555. # Otherwise generalize the 2-norm, Sum(x_i**ord)**(1/ord)
  1556. # Note that while useful this is not mathematically a norm
  1557. try:
  1558. return Pow(Add(*(abs(i) ** ord for i in vals)), S.One / ord)
  1559. except (NotImplementedError, TypeError):
  1560. raise ValueError("Expected order to be Number, Symbol, oo")
  1561. # Matrix Norms
  1562. else:
  1563. if ord == 1: # Maximum column sum
  1564. m = self.applyfunc(abs)
  1565. return Max(*[sum(m.col(i)) for i in range(m.cols)])
  1566. elif ord == 2: # Spectral Norm
  1567. # Maximum singular value
  1568. return Max(*self.singular_values())
  1569. elif ord == -2:
  1570. # Minimum singular value
  1571. return Min(*self.singular_values())
  1572. elif ord is S.Infinity: # Infinity Norm - Maximum row sum
  1573. m = self.applyfunc(abs)
  1574. return Max(*[sum(m.row(i)) for i in range(m.rows)])
  1575. elif (ord is None or isinstance(ord,
  1576. str) and ord.lower() in
  1577. ['f', 'fro', 'frobenius', 'vector']):
  1578. # Reshape as vector and send back to norm function
  1579. return self.vec().norm(ord=2)
  1580. else:
  1581. raise NotImplementedError("Matrix Norms under development")
  1582. def print_nonzero(self, symb="X"):
  1583. """Shows location of non-zero entries for fast shape lookup.
  1584. Examples
  1585. ========
  1586. >>> from sympy import Matrix, eye
  1587. >>> m = Matrix(2, 3, lambda i, j: i*3+j)
  1588. >>> m
  1589. Matrix([
  1590. [0, 1, 2],
  1591. [3, 4, 5]])
  1592. >>> m.print_nonzero()
  1593. [ XX]
  1594. [XXX]
  1595. >>> m = eye(4)
  1596. >>> m.print_nonzero("x")
  1597. [x ]
  1598. [ x ]
  1599. [ x ]
  1600. [ x]
  1601. """
  1602. s = []
  1603. for i in range(self.rows):
  1604. line = []
  1605. for j in range(self.cols):
  1606. if self[i, j] == 0:
  1607. line.append(" ")
  1608. else:
  1609. line.append(str(symb))
  1610. s.append("[%s]" % ''.join(line))
  1611. print('\n'.join(s))
  1612. def project(self, v):
  1613. """Return the projection of ``self`` onto the line containing ``v``.
  1614. Examples
  1615. ========
  1616. >>> from sympy import Matrix, S, sqrt
  1617. >>> V = Matrix([sqrt(3)/2, S.Half])
  1618. >>> x = Matrix([[1, 0]])
  1619. >>> V.project(x)
  1620. Matrix([[sqrt(3)/2, 0]])
  1621. >>> V.project(-x)
  1622. Matrix([[sqrt(3)/2, 0]])
  1623. """
  1624. return v * (self.dot(v) / v.dot(v))
  1625. def table(self, printer, rowstart='[', rowend=']', rowsep='\n',
  1626. colsep=', ', align='right'):
  1627. r"""
  1628. String form of Matrix as a table.
  1629. ``printer`` is the printer to use for on the elements (generally
  1630. something like StrPrinter())
  1631. ``rowstart`` is the string used to start each row (by default '[').
  1632. ``rowend`` is the string used to end each row (by default ']').
  1633. ``rowsep`` is the string used to separate rows (by default a newline).
  1634. ``colsep`` is the string used to separate columns (by default ', ').
  1635. ``align`` defines how the elements are aligned. Must be one of 'left',
  1636. 'right', or 'center'. You can also use '<', '>', and '^' to mean the
  1637. same thing, respectively.
  1638. This is used by the string printer for Matrix.
  1639. Examples
  1640. ========
  1641. >>> from sympy import Matrix, StrPrinter
  1642. >>> M = Matrix([[1, 2], [-33, 4]])
  1643. >>> printer = StrPrinter()
  1644. >>> M.table(printer)
  1645. '[ 1, 2]\n[-33, 4]'
  1646. >>> print(M.table(printer))
  1647. [ 1, 2]
  1648. [-33, 4]
  1649. >>> print(M.table(printer, rowsep=',\n'))
  1650. [ 1, 2],
  1651. [-33, 4]
  1652. >>> print('[%s]' % M.table(printer, rowsep=',\n'))
  1653. [[ 1, 2],
  1654. [-33, 4]]
  1655. >>> print(M.table(printer, colsep=' '))
  1656. [ 1 2]
  1657. [-33 4]
  1658. >>> print(M.table(printer, align='center'))
  1659. [ 1 , 2]
  1660. [-33, 4]
  1661. >>> print(M.table(printer, rowstart='{', rowend='}'))
  1662. { 1, 2}
  1663. {-33, 4}
  1664. """
  1665. # Handle zero dimensions:
  1666. if S.Zero in self.shape:
  1667. return '[]'
  1668. # Build table of string representations of the elements
  1669. res = []
  1670. # Track per-column max lengths for pretty alignment
  1671. maxlen = [0] * self.cols
  1672. for i in range(self.rows):
  1673. res.append([])
  1674. for j in range(self.cols):
  1675. s = printer._print(self[i, j])
  1676. res[-1].append(s)
  1677. maxlen[j] = max(len(s), maxlen[j])
  1678. # Patch strings together
  1679. align = {
  1680. 'left': 'ljust',
  1681. 'right': 'rjust',
  1682. 'center': 'center',
  1683. '<': 'ljust',
  1684. '>': 'rjust',
  1685. '^': 'center',
  1686. }[align]
  1687. for i, row in enumerate(res):
  1688. for j, elem in enumerate(row):
  1689. row[j] = getattr(elem, align)(maxlen[j])
  1690. res[i] = rowstart + colsep.join(row) + rowend
  1691. return rowsep.join(res)
  1692. def rank_decomposition(self, iszerofunc=_iszero, simplify=False):
  1693. return _rank_decomposition(self, iszerofunc=iszerofunc,
  1694. simplify=simplify)
  1695. def cholesky(self, hermitian=True):
  1696. raise NotImplementedError('This function is implemented in DenseMatrix or SparseMatrix')
  1697. def LDLdecomposition(self, hermitian=True):
  1698. raise NotImplementedError('This function is implemented in DenseMatrix or SparseMatrix')
  1699. def LUdecomposition(self, iszerofunc=_iszero, simpfunc=None,
  1700. rankcheck=False):
  1701. return _LUdecomposition(self, iszerofunc=iszerofunc, simpfunc=simpfunc,
  1702. rankcheck=rankcheck)
  1703. def LUdecomposition_Simple(self, iszerofunc=_iszero, simpfunc=None,
  1704. rankcheck=False):
  1705. return _LUdecomposition_Simple(self, iszerofunc=iszerofunc,
  1706. simpfunc=simpfunc, rankcheck=rankcheck)
  1707. def LUdecompositionFF(self):
  1708. return _LUdecompositionFF(self)
  1709. def singular_value_decomposition(self):
  1710. return _singular_value_decomposition(self)
  1711. def QRdecomposition(self):
  1712. return _QRdecomposition(self)
  1713. def upper_hessenberg_decomposition(self):
  1714. return _upper_hessenberg_decomposition(self)
  1715. def diagonal_solve(self, rhs):
  1716. return _diagonal_solve(self, rhs)
  1717. def lower_triangular_solve(self, rhs):
  1718. raise NotImplementedError('This function is implemented in DenseMatrix or SparseMatrix')
  1719. def upper_triangular_solve(self, rhs):
  1720. raise NotImplementedError('This function is implemented in DenseMatrix or SparseMatrix')
  1721. def cholesky_solve(self, rhs):
  1722. return _cholesky_solve(self, rhs)
  1723. def LDLsolve(self, rhs):
  1724. return _LDLsolve(self, rhs)
  1725. def LUsolve(self, rhs, iszerofunc=_iszero):
  1726. return _LUsolve(self, rhs, iszerofunc=iszerofunc)
  1727. def QRsolve(self, b):
  1728. return _QRsolve(self, b)
  1729. def gauss_jordan_solve(self, B, freevar=False):
  1730. return _gauss_jordan_solve(self, B, freevar=freevar)
  1731. def pinv_solve(self, B, arbitrary_matrix=None):
  1732. return _pinv_solve(self, B, arbitrary_matrix=arbitrary_matrix)
  1733. def solve(self, rhs, method='GJ'):
  1734. return _solve(self, rhs, method=method)
  1735. def solve_least_squares(self, rhs, method='CH'):
  1736. return _solve_least_squares(self, rhs, method=method)
  1737. def pinv(self, method='RD'):
  1738. return _pinv(self, method=method)
  1739. def inv_mod(self, m):
  1740. return _inv_mod(self, m)
  1741. def inverse_ADJ(self, iszerofunc=_iszero):
  1742. return _inv_ADJ(self, iszerofunc=iszerofunc)
  1743. def inverse_BLOCK(self, iszerofunc=_iszero):
  1744. return _inv_block(self, iszerofunc=iszerofunc)
  1745. def inverse_GE(self, iszerofunc=_iszero):
  1746. return _inv_GE(self, iszerofunc=iszerofunc)
  1747. def inverse_LU(self, iszerofunc=_iszero):
  1748. return _inv_LU(self, iszerofunc=iszerofunc)
  1749. def inverse_CH(self, iszerofunc=_iszero):
  1750. return _inv_CH(self, iszerofunc=iszerofunc)
  1751. def inverse_LDL(self, iszerofunc=_iszero):
  1752. return _inv_LDL(self, iszerofunc=iszerofunc)
  1753. def inverse_QR(self, iszerofunc=_iszero):
  1754. return _inv_QR(self, iszerofunc=iszerofunc)
  1755. def inv(self, method=None, iszerofunc=_iszero, try_block_diag=False):
  1756. return _inv(self, method=method, iszerofunc=iszerofunc,
  1757. try_block_diag=try_block_diag)
  1758. def connected_components(self):
  1759. return _connected_components(self)
  1760. def connected_components_decomposition(self):
  1761. return _connected_components_decomposition(self)
  1762. def strongly_connected_components(self):
  1763. return _strongly_connected_components(self)
  1764. def strongly_connected_components_decomposition(self, lower=True):
  1765. return _strongly_connected_components_decomposition(self, lower=lower)
  1766. _sage_ = Basic._sage_
  1767. rank_decomposition.__doc__ = _rank_decomposition.__doc__
  1768. cholesky.__doc__ = _cholesky.__doc__
  1769. LDLdecomposition.__doc__ = _LDLdecomposition.__doc__
  1770. LUdecomposition.__doc__ = _LUdecomposition.__doc__
  1771. LUdecomposition_Simple.__doc__ = _LUdecomposition_Simple.__doc__
  1772. LUdecompositionFF.__doc__ = _LUdecompositionFF.__doc__
  1773. singular_value_decomposition.__doc__ = _singular_value_decomposition.__doc__
  1774. QRdecomposition.__doc__ = _QRdecomposition.__doc__
  1775. upper_hessenberg_decomposition.__doc__ = _upper_hessenberg_decomposition.__doc__
  1776. diagonal_solve.__doc__ = _diagonal_solve.__doc__
  1777. lower_triangular_solve.__doc__ = _lower_triangular_solve.__doc__
  1778. upper_triangular_solve.__doc__ = _upper_triangular_solve.__doc__
  1779. cholesky_solve.__doc__ = _cholesky_solve.__doc__
  1780. LDLsolve.__doc__ = _LDLsolve.__doc__
  1781. LUsolve.__doc__ = _LUsolve.__doc__
  1782. QRsolve.__doc__ = _QRsolve.__doc__
  1783. gauss_jordan_solve.__doc__ = _gauss_jordan_solve.__doc__
  1784. pinv_solve.__doc__ = _pinv_solve.__doc__
  1785. solve.__doc__ = _solve.__doc__
  1786. solve_least_squares.__doc__ = _solve_least_squares.__doc__
  1787. pinv.__doc__ = _pinv.__doc__
  1788. inv_mod.__doc__ = _inv_mod.__doc__
  1789. inverse_ADJ.__doc__ = _inv_ADJ.__doc__
  1790. inverse_GE.__doc__ = _inv_GE.__doc__
  1791. inverse_LU.__doc__ = _inv_LU.__doc__
  1792. inverse_CH.__doc__ = _inv_CH.__doc__
  1793. inverse_LDL.__doc__ = _inv_LDL.__doc__
  1794. inverse_QR.__doc__ = _inv_QR.__doc__
  1795. inverse_BLOCK.__doc__ = _inv_block.__doc__
  1796. inv.__doc__ = _inv.__doc__
  1797. connected_components.__doc__ = _connected_components.__doc__
  1798. connected_components_decomposition.__doc__ = \
  1799. _connected_components_decomposition.__doc__
  1800. strongly_connected_components.__doc__ = \
  1801. _strongly_connected_components.__doc__
  1802. strongly_connected_components_decomposition.__doc__ = \
  1803. _strongly_connected_components_decomposition.__doc__