common.py 91 KB

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