common.py 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295
  1. """
  2. Basic methods common to all matrices to be used
  3. when creating more advanced matrices (e.g., matrices over rings,
  4. etc.).
  5. """
  6. from collections import defaultdict
  7. from collections.abc import Iterable
  8. from inspect import isfunction
  9. from functools import reduce
  10. from sympy.assumptions.refine import refine
  11. from sympy.core import SympifyError, Add
  12. from sympy.core.basic import Atom
  13. from sympy.core.decorators import call_highest_priority
  14. from sympy.core.kind import Kind, NumberKind
  15. from sympy.core.logic import fuzzy_and, FuzzyBool
  16. from sympy.core.mod import Mod
  17. from sympy.core.singleton import S
  18. from sympy.core.symbol import Symbol
  19. from sympy.core.sympify import sympify
  20. from sympy.functions import Abs
  21. from sympy.polys.polytools import Poly
  22. from sympy.simplify import simplify as _simplify
  23. from sympy.simplify.simplify import dotprodsimp as _dotprodsimp
  24. from sympy.utilities.exceptions import sympy_deprecation_warning
  25. from sympy.utilities.iterables import flatten, is_sequence
  26. from sympy.utilities.misc import as_int, filldedent
  27. from sympy.tensor.array import NDimArray
  28. from .utilities import _get_intermediate_simp_bool
  29. class MatrixError(Exception):
  30. pass
  31. class ShapeError(ValueError, MatrixError):
  32. """Wrong matrix shape"""
  33. pass
  34. class NonSquareMatrixError(ShapeError):
  35. pass
  36. class NonInvertibleMatrixError(ValueError, MatrixError):
  37. """The matrix in not invertible (division by multidimensional zero error)."""
  38. pass
  39. class NonPositiveDefiniteMatrixError(ValueError, MatrixError):
  40. """The matrix is not a positive-definite matrix."""
  41. pass
  42. class MatrixRequired:
  43. """All subclasses of matrix objects must implement the
  44. required matrix properties listed here."""
  45. rows = None # type: int
  46. cols = None # type: int
  47. _simplify = None
  48. @classmethod
  49. def _new(cls, *args, **kwargs):
  50. """`_new` must, at minimum, be callable as
  51. `_new(rows, cols, mat) where mat is a flat list of the
  52. elements of the matrix."""
  53. raise NotImplementedError("Subclasses must implement this.")
  54. def __eq__(self, other):
  55. raise NotImplementedError("Subclasses must implement this.")
  56. def __getitem__(self, key):
  57. """Implementations of __getitem__ should accept ints, in which
  58. case the matrix is indexed as a flat list, tuples (i,j) in which
  59. case the (i,j) entry is returned, slices, or mixed tuples (a,b)
  60. where a and b are any combination of slices and integers."""
  61. raise NotImplementedError("Subclasses must implement this.")
  62. def __len__(self):
  63. """The total number of entries in the matrix."""
  64. raise NotImplementedError("Subclasses must implement this.")
  65. @property
  66. def shape(self):
  67. raise NotImplementedError("Subclasses must implement this.")
  68. class MatrixShaping(MatrixRequired):
  69. """Provides basic matrix shaping and extracting of submatrices"""
  70. def _eval_col_del(self, col):
  71. def entry(i, j):
  72. return self[i, j] if j < col else self[i, j + 1]
  73. return self._new(self.rows, self.cols - 1, entry)
  74. def _eval_col_insert(self, pos, other):
  75. def entry(i, j):
  76. if j < pos:
  77. return self[i, j]
  78. elif pos <= j < pos + other.cols:
  79. return other[i, j - pos]
  80. return self[i, j - other.cols]
  81. return self._new(self.rows, self.cols + other.cols, entry)
  82. def _eval_col_join(self, other):
  83. rows = self.rows
  84. def entry(i, j):
  85. if i < rows:
  86. return self[i, j]
  87. return other[i - rows, j]
  88. return classof(self, other)._new(self.rows + other.rows, self.cols,
  89. entry)
  90. def _eval_extract(self, rowsList, colsList):
  91. mat = list(self)
  92. cols = self.cols
  93. indices = (i * cols + j for i in rowsList for j in colsList)
  94. return self._new(len(rowsList), len(colsList),
  95. list(mat[i] for i in indices))
  96. def _eval_get_diag_blocks(self):
  97. sub_blocks = []
  98. def recurse_sub_blocks(M):
  99. i = 1
  100. while i <= M.shape[0]:
  101. if i == 1:
  102. to_the_right = M[0, i:]
  103. to_the_bottom = M[i:, 0]
  104. else:
  105. to_the_right = M[:i, i:]
  106. to_the_bottom = M[i:, :i]
  107. if any(to_the_right) or any(to_the_bottom):
  108. i += 1
  109. continue
  110. else:
  111. sub_blocks.append(M[:i, :i])
  112. if M.shape == M[:i, :i].shape:
  113. return
  114. else:
  115. recurse_sub_blocks(M[i:, i:])
  116. return
  117. recurse_sub_blocks(self)
  118. return sub_blocks
  119. def _eval_row_del(self, row):
  120. def entry(i, j):
  121. return self[i, j] if i < row else self[i + 1, j]
  122. return self._new(self.rows - 1, self.cols, entry)
  123. def _eval_row_insert(self, pos, other):
  124. entries = list(self)
  125. insert_pos = pos * self.cols
  126. entries[insert_pos:insert_pos] = list(other)
  127. return self._new(self.rows + other.rows, self.cols, entries)
  128. def _eval_row_join(self, other):
  129. cols = self.cols
  130. def entry(i, j):
  131. if j < cols:
  132. return self[i, j]
  133. return other[i, j - cols]
  134. return classof(self, other)._new(self.rows, self.cols + other.cols,
  135. entry)
  136. def _eval_tolist(self):
  137. return [list(self[i,:]) for i in range(self.rows)]
  138. def _eval_todok(self):
  139. dok = {}
  140. rows, cols = self.shape
  141. for i in range(rows):
  142. for j in range(cols):
  143. val = self[i, j]
  144. if val != self.zero:
  145. dok[i, j] = val
  146. return dok
  147. def _eval_vec(self):
  148. rows = self.rows
  149. def entry(n, _):
  150. # we want to read off the columns first
  151. j = n // rows
  152. i = n - j * rows
  153. return self[i, j]
  154. return self._new(len(self), 1, entry)
  155. def _eval_vech(self, diagonal):
  156. c = self.cols
  157. v = []
  158. if diagonal:
  159. for j in range(c):
  160. for i in range(j, c):
  161. v.append(self[i, j])
  162. else:
  163. for j in range(c):
  164. for i in range(j + 1, c):
  165. v.append(self[i, j])
  166. return self._new(len(v), 1, v)
  167. def col_del(self, col):
  168. """Delete the specified column."""
  169. if col < 0:
  170. col += self.cols
  171. if not 0 <= col < self.cols:
  172. raise IndexError("Column {} is out of range.".format(col))
  173. return self._eval_col_del(col)
  174. def col_insert(self, pos, other):
  175. """Insert one or more columns at the given column position.
  176. Examples
  177. ========
  178. >>> from sympy import zeros, ones
  179. >>> M = zeros(3)
  180. >>> V = ones(3, 1)
  181. >>> M.col_insert(1, V)
  182. Matrix([
  183. [0, 1, 0, 0],
  184. [0, 1, 0, 0],
  185. [0, 1, 0, 0]])
  186. See Also
  187. ========
  188. col
  189. row_insert
  190. """
  191. # Allows you to build a matrix even if it is null matrix
  192. if not self:
  193. return type(self)(other)
  194. pos = as_int(pos)
  195. if pos < 0:
  196. pos = self.cols + pos
  197. if pos < 0:
  198. pos = 0
  199. elif pos > self.cols:
  200. pos = self.cols
  201. if self.rows != other.rows:
  202. raise ShapeError(
  203. "`self` and `other` must have the same number of rows.")
  204. return self._eval_col_insert(pos, other)
  205. def col_join(self, other):
  206. """Concatenates two matrices along self's last and other's first row.
  207. Examples
  208. ========
  209. >>> from sympy import zeros, ones
  210. >>> M = zeros(3)
  211. >>> V = ones(1, 3)
  212. >>> M.col_join(V)
  213. Matrix([
  214. [0, 0, 0],
  215. [0, 0, 0],
  216. [0, 0, 0],
  217. [1, 1, 1]])
  218. See Also
  219. ========
  220. col
  221. row_join
  222. """
  223. # A null matrix can always be stacked (see #10770)
  224. if self.rows == 0 and self.cols != other.cols:
  225. return self._new(0, other.cols, []).col_join(other)
  226. if self.cols != other.cols:
  227. raise ShapeError(
  228. "`self` and `other` must have the same number of columns.")
  229. return self._eval_col_join(other)
  230. def col(self, j):
  231. """Elementary column selector.
  232. Examples
  233. ========
  234. >>> from sympy import eye
  235. >>> eye(2).col(0)
  236. Matrix([
  237. [1],
  238. [0]])
  239. See Also
  240. ========
  241. row
  242. col_del
  243. col_join
  244. col_insert
  245. """
  246. return self[:, j]
  247. def extract(self, rowsList, colsList):
  248. r"""Return a submatrix by specifying a list of rows and columns.
  249. Negative indices can be given. All indices must be in the range
  250. $-n \le i < n$ where $n$ is the number of rows or columns.
  251. Examples
  252. ========
  253. >>> from sympy import Matrix
  254. >>> m = Matrix(4, 3, range(12))
  255. >>> m
  256. Matrix([
  257. [0, 1, 2],
  258. [3, 4, 5],
  259. [6, 7, 8],
  260. [9, 10, 11]])
  261. >>> m.extract([0, 1, 3], [0, 1])
  262. Matrix([
  263. [0, 1],
  264. [3, 4],
  265. [9, 10]])
  266. Rows or columns can be repeated:
  267. >>> m.extract([0, 0, 1], [-1])
  268. Matrix([
  269. [2],
  270. [2],
  271. [5]])
  272. Every other row can be taken by using range to provide the indices:
  273. >>> m.extract(range(0, m.rows, 2), [-1])
  274. Matrix([
  275. [2],
  276. [8]])
  277. RowsList or colsList can also be a list of booleans, in which case
  278. the rows or columns corresponding to the True values will be selected:
  279. >>> m.extract([0, 1, 2, 3], [True, False, True])
  280. Matrix([
  281. [0, 2],
  282. [3, 5],
  283. [6, 8],
  284. [9, 11]])
  285. """
  286. if not is_sequence(rowsList) or not is_sequence(colsList):
  287. raise TypeError("rowsList and colsList must be iterable")
  288. # ensure rowsList and colsList are lists of integers
  289. if rowsList and all(isinstance(i, bool) for i in rowsList):
  290. rowsList = [index for index, item in enumerate(rowsList) if item]
  291. if colsList and all(isinstance(i, bool) for i in colsList):
  292. colsList = [index for index, item in enumerate(colsList) if item]
  293. # ensure everything is in range
  294. rowsList = [a2idx(k, self.rows) for k in rowsList]
  295. colsList = [a2idx(k, self.cols) for k in colsList]
  296. return self._eval_extract(rowsList, colsList)
  297. def get_diag_blocks(self):
  298. """Obtains the square sub-matrices on the main diagonal of a square matrix.
  299. Useful for inverting symbolic matrices or solving systems of
  300. linear equations which may be decoupled by having a block diagonal
  301. structure.
  302. Examples
  303. ========
  304. >>> from sympy import Matrix
  305. >>> from sympy.abc import x, y, z
  306. >>> A = Matrix([[1, 3, 0, 0], [y, z*z, 0, 0], [0, 0, x, 0], [0, 0, 0, 0]])
  307. >>> a1, a2, a3 = A.get_diag_blocks()
  308. >>> a1
  309. Matrix([
  310. [1, 3],
  311. [y, z**2]])
  312. >>> a2
  313. Matrix([[x]])
  314. >>> a3
  315. Matrix([[0]])
  316. """
  317. return self._eval_get_diag_blocks()
  318. @classmethod
  319. def hstack(cls, *args):
  320. """Return a matrix formed by joining args horizontally (i.e.
  321. by repeated application of row_join).
  322. Examples
  323. ========
  324. >>> from sympy import Matrix, eye
  325. >>> Matrix.hstack(eye(2), 2*eye(2))
  326. Matrix([
  327. [1, 0, 2, 0],
  328. [0, 1, 0, 2]])
  329. """
  330. if len(args) == 0:
  331. return cls._new()
  332. kls = type(args[0])
  333. return reduce(kls.row_join, args)
  334. def reshape(self, rows, cols):
  335. """Reshape the matrix. Total number of elements must remain the same.
  336. Examples
  337. ========
  338. >>> from sympy import Matrix
  339. >>> m = Matrix(2, 3, lambda i, j: 1)
  340. >>> m
  341. Matrix([
  342. [1, 1, 1],
  343. [1, 1, 1]])
  344. >>> m.reshape(1, 6)
  345. Matrix([[1, 1, 1, 1, 1, 1]])
  346. >>> m.reshape(3, 2)
  347. Matrix([
  348. [1, 1],
  349. [1, 1],
  350. [1, 1]])
  351. """
  352. if self.rows * self.cols != rows * cols:
  353. raise ValueError("Invalid reshape parameters %d %d" % (rows, cols))
  354. return self._new(rows, cols, lambda i, j: self[i * cols + j])
  355. def row_del(self, row):
  356. """Delete the specified row."""
  357. if row < 0:
  358. row += self.rows
  359. if not 0 <= row < self.rows:
  360. raise IndexError("Row {} is out of range.".format(row))
  361. return self._eval_row_del(row)
  362. def row_insert(self, pos, other):
  363. """Insert one or more rows at the given row position.
  364. Examples
  365. ========
  366. >>> from sympy import zeros, ones
  367. >>> M = zeros(3)
  368. >>> V = ones(1, 3)
  369. >>> M.row_insert(1, V)
  370. Matrix([
  371. [0, 0, 0],
  372. [1, 1, 1],
  373. [0, 0, 0],
  374. [0, 0, 0]])
  375. See Also
  376. ========
  377. row
  378. col_insert
  379. """
  380. # Allows you to build a matrix even if it is null matrix
  381. if not self:
  382. return self._new(other)
  383. pos = as_int(pos)
  384. if pos < 0:
  385. pos = self.rows + pos
  386. if pos < 0:
  387. pos = 0
  388. elif pos > self.rows:
  389. pos = self.rows
  390. if self.cols != other.cols:
  391. raise ShapeError(
  392. "`self` and `other` must have the same number of columns.")
  393. return self._eval_row_insert(pos, other)
  394. def row_join(self, other):
  395. """Concatenates two matrices along self's last and rhs's first column
  396. Examples
  397. ========
  398. >>> from sympy import zeros, ones
  399. >>> M = zeros(3)
  400. >>> V = ones(3, 1)
  401. >>> M.row_join(V)
  402. Matrix([
  403. [0, 0, 0, 1],
  404. [0, 0, 0, 1],
  405. [0, 0, 0, 1]])
  406. See Also
  407. ========
  408. row
  409. col_join
  410. """
  411. # A null matrix can always be stacked (see #10770)
  412. if self.cols == 0 and self.rows != other.rows:
  413. return self._new(other.rows, 0, []).row_join(other)
  414. if self.rows != other.rows:
  415. raise ShapeError(
  416. "`self` and `rhs` must have the same number of rows.")
  417. return self._eval_row_join(other)
  418. def diagonal(self, k=0):
  419. """Returns the kth diagonal of self. The main diagonal
  420. corresponds to `k=0`; diagonals above and below correspond to
  421. `k > 0` and `k < 0`, respectively. The values of `self[i, j]`
  422. for which `j - i = k`, are returned in order of increasing
  423. `i + j`, starting with `i + j = |k|`.
  424. Examples
  425. ========
  426. >>> from sympy import Matrix
  427. >>> m = Matrix(3, 3, lambda i, j: j - i); m
  428. Matrix([
  429. [ 0, 1, 2],
  430. [-1, 0, 1],
  431. [-2, -1, 0]])
  432. >>> _.diagonal()
  433. Matrix([[0, 0, 0]])
  434. >>> m.diagonal(1)
  435. Matrix([[1, 1]])
  436. >>> m.diagonal(-2)
  437. Matrix([[-2]])
  438. Even though the diagonal is returned as a Matrix, the element
  439. retrieval can be done with a single index:
  440. >>> Matrix.diag(1, 2, 3).diagonal()[1] # instead of [0, 1]
  441. 2
  442. See Also
  443. ========
  444. diag - to create a diagonal matrix
  445. """
  446. rv = []
  447. k = as_int(k)
  448. r = 0 if k > 0 else -k
  449. c = 0 if r else k
  450. while True:
  451. if r == self.rows or c == self.cols:
  452. break
  453. rv.append(self[r, c])
  454. r += 1
  455. c += 1
  456. if not rv:
  457. raise ValueError(filldedent('''
  458. The %s diagonal is out of range [%s, %s]''' % (
  459. k, 1 - self.rows, self.cols - 1)))
  460. return self._new(1, len(rv), rv)
  461. def row(self, i):
  462. """Elementary row selector.
  463. Examples
  464. ========
  465. >>> from sympy import eye
  466. >>> eye(2).row(0)
  467. Matrix([[1, 0]])
  468. See Also
  469. ========
  470. col
  471. row_del
  472. row_join
  473. row_insert
  474. """
  475. return self[i, :]
  476. @property
  477. def shape(self):
  478. """The shape (dimensions) of the matrix as the 2-tuple (rows, cols).
  479. Examples
  480. ========
  481. >>> from sympy import zeros
  482. >>> M = zeros(2, 3)
  483. >>> M.shape
  484. (2, 3)
  485. >>> M.rows
  486. 2
  487. >>> M.cols
  488. 3
  489. """
  490. return (self.rows, self.cols)
  491. def todok(self):
  492. """Return the matrix as dictionary of keys.
  493. Examples
  494. ========
  495. >>> from sympy import Matrix
  496. >>> M = Matrix.eye(3)
  497. >>> M.todok()
  498. {(0, 0): 1, (1, 1): 1, (2, 2): 1}
  499. """
  500. return self._eval_todok()
  501. def tolist(self):
  502. """Return the Matrix as a nested Python list.
  503. Examples
  504. ========
  505. >>> from sympy import Matrix, ones
  506. >>> m = Matrix(3, 3, range(9))
  507. >>> m
  508. Matrix([
  509. [0, 1, 2],
  510. [3, 4, 5],
  511. [6, 7, 8]])
  512. >>> m.tolist()
  513. [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
  514. >>> ones(3, 0).tolist()
  515. [[], [], []]
  516. When there are no rows then it will not be possible to tell how
  517. many columns were in the original matrix:
  518. >>> ones(0, 3).tolist()
  519. []
  520. """
  521. if not self.rows:
  522. return []
  523. if not self.cols:
  524. return [[] for i in range(self.rows)]
  525. return self._eval_tolist()
  526. def todod(M):
  527. """Returns matrix as dict of dicts containing non-zero elements of the Matrix
  528. Examples
  529. ========
  530. >>> from sympy import Matrix
  531. >>> A = Matrix([[0, 1],[0, 3]])
  532. >>> A
  533. Matrix([
  534. [0, 1],
  535. [0, 3]])
  536. >>> A.todod()
  537. {0: {1: 1}, 1: {1: 3}}
  538. """
  539. rowsdict = {}
  540. Mlol = M.tolist()
  541. for i, Mi in enumerate(Mlol):
  542. row = {j: Mij for j, Mij in enumerate(Mi) if Mij}
  543. if row:
  544. rowsdict[i] = row
  545. return rowsdict
  546. def vec(self):
  547. """Return the Matrix converted into a one column matrix by stacking columns
  548. Examples
  549. ========
  550. >>> from sympy import Matrix
  551. >>> m=Matrix([[1, 3], [2, 4]])
  552. >>> m
  553. Matrix([
  554. [1, 3],
  555. [2, 4]])
  556. >>> m.vec()
  557. Matrix([
  558. [1],
  559. [2],
  560. [3],
  561. [4]])
  562. See Also
  563. ========
  564. vech
  565. """
  566. return self._eval_vec()
  567. def vech(self, diagonal=True, check_symmetry=True):
  568. """Reshapes the matrix into a column vector by stacking the
  569. elements in the lower triangle.
  570. Parameters
  571. ==========
  572. diagonal : bool, optional
  573. If ``True``, it includes the diagonal elements.
  574. check_symmetry : bool, optional
  575. If ``True``, it checks whether the matrix is symmetric.
  576. Examples
  577. ========
  578. >>> from sympy import Matrix
  579. >>> m=Matrix([[1, 2], [2, 3]])
  580. >>> m
  581. Matrix([
  582. [1, 2],
  583. [2, 3]])
  584. >>> m.vech()
  585. Matrix([
  586. [1],
  587. [2],
  588. [3]])
  589. >>> m.vech(diagonal=False)
  590. Matrix([[2]])
  591. Notes
  592. =====
  593. This should work for symmetric matrices and ``vech`` can
  594. represent symmetric matrices in vector form with less size than
  595. ``vec``.
  596. See Also
  597. ========
  598. vec
  599. """
  600. if not self.is_square:
  601. raise NonSquareMatrixError
  602. if check_symmetry and not self.is_symmetric():
  603. raise ValueError("The matrix is not symmetric.")
  604. return self._eval_vech(diagonal)
  605. @classmethod
  606. def vstack(cls, *args):
  607. """Return a matrix formed by joining args vertically (i.e.
  608. by repeated application of col_join).
  609. Examples
  610. ========
  611. >>> from sympy import Matrix, eye
  612. >>> Matrix.vstack(eye(2), 2*eye(2))
  613. Matrix([
  614. [1, 0],
  615. [0, 1],
  616. [2, 0],
  617. [0, 2]])
  618. """
  619. if len(args) == 0:
  620. return cls._new()
  621. kls = type(args[0])
  622. return reduce(kls.col_join, args)
  623. class MatrixSpecial(MatrixRequired):
  624. """Construction of special matrices"""
  625. @classmethod
  626. def _eval_diag(cls, rows, cols, diag_dict):
  627. """diag_dict is a defaultdict containing
  628. all the entries of the diagonal matrix."""
  629. def entry(i, j):
  630. return diag_dict[(i, j)]
  631. return cls._new(rows, cols, entry)
  632. @classmethod
  633. def _eval_eye(cls, rows, cols):
  634. vals = [cls.zero]*(rows*cols)
  635. vals[::cols+1] = [cls.one]*min(rows, cols)
  636. return cls._new(rows, cols, vals, copy=False)
  637. @classmethod
  638. def _eval_jordan_block(cls, rows, cols, eigenvalue, band='upper'):
  639. if band == 'lower':
  640. def entry(i, j):
  641. if i == j:
  642. return eigenvalue
  643. elif j + 1 == i:
  644. return cls.one
  645. return cls.zero
  646. else:
  647. def entry(i, j):
  648. if i == j:
  649. return eigenvalue
  650. elif i + 1 == j:
  651. return cls.one
  652. return cls.zero
  653. return cls._new(rows, cols, entry)
  654. @classmethod
  655. def _eval_ones(cls, rows, cols):
  656. def entry(i, j):
  657. return cls.one
  658. return cls._new(rows, cols, entry)
  659. @classmethod
  660. def _eval_zeros(cls, rows, cols):
  661. return cls._new(rows, cols, [cls.zero]*(rows*cols), copy=False)
  662. @classmethod
  663. def _eval_wilkinson(cls, n):
  664. def entry(i, j):
  665. return cls.one if i + 1 == j else cls.zero
  666. D = cls._new(2*n + 1, 2*n + 1, entry)
  667. wminus = cls.diag([i for i in range(-n, n + 1)], unpack=True) + D + D.T
  668. wplus = abs(cls.diag([i for i in range(-n, n + 1)], unpack=True)) + D + D.T
  669. return wminus, wplus
  670. @classmethod
  671. def diag(kls, *args, strict=False, unpack=True, rows=None, cols=None, **kwargs):
  672. """Returns a matrix with the specified diagonal.
  673. If matrices are passed, a block-diagonal matrix
  674. is created (i.e. the "direct sum" of the matrices).
  675. kwargs
  676. ======
  677. rows : rows of the resulting matrix; computed if
  678. not given.
  679. cols : columns of the resulting matrix; computed if
  680. not given.
  681. cls : class for the resulting matrix
  682. unpack : bool which, when True (default), unpacks a single
  683. sequence rather than interpreting it as a Matrix.
  684. strict : bool which, when False (default), allows Matrices to
  685. have variable-length rows.
  686. Examples
  687. ========
  688. >>> from sympy import Matrix
  689. >>> Matrix.diag(1, 2, 3)
  690. Matrix([
  691. [1, 0, 0],
  692. [0, 2, 0],
  693. [0, 0, 3]])
  694. The current default is to unpack a single sequence. If this is
  695. not desired, set `unpack=False` and it will be interpreted as
  696. a matrix.
  697. >>> Matrix.diag([1, 2, 3]) == Matrix.diag(1, 2, 3)
  698. True
  699. When more than one element is passed, each is interpreted as
  700. something to put on the diagonal. Lists are converted to
  701. matrices. Filling of the diagonal always continues from
  702. the bottom right hand corner of the previous item: this
  703. will create a block-diagonal matrix whether the matrices
  704. are square or not.
  705. >>> col = [1, 2, 3]
  706. >>> row = [[4, 5]]
  707. >>> Matrix.diag(col, row)
  708. Matrix([
  709. [1, 0, 0],
  710. [2, 0, 0],
  711. [3, 0, 0],
  712. [0, 4, 5]])
  713. When `unpack` is False, elements within a list need not all be
  714. of the same length. Setting `strict` to True would raise a
  715. ValueError for the following:
  716. >>> Matrix.diag([[1, 2, 3], [4, 5], [6]], unpack=False)
  717. Matrix([
  718. [1, 2, 3],
  719. [4, 5, 0],
  720. [6, 0, 0]])
  721. The type of the returned matrix can be set with the ``cls``
  722. keyword.
  723. >>> from sympy import ImmutableMatrix
  724. >>> from sympy.utilities.misc import func_name
  725. >>> func_name(Matrix.diag(1, cls=ImmutableMatrix))
  726. 'ImmutableDenseMatrix'
  727. A zero dimension matrix can be used to position the start of
  728. the filling at the start of an arbitrary row or column:
  729. >>> from sympy import ones
  730. >>> r2 = ones(0, 2)
  731. >>> Matrix.diag(r2, 1, 2)
  732. Matrix([
  733. [0, 0, 1, 0],
  734. [0, 0, 0, 2]])
  735. See Also
  736. ========
  737. eye
  738. diagonal - to extract a diagonal
  739. .dense.diag
  740. .expressions.blockmatrix.BlockMatrix
  741. .sparsetools.banded - to create multi-diagonal matrices
  742. """
  743. from sympy.matrices.matrices import MatrixBase
  744. from sympy.matrices.dense import Matrix
  745. from sympy.matrices import SparseMatrix
  746. klass = kwargs.get('cls', kls)
  747. if unpack and len(args) == 1 and is_sequence(args[0]) and \
  748. not isinstance(args[0], MatrixBase):
  749. args = args[0]
  750. # fill a default dict with the diagonal entries
  751. diag_entries = defaultdict(int)
  752. rmax = cmax = 0 # keep track of the biggest index seen
  753. for m in args:
  754. if isinstance(m, list):
  755. if strict:
  756. # if malformed, Matrix will raise an error
  757. _ = Matrix(m)
  758. r, c = _.shape
  759. m = _.tolist()
  760. else:
  761. r, c, smat = SparseMatrix._handle_creation_inputs(m)
  762. for (i, j), _ in smat.items():
  763. diag_entries[(i + rmax, j + cmax)] = _
  764. m = [] # to skip process below
  765. elif hasattr(m, 'shape'): # a Matrix
  766. # convert to list of lists
  767. r, c = m.shape
  768. m = m.tolist()
  769. else: # in this case, we're a single value
  770. diag_entries[(rmax, cmax)] = m
  771. rmax += 1
  772. cmax += 1
  773. continue
  774. # process list of lists
  775. for i in range(len(m)):
  776. for j, _ in enumerate(m[i]):
  777. diag_entries[(i + rmax, j + cmax)] = _
  778. rmax += r
  779. cmax += c
  780. if rows is None:
  781. rows, cols = cols, rows
  782. if rows is None:
  783. rows, cols = rmax, cmax
  784. else:
  785. cols = rows if cols is None else cols
  786. if rows < rmax or cols < cmax:
  787. raise ValueError(filldedent('''
  788. The constructed matrix is {} x {} but a size of {} x {}
  789. was specified.'''.format(rmax, cmax, rows, cols)))
  790. return klass._eval_diag(rows, cols, diag_entries)
  791. @classmethod
  792. def eye(kls, rows, cols=None, **kwargs):
  793. """Returns an identity matrix.
  794. Args
  795. ====
  796. rows : rows of the matrix
  797. cols : cols of the matrix (if None, cols=rows)
  798. kwargs
  799. ======
  800. cls : class of the returned matrix
  801. """
  802. if cols is None:
  803. cols = rows
  804. if rows < 0 or cols < 0:
  805. raise ValueError("Cannot create a {} x {} matrix. "
  806. "Both dimensions must be positive".format(rows, cols))
  807. klass = kwargs.get('cls', kls)
  808. rows, cols = as_int(rows), as_int(cols)
  809. return klass._eval_eye(rows, cols)
  810. @classmethod
  811. def jordan_block(kls, size=None, eigenvalue=None, *, band='upper', **kwargs):
  812. """Returns a Jordan block
  813. Parameters
  814. ==========
  815. size : Integer, optional
  816. Specifies the shape of the Jordan block matrix.
  817. eigenvalue : Number or Symbol
  818. Specifies the value for the main diagonal of the matrix.
  819. .. note::
  820. The keyword ``eigenval`` is also specified as an alias
  821. of this keyword, but it is not recommended to use.
  822. We may deprecate the alias in later release.
  823. band : 'upper' or 'lower', optional
  824. Specifies the position of the off-diagonal to put `1` s on.
  825. cls : Matrix, optional
  826. Specifies the matrix class of the output form.
  827. If it is not specified, the class type where the method is
  828. being executed on will be returned.
  829. rows, cols : Integer, optional
  830. Specifies the shape of the Jordan block matrix. See Notes
  831. section for the details of how these key works.
  832. .. deprecated:: 1.4
  833. The rows and cols parameters are deprecated and will be
  834. removed in a future version.
  835. Returns
  836. =======
  837. Matrix
  838. A Jordan block matrix.
  839. Raises
  840. ======
  841. ValueError
  842. If insufficient arguments are given for matrix size
  843. specification, or no eigenvalue is given.
  844. Examples
  845. ========
  846. Creating a default Jordan block:
  847. >>> from sympy import Matrix
  848. >>> from sympy.abc import x
  849. >>> Matrix.jordan_block(4, x)
  850. Matrix([
  851. [x, 1, 0, 0],
  852. [0, x, 1, 0],
  853. [0, 0, x, 1],
  854. [0, 0, 0, x]])
  855. Creating an alternative Jordan block matrix where `1` is on
  856. lower off-diagonal:
  857. >>> Matrix.jordan_block(4, x, band='lower')
  858. Matrix([
  859. [x, 0, 0, 0],
  860. [1, x, 0, 0],
  861. [0, 1, x, 0],
  862. [0, 0, 1, x]])
  863. Creating a Jordan block with keyword arguments
  864. >>> Matrix.jordan_block(size=4, eigenvalue=x)
  865. Matrix([
  866. [x, 1, 0, 0],
  867. [0, x, 1, 0],
  868. [0, 0, x, 1],
  869. [0, 0, 0, x]])
  870. Notes
  871. =====
  872. .. deprecated:: 1.4
  873. This feature is deprecated and will be removed in a future
  874. version.
  875. The keyword arguments ``size``, ``rows``, ``cols`` relates to
  876. the Jordan block size specifications.
  877. If you want to create a square Jordan block, specify either
  878. one of the three arguments.
  879. If you want to create a rectangular Jordan block, specify
  880. ``rows`` and ``cols`` individually.
  881. +--------------------------------+---------------------+
  882. | Arguments Given | Matrix Shape |
  883. +----------+----------+----------+----------+----------+
  884. | size | rows | cols | rows | cols |
  885. +==========+==========+==========+==========+==========+
  886. | size | Any | size | size |
  887. +----------+----------+----------+----------+----------+
  888. | | None | ValueError |
  889. | +----------+----------+----------+----------+
  890. | None | rows | None | rows | rows |
  891. | +----------+----------+----------+----------+
  892. | | None | cols | cols | cols |
  893. + +----------+----------+----------+----------+
  894. | | rows | cols | rows | cols |
  895. +----------+----------+----------+----------+----------+
  896. References
  897. ==========
  898. .. [1] https://en.wikipedia.org/wiki/Jordan_matrix
  899. """
  900. if 'rows' in kwargs or 'cols' in kwargs:
  901. msg = """
  902. The 'rows' and 'cols' keywords to Matrix.jordan_block() are
  903. deprecated. Use the 'size' parameter instead.
  904. """
  905. if 'rows' in kwargs and 'cols' in kwargs:
  906. msg += f"""\
  907. To get a non-square Jordan block matrix use a more generic
  908. banded matrix constructor, like
  909. def entry(i, j):
  910. if i == j:
  911. return eigenvalue
  912. elif {"i + 1 == j" if band == 'upper' else "j + 1 == i"}:
  913. return 1
  914. return 0
  915. Matrix({kwargs['rows']}, {kwargs['cols']}, entry)
  916. """
  917. sympy_deprecation_warning(msg, deprecated_since_version="1.4",
  918. active_deprecations_target="deprecated-matrix-jordan_block-rows-cols")
  919. klass = kwargs.pop('cls', kls)
  920. rows = kwargs.pop('rows', None)
  921. cols = kwargs.pop('cols', None)
  922. eigenval = kwargs.get('eigenval', None)
  923. if eigenvalue is None and eigenval is None:
  924. raise ValueError("Must supply an eigenvalue")
  925. elif eigenvalue != eigenval and None not in (eigenval, eigenvalue):
  926. raise ValueError(
  927. "Inconsistent values are given: 'eigenval'={}, "
  928. "'eigenvalue'={}".format(eigenval, eigenvalue))
  929. else:
  930. if eigenval is not None:
  931. eigenvalue = eigenval
  932. if (size, rows, cols) == (None, None, None):
  933. raise ValueError("Must supply a matrix size")
  934. if size is not None:
  935. rows, cols = size, size
  936. elif rows is not None and cols is None:
  937. cols = rows
  938. elif cols is not None and rows is None:
  939. rows = cols
  940. rows, cols = as_int(rows), as_int(cols)
  941. return klass._eval_jordan_block(rows, cols, eigenvalue, band)
  942. @classmethod
  943. def ones(kls, rows, cols=None, **kwargs):
  944. """Returns a matrix of ones.
  945. Args
  946. ====
  947. rows : rows of the matrix
  948. cols : cols of the matrix (if None, cols=rows)
  949. kwargs
  950. ======
  951. cls : class of the returned matrix
  952. """
  953. if cols is None:
  954. cols = rows
  955. klass = kwargs.get('cls', kls)
  956. rows, cols = as_int(rows), as_int(cols)
  957. return klass._eval_ones(rows, cols)
  958. @classmethod
  959. def zeros(kls, rows, cols=None, **kwargs):
  960. """Returns a matrix of zeros.
  961. Args
  962. ====
  963. rows : rows of the matrix
  964. cols : cols of the matrix (if None, cols=rows)
  965. kwargs
  966. ======
  967. cls : class of the returned matrix
  968. """
  969. if cols is None:
  970. cols = rows
  971. if rows < 0 or cols < 0:
  972. raise ValueError("Cannot create a {} x {} matrix. "
  973. "Both dimensions must be positive".format(rows, cols))
  974. klass = kwargs.get('cls', kls)
  975. rows, cols = as_int(rows), as_int(cols)
  976. return klass._eval_zeros(rows, cols)
  977. @classmethod
  978. def companion(kls, poly):
  979. """Returns a companion matrix of a polynomial.
  980. Examples
  981. ========
  982. >>> from sympy import Matrix, Poly, Symbol, symbols
  983. >>> x = Symbol('x')
  984. >>> c0, c1, c2, c3, c4 = symbols('c0:5')
  985. >>> p = Poly(c0 + c1*x + c2*x**2 + c3*x**3 + c4*x**4 + x**5, x)
  986. >>> Matrix.companion(p)
  987. Matrix([
  988. [0, 0, 0, 0, -c0],
  989. [1, 0, 0, 0, -c1],
  990. [0, 1, 0, 0, -c2],
  991. [0, 0, 1, 0, -c3],
  992. [0, 0, 0, 1, -c4]])
  993. """
  994. poly = kls._sympify(poly)
  995. if not isinstance(poly, Poly):
  996. raise ValueError("{} must be a Poly instance.".format(poly))
  997. if not poly.is_monic:
  998. raise ValueError("{} must be a monic polynomial.".format(poly))
  999. if not poly.is_univariate:
  1000. raise ValueError(
  1001. "{} must be a univariate polynomial.".format(poly))
  1002. size = poly.degree()
  1003. if not size >= 1:
  1004. raise ValueError(
  1005. "{} must have degree not less than 1.".format(poly))
  1006. coeffs = poly.all_coeffs()
  1007. def entry(i, j):
  1008. if j == size - 1:
  1009. return -coeffs[-1 - i]
  1010. elif i == j + 1:
  1011. return kls.one
  1012. return kls.zero
  1013. return kls._new(size, size, entry)
  1014. @classmethod
  1015. def wilkinson(kls, n, **kwargs):
  1016. """Returns two square Wilkinson Matrix of size 2*n + 1
  1017. $W_{2n + 1}^-, W_{2n + 1}^+ =$ Wilkinson(n)
  1018. Examples
  1019. ========
  1020. >>> from sympy import Matrix
  1021. >>> wminus, wplus = Matrix.wilkinson(3)
  1022. >>> wminus
  1023. Matrix([
  1024. [-3, 1, 0, 0, 0, 0, 0],
  1025. [ 1, -2, 1, 0, 0, 0, 0],
  1026. [ 0, 1, -1, 1, 0, 0, 0],
  1027. [ 0, 0, 1, 0, 1, 0, 0],
  1028. [ 0, 0, 0, 1, 1, 1, 0],
  1029. [ 0, 0, 0, 0, 1, 2, 1],
  1030. [ 0, 0, 0, 0, 0, 1, 3]])
  1031. >>> wplus
  1032. Matrix([
  1033. [3, 1, 0, 0, 0, 0, 0],
  1034. [1, 2, 1, 0, 0, 0, 0],
  1035. [0, 1, 1, 1, 0, 0, 0],
  1036. [0, 0, 1, 0, 1, 0, 0],
  1037. [0, 0, 0, 1, 1, 1, 0],
  1038. [0, 0, 0, 0, 1, 2, 1],
  1039. [0, 0, 0, 0, 0, 1, 3]])
  1040. References
  1041. ==========
  1042. .. [1] https://blogs.mathworks.com/cleve/2013/04/15/wilkinsons-matrices-2/
  1043. .. [2] J. H. Wilkinson, The Algebraic Eigenvalue Problem, Claredon Press, Oxford, 1965, 662 pp.
  1044. """
  1045. klass = kwargs.get('cls', kls)
  1046. n = as_int(n)
  1047. return klass._eval_wilkinson(n)
  1048. class MatrixProperties(MatrixRequired):
  1049. """Provides basic properties of a matrix."""
  1050. def _eval_atoms(self, *types):
  1051. result = set()
  1052. for i in self:
  1053. result.update(i.atoms(*types))
  1054. return result
  1055. def _eval_free_symbols(self):
  1056. return set().union(*(i.free_symbols for i in self if i))
  1057. def _eval_has(self, *patterns):
  1058. return any(a.has(*patterns) for a in self)
  1059. def _eval_is_anti_symmetric(self, simpfunc):
  1060. if not all(simpfunc(self[i, j] + self[j, i]).is_zero for i in range(self.rows) for j in range(self.cols)):
  1061. return False
  1062. return True
  1063. def _eval_is_diagonal(self):
  1064. for i in range(self.rows):
  1065. for j in range(self.cols):
  1066. if i != j and self[i, j]:
  1067. return False
  1068. return True
  1069. # _eval_is_hermitian is called by some general SymPy
  1070. # routines and has a different *args signature. Make
  1071. # sure the names don't clash by adding `_matrix_` in name.
  1072. def _eval_is_matrix_hermitian(self, simpfunc):
  1073. mat = self._new(self.rows, self.cols, lambda i, j: simpfunc(self[i, j] - self[j, i].conjugate()))
  1074. return mat.is_zero_matrix
  1075. def _eval_is_Identity(self) -> FuzzyBool:
  1076. def dirac(i, j):
  1077. if i == j:
  1078. return 1
  1079. return 0
  1080. return all(self[i, j] == dirac(i, j)
  1081. for i in range(self.rows)
  1082. for j in range(self.cols))
  1083. def _eval_is_lower_hessenberg(self):
  1084. return all(self[i, j].is_zero
  1085. for i in range(self.rows)
  1086. for j in range(i + 2, self.cols))
  1087. def _eval_is_lower(self):
  1088. return all(self[i, j].is_zero
  1089. for i in range(self.rows)
  1090. for j in range(i + 1, self.cols))
  1091. def _eval_is_symbolic(self):
  1092. return self.has(Symbol)
  1093. def _eval_is_symmetric(self, simpfunc):
  1094. mat = self._new(self.rows, self.cols, lambda i, j: simpfunc(self[i, j] - self[j, i]))
  1095. return mat.is_zero_matrix
  1096. def _eval_is_zero_matrix(self):
  1097. if any(i.is_zero == False for i in self):
  1098. return False
  1099. if any(i.is_zero is None for i in self):
  1100. return None
  1101. return True
  1102. def _eval_is_upper_hessenberg(self):
  1103. return all(self[i, j].is_zero
  1104. for i in range(2, self.rows)
  1105. for j in range(min(self.cols, (i - 1))))
  1106. def _eval_values(self):
  1107. return [i for i in self if not i.is_zero]
  1108. def _has_positive_diagonals(self):
  1109. diagonal_entries = (self[i, i] for i in range(self.rows))
  1110. return fuzzy_and(x.is_positive for x in diagonal_entries)
  1111. def _has_nonnegative_diagonals(self):
  1112. diagonal_entries = (self[i, i] for i in range(self.rows))
  1113. return fuzzy_and(x.is_nonnegative for x in diagonal_entries)
  1114. def atoms(self, *types):
  1115. """Returns the atoms that form the current object.
  1116. Examples
  1117. ========
  1118. >>> from sympy.abc import x, y
  1119. >>> from sympy import Matrix
  1120. >>> Matrix([[x]])
  1121. Matrix([[x]])
  1122. >>> _.atoms()
  1123. {x}
  1124. >>> Matrix([[x, y], [y, x]])
  1125. Matrix([
  1126. [x, y],
  1127. [y, x]])
  1128. >>> _.atoms()
  1129. {x, y}
  1130. """
  1131. types = tuple(t if isinstance(t, type) else type(t) for t in types)
  1132. if not types:
  1133. types = (Atom,)
  1134. return self._eval_atoms(*types)
  1135. @property
  1136. def free_symbols(self):
  1137. """Returns the free symbols within the matrix.
  1138. Examples
  1139. ========
  1140. >>> from sympy.abc import x
  1141. >>> from sympy import Matrix
  1142. >>> Matrix([[x], [1]]).free_symbols
  1143. {x}
  1144. """
  1145. return self._eval_free_symbols()
  1146. def has(self, *patterns):
  1147. """Test whether any subexpression matches any of the patterns.
  1148. Examples
  1149. ========
  1150. >>> from sympy import Matrix, SparseMatrix, Float
  1151. >>> from sympy.abc import x, y
  1152. >>> A = Matrix(((1, x), (0.2, 3)))
  1153. >>> B = SparseMatrix(((1, x), (0.2, 3)))
  1154. >>> A.has(x)
  1155. True
  1156. >>> A.has(y)
  1157. False
  1158. >>> A.has(Float)
  1159. True
  1160. >>> B.has(x)
  1161. True
  1162. >>> B.has(y)
  1163. False
  1164. >>> B.has(Float)
  1165. True
  1166. """
  1167. return self._eval_has(*patterns)
  1168. def is_anti_symmetric(self, simplify=True):
  1169. """Check if matrix M is an antisymmetric matrix,
  1170. that is, M is a square matrix with all M[i, j] == -M[j, i].
  1171. When ``simplify=True`` (default), the sum M[i, j] + M[j, i] is
  1172. simplified before testing to see if it is zero. By default,
  1173. the SymPy simplify function is used. To use a custom function
  1174. set simplify to a function that accepts a single argument which
  1175. returns a simplified expression. To skip simplification, set
  1176. simplify to False but note that although this will be faster,
  1177. it may induce false negatives.
  1178. Examples
  1179. ========
  1180. >>> from sympy import Matrix, symbols
  1181. >>> m = Matrix(2, 2, [0, 1, -1, 0])
  1182. >>> m
  1183. Matrix([
  1184. [ 0, 1],
  1185. [-1, 0]])
  1186. >>> m.is_anti_symmetric()
  1187. True
  1188. >>> x, y = symbols('x y')
  1189. >>> m = Matrix(2, 3, [0, 0, x, -y, 0, 0])
  1190. >>> m
  1191. Matrix([
  1192. [ 0, 0, x],
  1193. [-y, 0, 0]])
  1194. >>> m.is_anti_symmetric()
  1195. False
  1196. >>> from sympy.abc import x, y
  1197. >>> m = Matrix(3, 3, [0, x**2 + 2*x + 1, y,
  1198. ... -(x + 1)**2, 0, x*y,
  1199. ... -y, -x*y, 0])
  1200. Simplification of matrix elements is done by default so even
  1201. though two elements which should be equal and opposite wouldn't
  1202. pass an equality test, the matrix is still reported as
  1203. anti-symmetric:
  1204. >>> m[0, 1] == -m[1, 0]
  1205. False
  1206. >>> m.is_anti_symmetric()
  1207. True
  1208. If 'simplify=False' is used for the case when a Matrix is already
  1209. simplified, this will speed things up. Here, we see that without
  1210. simplification the matrix does not appear anti-symmetric:
  1211. >>> m.is_anti_symmetric(simplify=False)
  1212. False
  1213. But if the matrix were already expanded, then it would appear
  1214. anti-symmetric and simplification in the is_anti_symmetric routine
  1215. is not needed:
  1216. >>> m = m.expand()
  1217. >>> m.is_anti_symmetric(simplify=False)
  1218. True
  1219. """
  1220. # accept custom simplification
  1221. simpfunc = simplify
  1222. if not isfunction(simplify):
  1223. simpfunc = _simplify if simplify else lambda x: x
  1224. if not self.is_square:
  1225. return False
  1226. return self._eval_is_anti_symmetric(simpfunc)
  1227. def is_diagonal(self):
  1228. """Check if matrix is diagonal,
  1229. that is matrix in which the entries outside the main diagonal are all zero.
  1230. Examples
  1231. ========
  1232. >>> from sympy import Matrix, diag
  1233. >>> m = Matrix(2, 2, [1, 0, 0, 2])
  1234. >>> m
  1235. Matrix([
  1236. [1, 0],
  1237. [0, 2]])
  1238. >>> m.is_diagonal()
  1239. True
  1240. >>> m = Matrix(2, 2, [1, 1, 0, 2])
  1241. >>> m
  1242. Matrix([
  1243. [1, 1],
  1244. [0, 2]])
  1245. >>> m.is_diagonal()
  1246. False
  1247. >>> m = diag(1, 2, 3)
  1248. >>> m
  1249. Matrix([
  1250. [1, 0, 0],
  1251. [0, 2, 0],
  1252. [0, 0, 3]])
  1253. >>> m.is_diagonal()
  1254. True
  1255. See Also
  1256. ========
  1257. is_lower
  1258. is_upper
  1259. sympy.matrices.matrices.MatrixEigen.is_diagonalizable
  1260. diagonalize
  1261. """
  1262. return self._eval_is_diagonal()
  1263. @property
  1264. def is_weakly_diagonally_dominant(self):
  1265. r"""Tests if the matrix is row weakly diagonally dominant.
  1266. Explanation
  1267. ===========
  1268. A $n, n$ matrix $A$ is row weakly diagonally dominant if
  1269. .. math::
  1270. \left|A_{i, i}\right| \ge \sum_{j = 0, j \neq i}^{n-1}
  1271. \left|A_{i, j}\right| \quad {\text{for all }}
  1272. i \in \{ 0, ..., n-1 \}
  1273. Examples
  1274. ========
  1275. >>> from sympy import Matrix
  1276. >>> A = Matrix([[3, -2, 1], [1, -3, 2], [-1, 2, 4]])
  1277. >>> A.is_weakly_diagonally_dominant
  1278. True
  1279. >>> A = Matrix([[-2, 2, 1], [1, 3, 2], [1, -2, 0]])
  1280. >>> A.is_weakly_diagonally_dominant
  1281. False
  1282. >>> A = Matrix([[-4, 2, 1], [1, 6, 2], [1, -2, 5]])
  1283. >>> A.is_weakly_diagonally_dominant
  1284. True
  1285. Notes
  1286. =====
  1287. If you want to test whether a matrix is column diagonally
  1288. dominant, you can apply the test after transposing the matrix.
  1289. """
  1290. if not self.is_square:
  1291. return False
  1292. rows, cols = self.shape
  1293. def test_row(i):
  1294. summation = self.zero
  1295. for j in range(cols):
  1296. if i != j:
  1297. summation += Abs(self[i, j])
  1298. return (Abs(self[i, i]) - summation).is_nonnegative
  1299. return fuzzy_and(test_row(i) for i in range(rows))
  1300. @property
  1301. def is_strongly_diagonally_dominant(self):
  1302. r"""Tests if the matrix is row strongly diagonally dominant.
  1303. Explanation
  1304. ===========
  1305. A $n, n$ matrix $A$ is row strongly diagonally dominant if
  1306. .. math::
  1307. \left|A_{i, i}\right| > \sum_{j = 0, j \neq i}^{n-1}
  1308. \left|A_{i, j}\right| \quad {\text{for all }}
  1309. i \in \{ 0, ..., n-1 \}
  1310. Examples
  1311. ========
  1312. >>> from sympy import Matrix
  1313. >>> A = Matrix([[3, -2, 1], [1, -3, 2], [-1, 2, 4]])
  1314. >>> A.is_strongly_diagonally_dominant
  1315. False
  1316. >>> A = Matrix([[-2, 2, 1], [1, 3, 2], [1, -2, 0]])
  1317. >>> A.is_strongly_diagonally_dominant
  1318. False
  1319. >>> A = Matrix([[-4, 2, 1], [1, 6, 2], [1, -2, 5]])
  1320. >>> A.is_strongly_diagonally_dominant
  1321. True
  1322. Notes
  1323. =====
  1324. If you want to test whether a matrix is column diagonally
  1325. dominant, you can apply the test after transposing the matrix.
  1326. """
  1327. if not self.is_square:
  1328. return False
  1329. rows, cols = self.shape
  1330. def test_row(i):
  1331. summation = self.zero
  1332. for j in range(cols):
  1333. if i != j:
  1334. summation += Abs(self[i, j])
  1335. return (Abs(self[i, i]) - summation).is_positive
  1336. return fuzzy_and(test_row(i) for i in range(rows))
  1337. @property
  1338. def is_hermitian(self):
  1339. """Checks if the matrix is Hermitian.
  1340. In a Hermitian matrix element i,j is the complex conjugate of
  1341. element j,i.
  1342. Examples
  1343. ========
  1344. >>> from sympy import Matrix
  1345. >>> from sympy import I
  1346. >>> from sympy.abc import x
  1347. >>> a = Matrix([[1, I], [-I, 1]])
  1348. >>> a
  1349. Matrix([
  1350. [ 1, I],
  1351. [-I, 1]])
  1352. >>> a.is_hermitian
  1353. True
  1354. >>> a[0, 0] = 2*I
  1355. >>> a.is_hermitian
  1356. False
  1357. >>> a[0, 0] = x
  1358. >>> a.is_hermitian
  1359. >>> a[0, 1] = a[1, 0]*I
  1360. >>> a.is_hermitian
  1361. False
  1362. """
  1363. if not self.is_square:
  1364. return False
  1365. return self._eval_is_matrix_hermitian(_simplify)
  1366. @property
  1367. def is_Identity(self) -> FuzzyBool:
  1368. if not self.is_square:
  1369. return False
  1370. return self._eval_is_Identity()
  1371. @property
  1372. def is_lower_hessenberg(self):
  1373. r"""Checks if the matrix is in the lower-Hessenberg form.
  1374. The lower hessenberg matrix has zero entries
  1375. above the first superdiagonal.
  1376. Examples
  1377. ========
  1378. >>> from sympy import Matrix
  1379. >>> a = Matrix([[1, 2, 0, 0], [5, 2, 3, 0], [3, 4, 3, 7], [5, 6, 1, 1]])
  1380. >>> a
  1381. Matrix([
  1382. [1, 2, 0, 0],
  1383. [5, 2, 3, 0],
  1384. [3, 4, 3, 7],
  1385. [5, 6, 1, 1]])
  1386. >>> a.is_lower_hessenberg
  1387. True
  1388. See Also
  1389. ========
  1390. is_upper_hessenberg
  1391. is_lower
  1392. """
  1393. return self._eval_is_lower_hessenberg()
  1394. @property
  1395. def is_lower(self):
  1396. """Check if matrix is a lower triangular matrix. True can be returned
  1397. even if the matrix is not square.
  1398. Examples
  1399. ========
  1400. >>> from sympy import Matrix
  1401. >>> m = Matrix(2, 2, [1, 0, 0, 1])
  1402. >>> m
  1403. Matrix([
  1404. [1, 0],
  1405. [0, 1]])
  1406. >>> m.is_lower
  1407. True
  1408. >>> m = Matrix(4, 3, [0, 0, 0, 2, 0, 0, 1, 4, 0, 6, 6, 5])
  1409. >>> m
  1410. Matrix([
  1411. [0, 0, 0],
  1412. [2, 0, 0],
  1413. [1, 4, 0],
  1414. [6, 6, 5]])
  1415. >>> m.is_lower
  1416. True
  1417. >>> from sympy.abc import x, y
  1418. >>> m = Matrix(2, 2, [x**2 + y, y**2 + x, 0, x + y])
  1419. >>> m
  1420. Matrix([
  1421. [x**2 + y, x + y**2],
  1422. [ 0, x + y]])
  1423. >>> m.is_lower
  1424. False
  1425. See Also
  1426. ========
  1427. is_upper
  1428. is_diagonal
  1429. is_lower_hessenberg
  1430. """
  1431. return self._eval_is_lower()
  1432. @property
  1433. def is_square(self):
  1434. """Checks if a matrix is square.
  1435. A matrix is square if the number of rows equals the number of columns.
  1436. The empty matrix is square by definition, since the number of rows and
  1437. the number of columns are both zero.
  1438. Examples
  1439. ========
  1440. >>> from sympy import Matrix
  1441. >>> a = Matrix([[1, 2, 3], [4, 5, 6]])
  1442. >>> b = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  1443. >>> c = Matrix([])
  1444. >>> a.is_square
  1445. False
  1446. >>> b.is_square
  1447. True
  1448. >>> c.is_square
  1449. True
  1450. """
  1451. return self.rows == self.cols
  1452. def is_symbolic(self):
  1453. """Checks if any elements contain Symbols.
  1454. Examples
  1455. ========
  1456. >>> from sympy import Matrix
  1457. >>> from sympy.abc import x, y
  1458. >>> M = Matrix([[x, y], [1, 0]])
  1459. >>> M.is_symbolic()
  1460. True
  1461. """
  1462. return self._eval_is_symbolic()
  1463. def is_symmetric(self, simplify=True):
  1464. """Check if matrix is symmetric matrix,
  1465. that is square matrix and is equal to its transpose.
  1466. By default, simplifications occur before testing symmetry.
  1467. They can be skipped using 'simplify=False'; while speeding things a bit,
  1468. this may however induce false negatives.
  1469. Examples
  1470. ========
  1471. >>> from sympy import Matrix
  1472. >>> m = Matrix(2, 2, [0, 1, 1, 2])
  1473. >>> m
  1474. Matrix([
  1475. [0, 1],
  1476. [1, 2]])
  1477. >>> m.is_symmetric()
  1478. True
  1479. >>> m = Matrix(2, 2, [0, 1, 2, 0])
  1480. >>> m
  1481. Matrix([
  1482. [0, 1],
  1483. [2, 0]])
  1484. >>> m.is_symmetric()
  1485. False
  1486. >>> m = Matrix(2, 3, [0, 0, 0, 0, 0, 0])
  1487. >>> m
  1488. Matrix([
  1489. [0, 0, 0],
  1490. [0, 0, 0]])
  1491. >>> m.is_symmetric()
  1492. False
  1493. >>> from sympy.abc import x, y
  1494. >>> m = Matrix(3, 3, [1, x**2 + 2*x + 1, y, (x + 1)**2, 2, 0, y, 0, 3])
  1495. >>> m
  1496. Matrix([
  1497. [ 1, x**2 + 2*x + 1, y],
  1498. [(x + 1)**2, 2, 0],
  1499. [ y, 0, 3]])
  1500. >>> m.is_symmetric()
  1501. True
  1502. If the matrix is already simplified, you may speed-up is_symmetric()
  1503. test by using 'simplify=False'.
  1504. >>> bool(m.is_symmetric(simplify=False))
  1505. False
  1506. >>> m1 = m.expand()
  1507. >>> m1.is_symmetric(simplify=False)
  1508. True
  1509. """
  1510. simpfunc = simplify
  1511. if not isfunction(simplify):
  1512. simpfunc = _simplify if simplify else lambda x: x
  1513. if not self.is_square:
  1514. return False
  1515. return self._eval_is_symmetric(simpfunc)
  1516. @property
  1517. def is_upper_hessenberg(self):
  1518. """Checks if the matrix is the upper-Hessenberg form.
  1519. The upper hessenberg matrix has zero entries
  1520. below the first subdiagonal.
  1521. Examples
  1522. ========
  1523. >>> from sympy import Matrix
  1524. >>> a = Matrix([[1, 4, 2, 3], [3, 4, 1, 7], [0, 2, 3, 4], [0, 0, 1, 3]])
  1525. >>> a
  1526. Matrix([
  1527. [1, 4, 2, 3],
  1528. [3, 4, 1, 7],
  1529. [0, 2, 3, 4],
  1530. [0, 0, 1, 3]])
  1531. >>> a.is_upper_hessenberg
  1532. True
  1533. See Also
  1534. ========
  1535. is_lower_hessenberg
  1536. is_upper
  1537. """
  1538. return self._eval_is_upper_hessenberg()
  1539. @property
  1540. def is_upper(self):
  1541. """Check if matrix is an upper triangular matrix. True can be returned
  1542. even if the matrix is not square.
  1543. Examples
  1544. ========
  1545. >>> from sympy import Matrix
  1546. >>> m = Matrix(2, 2, [1, 0, 0, 1])
  1547. >>> m
  1548. Matrix([
  1549. [1, 0],
  1550. [0, 1]])
  1551. >>> m.is_upper
  1552. True
  1553. >>> m = Matrix(4, 3, [5, 1, 9, 0, 4, 6, 0, 0, 5, 0, 0, 0])
  1554. >>> m
  1555. Matrix([
  1556. [5, 1, 9],
  1557. [0, 4, 6],
  1558. [0, 0, 5],
  1559. [0, 0, 0]])
  1560. >>> m.is_upper
  1561. True
  1562. >>> m = Matrix(2, 3, [4, 2, 5, 6, 1, 1])
  1563. >>> m
  1564. Matrix([
  1565. [4, 2, 5],
  1566. [6, 1, 1]])
  1567. >>> m.is_upper
  1568. False
  1569. See Also
  1570. ========
  1571. is_lower
  1572. is_diagonal
  1573. is_upper_hessenberg
  1574. """
  1575. return all(self[i, j].is_zero
  1576. for i in range(1, self.rows)
  1577. for j in range(min(i, self.cols)))
  1578. @property
  1579. def is_zero_matrix(self):
  1580. """Checks if a matrix is a zero matrix.
  1581. A matrix is zero if every element is zero. A matrix need not be square
  1582. to be considered zero. The empty matrix is zero by the principle of
  1583. vacuous truth. For a matrix that may or may not be zero (e.g.
  1584. contains a symbol), this will be None
  1585. Examples
  1586. ========
  1587. >>> from sympy import Matrix, zeros
  1588. >>> from sympy.abc import x
  1589. >>> a = Matrix([[0, 0], [0, 0]])
  1590. >>> b = zeros(3, 4)
  1591. >>> c = Matrix([[0, 1], [0, 0]])
  1592. >>> d = Matrix([])
  1593. >>> e = Matrix([[x, 0], [0, 0]])
  1594. >>> a.is_zero_matrix
  1595. True
  1596. >>> b.is_zero_matrix
  1597. True
  1598. >>> c.is_zero_matrix
  1599. False
  1600. >>> d.is_zero_matrix
  1601. True
  1602. >>> e.is_zero_matrix
  1603. """
  1604. return self._eval_is_zero_matrix()
  1605. def values(self):
  1606. """Return non-zero values of self."""
  1607. return self._eval_values()
  1608. class MatrixOperations(MatrixRequired):
  1609. """Provides basic matrix shape and elementwise
  1610. operations. Should not be instantiated directly."""
  1611. def _eval_adjoint(self):
  1612. return self.transpose().conjugate()
  1613. def _eval_applyfunc(self, f):
  1614. out = self._new(self.rows, self.cols, [f(x) for x in self])
  1615. return out
  1616. def _eval_as_real_imag(self): # type: ignore
  1617. from sympy.functions.elementary.complexes import re, im
  1618. return (self.applyfunc(re), self.applyfunc(im))
  1619. def _eval_conjugate(self):
  1620. return self.applyfunc(lambda x: x.conjugate())
  1621. def _eval_permute_cols(self, perm):
  1622. # apply the permutation to a list
  1623. mapping = list(perm)
  1624. def entry(i, j):
  1625. return self[i, mapping[j]]
  1626. return self._new(self.rows, self.cols, entry)
  1627. def _eval_permute_rows(self, perm):
  1628. # apply the permutation to a list
  1629. mapping = list(perm)
  1630. def entry(i, j):
  1631. return self[mapping[i], j]
  1632. return self._new(self.rows, self.cols, entry)
  1633. def _eval_trace(self):
  1634. return sum(self[i, i] for i in range(self.rows))
  1635. def _eval_transpose(self):
  1636. return self._new(self.cols, self.rows, lambda i, j: self[j, i])
  1637. def adjoint(self):
  1638. """Conjugate transpose or Hermitian conjugation."""
  1639. return self._eval_adjoint()
  1640. def applyfunc(self, f):
  1641. """Apply a function to each element of the matrix.
  1642. Examples
  1643. ========
  1644. >>> from sympy import Matrix
  1645. >>> m = Matrix(2, 2, lambda i, j: i*2+j)
  1646. >>> m
  1647. Matrix([
  1648. [0, 1],
  1649. [2, 3]])
  1650. >>> m.applyfunc(lambda i: 2*i)
  1651. Matrix([
  1652. [0, 2],
  1653. [4, 6]])
  1654. """
  1655. if not callable(f):
  1656. raise TypeError("`f` must be callable.")
  1657. return self._eval_applyfunc(f)
  1658. def as_real_imag(self, deep=True, **hints):
  1659. """Returns a tuple containing the (real, imaginary) part of matrix."""
  1660. # XXX: Ignoring deep and hints...
  1661. return self._eval_as_real_imag()
  1662. def conjugate(self):
  1663. """Return the by-element conjugation.
  1664. Examples
  1665. ========
  1666. >>> from sympy import SparseMatrix, I
  1667. >>> a = SparseMatrix(((1, 2 + I), (3, 4), (I, -I)))
  1668. >>> a
  1669. Matrix([
  1670. [1, 2 + I],
  1671. [3, 4],
  1672. [I, -I]])
  1673. >>> a.C
  1674. Matrix([
  1675. [ 1, 2 - I],
  1676. [ 3, 4],
  1677. [-I, I]])
  1678. See Also
  1679. ========
  1680. transpose: Matrix transposition
  1681. H: Hermite conjugation
  1682. sympy.matrices.matrices.MatrixBase.D: Dirac conjugation
  1683. """
  1684. return self._eval_conjugate()
  1685. def doit(self, **kwargs):
  1686. return self.applyfunc(lambda x: x.doit())
  1687. def evalf(self, n=15, subs=None, maxn=100, chop=False, strict=False, quad=None, verbose=False):
  1688. """Apply evalf() to each element of self."""
  1689. options = {'subs':subs, 'maxn':maxn, 'chop':chop, 'strict':strict,
  1690. 'quad':quad, 'verbose':verbose}
  1691. return self.applyfunc(lambda i: i.evalf(n, **options))
  1692. def expand(self, deep=True, modulus=None, power_base=True, power_exp=True,
  1693. mul=True, log=True, multinomial=True, basic=True, **hints):
  1694. """Apply core.function.expand to each entry of the matrix.
  1695. Examples
  1696. ========
  1697. >>> from sympy.abc import x
  1698. >>> from sympy import Matrix
  1699. >>> Matrix(1, 1, [x*(x+1)])
  1700. Matrix([[x*(x + 1)]])
  1701. >>> _.expand()
  1702. Matrix([[x**2 + x]])
  1703. """
  1704. return self.applyfunc(lambda x: x.expand(
  1705. deep, modulus, power_base, power_exp, mul, log, multinomial, basic,
  1706. **hints))
  1707. @property
  1708. def H(self):
  1709. """Return Hermite conjugate.
  1710. Examples
  1711. ========
  1712. >>> from sympy import Matrix, I
  1713. >>> m = Matrix((0, 1 + I, 2, 3))
  1714. >>> m
  1715. Matrix([
  1716. [ 0],
  1717. [1 + I],
  1718. [ 2],
  1719. [ 3]])
  1720. >>> m.H
  1721. Matrix([[0, 1 - I, 2, 3]])
  1722. See Also
  1723. ========
  1724. conjugate: By-element conjugation
  1725. sympy.matrices.matrices.MatrixBase.D: Dirac conjugation
  1726. """
  1727. return self.T.C
  1728. def permute(self, perm, orientation='rows', direction='forward'):
  1729. r"""Permute the rows or columns of a matrix by the given list of
  1730. swaps.
  1731. Parameters
  1732. ==========
  1733. perm : Permutation, list, or list of lists
  1734. A representation for the permutation.
  1735. If it is ``Permutation``, it is used directly with some
  1736. resizing with respect to the matrix size.
  1737. If it is specified as list of lists,
  1738. (e.g., ``[[0, 1], [0, 2]]``), then the permutation is formed
  1739. from applying the product of cycles. The direction how the
  1740. cyclic product is applied is described in below.
  1741. If it is specified as a list, the list should represent
  1742. an array form of a permutation. (e.g., ``[1, 2, 0]``) which
  1743. would would form the swapping function
  1744. `0 \mapsto 1, 1 \mapsto 2, 2\mapsto 0`.
  1745. orientation : 'rows', 'cols'
  1746. A flag to control whether to permute the rows or the columns
  1747. direction : 'forward', 'backward'
  1748. A flag to control whether to apply the permutations from
  1749. the start of the list first, or from the back of the list
  1750. first.
  1751. For example, if the permutation specification is
  1752. ``[[0, 1], [0, 2]]``,
  1753. If the flag is set to ``'forward'``, the cycle would be
  1754. formed as `0 \mapsto 2, 2 \mapsto 1, 1 \mapsto 0`.
  1755. If the flag is set to ``'backward'``, the cycle would be
  1756. formed as `0 \mapsto 1, 1 \mapsto 2, 2 \mapsto 0`.
  1757. If the argument ``perm`` is not in a form of list of lists,
  1758. this flag takes no effect.
  1759. Examples
  1760. ========
  1761. >>> from sympy import eye
  1762. >>> M = eye(3)
  1763. >>> M.permute([[0, 1], [0, 2]], orientation='rows', direction='forward')
  1764. Matrix([
  1765. [0, 0, 1],
  1766. [1, 0, 0],
  1767. [0, 1, 0]])
  1768. >>> from sympy import eye
  1769. >>> M = eye(3)
  1770. >>> M.permute([[0, 1], [0, 2]], orientation='rows', direction='backward')
  1771. Matrix([
  1772. [0, 1, 0],
  1773. [0, 0, 1],
  1774. [1, 0, 0]])
  1775. Notes
  1776. =====
  1777. If a bijective function
  1778. `\sigma : \mathbb{N}_0 \rightarrow \mathbb{N}_0` denotes the
  1779. permutation.
  1780. If the matrix `A` is the matrix to permute, represented as
  1781. a horizontal or a vertical stack of vectors:
  1782. .. math::
  1783. A =
  1784. \begin{bmatrix}
  1785. a_0 \\ a_1 \\ \vdots \\ a_{n-1}
  1786. \end{bmatrix} =
  1787. \begin{bmatrix}
  1788. \alpha_0 & \alpha_1 & \cdots & \alpha_{n-1}
  1789. \end{bmatrix}
  1790. If the matrix `B` is the result, the permutation of matrix rows
  1791. is defined as:
  1792. .. math::
  1793. B := \begin{bmatrix}
  1794. a_{\sigma(0)} \\ a_{\sigma(1)} \\ \vdots \\ a_{\sigma(n-1)}
  1795. \end{bmatrix}
  1796. And the permutation of matrix columns is defined as:
  1797. .. math::
  1798. B := \begin{bmatrix}
  1799. \alpha_{\sigma(0)} & \alpha_{\sigma(1)} &
  1800. \cdots & \alpha_{\sigma(n-1)}
  1801. \end{bmatrix}
  1802. """
  1803. from sympy.combinatorics import Permutation
  1804. # allow british variants and `columns`
  1805. if direction == 'forwards':
  1806. direction = 'forward'
  1807. if direction == 'backwards':
  1808. direction = 'backward'
  1809. if orientation == 'columns':
  1810. orientation = 'cols'
  1811. if direction not in ('forward', 'backward'):
  1812. raise TypeError("direction='{}' is an invalid kwarg. "
  1813. "Try 'forward' or 'backward'".format(direction))
  1814. if orientation not in ('rows', 'cols'):
  1815. raise TypeError("orientation='{}' is an invalid kwarg. "
  1816. "Try 'rows' or 'cols'".format(orientation))
  1817. if not isinstance(perm, (Permutation, Iterable)):
  1818. raise ValueError(
  1819. "{} must be a list, a list of lists, "
  1820. "or a SymPy permutation object.".format(perm))
  1821. # ensure all swaps are in range
  1822. max_index = self.rows if orientation == 'rows' else self.cols
  1823. if not all(0 <= t <= max_index for t in flatten(list(perm))):
  1824. raise IndexError("`swap` indices out of range.")
  1825. if perm and not isinstance(perm, Permutation) and \
  1826. isinstance(perm[0], Iterable):
  1827. if direction == 'forward':
  1828. perm = list(reversed(perm))
  1829. perm = Permutation(perm, size=max_index+1)
  1830. else:
  1831. perm = Permutation(perm, size=max_index+1)
  1832. if orientation == 'rows':
  1833. return self._eval_permute_rows(perm)
  1834. if orientation == 'cols':
  1835. return self._eval_permute_cols(perm)
  1836. def permute_cols(self, swaps, direction='forward'):
  1837. """Alias for
  1838. ``self.permute(swaps, orientation='cols', direction=direction)``
  1839. See Also
  1840. ========
  1841. permute
  1842. """
  1843. return self.permute(swaps, orientation='cols', direction=direction)
  1844. def permute_rows(self, swaps, direction='forward'):
  1845. """Alias for
  1846. ``self.permute(swaps, orientation='rows', direction=direction)``
  1847. See Also
  1848. ========
  1849. permute
  1850. """
  1851. return self.permute(swaps, orientation='rows', direction=direction)
  1852. def refine(self, assumptions=True):
  1853. """Apply refine to each element of the matrix.
  1854. Examples
  1855. ========
  1856. >>> from sympy import Symbol, Matrix, Abs, sqrt, Q
  1857. >>> x = Symbol('x')
  1858. >>> Matrix([[Abs(x)**2, sqrt(x**2)],[sqrt(x**2), Abs(x)**2]])
  1859. Matrix([
  1860. [ Abs(x)**2, sqrt(x**2)],
  1861. [sqrt(x**2), Abs(x)**2]])
  1862. >>> _.refine(Q.real(x))
  1863. Matrix([
  1864. [ x**2, Abs(x)],
  1865. [Abs(x), x**2]])
  1866. """
  1867. return self.applyfunc(lambda x: refine(x, assumptions))
  1868. def replace(self, F, G, map=False, simultaneous=True, exact=None):
  1869. """Replaces Function F in Matrix entries with Function G.
  1870. Examples
  1871. ========
  1872. >>> from sympy import symbols, Function, Matrix
  1873. >>> F, G = symbols('F, G', cls=Function)
  1874. >>> M = Matrix(2, 2, lambda i, j: F(i+j)) ; M
  1875. Matrix([
  1876. [F(0), F(1)],
  1877. [F(1), F(2)]])
  1878. >>> N = M.replace(F,G)
  1879. >>> N
  1880. Matrix([
  1881. [G(0), G(1)],
  1882. [G(1), G(2)]])
  1883. """
  1884. return self.applyfunc(
  1885. lambda x: x.replace(F, G, map=map, simultaneous=simultaneous, exact=exact))
  1886. def rot90(self, k=1):
  1887. """Rotates Matrix by 90 degrees
  1888. Parameters
  1889. ==========
  1890. k : int
  1891. Specifies how many times the matrix is rotated by 90 degrees
  1892. (clockwise when positive, counter-clockwise when negative).
  1893. Examples
  1894. ========
  1895. >>> from sympy import Matrix, symbols
  1896. >>> A = Matrix(2, 2, symbols('a:d'))
  1897. >>> A
  1898. Matrix([
  1899. [a, b],
  1900. [c, d]])
  1901. Rotating the matrix clockwise one time:
  1902. >>> A.rot90(1)
  1903. Matrix([
  1904. [c, a],
  1905. [d, b]])
  1906. Rotating the matrix anticlockwise two times:
  1907. >>> A.rot90(-2)
  1908. Matrix([
  1909. [d, c],
  1910. [b, a]])
  1911. """
  1912. mod = k%4
  1913. if mod == 0:
  1914. return self
  1915. if mod == 1:
  1916. return self[::-1, ::].T
  1917. if mod == 2:
  1918. return self[::-1, ::-1]
  1919. if mod == 3:
  1920. return self[::, ::-1].T
  1921. def simplify(self, **kwargs):
  1922. """Apply simplify to each element of the matrix.
  1923. Examples
  1924. ========
  1925. >>> from sympy.abc import x, y
  1926. >>> from sympy import SparseMatrix, sin, cos
  1927. >>> SparseMatrix(1, 1, [x*sin(y)**2 + x*cos(y)**2])
  1928. Matrix([[x*sin(y)**2 + x*cos(y)**2]])
  1929. >>> _.simplify()
  1930. Matrix([[x]])
  1931. """
  1932. return self.applyfunc(lambda x: x.simplify(**kwargs))
  1933. def subs(self, *args, **kwargs): # should mirror core.basic.subs
  1934. """Return a new matrix with subs applied to each entry.
  1935. Examples
  1936. ========
  1937. >>> from sympy.abc import x, y
  1938. >>> from sympy import SparseMatrix, Matrix
  1939. >>> SparseMatrix(1, 1, [x])
  1940. Matrix([[x]])
  1941. >>> _.subs(x, y)
  1942. Matrix([[y]])
  1943. >>> Matrix(_).subs(y, x)
  1944. Matrix([[x]])
  1945. """
  1946. if len(args) == 1 and not isinstance(args[0], (dict, set)) and iter(args[0]) and not is_sequence(args[0]):
  1947. args = (list(args[0]),)
  1948. return self.applyfunc(lambda x: x.subs(*args, **kwargs))
  1949. def trace(self):
  1950. """
  1951. Returns the trace of a square matrix i.e. the sum of the
  1952. diagonal elements.
  1953. Examples
  1954. ========
  1955. >>> from sympy import Matrix
  1956. >>> A = Matrix(2, 2, [1, 2, 3, 4])
  1957. >>> A.trace()
  1958. 5
  1959. """
  1960. if self.rows != self.cols:
  1961. raise NonSquareMatrixError()
  1962. return self._eval_trace()
  1963. def transpose(self):
  1964. """
  1965. Returns the transpose of the matrix.
  1966. Examples
  1967. ========
  1968. >>> from sympy import Matrix
  1969. >>> A = Matrix(2, 2, [1, 2, 3, 4])
  1970. >>> A.transpose()
  1971. Matrix([
  1972. [1, 3],
  1973. [2, 4]])
  1974. >>> from sympy import Matrix, I
  1975. >>> m=Matrix(((1, 2+I), (3, 4)))
  1976. >>> m
  1977. Matrix([
  1978. [1, 2 + I],
  1979. [3, 4]])
  1980. >>> m.transpose()
  1981. Matrix([
  1982. [ 1, 3],
  1983. [2 + I, 4]])
  1984. >>> m.T == m.transpose()
  1985. True
  1986. See Also
  1987. ========
  1988. conjugate: By-element conjugation
  1989. """
  1990. return self._eval_transpose()
  1991. @property
  1992. def T(self):
  1993. '''Matrix transposition'''
  1994. return self.transpose()
  1995. @property
  1996. def C(self):
  1997. '''By-element conjugation'''
  1998. return self.conjugate()
  1999. def n(self, *args, **kwargs):
  2000. """Apply evalf() to each element of self."""
  2001. return self.evalf(*args, **kwargs)
  2002. def xreplace(self, rule): # should mirror core.basic.xreplace
  2003. """Return a new matrix with xreplace applied to each entry.
  2004. Examples
  2005. ========
  2006. >>> from sympy.abc import x, y
  2007. >>> from sympy import SparseMatrix, Matrix
  2008. >>> SparseMatrix(1, 1, [x])
  2009. Matrix([[x]])
  2010. >>> _.xreplace({x: y})
  2011. Matrix([[y]])
  2012. >>> Matrix(_).xreplace({y: x})
  2013. Matrix([[x]])
  2014. """
  2015. return self.applyfunc(lambda x: x.xreplace(rule))
  2016. def _eval_simplify(self, **kwargs):
  2017. # XXX: We can't use self.simplify here as mutable subclasses will
  2018. # override simplify and have it return None
  2019. return MatrixOperations.simplify(self, **kwargs)
  2020. def _eval_trigsimp(self, **opts):
  2021. from sympy.simplify import trigsimp
  2022. return self.applyfunc(lambda x: trigsimp(x, **opts))
  2023. def upper_triangular(self, k=0):
  2024. """returns the elements on and above the kth diagonal of a matrix.
  2025. If k is not specified then simply returns upper-triangular portion
  2026. of a matrix
  2027. Examples
  2028. ========
  2029. >>> from sympy import ones
  2030. >>> A = ones(4)
  2031. >>> A.upper_triangular()
  2032. Matrix([
  2033. [1, 1, 1, 1],
  2034. [0, 1, 1, 1],
  2035. [0, 0, 1, 1],
  2036. [0, 0, 0, 1]])
  2037. >>> A.upper_triangular(2)
  2038. Matrix([
  2039. [0, 0, 1, 1],
  2040. [0, 0, 0, 1],
  2041. [0, 0, 0, 0],
  2042. [0, 0, 0, 0]])
  2043. >>> A.upper_triangular(-1)
  2044. Matrix([
  2045. [1, 1, 1, 1],
  2046. [1, 1, 1, 1],
  2047. [0, 1, 1, 1],
  2048. [0, 0, 1, 1]])
  2049. """
  2050. def entry(i, j):
  2051. return self[i, j] if i + k <= j else self.zero
  2052. return self._new(self.rows, self.cols, entry)
  2053. def lower_triangular(self, k=0):
  2054. """returns the elements on and below the kth diagonal of a matrix.
  2055. If k is not specified then simply returns lower-triangular portion
  2056. of a matrix
  2057. Examples
  2058. ========
  2059. >>> from sympy import ones
  2060. >>> A = ones(4)
  2061. >>> A.lower_triangular()
  2062. Matrix([
  2063. [1, 0, 0, 0],
  2064. [1, 1, 0, 0],
  2065. [1, 1, 1, 0],
  2066. [1, 1, 1, 1]])
  2067. >>> A.lower_triangular(-2)
  2068. Matrix([
  2069. [0, 0, 0, 0],
  2070. [0, 0, 0, 0],
  2071. [1, 0, 0, 0],
  2072. [1, 1, 0, 0]])
  2073. >>> A.lower_triangular(1)
  2074. Matrix([
  2075. [1, 1, 0, 0],
  2076. [1, 1, 1, 0],
  2077. [1, 1, 1, 1],
  2078. [1, 1, 1, 1]])
  2079. """
  2080. def entry(i, j):
  2081. return self[i, j] if i + k >= j else self.zero
  2082. return self._new(self.rows, self.cols, entry)
  2083. class MatrixArithmetic(MatrixRequired):
  2084. """Provides basic matrix arithmetic operations.
  2085. Should not be instantiated directly."""
  2086. _op_priority = 10.01
  2087. def _eval_Abs(self):
  2088. return self._new(self.rows, self.cols, lambda i, j: Abs(self[i, j]))
  2089. def _eval_add(self, other):
  2090. return self._new(self.rows, self.cols,
  2091. lambda i, j: self[i, j] + other[i, j])
  2092. def _eval_matrix_mul(self, other):
  2093. def entry(i, j):
  2094. vec = [self[i,k]*other[k,j] for k in range(self.cols)]
  2095. try:
  2096. return Add(*vec)
  2097. except (TypeError, SympifyError):
  2098. # Some matrices don't work with `sum` or `Add`
  2099. # They don't work with `sum` because `sum` tries to add `0`
  2100. # Fall back to a safe way to multiply if the `Add` fails.
  2101. return reduce(lambda a, b: a + b, vec)
  2102. return self._new(self.rows, other.cols, entry)
  2103. def _eval_matrix_mul_elementwise(self, other):
  2104. return self._new(self.rows, self.cols, lambda i, j: self[i,j]*other[i,j])
  2105. def _eval_matrix_rmul(self, other):
  2106. def entry(i, j):
  2107. return sum(other[i,k]*self[k,j] for k in range(other.cols))
  2108. return self._new(other.rows, self.cols, entry)
  2109. def _eval_pow_by_recursion(self, num):
  2110. if num == 1:
  2111. return self
  2112. if num % 2 == 1:
  2113. a, b = self, self._eval_pow_by_recursion(num - 1)
  2114. else:
  2115. a = b = self._eval_pow_by_recursion(num // 2)
  2116. return a.multiply(b)
  2117. def _eval_pow_by_cayley(self, exp):
  2118. from sympy.discrete.recurrences import linrec_coeffs
  2119. row = self.shape[0]
  2120. p = self.charpoly()
  2121. coeffs = (-p).all_coeffs()[1:]
  2122. coeffs = linrec_coeffs(coeffs, exp)
  2123. new_mat = self.eye(row)
  2124. ans = self.zeros(row)
  2125. for i in range(row):
  2126. ans += coeffs[i]*new_mat
  2127. new_mat *= self
  2128. return ans
  2129. def _eval_pow_by_recursion_dotprodsimp(self, num, prevsimp=None):
  2130. if prevsimp is None:
  2131. prevsimp = [True]*len(self)
  2132. if num == 1:
  2133. return self
  2134. if num % 2 == 1:
  2135. a, b = self, self._eval_pow_by_recursion_dotprodsimp(num - 1,
  2136. prevsimp=prevsimp)
  2137. else:
  2138. a = b = self._eval_pow_by_recursion_dotprodsimp(num // 2,
  2139. prevsimp=prevsimp)
  2140. m = a.multiply(b, dotprodsimp=False)
  2141. lenm = len(m)
  2142. elems = [None]*lenm
  2143. for i in range(lenm):
  2144. if prevsimp[i]:
  2145. elems[i], prevsimp[i] = _dotprodsimp(m[i], withsimp=True)
  2146. else:
  2147. elems[i] = m[i]
  2148. return m._new(m.rows, m.cols, elems)
  2149. def _eval_scalar_mul(self, other):
  2150. return self._new(self.rows, self.cols, lambda i, j: self[i,j]*other)
  2151. def _eval_scalar_rmul(self, other):
  2152. return self._new(self.rows, self.cols, lambda i, j: other*self[i,j])
  2153. def _eval_Mod(self, other):
  2154. return self._new(self.rows, self.cols, lambda i, j: Mod(self[i, j], other))
  2155. # Python arithmetic functions
  2156. def __abs__(self):
  2157. """Returns a new matrix with entry-wise absolute values."""
  2158. return self._eval_Abs()
  2159. @call_highest_priority('__radd__')
  2160. def __add__(self, other):
  2161. """Return self + other, raising ShapeError if shapes do not match."""
  2162. if isinstance(other, NDimArray): # Matrix and array addition is currently not implemented
  2163. return NotImplemented
  2164. other = _matrixify(other)
  2165. # matrix-like objects can have shapes. This is
  2166. # our first sanity check.
  2167. if hasattr(other, 'shape'):
  2168. if self.shape != other.shape:
  2169. raise ShapeError("Matrix size mismatch: %s + %s" % (
  2170. self.shape, other.shape))
  2171. # honest SymPy matrices defer to their class's routine
  2172. if getattr(other, 'is_Matrix', False):
  2173. # call the highest-priority class's _eval_add
  2174. a, b = self, other
  2175. if a.__class__ != classof(a, b):
  2176. b, a = a, b
  2177. return a._eval_add(b)
  2178. # Matrix-like objects can be passed to CommonMatrix routines directly.
  2179. if getattr(other, 'is_MatrixLike', False):
  2180. return MatrixArithmetic._eval_add(self, other)
  2181. raise TypeError('cannot add %s and %s' % (type(self), type(other)))
  2182. @call_highest_priority('__rtruediv__')
  2183. def __truediv__(self, other):
  2184. return self * (self.one / other)
  2185. @call_highest_priority('__rmatmul__')
  2186. def __matmul__(self, other):
  2187. other = _matrixify(other)
  2188. if not getattr(other, 'is_Matrix', False) and not getattr(other, 'is_MatrixLike', False):
  2189. return NotImplemented
  2190. return self.__mul__(other)
  2191. def __mod__(self, other):
  2192. return self.applyfunc(lambda x: x % other)
  2193. @call_highest_priority('__rmul__')
  2194. def __mul__(self, other):
  2195. """Return self*other where other is either a scalar or a matrix
  2196. of compatible dimensions.
  2197. Examples
  2198. ========
  2199. >>> from sympy import Matrix
  2200. >>> A = Matrix([[1, 2, 3], [4, 5, 6]])
  2201. >>> 2*A == A*2 == Matrix([[2, 4, 6], [8, 10, 12]])
  2202. True
  2203. >>> B = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  2204. >>> A*B
  2205. Matrix([
  2206. [30, 36, 42],
  2207. [66, 81, 96]])
  2208. >>> B*A
  2209. Traceback (most recent call last):
  2210. ...
  2211. ShapeError: Matrices size mismatch.
  2212. >>>
  2213. See Also
  2214. ========
  2215. matrix_multiply_elementwise
  2216. """
  2217. return self.multiply(other)
  2218. def multiply(self, other, dotprodsimp=None):
  2219. """Same as __mul__() but with optional simplification.
  2220. Parameters
  2221. ==========
  2222. dotprodsimp : bool, optional
  2223. Specifies whether intermediate term algebraic simplification is used
  2224. during matrix multiplications to control expression blowup and thus
  2225. speed up calculation. Default is off.
  2226. """
  2227. isimpbool = _get_intermediate_simp_bool(False, dotprodsimp)
  2228. other = _matrixify(other)
  2229. # matrix-like objects can have shapes. This is
  2230. # our first sanity check. Double check other is not explicitly not a Matrix.
  2231. if (hasattr(other, 'shape') and len(other.shape) == 2 and
  2232. (getattr(other, 'is_Matrix', True) or
  2233. getattr(other, 'is_MatrixLike', True))):
  2234. if self.shape[1] != other.shape[0]:
  2235. raise ShapeError("Matrix size mismatch: %s * %s." % (
  2236. self.shape, other.shape))
  2237. # honest SymPy matrices defer to their class's routine
  2238. if getattr(other, 'is_Matrix', False):
  2239. m = self._eval_matrix_mul(other)
  2240. if isimpbool:
  2241. return m._new(m.rows, m.cols, [_dotprodsimp(e) for e in m])
  2242. return m
  2243. # Matrix-like objects can be passed to CommonMatrix routines directly.
  2244. if getattr(other, 'is_MatrixLike', False):
  2245. return MatrixArithmetic._eval_matrix_mul(self, other)
  2246. # if 'other' is not iterable then scalar multiplication.
  2247. if not isinstance(other, Iterable):
  2248. try:
  2249. return self._eval_scalar_mul(other)
  2250. except TypeError:
  2251. pass
  2252. return NotImplemented
  2253. def multiply_elementwise(self, other):
  2254. """Return the Hadamard product (elementwise product) of A and B
  2255. Examples
  2256. ========
  2257. >>> from sympy import Matrix
  2258. >>> A = Matrix([[0, 1, 2], [3, 4, 5]])
  2259. >>> B = Matrix([[1, 10, 100], [100, 10, 1]])
  2260. >>> A.multiply_elementwise(B)
  2261. Matrix([
  2262. [ 0, 10, 200],
  2263. [300, 40, 5]])
  2264. See Also
  2265. ========
  2266. sympy.matrices.matrices.MatrixBase.cross
  2267. sympy.matrices.matrices.MatrixBase.dot
  2268. multiply
  2269. """
  2270. if self.shape != other.shape:
  2271. raise ShapeError("Matrix shapes must agree {} != {}".format(self.shape, other.shape))
  2272. return self._eval_matrix_mul_elementwise(other)
  2273. def __neg__(self):
  2274. return self._eval_scalar_mul(-1)
  2275. @call_highest_priority('__rpow__')
  2276. def __pow__(self, exp):
  2277. """Return self**exp a scalar or symbol."""
  2278. return self.pow(exp)
  2279. def pow(self, exp, method=None):
  2280. r"""Return self**exp a scalar or symbol.
  2281. Parameters
  2282. ==========
  2283. method : multiply, mulsimp, jordan, cayley
  2284. If multiply then it returns exponentiation using recursion.
  2285. If jordan then Jordan form exponentiation will be used.
  2286. If cayley then the exponentiation is done using Cayley-Hamilton
  2287. theorem.
  2288. If mulsimp then the exponentiation is done using recursion
  2289. with dotprodsimp. This specifies whether intermediate term
  2290. algebraic simplification is used during naive matrix power to
  2291. control expression blowup and thus speed up calculation.
  2292. If None, then it heuristically decides which method to use.
  2293. """
  2294. if method is not None and method not in ['multiply', 'mulsimp', 'jordan', 'cayley']:
  2295. raise TypeError('No such method')
  2296. if self.rows != self.cols:
  2297. raise NonSquareMatrixError()
  2298. a = self
  2299. jordan_pow = getattr(a, '_matrix_pow_by_jordan_blocks', None)
  2300. exp = sympify(exp)
  2301. if exp.is_zero:
  2302. return a._new(a.rows, a.cols, lambda i, j: int(i == j))
  2303. if exp == 1:
  2304. return a
  2305. diagonal = getattr(a, 'is_diagonal', None)
  2306. if diagonal is not None and diagonal():
  2307. return a._new(a.rows, a.cols, lambda i, j: a[i,j]**exp if i == j else 0)
  2308. if exp.is_Number and exp % 1 == 0:
  2309. if a.rows == 1:
  2310. return a._new([[a[0]**exp]])
  2311. if exp < 0:
  2312. exp = -exp
  2313. a = a.inv()
  2314. # When certain conditions are met,
  2315. # Jordan block algorithm is faster than
  2316. # computation by recursion.
  2317. if method == 'jordan':
  2318. try:
  2319. return jordan_pow(exp)
  2320. except MatrixError:
  2321. if method == 'jordan':
  2322. raise
  2323. elif method == 'cayley':
  2324. if not exp.is_Number or exp % 1 != 0:
  2325. raise ValueError("cayley method is only valid for integer powers")
  2326. return a._eval_pow_by_cayley(exp)
  2327. elif method == "mulsimp":
  2328. if not exp.is_Number or exp % 1 != 0:
  2329. raise ValueError("mulsimp method is only valid for integer powers")
  2330. return a._eval_pow_by_recursion_dotprodsimp(exp)
  2331. elif method == "multiply":
  2332. if not exp.is_Number or exp % 1 != 0:
  2333. raise ValueError("multiply method is only valid for integer powers")
  2334. return a._eval_pow_by_recursion(exp)
  2335. elif method is None and exp.is_Number and exp % 1 == 0:
  2336. # Decide heuristically which method to apply
  2337. if a.rows == 2 and exp > 100000:
  2338. return jordan_pow(exp)
  2339. elif _get_intermediate_simp_bool(True, None):
  2340. return a._eval_pow_by_recursion_dotprodsimp(exp)
  2341. elif exp > 10000:
  2342. return a._eval_pow_by_cayley(exp)
  2343. else:
  2344. return a._eval_pow_by_recursion(exp)
  2345. if jordan_pow:
  2346. try:
  2347. return jordan_pow(exp)
  2348. except NonInvertibleMatrixError:
  2349. # Raised by jordan_pow on zero determinant matrix unless exp is
  2350. # definitely known to be a non-negative integer.
  2351. # Here we raise if n is definitely not a non-negative integer
  2352. # but otherwise we can leave this as an unevaluated MatPow.
  2353. if exp.is_integer is False or exp.is_nonnegative is False:
  2354. raise
  2355. from sympy.matrices.expressions import MatPow
  2356. return MatPow(a, exp)
  2357. @call_highest_priority('__add__')
  2358. def __radd__(self, other):
  2359. return self + other
  2360. @call_highest_priority('__matmul__')
  2361. def __rmatmul__(self, other):
  2362. other = _matrixify(other)
  2363. if not getattr(other, 'is_Matrix', False) and not getattr(other, 'is_MatrixLike', False):
  2364. return NotImplemented
  2365. return self.__rmul__(other)
  2366. @call_highest_priority('__mul__')
  2367. def __rmul__(self, other):
  2368. return self.rmultiply(other)
  2369. def rmultiply(self, other, dotprodsimp=None):
  2370. """Same as __rmul__() but with optional simplification.
  2371. Parameters
  2372. ==========
  2373. dotprodsimp : bool, optional
  2374. Specifies whether intermediate term algebraic simplification is used
  2375. during matrix multiplications to control expression blowup and thus
  2376. speed up calculation. Default is off.
  2377. """
  2378. isimpbool = _get_intermediate_simp_bool(False, dotprodsimp)
  2379. other = _matrixify(other)
  2380. # matrix-like objects can have shapes. This is
  2381. # our first sanity check. Double check other is not explicitly not a Matrix.
  2382. if (hasattr(other, 'shape') and len(other.shape) == 2 and
  2383. (getattr(other, 'is_Matrix', True) or
  2384. getattr(other, 'is_MatrixLike', True))):
  2385. if self.shape[0] != other.shape[1]:
  2386. raise ShapeError("Matrix size mismatch.")
  2387. # honest SymPy matrices defer to their class's routine
  2388. if getattr(other, 'is_Matrix', False):
  2389. m = self._eval_matrix_rmul(other)
  2390. if isimpbool:
  2391. return m._new(m.rows, m.cols, [_dotprodsimp(e) for e in m])
  2392. return m
  2393. # Matrix-like objects can be passed to CommonMatrix routines directly.
  2394. if getattr(other, 'is_MatrixLike', False):
  2395. return MatrixArithmetic._eval_matrix_rmul(self, other)
  2396. # if 'other' is not iterable then scalar multiplication.
  2397. if not isinstance(other, Iterable):
  2398. try:
  2399. return self._eval_scalar_rmul(other)
  2400. except TypeError:
  2401. pass
  2402. return NotImplemented
  2403. @call_highest_priority('__sub__')
  2404. def __rsub__(self, a):
  2405. return (-self) + a
  2406. @call_highest_priority('__rsub__')
  2407. def __sub__(self, a):
  2408. return self + (-a)
  2409. class MatrixCommon(MatrixArithmetic, MatrixOperations, MatrixProperties,
  2410. MatrixSpecial, MatrixShaping):
  2411. """All common matrix operations including basic arithmetic, shaping,
  2412. and special matrices like `zeros`, and `eye`."""
  2413. _diff_wrt = True # type: bool
  2414. class _MinimalMatrix:
  2415. """Class providing the minimum functionality
  2416. for a matrix-like object and implementing every method
  2417. required for a `MatrixRequired`. This class does not have everything
  2418. needed to become a full-fledged SymPy object, but it will satisfy the
  2419. requirements of anything inheriting from `MatrixRequired`. If you wish
  2420. to make a specialized matrix type, make sure to implement these
  2421. methods and properties with the exception of `__init__` and `__repr__`
  2422. which are included for convenience."""
  2423. is_MatrixLike = True
  2424. _sympify = staticmethod(sympify)
  2425. _class_priority = 3
  2426. zero = S.Zero
  2427. one = S.One
  2428. is_Matrix = True
  2429. is_MatrixExpr = False
  2430. @classmethod
  2431. def _new(cls, *args, **kwargs):
  2432. return cls(*args, **kwargs)
  2433. def __init__(self, rows, cols=None, mat=None, copy=False):
  2434. if isfunction(mat):
  2435. # if we passed in a function, use that to populate the indices
  2436. mat = list(mat(i, j) for i in range(rows) for j in range(cols))
  2437. if cols is None and mat is None:
  2438. mat = rows
  2439. rows, cols = getattr(mat, 'shape', (rows, cols))
  2440. try:
  2441. # if we passed in a list of lists, flatten it and set the size
  2442. if cols is None and mat is None:
  2443. mat = rows
  2444. cols = len(mat[0])
  2445. rows = len(mat)
  2446. mat = [x for l in mat for x in l]
  2447. except (IndexError, TypeError):
  2448. pass
  2449. self.mat = tuple(self._sympify(x) for x in mat)
  2450. self.rows, self.cols = rows, cols
  2451. if self.rows is None or self.cols is None:
  2452. raise NotImplementedError("Cannot initialize matrix with given parameters")
  2453. def __getitem__(self, key):
  2454. def _normalize_slices(row_slice, col_slice):
  2455. """Ensure that row_slice and col_slice do not have
  2456. `None` in their arguments. Any integers are converted
  2457. to slices of length 1"""
  2458. if not isinstance(row_slice, slice):
  2459. row_slice = slice(row_slice, row_slice + 1, None)
  2460. row_slice = slice(*row_slice.indices(self.rows))
  2461. if not isinstance(col_slice, slice):
  2462. col_slice = slice(col_slice, col_slice + 1, None)
  2463. col_slice = slice(*col_slice.indices(self.cols))
  2464. return (row_slice, col_slice)
  2465. def _coord_to_index(i, j):
  2466. """Return the index in _mat corresponding
  2467. to the (i,j) position in the matrix. """
  2468. return i * self.cols + j
  2469. if isinstance(key, tuple):
  2470. i, j = key
  2471. if isinstance(i, slice) or isinstance(j, slice):
  2472. # if the coordinates are not slices, make them so
  2473. # and expand the slices so they don't contain `None`
  2474. i, j = _normalize_slices(i, j)
  2475. rowsList, colsList = list(range(self.rows))[i], \
  2476. list(range(self.cols))[j]
  2477. indices = (i * self.cols + j for i in rowsList for j in
  2478. colsList)
  2479. return self._new(len(rowsList), len(colsList),
  2480. list(self.mat[i] for i in indices))
  2481. # if the key is a tuple of ints, change
  2482. # it to an array index
  2483. key = _coord_to_index(i, j)
  2484. return self.mat[key]
  2485. def __eq__(self, other):
  2486. try:
  2487. classof(self, other)
  2488. except TypeError:
  2489. return False
  2490. return (
  2491. self.shape == other.shape and list(self) == list(other))
  2492. def __len__(self):
  2493. return self.rows*self.cols
  2494. def __repr__(self):
  2495. return "_MinimalMatrix({}, {}, {})".format(self.rows, self.cols,
  2496. self.mat)
  2497. @property
  2498. def shape(self):
  2499. return (self.rows, self.cols)
  2500. class _CastableMatrix: # this is needed here ONLY FOR TESTS.
  2501. def as_mutable(self):
  2502. return self
  2503. def as_immutable(self):
  2504. return self
  2505. class _MatrixWrapper:
  2506. """Wrapper class providing the minimum functionality for a matrix-like
  2507. object: .rows, .cols, .shape, indexability, and iterability. CommonMatrix
  2508. math operations should work on matrix-like objects. This one is intended for
  2509. matrix-like objects which use the same indexing format as SymPy with respect
  2510. to returning matrix elements instead of rows for non-tuple indexes.
  2511. """
  2512. is_Matrix = False # needs to be here because of __getattr__
  2513. is_MatrixLike = True
  2514. def __init__(self, mat, shape):
  2515. self.mat = mat
  2516. self.shape = shape
  2517. self.rows, self.cols = shape
  2518. def __getitem__(self, key):
  2519. if isinstance(key, tuple):
  2520. return sympify(self.mat.__getitem__(key))
  2521. return sympify(self.mat.__getitem__((key // self.rows, key % self.cols)))
  2522. def __iter__(self): # supports numpy.matrix and numpy.array
  2523. mat = self.mat
  2524. cols = self.cols
  2525. return iter(sympify(mat[r, c]) for r in range(self.rows) for c in range(cols))
  2526. class MatrixKind(Kind):
  2527. """
  2528. Kind for all matrices in SymPy.
  2529. Basic class for this kind is ``MatrixBase`` and ``MatrixExpr``,
  2530. but any expression representing the matrix can have this.
  2531. Parameters
  2532. ==========
  2533. element_kind : Kind
  2534. Kind of the element. Default is :obj:NumberKind `<sympy.core.kind.NumberKind>`,
  2535. which means that the matrix contains only numbers.
  2536. Examples
  2537. ========
  2538. Any instance of matrix class has ``MatrixKind``.
  2539. >>> from sympy import MatrixSymbol
  2540. >>> A = MatrixSymbol('A', 2,2)
  2541. >>> A.kind
  2542. MatrixKind(NumberKind)
  2543. Although expression representing a matrix may be not instance of
  2544. matrix class, it will have ``MatrixKind`` as well.
  2545. >>> from sympy import MatrixExpr, Integral
  2546. >>> from sympy.abc import x
  2547. >>> intM = Integral(A, x)
  2548. >>> isinstance(intM, MatrixExpr)
  2549. False
  2550. >>> intM.kind
  2551. MatrixKind(NumberKind)
  2552. Use ``isinstance()`` to check for ``MatrixKind` without specifying
  2553. the element kind. Use ``is`` with specifying the element kind.
  2554. >>> from sympy import Matrix
  2555. >>> from sympy.core import NumberKind
  2556. >>> from sympy.matrices import MatrixKind
  2557. >>> M = Matrix([1, 2])
  2558. >>> isinstance(M.kind, MatrixKind)
  2559. True
  2560. >>> M.kind is MatrixKind(NumberKind)
  2561. True
  2562. See Also
  2563. ========
  2564. shape : Function to return the shape of objects with ``MatrixKind``.
  2565. """
  2566. def __new__(cls, element_kind=NumberKind):
  2567. obj = super().__new__(cls, element_kind)
  2568. obj.element_kind = element_kind
  2569. return obj
  2570. def __repr__(self):
  2571. return "MatrixKind(%s)" % self.element_kind
  2572. def _matrixify(mat):
  2573. """If `mat` is a Matrix or is matrix-like,
  2574. return a Matrix or MatrixWrapper object. Otherwise
  2575. `mat` is passed through without modification."""
  2576. if getattr(mat, 'is_Matrix', False) or getattr(mat, 'is_MatrixLike', False):
  2577. return mat
  2578. if not(getattr(mat, 'is_Matrix', True) or getattr(mat, 'is_MatrixLike', True)):
  2579. return mat
  2580. shape = None
  2581. if hasattr(mat, 'shape'): # numpy, scipy.sparse
  2582. if len(mat.shape) == 2:
  2583. shape = mat.shape
  2584. elif hasattr(mat, 'rows') and hasattr(mat, 'cols'): # mpmath
  2585. shape = (mat.rows, mat.cols)
  2586. if shape:
  2587. return _MatrixWrapper(mat, shape)
  2588. return mat
  2589. def a2idx(j, n=None):
  2590. """Return integer after making positive and validating against n."""
  2591. if not isinstance(j, int):
  2592. jindex = getattr(j, '__index__', None)
  2593. if jindex is not None:
  2594. j = jindex()
  2595. else:
  2596. raise IndexError("Invalid index a[%r]" % (j,))
  2597. if n is not None:
  2598. if j < 0:
  2599. j += n
  2600. if not (j >= 0 and j < n):
  2601. raise IndexError("Index out of range: a[%s]" % (j,))
  2602. return int(j)
  2603. def classof(A, B):
  2604. """
  2605. Get the type of the result when combining matrices of different types.
  2606. Currently the strategy is that immutability is contagious.
  2607. Examples
  2608. ========
  2609. >>> from sympy import Matrix, ImmutableMatrix
  2610. >>> from sympy.matrices.common import classof
  2611. >>> M = Matrix([[1, 2], [3, 4]]) # a Mutable Matrix
  2612. >>> IM = ImmutableMatrix([[1, 2], [3, 4]])
  2613. >>> classof(M, IM)
  2614. <class 'sympy.matrices.immutable.ImmutableDenseMatrix'>
  2615. """
  2616. priority_A = getattr(A, '_class_priority', None)
  2617. priority_B = getattr(B, '_class_priority', None)
  2618. if None not in (priority_A, priority_B):
  2619. if A._class_priority > B._class_priority:
  2620. return A.__class__
  2621. else:
  2622. return B.__class__
  2623. try:
  2624. import numpy
  2625. except ImportError:
  2626. pass
  2627. else:
  2628. if isinstance(A, numpy.ndarray):
  2629. return B.__class__
  2630. if isinstance(B, numpy.ndarray):
  2631. return A.__class__
  2632. raise TypeError("Incompatible classes %s, %s" % (A.__class__, B.__class__))