test_matrices.py 141 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005
  1. import random
  2. import concurrent.futures
  3. from collections.abc import Hashable
  4. from sympy.core.add import Add
  5. from sympy.core.function import (Function, diff, expand)
  6. from sympy.core.numbers import (E, Float, I, Integer, Rational, nan, oo, pi)
  7. from sympy.core.power import Pow
  8. from sympy.core.singleton import S
  9. from sympy.core.symbol import (Symbol, symbols)
  10. from sympy.core.sympify import sympify
  11. from sympy.functions.elementary.complexes import Abs
  12. from sympy.functions.elementary.exponential import (exp, log)
  13. from sympy.functions.elementary.miscellaneous import (Max, Min, sqrt)
  14. from sympy.functions.elementary.trigonometric import (cos, sin, tan)
  15. from sympy.integrals.integrals import integrate
  16. from sympy.polys.polytools import (Poly, PurePoly)
  17. from sympy.printing.str import sstr
  18. from sympy.sets.sets import FiniteSet
  19. from sympy.simplify.simplify import (signsimp, simplify)
  20. from sympy.simplify.trigsimp import trigsimp
  21. from sympy.matrices.matrices import (ShapeError, MatrixError,
  22. NonSquareMatrixError, DeferredVector, _find_reasonable_pivot_naive,
  23. _simplify)
  24. from sympy.matrices import (
  25. GramSchmidt, ImmutableMatrix, ImmutableSparseMatrix, Matrix,
  26. SparseMatrix, casoratian, diag, eye, hessian,
  27. matrix_multiply_elementwise, ones, randMatrix, rot_axis1, rot_axis2,
  28. rot_axis3, wronskian, zeros, MutableDenseMatrix, ImmutableDenseMatrix,
  29. MatrixSymbol, dotprodsimp, rot_ccw_axis1, rot_ccw_axis2, rot_ccw_axis3)
  30. from sympy.matrices.utilities import _dotprodsimp_state
  31. from sympy.core import Tuple, Wild
  32. from sympy.functions.special.tensor_functions import KroneckerDelta
  33. from sympy.utilities.iterables import flatten, capture, iterable
  34. from sympy.utilities.exceptions import ignore_warnings, SymPyDeprecationWarning
  35. from sympy.testing.pytest import (raises, XFAIL, slow, skip, skip_under_pyodide,
  36. warns_deprecated_sympy, warns)
  37. from sympy.assumptions import Q
  38. from sympy.tensor.array import Array
  39. from sympy.matrices.expressions import MatPow
  40. from sympy.algebras import Quaternion
  41. from sympy.abc import a, b, c, d, x, y, z, t
  42. # don't re-order this list
  43. classes = (Matrix, SparseMatrix, ImmutableMatrix, ImmutableSparseMatrix)
  44. def test_args():
  45. for n, cls in enumerate(classes):
  46. m = cls.zeros(3, 2)
  47. # all should give back the same type of arguments, e.g. ints for shape
  48. assert m.shape == (3, 2) and all(type(i) is int for i in m.shape)
  49. assert m.rows == 3 and type(m.rows) is int
  50. assert m.cols == 2 and type(m.cols) is int
  51. if not n % 2:
  52. assert type(m.flat()) in (list, tuple, Tuple)
  53. else:
  54. assert type(m.todok()) is dict
  55. def test_deprecated_mat_smat():
  56. for cls in Matrix, ImmutableMatrix:
  57. m = cls.zeros(3, 2)
  58. with warns_deprecated_sympy():
  59. mat = m._mat
  60. assert mat == m.flat()
  61. for cls in SparseMatrix, ImmutableSparseMatrix:
  62. m = cls.zeros(3, 2)
  63. with warns_deprecated_sympy():
  64. smat = m._smat
  65. assert smat == m.todok()
  66. def test_division():
  67. v = Matrix(1, 2, [x, y])
  68. assert v/z == Matrix(1, 2, [x/z, y/z])
  69. def test_sum():
  70. m = Matrix([[1, 2, 3], [x, y, x], [2*y, -50, z*x]])
  71. assert m + m == Matrix([[2, 4, 6], [2*x, 2*y, 2*x], [4*y, -100, 2*z*x]])
  72. n = Matrix(1, 2, [1, 2])
  73. raises(ShapeError, lambda: m + n)
  74. def test_abs():
  75. m = Matrix(1, 2, [-3, x])
  76. n = Matrix(1, 2, [3, Abs(x)])
  77. assert abs(m) == n
  78. def test_addition():
  79. a = Matrix((
  80. (1, 2),
  81. (3, 1),
  82. ))
  83. b = Matrix((
  84. (1, 2),
  85. (3, 0),
  86. ))
  87. assert a + b == a.add(b) == Matrix([[2, 4], [6, 1]])
  88. def test_fancy_index_matrix():
  89. for M in (Matrix, SparseMatrix):
  90. a = M(3, 3, range(9))
  91. assert a == a[:, :]
  92. assert a[1, :] == Matrix(1, 3, [3, 4, 5])
  93. assert a[:, 1] == Matrix([1, 4, 7])
  94. assert a[[0, 1], :] == Matrix([[0, 1, 2], [3, 4, 5]])
  95. assert a[[0, 1], 2] == a[[0, 1], [2]]
  96. assert a[2, [0, 1]] == a[[2], [0, 1]]
  97. assert a[:, [0, 1]] == Matrix([[0, 1], [3, 4], [6, 7]])
  98. assert a[0, 0] == 0
  99. assert a[0:2, :] == Matrix([[0, 1, 2], [3, 4, 5]])
  100. assert a[:, 0:2] == Matrix([[0, 1], [3, 4], [6, 7]])
  101. assert a[::2, 1] == a[[0, 2], 1]
  102. assert a[1, ::2] == a[1, [0, 2]]
  103. a = M(3, 3, range(9))
  104. assert a[[0, 2, 1, 2, 1], :] == Matrix([
  105. [0, 1, 2],
  106. [6, 7, 8],
  107. [3, 4, 5],
  108. [6, 7, 8],
  109. [3, 4, 5]])
  110. assert a[:, [0,2,1,2,1]] == Matrix([
  111. [0, 2, 1, 2, 1],
  112. [3, 5, 4, 5, 4],
  113. [6, 8, 7, 8, 7]])
  114. a = SparseMatrix.zeros(3)
  115. a[1, 2] = 2
  116. a[0, 1] = 3
  117. a[2, 0] = 4
  118. assert a.extract([1, 1], [2]) == Matrix([
  119. [2],
  120. [2]])
  121. assert a.extract([1, 0], [2, 2, 2]) == Matrix([
  122. [2, 2, 2],
  123. [0, 0, 0]])
  124. assert a.extract([1, 0, 1, 2], [2, 0, 1, 0]) == Matrix([
  125. [2, 0, 0, 0],
  126. [0, 0, 3, 0],
  127. [2, 0, 0, 0],
  128. [0, 4, 0, 4]])
  129. def test_multiplication():
  130. a = Matrix((
  131. (1, 2),
  132. (3, 1),
  133. (0, 6),
  134. ))
  135. b = Matrix((
  136. (1, 2),
  137. (3, 0),
  138. ))
  139. c = a*b
  140. assert c[0, 0] == 7
  141. assert c[0, 1] == 2
  142. assert c[1, 0] == 6
  143. assert c[1, 1] == 6
  144. assert c[2, 0] == 18
  145. assert c[2, 1] == 0
  146. try:
  147. eval('c = a @ b')
  148. except SyntaxError:
  149. pass
  150. else:
  151. assert c[0, 0] == 7
  152. assert c[0, 1] == 2
  153. assert c[1, 0] == 6
  154. assert c[1, 1] == 6
  155. assert c[2, 0] == 18
  156. assert c[2, 1] == 0
  157. h = matrix_multiply_elementwise(a, c)
  158. assert h == a.multiply_elementwise(c)
  159. assert h[0, 0] == 7
  160. assert h[0, 1] == 4
  161. assert h[1, 0] == 18
  162. assert h[1, 1] == 6
  163. assert h[2, 0] == 0
  164. assert h[2, 1] == 0
  165. raises(ShapeError, lambda: matrix_multiply_elementwise(a, b))
  166. c = b * Symbol("x")
  167. assert isinstance(c, Matrix)
  168. assert c[0, 0] == x
  169. assert c[0, 1] == 2*x
  170. assert c[1, 0] == 3*x
  171. assert c[1, 1] == 0
  172. c2 = x * b
  173. assert c == c2
  174. c = 5 * b
  175. assert isinstance(c, Matrix)
  176. assert c[0, 0] == 5
  177. assert c[0, 1] == 2*5
  178. assert c[1, 0] == 3*5
  179. assert c[1, 1] == 0
  180. try:
  181. eval('c = 5 @ b')
  182. except SyntaxError:
  183. pass
  184. else:
  185. assert isinstance(c, Matrix)
  186. assert c[0, 0] == 5
  187. assert c[0, 1] == 2*5
  188. assert c[1, 0] == 3*5
  189. assert c[1, 1] == 0
  190. M = Matrix([[oo, 0], [0, oo]])
  191. assert M ** 2 == M
  192. M = Matrix([[oo, oo], [0, 0]])
  193. assert M ** 2 == Matrix([[nan, nan], [nan, nan]])
  194. def test_power():
  195. raises(NonSquareMatrixError, lambda: Matrix((1, 2))**2)
  196. R = Rational
  197. A = Matrix([[2, 3], [4, 5]])
  198. assert (A**-3)[:] == [R(-269)/8, R(153)/8, R(51)/2, R(-29)/2]
  199. assert (A**5)[:] == [6140, 8097, 10796, 14237]
  200. A = Matrix([[2, 1, 3], [4, 2, 4], [6, 12, 1]])
  201. assert (A**3)[:] == [290, 262, 251, 448, 440, 368, 702, 954, 433]
  202. assert A**0 == eye(3)
  203. assert A**1 == A
  204. assert (Matrix([[2]]) ** 100)[0, 0] == 2**100
  205. assert eye(2)**10000000 == eye(2)
  206. assert Matrix([[1, 2], [3, 4]])**Integer(2) == Matrix([[7, 10], [15, 22]])
  207. A = Matrix([[33, 24], [48, 57]])
  208. assert (A**S.Half)[:] == [5, 2, 4, 7]
  209. A = Matrix([[0, 4], [-1, 5]])
  210. assert (A**S.Half)**2 == A
  211. assert Matrix([[1, 0], [1, 1]])**S.Half == Matrix([[1, 0], [S.Half, 1]])
  212. assert Matrix([[1, 0], [1, 1]])**0.5 == Matrix([[1, 0], [0.5, 1]])
  213. from sympy.abc import n
  214. assert Matrix([[1, a], [0, 1]])**n == Matrix([[1, a*n], [0, 1]])
  215. assert Matrix([[b, a], [0, b]])**n == Matrix([[b**n, a*b**(n-1)*n], [0, b**n]])
  216. assert Matrix([
  217. [a**n, a**(n - 1)*n, (a**n*n**2 - a**n*n)/(2*a**2)],
  218. [ 0, a**n, a**(n - 1)*n],
  219. [ 0, 0, a**n]])
  220. assert Matrix([[a, 1, 0], [0, a, 0], [0, 0, b]])**n == Matrix([
  221. [a**n, a**(n-1)*n, 0],
  222. [0, a**n, 0],
  223. [0, 0, b**n]])
  224. A = Matrix([[1, 0], [1, 7]])
  225. assert A._matrix_pow_by_jordan_blocks(S(3)) == A._eval_pow_by_recursion(3)
  226. A = Matrix([[2]])
  227. assert A**10 == Matrix([[2**10]]) == A._matrix_pow_by_jordan_blocks(S(10)) == \
  228. A._eval_pow_by_recursion(10)
  229. # testing a matrix that cannot be jordan blocked issue 11766
  230. m = Matrix([[3, 0, 0, 0, -3], [0, -3, -3, 0, 3], [0, 3, 0, 3, 0], [0, 0, 3, 0, 3], [3, 0, 0, 3, 0]])
  231. raises(MatrixError, lambda: m._matrix_pow_by_jordan_blocks(S(10)))
  232. # test issue 11964
  233. raises(MatrixError, lambda: Matrix([[1, 1], [3, 3]])._matrix_pow_by_jordan_blocks(S(-10)))
  234. A = Matrix([[0, 1, 0], [0, 0, 1], [0, 0, 0]]) # Nilpotent jordan block size 3
  235. assert A**10.0 == Matrix([[0, 0, 0], [0, 0, 0], [0, 0, 0]])
  236. raises(ValueError, lambda: A**2.1)
  237. raises(ValueError, lambda: A**Rational(3, 2))
  238. A = Matrix([[8, 1], [3, 2]])
  239. assert A**10.0 == Matrix([[1760744107, 272388050], [817164150, 126415807]])
  240. A = Matrix([[0, 0, 1], [0, 0, 1], [0, 0, 1]]) # Nilpotent jordan block size 1
  241. assert A**10.0 == Matrix([[0, 0, 1], [0, 0, 1], [0, 0, 1]])
  242. A = Matrix([[0, 1, 0], [0, 0, 1], [0, 0, 1]]) # Nilpotent jordan block size 2
  243. assert A**10.0 == Matrix([[0, 0, 1], [0, 0, 1], [0, 0, 1]])
  244. n = Symbol('n', integer=True)
  245. assert isinstance(A**n, MatPow)
  246. n = Symbol('n', integer=True, negative=True)
  247. raises(ValueError, lambda: A**n)
  248. n = Symbol('n', integer=True, nonnegative=True)
  249. assert A**n == Matrix([
  250. [KroneckerDelta(0, n), KroneckerDelta(1, n), -KroneckerDelta(0, n) - KroneckerDelta(1, n) + 1],
  251. [ 0, KroneckerDelta(0, n), 1 - KroneckerDelta(0, n)],
  252. [ 0, 0, 1]])
  253. assert A**(n + 2) == Matrix([[0, 0, 1], [0, 0, 1], [0, 0, 1]])
  254. raises(ValueError, lambda: A**Rational(3, 2))
  255. A = Matrix([[0, 0, 1], [3, 0, 1], [4, 3, 1]])
  256. assert A**5.0 == Matrix([[168, 72, 89], [291, 144, 161], [572, 267, 329]])
  257. assert A**5.0 == A**5
  258. A = Matrix([[0, 1, 0],[-1, 0, 0],[0, 0, 0]])
  259. n = Symbol("n")
  260. An = A**n
  261. assert An.subs(n, 2).doit() == A**2
  262. raises(ValueError, lambda: An.subs(n, -2).doit())
  263. assert An * An == A**(2*n)
  264. # concretizing behavior for non-integer and complex powers
  265. A = Matrix([[0,0,0],[0,0,0],[0,0,0]])
  266. n = Symbol('n', integer=True, positive=True)
  267. assert A**n == A
  268. n = Symbol('n', integer=True, nonnegative=True)
  269. assert A**n == diag(0**n, 0**n, 0**n)
  270. assert (A**n).subs(n, 0) == eye(3)
  271. assert (A**n).subs(n, 1) == zeros(3)
  272. A = Matrix ([[2,0,0],[0,2,0],[0,0,2]])
  273. assert A**2.1 == diag (2**2.1, 2**2.1, 2**2.1)
  274. assert A**I == diag (2**I, 2**I, 2**I)
  275. A = Matrix([[0, 1, 0], [0, 0, 1], [0, 0, 1]])
  276. raises(ValueError, lambda: A**2.1)
  277. raises(ValueError, lambda: A**I)
  278. A = Matrix([[S.Half, S.Half], [S.Half, S.Half]])
  279. assert A**S.Half == A
  280. A = Matrix([[1, 1],[3, 3]])
  281. assert A**S.Half == Matrix ([[S.Half, S.Half], [3*S.Half, 3*S.Half]])
  282. def test_issue_17247_expression_blowup_1():
  283. M = Matrix([[1+x, 1-x], [1-x, 1+x]])
  284. with dotprodsimp(True):
  285. assert M.exp().expand() == Matrix([
  286. [ (exp(2*x) + exp(2))/2, (-exp(2*x) + exp(2))/2],
  287. [(-exp(2*x) + exp(2))/2, (exp(2*x) + exp(2))/2]])
  288. def test_issue_17247_expression_blowup_2():
  289. M = Matrix([[1+x, 1-x], [1-x, 1+x]])
  290. with dotprodsimp(True):
  291. P, J = M.jordan_form ()
  292. assert P*J*P.inv()
  293. def test_issue_17247_expression_blowup_3():
  294. M = Matrix([[1+x, 1-x], [1-x, 1+x]])
  295. with dotprodsimp(True):
  296. assert M**100 == Matrix([
  297. [633825300114114700748351602688*x**100 + 633825300114114700748351602688, 633825300114114700748351602688 - 633825300114114700748351602688*x**100],
  298. [633825300114114700748351602688 - 633825300114114700748351602688*x**100, 633825300114114700748351602688*x**100 + 633825300114114700748351602688]])
  299. def test_issue_17247_expression_blowup_4():
  300. # This matrix takes extremely long on current master even with intermediate simplification so an abbreviated version is used. It is left here for test in case of future optimizations.
  301. # M = Matrix(S('''[
  302. # [ -3/4, 45/32 - 37*I/16, 1/4 + I/2, -129/64 - 9*I/64, 1/4 - 5*I/16, 65/128 + 87*I/64, -9/32 - I/16, 183/256 - 97*I/128, 3/64 + 13*I/64, -23/32 - 59*I/256, 15/128 - 3*I/32, 19/256 + 551*I/1024],
  303. # [-149/64 + 49*I/32, -177/128 - 1369*I/128, 125/64 + 87*I/64, -2063/256 + 541*I/128, 85/256 - 33*I/16, 805/128 + 2415*I/512, -219/128 + 115*I/256, 6301/4096 - 6609*I/1024, 119/128 + 143*I/128, -10879/2048 + 4343*I/4096, 129/256 - 549*I/512, 42533/16384 + 29103*I/8192],
  304. # [ 1/2 - I, 9/4 + 55*I/16, -3/4, 45/32 - 37*I/16, 1/4 + I/2, -129/64 - 9*I/64, 1/4 - 5*I/16, 65/128 + 87*I/64, -9/32 - I/16, 183/256 - 97*I/128, 3/64 + 13*I/64, -23/32 - 59*I/256],
  305. # [ -5/8 - 39*I/16, 2473/256 + 137*I/64, -149/64 + 49*I/32, -177/128 - 1369*I/128, 125/64 + 87*I/64, -2063/256 + 541*I/128, 85/256 - 33*I/16, 805/128 + 2415*I/512, -219/128 + 115*I/256, 6301/4096 - 6609*I/1024, 119/128 + 143*I/128, -10879/2048 + 4343*I/4096],
  306. # [ 1 + I, -19/4 + 5*I/4, 1/2 - I, 9/4 + 55*I/16, -3/4, 45/32 - 37*I/16, 1/4 + I/2, -129/64 - 9*I/64, 1/4 - 5*I/16, 65/128 + 87*I/64, -9/32 - I/16, 183/256 - 97*I/128],
  307. # [ 21/8 + I, -537/64 + 143*I/16, -5/8 - 39*I/16, 2473/256 + 137*I/64, -149/64 + 49*I/32, -177/128 - 1369*I/128, 125/64 + 87*I/64, -2063/256 + 541*I/128, 85/256 - 33*I/16, 805/128 + 2415*I/512, -219/128 + 115*I/256, 6301/4096 - 6609*I/1024],
  308. # [ -2, 17/4 - 13*I/2, 1 + I, -19/4 + 5*I/4, 1/2 - I, 9/4 + 55*I/16, -3/4, 45/32 - 37*I/16, 1/4 + I/2, -129/64 - 9*I/64, 1/4 - 5*I/16, 65/128 + 87*I/64],
  309. # [ 1/4 + 13*I/4, -825/64 - 147*I/32, 21/8 + I, -537/64 + 143*I/16, -5/8 - 39*I/16, 2473/256 + 137*I/64, -149/64 + 49*I/32, -177/128 - 1369*I/128, 125/64 + 87*I/64, -2063/256 + 541*I/128, 85/256 - 33*I/16, 805/128 + 2415*I/512],
  310. # [ -4*I, 27/2 + 6*I, -2, 17/4 - 13*I/2, 1 + I, -19/4 + 5*I/4, 1/2 - I, 9/4 + 55*I/16, -3/4, 45/32 - 37*I/16, 1/4 + I/2, -129/64 - 9*I/64],
  311. # [ 1/4 + 5*I/2, -23/8 - 57*I/16, 1/4 + 13*I/4, -825/64 - 147*I/32, 21/8 + I, -537/64 + 143*I/16, -5/8 - 39*I/16, 2473/256 + 137*I/64, -149/64 + 49*I/32, -177/128 - 1369*I/128, 125/64 + 87*I/64, -2063/256 + 541*I/128],
  312. # [ -4, 9 - 5*I, -4*I, 27/2 + 6*I, -2, 17/4 - 13*I/2, 1 + I, -19/4 + 5*I/4, 1/2 - I, 9/4 + 55*I/16, -3/4, 45/32 - 37*I/16],
  313. # [ -2*I, 119/8 + 29*I/4, 1/4 + 5*I/2, -23/8 - 57*I/16, 1/4 + 13*I/4, -825/64 - 147*I/32, 21/8 + I, -537/64 + 143*I/16, -5/8 - 39*I/16, 2473/256 + 137*I/64, -149/64 + 49*I/32, -177/128 - 1369*I/128]]'''))
  314. # assert M**10 == Matrix([
  315. # [ 7*(-221393644768594642173548179825793834595 - 1861633166167425978847110897013541127952*I)/9671406556917033397649408, 15*(31670992489131684885307005100073928751695 + 10329090958303458811115024718207404523808*I)/77371252455336267181195264, 7*(-3710978679372178839237291049477017392703 + 1377706064483132637295566581525806894169*I)/19342813113834066795298816, (9727707023582419994616144751727760051598 - 59261571067013123836477348473611225724433*I)/9671406556917033397649408, (31896723509506857062605551443641668183707 + 54643444538699269118869436271152084599580*I)/38685626227668133590597632, (-2024044860947539028275487595741003997397402 + 130959428791783397562960461903698670485863*I)/309485009821345068724781056, 3*(26190251453797590396533756519358368860907 - 27221191754180839338002754608545400941638*I)/77371252455336267181195264, (1154643595139959842768960128434994698330461 + 3385496216250226964322872072260446072295634*I)/618970019642690137449562112, 3*(-31849347263064464698310044805285774295286 - 11877437776464148281991240541742691164309*I)/77371252455336267181195264, (4661330392283532534549306589669150228040221 - 4171259766019818631067810706563064103956871*I)/1237940039285380274899124224, (9598353794289061833850770474812760144506 + 358027153990999990968244906482319780943983*I)/309485009821345068724781056, (-9755135335127734571547571921702373498554177 - 4837981372692695195747379349593041939686540*I)/2475880078570760549798248448],
  316. # [(-379516731607474268954110071392894274962069 - 422272153179747548473724096872271700878296*I)/77371252455336267181195264, (41324748029613152354787280677832014263339501 - 12715121258662668420833935373453570749288074*I)/1237940039285380274899124224, (-339216903907423793947110742819264306542397 + 494174755147303922029979279454787373566517*I)/77371252455336267181195264, (-18121350839962855576667529908850640619878381 - 37413012454129786092962531597292531089199003*I)/1237940039285380274899124224, (2489661087330511608618880408199633556675926 + 1137821536550153872137379935240732287260863*I)/309485009821345068724781056, (-136644109701594123227587016790354220062972119 + 110130123468183660555391413889600443583585272*I)/4951760157141521099596496896, (1488043981274920070468141664150073426459593 - 9691968079933445130866371609614474474327650*I)/1237940039285380274899124224, 27*(4636797403026872518131756991410164760195942 + 3369103221138229204457272860484005850416533*I)/4951760157141521099596496896, (-8534279107365915284081669381642269800472363 + 2241118846262661434336333368511372725482742*I)/1237940039285380274899124224, (60923350128174260992536531692058086830950875 - 263673488093551053385865699805250505661590126*I)/9903520314283042199192993792, (18520943561240714459282253753348921824172569 + 24846649186468656345966986622110971925703604*I)/4951760157141521099596496896, (-232781130692604829085973604213529649638644431 + 35981505277760667933017117949103953338570617*I)/9903520314283042199192993792],
  317. # [ (8742968295129404279528270438201520488950 + 3061473358639249112126847237482570858327*I)/4835703278458516698824704, (-245657313712011778432792959787098074935273 + 253113767861878869678042729088355086740856*I)/38685626227668133590597632, (1947031161734702327107371192008011621193 - 19462330079296259148177542369999791122762*I)/9671406556917033397649408, (552856485625209001527688949522750288619217 + 392928441196156725372494335248099016686580*I)/77371252455336267181195264, (-44542866621905323121630214897126343414629 + 3265340021421335059323962377647649632959*I)/19342813113834066795298816, (136272594005759723105646069956434264218730 - 330975364731707309489523680957584684763587*I)/38685626227668133590597632, (27392593965554149283318732469825168894401 + 75157071243800133880129376047131061115278*I)/38685626227668133590597632, 7*(-357821652913266734749960136017214096276154 - 45509144466378076475315751988405961498243*I)/309485009821345068724781056, (104485001373574280824835174390219397141149 - 99041000529599568255829489765415726168162*I)/77371252455336267181195264, (1198066993119982409323525798509037696321291 + 4249784165667887866939369628840569844519936*I)/618970019642690137449562112, (-114985392587849953209115599084503853611014 - 52510376847189529234864487459476242883449*I)/77371252455336267181195264, (6094620517051332877965959223269600650951573 - 4683469779240530439185019982269137976201163*I)/1237940039285380274899124224],
  318. # [ (611292255597977285752123848828590587708323 - 216821743518546668382662964473055912169502*I)/77371252455336267181195264, (-1144023204575811464652692396337616594307487 + 12295317806312398617498029126807758490062855*I)/309485009821345068724781056, (-374093027769390002505693378578475235158281 - 573533923565898290299607461660384634333639*I)/77371252455336267181195264, (47405570632186659000138546955372796986832987 - 2837476058950808941605000274055970055096534*I)/1237940039285380274899124224, (-571573207393621076306216726219753090535121 + 533381457185823100878764749236639320783831*I)/77371252455336267181195264, (-7096548151856165056213543560958582513797519 - 24035731898756040059329175131592138642195366*I)/618970019642690137449562112, (2396762128833271142000266170154694033849225 + 1448501087375679588770230529017516492953051*I)/309485009821345068724781056, (-150609293845161968447166237242456473262037053 + 92581148080922977153207018003184520294188436*I)/4951760157141521099596496896, 5*(270278244730804315149356082977618054486347 - 1997830155222496880429743815321662710091562*I)/1237940039285380274899124224, (62978424789588828258068912690172109324360330 + 44803641177219298311493356929537007630129097*I)/2475880078570760549798248448, 19*(-451431106327656743945775812536216598712236 + 114924966793632084379437683991151177407937*I)/1237940039285380274899124224, (63417747628891221594106738815256002143915995 - 261508229397507037136324178612212080871150958*I)/9903520314283042199192993792],
  319. # [ (-2144231934021288786200752920446633703357 + 2305614436009705803670842248131563850246*I)/1208925819614629174706176, (-90720949337459896266067589013987007078153 - 221951119475096403601562347412753844534569*I)/19342813113834066795298816, (11590973613116630788176337262688659880376 + 6514520676308992726483494976339330626159*I)/4835703278458516698824704, 3*(-131776217149000326618649542018343107657237 + 79095042939612668486212006406818285287004*I)/38685626227668133590597632, (10100577916793945997239221374025741184951 - 28631383488085522003281589065994018550748*I)/9671406556917033397649408, 67*(10090295594251078955008130473573667572549 + 10449901522697161049513326446427839676762*I)/77371252455336267181195264, (-54270981296988368730689531355811033930513 - 3413683117592637309471893510944045467443*I)/19342813113834066795298816, (440372322928679910536575560069973699181278 - 736603803202303189048085196176918214409081*I)/77371252455336267181195264, (33220374714789391132887731139763250155295 + 92055083048787219934030779066298919603554*I)/38685626227668133590597632, 5*(-594638554579967244348856981610805281527116 - 82309245323128933521987392165716076704057*I)/309485009821345068724781056, (128056368815300084550013708313312073721955 - 114619107488668120303579745393765245911404*I)/77371252455336267181195264, 21*(59839959255173222962789517794121843393573 + 241507883613676387255359616163487405826334*I)/618970019642690137449562112],
  320. # [ (-13454485022325376674626653802541391955147 + 184471402121905621396582628515905949793486*I)/19342813113834066795298816, (-6158730123400322562149780662133074862437105 - 3416173052604643794120262081623703514107476*I)/154742504910672534362390528, (770558003844914708453618983120686116100419 - 127758381209767638635199674005029818518766*I)/77371252455336267181195264, (-4693005771813492267479835161596671660631703 + 12703585094750991389845384539501921531449948*I)/309485009821345068724781056, (-295028157441149027913545676461260860036601 - 841544569970643160358138082317324743450770*I)/77371252455336267181195264, (56716442796929448856312202561538574275502893 + 7216818824772560379753073185990186711454778*I)/1237940039285380274899124224, 15*(-87061038932753366532685677510172566368387 + 61306141156647596310941396434445461895538*I)/154742504910672534362390528, (-3455315109680781412178133042301025723909347 - 24969329563196972466388460746447646686670670*I)/618970019642690137449562112, (2453418854160886481106557323699250865361849 + 1497886802326243014471854112161398141242514*I)/309485009821345068724781056, (-151343224544252091980004429001205664193082173 + 90471883264187337053549090899816228846836628*I)/4951760157141521099596496896, (1652018205533026103358164026239417416432989 - 9959733619236515024261775397109724431400162*I)/1237940039285380274899124224, 3*(40676374242956907656984876692623172736522006 + 31023357083037817469535762230872667581366205*I)/4951760157141521099596496896],
  321. # [ (-1226990509403328460274658603410696548387 - 4131739423109992672186585941938392788458*I)/1208925819614629174706176, (162392818524418973411975140074368079662703 + 23706194236915374831230612374344230400704*I)/9671406556917033397649408, (-3935678233089814180000602553655565621193 + 2283744757287145199688061892165659502483*I)/1208925819614629174706176, (-2400210250844254483454290806930306285131 - 315571356806370996069052930302295432758205*I)/19342813113834066795298816, (13365917938215281056563183751673390817910 + 15911483133819801118348625831132324863881*I)/4835703278458516698824704, 3*(-215950551370668982657516660700301003897855 + 51684341999223632631602864028309400489378*I)/38685626227668133590597632, (20886089946811765149439844691320027184765 - 30806277083146786592790625980769214361844*I)/9671406556917033397649408, (562180634592713285745940856221105667874855 + 1031543963988260765153550559766662245114916*I)/77371252455336267181195264, (-65820625814810177122941758625652476012867 - 12429918324787060890804395323920477537595*I)/19342813113834066795298816, (319147848192012911298771180196635859221089 - 402403304933906769233365689834404519960394*I)/38685626227668133590597632, (23035615120921026080284733394359587955057 + 115351677687031786114651452775242461310624*I)/38685626227668133590597632, (-3426830634881892756966440108592579264936130 - 1022954961164128745603407283836365128598559*I)/309485009821345068724781056],
  322. # [ (-192574788060137531023716449082856117537757 - 69222967328876859586831013062387845780692*I)/19342813113834066795298816, (2736383768828013152914815341491629299773262 - 2773252698016291897599353862072533475408743*I)/77371252455336267181195264, (-23280005281223837717773057436155921656805 + 214784953368021840006305033048142888879224*I)/19342813113834066795298816, (-3035247484028969580570400133318947903462326 - 2195168903335435855621328554626336958674325*I)/77371252455336267181195264, (984552428291526892214541708637840971548653 - 64006622534521425620714598573494988589378*I)/77371252455336267181195264, (-3070650452470333005276715136041262898509903 + 7286424705750810474140953092161794621989080*I)/154742504910672534362390528, (-147848877109756404594659513386972921139270 - 416306113044186424749331418059456047650861*I)/38685626227668133590597632, (55272118474097814260289392337160619494260781 + 7494019668394781211907115583302403519488058*I)/1237940039285380274899124224, (-581537886583682322424771088996959213068864 + 542191617758465339135308203815256798407429*I)/77371252455336267181195264, (-6422548983676355789975736799494791970390991 - 23524183982209004826464749309156698827737702*I)/618970019642690137449562112, 7*(180747195387024536886923192475064903482083 + 84352527693562434817771649853047924991804*I)/154742504910672534362390528, (-135485179036717001055310712747643466592387031 + 102346575226653028836678855697782273460527608*I)/4951760157141521099596496896],
  323. # [ (3384238362616083147067025892852431152105 + 156724444932584900214919898954874618256*I)/604462909807314587353088, (-59558300950677430189587207338385764871866 + 114427143574375271097298201388331237478857*I)/4835703278458516698824704, (-1356835789870635633517710130971800616227 - 7023484098542340388800213478357340875410*I)/1208925819614629174706176, (234884918567993750975181728413524549575881 + 79757294640629983786895695752733890213506*I)/9671406556917033397649408, (-7632732774935120473359202657160313866419 + 2905452608512927560554702228553291839465*I)/1208925819614629174706176, (52291747908702842344842889809762246649489 - 520996778817151392090736149644507525892649*I)/19342813113834066795298816, (17472406829219127839967951180375981717322 + 23464704213841582137898905375041819568669*I)/4835703278458516698824704, (-911026971811893092350229536132730760943307 + 150799318130900944080399439626714846752360*I)/38685626227668133590597632, (26234457233977042811089020440646443590687 - 45650293039576452023692126463683727692890*I)/9671406556917033397649408, 3*(288348388717468992528382586652654351121357 + 454526517721403048270274049572136109264668*I)/77371252455336267181195264, (-91583492367747094223295011999405657956347 - 12704691128268298435362255538069612411331*I)/19342813113834066795298816, (411208730251327843849027957710164064354221 - 569898526380691606955496789378230959965898*I)/38685626227668133590597632],
  324. # [ (27127513117071487872628354831658811211795 - 37765296987901990355760582016892124833857*I)/4835703278458516698824704, (1741779916057680444272938534338833170625435 + 3083041729779495966997526404685535449810378*I)/77371252455336267181195264, 3*(-60642236251815783728374561836962709533401 - 24630301165439580049891518846174101510744*I)/19342813113834066795298816, 3*(445885207364591681637745678755008757483408 - 350948497734812895032502179455610024541643*I)/38685626227668133590597632, (-47373295621391195484367368282471381775684 + 219122969294089357477027867028071400054973*I)/19342813113834066795298816, (-2801565819673198722993348253876353741520438 - 2250142129822658548391697042460298703335701*I)/77371252455336267181195264, (801448252275607253266997552356128790317119 - 50890367688077858227059515894356594900558*I)/77371252455336267181195264, (-5082187758525931944557763799137987573501207 + 11610432359082071866576699236013484487676124*I)/309485009821345068724781056, (-328925127096560623794883760398247685166830 - 643447969697471610060622160899409680422019*I)/77371252455336267181195264, 15*(2954944669454003684028194956846659916299765 + 33434406416888505837444969347824812608566*I)/1237940039285380274899124224, (-415749104352001509942256567958449835766827 + 479330966144175743357171151440020955412219*I)/77371252455336267181195264, 3*(-4639987285852134369449873547637372282914255 - 11994411888966030153196659207284951579243273*I)/1237940039285380274899124224],
  325. # [ (-478846096206269117345024348666145495601 + 1249092488629201351470551186322814883283*I)/302231454903657293676544, (-17749319421930878799354766626365926894989 - 18264580106418628161818752318217357231971*I)/1208925819614629174706176, (2801110795431528876849623279389579072819 + 363258850073786330770713557775566973248*I)/604462909807314587353088, (-59053496693129013745775512127095650616252 + 78143588734197260279248498898321500167517*I)/4835703278458516698824704, (-283186724922498212468162690097101115349 - 6443437753863179883794497936345437398276*I)/1208925819614629174706176, (188799118826748909206887165661384998787543 + 84274736720556630026311383931055307398820*I)/9671406556917033397649408, (-5482217151670072904078758141270295025989 + 1818284338672191024475557065444481298568*I)/1208925819614629174706176, (56564463395350195513805521309731217952281 - 360208541416798112109946262159695452898431*I)/19342813113834066795298816, 11*(1259539805728870739006416869463689438068 + 1409136581547898074455004171305324917387*I)/4835703278458516698824704, 5*(-123701190701414554945251071190688818343325 + 30997157322590424677294553832111902279712*I)/38685626227668133590597632, (16130917381301373033736295883982414239781 - 32752041297570919727145380131926943374516*I)/9671406556917033397649408, (650301385108223834347093740500375498354925 + 899526407681131828596801223402866051809258*I)/77371252455336267181195264],
  326. # [ (9011388245256140876590294262420614839483 + 8167917972423946282513000869327525382672*I)/1208925819614629174706176, (-426393174084720190126376382194036323028924 + 180692224825757525982858693158209545430621*I)/9671406556917033397649408, (24588556702197802674765733448108154175535 - 45091766022876486566421953254051868331066*I)/4835703278458516698824704, (1872113939365285277373877183750416985089691 + 3030392393733212574744122057679633775773130*I)/77371252455336267181195264, (-222173405538046189185754954524429864167549 - 75193157893478637039381059488387511299116*I)/19342813113834066795298816, (2670821320766222522963689317316937579844558 - 2645837121493554383087981511645435472169191*I)/77371252455336267181195264, 5*(-2100110309556476773796963197283876204940 + 41957457246479840487980315496957337371937*I)/19342813113834066795298816, (-5733743755499084165382383818991531258980593 - 3328949988392698205198574824396695027195732*I)/154742504910672534362390528, (707827994365259025461378911159398206329247 - 265730616623227695108042528694302299777294*I)/77371252455336267181195264, (-1442501604682933002895864804409322823788319 + 11504137805563265043376405214378288793343879*I)/309485009821345068724781056, (-56130472299445561499538726459719629522285 - 61117552419727805035810982426639329818864*I)/9671406556917033397649408, (39053692321126079849054272431599539429908717 - 10209127700342570953247177602860848130710666*I)/1237940039285380274899124224]])
  327. M = Matrix(S('''[
  328. [ -3/4, 45/32 - 37*I/16, 1/4 + I/2, -129/64 - 9*I/64, 1/4 - 5*I/16, 65/128 + 87*I/64],
  329. [-149/64 + 49*I/32, -177/128 - 1369*I/128, 125/64 + 87*I/64, -2063/256 + 541*I/128, 85/256 - 33*I/16, 805/128 + 2415*I/512],
  330. [ 1/2 - I, 9/4 + 55*I/16, -3/4, 45/32 - 37*I/16, 1/4 + I/2, -129/64 - 9*I/64],
  331. [ -5/8 - 39*I/16, 2473/256 + 137*I/64, -149/64 + 49*I/32, -177/128 - 1369*I/128, 125/64 + 87*I/64, -2063/256 + 541*I/128],
  332. [ 1 + I, -19/4 + 5*I/4, 1/2 - I, 9/4 + 55*I/16, -3/4, 45/32 - 37*I/16],
  333. [ 21/8 + I, -537/64 + 143*I/16, -5/8 - 39*I/16, 2473/256 + 137*I/64, -149/64 + 49*I/32, -177/128 - 1369*I/128]]'''))
  334. with dotprodsimp(True):
  335. assert M**10 == Matrix(S('''[
  336. [ 7369525394972778926719607798014571861/604462909807314587353088 - 229284202061790301477392339912557559*I/151115727451828646838272, -19704281515163975949388435612632058035/1208925819614629174706176 + 14319858347987648723768698170712102887*I/302231454903657293676544, -3623281909451783042932142262164941211/604462909807314587353088 - 6039240602494288615094338643452320495*I/604462909807314587353088, 109260497799140408739847239685705357695/2417851639229258349412352 - 7427566006564572463236368211555511431*I/2417851639229258349412352, -16095803767674394244695716092817006641/2417851639229258349412352 + 10336681897356760057393429626719177583*I/1208925819614629174706176, -42207883340488041844332828574359769743/2417851639229258349412352 - 182332262671671273188016400290188468499*I/4835703278458516698824704],
  337. [50566491050825573392726324995779608259/1208925819614629174706176 - 90047007594468146222002432884052362145*I/2417851639229258349412352, 74273703462900000967697427843983822011/1208925819614629174706176 + 265947522682943571171988741842776095421*I/1208925819614629174706176, -116900341394390200556829767923360888429/2417851639229258349412352 - 53153263356679268823910621474478756845*I/2417851639229258349412352, 195407378023867871243426523048612490249/1208925819614629174706176 - 1242417915995360200584837585002906728929*I/9671406556917033397649408, -863597594389821970177319682495878193/302231454903657293676544 + 476936100741548328800725360758734300481*I/9671406556917033397649408, -3154451590535653853562472176601754835575/19342813113834066795298816 - 232909875490506237386836489998407329215*I/2417851639229258349412352],
  338. [ -1715444997702484578716037230949868543/302231454903657293676544 + 5009695651321306866158517287924120777*I/302231454903657293676544, -30551582497996879620371947949342101301/604462909807314587353088 - 7632518367986526187139161303331519629*I/151115727451828646838272, 312680739924495153190604170938220575/18889465931478580854784 - 108664334509328818765959789219208459*I/75557863725914323419136, -14693696966703036206178521686918865509/604462909807314587353088 + 72345386220900843930147151999899692401*I/1208925819614629174706176, -8218872496728882299722894680635296519/1208925819614629174706176 - 16776782833358893712645864791807664983*I/1208925819614629174706176, 143237839169380078671242929143670635137/2417851639229258349412352 + 2883817094806115974748882735218469447*I/2417851639229258349412352],
  339. [ 3087979417831061365023111800749855987/151115727451828646838272 + 34441942370802869368851419102423997089*I/604462909807314587353088, -148309181940158040917731426845476175667/604462909807314587353088 - 263987151804109387844966835369350904919*I/9671406556917033397649408, 50259518594816377378747711930008883165/1208925819614629174706176 - 95713974916869240305450001443767979653*I/2417851639229258349412352, 153466447023875527996457943521467271119/2417851639229258349412352 + 517285524891117105834922278517084871349*I/2417851639229258349412352, -29184653615412989036678939366291205575/604462909807314587353088 - 27551322282526322041080173287022121083*I/1208925819614629174706176, 196404220110085511863671393922447671649/1208925819614629174706176 - 1204712019400186021982272049902206202145*I/9671406556917033397649408],
  340. [ -2632581805949645784625606590600098779/151115727451828646838272 - 589957435912868015140272627522612771*I/37778931862957161709568, 26727850893953715274702844733506310247/302231454903657293676544 - 10825791956782128799168209600694020481*I/302231454903657293676544, -1036348763702366164044671908440791295/151115727451828646838272 + 3188624571414467767868303105288107375*I/151115727451828646838272, -36814959939970644875593411585393242449/604462909807314587353088 - 18457555789119782404850043842902832647*I/302231454903657293676544, 12454491297984637815063964572803058647/604462909807314587353088 - 340489532842249733975074349495329171*I/302231454903657293676544, -19547211751145597258386735573258916681/604462909807314587353088 + 87299583775782199663414539883938008933*I/1208925819614629174706176],
  341. [ -40281994229560039213253423262678393183/604462909807314587353088 - 2939986850065527327299273003299736641*I/604462909807314587353088, 331940684638052085845743020267462794181/2417851639229258349412352 - 284574901963624403933361315517248458969*I/1208925819614629174706176, 6453843623051745485064693628073010961/302231454903657293676544 + 36062454107479732681350914931391590957*I/604462909807314587353088, -147665869053634695632880753646441962067/604462909807314587353088 - 305987938660447291246597544085345123927*I/9671406556917033397649408, 107821369195275772166593879711259469423/2417851639229258349412352 - 11645185518211204108659001435013326687*I/302231454903657293676544, 64121228424717666402009446088588091619/1208925819614629174706176 + 265557133337095047883844369272389762133*I/1208925819614629174706176]]'''))
  342. def test_issue_17247_expression_blowup_5():
  343. M = Matrix(6, 6, lambda i, j: 1 + (-1)**(i+j)*I)
  344. with dotprodsimp(True):
  345. assert M.charpoly('x') == PurePoly(x**6 + (-6 - 6*I)*x**5 + 36*I*x**4, x, domain='EX')
  346. def test_issue_17247_expression_blowup_6():
  347. M = Matrix(8, 8, [x+i for i in range (64)])
  348. with dotprodsimp(True):
  349. assert M.det('bareiss') == 0
  350. def test_issue_17247_expression_blowup_7():
  351. M = Matrix(6, 6, lambda i, j: 1 + (-1)**(i+j)*I)
  352. with dotprodsimp(True):
  353. assert M.det('berkowitz') == 0
  354. def test_issue_17247_expression_blowup_8():
  355. M = Matrix(8, 8, [x+i for i in range (64)])
  356. with dotprodsimp(True):
  357. assert M.det('lu') == 0
  358. def test_issue_17247_expression_blowup_9():
  359. M = Matrix(8, 8, [x+i for i in range (64)])
  360. with dotprodsimp(True):
  361. assert M.rref() == (Matrix([
  362. [1, 0, -1, -2, -3, -4, -5, -6],
  363. [0, 1, 2, 3, 4, 5, 6, 7],
  364. [0, 0, 0, 0, 0, 0, 0, 0],
  365. [0, 0, 0, 0, 0, 0, 0, 0],
  366. [0, 0, 0, 0, 0, 0, 0, 0],
  367. [0, 0, 0, 0, 0, 0, 0, 0],
  368. [0, 0, 0, 0, 0, 0, 0, 0],
  369. [0, 0, 0, 0, 0, 0, 0, 0]]), (0, 1))
  370. def test_issue_17247_expression_blowup_10():
  371. M = Matrix(6, 6, lambda i, j: 1 + (-1)**(i+j)*I)
  372. with dotprodsimp(True):
  373. assert M.cofactor(0, 0) == 0
  374. def test_issue_17247_expression_blowup_11():
  375. M = Matrix(6, 6, lambda i, j: 1 + (-1)**(i+j)*I)
  376. with dotprodsimp(True):
  377. assert M.cofactor_matrix() == Matrix(6, 6, [0]*36)
  378. def test_issue_17247_expression_blowup_12():
  379. M = Matrix(6, 6, lambda i, j: 1 + (-1)**(i+j)*I)
  380. with dotprodsimp(True):
  381. assert M.eigenvals() == {6: 1, 6*I: 1, 0: 4}
  382. def test_issue_17247_expression_blowup_13():
  383. M = Matrix([
  384. [ 0, 1 - x, x + 1, 1 - x],
  385. [1 - x, x + 1, 0, x + 1],
  386. [ 0, 1 - x, x + 1, 1 - x],
  387. [ 0, 0, 1 - x, 0]])
  388. ev = M.eigenvects()
  389. assert ev[0] == (0, 2, [Matrix([0, -1, 0, 1])])
  390. assert ev[1][0] == x - sqrt(2)*(x - 1) + 1
  391. assert ev[1][1] == 1
  392. assert ev[1][2][0].expand(deep=False, numer=True) == Matrix([
  393. [(-x + sqrt(2)*(x - 1) - 1)/(x - 1)],
  394. [-4*x/(x**2 - 2*x + 1) + (x + 1)*(x - sqrt(2)*(x - 1) + 1)/(x**2 - 2*x + 1)],
  395. [(-x + sqrt(2)*(x - 1) - 1)/(x - 1)],
  396. [1]
  397. ])
  398. assert ev[2][0] == x + sqrt(2)*(x - 1) + 1
  399. assert ev[2][1] == 1
  400. assert ev[2][2][0].expand(deep=False, numer=True) == Matrix([
  401. [(-x - sqrt(2)*(x - 1) - 1)/(x - 1)],
  402. [-4*x/(x**2 - 2*x + 1) + (x + 1)*(x + sqrt(2)*(x - 1) + 1)/(x**2 - 2*x + 1)],
  403. [(-x - sqrt(2)*(x - 1) - 1)/(x - 1)],
  404. [1]
  405. ])
  406. def test_issue_17247_expression_blowup_14():
  407. M = Matrix(8, 8, ([1+x, 1-x]*4 + [1-x, 1+x]*4)*4)
  408. with dotprodsimp(True):
  409. assert M.echelon_form() == Matrix([
  410. [x + 1, 1 - x, x + 1, 1 - x, x + 1, 1 - x, x + 1, 1 - x],
  411. [ 0, 4*x, 0, 4*x, 0, 4*x, 0, 4*x],
  412. [ 0, 0, 0, 0, 0, 0, 0, 0],
  413. [ 0, 0, 0, 0, 0, 0, 0, 0],
  414. [ 0, 0, 0, 0, 0, 0, 0, 0],
  415. [ 0, 0, 0, 0, 0, 0, 0, 0],
  416. [ 0, 0, 0, 0, 0, 0, 0, 0],
  417. [ 0, 0, 0, 0, 0, 0, 0, 0]])
  418. def test_issue_17247_expression_blowup_15():
  419. M = Matrix(8, 8, ([1+x, 1-x]*4 + [1-x, 1+x]*4)*4)
  420. with dotprodsimp(True):
  421. assert M.rowspace() == [Matrix([[x + 1, 1 - x, x + 1, 1 - x, x + 1, 1 - x, x + 1, 1 - x]]), Matrix([[0, 4*x, 0, 4*x, 0, 4*x, 0, 4*x]])]
  422. def test_issue_17247_expression_blowup_16():
  423. M = Matrix(8, 8, ([1+x, 1-x]*4 + [1-x, 1+x]*4)*4)
  424. with dotprodsimp(True):
  425. assert M.columnspace() == [Matrix([[x + 1],[1 - x],[x + 1],[1 - x],[x + 1],[1 - x],[x + 1],[1 - x]]), Matrix([[1 - x],[x + 1],[1 - x],[x + 1],[1 - x],[x + 1],[1 - x],[x + 1]])]
  426. def test_issue_17247_expression_blowup_17():
  427. M = Matrix(8, 8, [x+i for i in range (64)])
  428. with dotprodsimp(True):
  429. assert M.nullspace() == [
  430. Matrix([[1],[-2],[1],[0],[0],[0],[0],[0]]),
  431. Matrix([[2],[-3],[0],[1],[0],[0],[0],[0]]),
  432. Matrix([[3],[-4],[0],[0],[1],[0],[0],[0]]),
  433. Matrix([[4],[-5],[0],[0],[0],[1],[0],[0]]),
  434. Matrix([[5],[-6],[0],[0],[0],[0],[1],[0]]),
  435. Matrix([[6],[-7],[0],[0],[0],[0],[0],[1]])]
  436. def test_issue_17247_expression_blowup_18():
  437. M = Matrix(6, 6, ([1+x, 1-x]*3 + [1-x, 1+x]*3)*3)
  438. with dotprodsimp(True):
  439. assert not M.is_nilpotent()
  440. def test_issue_17247_expression_blowup_19():
  441. M = Matrix(S('''[
  442. [ -3/4, 0, 1/4 + I/2, 0],
  443. [ 0, -177/128 - 1369*I/128, 0, -2063/256 + 541*I/128],
  444. [ 1/2 - I, 0, 0, 0],
  445. [ 0, 0, 0, -177/128 - 1369*I/128]]'''))
  446. with dotprodsimp(True):
  447. assert not M.is_diagonalizable()
  448. def test_issue_17247_expression_blowup_20():
  449. M = Matrix([
  450. [x + 1, 1 - x, 0, 0],
  451. [1 - x, x + 1, 0, x + 1],
  452. [ 0, 1 - x, x + 1, 0],
  453. [ 0, 0, 0, x + 1]])
  454. with dotprodsimp(True):
  455. assert M.diagonalize() == (Matrix([
  456. [1, 1, 0, (x + 1)/(x - 1)],
  457. [1, -1, 0, 0],
  458. [1, 1, 1, 0],
  459. [0, 0, 0, 1]]),
  460. Matrix([
  461. [2, 0, 0, 0],
  462. [0, 2*x, 0, 0],
  463. [0, 0, x + 1, 0],
  464. [0, 0, 0, x + 1]]))
  465. def test_issue_17247_expression_blowup_21():
  466. M = Matrix(S('''[
  467. [ -3/4, 45/32 - 37*I/16, 0, 0],
  468. [-149/64 + 49*I/32, -177/128 - 1369*I/128, 0, -2063/256 + 541*I/128],
  469. [ 0, 9/4 + 55*I/16, 2473/256 + 137*I/64, 0],
  470. [ 0, 0, 0, -177/128 - 1369*I/128]]'''))
  471. with dotprodsimp(True):
  472. assert M.inv(method='GE') == Matrix(S('''[
  473. [-26194832/3470993 - 31733264*I/3470993, 156352/3470993 + 10325632*I/3470993, 0, -7741283181072/3306971225785 + 2999007604624*I/3306971225785],
  474. [4408224/3470993 - 9675328*I/3470993, -2422272/3470993 + 1523712*I/3470993, 0, -1824666489984/3306971225785 - 1401091949952*I/3306971225785],
  475. [-26406945676288/22270005630769 + 10245925485056*I/22270005630769, 7453523312640/22270005630769 + 1601616519168*I/22270005630769, 633088/6416033 - 140288*I/6416033, 872209227109521408/21217636514687010905 + 6066405081802389504*I/21217636514687010905],
  476. [0, 0, 0, -11328/952745 + 87616*I/952745]]'''))
  477. def test_issue_17247_expression_blowup_22():
  478. M = Matrix(S('''[
  479. [ -3/4, 45/32 - 37*I/16, 0, 0],
  480. [-149/64 + 49*I/32, -177/128 - 1369*I/128, 0, -2063/256 + 541*I/128],
  481. [ 0, 9/4 + 55*I/16, 2473/256 + 137*I/64, 0],
  482. [ 0, 0, 0, -177/128 - 1369*I/128]]'''))
  483. with dotprodsimp(True):
  484. assert M.inv(method='LU') == Matrix(S('''[
  485. [-26194832/3470993 - 31733264*I/3470993, 156352/3470993 + 10325632*I/3470993, 0, -7741283181072/3306971225785 + 2999007604624*I/3306971225785],
  486. [4408224/3470993 - 9675328*I/3470993, -2422272/3470993 + 1523712*I/3470993, 0, -1824666489984/3306971225785 - 1401091949952*I/3306971225785],
  487. [-26406945676288/22270005630769 + 10245925485056*I/22270005630769, 7453523312640/22270005630769 + 1601616519168*I/22270005630769, 633088/6416033 - 140288*I/6416033, 872209227109521408/21217636514687010905 + 6066405081802389504*I/21217636514687010905],
  488. [0, 0, 0, -11328/952745 + 87616*I/952745]]'''))
  489. def test_issue_17247_expression_blowup_23():
  490. M = Matrix(S('''[
  491. [ -3/4, 45/32 - 37*I/16, 0, 0],
  492. [-149/64 + 49*I/32, -177/128 - 1369*I/128, 0, -2063/256 + 541*I/128],
  493. [ 0, 9/4 + 55*I/16, 2473/256 + 137*I/64, 0],
  494. [ 0, 0, 0, -177/128 - 1369*I/128]]'''))
  495. with dotprodsimp(True):
  496. assert M.inv(method='ADJ').expand() == Matrix(S('''[
  497. [-26194832/3470993 - 31733264*I/3470993, 156352/3470993 + 10325632*I/3470993, 0, -7741283181072/3306971225785 + 2999007604624*I/3306971225785],
  498. [4408224/3470993 - 9675328*I/3470993, -2422272/3470993 + 1523712*I/3470993, 0, -1824666489984/3306971225785 - 1401091949952*I/3306971225785],
  499. [-26406945676288/22270005630769 + 10245925485056*I/22270005630769, 7453523312640/22270005630769 + 1601616519168*I/22270005630769, 633088/6416033 - 140288*I/6416033, 872209227109521408/21217636514687010905 + 6066405081802389504*I/21217636514687010905],
  500. [0, 0, 0, -11328/952745 + 87616*I/952745]]'''))
  501. def test_issue_17247_expression_blowup_24():
  502. M = SparseMatrix(S('''[
  503. [ -3/4, 45/32 - 37*I/16, 0, 0],
  504. [-149/64 + 49*I/32, -177/128 - 1369*I/128, 0, -2063/256 + 541*I/128],
  505. [ 0, 9/4 + 55*I/16, 2473/256 + 137*I/64, 0],
  506. [ 0, 0, 0, -177/128 - 1369*I/128]]'''))
  507. with dotprodsimp(True):
  508. assert M.inv(method='CH') == Matrix(S('''[
  509. [-26194832/3470993 - 31733264*I/3470993, 156352/3470993 + 10325632*I/3470993, 0, -7741283181072/3306971225785 + 2999007604624*I/3306971225785],
  510. [4408224/3470993 - 9675328*I/3470993, -2422272/3470993 + 1523712*I/3470993, 0, -1824666489984/3306971225785 - 1401091949952*I/3306971225785],
  511. [-26406945676288/22270005630769 + 10245925485056*I/22270005630769, 7453523312640/22270005630769 + 1601616519168*I/22270005630769, 633088/6416033 - 140288*I/6416033, 872209227109521408/21217636514687010905 + 6066405081802389504*I/21217636514687010905],
  512. [0, 0, 0, -11328/952745 + 87616*I/952745]]'''))
  513. def test_issue_17247_expression_blowup_25():
  514. M = SparseMatrix(S('''[
  515. [ -3/4, 45/32 - 37*I/16, 0, 0],
  516. [-149/64 + 49*I/32, -177/128 - 1369*I/128, 0, -2063/256 + 541*I/128],
  517. [ 0, 9/4 + 55*I/16, 2473/256 + 137*I/64, 0],
  518. [ 0, 0, 0, -177/128 - 1369*I/128]]'''))
  519. with dotprodsimp(True):
  520. assert M.inv(method='LDL') == Matrix(S('''[
  521. [-26194832/3470993 - 31733264*I/3470993, 156352/3470993 + 10325632*I/3470993, 0, -7741283181072/3306971225785 + 2999007604624*I/3306971225785],
  522. [4408224/3470993 - 9675328*I/3470993, -2422272/3470993 + 1523712*I/3470993, 0, -1824666489984/3306971225785 - 1401091949952*I/3306971225785],
  523. [-26406945676288/22270005630769 + 10245925485056*I/22270005630769, 7453523312640/22270005630769 + 1601616519168*I/22270005630769, 633088/6416033 - 140288*I/6416033, 872209227109521408/21217636514687010905 + 6066405081802389504*I/21217636514687010905],
  524. [0, 0, 0, -11328/952745 + 87616*I/952745]]'''))
  525. def test_issue_17247_expression_blowup_26():
  526. M = Matrix(S('''[
  527. [ -3/4, 45/32 - 37*I/16, 1/4 + I/2, -129/64 - 9*I/64, 1/4 - 5*I/16, 65/128 + 87*I/64, -9/32 - I/16, 183/256 - 97*I/128],
  528. [-149/64 + 49*I/32, -177/128 - 1369*I/128, 125/64 + 87*I/64, -2063/256 + 541*I/128, 85/256 - 33*I/16, 805/128 + 2415*I/512, -219/128 + 115*I/256, 6301/4096 - 6609*I/1024],
  529. [ 1/2 - I, 9/4 + 55*I/16, -3/4, 45/32 - 37*I/16, 1/4 + I/2, -129/64 - 9*I/64, 1/4 - 5*I/16, 65/128 + 87*I/64],
  530. [ -5/8 - 39*I/16, 2473/256 + 137*I/64, -149/64 + 49*I/32, -177/128 - 1369*I/128, 125/64 + 87*I/64, -2063/256 + 541*I/128, 85/256 - 33*I/16, 805/128 + 2415*I/512],
  531. [ 1 + I, -19/4 + 5*I/4, 1/2 - I, 9/4 + 55*I/16, -3/4, 45/32 - 37*I/16, 1/4 + I/2, -129/64 - 9*I/64],
  532. [ 21/8 + I, -537/64 + 143*I/16, -5/8 - 39*I/16, 2473/256 + 137*I/64, -149/64 + 49*I/32, -177/128 - 1369*I/128, 125/64 + 87*I/64, -2063/256 + 541*I/128],
  533. [ -2, 17/4 - 13*I/2, 1 + I, -19/4 + 5*I/4, 1/2 - I, 9/4 + 55*I/16, -3/4, 45/32 - 37*I/16],
  534. [ 1/4 + 13*I/4, -825/64 - 147*I/32, 21/8 + I, -537/64 + 143*I/16, -5/8 - 39*I/16, 2473/256 + 137*I/64, -149/64 + 49*I/32, -177/128 - 1369*I/128]]'''))
  535. with dotprodsimp(True):
  536. assert M.rank() == 4
  537. def test_issue_17247_expression_blowup_27():
  538. M = Matrix([
  539. [ 0, 1 - x, x + 1, 1 - x],
  540. [1 - x, x + 1, 0, x + 1],
  541. [ 0, 1 - x, x + 1, 1 - x],
  542. [ 0, 0, 1 - x, 0]])
  543. with dotprodsimp(True):
  544. P, J = M.jordan_form()
  545. assert P.expand() == Matrix(S('''[
  546. [ 0, 4*x/(x**2 - 2*x + 1), -(-17*x**4 + 12*sqrt(2)*x**4 - 4*sqrt(2)*x**3 + 6*x**3 - 6*x - 4*sqrt(2)*x + 12*sqrt(2) + 17)/(-7*x**4 + 5*sqrt(2)*x**4 - 6*sqrt(2)*x**3 + 8*x**3 - 2*x**2 + 8*x + 6*sqrt(2)*x - 5*sqrt(2) - 7), -(12*sqrt(2)*x**4 + 17*x**4 - 6*x**3 - 4*sqrt(2)*x**3 - 4*sqrt(2)*x + 6*x - 17 + 12*sqrt(2))/(7*x**4 + 5*sqrt(2)*x**4 - 6*sqrt(2)*x**3 - 8*x**3 + 2*x**2 - 8*x + 6*sqrt(2)*x - 5*sqrt(2) + 7)],
  547. [x - 1, x/(x - 1) + 1/(x - 1), (-7*x**3 + 5*sqrt(2)*x**3 - x**2 + sqrt(2)*x**2 - sqrt(2)*x - x - 5*sqrt(2) - 7)/(-3*x**3 + 2*sqrt(2)*x**3 - 2*sqrt(2)*x**2 + 3*x**2 + 2*sqrt(2)*x + 3*x - 3 - 2*sqrt(2)), (7*x**3 + 5*sqrt(2)*x**3 + x**2 + sqrt(2)*x**2 - sqrt(2)*x + x - 5*sqrt(2) + 7)/(2*sqrt(2)*x**3 + 3*x**3 - 3*x**2 - 2*sqrt(2)*x**2 - 3*x + 2*sqrt(2)*x - 2*sqrt(2) + 3)],
  548. [ 0, 1, -(-3*x**2 + 2*sqrt(2)*x**2 + 2*x - 3 - 2*sqrt(2))/(-x**2 + sqrt(2)*x**2 - 2*sqrt(2)*x + 1 + sqrt(2)), -(2*sqrt(2)*x**2 + 3*x**2 - 2*x - 2*sqrt(2) + 3)/(x**2 + sqrt(2)*x**2 - 2*sqrt(2)*x - 1 + sqrt(2))],
  549. [1 - x, 0, 1, 1]]''')).expand()
  550. assert J == Matrix(S('''[
  551. [0, 1, 0, 0],
  552. [0, 0, 0, 0],
  553. [0, 0, x - sqrt(2)*(x - 1) + 1, 0],
  554. [0, 0, 0, x + sqrt(2)*(x - 1) + 1]]'''))
  555. def test_issue_17247_expression_blowup_28():
  556. M = Matrix(S('''[
  557. [ -3/4, 45/32 - 37*I/16, 0, 0],
  558. [-149/64 + 49*I/32, -177/128 - 1369*I/128, 0, -2063/256 + 541*I/128],
  559. [ 0, 9/4 + 55*I/16, 2473/256 + 137*I/64, 0],
  560. [ 0, 0, 0, -177/128 - 1369*I/128]]'''))
  561. with dotprodsimp(True):
  562. assert M.singular_values() == S('''[
  563. sqrt(14609315/131072 + sqrt(64789115132571/2147483648 - 2*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3) + 76627253330829751075/(35184372088832*sqrt(64789115132571/4294967296 + 3546944054712886603889144627/(110680464442257309696*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3)) + 2*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3))) - 3546944054712886603889144627/(110680464442257309696*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3)))/2 + sqrt(64789115132571/4294967296 + 3546944054712886603889144627/(110680464442257309696*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3)) + 2*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3))/2),
  564. sqrt(14609315/131072 - sqrt(64789115132571/2147483648 - 2*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3) + 76627253330829751075/(35184372088832*sqrt(64789115132571/4294967296 + 3546944054712886603889144627/(110680464442257309696*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3)) + 2*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3))) - 3546944054712886603889144627/(110680464442257309696*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3)))/2 + sqrt(64789115132571/4294967296 + 3546944054712886603889144627/(110680464442257309696*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3)) + 2*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3))/2),
  565. sqrt(14609315/131072 - sqrt(64789115132571/4294967296 + 3546944054712886603889144627/(110680464442257309696*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3)) + 2*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3))/2 + sqrt(64789115132571/2147483648 - 2*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3) - 76627253330829751075/(35184372088832*sqrt(64789115132571/4294967296 + 3546944054712886603889144627/(110680464442257309696*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3)) + 2*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3))) - 3546944054712886603889144627/(110680464442257309696*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3)))/2),
  566. sqrt(14609315/131072 - sqrt(64789115132571/4294967296 + 3546944054712886603889144627/(110680464442257309696*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3)) + 2*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3))/2 - sqrt(64789115132571/2147483648 - 2*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3) - 76627253330829751075/(35184372088832*sqrt(64789115132571/4294967296 + 3546944054712886603889144627/(110680464442257309696*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3)) + 2*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3))) - 3546944054712886603889144627/(110680464442257309696*(25895222463957462655758224991455280215303/633825300114114700748351602688 + sqrt(1213909058710955930446995195883114969038524625997915131236390724543989220134670)*I/22282920707136844948184236032)**(1/3)))/2)]''')
  567. def test_issue_16823():
  568. # This still needs to be fixed if not using dotprodsimp.
  569. M = Matrix(S('''[
  570. [1+I,-19/4+5/4*I,1/2-I,9/4+55/16*I,-3/4,45/32-37/16*I,1/4+1/2*I,-129/64-9/64*I,1/4-5/16*I,65/128+87/64*I,-9/32-1/16*I,183/256-97/128*I,3/64+13/64*I,-23/32-59/256*I,15/128-3/32*I,19/256+551/1024*I],
  571. [21/8+I,-537/64+143/16*I,-5/8-39/16*I,2473/256+137/64*I,-149/64+49/32*I,-177/128-1369/128*I,125/64+87/64*I,-2063/256+541/128*I,85/256-33/16*I,805/128+2415/512*I,-219/128+115/256*I,6301/4096-6609/1024*I,119/128+143/128*I,-10879/2048+4343/4096*I,129/256-549/512*I,42533/16384+29103/8192*I],
  572. [-2,17/4-13/2*I,1+I,-19/4+5/4*I,1/2-I,9/4+55/16*I,-3/4,45/32-37/16*I,1/4+1/2*I,-129/64-9/64*I,1/4-5/16*I,65/128+87/64*I,-9/32-1/16*I,183/256-97/128*I,3/64+13/64*I,-23/32-59/256*I],
  573. [1/4+13/4*I,-825/64-147/32*I,21/8+I,-537/64+143/16*I,-5/8-39/16*I,2473/256+137/64*I,-149/64+49/32*I,-177/128-1369/128*I,125/64+87/64*I,-2063/256+541/128*I,85/256-33/16*I,805/128+2415/512*I,-219/128+115/256*I,6301/4096-6609/1024*I,119/128+143/128*I,-10879/2048+4343/4096*I],
  574. [-4*I,27/2+6*I,-2,17/4-13/2*I,1+I,-19/4+5/4*I,1/2-I,9/4+55/16*I,-3/4,45/32-37/16*I,1/4+1/2*I,-129/64-9/64*I,1/4-5/16*I,65/128+87/64*I,-9/32-1/16*I,183/256-97/128*I],
  575. [1/4+5/2*I,-23/8-57/16*I,1/4+13/4*I,-825/64-147/32*I,21/8+I,-537/64+143/16*I,-5/8-39/16*I,2473/256+137/64*I,-149/64+49/32*I,-177/128-1369/128*I,125/64+87/64*I,-2063/256+541/128*I,85/256-33/16*I,805/128+2415/512*I,-219/128+115/256*I,6301/4096-6609/1024*I],
  576. [-4,9-5*I,-4*I,27/2+6*I,-2,17/4-13/2*I,1+I,-19/4+5/4*I,1/2-I,9/4+55/16*I,-3/4,45/32-37/16*I,1/4+1/2*I,-129/64-9/64*I,1/4-5/16*I,65/128+87/64*I],
  577. [-2*I,119/8+29/4*I,1/4+5/2*I,-23/8-57/16*I,1/4+13/4*I,-825/64-147/32*I,21/8+I,-537/64+143/16*I,-5/8-39/16*I,2473/256+137/64*I,-149/64+49/32*I,-177/128-1369/128*I,125/64+87/64*I,-2063/256+541/128*I,85/256-33/16*I,805/128+2415/512*I],
  578. [0,-6,-4,9-5*I,-4*I,27/2+6*I,-2,17/4-13/2*I,1+I,-19/4+5/4*I,1/2-I,9/4+55/16*I,-3/4,45/32-37/16*I,1/4+1/2*I,-129/64-9/64*I],
  579. [1,-9/4+3*I,-2*I,119/8+29/4*I,1/4+5/2*I,-23/8-57/16*I,1/4+13/4*I,-825/64-147/32*I,21/8+I,-537/64+143/16*I,-5/8-39/16*I,2473/256+137/64*I,-149/64+49/32*I,-177/128-1369/128*I,125/64+87/64*I,-2063/256+541/128*I],
  580. [0,-4*I,0,-6,-4,9-5*I,-4*I,27/2+6*I,-2,17/4-13/2*I,1+I,-19/4+5/4*I,1/2-I,9/4+55/16*I,-3/4,45/32-37/16*I],
  581. [0,1/4+1/2*I,1,-9/4+3*I,-2*I,119/8+29/4*I,1/4+5/2*I,-23/8-57/16*I,1/4+13/4*I,-825/64-147/32*I,21/8+I,-537/64+143/16*I,-5/8-39/16*I,2473/256+137/64*I,-149/64+49/32*I,-177/128-1369/128*I]]'''))
  582. with dotprodsimp(True):
  583. assert M.rank() == 8
  584. def test_issue_18531():
  585. # solve_linear_system still needs fixing but the rref works.
  586. M = Matrix([
  587. [1, 1, 1, 1, 1, 0, 1, 0, 0],
  588. [1 + sqrt(2), -1 + sqrt(2), 1 - sqrt(2), -sqrt(2) - 1, 1, 1, -1, 1, 1],
  589. [-5 + 2*sqrt(2), -5 - 2*sqrt(2), -5 - 2*sqrt(2), -5 + 2*sqrt(2), -7, 2, -7, -2, 0],
  590. [-3*sqrt(2) - 1, 1 - 3*sqrt(2), -1 + 3*sqrt(2), 1 + 3*sqrt(2), -7, -5, 7, -5, 3],
  591. [7 - 4*sqrt(2), 4*sqrt(2) + 7, 4*sqrt(2) + 7, 7 - 4*sqrt(2), 7, -12, 7, 12, 0],
  592. [-1 + 3*sqrt(2), 1 + 3*sqrt(2), -3*sqrt(2) - 1, 1 - 3*sqrt(2), 7, -5, -7, -5, 3],
  593. [-3 + 2*sqrt(2), -3 - 2*sqrt(2), -3 - 2*sqrt(2), -3 + 2*sqrt(2), -1, 2, -1, -2, 0],
  594. [1 - sqrt(2), -sqrt(2) - 1, 1 + sqrt(2), -1 + sqrt(2), -1, 1, 1, 1, 1]
  595. ])
  596. with dotprodsimp(True):
  597. assert M.rref() == (Matrix([
  598. [1, 0, 0, 0, 0, 0, 0, 0, S(1)/2],
  599. [0, 1, 0, 0, 0, 0, 0, 0, -S(1)/2],
  600. [0, 0, 1, 0, 0, 0, 0, 0, S(1)/2],
  601. [0, 0, 0, 1, 0, 0, 0, 0, -S(1)/2],
  602. [0, 0, 0, 0, 1, 0, 0, 0, 0],
  603. [0, 0, 0, 0, 0, 1, 0, 0, -S(1)/2],
  604. [0, 0, 0, 0, 0, 0, 1, 0, 0],
  605. [0, 0, 0, 0, 0, 0, 0, 1, -S(1)/2]]), (0, 1, 2, 3, 4, 5, 6, 7))
  606. def test_creation():
  607. raises(ValueError, lambda: Matrix(5, 5, range(20)))
  608. raises(ValueError, lambda: Matrix(5, -1, []))
  609. raises(IndexError, lambda: Matrix((1, 2))[2])
  610. with raises(IndexError):
  611. Matrix((1, 2))[3] = 5
  612. assert Matrix() == Matrix([]) == Matrix([[]]) == Matrix(0, 0, [])
  613. # anything used to be allowed in a matrix
  614. with warns_deprecated_sympy():
  615. assert Matrix([[[1], (2,)]]).tolist() == [[[1], (2,)]]
  616. with warns_deprecated_sympy():
  617. assert Matrix([[[1], (2,)]]).T.tolist() == [[[1]], [(2,)]]
  618. M = Matrix([[0]])
  619. with warns_deprecated_sympy():
  620. M[0, 0] = S.EmptySet
  621. a = Matrix([[x, 0], [0, 0]])
  622. m = a
  623. assert m.cols == m.rows
  624. assert m.cols == 2
  625. assert m[:] == [x, 0, 0, 0]
  626. b = Matrix(2, 2, [x, 0, 0, 0])
  627. m = b
  628. assert m.cols == m.rows
  629. assert m.cols == 2
  630. assert m[:] == [x, 0, 0, 0]
  631. assert a == b
  632. assert Matrix(b) == b
  633. c23 = Matrix(2, 3, range(1, 7))
  634. c13 = Matrix(1, 3, range(7, 10))
  635. c = Matrix([c23, c13])
  636. assert c.cols == 3
  637. assert c.rows == 3
  638. assert c[:] == [1, 2, 3, 4, 5, 6, 7, 8, 9]
  639. assert Matrix(eye(2)) == eye(2)
  640. assert ImmutableMatrix(ImmutableMatrix(eye(2))) == ImmutableMatrix(eye(2))
  641. assert ImmutableMatrix(c) == c.as_immutable()
  642. assert Matrix(ImmutableMatrix(c)) == ImmutableMatrix(c).as_mutable()
  643. assert c is not Matrix(c)
  644. dat = [[ones(3,2), ones(3,3)*2], [ones(2,3)*3, ones(2,2)*4]]
  645. M = Matrix(dat)
  646. assert M == Matrix([
  647. [1, 1, 2, 2, 2],
  648. [1, 1, 2, 2, 2],
  649. [1, 1, 2, 2, 2],
  650. [3, 3, 3, 4, 4],
  651. [3, 3, 3, 4, 4]])
  652. assert M.tolist() != dat
  653. # keep block form if evaluate=False
  654. assert Matrix(dat, evaluate=False).tolist() == dat
  655. A = MatrixSymbol("A", 2, 2)
  656. dat = [ones(2), A]
  657. assert Matrix(dat) == Matrix([
  658. [ 1, 1],
  659. [ 1, 1],
  660. [A[0, 0], A[0, 1]],
  661. [A[1, 0], A[1, 1]]])
  662. with warns_deprecated_sympy():
  663. assert Matrix(dat, evaluate=False).tolist() == [[i] for i in dat]
  664. # 0-dim tolerance
  665. assert Matrix([ones(2), ones(0)]) == Matrix([ones(2)])
  666. raises(ValueError, lambda: Matrix([ones(2), ones(0, 3)]))
  667. raises(ValueError, lambda: Matrix([ones(2), ones(3, 0)]))
  668. # mix of Matrix and iterable
  669. M = Matrix([[1, 2], [3, 4]])
  670. M2 = Matrix([M, (5, 6)])
  671. assert M2 == Matrix([[1, 2], [3, 4], [5, 6]])
  672. def test_irregular_block():
  673. assert Matrix.irregular(3, ones(2,1), ones(3,3)*2, ones(2,2)*3,
  674. ones(1,1)*4, ones(2,2)*5, ones(1,2)*6, ones(1,2)*7) == Matrix([
  675. [1, 2, 2, 2, 3, 3],
  676. [1, 2, 2, 2, 3, 3],
  677. [4, 2, 2, 2, 5, 5],
  678. [6, 6, 7, 7, 5, 5]])
  679. def test_tolist():
  680. lst = [[S.One, S.Half, x*y, S.Zero], [x, y, z, x**2], [y, -S.One, z*x, 3]]
  681. m = Matrix(lst)
  682. assert m.tolist() == lst
  683. def test_as_mutable():
  684. assert zeros(0, 3).as_mutable() == zeros(0, 3)
  685. assert zeros(0, 3).as_immutable() == ImmutableMatrix(zeros(0, 3))
  686. assert zeros(3, 0).as_immutable() == ImmutableMatrix(zeros(3, 0))
  687. def test_slicing():
  688. m0 = eye(4)
  689. assert m0[:3, :3] == eye(3)
  690. assert m0[2:4, 0:2] == zeros(2)
  691. m1 = Matrix(3, 3, lambda i, j: i + j)
  692. assert m1[0, :] == Matrix(1, 3, (0, 1, 2))
  693. assert m1[1:3, 1] == Matrix(2, 1, (2, 3))
  694. m2 = Matrix([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]])
  695. assert m2[:, -1] == Matrix(4, 1, [3, 7, 11, 15])
  696. assert m2[-2:, :] == Matrix([[8, 9, 10, 11], [12, 13, 14, 15]])
  697. def test_submatrix_assignment():
  698. m = zeros(4)
  699. m[2:4, 2:4] = eye(2)
  700. assert m == Matrix(((0, 0, 0, 0),
  701. (0, 0, 0, 0),
  702. (0, 0, 1, 0),
  703. (0, 0, 0, 1)))
  704. m[:2, :2] = eye(2)
  705. assert m == eye(4)
  706. m[:, 0] = Matrix(4, 1, (1, 2, 3, 4))
  707. assert m == Matrix(((1, 0, 0, 0),
  708. (2, 1, 0, 0),
  709. (3, 0, 1, 0),
  710. (4, 0, 0, 1)))
  711. m[:, :] = zeros(4)
  712. assert m == zeros(4)
  713. m[:, :] = [(1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12), (13, 14, 15, 16)]
  714. assert m == Matrix(((1, 2, 3, 4),
  715. (5, 6, 7, 8),
  716. (9, 10, 11, 12),
  717. (13, 14, 15, 16)))
  718. m[:2, 0] = [0, 0]
  719. assert m == Matrix(((0, 2, 3, 4),
  720. (0, 6, 7, 8),
  721. (9, 10, 11, 12),
  722. (13, 14, 15, 16)))
  723. def test_extract():
  724. m = Matrix(4, 3, lambda i, j: i*3 + j)
  725. assert m.extract([0, 1, 3], [0, 1]) == Matrix(3, 2, [0, 1, 3, 4, 9, 10])
  726. assert m.extract([0, 3], [0, 0, 2]) == Matrix(2, 3, [0, 0, 2, 9, 9, 11])
  727. assert m.extract(range(4), range(3)) == m
  728. raises(IndexError, lambda: m.extract([4], [0]))
  729. raises(IndexError, lambda: m.extract([0], [3]))
  730. def test_reshape():
  731. m0 = eye(3)
  732. assert m0.reshape(1, 9) == Matrix(1, 9, (1, 0, 0, 0, 1, 0, 0, 0, 1))
  733. m1 = Matrix(3, 4, lambda i, j: i + j)
  734. assert m1.reshape(
  735. 4, 3) == Matrix(((0, 1, 2), (3, 1, 2), (3, 4, 2), (3, 4, 5)))
  736. assert m1.reshape(2, 6) == Matrix(((0, 1, 2, 3, 1, 2), (3, 4, 2, 3, 4, 5)))
  737. def test_applyfunc():
  738. m0 = eye(3)
  739. assert m0.applyfunc(lambda x: 2*x) == eye(3)*2
  740. assert m0.applyfunc(lambda x: 0) == zeros(3)
  741. def test_expand():
  742. m0 = Matrix([[x*(x + y), 2], [((x + y)*y)*x, x*(y + x*(x + y))]])
  743. # Test if expand() returns a matrix
  744. m1 = m0.expand()
  745. assert m1 == Matrix(
  746. [[x*y + x**2, 2], [x*y**2 + y*x**2, x*y + y*x**2 + x**3]])
  747. a = Symbol('a', real=True)
  748. assert Matrix([exp(I*a)]).expand(complex=True) == \
  749. Matrix([cos(a) + I*sin(a)])
  750. assert Matrix([[0, 1, 2], [0, 0, -1], [0, 0, 0]]).exp() == Matrix([
  751. [1, 1, Rational(3, 2)],
  752. [0, 1, -1],
  753. [0, 0, 1]]
  754. )
  755. def test_refine():
  756. m0 = Matrix([[Abs(x)**2, sqrt(x**2)],
  757. [sqrt(x**2)*Abs(y)**2, sqrt(y**2)*Abs(x)**2]])
  758. m1 = m0.refine(Q.real(x) & Q.real(y))
  759. assert m1 == Matrix([[x**2, Abs(x)], [y**2*Abs(x), x**2*Abs(y)]])
  760. m1 = m0.refine(Q.positive(x) & Q.positive(y))
  761. assert m1 == Matrix([[x**2, x], [x*y**2, x**2*y]])
  762. m1 = m0.refine(Q.negative(x) & Q.negative(y))
  763. assert m1 == Matrix([[x**2, -x], [-x*y**2, -x**2*y]])
  764. def test_random():
  765. M = randMatrix(3, 3)
  766. M = randMatrix(3, 3, seed=3)
  767. assert M == randMatrix(3, 3, seed=3)
  768. M = randMatrix(3, 4, 0, 150)
  769. M = randMatrix(3, seed=4, symmetric=True)
  770. assert M == randMatrix(3, seed=4, symmetric=True)
  771. S = M.copy()
  772. S.simplify()
  773. assert S == M # doesn't fail when elements are Numbers, not int
  774. rng = random.Random(4)
  775. assert M == randMatrix(3, symmetric=True, prng=rng)
  776. # Ensure symmetry
  777. for size in (10, 11): # Test odd and even
  778. for percent in (100, 70, 30):
  779. M = randMatrix(size, symmetric=True, percent=percent, prng=rng)
  780. assert M == M.T
  781. M = randMatrix(10, min=1, percent=70)
  782. zero_count = 0
  783. for i in range(M.shape[0]):
  784. for j in range(M.shape[1]):
  785. if M[i, j] == 0:
  786. zero_count += 1
  787. assert zero_count == 30
  788. def test_inverse():
  789. A = eye(4)
  790. assert A.inv() == eye(4)
  791. assert A.inv(method="LU") == eye(4)
  792. assert A.inv(method="ADJ") == eye(4)
  793. assert A.inv(method="CH") == eye(4)
  794. assert A.inv(method="LDL") == eye(4)
  795. assert A.inv(method="QR") == eye(4)
  796. A = Matrix([[2, 3, 5],
  797. [3, 6, 2],
  798. [8, 3, 6]])
  799. Ainv = A.inv()
  800. assert A*Ainv == eye(3)
  801. assert A.inv(method="LU") == Ainv
  802. assert A.inv(method="ADJ") == Ainv
  803. assert A.inv(method="CH") == Ainv
  804. assert A.inv(method="LDL") == Ainv
  805. assert A.inv(method="QR") == Ainv
  806. AA = Matrix([[0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0],
  807. [1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0],
  808. [1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1],
  809. [1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0],
  810. [1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0],
  811. [1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1],
  812. [0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0],
  813. [1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1],
  814. [0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1],
  815. [1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0],
  816. [0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0],
  817. [1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0],
  818. [0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1],
  819. [1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0],
  820. [0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0],
  821. [1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0],
  822. [0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1],
  823. [0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1],
  824. [1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1],
  825. [0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  826. [1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1],
  827. [0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1],
  828. [0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0],
  829. [0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0],
  830. [0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0]])
  831. assert AA.inv(method="BLOCK") * AA == eye(AA.shape[0])
  832. # test that immutability is not a problem
  833. cls = ImmutableMatrix
  834. m = cls([[48, 49, 31],
  835. [ 9, 71, 94],
  836. [59, 28, 65]])
  837. assert all(type(m.inv(s)) is cls for s in 'GE ADJ LU CH LDL QR'.split())
  838. cls = ImmutableSparseMatrix
  839. m = cls([[48, 49, 31],
  840. [ 9, 71, 94],
  841. [59, 28, 65]])
  842. assert all(type(m.inv(s)) is cls for s in 'GE ADJ LU CH LDL QR'.split())
  843. def test_matrix_inverse_mod():
  844. A = Matrix(2, 1, [1, 0])
  845. raises(NonSquareMatrixError, lambda: A.inv_mod(2))
  846. A = Matrix(2, 2, [1, 0, 0, 0])
  847. raises(ValueError, lambda: A.inv_mod(2))
  848. A = Matrix(2, 2, [1, 2, 3, 4])
  849. Ai = Matrix(2, 2, [1, 1, 0, 1])
  850. assert A.inv_mod(3) == Ai
  851. A = Matrix(2, 2, [1, 0, 0, 1])
  852. assert A.inv_mod(2) == A
  853. A = Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
  854. raises(ValueError, lambda: A.inv_mod(5))
  855. A = Matrix(3, 3, [5, 1, 3, 2, 6, 0, 2, 1, 1])
  856. Ai = Matrix(3, 3, [6, 8, 0, 1, 5, 6, 5, 6, 4])
  857. assert A.inv_mod(9) == Ai
  858. A = Matrix(3, 3, [1, 6, -3, 4, 1, -5, 3, -5, 5])
  859. Ai = Matrix(3, 3, [4, 3, 3, 1, 2, 5, 1, 5, 1])
  860. assert A.inv_mod(6) == Ai
  861. A = Matrix(3, 3, [1, 6, 1, 4, 1, 5, 3, 2, 5])
  862. Ai = Matrix(3, 3, [6, 0, 3, 6, 6, 4, 1, 6, 1])
  863. assert A.inv_mod(7) == Ai
  864. def test_jacobian_hessian():
  865. L = Matrix(1, 2, [x**2*y, 2*y**2 + x*y])
  866. syms = [x, y]
  867. assert L.jacobian(syms) == Matrix([[2*x*y, x**2], [y, 4*y + x]])
  868. L = Matrix(1, 2, [x, x**2*y**3])
  869. assert L.jacobian(syms) == Matrix([[1, 0], [2*x*y**3, x**2*3*y**2]])
  870. f = x**2*y
  871. syms = [x, y]
  872. assert hessian(f, syms) == Matrix([[2*y, 2*x], [2*x, 0]])
  873. f = x**2*y**3
  874. assert hessian(f, syms) == \
  875. Matrix([[2*y**3, 6*x*y**2], [6*x*y**2, 6*x**2*y]])
  876. f = z + x*y**2
  877. g = x**2 + 2*y**3
  878. ans = Matrix([[0, 2*y],
  879. [2*y, 2*x]])
  880. assert ans == hessian(f, Matrix([x, y]))
  881. assert ans == hessian(f, Matrix([x, y]).T)
  882. assert hessian(f, (y, x), [g]) == Matrix([
  883. [ 0, 6*y**2, 2*x],
  884. [6*y**2, 2*x, 2*y],
  885. [ 2*x, 2*y, 0]])
  886. def test_wronskian():
  887. assert wronskian([cos(x), sin(x)], x) == cos(x)**2 + sin(x)**2
  888. assert wronskian([exp(x), exp(2*x)], x) == exp(3*x)
  889. assert wronskian([exp(x), x], x) == exp(x) - x*exp(x)
  890. assert wronskian([1, x, x**2], x) == 2
  891. w1 = -6*exp(x)*sin(x)*x + 6*cos(x)*exp(x)*x**2 - 6*exp(x)*cos(x)*x - \
  892. exp(x)*cos(x)*x**3 + exp(x)*sin(x)*x**3
  893. assert wronskian([exp(x), cos(x), x**3], x).expand() == w1
  894. assert wronskian([exp(x), cos(x), x**3], x, method='berkowitz').expand() \
  895. == w1
  896. w2 = -x**3*cos(x)**2 - x**3*sin(x)**2 - 6*x*cos(x)**2 - 6*x*sin(x)**2
  897. assert wronskian([sin(x), cos(x), x**3], x).expand() == w2
  898. assert wronskian([sin(x), cos(x), x**3], x, method='berkowitz').expand() \
  899. == w2
  900. assert wronskian([], x) == 1
  901. def test_subs():
  902. assert Matrix([[1, x], [x, 4]]).subs(x, 5) == Matrix([[1, 5], [5, 4]])
  903. assert Matrix([[x, 2], [x + y, 4]]).subs([[x, -1], [y, -2]]) == \
  904. Matrix([[-1, 2], [-3, 4]])
  905. assert Matrix([[x, 2], [x + y, 4]]).subs([(x, -1), (y, -2)]) == \
  906. Matrix([[-1, 2], [-3, 4]])
  907. assert Matrix([[x, 2], [x + y, 4]]).subs({x: -1, y: -2}) == \
  908. Matrix([[-1, 2], [-3, 4]])
  909. assert Matrix([x*y]).subs({x: y - 1, y: x - 1}, simultaneous=True) == \
  910. Matrix([(x - 1)*(y - 1)])
  911. for cls in classes:
  912. assert Matrix([[2, 0], [0, 2]]) == cls.eye(2).subs(1, 2)
  913. def test_xreplace():
  914. assert Matrix([[1, x], [x, 4]]).xreplace({x: 5}) == \
  915. Matrix([[1, 5], [5, 4]])
  916. assert Matrix([[x, 2], [x + y, 4]]).xreplace({x: -1, y: -2}) == \
  917. Matrix([[-1, 2], [-3, 4]])
  918. for cls in classes:
  919. assert Matrix([[2, 0], [0, 2]]) == cls.eye(2).xreplace({1: 2})
  920. def test_simplify():
  921. n = Symbol('n')
  922. f = Function('f')
  923. M = Matrix([[ 1/x + 1/y, (x + x*y) / x ],
  924. [ (f(x) + y*f(x))/f(x), 2 * (1/n - cos(n * pi)/n) / pi ]])
  925. M.simplify()
  926. assert M == Matrix([[ (x + y)/(x * y), 1 + y ],
  927. [ 1 + y, 2*((1 - 1*cos(pi*n))/(pi*n)) ]])
  928. eq = (1 + x)**2
  929. M = Matrix([[eq]])
  930. M.simplify()
  931. assert M == Matrix([[eq]])
  932. M.simplify(ratio=oo)
  933. assert M == Matrix([[eq.simplify(ratio=oo)]])
  934. def test_transpose():
  935. M = Matrix([[1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
  936. [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]])
  937. assert M.T == Matrix( [ [1, 1],
  938. [2, 2],
  939. [3, 3],
  940. [4, 4],
  941. [5, 5],
  942. [6, 6],
  943. [7, 7],
  944. [8, 8],
  945. [9, 9],
  946. [0, 0] ])
  947. assert M.T.T == M
  948. assert M.T == M.transpose()
  949. def test_conjugate():
  950. M = Matrix([[0, I, 5],
  951. [1, 2, 0]])
  952. assert M.T == Matrix([[0, 1],
  953. [I, 2],
  954. [5, 0]])
  955. assert M.C == Matrix([[0, -I, 5],
  956. [1, 2, 0]])
  957. assert M.C == M.conjugate()
  958. assert M.H == M.T.C
  959. assert M.H == Matrix([[ 0, 1],
  960. [-I, 2],
  961. [ 5, 0]])
  962. def test_conj_dirac():
  963. raises(AttributeError, lambda: eye(3).D)
  964. M = Matrix([[1, I, I, I],
  965. [0, 1, I, I],
  966. [0, 0, 1, I],
  967. [0, 0, 0, 1]])
  968. assert M.D == Matrix([[ 1, 0, 0, 0],
  969. [-I, 1, 0, 0],
  970. [-I, -I, -1, 0],
  971. [-I, -I, I, -1]])
  972. def test_trace():
  973. M = Matrix([[1, 0, 0],
  974. [0, 5, 0],
  975. [0, 0, 8]])
  976. assert M.trace() == 14
  977. def test_shape():
  978. M = Matrix([[x, 0, 0],
  979. [0, y, 0]])
  980. assert M.shape == (2, 3)
  981. def test_col_row_op():
  982. M = Matrix([[x, 0, 0],
  983. [0, y, 0]])
  984. M.row_op(1, lambda r, j: r + j + 1)
  985. assert M == Matrix([[x, 0, 0],
  986. [1, y + 2, 3]])
  987. M.col_op(0, lambda c, j: c + y**j)
  988. assert M == Matrix([[x + 1, 0, 0],
  989. [1 + y, y + 2, 3]])
  990. # neither row nor slice give copies that allow the original matrix to
  991. # be changed
  992. assert M.row(0) == Matrix([[x + 1, 0, 0]])
  993. r1 = M.row(0)
  994. r1[0] = 42
  995. assert M[0, 0] == x + 1
  996. r1 = M[0, :-1] # also testing negative slice
  997. r1[0] = 42
  998. assert M[0, 0] == x + 1
  999. c1 = M.col(0)
  1000. assert c1 == Matrix([x + 1, 1 + y])
  1001. c1[0] = 0
  1002. assert M[0, 0] == x + 1
  1003. c1 = M[:, 0]
  1004. c1[0] = 42
  1005. assert M[0, 0] == x + 1
  1006. def test_zip_row_op():
  1007. for cls in classes[:2]: # XXX: immutable matrices don't support row ops
  1008. M = cls.eye(3)
  1009. M.zip_row_op(1, 0, lambda v, u: v + 2*u)
  1010. assert M == cls([[1, 0, 0],
  1011. [2, 1, 0],
  1012. [0, 0, 1]])
  1013. M = cls.eye(3)*2
  1014. M[0, 1] = -1
  1015. M.zip_row_op(1, 0, lambda v, u: v + 2*u); M
  1016. assert M == cls([[2, -1, 0],
  1017. [4, 0, 0],
  1018. [0, 0, 2]])
  1019. def test_issue_3950():
  1020. m = Matrix([1, 2, 3])
  1021. a = Matrix([1, 2, 3])
  1022. b = Matrix([2, 2, 3])
  1023. assert not (m in [])
  1024. assert not (m in [1])
  1025. assert m != 1
  1026. assert m == a
  1027. assert m != b
  1028. def test_issue_3981():
  1029. class Index1:
  1030. def __index__(self):
  1031. return 1
  1032. class Index2:
  1033. def __index__(self):
  1034. return 2
  1035. index1 = Index1()
  1036. index2 = Index2()
  1037. m = Matrix([1, 2, 3])
  1038. assert m[index2] == 3
  1039. m[index2] = 5
  1040. assert m[2] == 5
  1041. m = Matrix([[1, 2, 3], [4, 5, 6]])
  1042. assert m[index1, index2] == 6
  1043. assert m[1, index2] == 6
  1044. assert m[index1, 2] == 6
  1045. m[index1, index2] = 4
  1046. assert m[1, 2] == 4
  1047. m[1, index2] = 6
  1048. assert m[1, 2] == 6
  1049. m[index1, 2] = 8
  1050. assert m[1, 2] == 8
  1051. def test_evalf():
  1052. a = Matrix([sqrt(5), 6])
  1053. assert all(a.evalf()[i] == a[i].evalf() for i in range(2))
  1054. assert all(a.evalf(2)[i] == a[i].evalf(2) for i in range(2))
  1055. assert all(a.n(2)[i] == a[i].n(2) for i in range(2))
  1056. def test_is_symbolic():
  1057. a = Matrix([[x, x], [x, x]])
  1058. assert a.is_symbolic() is True
  1059. a = Matrix([[1, 2, 3, 4], [5, 6, 7, 8]])
  1060. assert a.is_symbolic() is False
  1061. a = Matrix([[1, 2, 3, 4], [5, 6, x, 8]])
  1062. assert a.is_symbolic() is True
  1063. a = Matrix([[1, x, 3]])
  1064. assert a.is_symbolic() is True
  1065. a = Matrix([[1, 2, 3]])
  1066. assert a.is_symbolic() is False
  1067. a = Matrix([[1], [x], [3]])
  1068. assert a.is_symbolic() is True
  1069. a = Matrix([[1], [2], [3]])
  1070. assert a.is_symbolic() is False
  1071. def test_is_upper():
  1072. a = Matrix([[1, 2, 3]])
  1073. assert a.is_upper is True
  1074. a = Matrix([[1], [2], [3]])
  1075. assert a.is_upper is False
  1076. a = zeros(4, 2)
  1077. assert a.is_upper is True
  1078. def test_is_lower():
  1079. a = Matrix([[1, 2, 3]])
  1080. assert a.is_lower is False
  1081. a = Matrix([[1], [2], [3]])
  1082. assert a.is_lower is True
  1083. def test_is_nilpotent():
  1084. a = Matrix(4, 4, [0, 2, 1, 6, 0, 0, 1, 2, 0, 0, 0, 3, 0, 0, 0, 0])
  1085. assert a.is_nilpotent()
  1086. a = Matrix([[1, 0], [0, 1]])
  1087. assert not a.is_nilpotent()
  1088. a = Matrix([])
  1089. assert a.is_nilpotent()
  1090. def test_zeros_ones_fill():
  1091. n, m = 3, 5
  1092. a = zeros(n, m)
  1093. a.fill( 5 )
  1094. b = 5 * ones(n, m)
  1095. assert a == b
  1096. assert a.rows == b.rows == 3
  1097. assert a.cols == b.cols == 5
  1098. assert a.shape == b.shape == (3, 5)
  1099. assert zeros(2) == zeros(2, 2)
  1100. assert ones(2) == ones(2, 2)
  1101. assert zeros(2, 3) == Matrix(2, 3, [0]*6)
  1102. assert ones(2, 3) == Matrix(2, 3, [1]*6)
  1103. a.fill(0)
  1104. assert a == zeros(n, m)
  1105. def test_empty_zeros():
  1106. a = zeros(0)
  1107. assert a == Matrix()
  1108. a = zeros(0, 2)
  1109. assert a.rows == 0
  1110. assert a.cols == 2
  1111. a = zeros(2, 0)
  1112. assert a.rows == 2
  1113. assert a.cols == 0
  1114. def test_issue_3749():
  1115. a = Matrix([[x**2, x*y], [x*sin(y), x*cos(y)]])
  1116. assert a.diff(x) == Matrix([[2*x, y], [sin(y), cos(y)]])
  1117. assert Matrix([
  1118. [x, -x, x**2],
  1119. [exp(x), 1/x - exp(-x), x + 1/x]]).limit(x, oo) == \
  1120. Matrix([[oo, -oo, oo], [oo, 0, oo]])
  1121. assert Matrix([
  1122. [(exp(x) - 1)/x, 2*x + y*x, x**x ],
  1123. [1/x, abs(x), abs(sin(x + 1))]]).limit(x, 0) == \
  1124. Matrix([[1, 0, 1], [oo, 0, sin(1)]])
  1125. assert a.integrate(x) == Matrix([
  1126. [Rational(1, 3)*x**3, y*x**2/2],
  1127. [x**2*sin(y)/2, x**2*cos(y)/2]])
  1128. def test_inv_iszerofunc():
  1129. A = eye(4)
  1130. A.col_swap(0, 1)
  1131. for method in "GE", "LU":
  1132. assert A.inv(method=method, iszerofunc=lambda x: x == 0) == \
  1133. A.inv(method="ADJ")
  1134. def test_jacobian_metrics():
  1135. rho, phi = symbols("rho,phi")
  1136. X = Matrix([rho*cos(phi), rho*sin(phi)])
  1137. Y = Matrix([rho, phi])
  1138. J = X.jacobian(Y)
  1139. assert J == X.jacobian(Y.T)
  1140. assert J == (X.T).jacobian(Y)
  1141. assert J == (X.T).jacobian(Y.T)
  1142. g = J.T*eye(J.shape[0])*J
  1143. g = g.applyfunc(trigsimp)
  1144. assert g == Matrix([[1, 0], [0, rho**2]])
  1145. def test_jacobian2():
  1146. rho, phi = symbols("rho,phi")
  1147. X = Matrix([rho*cos(phi), rho*sin(phi), rho**2])
  1148. Y = Matrix([rho, phi])
  1149. J = Matrix([
  1150. [cos(phi), -rho*sin(phi)],
  1151. [sin(phi), rho*cos(phi)],
  1152. [ 2*rho, 0],
  1153. ])
  1154. assert X.jacobian(Y) == J
  1155. def test_issue_4564():
  1156. X = Matrix([exp(x + y + z), exp(x + y + z), exp(x + y + z)])
  1157. Y = Matrix([x, y, z])
  1158. for i in range(1, 3):
  1159. for j in range(1, 3):
  1160. X_slice = X[:i, :]
  1161. Y_slice = Y[:j, :]
  1162. J = X_slice.jacobian(Y_slice)
  1163. assert J.rows == i
  1164. assert J.cols == j
  1165. for k in range(j):
  1166. assert J[:, k] == X_slice
  1167. def test_nonvectorJacobian():
  1168. X = Matrix([[exp(x + y + z), exp(x + y + z)],
  1169. [exp(x + y + z), exp(x + y + z)]])
  1170. raises(TypeError, lambda: X.jacobian(Matrix([x, y, z])))
  1171. X = X[0, :]
  1172. Y = Matrix([[x, y], [x, z]])
  1173. raises(TypeError, lambda: X.jacobian(Y))
  1174. raises(TypeError, lambda: X.jacobian(Matrix([ [x, y], [x, z] ])))
  1175. def test_vec():
  1176. m = Matrix([[1, 3], [2, 4]])
  1177. m_vec = m.vec()
  1178. assert m_vec.cols == 1
  1179. for i in range(4):
  1180. assert m_vec[i] == i + 1
  1181. def test_vech():
  1182. m = Matrix([[1, 2], [2, 3]])
  1183. m_vech = m.vech()
  1184. assert m_vech.cols == 1
  1185. for i in range(3):
  1186. assert m_vech[i] == i + 1
  1187. m_vech = m.vech(diagonal=False)
  1188. assert m_vech[0] == 2
  1189. m = Matrix([[1, x*(x + y)], [y*x + x**2, 1]])
  1190. m_vech = m.vech(diagonal=False)
  1191. assert m_vech[0] == y*x + x**2
  1192. m = Matrix([[1, x*(x + y)], [y*x, 1]])
  1193. m_vech = m.vech(diagonal=False, check_symmetry=False)
  1194. assert m_vech[0] == y*x
  1195. raises(ShapeError, lambda: Matrix([[1, 3]]).vech())
  1196. raises(ValueError, lambda: Matrix([[1, 3], [2, 4]]).vech())
  1197. raises(ShapeError, lambda: Matrix([[1, 3]]).vech())
  1198. raises(ValueError, lambda: Matrix([[1, 3], [2, 4]]).vech())
  1199. def test_diag():
  1200. # mostly tested in testcommonmatrix.py
  1201. assert diag([1, 2, 3]) == Matrix([1, 2, 3])
  1202. m = [1, 2, [3]]
  1203. raises(ValueError, lambda: diag(m))
  1204. assert diag(m, strict=False) == Matrix([1, 2, 3])
  1205. def test_get_diag_blocks1():
  1206. a = Matrix([[1, 2], [2, 3]])
  1207. b = Matrix([[3, x], [y, 3]])
  1208. c = Matrix([[3, x, 3], [y, 3, z], [x, y, z]])
  1209. assert a.get_diag_blocks() == [a]
  1210. assert b.get_diag_blocks() == [b]
  1211. assert c.get_diag_blocks() == [c]
  1212. def test_get_diag_blocks2():
  1213. a = Matrix([[1, 2], [2, 3]])
  1214. b = Matrix([[3, x], [y, 3]])
  1215. c = Matrix([[3, x, 3], [y, 3, z], [x, y, z]])
  1216. assert diag(a, b, b).get_diag_blocks() == [a, b, b]
  1217. assert diag(a, b, c).get_diag_blocks() == [a, b, c]
  1218. assert diag(a, c, b).get_diag_blocks() == [a, c, b]
  1219. assert diag(c, c, b).get_diag_blocks() == [c, c, b]
  1220. def test_inv_block():
  1221. a = Matrix([[1, 2], [2, 3]])
  1222. b = Matrix([[3, x], [y, 3]])
  1223. c = Matrix([[3, x, 3], [y, 3, z], [x, y, z]])
  1224. A = diag(a, b, b)
  1225. assert A.inv(try_block_diag=True) == diag(a.inv(), b.inv(), b.inv())
  1226. A = diag(a, b, c)
  1227. assert A.inv(try_block_diag=True) == diag(a.inv(), b.inv(), c.inv())
  1228. A = diag(a, c, b)
  1229. assert A.inv(try_block_diag=True) == diag(a.inv(), c.inv(), b.inv())
  1230. A = diag(a, a, b, a, c, a)
  1231. assert A.inv(try_block_diag=True) == diag(
  1232. a.inv(), a.inv(), b.inv(), a.inv(), c.inv(), a.inv())
  1233. assert A.inv(try_block_diag=True, method="ADJ") == diag(
  1234. a.inv(method="ADJ"), a.inv(method="ADJ"), b.inv(method="ADJ"),
  1235. a.inv(method="ADJ"), c.inv(method="ADJ"), a.inv(method="ADJ"))
  1236. def test_creation_args():
  1237. """
  1238. Check that matrix dimensions can be specified using any reasonable type
  1239. (see issue 4614).
  1240. """
  1241. raises(ValueError, lambda: zeros(3, -1))
  1242. raises(TypeError, lambda: zeros(1, 2, 3, 4))
  1243. assert zeros(int(3)) == zeros(3)
  1244. assert zeros(Integer(3)) == zeros(3)
  1245. raises(ValueError, lambda: zeros(3.))
  1246. assert eye(int(3)) == eye(3)
  1247. assert eye(Integer(3)) == eye(3)
  1248. raises(ValueError, lambda: eye(3.))
  1249. assert ones(int(3), Integer(4)) == ones(3, 4)
  1250. raises(TypeError, lambda: Matrix(5))
  1251. raises(TypeError, lambda: Matrix(1, 2))
  1252. raises(ValueError, lambda: Matrix([1, [2]]))
  1253. def test_diagonal_symmetrical():
  1254. m = Matrix(2, 2, [0, 1, 1, 0])
  1255. assert not m.is_diagonal()
  1256. assert m.is_symmetric()
  1257. assert m.is_symmetric(simplify=False)
  1258. m = Matrix(2, 2, [1, 0, 0, 1])
  1259. assert m.is_diagonal()
  1260. m = diag(1, 2, 3)
  1261. assert m.is_diagonal()
  1262. assert m.is_symmetric()
  1263. m = Matrix(3, 3, [1, 0, 0, 0, 2, 0, 0, 0, 3])
  1264. assert m == diag(1, 2, 3)
  1265. m = Matrix(2, 3, zeros(2, 3))
  1266. assert not m.is_symmetric()
  1267. assert m.is_diagonal()
  1268. m = Matrix(((5, 0), (0, 6), (0, 0)))
  1269. assert m.is_diagonal()
  1270. m = Matrix(((5, 0, 0), (0, 6, 0)))
  1271. assert m.is_diagonal()
  1272. m = Matrix(3, 3, [1, x**2 + 2*x + 1, y, (x + 1)**2, 2, 0, y, 0, 3])
  1273. assert m.is_symmetric()
  1274. assert not m.is_symmetric(simplify=False)
  1275. assert m.expand().is_symmetric(simplify=False)
  1276. def test_diagonalization():
  1277. m = Matrix([[1, 2+I], [2-I, 3]])
  1278. assert m.is_diagonalizable()
  1279. m = Matrix(3, 2, [-3, 1, -3, 20, 3, 10])
  1280. assert not m.is_diagonalizable()
  1281. assert not m.is_symmetric()
  1282. raises(NonSquareMatrixError, lambda: m.diagonalize())
  1283. # diagonalizable
  1284. m = diag(1, 2, 3)
  1285. (P, D) = m.diagonalize()
  1286. assert P == eye(3)
  1287. assert D == m
  1288. m = Matrix(2, 2, [0, 1, 1, 0])
  1289. assert m.is_symmetric()
  1290. assert m.is_diagonalizable()
  1291. (P, D) = m.diagonalize()
  1292. assert P.inv() * m * P == D
  1293. m = Matrix(2, 2, [1, 0, 0, 3])
  1294. assert m.is_symmetric()
  1295. assert m.is_diagonalizable()
  1296. (P, D) = m.diagonalize()
  1297. assert P.inv() * m * P == D
  1298. assert P == eye(2)
  1299. assert D == m
  1300. m = Matrix(2, 2, [1, 1, 0, 0])
  1301. assert m.is_diagonalizable()
  1302. (P, D) = m.diagonalize()
  1303. assert P.inv() * m * P == D
  1304. m = Matrix(3, 3, [1, 2, 0, 0, 3, 0, 2, -4, 2])
  1305. assert m.is_diagonalizable()
  1306. (P, D) = m.diagonalize()
  1307. assert P.inv() * m * P == D
  1308. for i in P:
  1309. assert i.as_numer_denom()[1] == 1
  1310. m = Matrix(2, 2, [1, 0, 0, 0])
  1311. assert m.is_diagonal()
  1312. assert m.is_diagonalizable()
  1313. (P, D) = m.diagonalize()
  1314. assert P.inv() * m * P == D
  1315. assert P == Matrix([[0, 1], [1, 0]])
  1316. # diagonalizable, complex only
  1317. m = Matrix(2, 2, [0, 1, -1, 0])
  1318. assert not m.is_diagonalizable(True)
  1319. raises(MatrixError, lambda: m.diagonalize(True))
  1320. assert m.is_diagonalizable()
  1321. (P, D) = m.diagonalize()
  1322. assert P.inv() * m * P == D
  1323. # not diagonalizable
  1324. m = Matrix(2, 2, [0, 1, 0, 0])
  1325. assert not m.is_diagonalizable()
  1326. raises(MatrixError, lambda: m.diagonalize())
  1327. m = Matrix(3, 3, [-3, 1, -3, 20, 3, 10, 2, -2, 4])
  1328. assert not m.is_diagonalizable()
  1329. raises(MatrixError, lambda: m.diagonalize())
  1330. # symbolic
  1331. a, b, c, d = symbols('a b c d')
  1332. m = Matrix(2, 2, [a, c, c, b])
  1333. assert m.is_symmetric()
  1334. assert m.is_diagonalizable()
  1335. def test_issue_15887():
  1336. # Mutable matrix should not use cache
  1337. a = MutableDenseMatrix([[0, 1], [1, 0]])
  1338. assert a.is_diagonalizable() is True
  1339. a[1, 0] = 0
  1340. assert a.is_diagonalizable() is False
  1341. a = MutableDenseMatrix([[0, 1], [1, 0]])
  1342. a.diagonalize()
  1343. a[1, 0] = 0
  1344. raises(MatrixError, lambda: a.diagonalize())
  1345. def test_jordan_form():
  1346. m = Matrix(3, 2, [-3, 1, -3, 20, 3, 10])
  1347. raises(NonSquareMatrixError, lambda: m.jordan_form())
  1348. # diagonalizable
  1349. m = Matrix(3, 3, [7, -12, 6, 10, -19, 10, 12, -24, 13])
  1350. Jmust = Matrix(3, 3, [-1, 0, 0, 0, 1, 0, 0, 0, 1])
  1351. P, J = m.jordan_form()
  1352. assert Jmust == J
  1353. assert Jmust == m.diagonalize()[1]
  1354. # m = Matrix(3, 3, [0, 6, 3, 1, 3, 1, -2, 2, 1])
  1355. # m.jordan_form() # very long
  1356. # m.jordan_form() #
  1357. # diagonalizable, complex only
  1358. # Jordan cells
  1359. # complexity: one of eigenvalues is zero
  1360. m = Matrix(3, 3, [0, 1, 0, -4, 4, 0, -2, 1, 2])
  1361. # The blocks are ordered according to the value of their eigenvalues,
  1362. # in order to make the matrix compatible with .diagonalize()
  1363. Jmust = Matrix(3, 3, [2, 1, 0, 0, 2, 0, 0, 0, 2])
  1364. P, J = m.jordan_form()
  1365. assert Jmust == J
  1366. # complexity: all of eigenvalues are equal
  1367. m = Matrix(3, 3, [2, 6, -15, 1, 1, -5, 1, 2, -6])
  1368. # Jmust = Matrix(3, 3, [-1, 0, 0, 0, -1, 1, 0, 0, -1])
  1369. # same here see 1456ff
  1370. Jmust = Matrix(3, 3, [-1, 1, 0, 0, -1, 0, 0, 0, -1])
  1371. P, J = m.jordan_form()
  1372. assert Jmust == J
  1373. # complexity: two of eigenvalues are zero
  1374. m = Matrix(3, 3, [4, -5, 2, 5, -7, 3, 6, -9, 4])
  1375. Jmust = Matrix(3, 3, [0, 1, 0, 0, 0, 0, 0, 0, 1])
  1376. P, J = m.jordan_form()
  1377. assert Jmust == J
  1378. m = Matrix(4, 4, [6, 5, -2, -3, -3, -1, 3, 3, 2, 1, -2, -3, -1, 1, 5, 5])
  1379. Jmust = Matrix(4, 4, [2, 1, 0, 0,
  1380. 0, 2, 0, 0,
  1381. 0, 0, 2, 1,
  1382. 0, 0, 0, 2]
  1383. )
  1384. P, J = m.jordan_form()
  1385. assert Jmust == J
  1386. m = Matrix(4, 4, [6, 2, -8, -6, -3, 2, 9, 6, 2, -2, -8, -6, -1, 0, 3, 4])
  1387. # Jmust = Matrix(4, 4, [2, 0, 0, 0, 0, 2, 1, 0, 0, 0, 2, 0, 0, 0, 0, -2])
  1388. # same here see 1456ff
  1389. Jmust = Matrix(4, 4, [-2, 0, 0, 0,
  1390. 0, 2, 1, 0,
  1391. 0, 0, 2, 0,
  1392. 0, 0, 0, 2])
  1393. P, J = m.jordan_form()
  1394. assert Jmust == J
  1395. m = Matrix(4, 4, [5, 4, 2, 1, 0, 1, -1, -1, -1, -1, 3, 0, 1, 1, -1, 2])
  1396. assert not m.is_diagonalizable()
  1397. Jmust = Matrix(4, 4, [1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 4, 1, 0, 0, 0, 4])
  1398. P, J = m.jordan_form()
  1399. assert Jmust == J
  1400. # checking for maximum precision to remain unchanged
  1401. m = Matrix([[Float('1.0', precision=110), Float('2.0', precision=110)],
  1402. [Float('3.14159265358979323846264338327', precision=110), Float('4.0', precision=110)]])
  1403. P, J = m.jordan_form()
  1404. for term in J.values():
  1405. if isinstance(term, Float):
  1406. assert term._prec == 110
  1407. def test_jordan_form_complex_issue_9274():
  1408. A = Matrix([[ 2, 4, 1, 0],
  1409. [-4, 2, 0, 1],
  1410. [ 0, 0, 2, 4],
  1411. [ 0, 0, -4, 2]])
  1412. p = 2 - 4*I;
  1413. q = 2 + 4*I;
  1414. Jmust1 = Matrix([[p, 1, 0, 0],
  1415. [0, p, 0, 0],
  1416. [0, 0, q, 1],
  1417. [0, 0, 0, q]])
  1418. Jmust2 = Matrix([[q, 1, 0, 0],
  1419. [0, q, 0, 0],
  1420. [0, 0, p, 1],
  1421. [0, 0, 0, p]])
  1422. P, J = A.jordan_form()
  1423. assert J == Jmust1 or J == Jmust2
  1424. assert simplify(P*J*P.inv()) == A
  1425. def test_issue_10220():
  1426. # two non-orthogonal Jordan blocks with eigenvalue 1
  1427. M = Matrix([[1, 0, 0, 1],
  1428. [0, 1, 1, 0],
  1429. [0, 0, 1, 1],
  1430. [0, 0, 0, 1]])
  1431. P, J = M.jordan_form()
  1432. assert P == Matrix([[0, 1, 0, 1],
  1433. [1, 0, 0, 0],
  1434. [0, 1, 0, 0],
  1435. [0, 0, 1, 0]])
  1436. assert J == Matrix([
  1437. [1, 1, 0, 0],
  1438. [0, 1, 1, 0],
  1439. [0, 0, 1, 0],
  1440. [0, 0, 0, 1]])
  1441. def test_jordan_form_issue_15858():
  1442. A = Matrix([
  1443. [1, 1, 1, 0],
  1444. [-2, -1, 0, -1],
  1445. [0, 0, -1, -1],
  1446. [0, 0, 2, 1]])
  1447. (P, J) = A.jordan_form()
  1448. assert P.expand() == Matrix([
  1449. [ -I, -I/2, I, I/2],
  1450. [-1 + I, 0, -1 - I, 0],
  1451. [ 0, -S(1)/2 - I/2, 0, -S(1)/2 + I/2],
  1452. [ 0, 1, 0, 1]])
  1453. assert J == Matrix([
  1454. [-I, 1, 0, 0],
  1455. [0, -I, 0, 0],
  1456. [0, 0, I, 1],
  1457. [0, 0, 0, I]])
  1458. def test_Matrix_berkowitz_charpoly():
  1459. UA, K_i, K_w = symbols('UA K_i K_w')
  1460. A = Matrix([[-K_i - UA + K_i**2/(K_i + K_w), K_i*K_w/(K_i + K_w)],
  1461. [ K_i*K_w/(K_i + K_w), -K_w + K_w**2/(K_i + K_w)]])
  1462. charpoly = A.charpoly(x)
  1463. assert charpoly == \
  1464. Poly(x**2 + (K_i*UA + K_w*UA + 2*K_i*K_w)/(K_i + K_w)*x +
  1465. K_i*K_w*UA/(K_i + K_w), x, domain='ZZ(K_i,K_w,UA)')
  1466. assert type(charpoly) is PurePoly
  1467. A = Matrix([[1, 3], [2, 0]])
  1468. assert A.charpoly() == A.charpoly(x) == PurePoly(x**2 - x - 6)
  1469. A = Matrix([[1, 2], [x, 0]])
  1470. p = A.charpoly(x)
  1471. assert p.gen != x
  1472. assert p.as_expr().subs(p.gen, x) == x**2 - 3*x
  1473. def test_exp_jordan_block():
  1474. l = Symbol('lamda')
  1475. m = Matrix.jordan_block(1, l)
  1476. assert m._eval_matrix_exp_jblock() == Matrix([[exp(l)]])
  1477. m = Matrix.jordan_block(3, l)
  1478. assert m._eval_matrix_exp_jblock() == \
  1479. Matrix([
  1480. [exp(l), exp(l), exp(l)/2],
  1481. [0, exp(l), exp(l)],
  1482. [0, 0, exp(l)]])
  1483. def test_exp():
  1484. m = Matrix([[3, 4], [0, -2]])
  1485. m_exp = Matrix([[exp(3), -4*exp(-2)/5 + 4*exp(3)/5], [0, exp(-2)]])
  1486. assert m.exp() == m_exp
  1487. assert exp(m) == m_exp
  1488. m = Matrix([[1, 0], [0, 1]])
  1489. assert m.exp() == Matrix([[E, 0], [0, E]])
  1490. assert exp(m) == Matrix([[E, 0], [0, E]])
  1491. m = Matrix([[1, -1], [1, 1]])
  1492. assert m.exp() == Matrix([[E*cos(1), -E*sin(1)], [E*sin(1), E*cos(1)]])
  1493. def test_log():
  1494. l = Symbol('lamda')
  1495. m = Matrix.jordan_block(1, l)
  1496. assert m._eval_matrix_log_jblock() == Matrix([[log(l)]])
  1497. m = Matrix.jordan_block(4, l)
  1498. assert m._eval_matrix_log_jblock() == \
  1499. Matrix(
  1500. [
  1501. [log(l), 1/l, -1/(2*l**2), 1/(3*l**3)],
  1502. [0, log(l), 1/l, -1/(2*l**2)],
  1503. [0, 0, log(l), 1/l],
  1504. [0, 0, 0, log(l)]
  1505. ]
  1506. )
  1507. m = Matrix(
  1508. [[0, 0, 1],
  1509. [0, 0, 0],
  1510. [-1, 0, 0]]
  1511. )
  1512. raises(MatrixError, lambda: m.log())
  1513. def test_has():
  1514. A = Matrix(((x, y), (2, 3)))
  1515. assert A.has(x)
  1516. assert not A.has(z)
  1517. assert A.has(Symbol)
  1518. A = A.subs(x, 2)
  1519. assert not A.has(x)
  1520. def test_find_reasonable_pivot_naive_finds_guaranteed_nonzero1():
  1521. # Test if matrices._find_reasonable_pivot_naive()
  1522. # finds a guaranteed non-zero pivot when the
  1523. # some of the candidate pivots are symbolic expressions.
  1524. # Keyword argument: simpfunc=None indicates that no simplifications
  1525. # should be performed during the search.
  1526. x = Symbol('x')
  1527. column = Matrix(3, 1, [x, cos(x)**2 + sin(x)**2, S.Half])
  1528. pivot_offset, pivot_val, pivot_assumed_nonzero, simplified =\
  1529. _find_reasonable_pivot_naive(column)
  1530. assert pivot_val == S.Half
  1531. def test_find_reasonable_pivot_naive_finds_guaranteed_nonzero2():
  1532. # Test if matrices._find_reasonable_pivot_naive()
  1533. # finds a guaranteed non-zero pivot when the
  1534. # some of the candidate pivots are symbolic expressions.
  1535. # Keyword argument: simpfunc=_simplify indicates that the search
  1536. # should attempt to simplify candidate pivots.
  1537. x = Symbol('x')
  1538. column = Matrix(3, 1,
  1539. [x,
  1540. cos(x)**2+sin(x)**2+x**2,
  1541. cos(x)**2+sin(x)**2])
  1542. pivot_offset, pivot_val, pivot_assumed_nonzero, simplified =\
  1543. _find_reasonable_pivot_naive(column, simpfunc=_simplify)
  1544. assert pivot_val == 1
  1545. def test_find_reasonable_pivot_naive_simplifies():
  1546. # Test if matrices._find_reasonable_pivot_naive()
  1547. # simplifies candidate pivots, and reports
  1548. # their offsets correctly.
  1549. x = Symbol('x')
  1550. column = Matrix(3, 1,
  1551. [x,
  1552. cos(x)**2+sin(x)**2+x,
  1553. cos(x)**2+sin(x)**2])
  1554. pivot_offset, pivot_val, pivot_assumed_nonzero, simplified =\
  1555. _find_reasonable_pivot_naive(column, simpfunc=_simplify)
  1556. assert len(simplified) == 2
  1557. assert simplified[0][0] == 1
  1558. assert simplified[0][1] == 1+x
  1559. assert simplified[1][0] == 2
  1560. assert simplified[1][1] == 1
  1561. def test_errors():
  1562. raises(ValueError, lambda: Matrix([[1, 2], [1]]))
  1563. raises(IndexError, lambda: Matrix([[1, 2]])[1.2, 5])
  1564. raises(IndexError, lambda: Matrix([[1, 2]])[1, 5.2])
  1565. raises(ValueError, lambda: randMatrix(3, c=4, symmetric=True))
  1566. raises(ValueError, lambda: Matrix([1, 2]).reshape(4, 6))
  1567. raises(ShapeError,
  1568. lambda: Matrix([[1, 2], [3, 4]]).copyin_matrix([1, 0], Matrix([1, 2])))
  1569. raises(TypeError, lambda: Matrix([[1, 2], [3, 4]]).copyin_list([0,
  1570. 1], set()))
  1571. raises(NonSquareMatrixError, lambda: Matrix([[1, 2, 3], [2, 3, 0]]).inv())
  1572. raises(ShapeError,
  1573. lambda: Matrix(1, 2, [1, 2]).row_join(Matrix([[1, 2], [3, 4]])))
  1574. raises(
  1575. ShapeError, lambda: Matrix([1, 2]).col_join(Matrix([[1, 2], [3, 4]])))
  1576. raises(ShapeError, lambda: Matrix([1]).row_insert(1, Matrix([[1,
  1577. 2], [3, 4]])))
  1578. raises(ShapeError, lambda: Matrix([1]).col_insert(1, Matrix([[1,
  1579. 2], [3, 4]])))
  1580. raises(NonSquareMatrixError, lambda: Matrix([1, 2]).trace())
  1581. raises(TypeError, lambda: Matrix([1]).applyfunc(1))
  1582. raises(ValueError, lambda: Matrix([[1, 2], [3, 4]]).minor(4, 5))
  1583. raises(ValueError, lambda: Matrix([[1, 2], [3, 4]]).minor_submatrix(4, 5))
  1584. raises(TypeError, lambda: Matrix([1, 2, 3]).cross(1))
  1585. raises(TypeError, lambda: Matrix([1, 2, 3]).dot(1))
  1586. raises(ShapeError, lambda: Matrix([1, 2, 3]).dot(Matrix([1, 2])))
  1587. raises(ShapeError, lambda: Matrix([1, 2]).dot([]))
  1588. raises(TypeError, lambda: Matrix([1, 2]).dot('a'))
  1589. raises(ShapeError, lambda: Matrix([1, 2]).dot([1, 2, 3]))
  1590. raises(NonSquareMatrixError, lambda: Matrix([1, 2, 3]).exp())
  1591. raises(ShapeError, lambda: Matrix([[1, 2], [3, 4]]).normalized())
  1592. raises(ValueError, lambda: Matrix([1, 2]).inv(method='not a method'))
  1593. raises(NonSquareMatrixError, lambda: Matrix([1, 2]).inverse_GE())
  1594. raises(ValueError, lambda: Matrix([[1, 2], [1, 2]]).inverse_GE())
  1595. raises(NonSquareMatrixError, lambda: Matrix([1, 2]).inverse_ADJ())
  1596. raises(ValueError, lambda: Matrix([[1, 2], [1, 2]]).inverse_ADJ())
  1597. raises(NonSquareMatrixError, lambda: Matrix([1, 2]).inverse_LU())
  1598. raises(NonSquareMatrixError, lambda: Matrix([1, 2]).is_nilpotent())
  1599. raises(NonSquareMatrixError, lambda: Matrix([1, 2]).det())
  1600. raises(ValueError,
  1601. lambda: Matrix([[1, 2], [3, 4]]).det(method='Not a real method'))
  1602. raises(ValueError,
  1603. lambda: Matrix([[1, 2, 3, 4], [5, 6, 7, 8],
  1604. [9, 10, 11, 12], [13, 14, 15, 16]]).det(iszerofunc="Not function"))
  1605. raises(ValueError,
  1606. lambda: Matrix([[1, 2, 3, 4], [5, 6, 7, 8],
  1607. [9, 10, 11, 12], [13, 14, 15, 16]]).det(iszerofunc=False))
  1608. raises(ValueError,
  1609. lambda: hessian(Matrix([[1, 2], [3, 4]]), Matrix([[1, 2], [2, 1]])))
  1610. raises(ValueError, lambda: hessian(Matrix([[1, 2], [3, 4]]), []))
  1611. raises(ValueError, lambda: hessian(Symbol('x')**2, 'a'))
  1612. raises(IndexError, lambda: eye(3)[5, 2])
  1613. raises(IndexError, lambda: eye(3)[2, 5])
  1614. M = Matrix(((1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12), (13, 14, 15, 16)))
  1615. raises(ValueError, lambda: M.det('method=LU_decomposition()'))
  1616. V = Matrix([[10, 10, 10]])
  1617. M = Matrix([[1, 2, 3], [2, 3, 4], [3, 4, 5]])
  1618. raises(ValueError, lambda: M.row_insert(4.7, V))
  1619. M = Matrix([[1, 2, 3], [2, 3, 4], [3, 4, 5]])
  1620. raises(ValueError, lambda: M.col_insert(-4.2, V))
  1621. def test_len():
  1622. assert len(Matrix()) == 0
  1623. assert len(Matrix([[1, 2]])) == len(Matrix([[1], [2]])) == 2
  1624. assert len(Matrix(0, 2, lambda i, j: 0)) == \
  1625. len(Matrix(2, 0, lambda i, j: 0)) == 0
  1626. assert len(Matrix([[0, 1, 2], [3, 4, 5]])) == 6
  1627. assert Matrix([1]) == Matrix([[1]])
  1628. assert not Matrix()
  1629. assert Matrix() == Matrix([])
  1630. def test_integrate():
  1631. A = Matrix(((1, 4, x), (y, 2, 4), (10, 5, x**2)))
  1632. assert A.integrate(x) == \
  1633. Matrix(((x, 4*x, x**2/2), (x*y, 2*x, 4*x), (10*x, 5*x, x**3/3)))
  1634. assert A.integrate(y) == \
  1635. Matrix(((y, 4*y, x*y), (y**2/2, 2*y, 4*y), (10*y, 5*y, y*x**2)))
  1636. def test_limit():
  1637. A = Matrix(((1, 4, sin(x)/x), (y, 2, 4), (10, 5, x**2 + 1)))
  1638. assert A.limit(x, 0) == Matrix(((1, 4, 1), (y, 2, 4), (10, 5, 1)))
  1639. def test_diff():
  1640. A = MutableDenseMatrix(((1, 4, x), (y, 2, 4), (10, 5, x**2 + 1)))
  1641. assert isinstance(A.diff(x), type(A))
  1642. assert A.diff(x) == MutableDenseMatrix(((0, 0, 1), (0, 0, 0), (0, 0, 2*x)))
  1643. assert A.diff(y) == MutableDenseMatrix(((0, 0, 0), (1, 0, 0), (0, 0, 0)))
  1644. assert diff(A, x) == MutableDenseMatrix(((0, 0, 1), (0, 0, 0), (0, 0, 2*x)))
  1645. assert diff(A, y) == MutableDenseMatrix(((0, 0, 0), (1, 0, 0), (0, 0, 0)))
  1646. A_imm = A.as_immutable()
  1647. assert isinstance(A_imm.diff(x), type(A_imm))
  1648. assert A_imm.diff(x) == ImmutableDenseMatrix(((0, 0, 1), (0, 0, 0), (0, 0, 2*x)))
  1649. assert A_imm.diff(y) == ImmutableDenseMatrix(((0, 0, 0), (1, 0, 0), (0, 0, 0)))
  1650. assert diff(A_imm, x) == ImmutableDenseMatrix(((0, 0, 1), (0, 0, 0), (0, 0, 2*x)))
  1651. assert diff(A_imm, y) == ImmutableDenseMatrix(((0, 0, 0), (1, 0, 0), (0, 0, 0)))
  1652. def test_diff_by_matrix():
  1653. # Derive matrix by matrix:
  1654. A = MutableDenseMatrix([[x, y], [z, t]])
  1655. assert A.diff(A) == Array([[[[1, 0], [0, 0]], [[0, 1], [0, 0]]], [[[0, 0], [1, 0]], [[0, 0], [0, 1]]]])
  1656. assert diff(A, A) == Array([[[[1, 0], [0, 0]], [[0, 1], [0, 0]]], [[[0, 0], [1, 0]], [[0, 0], [0, 1]]]])
  1657. A_imm = A.as_immutable()
  1658. assert A_imm.diff(A_imm) == Array([[[[1, 0], [0, 0]], [[0, 1], [0, 0]]], [[[0, 0], [1, 0]], [[0, 0], [0, 1]]]])
  1659. assert diff(A_imm, A_imm) == Array([[[[1, 0], [0, 0]], [[0, 1], [0, 0]]], [[[0, 0], [1, 0]], [[0, 0], [0, 1]]]])
  1660. # Derive a constant matrix:
  1661. assert A.diff(a) == MutableDenseMatrix([[0, 0], [0, 0]])
  1662. B = ImmutableDenseMatrix([a, b])
  1663. assert A.diff(B) == Array.zeros(2, 1, 2, 2)
  1664. assert A.diff(A) == Array([[[[1, 0], [0, 0]], [[0, 1], [0, 0]]], [[[0, 0], [1, 0]], [[0, 0], [0, 1]]]])
  1665. # Test diff with tuples:
  1666. dB = B.diff([[a, b]])
  1667. assert dB.shape == (2, 2, 1)
  1668. assert dB == Array([[[1], [0]], [[0], [1]]])
  1669. f = Function("f")
  1670. fxyz = f(x, y, z)
  1671. assert fxyz.diff([[x, y, z]]) == Array([fxyz.diff(x), fxyz.diff(y), fxyz.diff(z)])
  1672. assert fxyz.diff(([x, y, z], 2)) == Array([
  1673. [fxyz.diff(x, 2), fxyz.diff(x, y), fxyz.diff(x, z)],
  1674. [fxyz.diff(x, y), fxyz.diff(y, 2), fxyz.diff(y, z)],
  1675. [fxyz.diff(x, z), fxyz.diff(z, y), fxyz.diff(z, 2)],
  1676. ])
  1677. expr = sin(x)*exp(y)
  1678. assert expr.diff([[x, y]]) == Array([cos(x)*exp(y), sin(x)*exp(y)])
  1679. assert expr.diff(y, ((x, y),)) == Array([cos(x)*exp(y), sin(x)*exp(y)])
  1680. assert expr.diff(x, ((x, y),)) == Array([-sin(x)*exp(y), cos(x)*exp(y)])
  1681. assert expr.diff(((y, x),), [[x, y]]) == Array([[cos(x)*exp(y), -sin(x)*exp(y)], [sin(x)*exp(y), cos(x)*exp(y)]])
  1682. # Test different notations:
  1683. assert fxyz.diff(x).diff(y).diff(x) == fxyz.diff(((x, y, z),), 3)[0, 1, 0]
  1684. assert fxyz.diff(z).diff(y).diff(x) == fxyz.diff(((x, y, z),), 3)[2, 1, 0]
  1685. assert fxyz.diff([[x, y, z]], ((z, y, x),)) == Array([[fxyz.diff(i).diff(j) for i in (x, y, z)] for j in (z, y, x)])
  1686. # Test scalar derived by matrix remains matrix:
  1687. res = x.diff(Matrix([[x, y]]))
  1688. assert isinstance(res, ImmutableDenseMatrix)
  1689. assert res == Matrix([[1, 0]])
  1690. res = (x**3).diff(Matrix([[x, y]]))
  1691. assert isinstance(res, ImmutableDenseMatrix)
  1692. assert res == Matrix([[3*x**2, 0]])
  1693. def test_getattr():
  1694. A = Matrix(((1, 4, x), (y, 2, 4), (10, 5, x**2 + 1)))
  1695. raises(AttributeError, lambda: A.nonexistantattribute)
  1696. assert getattr(A, 'diff')(x) == Matrix(((0, 0, 1), (0, 0, 0), (0, 0, 2*x)))
  1697. def test_hessenberg():
  1698. A = Matrix([[3, 4, 1], [2, 4, 5], [0, 1, 2]])
  1699. assert A.is_upper_hessenberg
  1700. A = A.T
  1701. assert A.is_lower_hessenberg
  1702. A[0, -1] = 1
  1703. assert A.is_lower_hessenberg is False
  1704. A = Matrix([[3, 4, 1], [2, 4, 5], [3, 1, 2]])
  1705. assert not A.is_upper_hessenberg
  1706. A = zeros(5, 2)
  1707. assert A.is_upper_hessenberg
  1708. def test_cholesky():
  1709. raises(NonSquareMatrixError, lambda: Matrix((1, 2)).cholesky())
  1710. raises(ValueError, lambda: Matrix(((1, 2), (3, 4))).cholesky())
  1711. raises(ValueError, lambda: Matrix(((5 + I, 0), (0, 1))).cholesky())
  1712. raises(ValueError, lambda: Matrix(((1, 5), (5, 1))).cholesky())
  1713. raises(ValueError, lambda: Matrix(((1, 2), (3, 4))).cholesky(hermitian=False))
  1714. assert Matrix(((5 + I, 0), (0, 1))).cholesky(hermitian=False) == Matrix([
  1715. [sqrt(5 + I), 0], [0, 1]])
  1716. A = Matrix(((1, 5), (5, 1)))
  1717. L = A.cholesky(hermitian=False)
  1718. assert L == Matrix([[1, 0], [5, 2*sqrt(6)*I]])
  1719. assert L*L.T == A
  1720. A = Matrix(((25, 15, -5), (15, 18, 0), (-5, 0, 11)))
  1721. L = A.cholesky()
  1722. assert L * L.T == A
  1723. assert L.is_lower
  1724. assert L == Matrix([[5, 0, 0], [3, 3, 0], [-1, 1, 3]])
  1725. A = Matrix(((4, -2*I, 2 + 2*I), (2*I, 2, -1 + I), (2 - 2*I, -1 - I, 11)))
  1726. assert A.cholesky().expand() == Matrix(((2, 0, 0), (I, 1, 0), (1 - I, 0, 3)))
  1727. raises(NonSquareMatrixError, lambda: SparseMatrix((1, 2)).cholesky())
  1728. raises(ValueError, lambda: SparseMatrix(((1, 2), (3, 4))).cholesky())
  1729. raises(ValueError, lambda: SparseMatrix(((5 + I, 0), (0, 1))).cholesky())
  1730. raises(ValueError, lambda: SparseMatrix(((1, 5), (5, 1))).cholesky())
  1731. raises(ValueError, lambda: SparseMatrix(((1, 2), (3, 4))).cholesky(hermitian=False))
  1732. assert SparseMatrix(((5 + I, 0), (0, 1))).cholesky(hermitian=False) == Matrix([
  1733. [sqrt(5 + I), 0], [0, 1]])
  1734. A = SparseMatrix(((1, 5), (5, 1)))
  1735. L = A.cholesky(hermitian=False)
  1736. assert L == Matrix([[1, 0], [5, 2*sqrt(6)*I]])
  1737. assert L*L.T == A
  1738. A = SparseMatrix(((25, 15, -5), (15, 18, 0), (-5, 0, 11)))
  1739. L = A.cholesky()
  1740. assert L * L.T == A
  1741. assert L.is_lower
  1742. assert L == Matrix([[5, 0, 0], [3, 3, 0], [-1, 1, 3]])
  1743. A = SparseMatrix(((4, -2*I, 2 + 2*I), (2*I, 2, -1 + I), (2 - 2*I, -1 - I, 11)))
  1744. assert A.cholesky() == Matrix(((2, 0, 0), (I, 1, 0), (1 - I, 0, 3)))
  1745. def test_matrix_norm():
  1746. # Vector Tests
  1747. # Test columns and symbols
  1748. x = Symbol('x', real=True)
  1749. v = Matrix([cos(x), sin(x)])
  1750. assert trigsimp(v.norm(2)) == 1
  1751. assert v.norm(10) == Pow(cos(x)**10 + sin(x)**10, Rational(1, 10))
  1752. # Test Rows
  1753. A = Matrix([[5, Rational(3, 2)]])
  1754. assert A.norm() == Pow(25 + Rational(9, 4), S.Half)
  1755. assert A.norm(oo) == max(A)
  1756. assert A.norm(-oo) == min(A)
  1757. # Matrix Tests
  1758. # Intuitive test
  1759. A = Matrix([[1, 1], [1, 1]])
  1760. assert A.norm(2) == 2
  1761. assert A.norm(-2) == 0
  1762. assert A.norm('frobenius') == 2
  1763. assert eye(10).norm(2) == eye(10).norm(-2) == 1
  1764. assert A.norm(oo) == 2
  1765. # Test with Symbols and more complex entries
  1766. A = Matrix([[3, y, y], [x, S.Half, -pi]])
  1767. assert (A.norm('fro')
  1768. == sqrt(Rational(37, 4) + 2*abs(y)**2 + pi**2 + x**2))
  1769. # Check non-square
  1770. A = Matrix([[1, 2, -3], [4, 5, Rational(13, 2)]])
  1771. assert A.norm(2) == sqrt(Rational(389, 8) + sqrt(78665)/8)
  1772. assert A.norm(-2) is S.Zero
  1773. assert A.norm('frobenius') == sqrt(389)/2
  1774. # Test properties of matrix norms
  1775. # https://en.wikipedia.org/wiki/Matrix_norm#Definition
  1776. # Two matrices
  1777. A = Matrix([[1, 2], [3, 4]])
  1778. B = Matrix([[5, 5], [-2, 2]])
  1779. C = Matrix([[0, -I], [I, 0]])
  1780. D = Matrix([[1, 0], [0, -1]])
  1781. L = [A, B, C, D]
  1782. alpha = Symbol('alpha', real=True)
  1783. for order in ['fro', 2, -2]:
  1784. # Zero Check
  1785. assert zeros(3).norm(order) is S.Zero
  1786. # Check Triangle Inequality for all Pairs of Matrices
  1787. for X in L:
  1788. for Y in L:
  1789. dif = (X.norm(order) + Y.norm(order) -
  1790. (X + Y).norm(order))
  1791. assert (dif >= 0)
  1792. # Scalar multiplication linearity
  1793. for M in [A, B, C, D]:
  1794. dif = simplify((alpha*M).norm(order) -
  1795. abs(alpha) * M.norm(order))
  1796. assert dif == 0
  1797. # Test Properties of Vector Norms
  1798. # https://en.wikipedia.org/wiki/Vector_norm
  1799. # Two column vectors
  1800. a = Matrix([1, 1 - 1*I, -3])
  1801. b = Matrix([S.Half, 1*I, 1])
  1802. c = Matrix([-1, -1, -1])
  1803. d = Matrix([3, 2, I])
  1804. e = Matrix([Integer(1e2), Rational(1, 1e2), 1])
  1805. L = [a, b, c, d, e]
  1806. alpha = Symbol('alpha', real=True)
  1807. for order in [1, 2, -1, -2, S.Infinity, S.NegativeInfinity, pi]:
  1808. # Zero Check
  1809. if order > 0:
  1810. assert Matrix([0, 0, 0]).norm(order) is S.Zero
  1811. # Triangle inequality on all pairs
  1812. if order >= 1: # Triangle InEq holds only for these norms
  1813. for X in L:
  1814. for Y in L:
  1815. dif = (X.norm(order) + Y.norm(order) -
  1816. (X + Y).norm(order))
  1817. assert simplify(dif >= 0) is S.true
  1818. # Linear to scalar multiplication
  1819. if order in [1, 2, -1, -2, S.Infinity, S.NegativeInfinity]:
  1820. for X in L:
  1821. dif = simplify((alpha*X).norm(order) -
  1822. (abs(alpha) * X.norm(order)))
  1823. assert dif == 0
  1824. # ord=1
  1825. M = Matrix(3, 3, [1, 3, 0, -2, -1, 0, 3, 9, 6])
  1826. assert M.norm(1) == 13
  1827. def test_condition_number():
  1828. x = Symbol('x', real=True)
  1829. A = eye(3)
  1830. A[0, 0] = 10
  1831. A[2, 2] = Rational(1, 10)
  1832. assert A.condition_number() == 100
  1833. A[1, 1] = x
  1834. assert A.condition_number() == Max(10, Abs(x)) / Min(Rational(1, 10), Abs(x))
  1835. M = Matrix([[cos(x), sin(x)], [-sin(x), cos(x)]])
  1836. Mc = M.condition_number()
  1837. assert all(Float(1.).epsilon_eq(Mc.subs(x, val).evalf()) for val in
  1838. [Rational(1, 5), S.Half, Rational(1, 10), pi/2, pi, pi*Rational(7, 4) ])
  1839. #issue 10782
  1840. assert Matrix([]).condition_number() == 0
  1841. def test_equality():
  1842. A = Matrix(((1, 2, 3), (4, 5, 6), (7, 8, 9)))
  1843. B = Matrix(((9, 8, 7), (6, 5, 4), (3, 2, 1)))
  1844. assert A == A[:, :]
  1845. assert not A != A[:, :]
  1846. assert not A == B
  1847. assert A != B
  1848. assert A != 10
  1849. assert not A == 10
  1850. # A SparseMatrix can be equal to a Matrix
  1851. C = SparseMatrix(((1, 0, 0), (0, 1, 0), (0, 0, 1)))
  1852. D = Matrix(((1, 0, 0), (0, 1, 0), (0, 0, 1)))
  1853. assert C == D
  1854. assert not C != D
  1855. def test_col_join():
  1856. assert eye(3).col_join(Matrix([[7, 7, 7]])) == \
  1857. Matrix([[1, 0, 0],
  1858. [0, 1, 0],
  1859. [0, 0, 1],
  1860. [7, 7, 7]])
  1861. def test_row_insert():
  1862. r4 = Matrix([[4, 4, 4]])
  1863. for i in range(-4, 5):
  1864. l = [1, 0, 0]
  1865. l.insert(i, 4)
  1866. assert flatten(eye(3).row_insert(i, r4).col(0).tolist()) == l
  1867. def test_col_insert():
  1868. c4 = Matrix([4, 4, 4])
  1869. for i in range(-4, 5):
  1870. l = [0, 0, 0]
  1871. l.insert(i, 4)
  1872. assert flatten(zeros(3).col_insert(i, c4).row(0).tolist()) == l
  1873. def test_normalized():
  1874. assert Matrix([3, 4]).normalized() == \
  1875. Matrix([Rational(3, 5), Rational(4, 5)])
  1876. # Zero vector trivial cases
  1877. assert Matrix([0, 0, 0]).normalized() == Matrix([0, 0, 0])
  1878. # Machine precision error truncation trivial cases
  1879. m = Matrix([0,0,1.e-100])
  1880. assert m.normalized(
  1881. iszerofunc=lambda x: x.evalf(n=10, chop=True).is_zero
  1882. ) == Matrix([0, 0, 0])
  1883. def test_print_nonzero():
  1884. assert capture(lambda: eye(3).print_nonzero()) == \
  1885. '[X ]\n[ X ]\n[ X]\n'
  1886. assert capture(lambda: eye(3).print_nonzero('.')) == \
  1887. '[. ]\n[ . ]\n[ .]\n'
  1888. def test_zeros_eye():
  1889. assert Matrix.eye(3) == eye(3)
  1890. assert Matrix.zeros(3) == zeros(3)
  1891. assert ones(3, 4) == Matrix(3, 4, [1]*12)
  1892. i = Matrix([[1, 0], [0, 1]])
  1893. z = Matrix([[0, 0], [0, 0]])
  1894. for cls in classes:
  1895. m = cls.eye(2)
  1896. assert i == m # but m == i will fail if m is immutable
  1897. assert i == eye(2, cls=cls)
  1898. assert type(m) == cls
  1899. m = cls.zeros(2)
  1900. assert z == m
  1901. assert z == zeros(2, cls=cls)
  1902. assert type(m) == cls
  1903. def test_is_zero():
  1904. assert Matrix().is_zero_matrix
  1905. assert Matrix([[0, 0], [0, 0]]).is_zero_matrix
  1906. assert zeros(3, 4).is_zero_matrix
  1907. assert not eye(3).is_zero_matrix
  1908. assert Matrix([[x, 0], [0, 0]]).is_zero_matrix == None
  1909. assert SparseMatrix([[x, 0], [0, 0]]).is_zero_matrix == None
  1910. assert ImmutableMatrix([[x, 0], [0, 0]]).is_zero_matrix == None
  1911. assert ImmutableSparseMatrix([[x, 0], [0, 0]]).is_zero_matrix == None
  1912. assert Matrix([[x, 1], [0, 0]]).is_zero_matrix == False
  1913. a = Symbol('a', nonzero=True)
  1914. assert Matrix([[a, 0], [0, 0]]).is_zero_matrix == False
  1915. def test_rotation_matrices():
  1916. # This tests the rotation matrices by rotating about an axis and back.
  1917. theta = pi/3
  1918. r3_plus = rot_axis3(theta)
  1919. r3_minus = rot_axis3(-theta)
  1920. r2_plus = rot_axis2(theta)
  1921. r2_minus = rot_axis2(-theta)
  1922. r1_plus = rot_axis1(theta)
  1923. r1_minus = rot_axis1(-theta)
  1924. assert r3_minus*r3_plus*eye(3) == eye(3)
  1925. assert r2_minus*r2_plus*eye(3) == eye(3)
  1926. assert r1_minus*r1_plus*eye(3) == eye(3)
  1927. # Check the correctness of the trace of the rotation matrix
  1928. assert r1_plus.trace() == 1 + 2*cos(theta)
  1929. assert r2_plus.trace() == 1 + 2*cos(theta)
  1930. assert r3_plus.trace() == 1 + 2*cos(theta)
  1931. # Check that a rotation with zero angle doesn't change anything.
  1932. assert rot_axis1(0) == eye(3)
  1933. assert rot_axis2(0) == eye(3)
  1934. assert rot_axis3(0) == eye(3)
  1935. # Check left-hand convention
  1936. # see Issue #24529
  1937. q1 = Quaternion.from_axis_angle([1, 0, 0], pi / 2)
  1938. q2 = Quaternion.from_axis_angle([0, 1, 0], pi / 2)
  1939. q3 = Quaternion.from_axis_angle([0, 0, 1], pi / 2)
  1940. assert rot_axis1(- pi / 2) == q1.to_rotation_matrix()
  1941. assert rot_axis2(- pi / 2) == q2.to_rotation_matrix()
  1942. assert rot_axis3(- pi / 2) == q3.to_rotation_matrix()
  1943. # Check right-hand convention
  1944. assert rot_ccw_axis1(+ pi / 2) == q1.to_rotation_matrix()
  1945. assert rot_ccw_axis2(+ pi / 2) == q2.to_rotation_matrix()
  1946. assert rot_ccw_axis3(+ pi / 2) == q3.to_rotation_matrix()
  1947. def test_DeferredVector():
  1948. assert str(DeferredVector("vector")[4]) == "vector[4]"
  1949. assert sympify(DeferredVector("d")) == DeferredVector("d")
  1950. raises(IndexError, lambda: DeferredVector("d")[-1])
  1951. assert str(DeferredVector("d")) == "d"
  1952. assert repr(DeferredVector("test")) == "DeferredVector('test')"
  1953. def test_DeferredVector_not_iterable():
  1954. assert not iterable(DeferredVector('X'))
  1955. def test_DeferredVector_Matrix():
  1956. raises(TypeError, lambda: Matrix(DeferredVector("V")))
  1957. def test_GramSchmidt():
  1958. R = Rational
  1959. m1 = Matrix(1, 2, [1, 2])
  1960. m2 = Matrix(1, 2, [2, 3])
  1961. assert GramSchmidt([m1, m2]) == \
  1962. [Matrix(1, 2, [1, 2]), Matrix(1, 2, [R(2)/5, R(-1)/5])]
  1963. assert GramSchmidt([m1.T, m2.T]) == \
  1964. [Matrix(2, 1, [1, 2]), Matrix(2, 1, [R(2)/5, R(-1)/5])]
  1965. # from wikipedia
  1966. assert GramSchmidt([Matrix([3, 1]), Matrix([2, 2])], True) == [
  1967. Matrix([3*sqrt(10)/10, sqrt(10)/10]),
  1968. Matrix([-sqrt(10)/10, 3*sqrt(10)/10])]
  1969. # https://github.com/sympy/sympy/issues/9488
  1970. L = FiniteSet(Matrix([1]))
  1971. assert GramSchmidt(L) == [Matrix([[1]])]
  1972. def test_casoratian():
  1973. assert casoratian([1, 2, 3, 4], 1) == 0
  1974. assert casoratian([1, 2, 3, 4], 1, zero=False) == 0
  1975. def test_zero_dimension_multiply():
  1976. assert (Matrix()*zeros(0, 3)).shape == (0, 3)
  1977. assert zeros(3, 0)*zeros(0, 3) == zeros(3, 3)
  1978. assert zeros(0, 3)*zeros(3, 0) == Matrix()
  1979. def test_slice_issue_2884():
  1980. m = Matrix(2, 2, range(4))
  1981. assert m[1, :] == Matrix([[2, 3]])
  1982. assert m[-1, :] == Matrix([[2, 3]])
  1983. assert m[:, 1] == Matrix([[1, 3]]).T
  1984. assert m[:, -1] == Matrix([[1, 3]]).T
  1985. raises(IndexError, lambda: m[2, :])
  1986. raises(IndexError, lambda: m[2, 2])
  1987. def test_slice_issue_3401():
  1988. assert zeros(0, 3)[:, -1].shape == (0, 1)
  1989. assert zeros(3, 0)[0, :] == Matrix(1, 0, [])
  1990. def test_copyin():
  1991. s = zeros(3, 3)
  1992. s[3] = 1
  1993. assert s[:, 0] == Matrix([0, 1, 0])
  1994. assert s[3] == 1
  1995. assert s[3: 4] == [1]
  1996. s[1, 1] = 42
  1997. assert s[1, 1] == 42
  1998. assert s[1, 1:] == Matrix([[42, 0]])
  1999. s[1, 1:] = Matrix([[5, 6]])
  2000. assert s[1, :] == Matrix([[1, 5, 6]])
  2001. s[1, 1:] = [[42, 43]]
  2002. assert s[1, :] == Matrix([[1, 42, 43]])
  2003. s[0, 0] = 17
  2004. assert s[:, :1] == Matrix([17, 1, 0])
  2005. s[0, 0] = [1, 1, 1]
  2006. assert s[:, 0] == Matrix([1, 1, 1])
  2007. s[0, 0] = Matrix([1, 1, 1])
  2008. assert s[:, 0] == Matrix([1, 1, 1])
  2009. s[0, 0] = SparseMatrix([1, 1, 1])
  2010. assert s[:, 0] == Matrix([1, 1, 1])
  2011. def test_invertible_check():
  2012. # sometimes a singular matrix will have a pivot vector shorter than
  2013. # the number of rows in a matrix...
  2014. assert Matrix([[1, 2], [1, 2]]).rref() == (Matrix([[1, 2], [0, 0]]), (0,))
  2015. raises(ValueError, lambda: Matrix([[1, 2], [1, 2]]).inv())
  2016. m = Matrix([
  2017. [-1, -1, 0],
  2018. [ x, 1, 1],
  2019. [ 1, x, -1],
  2020. ])
  2021. assert len(m.rref()[1]) != m.rows
  2022. # in addition, unless simplify=True in the call to rref, the identity
  2023. # matrix will be returned even though m is not invertible
  2024. assert m.rref()[0] != eye(3)
  2025. assert m.rref(simplify=signsimp)[0] != eye(3)
  2026. raises(ValueError, lambda: m.inv(method="ADJ"))
  2027. raises(ValueError, lambda: m.inv(method="GE"))
  2028. raises(ValueError, lambda: m.inv(method="LU"))
  2029. def test_issue_3959():
  2030. x, y = symbols('x, y')
  2031. e = x*y
  2032. assert e.subs(x, Matrix([3, 5, 3])) == Matrix([3, 5, 3])*y
  2033. def test_issue_5964():
  2034. assert str(Matrix([[1, 2], [3, 4]])) == 'Matrix([[1, 2], [3, 4]])'
  2035. def test_issue_7604():
  2036. x, y = symbols("x y")
  2037. assert sstr(Matrix([[x, 2*y], [y**2, x + 3]])) == \
  2038. 'Matrix([\n[ x, 2*y],\n[y**2, x + 3]])'
  2039. def test_is_Identity():
  2040. assert eye(3).is_Identity
  2041. assert eye(3).as_immutable().is_Identity
  2042. assert not zeros(3).is_Identity
  2043. assert not ones(3).is_Identity
  2044. # issue 6242
  2045. assert not Matrix([[1, 0, 0]]).is_Identity
  2046. # issue 8854
  2047. assert SparseMatrix(3,3, {(0,0):1, (1,1):1, (2,2):1}).is_Identity
  2048. assert not SparseMatrix(2,3, range(6)).is_Identity
  2049. assert not SparseMatrix(3,3, {(0,0):1, (1,1):1}).is_Identity
  2050. assert not SparseMatrix(3,3, {(0,0):1, (1,1):1, (2,2):1, (0,1):2, (0,2):3}).is_Identity
  2051. def test_dot():
  2052. assert ones(1, 3).dot(ones(3, 1)) == 3
  2053. assert ones(1, 3).dot([1, 1, 1]) == 3
  2054. assert Matrix([1, 2, 3]).dot(Matrix([1, 2, 3])) == 14
  2055. assert Matrix([1, 2, 3*I]).dot(Matrix([I, 2, 3*I])) == -5 + I
  2056. assert Matrix([1, 2, 3*I]).dot(Matrix([I, 2, 3*I]), hermitian=False) == -5 + I
  2057. assert Matrix([1, 2, 3*I]).dot(Matrix([I, 2, 3*I]), hermitian=True) == 13 + I
  2058. assert Matrix([1, 2, 3*I]).dot(Matrix([I, 2, 3*I]), hermitian=True, conjugate_convention="physics") == 13 - I
  2059. assert Matrix([1, 2, 3*I]).dot(Matrix([4, 5*I, 6]), hermitian=True, conjugate_convention="right") == 4 + 8*I
  2060. assert Matrix([1, 2, 3*I]).dot(Matrix([4, 5*I, 6]), hermitian=True, conjugate_convention="left") == 4 - 8*I
  2061. assert Matrix([I, 2*I]).dot(Matrix([I, 2*I]), hermitian=False, conjugate_convention="left") == -5
  2062. assert Matrix([I, 2*I]).dot(Matrix([I, 2*I]), conjugate_convention="left") == 5
  2063. raises(ValueError, lambda: Matrix([1, 2]).dot(Matrix([3, 4]), hermitian=True, conjugate_convention="test"))
  2064. def test_dual():
  2065. B_x, B_y, B_z, E_x, E_y, E_z = symbols(
  2066. 'B_x B_y B_z E_x E_y E_z', real=True)
  2067. F = Matrix((
  2068. ( 0, E_x, E_y, E_z),
  2069. (-E_x, 0, B_z, -B_y),
  2070. (-E_y, -B_z, 0, B_x),
  2071. (-E_z, B_y, -B_x, 0)
  2072. ))
  2073. Fd = Matrix((
  2074. ( 0, -B_x, -B_y, -B_z),
  2075. (B_x, 0, E_z, -E_y),
  2076. (B_y, -E_z, 0, E_x),
  2077. (B_z, E_y, -E_x, 0)
  2078. ))
  2079. assert F.dual().equals(Fd)
  2080. assert eye(3).dual().equals(zeros(3))
  2081. assert F.dual().dual().equals(-F)
  2082. def test_anti_symmetric():
  2083. assert Matrix([1, 2]).is_anti_symmetric() is False
  2084. m = Matrix(3, 3, [0, x**2 + 2*x + 1, y, -(x + 1)**2, 0, x*y, -y, -x*y, 0])
  2085. assert m.is_anti_symmetric() is True
  2086. assert m.is_anti_symmetric(simplify=False) is False
  2087. assert m.is_anti_symmetric(simplify=lambda x: x) is False
  2088. # tweak to fail
  2089. m[2, 1] = -m[2, 1]
  2090. assert m.is_anti_symmetric() is False
  2091. # untweak
  2092. m[2, 1] = -m[2, 1]
  2093. m = m.expand()
  2094. assert m.is_anti_symmetric(simplify=False) is True
  2095. m[0, 0] = 1
  2096. assert m.is_anti_symmetric() is False
  2097. def test_normalize_sort_diogonalization():
  2098. A = Matrix(((1, 2), (2, 1)))
  2099. P, Q = A.diagonalize(normalize=True)
  2100. assert P*P.T == P.T*P == eye(P.cols)
  2101. P, Q = A.diagonalize(normalize=True, sort=True)
  2102. assert P*P.T == P.T*P == eye(P.cols)
  2103. assert P*Q*P.inv() == A
  2104. def test_issue_5321():
  2105. raises(ValueError, lambda: Matrix([[1, 2, 3], Matrix(0, 1, [])]))
  2106. def test_issue_5320():
  2107. assert Matrix.hstack(eye(2), 2*eye(2)) == Matrix([
  2108. [1, 0, 2, 0],
  2109. [0, 1, 0, 2]
  2110. ])
  2111. assert Matrix.vstack(eye(2), 2*eye(2)) == Matrix([
  2112. [1, 0],
  2113. [0, 1],
  2114. [2, 0],
  2115. [0, 2]
  2116. ])
  2117. cls = SparseMatrix
  2118. assert cls.hstack(cls(eye(2)), cls(2*eye(2))) == Matrix([
  2119. [1, 0, 2, 0],
  2120. [0, 1, 0, 2]
  2121. ])
  2122. def test_issue_11944():
  2123. A = Matrix([[1]])
  2124. AIm = sympify(A)
  2125. assert Matrix.hstack(AIm, A) == Matrix([[1, 1]])
  2126. assert Matrix.vstack(AIm, A) == Matrix([[1], [1]])
  2127. def test_cross():
  2128. a = [1, 2, 3]
  2129. b = [3, 4, 5]
  2130. col = Matrix([-2, 4, -2])
  2131. row = col.T
  2132. def test(M, ans):
  2133. assert ans == M
  2134. assert type(M) == cls
  2135. for cls in classes:
  2136. A = cls(a)
  2137. B = cls(b)
  2138. test(A.cross(B), col)
  2139. test(A.cross(B.T), col)
  2140. test(A.T.cross(B.T), row)
  2141. test(A.T.cross(B), row)
  2142. raises(ShapeError, lambda:
  2143. Matrix(1, 2, [1, 1]).cross(Matrix(1, 2, [1, 1])))
  2144. def test_hash():
  2145. for cls in classes[-2:]:
  2146. s = {cls.eye(1), cls.eye(1)}
  2147. assert len(s) == 1 and s.pop() == cls.eye(1)
  2148. # issue 3979
  2149. for cls in classes[:2]:
  2150. assert not isinstance(cls.eye(1), Hashable)
  2151. @XFAIL
  2152. def test_issue_3979():
  2153. # when this passes, delete this and change the [1:2]
  2154. # to [:2] in the test_hash above for issue 3979
  2155. cls = classes[0]
  2156. raises(AttributeError, lambda: hash(cls.eye(1)))
  2157. def test_adjoint():
  2158. dat = [[0, I], [1, 0]]
  2159. ans = Matrix([[0, 1], [-I, 0]])
  2160. for cls in classes:
  2161. assert ans == cls(dat).adjoint()
  2162. def test_simplify_immutable():
  2163. assert simplify(ImmutableMatrix([[sin(x)**2 + cos(x)**2]])) == \
  2164. ImmutableMatrix([[1]])
  2165. def test_replace():
  2166. F, G = symbols('F, G', cls=Function)
  2167. K = Matrix(2, 2, lambda i, j: G(i+j))
  2168. M = Matrix(2, 2, lambda i, j: F(i+j))
  2169. N = M.replace(F, G)
  2170. assert N == K
  2171. def test_replace_map():
  2172. F, G = symbols('F, G', cls=Function)
  2173. with warns_deprecated_sympy():
  2174. K = Matrix(2, 2, [(G(0), {F(0): G(0)}), (G(1), {F(1): G(1)}),
  2175. (G(1), {F(1): G(1)}), (G(2), {F(2): G(2)})])
  2176. M = Matrix(2, 2, lambda i, j: F(i+j))
  2177. with warns(SymPyDeprecationWarning, test_stacklevel=False):
  2178. N = M.replace(F, G, True)
  2179. assert N == K
  2180. def test_atoms():
  2181. m = Matrix([[1, 2], [x, 1 - 1/x]])
  2182. assert m.atoms() == {S.One,S(2),S.NegativeOne, x}
  2183. assert m.atoms(Symbol) == {x}
  2184. def test_pinv():
  2185. # Pseudoinverse of an invertible matrix is the inverse.
  2186. A1 = Matrix([[a, b], [c, d]])
  2187. assert simplify(A1.pinv(method="RD")) == simplify(A1.inv())
  2188. # Test the four properties of the pseudoinverse for various matrices.
  2189. As = [Matrix([[13, 104], [2212, 3], [-3, 5]]),
  2190. Matrix([[1, 7, 9], [11, 17, 19]]),
  2191. Matrix([a, b])]
  2192. for A in As:
  2193. A_pinv = A.pinv(method="RD")
  2194. AAp = A * A_pinv
  2195. ApA = A_pinv * A
  2196. assert simplify(AAp * A) == A
  2197. assert simplify(ApA * A_pinv) == A_pinv
  2198. assert AAp.H == AAp
  2199. assert ApA.H == ApA
  2200. # XXX Pinv with diagonalization makes expression too complicated.
  2201. for A in As:
  2202. A_pinv = simplify(A.pinv(method="ED"))
  2203. AAp = A * A_pinv
  2204. ApA = A_pinv * A
  2205. assert simplify(AAp * A) == A
  2206. assert simplify(ApA * A_pinv) == A_pinv
  2207. assert AAp.H == AAp
  2208. assert ApA.H == ApA
  2209. # XXX Computing pinv using diagonalization makes an expression that
  2210. # is too complicated to simplify.
  2211. # A1 = Matrix([[a, b], [c, d]])
  2212. # assert simplify(A1.pinv(method="ED")) == simplify(A1.inv())
  2213. # so this is tested numerically at a fixed random point
  2214. from sympy.core.numbers import comp
  2215. q = A1.pinv(method="ED")
  2216. w = A1.inv()
  2217. reps = {a: -73633, b: 11362, c: 55486, d: 62570}
  2218. assert all(
  2219. comp(i.n(), j.n())
  2220. for i, j in zip(q.subs(reps), w.subs(reps))
  2221. )
  2222. @slow
  2223. @XFAIL
  2224. def test_pinv_rank_deficient_when_diagonalization_fails():
  2225. # Test the four properties of the pseudoinverse for matrices when
  2226. # diagonalization of A.H*A fails.
  2227. As = [
  2228. Matrix([
  2229. [61, 89, 55, 20, 71, 0],
  2230. [62, 96, 85, 85, 16, 0],
  2231. [69, 56, 17, 4, 54, 0],
  2232. [10, 54, 91, 41, 71, 0],
  2233. [ 7, 30, 10, 48, 90, 0],
  2234. [0, 0, 0, 0, 0, 0]])
  2235. ]
  2236. for A in As:
  2237. A_pinv = A.pinv(method="ED")
  2238. AAp = A * A_pinv
  2239. ApA = A_pinv * A
  2240. assert AAp.H == AAp
  2241. assert ApA.H == ApA
  2242. def test_issue_7201():
  2243. assert ones(0, 1) + ones(0, 1) == Matrix(0, 1, [])
  2244. assert ones(1, 0) + ones(1, 0) == Matrix(1, 0, [])
  2245. def test_free_symbols():
  2246. for M in ImmutableMatrix, ImmutableSparseMatrix, Matrix, SparseMatrix:
  2247. assert M([[x], [0]]).free_symbols == {x}
  2248. def test_from_ndarray():
  2249. """See issue 7465."""
  2250. try:
  2251. from numpy import array
  2252. except ImportError:
  2253. skip('NumPy must be available to test creating matrices from ndarrays')
  2254. assert Matrix(array([1, 2, 3])) == Matrix([1, 2, 3])
  2255. assert Matrix(array([[1, 2, 3]])) == Matrix([[1, 2, 3]])
  2256. assert Matrix(array([[1, 2, 3], [4, 5, 6]])) == \
  2257. Matrix([[1, 2, 3], [4, 5, 6]])
  2258. assert Matrix(array([x, y, z])) == Matrix([x, y, z])
  2259. raises(NotImplementedError,
  2260. lambda: Matrix(array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])))
  2261. assert Matrix([array([1, 2]), array([3, 4])]) == Matrix([[1, 2], [3, 4]])
  2262. assert Matrix([array([1, 2]), [3, 4]]) == Matrix([[1, 2], [3, 4]])
  2263. assert Matrix([array([]), array([])]) == Matrix([])
  2264. def test_17522_numpy():
  2265. from sympy.matrices.common import _matrixify
  2266. try:
  2267. from numpy import array, matrix
  2268. except ImportError:
  2269. skip('NumPy must be available to test indexing matrixified NumPy ndarrays and matrices')
  2270. m = _matrixify(array([[1, 2], [3, 4]]))
  2271. assert m[3] == 4
  2272. assert list(m) == [1, 2, 3, 4]
  2273. with ignore_warnings(PendingDeprecationWarning):
  2274. m = _matrixify(matrix([[1, 2], [3, 4]]))
  2275. assert m[3] == 4
  2276. assert list(m) == [1, 2, 3, 4]
  2277. def test_17522_mpmath():
  2278. from sympy.matrices.common import _matrixify
  2279. try:
  2280. from mpmath import matrix
  2281. except ImportError:
  2282. skip('mpmath must be available to test indexing matrixified mpmath matrices')
  2283. m = _matrixify(matrix([[1, 2], [3, 4]]))
  2284. assert m[3] == 4.0
  2285. assert list(m) == [1.0, 2.0, 3.0, 4.0]
  2286. def test_17522_scipy():
  2287. from sympy.matrices.common import _matrixify
  2288. try:
  2289. from scipy.sparse import csr_matrix
  2290. except ImportError:
  2291. skip('SciPy must be available to test indexing matrixified SciPy sparse matrices')
  2292. m = _matrixify(csr_matrix([[1, 2], [3, 4]]))
  2293. assert m[3] == 4
  2294. assert list(m) == [1, 2, 3, 4]
  2295. def test_hermitian():
  2296. a = Matrix([[1, I], [-I, 1]])
  2297. assert a.is_hermitian
  2298. a[0, 0] = 2*I
  2299. assert a.is_hermitian is False
  2300. a[0, 0] = x
  2301. assert a.is_hermitian is None
  2302. a[0, 1] = a[1, 0]*I
  2303. assert a.is_hermitian is False
  2304. def test_doit():
  2305. a = Matrix([[Add(x,x, evaluate=False)]])
  2306. assert a[0] != 2*x
  2307. assert a.doit() == Matrix([[2*x]])
  2308. def test_issue_9457_9467_9876():
  2309. # for row_del(index)
  2310. M = Matrix([[1, 2, 3], [2, 3, 4], [3, 4, 5]])
  2311. M.row_del(1)
  2312. assert M == Matrix([[1, 2, 3], [3, 4, 5]])
  2313. N = Matrix([[1, 2, 3], [2, 3, 4], [3, 4, 5]])
  2314. N.row_del(-2)
  2315. assert N == Matrix([[1, 2, 3], [3, 4, 5]])
  2316. O = Matrix([[1, 2, 3], [5, 6, 7], [9, 10, 11]])
  2317. O.row_del(-1)
  2318. assert O == Matrix([[1, 2, 3], [5, 6, 7]])
  2319. P = Matrix([[1, 2, 3], [2, 3, 4], [3, 4, 5]])
  2320. raises(IndexError, lambda: P.row_del(10))
  2321. Q = Matrix([[1, 2, 3], [2, 3, 4], [3, 4, 5]])
  2322. raises(IndexError, lambda: Q.row_del(-10))
  2323. # for col_del(index)
  2324. M = Matrix([[1, 2, 3], [2, 3, 4], [3, 4, 5]])
  2325. M.col_del(1)
  2326. assert M == Matrix([[1, 3], [2, 4], [3, 5]])
  2327. N = Matrix([[1, 2, 3], [2, 3, 4], [3, 4, 5]])
  2328. N.col_del(-2)
  2329. assert N == Matrix([[1, 3], [2, 4], [3, 5]])
  2330. P = Matrix([[1, 2, 3], [2, 3, 4], [3, 4, 5]])
  2331. raises(IndexError, lambda: P.col_del(10))
  2332. Q = Matrix([[1, 2, 3], [2, 3, 4], [3, 4, 5]])
  2333. raises(IndexError, lambda: Q.col_del(-10))
  2334. def test_issue_9422():
  2335. x, y = symbols('x y', commutative=False)
  2336. a, b = symbols('a b')
  2337. M = eye(2)
  2338. M1 = Matrix(2, 2, [x, y, y, z])
  2339. assert y*x*M != x*y*M
  2340. assert b*a*M == a*b*M
  2341. assert x*M1 != M1*x
  2342. assert a*M1 == M1*a
  2343. assert y*x*M == Matrix([[y*x, 0], [0, y*x]])
  2344. def test_issue_10770():
  2345. M = Matrix([])
  2346. a = ['col_insert', 'row_join'], Matrix([9, 6, 3])
  2347. b = ['row_insert', 'col_join'], a[1].T
  2348. c = ['row_insert', 'col_insert'], Matrix([[1, 2], [3, 4]])
  2349. for ops, m in (a, b, c):
  2350. for op in ops:
  2351. f = getattr(M, op)
  2352. new = f(m) if 'join' in op else f(42, m)
  2353. assert new == m and id(new) != id(m)
  2354. def test_issue_10658():
  2355. A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  2356. assert A.extract([0, 1, 2], [True, True, False]) == \
  2357. Matrix([[1, 2], [4, 5], [7, 8]])
  2358. assert A.extract([0, 1, 2], [True, False, False]) == Matrix([[1], [4], [7]])
  2359. assert A.extract([True, False, False], [0, 1, 2]) == Matrix([[1, 2, 3]])
  2360. assert A.extract([True, False, True], [0, 1, 2]) == \
  2361. Matrix([[1, 2, 3], [7, 8, 9]])
  2362. assert A.extract([0, 1, 2], [False, False, False]) == Matrix(3, 0, [])
  2363. assert A.extract([False, False, False], [0, 1, 2]) == Matrix(0, 3, [])
  2364. assert A.extract([True, False, True], [False, True, False]) == \
  2365. Matrix([[2], [8]])
  2366. def test_opportunistic_simplification():
  2367. # this test relates to issue #10718, #9480, #11434
  2368. # issue #9480
  2369. m = Matrix([[-5 + 5*sqrt(2), -5], [-5*sqrt(2)/2 + 5, -5*sqrt(2)/2]])
  2370. assert m.rank() == 1
  2371. # issue #10781
  2372. m = Matrix([[3+3*sqrt(3)*I, -9],[4,-3+3*sqrt(3)*I]])
  2373. assert simplify(m.rref()[0] - Matrix([[1, -9/(3 + 3*sqrt(3)*I)], [0, 0]])) == zeros(2, 2)
  2374. # issue #11434
  2375. ax,ay,bx,by,cx,cy,dx,dy,ex,ey,t0,t1 = symbols('a_x a_y b_x b_y c_x c_y d_x d_y e_x e_y t_0 t_1')
  2376. m = Matrix([[ax,ay,ax*t0,ay*t0,0],[bx,by,bx*t0,by*t0,0],[cx,cy,cx*t0,cy*t0,1],[dx,dy,dx*t0,dy*t0,1],[ex,ey,2*ex*t1-ex*t0,2*ey*t1-ey*t0,0]])
  2377. assert m.rank() == 4
  2378. def test_partial_pivoting():
  2379. # example from https://en.wikipedia.org/wiki/Pivot_element
  2380. # partial pivoting with back substitution gives a perfect result
  2381. # naive pivoting give an error ~1e-13, so anything better than
  2382. # 1e-15 is good
  2383. mm=Matrix([[0.003, 59.14, 59.17], [5.291, -6.13, 46.78]])
  2384. assert (mm.rref()[0] - Matrix([[1.0, 0, 10.0],
  2385. [ 0, 1.0, 1.0]])).norm() < 1e-15
  2386. # issue #11549
  2387. m_mixed = Matrix([[6e-17, 1.0, 4],
  2388. [ -1.0, 0, 8],
  2389. [ 0, 0, 1]])
  2390. m_float = Matrix([[6e-17, 1.0, 4.],
  2391. [ -1.0, 0., 8.],
  2392. [ 0., 0., 1.]])
  2393. m_inv = Matrix([[ 0, -1.0, 8.0],
  2394. [1.0, 6.0e-17, -4.0],
  2395. [ 0, 0, 1]])
  2396. # this example is numerically unstable and involves a matrix with a norm >= 8,
  2397. # this comparing the difference of the results with 1e-15 is numerically sound.
  2398. assert (m_mixed.inv() - m_inv).norm() < 1e-15
  2399. assert (m_float.inv() - m_inv).norm() < 1e-15
  2400. def test_iszero_substitution():
  2401. """ When doing numerical computations, all elements that pass
  2402. the iszerofunc test should be set to numerically zero if they
  2403. aren't already. """
  2404. # Matrix from issue #9060
  2405. m = Matrix([[0.9, -0.1, -0.2, 0],[-0.8, 0.9, -0.4, 0],[-0.1, -0.8, 0.6, 0]])
  2406. m_rref = m.rref(iszerofunc=lambda x: abs(x)<6e-15)[0]
  2407. m_correct = Matrix([[1.0, 0, -0.301369863013699, 0],[ 0, 1.0, -0.712328767123288, 0],[ 0, 0, 0, 0]])
  2408. m_diff = m_rref - m_correct
  2409. assert m_diff.norm() < 1e-15
  2410. # if a zero-substitution wasn't made, this entry will be -1.11022302462516e-16
  2411. assert m_rref[2,2] == 0
  2412. def test_issue_11238():
  2413. from sympy.geometry.point import Point
  2414. xx = 8*tan(pi*Rational(13, 45))/(tan(pi*Rational(13, 45)) + sqrt(3))
  2415. yy = (-8*sqrt(3)*tan(pi*Rational(13, 45))**2 + 24*tan(pi*Rational(13, 45)))/(-3 + tan(pi*Rational(13, 45))**2)
  2416. p1 = Point(0, 0)
  2417. p2 = Point(1, -sqrt(3))
  2418. p0 = Point(xx,yy)
  2419. m1 = Matrix([p1 - simplify(p0), p2 - simplify(p0)])
  2420. m2 = Matrix([p1 - p0, p2 - p0])
  2421. m3 = Matrix([simplify(p1 - p0), simplify(p2 - p0)])
  2422. # This system has expressions which are zero and
  2423. # cannot be easily proved to be such, so without
  2424. # numerical testing, these assertions will fail.
  2425. Z = lambda x: abs(x.n()) < 1e-20
  2426. assert m1.rank(simplify=True, iszerofunc=Z) == 1
  2427. assert m2.rank(simplify=True, iszerofunc=Z) == 1
  2428. assert m3.rank(simplify=True, iszerofunc=Z) == 1
  2429. def test_as_real_imag():
  2430. m1 = Matrix(2,2,[1,2,3,4])
  2431. m2 = m1*S.ImaginaryUnit
  2432. m3 = m1 + m2
  2433. for kls in classes:
  2434. a,b = kls(m3).as_real_imag()
  2435. assert list(a) == list(m1)
  2436. assert list(b) == list(m1)
  2437. def test_deprecated():
  2438. # Maintain tests for deprecated functions. We must capture
  2439. # the deprecation warnings. When the deprecated functionality is
  2440. # removed, the corresponding tests should be removed.
  2441. m = Matrix(3, 3, [0, 1, 0, -4, 4, 0, -2, 1, 2])
  2442. P, Jcells = m.jordan_cells()
  2443. assert Jcells[1] == Matrix(1, 1, [2])
  2444. assert Jcells[0] == Matrix(2, 2, [2, 1, 0, 2])
  2445. def test_issue_14489():
  2446. from sympy.core.mod import Mod
  2447. A = Matrix([-1, 1, 2])
  2448. B = Matrix([10, 20, -15])
  2449. assert Mod(A, 3) == Matrix([2, 1, 2])
  2450. assert Mod(B, 4) == Matrix([2, 0, 1])
  2451. def test_issue_14943():
  2452. # Test that __array__ accepts the optional dtype argument
  2453. try:
  2454. from numpy import array
  2455. except ImportError:
  2456. skip('NumPy must be available to test creating matrices from ndarrays')
  2457. M = Matrix([[1,2], [3,4]])
  2458. assert array(M, dtype=float).dtype.name == 'float64'
  2459. def test_case_6913():
  2460. m = MatrixSymbol('m', 1, 1)
  2461. a = Symbol("a")
  2462. a = m[0, 0]>0
  2463. assert str(a) == 'm[0, 0] > 0'
  2464. def test_issue_11948():
  2465. A = MatrixSymbol('A', 3, 3)
  2466. a = Wild('a')
  2467. assert A.match(a) == {a: A}
  2468. def test_gramschmidt_conjugate_dot():
  2469. vecs = [Matrix([1, I]), Matrix([1, -I])]
  2470. assert Matrix.orthogonalize(*vecs) == \
  2471. [Matrix([[1], [I]]), Matrix([[1], [-I]])]
  2472. vecs = [Matrix([1, I, 0]), Matrix([I, 0, -I])]
  2473. assert Matrix.orthogonalize(*vecs) == \
  2474. [Matrix([[1], [I], [0]]), Matrix([[I/2], [S(1)/2], [-I]])]
  2475. mat = Matrix([[1, I], [1, -I]])
  2476. Q, R = mat.QRdecomposition()
  2477. assert Q * Q.H == Matrix.eye(2)
  2478. def test_issue_8207():
  2479. a = Matrix(MatrixSymbol('a', 3, 1))
  2480. b = Matrix(MatrixSymbol('b', 3, 1))
  2481. c = a.dot(b)
  2482. d = diff(c, a[0, 0])
  2483. e = diff(d, a[0, 0])
  2484. assert d == b[0, 0]
  2485. assert e == 0
  2486. def test_func():
  2487. from sympy.simplify.simplify import nthroot
  2488. A = Matrix([[1, 2],[0, 3]])
  2489. assert A.analytic_func(sin(x*t), x) == Matrix([[sin(t), sin(3*t) - sin(t)], [0, sin(3*t)]])
  2490. A = Matrix([[2, 1],[1, 2]])
  2491. assert (pi * A / 6).analytic_func(cos(x), x) == Matrix([[sqrt(3)/4, -sqrt(3)/4], [-sqrt(3)/4, sqrt(3)/4]])
  2492. raises(ValueError, lambda : zeros(5).analytic_func(log(x), x))
  2493. raises(ValueError, lambda : (A*x).analytic_func(log(x), x))
  2494. A = Matrix([[0, -1, -2, 3], [0, -1, -2, 3], [0, 1, 0, -1], [0, 0, -1, 1]])
  2495. assert A.analytic_func(exp(x), x) == A.exp()
  2496. raises(ValueError, lambda : A.analytic_func(sqrt(x), x))
  2497. A = Matrix([[41, 12],[12, 34]])
  2498. assert simplify(A.analytic_func(sqrt(x), x)**2) == A
  2499. A = Matrix([[3, -12, 4], [-1, 0, -2], [-1, 5, -1]])
  2500. assert simplify(A.analytic_func(nthroot(x, 3), x)**3) == A
  2501. A = Matrix([[2, 0, 0, 0], [1, 2, 0, 0], [0, 1, 3, 0], [0, 0, 1, 3]])
  2502. assert A.analytic_func(exp(x), x) == A.exp()
  2503. A = Matrix([[0, 2, 1, 6], [0, 0, 1, 2], [0, 0, 0, 3], [0, 0, 0, 0]])
  2504. assert A.analytic_func(exp(x*t), x) == expand(simplify((A*t).exp()))
  2505. @skip_under_pyodide("Cannot create threads under pyodide.")
  2506. def test_issue_19809():
  2507. def f():
  2508. assert _dotprodsimp_state.state == None
  2509. m = Matrix([[1]])
  2510. m = m * m
  2511. return True
  2512. with dotprodsimp(True):
  2513. with concurrent.futures.ThreadPoolExecutor() as executor:
  2514. future = executor.submit(f)
  2515. assert future.result()
  2516. def test_issue_23276():
  2517. M = Matrix([x, y])
  2518. assert integrate(M, (x, 0, 1), (y, 0, 1)) == Matrix([
  2519. [S.Half],
  2520. [S.Half]])