test_wester.py 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099
  1. """ Tests from Michael Wester's 1999 paper "Review of CAS mathematical
  2. capabilities".
  3. http://www.math.unm.edu/~wester/cas/book/Wester.pdf
  4. See also http://math.unm.edu/~wester/cas_review.html for detailed output of
  5. each tested system.
  6. """
  7. from sympy.assumptions.ask import Q, ask
  8. from sympy.assumptions.refine import refine
  9. from sympy.concrete.products import product
  10. from sympy.core import EulerGamma
  11. from sympy.core.evalf import N
  12. from sympy.core.function import (Derivative, Function, Lambda, Subs,
  13. diff, expand, expand_func)
  14. from sympy.core.mul import Mul
  15. from sympy.core.numbers import (AlgebraicNumber, E, I, Rational, igcd,
  16. nan, oo, pi, zoo)
  17. from sympy.core.relational import Eq, Lt
  18. from sympy.core.singleton import S
  19. from sympy.core.symbol import Dummy, Symbol, symbols
  20. from sympy.functions.combinatorial.factorials import (rf, binomial,
  21. factorial, factorial2)
  22. from sympy.functions.combinatorial.numbers import bernoulli, fibonacci
  23. from sympy.functions.elementary.complexes import (conjugate, im, re,
  24. sign)
  25. from sympy.functions.elementary.exponential import LambertW, exp, log
  26. from sympy.functions.elementary.hyperbolic import (asinh, cosh, sinh,
  27. tanh)
  28. from sympy.functions.elementary.integers import ceiling, floor
  29. from sympy.functions.elementary.miscellaneous import Max, Min, sqrt
  30. from sympy.functions.elementary.piecewise import Piecewise
  31. from sympy.functions.elementary.trigonometric import (acos, acot, asin,
  32. atan, cos, cot, csc, sec, sin, tan)
  33. from sympy.functions.special.bessel import besselj
  34. from sympy.functions.special.delta_functions import DiracDelta
  35. from sympy.functions.special.elliptic_integrals import (elliptic_e,
  36. elliptic_f)
  37. from sympy.functions.special.gamma_functions import gamma, polygamma
  38. from sympy.functions.special.hyper import hyper
  39. from sympy.functions.special.polynomials import (assoc_legendre,
  40. chebyshevt)
  41. from sympy.functions.special.zeta_functions import polylog
  42. from sympy.geometry.util import idiff
  43. from sympy.logic.boolalg import And
  44. from sympy.matrices.dense import hessian, wronskian
  45. from sympy.matrices.expressions.matmul import MatMul
  46. from sympy.ntheory.continued_fraction import (
  47. continued_fraction_convergents as cf_c,
  48. continued_fraction_iterator as cf_i, continued_fraction_periodic as
  49. cf_p, continued_fraction_reduce as cf_r)
  50. from sympy.ntheory.factor_ import factorint, totient
  51. from sympy.ntheory.generate import primerange
  52. from sympy.ntheory.partitions_ import npartitions
  53. from sympy.polys.domains.integerring import ZZ
  54. from sympy.polys.orthopolys import legendre_poly
  55. from sympy.polys.partfrac import apart
  56. from sympy.polys.polytools import Poly, factor, gcd, resultant
  57. from sympy.series.limits import limit
  58. from sympy.series.order import O
  59. from sympy.series.residues import residue
  60. from sympy.series.series import series
  61. from sympy.sets.fancysets import ImageSet
  62. from sympy.sets.sets import FiniteSet, Intersection, Interval, Union
  63. from sympy.simplify.combsimp import combsimp
  64. from sympy.simplify.hyperexpand import hyperexpand
  65. from sympy.simplify.powsimp import powdenest, powsimp
  66. from sympy.simplify.radsimp import radsimp
  67. from sympy.simplify.simplify import logcombine, simplify
  68. from sympy.simplify.sqrtdenest import sqrtdenest
  69. from sympy.simplify.trigsimp import trigsimp
  70. from sympy.solvers.solvers import solve
  71. import mpmath
  72. from sympy.functions.combinatorial.numbers import stirling
  73. from sympy.functions.special.delta_functions import Heaviside
  74. from sympy.functions.special.error_functions import Ci, Si, erf
  75. from sympy.functions.special.zeta_functions import zeta
  76. from sympy.testing.pytest import (XFAIL, slow, SKIP, skip, ON_CI,
  77. raises)
  78. from sympy.utilities.iterables import partitions
  79. from mpmath import mpi, mpc
  80. from sympy.matrices import Matrix, GramSchmidt, eye
  81. from sympy.matrices.expressions.blockmatrix import BlockMatrix, block_collapse
  82. from sympy.matrices.expressions import MatrixSymbol, ZeroMatrix
  83. from sympy.physics.quantum import Commutator
  84. from sympy.polys.rings import PolyRing
  85. from sympy.polys.fields import FracField
  86. from sympy.polys.solvers import solve_lin_sys
  87. from sympy.concrete import Sum
  88. from sympy.concrete.products import Product
  89. from sympy.integrals import integrate
  90. from sympy.integrals.transforms import laplace_transform,\
  91. inverse_laplace_transform, LaplaceTransform, fourier_transform,\
  92. mellin_transform
  93. from sympy.solvers.recurr import rsolve
  94. from sympy.solvers.solveset import solveset, solveset_real, linsolve
  95. from sympy.solvers.ode import dsolve
  96. from sympy.core.relational import Equality
  97. from itertools import islice, takewhile
  98. from sympy.series.formal import fps
  99. from sympy.series.fourier import fourier_series
  100. from sympy.calculus.util import minimum
  101. EmptySet = S.EmptySet
  102. R = Rational
  103. x, y, z = symbols('x y z')
  104. i, j, k, l, m, n = symbols('i j k l m n', integer=True)
  105. f = Function('f')
  106. g = Function('g')
  107. # A. Boolean Logic and Quantifier Elimination
  108. # Not implemented.
  109. # B. Set Theory
  110. def test_B1():
  111. assert (FiniteSet(i, j, j, k, k, k) | FiniteSet(l, k, j) |
  112. FiniteSet(j, m, j)) == FiniteSet(i, j, k, l, m)
  113. def test_B2():
  114. assert (FiniteSet(i, j, j, k, k, k) & FiniteSet(l, k, j) &
  115. FiniteSet(j, m, j)) == Intersection({j, m}, {i, j, k}, {j, k, l})
  116. # Previous output below. Not sure why that should be the expected output.
  117. # There should probably be a way to rewrite Intersections that way but I
  118. # don't see why an Intersection should evaluate like that:
  119. #
  120. # == Union({j}, Intersection({m}, Union({j, k}, Intersection({i}, {l}))))
  121. def test_B3():
  122. assert (FiniteSet(i, j, k, l, m) - FiniteSet(j) ==
  123. FiniteSet(i, k, l, m))
  124. def test_B4():
  125. assert (FiniteSet(*(FiniteSet(i, j)*FiniteSet(k, l))) ==
  126. FiniteSet((i, k), (i, l), (j, k), (j, l)))
  127. # C. Numbers
  128. def test_C1():
  129. assert (factorial(50) ==
  130. 30414093201713378043612608166064768844377641568960512000000000000)
  131. def test_C2():
  132. assert (factorint(factorial(50)) == {2: 47, 3: 22, 5: 12, 7: 8,
  133. 11: 4, 13: 3, 17: 2, 19: 2, 23: 2, 29: 1, 31: 1, 37: 1,
  134. 41: 1, 43: 1, 47: 1})
  135. def test_C3():
  136. assert (factorial2(10), factorial2(9)) == (3840, 945)
  137. # Base conversions; not really implemented by SymPy
  138. # Whatever. Take credit!
  139. def test_C4():
  140. assert 0xABC == 2748
  141. def test_C5():
  142. assert 123 == int('234', 7)
  143. def test_C6():
  144. assert int('677', 8) == int('1BF', 16) == 447
  145. def test_C7():
  146. assert log(32768, 8) == 5
  147. def test_C8():
  148. # Modular multiplicative inverse. Would be nice if divmod could do this.
  149. assert ZZ.invert(5, 7) == 3
  150. assert ZZ.invert(5, 6) == 5
  151. def test_C9():
  152. assert igcd(igcd(1776, 1554), 5698) == 74
  153. def test_C10():
  154. x = 0
  155. for n in range(2, 11):
  156. x += R(1, n)
  157. assert x == R(4861, 2520)
  158. def test_C11():
  159. assert R(1, 7) == S('0.[142857]')
  160. def test_C12():
  161. assert R(7, 11) * R(22, 7) == 2
  162. def test_C13():
  163. test = R(10, 7) * (1 + R(29, 1000)) ** R(1, 3)
  164. good = 3 ** R(1, 3)
  165. assert test == good
  166. def test_C14():
  167. assert sqrtdenest(sqrt(2*sqrt(3) + 4)) == 1 + sqrt(3)
  168. def test_C15():
  169. test = sqrtdenest(sqrt(14 + 3*sqrt(3 + 2*sqrt(5 - 12*sqrt(3 - 2*sqrt(2))))))
  170. good = sqrt(2) + 3
  171. assert test == good
  172. def test_C16():
  173. test = sqrtdenest(sqrt(10 + 2*sqrt(6) + 2*sqrt(10) + 2*sqrt(15)))
  174. good = sqrt(2) + sqrt(3) + sqrt(5)
  175. assert test == good
  176. def test_C17():
  177. test = radsimp((sqrt(3) + sqrt(2)) / (sqrt(3) - sqrt(2)))
  178. good = 5 + 2*sqrt(6)
  179. assert test == good
  180. def test_C18():
  181. assert simplify((sqrt(-2 + sqrt(-5)) * sqrt(-2 - sqrt(-5))).expand(complex=True)) == 3
  182. @XFAIL
  183. def test_C19():
  184. assert radsimp(simplify((90 + 34*sqrt(7)) ** R(1, 3))) == 3 + sqrt(7)
  185. def test_C20():
  186. inside = (135 + 78*sqrt(3))
  187. test = AlgebraicNumber((inside**R(2, 3) + 3) * sqrt(3) / inside**R(1, 3))
  188. assert simplify(test) == AlgebraicNumber(12)
  189. def test_C21():
  190. assert simplify(AlgebraicNumber((41 + 29*sqrt(2)) ** R(1, 5))) == \
  191. AlgebraicNumber(1 + sqrt(2))
  192. @XFAIL
  193. def test_C22():
  194. test = simplify(((6 - 4*sqrt(2))*log(3 - 2*sqrt(2)) + (3 - 2*sqrt(2))*log(17
  195. - 12*sqrt(2)) + 32 - 24*sqrt(2)) / (48*sqrt(2) - 72))
  196. good = sqrt(2)/3 - log(sqrt(2) - 1)/3
  197. assert test == good
  198. def test_C23():
  199. assert 2 * oo - 3 is oo
  200. @XFAIL
  201. def test_C24():
  202. raise NotImplementedError("2**aleph_null == aleph_1")
  203. # D. Numerical Analysis
  204. def test_D1():
  205. assert 0.0 / sqrt(2) == 0.0
  206. def test_D2():
  207. assert str(exp(-1000000).evalf()) == '3.29683147808856e-434295'
  208. def test_D3():
  209. assert exp(pi*sqrt(163)).evalf(50).num.ae(262537412640768744)
  210. def test_D4():
  211. assert floor(R(-5, 3)) == -2
  212. assert ceiling(R(-5, 3)) == -1
  213. @XFAIL
  214. def test_D5():
  215. raise NotImplementedError("cubic_spline([1, 2, 4, 5], [1, 4, 2, 3], x)(3) == 27/8")
  216. @XFAIL
  217. def test_D6():
  218. raise NotImplementedError("translate sum(a[i]*x**i, (i,1,n)) to FORTRAN")
  219. @XFAIL
  220. def test_D7():
  221. raise NotImplementedError("translate sum(a[i]*x**i, (i,1,n)) to C")
  222. @XFAIL
  223. def test_D8():
  224. # One way is to cheat by converting the sum to a string,
  225. # and replacing the '[' and ']' with ''.
  226. # E.g., horner(S(str(_).replace('[','').replace(']','')))
  227. raise NotImplementedError("apply Horner's rule to sum(a[i]*x**i, (i,1,5))")
  228. @XFAIL
  229. def test_D9():
  230. raise NotImplementedError("translate D8 to FORTRAN")
  231. @XFAIL
  232. def test_D10():
  233. raise NotImplementedError("translate D8 to C")
  234. @XFAIL
  235. def test_D11():
  236. #Is there a way to use count_ops?
  237. raise NotImplementedError("flops(sum(product(f[i][k], (i,1,k)), (k,1,n)))")
  238. @XFAIL
  239. def test_D12():
  240. assert (mpi(-4, 2) * x + mpi(1, 3)) ** 2 == mpi(-8, 16)*x**2 + mpi(-24, 12)*x + mpi(1, 9)
  241. @XFAIL
  242. def test_D13():
  243. raise NotImplementedError("discretize a PDE: diff(f(x,t),t) == diff(diff(f(x,t),x),x)")
  244. # E. Statistics
  245. # See scipy; all of this is numerical.
  246. # F. Combinatorial Theory.
  247. def test_F1():
  248. assert rf(x, 3) == x*(1 + x)*(2 + x)
  249. def test_F2():
  250. assert expand_func(binomial(n, 3)) == n*(n - 1)*(n - 2)/6
  251. @XFAIL
  252. def test_F3():
  253. assert combsimp(2**n * factorial(n) * factorial2(2*n - 1)) == factorial(2*n)
  254. @XFAIL
  255. def test_F4():
  256. assert combsimp(2**n * factorial(n) * product(2*k - 1, (k, 1, n))) == factorial(2*n)
  257. @XFAIL
  258. def test_F5():
  259. assert gamma(n + R(1, 2)) / sqrt(pi) / factorial(n) == factorial(2*n)/2**(2*n)/factorial(n)**2
  260. def test_F6():
  261. partTest = [p.copy() for p in partitions(4)]
  262. partDesired = [{4: 1}, {1: 1, 3: 1}, {2: 2}, {1: 2, 2:1}, {1: 4}]
  263. assert partTest == partDesired
  264. def test_F7():
  265. assert npartitions(4) == 5
  266. def test_F8():
  267. assert stirling(5, 2, signed=True) == -50 # if signed, then kind=1
  268. def test_F9():
  269. assert totient(1776) == 576
  270. # G. Number Theory
  271. def test_G1():
  272. assert list(primerange(999983, 1000004)) == [999983, 1000003]
  273. @XFAIL
  274. def test_G2():
  275. raise NotImplementedError("find the primitive root of 191 == 19")
  276. @XFAIL
  277. def test_G3():
  278. raise NotImplementedError("(a+b)**p mod p == a**p + b**p mod p; p prime")
  279. # ... G14 Modular equations are not implemented.
  280. def test_G15():
  281. assert Rational(sqrt(3).evalf()).limit_denominator(15) == R(26, 15)
  282. assert list(takewhile(lambda x: x.q <= 15, cf_c(cf_i(sqrt(3)))))[-1] == \
  283. R(26, 15)
  284. def test_G16():
  285. assert list(islice(cf_i(pi),10)) == [3, 7, 15, 1, 292, 1, 1, 1, 2, 1]
  286. def test_G17():
  287. assert cf_p(0, 1, 23) == [4, [1, 3, 1, 8]]
  288. def test_G18():
  289. assert cf_p(1, 2, 5) == [[1]]
  290. assert cf_r([[1]]).expand() == S.Half + sqrt(5)/2
  291. @XFAIL
  292. def test_G19():
  293. s = symbols('s', integer=True, positive=True)
  294. it = cf_i((exp(1/s) - 1)/(exp(1/s) + 1))
  295. assert list(islice(it, 5)) == [0, 2*s, 6*s, 10*s, 14*s]
  296. def test_G20():
  297. s = symbols('s', integer=True, positive=True)
  298. # Wester erroneously has this as -s + sqrt(s**2 + 1)
  299. assert cf_r([[2*s]]) == s + sqrt(s**2 + 1)
  300. @XFAIL
  301. def test_G20b():
  302. s = symbols('s', integer=True, positive=True)
  303. assert cf_p(s, 1, s**2 + 1) == [[2*s]]
  304. # H. Algebra
  305. def test_H1():
  306. assert simplify(2*2**n) == simplify(2**(n + 1))
  307. assert powdenest(2*2**n) == simplify(2**(n + 1))
  308. def test_H2():
  309. assert powsimp(4 * 2**n) == 2**(n + 2)
  310. def test_H3():
  311. assert (-1)**(n*(n + 1)) == 1
  312. def test_H4():
  313. expr = factor(6*x - 10)
  314. assert type(expr) is Mul
  315. assert expr.args[0] == 2
  316. assert expr.args[1] == 3*x - 5
  317. p1 = 64*x**34 - 21*x**47 - 126*x**8 - 46*x**5 - 16*x**60 - 81
  318. p2 = 72*x**60 - 25*x**25 - 19*x**23 - 22*x**39 - 83*x**52 + 54*x**10 + 81
  319. q = 34*x**19 - 25*x**16 + 70*x**7 + 20*x**3 - 91*x - 86
  320. def test_H5():
  321. assert gcd(p1, p2, x) == 1
  322. def test_H6():
  323. assert gcd(expand(p1 * q), expand(p2 * q)) == q
  324. def test_H7():
  325. p1 = 24*x*y**19*z**8 - 47*x**17*y**5*z**8 + 6*x**15*y**9*z**2 - 3*x**22 + 5
  326. p2 = 34*x**5*y**8*z**13 + 20*x**7*y**7*z**7 + 12*x**9*y**16*z**4 + 80*y**14*z
  327. assert gcd(p1, p2, x, y, z) == 1
  328. def test_H8():
  329. p1 = 24*x*y**19*z**8 - 47*x**17*y**5*z**8 + 6*x**15*y**9*z**2 - 3*x**22 + 5
  330. p2 = 34*x**5*y**8*z**13 + 20*x**7*y**7*z**7 + 12*x**9*y**16*z**4 + 80*y**14*z
  331. q = 11*x**12*y**7*z**13 - 23*x**2*y**8*z**10 + 47*x**17*y**5*z**8
  332. assert gcd(p1 * q, p2 * q, x, y, z) == q
  333. def test_H9():
  334. x = Symbol('x', zero=False)
  335. p1 = 2*x**(n + 4) - x**(n + 2)
  336. p2 = 4*x**(n + 1) + 3*x**n
  337. assert gcd(p1, p2) == x**n
  338. def test_H10():
  339. p1 = 3*x**4 + 3*x**3 + x**2 - x - 2
  340. p2 = x**3 - 3*x**2 + x + 5
  341. assert resultant(p1, p2, x) == 0
  342. def test_H11():
  343. assert resultant(p1 * q, p2 * q, x) == 0
  344. def test_H12():
  345. num = x**2 - 4
  346. den = x**2 + 4*x + 4
  347. assert simplify(num/den) == (x - 2)/(x + 2)
  348. @XFAIL
  349. def test_H13():
  350. assert simplify((exp(x) - 1) / (exp(x/2) + 1)) == exp(x/2) - 1
  351. def test_H14():
  352. p = (x + 1) ** 20
  353. ep = expand(p)
  354. assert ep == (1 + 20*x + 190*x**2 + 1140*x**3 + 4845*x**4 + 15504*x**5
  355. + 38760*x**6 + 77520*x**7 + 125970*x**8 + 167960*x**9 + 184756*x**10
  356. + 167960*x**11 + 125970*x**12 + 77520*x**13 + 38760*x**14 + 15504*x**15
  357. + 4845*x**16 + 1140*x**17 + 190*x**18 + 20*x**19 + x**20)
  358. dep = diff(ep, x)
  359. assert dep == (20 + 380*x + 3420*x**2 + 19380*x**3 + 77520*x**4
  360. + 232560*x**5 + 542640*x**6 + 1007760*x**7 + 1511640*x**8 + 1847560*x**9
  361. + 1847560*x**10 + 1511640*x**11 + 1007760*x**12 + 542640*x**13
  362. + 232560*x**14 + 77520*x**15 + 19380*x**16 + 3420*x**17 + 380*x**18
  363. + 20*x**19)
  364. assert factor(dep) == 20*(1 + x)**19
  365. def test_H15():
  366. assert simplify(Mul(*[x - r for r in solveset(x**3 + x**2 - 7)])) == x**3 + x**2 - 7
  367. def test_H16():
  368. assert factor(x**100 - 1) == ((x - 1)*(x + 1)*(x**2 + 1)*(x**4 - x**3
  369. + x**2 - x + 1)*(x**4 + x**3 + x**2 + x + 1)*(x**8 - x**6 + x**4
  370. - x**2 + 1)*(x**20 - x**15 + x**10 - x**5 + 1)*(x**20 + x**15 + x**10
  371. + x**5 + 1)*(x**40 - x**30 + x**20 - x**10 + 1))
  372. def test_H17():
  373. assert simplify(factor(expand(p1 * p2)) - p1*p2) == 0
  374. @XFAIL
  375. def test_H18():
  376. # Factor over complex rationals.
  377. test = factor(4*x**4 + 8*x**3 + 77*x**2 + 18*x + 153)
  378. good = (2*x + 3*I)*(2*x - 3*I)*(x + 1 - 4*I)*(x + 1 + 4*I)
  379. assert test == good
  380. def test_H19():
  381. a = symbols('a')
  382. # The idea is to let a**2 == 2, then solve 1/(a-1). Answer is a+1")
  383. assert Poly(a - 1).invert(Poly(a**2 - 2)) == a + 1
  384. @XFAIL
  385. def test_H20():
  386. raise NotImplementedError("let a**2==2; (x**3 + (a-2)*x**2 - "
  387. + "(2*a+3)*x - 3*a) / (x**2-2) = (x**2 - 2*x - 3) / (x-a)")
  388. @XFAIL
  389. def test_H21():
  390. raise NotImplementedError("evaluate (b+c)**4 assuming b**3==2, c**2==3. \
  391. Answer is 2*b + 8*c + 18*b**2 + 12*b*c + 9")
  392. def test_H22():
  393. assert factor(x**4 - 3*x**2 + 1, modulus=5) == (x - 2)**2 * (x + 2)**2
  394. def test_H23():
  395. f = x**11 + x + 1
  396. g = (x**2 + x + 1) * (x**9 - x**8 + x**6 - x**5 + x**3 - x**2 + 1)
  397. assert factor(f, modulus=65537) == g
  398. def test_H24():
  399. phi = AlgebraicNumber(S.GoldenRatio.expand(func=True), alias='phi')
  400. assert factor(x**4 - 3*x**2 + 1, extension=phi) == \
  401. (x - phi)*(x + 1 - phi)*(x - 1 + phi)*(x + phi)
  402. def test_H25():
  403. e = (x - 2*y**2 + 3*z**3) ** 20
  404. assert factor(expand(e)) == e
  405. def test_H26():
  406. g = expand((sin(x) - 2*cos(y)**2 + 3*tan(z)**3)**20)
  407. assert factor(g, expand=False) == (-sin(x) + 2*cos(y)**2 - 3*tan(z)**3)**20
  408. def test_H27():
  409. f = 24*x*y**19*z**8 - 47*x**17*y**5*z**8 + 6*x**15*y**9*z**2 - 3*x**22 + 5
  410. g = 34*x**5*y**8*z**13 + 20*x**7*y**7*z**7 + 12*x**9*y**16*z**4 + 80*y**14*z
  411. h = -2*z*y**7 \
  412. *(6*x**9*y**9*z**3 + 10*x**7*z**6 + 17*y*x**5*z**12 + 40*y**7) \
  413. *(3*x**22 + 47*x**17*y**5*z**8 - 6*x**15*y**9*z**2 - 24*x*y**19*z**8 - 5)
  414. assert factor(expand(f*g)) == h
  415. @XFAIL
  416. def test_H28():
  417. raise NotImplementedError("expand ((1 - c**2)**5 * (1 - s**2)**5 * "
  418. + "(c**2 + s**2)**10) with c**2 + s**2 = 1. Answer is c**10*s**10.")
  419. @XFAIL
  420. def test_H29():
  421. assert factor(4*x**2 - 21*x*y + 20*y**2, modulus=3) == (x + y)*(x - y)
  422. def test_H30():
  423. test = factor(x**3 + y**3, extension=sqrt(-3))
  424. answer = (x + y)*(x + y*(-R(1, 2) - sqrt(3)/2*I))*(x + y*(-R(1, 2) + sqrt(3)/2*I))
  425. assert answer == test
  426. def test_H31():
  427. f = (x**2 + 2*x + 3)/(x**3 + 4*x**2 + 5*x + 2)
  428. g = 2 / (x + 1)**2 - 2 / (x + 1) + 3 / (x + 2)
  429. assert apart(f) == g
  430. @XFAIL
  431. def test_H32(): # issue 6558
  432. raise NotImplementedError("[A*B*C - (A*B*C)**(-1)]*A*C*B (product \
  433. of a non-commuting product and its inverse)")
  434. def test_H33():
  435. A, B, C = symbols('A, B, C', commutative=False)
  436. assert (Commutator(A, Commutator(B, C))
  437. + Commutator(B, Commutator(C, A))
  438. + Commutator(C, Commutator(A, B))).doit().expand() == 0
  439. # I. Trigonometry
  440. def test_I1():
  441. assert tan(pi*R(7, 10)) == -sqrt(1 + 2/sqrt(5))
  442. @XFAIL
  443. def test_I2():
  444. assert sqrt((1 + cos(6))/2) == -cos(3)
  445. def test_I3():
  446. assert cos(n*pi) + sin((4*n - 1)*pi/2) == (-1)**n - 1
  447. def test_I4():
  448. assert refine(cos(pi*cos(n*pi)) + sin(pi/2*cos(n*pi)), Q.integer(n)) == (-1)**n - 1
  449. @XFAIL
  450. def test_I5():
  451. assert sin((n**5/5 + n**4/2 + n**3/3 - n/30) * pi) == 0
  452. @XFAIL
  453. def test_I6():
  454. raise NotImplementedError("assuming -3*pi<x<-5*pi/2, abs(cos(x)) == -cos(x), abs(sin(x)) == -sin(x)")
  455. @XFAIL
  456. def test_I7():
  457. assert cos(3*x)/cos(x) == cos(x)**2 - 3*sin(x)**2
  458. @XFAIL
  459. def test_I8():
  460. assert cos(3*x)/cos(x) == 2*cos(2*x) - 1
  461. @XFAIL
  462. def test_I9():
  463. # Supposed to do this with rewrite rules.
  464. assert cos(3*x)/cos(x) == cos(x)**2 - 3*sin(x)**2
  465. def test_I10():
  466. assert trigsimp((tan(x)**2 + 1 - cos(x)**-2) / (sin(x)**2 + cos(x)**2 - 1)) is nan
  467. @SKIP("hangs")
  468. @XFAIL
  469. def test_I11():
  470. assert limit((tan(x)**2 + 1 - cos(x)**-2) / (sin(x)**2 + cos(x)**2 - 1), x, 0) != 0
  471. @XFAIL
  472. def test_I12():
  473. # This should fail or return nan or something.
  474. res = diff((tan(x)**2 + 1 - cos(x)**-2) / (sin(x)**2 + cos(x)**2 - 1), x)
  475. assert res is nan # trigsimp(res) gives nan
  476. # J. Special functions.
  477. def test_J1():
  478. assert bernoulli(16) == R(-3617, 510)
  479. def test_J2():
  480. assert diff(elliptic_e(x, y**2), y) == (elliptic_e(x, y**2) - elliptic_f(x, y**2))/y
  481. @XFAIL
  482. def test_J3():
  483. raise NotImplementedError("Jacobi elliptic functions: diff(dn(u,k), u) == -k**2*sn(u,k)*cn(u,k)")
  484. def test_J4():
  485. assert gamma(R(-1, 2)) == -2*sqrt(pi)
  486. def test_J5():
  487. assert polygamma(0, R(1, 3)) == -log(3) - sqrt(3)*pi/6 - EulerGamma - log(sqrt(3))
  488. def test_J6():
  489. assert mpmath.besselj(2, 1 + 1j).ae(mpc('0.04157988694396212', '0.24739764151330632'))
  490. def test_J7():
  491. assert simplify(besselj(R(-5,2), pi/2)) == 12/(pi**2)
  492. def test_J8():
  493. p = besselj(R(3,2), z)
  494. q = (sin(z)/z - cos(z))/sqrt(pi*z/2)
  495. assert simplify(expand_func(p) -q) == 0
  496. def test_J9():
  497. assert besselj(0, z).diff(z) == - besselj(1, z)
  498. def test_J10():
  499. mu, nu = symbols('mu, nu', integer=True)
  500. assert assoc_legendre(nu, mu, 0) == 2**mu*sqrt(pi)/gamma((nu - mu)/2 + 1)/gamma((-nu - mu + 1)/2)
  501. def test_J11():
  502. assert simplify(assoc_legendre(3, 1, x)) == simplify(-R(3, 2)*sqrt(1 - x**2)*(5*x**2 - 1))
  503. @slow
  504. def test_J12():
  505. assert simplify(chebyshevt(1008, x) - 2*x*chebyshevt(1007, x) + chebyshevt(1006, x)) == 0
  506. def test_J13():
  507. a = symbols('a', integer=True, negative=False)
  508. assert chebyshevt(a, -1) == (-1)**a
  509. def test_J14():
  510. p = hyper([S.Half, S.Half], [R(3, 2)], z**2)
  511. assert hyperexpand(p) == asin(z)/z
  512. @XFAIL
  513. def test_J15():
  514. raise NotImplementedError("F((n+2)/2,-(n-2)/2,R(3,2),sin(z)**2) == sin(n*z)/(n*sin(z)*cos(z)); F(.) is hypergeometric function")
  515. @XFAIL
  516. def test_J16():
  517. raise NotImplementedError("diff(zeta(x), x) @ x=0 == -log(2*pi)/2")
  518. def test_J17():
  519. assert integrate(f((x + 2)/5)*DiracDelta((x - 2)/3) - g(x)*diff(DiracDelta(x - 1), x), (x, 0, 3)) == 3*f(R(4, 5)) + Subs(Derivative(g(x), x), x, 1)
  520. @XFAIL
  521. def test_J18():
  522. raise NotImplementedError("define an antisymmetric function")
  523. # K. The Complex Domain
  524. def test_K1():
  525. z1, z2 = symbols('z1, z2', complex=True)
  526. assert re(z1 + I*z2) == -im(z2) + re(z1)
  527. assert im(z1 + I*z2) == im(z1) + re(z2)
  528. def test_K2():
  529. assert abs(3 - sqrt(7) + I*sqrt(6*sqrt(7) - 15)) == 1
  530. @XFAIL
  531. def test_K3():
  532. a, b = symbols('a, b', real=True)
  533. assert simplify(abs(1/(a + I/a + I*b))) == 1/sqrt(a**2 + (I/a + b)**2)
  534. def test_K4():
  535. assert log(3 + 4*I).expand(complex=True) == log(5) + I*atan(R(4, 3))
  536. def test_K5():
  537. x, y = symbols('x, y', real=True)
  538. assert tan(x + I*y).expand(complex=True) == (sin(2*x)/(cos(2*x) +
  539. cosh(2*y)) + I*sinh(2*y)/(cos(2*x) + cosh(2*y)))
  540. def test_K6():
  541. assert sqrt(x*y*abs(z)**2)/(sqrt(x)*abs(z)) == sqrt(x*y)/sqrt(x)
  542. assert sqrt(x*y*abs(z)**2)/(sqrt(x)*abs(z)) != sqrt(y)
  543. def test_K7():
  544. y = symbols('y', real=True, negative=False)
  545. expr = sqrt(x*y*abs(z)**2)/(sqrt(x)*abs(z))
  546. sexpr = simplify(expr)
  547. assert sexpr == sqrt(y)
  548. def test_K8():
  549. z = symbols('z', complex=True)
  550. assert simplify(sqrt(1/z) - 1/sqrt(z)) != 0 # Passes
  551. z = symbols('z', complex=True, negative=False)
  552. assert simplify(sqrt(1/z) - 1/sqrt(z)) == 0 # Fails
  553. def test_K9():
  554. z = symbols('z', positive=True)
  555. assert simplify(sqrt(1/z) - 1/sqrt(z)) == 0
  556. def test_K10():
  557. z = symbols('z', negative=True)
  558. assert simplify(sqrt(1/z) + 1/sqrt(z)) == 0
  559. # This goes up to K25
  560. # L. Determining Zero Equivalence
  561. def test_L1():
  562. assert sqrt(997) - (997**3)**R(1, 6) == 0
  563. def test_L2():
  564. assert sqrt(999983) - (999983**3)**R(1, 6) == 0
  565. def test_L3():
  566. assert simplify((2**R(1, 3) + 4**R(1, 3))**3 - 6*(2**R(1, 3) + 4**R(1, 3)) - 6) == 0
  567. def test_L4():
  568. assert trigsimp(cos(x)**3 + cos(x)*sin(x)**2 - cos(x)) == 0
  569. @XFAIL
  570. def test_L5():
  571. assert log(tan(R(1, 2)*x + pi/4)) - asinh(tan(x)) == 0
  572. def test_L6():
  573. assert (log(tan(x/2 + pi/4)) - asinh(tan(x))).diff(x).subs({x: 0}) == 0
  574. @XFAIL
  575. def test_L7():
  576. assert simplify(log((2*sqrt(x) + 1)/(sqrt(4*x + 4*sqrt(x) + 1)))) == 0
  577. @XFAIL
  578. def test_L8():
  579. assert simplify((4*x + 4*sqrt(x) + 1)**(sqrt(x)/(2*sqrt(x) + 1)) \
  580. *(2*sqrt(x) + 1)**(1/(2*sqrt(x) + 1)) - 2*sqrt(x) - 1) == 0
  581. @XFAIL
  582. def test_L9():
  583. z = symbols('z', complex=True)
  584. assert simplify(2**(1 - z)*gamma(z)*zeta(z)*cos(z*pi/2) - pi**2*zeta(1 - z)) == 0
  585. # M. Equations
  586. @XFAIL
  587. def test_M1():
  588. assert Equality(x, 2)/2 + Equality(1, 1) == Equality(x/2 + 1, 2)
  589. def test_M2():
  590. # The roots of this equation should all be real. Note that this
  591. # doesn't test that they are correct.
  592. sol = solveset(3*x**3 - 18*x**2 + 33*x - 19, x)
  593. assert all(s.expand(complex=True).is_real for s in sol)
  594. @XFAIL
  595. def test_M5():
  596. assert solveset(x**6 - 9*x**4 - 4*x**3 + 27*x**2 - 36*x - 23, x) == FiniteSet(2**(1/3) + sqrt(3), 2**(1/3) - sqrt(3), +sqrt(3) - 1/2**(2/3) + I*sqrt(3)/2**(2/3), +sqrt(3) - 1/2**(2/3) - I*sqrt(3)/2**(2/3), -sqrt(3) - 1/2**(2/3) + I*sqrt(3)/2**(2/3), -sqrt(3) - 1/2**(2/3) - I*sqrt(3)/2**(2/3))
  597. def test_M6():
  598. assert set(solveset(x**7 - 1, x)) == \
  599. {cos(n*pi*R(2, 7)) + I*sin(n*pi*R(2, 7)) for n in range(0, 7)}
  600. # The paper asks for exp terms, but sin's and cos's may be acceptable;
  601. # if the results are simplified, exp terms appear for all but
  602. # -sin(pi/14) - I*cos(pi/14) and -sin(pi/14) + I*cos(pi/14) which
  603. # will simplify if you apply the transformation foo.rewrite(exp).expand()
  604. def test_M7():
  605. # TODO: Replace solve with solveset, as of now test fails for solveset
  606. assert set(solve(x**8 - 8*x**7 + 34*x**6 - 92*x**5 + 175*x**4 - 236*x**3 +
  607. 226*x**2 - 140*x + 46, x)) == {
  608. 1 - sqrt(2)*I*sqrt(-sqrt(-3 + 4*sqrt(3)) + 3)/2,
  609. 1 - sqrt(2)*sqrt(-3 + I*sqrt(3 + 4*sqrt(3)))/2,
  610. 1 - sqrt(2)*I*sqrt(sqrt(-3 + 4*sqrt(3)) + 3)/2,
  611. 1 - sqrt(2)*sqrt(-3 - I*sqrt(3 + 4*sqrt(3)))/2,
  612. 1 + sqrt(2)*I*sqrt(sqrt(-3 + 4*sqrt(3)) + 3)/2,
  613. 1 + sqrt(2)*sqrt(-3 - I*sqrt(3 + 4*sqrt(3)))/2,
  614. 1 + sqrt(2)*sqrt(-3 + I*sqrt(3 + 4*sqrt(3)))/2,
  615. 1 + sqrt(2)*I*sqrt(-sqrt(-3 + 4*sqrt(3)) + 3)/2,
  616. }
  617. @XFAIL # There are an infinite number of solutions.
  618. def test_M8():
  619. x = Symbol('x')
  620. z = symbols('z', complex=True)
  621. assert solveset(exp(2*x) + 2*exp(x) + 1 - z, x, S.Reals) == \
  622. FiniteSet(log(1 + z - 2*sqrt(z))/2, log(1 + z + 2*sqrt(z))/2)
  623. # This one could be simplified better (the 1/2 could be pulled into the log
  624. # as a sqrt, and the function inside the log can be factored as a square,
  625. # giving [log(sqrt(z) - 1), log(sqrt(z) + 1)]). Also, there should be an
  626. # infinite number of solutions.
  627. # x = {log(sqrt(z) - 1), log(sqrt(z) + 1) + i pi} [+ n 2 pi i, + n 2 pi i]
  628. # where n is an arbitrary integer. See url of detailed output above.
  629. @XFAIL
  630. def test_M9():
  631. # x = symbols('x')
  632. raise NotImplementedError("solveset(exp(2-x**2)-exp(-x),x) has complex solutions.")
  633. def test_M10():
  634. # TODO: Replace solve with solveset, as of now test fails for solveset
  635. assert solve(exp(x) - x, x) == [-LambertW(-1)]
  636. @XFAIL
  637. def test_M11():
  638. assert solveset(x**x - x, x) == FiniteSet(-1, 1)
  639. def test_M12():
  640. # TODO: x = [-1, 2*(+/-asinh(1)*I + n*pi}, 3*(pi/6 + n*pi/3)]
  641. # TODO: Replace solve with solveset, as of now test fails for solveset
  642. assert solve((x + 1)*(sin(x)**2 + 1)**2*cos(3*x)**3, x) == [
  643. -1, pi/6, pi/2,
  644. - I*log(1 + sqrt(2)), I*log(1 + sqrt(2)),
  645. pi - I*log(1 + sqrt(2)), pi + I*log(1 + sqrt(2)),
  646. ]
  647. @XFAIL
  648. def test_M13():
  649. n = Dummy('n')
  650. assert solveset_real(sin(x) - cos(x), x) == ImageSet(Lambda(n, n*pi - pi*R(7, 4)), S.Integers)
  651. @XFAIL
  652. def test_M14():
  653. n = Dummy('n')
  654. assert solveset_real(tan(x) - 1, x) == ImageSet(Lambda(n, n*pi + pi/4), S.Integers)
  655. def test_M15():
  656. n = Dummy('n')
  657. got = solveset(sin(x) - S.Half)
  658. assert any(got.dummy_eq(i) for i in (
  659. Union(ImageSet(Lambda(n, 2*n*pi + pi/6), S.Integers),
  660. ImageSet(Lambda(n, 2*n*pi + pi*R(5, 6)), S.Integers)),
  661. Union(ImageSet(Lambda(n, 2*n*pi + pi*R(5, 6)), S.Integers),
  662. ImageSet(Lambda(n, 2*n*pi + pi/6), S.Integers))))
  663. @XFAIL
  664. def test_M16():
  665. n = Dummy('n')
  666. assert solveset(sin(x) - tan(x), x) == ImageSet(Lambda(n, n*pi), S.Integers)
  667. @XFAIL
  668. def test_M17():
  669. assert solveset_real(asin(x) - atan(x), x) == FiniteSet(0)
  670. @XFAIL
  671. def test_M18():
  672. assert solveset_real(acos(x) - atan(x), x) == FiniteSet(sqrt((sqrt(5) - 1)/2))
  673. def test_M19():
  674. # TODO: Replace solve with solveset, as of now test fails for solveset
  675. assert solve((x - 2)/x**R(1, 3), x) == [2]
  676. def test_M20():
  677. assert solveset(sqrt(x**2 + 1) - x + 2, x) == EmptySet
  678. def test_M21():
  679. assert solveset(x + sqrt(x) - 2) == FiniteSet(1)
  680. def test_M22():
  681. assert solveset(2*sqrt(x) + 3*x**R(1, 4) - 2) == FiniteSet(R(1, 16))
  682. def test_M23():
  683. x = symbols('x', complex=True)
  684. # TODO: Replace solve with solveset, as of now test fails for solveset
  685. assert solve(x - 1/sqrt(1 + x**2)) == [
  686. -I*sqrt(S.Half + sqrt(5)/2), sqrt(Rational(-1, 2) + sqrt(5)/2)]
  687. def test_M24():
  688. # TODO: Replace solve with solveset, as of now test fails for solveset
  689. solution = solve(1 - binomial(m, 2)*2**k, k)
  690. answer = log(2/(m*(m - 1)), 2)
  691. assert solution[0].expand() == answer.expand()
  692. def test_M25():
  693. a, b, c, d = symbols(':d', positive=True)
  694. x = symbols('x')
  695. # TODO: Replace solve with solveset, as of now test fails for solveset
  696. assert solve(a*b**x - c*d**x, x)[0].expand() == (log(c/a)/log(b/d)).expand()
  697. def test_M26():
  698. # TODO: Replace solve with solveset, as of now test fails for solveset
  699. assert solve(sqrt(log(x)) - log(sqrt(x))) == [1, exp(4)]
  700. def test_M27():
  701. x = symbols('x', real=True)
  702. b = symbols('b', real=True)
  703. # TODO: Replace solve with solveset which gives both [+/- current answer]
  704. # note that there is a typo in this test in the wester.pdf; there is no
  705. # real solution for the equation as it appears in wester.pdf
  706. assert solve(log(acos(asin(x**R(2, 3) - b)) - 1) + 2, x
  707. ) == [(b + sin(cos(exp(-2) + 1)))**R(3, 2)]
  708. @XFAIL
  709. def test_M28():
  710. assert solveset_real(5*x + exp((x - 5)/2) - 8*x**3, x, assume=Q.real(x)) == [-0.784966, -0.016291, 0.802557]
  711. def test_M29():
  712. x = symbols('x')
  713. assert solveset(abs(x - 1) - 2, domain=S.Reals) == FiniteSet(-1, 3)
  714. def test_M30():
  715. # TODO: Replace solve with solveset, as of now
  716. # solveset doesn't supports assumptions
  717. # assert solve(abs(2*x + 5) - abs(x - 2),x, assume=Q.real(x)) == [-1, -7]
  718. assert solveset_real(abs(2*x + 5) - abs(x - 2), x) == FiniteSet(-1, -7)
  719. def test_M31():
  720. # TODO: Replace solve with solveset, as of now
  721. # solveset doesn't supports assumptions
  722. # assert solve(1 - abs(x) - max(-x - 2, x - 2),x, assume=Q.real(x)) == [-3/2, 3/2]
  723. assert solveset_real(1 - abs(x) - Max(-x - 2, x - 2), x) == FiniteSet(R(-3, 2), R(3, 2))
  724. @XFAIL
  725. def test_M32():
  726. # TODO: Replace solve with solveset, as of now
  727. # solveset doesn't supports assumptions
  728. assert solveset_real(Max(2 - x**2, x)- Max(-x, (x**3)/9), x) == FiniteSet(-1, 3)
  729. @XFAIL
  730. def test_M33():
  731. # TODO: Replace solve with solveset, as of now
  732. # solveset doesn't supports assumptions
  733. # Second answer can be written in another form. The second answer is the root of x**3 + 9*x**2 - 18 = 0 in the interval (-2, -1).
  734. assert solveset_real(Max(2 - x**2, x) - x**3/9, x) == FiniteSet(-3, -1.554894, 3)
  735. @XFAIL
  736. def test_M34():
  737. z = symbols('z', complex=True)
  738. assert solveset((1 + I) * z + (2 - I) * conjugate(z) + 3*I, z) == FiniteSet(2 + 3*I)
  739. def test_M35():
  740. x, y = symbols('x y', real=True)
  741. assert linsolve((3*x - 2*y - I*y + 3*I).as_real_imag(), y, x) == FiniteSet((3, 2))
  742. def test_M36():
  743. # TODO: Replace solve with solveset, as of now
  744. # solveset doesn't supports solving for function
  745. # assert solve(f**2 + f - 2, x) == [Eq(f(x), 1), Eq(f(x), -2)]
  746. assert solveset(f(x)**2 + f(x) - 2, f(x)) == FiniteSet(-2, 1)
  747. def test_M37():
  748. assert linsolve([x + y + z - 6, 2*x + y + 2*z - 10, x + 3*y + z - 10 ], x, y, z) == \
  749. FiniteSet((-z + 4, 2, z))
  750. def test_M38():
  751. a, b, c = symbols('a, b, c')
  752. domain = FracField([a, b, c], ZZ).to_domain()
  753. ring = PolyRing('k1:50', domain)
  754. (k1, k2, k3, k4, k5, k6, k7, k8, k9, k10,
  755. k11, k12, k13, k14, k15, k16, k17, k18, k19, k20,
  756. k21, k22, k23, k24, k25, k26, k27, k28, k29, k30,
  757. k31, k32, k33, k34, k35, k36, k37, k38, k39, k40,
  758. k41, k42, k43, k44, k45, k46, k47, k48, k49) = ring.gens
  759. system = [
  760. -b*k8/a + c*k8/a, -b*k11/a + c*k11/a, -b*k10/a + c*k10/a + k2, -k3 - b*k9/a + c*k9/a,
  761. -b*k14/a + c*k14/a, -b*k15/a + c*k15/a, -b*k18/a + c*k18/a - k2, -b*k17/a + c*k17/a,
  762. -b*k16/a + c*k16/a + k4, -b*k13/a + c*k13/a - b*k21/a + c*k21/a + b*k5/a - c*k5/a,
  763. b*k44/a - c*k44/a, -b*k45/a + c*k45/a, -b*k20/a + c*k20/a, -b*k44/a + c*k44/a,
  764. b*k46/a - c*k46/a, b**2*k47/a**2 - 2*b*c*k47/a**2 + c**2*k47/a**2, k3, -k4,
  765. -b*k12/a + c*k12/a - a*k6/b + c*k6/b, -b*k19/a + c*k19/a + a*k7/c - b*k7/c,
  766. b*k45/a - c*k45/a, -b*k46/a + c*k46/a, -k48 + c*k48/a + c*k48/b - c**2*k48/(a*b),
  767. -k49 + b*k49/a + b*k49/c - b**2*k49/(a*c), a*k1/b - c*k1/b, a*k4/b - c*k4/b,
  768. a*k3/b - c*k3/b + k9, -k10 + a*k2/b - c*k2/b, a*k7/b - c*k7/b, -k9, k11,
  769. b*k12/a - c*k12/a + a*k6/b - c*k6/b, a*k15/b - c*k15/b, k10 + a*k18/b - c*k18/b,
  770. -k11 + a*k17/b - c*k17/b, a*k16/b - c*k16/b, -a*k13/b + c*k13/b + a*k21/b - c*k21/b + a*k5/b - c*k5/b,
  771. -a*k44/b + c*k44/b, a*k45/b - c*k45/b, a*k14/c - b*k14/c + a*k20/b - c*k20/b,
  772. a*k44/b - c*k44/b, -a*k46/b + c*k46/b, -k47 + c*k47/a + c*k47/b - c**2*k47/(a*b),
  773. a*k19/b - c*k19/b, -a*k45/b + c*k45/b, a*k46/b - c*k46/b, a**2*k48/b**2 - 2*a*c*k48/b**2 + c**2*k48/b**2,
  774. -k49 + a*k49/b + a*k49/c - a**2*k49/(b*c), k16, -k17, -a*k1/c + b*k1/c,
  775. -k16 - a*k4/c + b*k4/c, -a*k3/c + b*k3/c, k18 - a*k2/c + b*k2/c, b*k19/a - c*k19/a - a*k7/c + b*k7/c,
  776. -a*k6/c + b*k6/c, -a*k8/c + b*k8/c, -a*k11/c + b*k11/c + k17, -a*k10/c + b*k10/c - k18,
  777. -a*k9/c + b*k9/c, -a*k14/c + b*k14/c - a*k20/b + c*k20/b, -a*k13/c + b*k13/c + a*k21/c - b*k21/c - a*k5/c + b*k5/c,
  778. a*k44/c - b*k44/c, -a*k45/c + b*k45/c, -a*k44/c + b*k44/c, a*k46/c - b*k46/c,
  779. -k47 + b*k47/a + b*k47/c - b**2*k47/(a*c), -a*k12/c + b*k12/c, a*k45/c - b*k45/c,
  780. -a*k46/c + b*k46/c, -k48 + a*k48/b + a*k48/c - a**2*k48/(b*c),
  781. a**2*k49/c**2 - 2*a*b*k49/c**2 + b**2*k49/c**2, k8, k11, -k15, k10 - k18,
  782. -k17, k9, -k16, -k29, k14 - k32, -k21 + k23 - k31, -k24 - k30, -k35, k44,
  783. -k45, k36, k13 - k23 + k39, -k20 + k38, k25 + k37, b*k26/a - c*k26/a - k34 + k42,
  784. -2*k44, k45, k46, b*k47/a - c*k47/a, k41, k44, -k46, -b*k47/a + c*k47/a,
  785. k12 + k24, -k19 - k25, -a*k27/b + c*k27/b - k33, k45, -k46, -a*k48/b + c*k48/b,
  786. a*k28/c - b*k28/c + k40, -k45, k46, a*k48/b - c*k48/b, a*k49/c - b*k49/c,
  787. -a*k49/c + b*k49/c, -k1, -k4, -k3, k15, k18 - k2, k17, k16, k22, k25 - k7,
  788. k24 + k30, k21 + k23 - k31, k28, -k44, k45, -k30 - k6, k20 + k32, k27 + b*k33/a - c*k33/a,
  789. k44, -k46, -b*k47/a + c*k47/a, -k36, k31 - k39 - k5, -k32 - k38, k19 - k37,
  790. k26 - a*k34/b + c*k34/b - k42, k44, -2*k45, k46, a*k48/b - c*k48/b,
  791. a*k35/c - b*k35/c - k41, -k44, k46, b*k47/a - c*k47/a, -a*k49/c + b*k49/c,
  792. -k40, k45, -k46, -a*k48/b + c*k48/b, a*k49/c - b*k49/c, k1, k4, k3, -k8,
  793. -k11, -k10 + k2, -k9, k37 + k7, -k14 - k38, -k22, -k25 - k37, -k24 + k6,
  794. -k13 - k23 + k39, -k28 + b*k40/a - c*k40/a, k44, -k45, -k27, -k44, k46,
  795. b*k47/a - c*k47/a, k29, k32 + k38, k31 - k39 + k5, -k12 + k30, k35 - a*k41/b + c*k41/b,
  796. -k44, k45, -k26 + k34 + a*k42/c - b*k42/c, k44, k45, -2*k46, -b*k47/a + c*k47/a,
  797. -a*k48/b + c*k48/b, a*k49/c - b*k49/c, k33, -k45, k46, a*k48/b - c*k48/b,
  798. -a*k49/c + b*k49/c
  799. ]
  800. solution = {
  801. k49: 0, k48: 0, k47: 0, k46: 0, k45: 0, k44: 0, k41: 0, k40: 0,
  802. k38: 0, k37: 0, k36: 0, k35: 0, k33: 0, k32: 0, k30: 0, k29: 0,
  803. k28: 0, k27: 0, k25: 0, k24: 0, k22: 0, k21: 0, k20: 0, k19: 0,
  804. k18: 0, k17: 0, k16: 0, k15: 0, k14: 0, k13: 0, k12: 0, k11: 0,
  805. k10: 0, k9: 0, k8: 0, k7: 0, k6: 0, k5: 0, k4: 0, k3: 0,
  806. k2: 0, k1: 0,
  807. k34: b/c*k42, k31: k39, k26: a/c*k42, k23: k39
  808. }
  809. assert solve_lin_sys(system, ring) == solution
  810. def test_M39():
  811. x, y, z = symbols('x y z', complex=True)
  812. # TODO: Replace solve with solveset, as of now
  813. # solveset doesn't supports non-linear multivariate
  814. assert solve([x**2*y + 3*y*z - 4, -3*x**2*z + 2*y**2 + 1, 2*y*z**2 - z**2 - 1 ]) ==\
  815. [{y: 1, z: 1, x: -1}, {y: 1, z: 1, x: 1},\
  816. {y: sqrt(2)*I, z: R(1,3) - sqrt(2)*I/3, x: -sqrt(-1 - sqrt(2)*I)},\
  817. {y: sqrt(2)*I, z: R(1,3) - sqrt(2)*I/3, x: sqrt(-1 - sqrt(2)*I)},\
  818. {y: -sqrt(2)*I, z: R(1,3) + sqrt(2)*I/3, x: -sqrt(-1 + sqrt(2)*I)},\
  819. {y: -sqrt(2)*I, z: R(1,3) + sqrt(2)*I/3, x: sqrt(-1 + sqrt(2)*I)}]
  820. # N. Inequalities
  821. def test_N1():
  822. assert ask(E**pi > pi**E)
  823. @XFAIL
  824. def test_N2():
  825. x = symbols('x', real=True)
  826. assert ask(x**4 - x + 1 > 0) is True
  827. assert ask(x**4 - x + 1 > 1) is False
  828. @XFAIL
  829. def test_N3():
  830. x = symbols('x', real=True)
  831. assert ask(And(Lt(-1, x), Lt(x, 1)), abs(x) < 1 )
  832. @XFAIL
  833. def test_N4():
  834. x, y = symbols('x y', real=True)
  835. assert ask(2*x**2 > 2*y**2, (x > y) & (y > 0)) is True
  836. @XFAIL
  837. def test_N5():
  838. x, y, k = symbols('x y k', real=True)
  839. assert ask(k*x**2 > k*y**2, (x > y) & (y > 0) & (k > 0)) is True
  840. @slow
  841. @XFAIL
  842. def test_N6():
  843. x, y, k, n = symbols('x y k n', real=True)
  844. assert ask(k*x**n > k*y**n, (x > y) & (y > 0) & (k > 0) & (n > 0)) is True
  845. @XFAIL
  846. def test_N7():
  847. x, y = symbols('x y', real=True)
  848. assert ask(y > 0, (x > 1) & (y >= x - 1)) is True
  849. @XFAIL
  850. @slow
  851. def test_N8():
  852. x, y, z = symbols('x y z', real=True)
  853. assert ask(Eq(x, y) & Eq(y, z),
  854. (x >= y) & (y >= z) & (z >= x))
  855. def test_N9():
  856. x = Symbol('x')
  857. assert solveset(abs(x - 1) > 2, domain=S.Reals) == Union(Interval(-oo, -1, False, True),
  858. Interval(3, oo, True))
  859. def test_N10():
  860. x = Symbol('x')
  861. p = (x - 1)*(x - 2)*(x - 3)*(x - 4)*(x - 5)
  862. assert solveset(expand(p) < 0, domain=S.Reals) == Union(Interval(-oo, 1, True, True),
  863. Interval(2, 3, True, True),
  864. Interval(4, 5, True, True))
  865. def test_N11():
  866. x = Symbol('x')
  867. assert solveset(6/(x - 3) <= 3, domain=S.Reals) == Union(Interval(-oo, 3, True, True), Interval(5, oo))
  868. def test_N12():
  869. x = Symbol('x')
  870. assert solveset(sqrt(x) < 2, domain=S.Reals) == Interval(0, 4, False, True)
  871. def test_N13():
  872. x = Symbol('x')
  873. assert solveset(sin(x) < 2, domain=S.Reals) == S.Reals
  874. @XFAIL
  875. def test_N14():
  876. x = Symbol('x')
  877. # Gives 'Union(Interval(Integer(0), Mul(Rational(1, 2), pi), false, true),
  878. # Interval(Mul(Rational(1, 2), pi), Mul(Integer(2), pi), true, false))'
  879. # which is not the correct answer, but the provided also seems wrong.
  880. assert solveset(sin(x) < 1, x, domain=S.Reals) == Union(Interval(-oo, pi/2, True, True),
  881. Interval(pi/2, oo, True, True))
  882. def test_N15():
  883. r, t = symbols('r t')
  884. # raises NotImplementedError: only univariate inequalities are supported
  885. solveset(abs(2*r*(cos(t) - 1) + 1) <= 1, r, S.Reals)
  886. def test_N16():
  887. r, t = symbols('r t')
  888. solveset((r**2)*((cos(t) - 4)**2)*sin(t)**2 < 9, r, S.Reals)
  889. @XFAIL
  890. def test_N17():
  891. # currently only univariate inequalities are supported
  892. assert solveset((x + y > 0, x - y < 0), (x, y)) == (abs(x) < y)
  893. def test_O1():
  894. M = Matrix((1 + I, -2, 3*I))
  895. assert sqrt(expand(M.dot(M.H))) == sqrt(15)
  896. def test_O2():
  897. assert Matrix((2, 2, -3)).cross(Matrix((1, 3, 1))) == Matrix([[11],
  898. [-5],
  899. [4]])
  900. # The vector module has no way of representing vectors symbolically (without
  901. # respect to a basis)
  902. @XFAIL
  903. def test_O3():
  904. # assert (va ^ vb) | (vc ^ vd) == -(va | vc)*(vb | vd) + (va | vd)*(vb | vc)
  905. raise NotImplementedError("""The vector module has no way of representing
  906. vectors symbolically (without respect to a basis)""")
  907. def test_O4():
  908. from sympy.vector import CoordSys3D, Del
  909. N = CoordSys3D("N")
  910. delop = Del()
  911. i, j, k = N.base_vectors()
  912. x, y, z = N.base_scalars()
  913. F = i*(x*y*z) + j*((x*y*z)**2) + k*((y**2)*(z**3))
  914. assert delop.cross(F).doit() == (-2*x**2*y**2*z + 2*y*z**3)*i + x*y*j + (2*x*y**2*z**2 - x*z)*k
  915. @XFAIL
  916. def test_O5():
  917. #assert grad|(f^g)-g|(grad^f)+f|(grad^g) == 0
  918. raise NotImplementedError("""The vector module has no way of representing
  919. vectors symbolically (without respect to a basis)""")
  920. #testO8-O9 MISSING!!
  921. def test_O10():
  922. L = [Matrix([2, 3, 5]), Matrix([3, 6, 2]), Matrix([8, 3, 6])]
  923. assert GramSchmidt(L) == [Matrix([
  924. [2],
  925. [3],
  926. [5]]),
  927. Matrix([
  928. [R(23, 19)],
  929. [R(63, 19)],
  930. [R(-47, 19)]]),
  931. Matrix([
  932. [R(1692, 353)],
  933. [R(-1551, 706)],
  934. [R(-423, 706)]])]
  935. def test_P1():
  936. assert Matrix(3, 3, lambda i, j: j - i).diagonal(-1) == Matrix(
  937. 1, 2, [-1, -1])
  938. def test_P2():
  939. M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  940. M.row_del(1)
  941. M.col_del(2)
  942. assert M == Matrix([[1, 2],
  943. [7, 8]])
  944. def test_P3():
  945. A = Matrix([
  946. [11, 12, 13, 14],
  947. [21, 22, 23, 24],
  948. [31, 32, 33, 34],
  949. [41, 42, 43, 44]])
  950. A11 = A[0:3, 1:4]
  951. A12 = A[(0, 1, 3), (2, 0, 3)]
  952. A21 = A
  953. A221 = -A[0:2, 2:4]
  954. A222 = -A[(3, 0), (2, 1)]
  955. A22 = BlockMatrix([[A221, A222]]).T
  956. rows = [[-A11, A12], [A21, A22]]
  957. raises(ValueError, lambda: BlockMatrix(rows))
  958. B = Matrix(rows)
  959. assert B == Matrix([
  960. [-12, -13, -14, 13, 11, 14],
  961. [-22, -23, -24, 23, 21, 24],
  962. [-32, -33, -34, 43, 41, 44],
  963. [11, 12, 13, 14, -13, -23],
  964. [21, 22, 23, 24, -14, -24],
  965. [31, 32, 33, 34, -43, -13],
  966. [41, 42, 43, 44, -42, -12]])
  967. @XFAIL
  968. def test_P4():
  969. raise NotImplementedError("Block matrix diagonalization not supported")
  970. def test_P5():
  971. M = Matrix([[7, 11],
  972. [3, 8]])
  973. assert M % 2 == Matrix([[1, 1],
  974. [1, 0]])
  975. def test_P6():
  976. M = Matrix([[cos(x), sin(x)],
  977. [-sin(x), cos(x)]])
  978. assert M.diff(x, 2) == Matrix([[-cos(x), -sin(x)],
  979. [sin(x), -cos(x)]])
  980. def test_P7():
  981. M = Matrix([[x, y]])*(
  982. z*Matrix([[1, 3, 5],
  983. [2, 4, 6]]) + Matrix([[7, -9, 11],
  984. [-8, 10, -12]]))
  985. assert M == Matrix([[x*(z + 7) + y*(2*z - 8), x*(3*z - 9) + y*(4*z + 10),
  986. x*(5*z + 11) + y*(6*z - 12)]])
  987. def test_P8():
  988. M = Matrix([[1, -2*I],
  989. [-3*I, 4]])
  990. assert M.norm(ord=S.Infinity) == 7
  991. def test_P9():
  992. a, b, c = symbols('a b c', nonzero=True)
  993. M = Matrix([[a/(b*c), 1/c, 1/b],
  994. [1/c, b/(a*c), 1/a],
  995. [1/b, 1/a, c/(a*b)]])
  996. assert factor(M.norm('fro')) == (a**2 + b**2 + c**2)/(abs(a)*abs(b)*abs(c))
  997. @XFAIL
  998. def test_P10():
  999. M = Matrix([[1, 2 + 3*I],
  1000. [f(4 - 5*I), 6]])
  1001. # conjugate(f(4 - 5*i)) is not simplified to f(4+5*I)
  1002. assert M.H == Matrix([[1, f(4 + 5*I)],
  1003. [2 + 3*I, 6]])
  1004. @XFAIL
  1005. def test_P11():
  1006. # raises NotImplementedError("Matrix([[x,y],[1,x*y]]).inv()
  1007. # not simplifying to extract common factor")
  1008. assert Matrix([[x, y],
  1009. [1, x*y]]).inv() == (1/(x**2 - 1))*Matrix([[x, -1],
  1010. [-1/y, x/y]])
  1011. def test_P11_workaround():
  1012. # This test was changed to inverse method ADJ because it depended on the
  1013. # specific form of inverse returned from the 'GE' method which has changed.
  1014. M = Matrix([[x, y], [1, x*y]]).inv('ADJ')
  1015. c = gcd(tuple(M))
  1016. assert MatMul(c, M/c, evaluate=False) == MatMul(c, Matrix([
  1017. [x*y, -y],
  1018. [ -1, x]]), evaluate=False)
  1019. def test_P12():
  1020. A11 = MatrixSymbol('A11', n, n)
  1021. A12 = MatrixSymbol('A12', n, n)
  1022. A22 = MatrixSymbol('A22', n, n)
  1023. B = BlockMatrix([[A11, A12],
  1024. [ZeroMatrix(n, n), A22]])
  1025. assert block_collapse(B.I) == BlockMatrix([[A11.I, (-1)*A11.I*A12*A22.I],
  1026. [ZeroMatrix(n, n), A22.I]])
  1027. def test_P13():
  1028. M = Matrix([[1, x - 2, x - 3],
  1029. [x - 1, x**2 - 3*x + 6, x**2 - 3*x - 2],
  1030. [x - 2, x**2 - 8, 2*(x**2) - 12*x + 14]])
  1031. L, U, _ = M.LUdecomposition()
  1032. assert simplify(L) == Matrix([[1, 0, 0],
  1033. [x - 1, 1, 0],
  1034. [x - 2, x - 3, 1]])
  1035. assert simplify(U) == Matrix([[1, x - 2, x - 3],
  1036. [0, 4, x - 5],
  1037. [0, 0, x - 7]])
  1038. def test_P14():
  1039. M = Matrix([[1, 2, 3, 1, 3],
  1040. [3, 2, 1, 1, 7],
  1041. [0, 2, 4, 1, 1],
  1042. [1, 1, 1, 1, 4]])
  1043. R, _ = M.rref()
  1044. assert R == Matrix([[1, 0, -1, 0, 2],
  1045. [0, 1, 2, 0, -1],
  1046. [0, 0, 0, 1, 3],
  1047. [0, 0, 0, 0, 0]])
  1048. def test_P15():
  1049. M = Matrix([[-1, 3, 7, -5],
  1050. [4, -2, 1, 3],
  1051. [2, 4, 15, -7]])
  1052. assert M.rank() == 2
  1053. def test_P16():
  1054. M = Matrix([[2*sqrt(2), 8],
  1055. [6*sqrt(6), 24*sqrt(3)]])
  1056. assert M.rank() == 1
  1057. def test_P17():
  1058. t = symbols('t', real=True)
  1059. M=Matrix([
  1060. [sin(2*t), cos(2*t)],
  1061. [2*(1 - (cos(t)**2))*cos(t), (1 - 2*(sin(t)**2))*sin(t)]])
  1062. assert M.rank() == 1
  1063. def test_P18():
  1064. M = Matrix([[1, 0, -2, 0],
  1065. [-2, 1, 0, 3],
  1066. [-1, 2, -6, 6]])
  1067. assert M.nullspace() == [Matrix([[2],
  1068. [4],
  1069. [1],
  1070. [0]]),
  1071. Matrix([[0],
  1072. [-3],
  1073. [0],
  1074. [1]])]
  1075. def test_P19():
  1076. w = symbols('w')
  1077. M = Matrix([[1, 1, 1, 1],
  1078. [w, x, y, z],
  1079. [w**2, x**2, y**2, z**2],
  1080. [w**3, x**3, y**3, z**3]])
  1081. assert M.det() == (w**3*x**2*y - w**3*x**2*z - w**3*x*y**2 + w**3*x*z**2
  1082. + w**3*y**2*z - w**3*y*z**2 - w**2*x**3*y + w**2*x**3*z
  1083. + w**2*x*y**3 - w**2*x*z**3 - w**2*y**3*z + w**2*y*z**3
  1084. + w*x**3*y**2 - w*x**3*z**2 - w*x**2*y**3 + w*x**2*z**3
  1085. + w*y**3*z**2 - w*y**2*z**3 - x**3*y**2*z + x**3*y*z**2
  1086. + x**2*y**3*z - x**2*y*z**3 - x*y**3*z**2 + x*y**2*z**3
  1087. )
  1088. @XFAIL
  1089. def test_P20():
  1090. raise NotImplementedError("Matrix minimal polynomial not supported")
  1091. def test_P21():
  1092. M = Matrix([[5, -3, -7],
  1093. [-2, 1, 2],
  1094. [2, -3, -4]])
  1095. assert M.charpoly(x).as_expr() == x**3 - 2*x**2 - 5*x + 6
  1096. def test_P22():
  1097. d = 100
  1098. M = (2 - x)*eye(d)
  1099. assert M.eigenvals() == {-x + 2: d}
  1100. def test_P23():
  1101. M = Matrix([
  1102. [2, 1, 0, 0, 0],
  1103. [1, 2, 1, 0, 0],
  1104. [0, 1, 2, 1, 0],
  1105. [0, 0, 1, 2, 1],
  1106. [0, 0, 0, 1, 2]])
  1107. assert M.eigenvals() == {
  1108. S('1'): 1,
  1109. S('2'): 1,
  1110. S('3'): 1,
  1111. S('sqrt(3) + 2'): 1,
  1112. S('-sqrt(3) + 2'): 1}
  1113. def test_P24():
  1114. M = Matrix([[611, 196, -192, 407, -8, -52, -49, 29],
  1115. [196, 899, 113, -192, -71, -43, -8, -44],
  1116. [-192, 113, 899, 196, 61, 49, 8, 52],
  1117. [ 407, -192, 196, 611, 8, 44, 59, -23],
  1118. [ -8, -71, 61, 8, 411, -599, 208, 208],
  1119. [ -52, -43, 49, 44, -599, 411, 208, 208],
  1120. [ -49, -8, 8, 59, 208, 208, 99, -911],
  1121. [ 29, -44, 52, -23, 208, 208, -911, 99]])
  1122. assert M.eigenvals() == {
  1123. S('0'): 1,
  1124. S('10*sqrt(10405)'): 1,
  1125. S('100*sqrt(26) + 510'): 1,
  1126. S('1000'): 2,
  1127. S('-100*sqrt(26) + 510'): 1,
  1128. S('-10*sqrt(10405)'): 1,
  1129. S('1020'): 1}
  1130. def test_P25():
  1131. MF = N(Matrix([[ 611, 196, -192, 407, -8, -52, -49, 29],
  1132. [ 196, 899, 113, -192, -71, -43, -8, -44],
  1133. [-192, 113, 899, 196, 61, 49, 8, 52],
  1134. [ 407, -192, 196, 611, 8, 44, 59, -23],
  1135. [ -8, -71, 61, 8, 411, -599, 208, 208],
  1136. [ -52, -43, 49, 44, -599, 411, 208, 208],
  1137. [ -49, -8, 8, 59, 208, 208, 99, -911],
  1138. [ 29, -44, 52, -23, 208, 208, -911, 99]]))
  1139. ev_1 = sorted(MF.eigenvals(multiple=True))
  1140. ev_2 = sorted(
  1141. [-1020.0490184299969, 0.0, 0.09804864072151699, 1000.0, 1000.0,
  1142. 1019.9019513592784, 1020.0, 1020.0490184299969])
  1143. for x, y in zip(ev_1, ev_2):
  1144. assert abs(x - y) < 1e-12
  1145. def test_P26():
  1146. a0, a1, a2, a3, a4 = symbols('a0 a1 a2 a3 a4')
  1147. M = Matrix([[-a4, -a3, -a2, -a1, -a0, 0, 0, 0, 0],
  1148. [ 1, 0, 0, 0, 0, 0, 0, 0, 0],
  1149. [ 0, 1, 0, 0, 0, 0, 0, 0, 0],
  1150. [ 0, 0, 1, 0, 0, 0, 0, 0, 0],
  1151. [ 0, 0, 0, 1, 0, 0, 0, 0, 0],
  1152. [ 0, 0, 0, 0, 0, -1, -1, 0, 0],
  1153. [ 0, 0, 0, 0, 0, 1, 0, 0, 0],
  1154. [ 0, 0, 0, 0, 0, 0, 1, -1, -1],
  1155. [ 0, 0, 0, 0, 0, 0, 0, 1, 0]])
  1156. assert M.eigenvals(error_when_incomplete=False) == {
  1157. S('-1/2 - sqrt(3)*I/2'): 2,
  1158. S('-1/2 + sqrt(3)*I/2'): 2}
  1159. def test_P27():
  1160. a = symbols('a')
  1161. M = Matrix([[a, 0, 0, 0, 0],
  1162. [0, 0, 0, 0, 1],
  1163. [0, 0, a, 0, 0],
  1164. [0, 0, 0, a, 0],
  1165. [0, -2, 0, 0, 2]])
  1166. assert M.eigenvects() == [
  1167. (a, 3, [
  1168. Matrix([1, 0, 0, 0, 0]),
  1169. Matrix([0, 0, 1, 0, 0]),
  1170. Matrix([0, 0, 0, 1, 0])
  1171. ]),
  1172. (1 - I, 1, [
  1173. Matrix([0, (1 + I)/2, 0, 0, 1])
  1174. ]),
  1175. (1 + I, 1, [
  1176. Matrix([0, (1 - I)/2, 0, 0, 1])
  1177. ]),
  1178. ]
  1179. @XFAIL
  1180. def test_P28():
  1181. raise NotImplementedError("Generalized eigenvectors not supported \
  1182. https://github.com/sympy/sympy/issues/5293")
  1183. @XFAIL
  1184. def test_P29():
  1185. raise NotImplementedError("Generalized eigenvectors not supported \
  1186. https://github.com/sympy/sympy/issues/5293")
  1187. def test_P30():
  1188. M = Matrix([[1, 0, 0, 1, -1],
  1189. [0, 1, -2, 3, -3],
  1190. [0, 0, -1, 2, -2],
  1191. [1, -1, 1, 0, 1],
  1192. [1, -1, 1, -1, 2]])
  1193. _, J = M.jordan_form()
  1194. assert J == Matrix([[-1, 0, 0, 0, 0],
  1195. [0, 1, 1, 0, 0],
  1196. [0, 0, 1, 0, 0],
  1197. [0, 0, 0, 1, 1],
  1198. [0, 0, 0, 0, 1]])
  1199. @XFAIL
  1200. def test_P31():
  1201. raise NotImplementedError("Smith normal form not implemented")
  1202. def test_P32():
  1203. M = Matrix([[1, -2],
  1204. [2, 1]])
  1205. assert exp(M).rewrite(cos).simplify() == Matrix([[E*cos(2), -E*sin(2)],
  1206. [E*sin(2), E*cos(2)]])
  1207. def test_P33():
  1208. w, t = symbols('w t')
  1209. M = Matrix([[0, 1, 0, 0],
  1210. [0, 0, 0, 2*w],
  1211. [0, 0, 0, 1],
  1212. [0, -2*w, 3*w**2, 0]])
  1213. assert exp(M*t).rewrite(cos).expand() == Matrix([
  1214. [1, -3*t + 4*sin(t*w)/w, 6*t*w - 6*sin(t*w), -2*cos(t*w)/w + 2/w],
  1215. [0, 4*cos(t*w) - 3, -6*w*cos(t*w) + 6*w, 2*sin(t*w)],
  1216. [0, 2*cos(t*w)/w - 2/w, -3*cos(t*w) + 4, sin(t*w)/w],
  1217. [0, -2*sin(t*w), 3*w*sin(t*w), cos(t*w)]])
  1218. @XFAIL
  1219. def test_P34():
  1220. a, b, c = symbols('a b c', real=True)
  1221. M = Matrix([[a, 1, 0, 0, 0, 0],
  1222. [0, a, 0, 0, 0, 0],
  1223. [0, 0, b, 0, 0, 0],
  1224. [0, 0, 0, c, 1, 0],
  1225. [0, 0, 0, 0, c, 1],
  1226. [0, 0, 0, 0, 0, c]])
  1227. # raises exception, sin(M) not supported. exp(M*I) also not supported
  1228. # https://github.com/sympy/sympy/issues/6218
  1229. assert sin(M) == Matrix([[sin(a), cos(a), 0, 0, 0, 0],
  1230. [0, sin(a), 0, 0, 0, 0],
  1231. [0, 0, sin(b), 0, 0, 0],
  1232. [0, 0, 0, sin(c), cos(c), -sin(c)/2],
  1233. [0, 0, 0, 0, sin(c), cos(c)],
  1234. [0, 0, 0, 0, 0, sin(c)]])
  1235. @XFAIL
  1236. def test_P35():
  1237. M = pi/2*Matrix([[2, 1, 1],
  1238. [2, 3, 2],
  1239. [1, 1, 2]])
  1240. # raises exception, sin(M) not supported. exp(M*I) also not supported
  1241. # https://github.com/sympy/sympy/issues/6218
  1242. assert sin(M) == eye(3)
  1243. @XFAIL
  1244. def test_P36():
  1245. M = Matrix([[10, 7],
  1246. [7, 17]])
  1247. assert sqrt(M) == Matrix([[3, 1],
  1248. [1, 4]])
  1249. def test_P37():
  1250. M = Matrix([[1, 1, 0],
  1251. [0, 1, 0],
  1252. [0, 0, 1]])
  1253. assert M**S.Half == Matrix([[1, R(1, 2), 0],
  1254. [0, 1, 0],
  1255. [0, 0, 1]])
  1256. @XFAIL
  1257. def test_P38():
  1258. M=Matrix([[0, 1, 0],
  1259. [0, 0, 0],
  1260. [0, 0, 0]])
  1261. with raises(AssertionError):
  1262. # raises ValueError: Matrix det == 0; not invertible
  1263. M**S.Half
  1264. # if it doesn't raise then this assertion will be
  1265. # raised and the test will be flagged as not XFAILing
  1266. assert None
  1267. @XFAIL
  1268. def test_P39():
  1269. """
  1270. M=Matrix([
  1271. [1, 1],
  1272. [2, 2],
  1273. [3, 3]])
  1274. M.SVD()
  1275. """
  1276. raise NotImplementedError("Singular value decomposition not implemented")
  1277. def test_P40():
  1278. r, t = symbols('r t', real=True)
  1279. M = Matrix([r*cos(t), r*sin(t)])
  1280. assert M.jacobian(Matrix([r, t])) == Matrix([[cos(t), -r*sin(t)],
  1281. [sin(t), r*cos(t)]])
  1282. def test_P41():
  1283. r, t = symbols('r t', real=True)
  1284. assert hessian(r**2*sin(t),(r,t)) == Matrix([[ 2*sin(t), 2*r*cos(t)],
  1285. [2*r*cos(t), -r**2*sin(t)]])
  1286. def test_P42():
  1287. assert wronskian([cos(x), sin(x)], x).simplify() == 1
  1288. def test_P43():
  1289. def __my_jacobian(M, Y):
  1290. return Matrix([M.diff(v).T for v in Y]).T
  1291. r, t = symbols('r t', real=True)
  1292. M = Matrix([r*cos(t), r*sin(t)])
  1293. assert __my_jacobian(M,[r,t]) == Matrix([[cos(t), -r*sin(t)],
  1294. [sin(t), r*cos(t)]])
  1295. def test_P44():
  1296. def __my_hessian(f, Y):
  1297. V = Matrix([diff(f, v) for v in Y])
  1298. return Matrix([V.T.diff(v) for v in Y])
  1299. r, t = symbols('r t', real=True)
  1300. assert __my_hessian(r**2*sin(t), (r, t)) == Matrix([
  1301. [ 2*sin(t), 2*r*cos(t)],
  1302. [2*r*cos(t), -r**2*sin(t)]])
  1303. def test_P45():
  1304. def __my_wronskian(Y, v):
  1305. M = Matrix([Matrix(Y).T.diff(x, n) for n in range(0, len(Y))])
  1306. return M.det()
  1307. assert __my_wronskian([cos(x), sin(x)], x).simplify() == 1
  1308. # Q1-Q6 Tensor tests missing
  1309. @XFAIL
  1310. def test_R1():
  1311. i, j, n = symbols('i j n', integer=True, positive=True)
  1312. xn = MatrixSymbol('xn', n, 1)
  1313. Sm = Sum((xn[i, 0] - Sum(xn[j, 0], (j, 0, n - 1))/n)**2, (i, 0, n - 1))
  1314. # sum does not calculate
  1315. # Unknown result
  1316. Sm.doit()
  1317. raise NotImplementedError('Unknown result')
  1318. @XFAIL
  1319. def test_R2():
  1320. m, b = symbols('m b')
  1321. i, n = symbols('i n', integer=True, positive=True)
  1322. xn = MatrixSymbol('xn', n, 1)
  1323. yn = MatrixSymbol('yn', n, 1)
  1324. f = Sum((yn[i, 0] - m*xn[i, 0] - b)**2, (i, 0, n - 1))
  1325. f1 = diff(f, m)
  1326. f2 = diff(f, b)
  1327. # raises TypeError: solveset() takes at most 2 arguments (3 given)
  1328. solveset((f1, f2), (m, b), domain=S.Reals)
  1329. @XFAIL
  1330. def test_R3():
  1331. n, k = symbols('n k', integer=True, positive=True)
  1332. sk = ((-1)**k) * (binomial(2*n, k))**2
  1333. Sm = Sum(sk, (k, 1, oo))
  1334. T = Sm.doit()
  1335. T2 = T.combsimp()
  1336. # returns -((-1)**n*factorial(2*n)
  1337. # - (factorial(n))**2)*exp_polar(-I*pi)/(factorial(n))**2
  1338. assert T2 == (-1)**n*binomial(2*n, n)
  1339. @XFAIL
  1340. def test_R4():
  1341. # Macsyma indefinite sum test case:
  1342. #(c15) /* Check whether the full Gosper algorithm is implemented
  1343. # => 1/2^(n + 1) binomial(n, k - 1) */
  1344. #closedform(indefsum(binomial(n, k)/2^n - binomial(n + 1, k)/2^(n + 1), k));
  1345. #Time= 2690 msecs
  1346. # (- n + k - 1) binomial(n + 1, k)
  1347. #(d15) - --------------------------------
  1348. # n
  1349. # 2 2 (n + 1)
  1350. #
  1351. #(c16) factcomb(makefact(%));
  1352. #Time= 220 msecs
  1353. # n!
  1354. #(d16) ----------------
  1355. # n
  1356. # 2 k! 2 (n - k)!
  1357. # Might be possible after fixing https://github.com/sympy/sympy/pull/1879
  1358. raise NotImplementedError("Indefinite sum not supported")
  1359. @XFAIL
  1360. def test_R5():
  1361. a, b, c, n, k = symbols('a b c n k', integer=True, positive=True)
  1362. sk = ((-1)**k)*(binomial(a + b, a + k)
  1363. *binomial(b + c, b + k)*binomial(c + a, c + k))
  1364. Sm = Sum(sk, (k, 1, oo))
  1365. T = Sm.doit() # hypergeometric series not calculated
  1366. assert T == factorial(a+b+c)/(factorial(a)*factorial(b)*factorial(c))
  1367. def test_R6():
  1368. n, k = symbols('n k', integer=True, positive=True)
  1369. gn = MatrixSymbol('gn', n + 2, 1)
  1370. Sm = Sum(gn[k, 0] - gn[k - 1, 0], (k, 1, n + 1))
  1371. assert Sm.doit() == -gn[0, 0] + gn[n + 1, 0]
  1372. def test_R7():
  1373. n, k = symbols('n k', integer=True, positive=True)
  1374. T = Sum(k**3,(k,1,n)).doit()
  1375. assert T.factor() == n**2*(n + 1)**2/4
  1376. @XFAIL
  1377. def test_R8():
  1378. n, k = symbols('n k', integer=True, positive=True)
  1379. Sm = Sum(k**2*binomial(n, k), (k, 1, n))
  1380. T = Sm.doit() #returns Piecewise function
  1381. assert T.combsimp() == n*(n + 1)*2**(n - 2)
  1382. def test_R9():
  1383. n, k = symbols('n k', integer=True, positive=True)
  1384. Sm = Sum(binomial(n, k - 1)/k, (k, 1, n + 1))
  1385. assert Sm.doit().simplify() == (2**(n + 1) - 1)/(n + 1)
  1386. @XFAIL
  1387. def test_R10():
  1388. n, m, r, k = symbols('n m r k', integer=True, positive=True)
  1389. Sm = Sum(binomial(n, k)*binomial(m, r - k), (k, 0, r))
  1390. T = Sm.doit()
  1391. T2 = T.combsimp().rewrite(factorial)
  1392. assert T2 == factorial(m + n)/(factorial(r)*factorial(m + n - r))
  1393. assert T2 == binomial(m + n, r).rewrite(factorial)
  1394. # rewrite(binomial) is not working.
  1395. # https://github.com/sympy/sympy/issues/7135
  1396. T3 = T2.rewrite(binomial)
  1397. assert T3 == binomial(m + n, r)
  1398. @XFAIL
  1399. def test_R11():
  1400. n, k = symbols('n k', integer=True, positive=True)
  1401. sk = binomial(n, k)*fibonacci(k)
  1402. Sm = Sum(sk, (k, 0, n))
  1403. T = Sm.doit()
  1404. # Fibonacci simplification not implemented
  1405. # https://github.com/sympy/sympy/issues/7134
  1406. assert T == fibonacci(2*n)
  1407. @XFAIL
  1408. def test_R12():
  1409. n, k = symbols('n k', integer=True, positive=True)
  1410. Sm = Sum(fibonacci(k)**2, (k, 0, n))
  1411. T = Sm.doit()
  1412. assert T == fibonacci(n)*fibonacci(n + 1)
  1413. @XFAIL
  1414. def test_R13():
  1415. n, k = symbols('n k', integer=True, positive=True)
  1416. Sm = Sum(sin(k*x), (k, 1, n))
  1417. T = Sm.doit() # Sum is not calculated
  1418. assert T.simplify() == cot(x/2)/2 - cos(x*(2*n + 1)/2)/(2*sin(x/2))
  1419. @XFAIL
  1420. def test_R14():
  1421. n, k = symbols('n k', integer=True, positive=True)
  1422. Sm = Sum(sin((2*k - 1)*x), (k, 1, n))
  1423. T = Sm.doit() # Sum is not calculated
  1424. assert T.simplify() == sin(n*x)**2/sin(x)
  1425. @XFAIL
  1426. def test_R15():
  1427. n, k = symbols('n k', integer=True, positive=True)
  1428. Sm = Sum(binomial(n - k, k), (k, 0, floor(n/2)))
  1429. T = Sm.doit() # Sum is not calculated
  1430. assert T.simplify() == fibonacci(n + 1)
  1431. def test_R16():
  1432. k = symbols('k', integer=True, positive=True)
  1433. Sm = Sum(1/k**2 + 1/k**3, (k, 1, oo))
  1434. assert Sm.doit() == zeta(3) + pi**2/6
  1435. def test_R17():
  1436. k = symbols('k', integer=True, positive=True)
  1437. assert abs(float(Sum(1/k**2 + 1/k**3, (k, 1, oo)))
  1438. - 2.8469909700078206) < 1e-15
  1439. def test_R18():
  1440. k = symbols('k', integer=True, positive=True)
  1441. Sm = Sum(1/(2**k*k**2), (k, 1, oo))
  1442. T = Sm.doit()
  1443. assert T.simplify() == -log(2)**2/2 + pi**2/12
  1444. @slow
  1445. @XFAIL
  1446. def test_R19():
  1447. k = symbols('k', integer=True, positive=True)
  1448. Sm = Sum(1/((3*k + 1)*(3*k + 2)*(3*k + 3)), (k, 0, oo))
  1449. T = Sm.doit()
  1450. # assert fails, T not simplified
  1451. assert T.simplify() == -log(3)/4 + sqrt(3)*pi/12
  1452. @XFAIL
  1453. def test_R20():
  1454. n, k = symbols('n k', integer=True, positive=True)
  1455. Sm = Sum(binomial(n, 4*k), (k, 0, oo))
  1456. T = Sm.doit()
  1457. # assert fails, T not simplified
  1458. assert T.simplify() == 2**(n/2)*cos(pi*n/4)/2 + 2**(n - 1)/2
  1459. @XFAIL
  1460. def test_R21():
  1461. k = symbols('k', integer=True, positive=True)
  1462. Sm = Sum(1/(sqrt(k*(k + 1)) * (sqrt(k) + sqrt(k + 1))), (k, 1, oo))
  1463. T = Sm.doit() # Sum not calculated
  1464. assert T.simplify() == 1
  1465. # test_R22 answer not available in Wester samples
  1466. # Sum(Sum(binomial(n, k)*binomial(n - k, n - 2*k)*x**n*y**(n - 2*k),
  1467. # (k, 0, floor(n/2))), (n, 0, oo)) with abs(x*y)<1?
  1468. @XFAIL
  1469. def test_R23():
  1470. n, k = symbols('n k', integer=True, positive=True)
  1471. Sm = Sum(Sum((factorial(n)/(factorial(k)**2*factorial(n - 2*k)))*
  1472. (x/y)**k*(x*y)**(n - k), (n, 2*k, oo)), (k, 0, oo))
  1473. # Missing how to express constraint abs(x*y)<1?
  1474. T = Sm.doit() # Sum not calculated
  1475. assert T == -1/sqrt(x**2*y**2 - 4*x**2 - 2*x*y + 1)
  1476. def test_R24():
  1477. m, k = symbols('m k', integer=True, positive=True)
  1478. Sm = Sum(Product(k/(2*k - 1), (k, 1, m)), (m, 2, oo))
  1479. assert Sm.doit() == pi/2
  1480. def test_S1():
  1481. k = symbols('k', integer=True, positive=True)
  1482. Pr = Product(gamma(k/3), (k, 1, 8))
  1483. assert Pr.doit().simplify() == 640*sqrt(3)*pi**3/6561
  1484. def test_S2():
  1485. n, k = symbols('n k', integer=True, positive=True)
  1486. assert Product(k, (k, 1, n)).doit() == factorial(n)
  1487. def test_S3():
  1488. n, k = symbols('n k', integer=True, positive=True)
  1489. assert Product(x**k, (k, 1, n)).doit().simplify() == x**(n*(n + 1)/2)
  1490. def test_S4():
  1491. n, k = symbols('n k', integer=True, positive=True)
  1492. assert Product(1 + 1/k, (k, 1, n -1)).doit().simplify() == n
  1493. def test_S5():
  1494. n, k = symbols('n k', integer=True, positive=True)
  1495. assert (Product((2*k - 1)/(2*k), (k, 1, n)).doit().gammasimp() ==
  1496. gamma(n + S.Half)/(sqrt(pi)*gamma(n + 1)))
  1497. @XFAIL
  1498. def test_S6():
  1499. n, k = symbols('n k', integer=True, positive=True)
  1500. # Product does not evaluate
  1501. assert (Product(x**2 -2*x*cos(k*pi/n) + 1, (k, 1, n - 1)).doit().simplify()
  1502. == (x**(2*n) - 1)/(x**2 - 1))
  1503. @XFAIL
  1504. def test_S7():
  1505. k = symbols('k', integer=True, positive=True)
  1506. Pr = Product((k**3 - 1)/(k**3 + 1), (k, 2, oo))
  1507. T = Pr.doit() # Product does not evaluate
  1508. assert T.simplify() == R(2, 3)
  1509. @XFAIL
  1510. def test_S8():
  1511. k = symbols('k', integer=True, positive=True)
  1512. Pr = Product(1 - 1/(2*k)**2, (k, 1, oo))
  1513. T = Pr.doit()
  1514. # Product does not evaluate
  1515. assert T.simplify() == 2/pi
  1516. @XFAIL
  1517. def test_S9():
  1518. k = symbols('k', integer=True, positive=True)
  1519. Pr = Product(1 + (-1)**(k + 1)/(2*k - 1), (k, 1, oo))
  1520. T = Pr.doit()
  1521. # Product produces 0
  1522. # https://github.com/sympy/sympy/issues/7133
  1523. assert T.simplify() == sqrt(2)
  1524. @XFAIL
  1525. def test_S10():
  1526. k = symbols('k', integer=True, positive=True)
  1527. Pr = Product((k*(k + 1) + 1 + I)/(k*(k + 1) + 1 - I), (k, 0, oo))
  1528. T = Pr.doit()
  1529. # Product does not evaluate
  1530. assert T.simplify() == -1
  1531. def test_T1():
  1532. assert limit((1 + 1/n)**n, n, oo) == E
  1533. assert limit((1 - cos(x))/x**2, x, 0) == S.Half
  1534. def test_T2():
  1535. assert limit((3**x + 5**x)**(1/x), x, oo) == 5
  1536. def test_T3():
  1537. assert limit(log(x)/(log(x) + sin(x)), x, oo) == 1
  1538. def test_T4():
  1539. assert limit((exp(x*exp(-x)/(exp(-x) + exp(-2*x**2/(x + 1))))
  1540. - exp(x))/x, x, oo) == -exp(2)
  1541. def test_T5():
  1542. assert limit(x*log(x)*log(x*exp(x) - x**2)**2/log(log(x**2
  1543. + 2*exp(exp(3*x**3*log(x))))), x, oo) == R(1, 3)
  1544. def test_T6():
  1545. assert limit(1/n * factorial(n)**(1/n), n, oo) == exp(-1)
  1546. def test_T7():
  1547. limit(1/n * gamma(n + 1)**(1/n), n, oo)
  1548. def test_T8():
  1549. a, z = symbols('a z', positive=True)
  1550. assert limit(gamma(z + a)/gamma(z)*exp(-a*log(z)), z, oo) == 1
  1551. @XFAIL
  1552. def test_T9():
  1553. z, k = symbols('z k', positive=True)
  1554. # raises NotImplementedError:
  1555. # Don't know how to calculate the mrv of '(1, k)'
  1556. assert limit(hyper((1, k), (1,), z/k), k, oo) == exp(z)
  1557. @XFAIL
  1558. def test_T10():
  1559. # No longer raises PoleError, but should return euler-mascheroni constant
  1560. assert limit(zeta(x) - 1/(x - 1), x, 1) == integrate(-1/x + 1/floor(x), (x, 1, oo))
  1561. @XFAIL
  1562. def test_T11():
  1563. n, k = symbols('n k', integer=True, positive=True)
  1564. # evaluates to 0
  1565. assert limit(n**x/(x*product((1 + x/k), (k, 1, n))), n, oo) == gamma(x)
  1566. def test_T12():
  1567. x, t = symbols('x t', real=True)
  1568. # Does not evaluate the limit but returns an expression with erf
  1569. assert limit(x * integrate(exp(-t**2), (t, 0, x))/(1 - exp(-x**2)),
  1570. x, 0) == 1
  1571. def test_T13():
  1572. x = symbols('x', real=True)
  1573. assert [limit(x/abs(x), x, 0, dir='-'),
  1574. limit(x/abs(x), x, 0, dir='+')] == [-1, 1]
  1575. def test_T14():
  1576. x = symbols('x', real=True)
  1577. assert limit(atan(-log(x)), x, 0, dir='+') == pi/2
  1578. def test_U1():
  1579. x = symbols('x', real=True)
  1580. assert diff(abs(x), x) == sign(x)
  1581. def test_U2():
  1582. f = Lambda(x, Piecewise((-x, x < 0), (x, x >= 0)))
  1583. assert diff(f(x), x) == Piecewise((-1, x < 0), (1, x >= 0))
  1584. def test_U3():
  1585. f = Lambda(x, Piecewise((x**2 - 1, x == 1), (x**3, x != 1)))
  1586. f1 = Lambda(x, diff(f(x), x))
  1587. assert f1(x) == 3*x**2
  1588. assert f1(1) == 3
  1589. @XFAIL
  1590. def test_U4():
  1591. n = symbols('n', integer=True, positive=True)
  1592. x = symbols('x', real=True)
  1593. d = diff(x**n, x, n)
  1594. assert d.rewrite(factorial) == factorial(n)
  1595. def test_U5():
  1596. # issue 6681
  1597. t = symbols('t')
  1598. ans = (
  1599. Derivative(f(g(t)), g(t))*Derivative(g(t), (t, 2)) +
  1600. Derivative(f(g(t)), (g(t), 2))*Derivative(g(t), t)**2)
  1601. assert f(g(t)).diff(t, 2) == ans
  1602. assert ans.doit() == ans
  1603. def test_U6():
  1604. h = Function('h')
  1605. T = integrate(f(y), (y, h(x), g(x)))
  1606. assert T.diff(x) == (
  1607. f(g(x))*Derivative(g(x), x) - f(h(x))*Derivative(h(x), x))
  1608. @XFAIL
  1609. def test_U7():
  1610. p, t = symbols('p t', real=True)
  1611. # Exact differential => d(V(P, T)) => dV/dP DP + dV/dT DT
  1612. # raises ValueError: Since there is more than one variable in the
  1613. # expression, the variable(s) of differentiation must be supplied to
  1614. # differentiate f(p,t)
  1615. diff(f(p, t))
  1616. def test_U8():
  1617. x, y = symbols('x y', real=True)
  1618. eq = cos(x*y) + x
  1619. # If SymPy had implicit_diff() function this hack could be avoided
  1620. # TODO: Replace solve with solveset, current test fails for solveset
  1621. assert idiff(y - eq, y, x) == (-y*sin(x*y) + 1)/(x*sin(x*y) + 1)
  1622. def test_U9():
  1623. # Wester sample case for Maple:
  1624. # O29 := diff(f(x, y), x) + diff(f(x, y), y);
  1625. # /d \ /d \
  1626. # |-- f(x, y)| + |-- f(x, y)|
  1627. # \dx / \dy /
  1628. #
  1629. # O30 := factor(subs(f(x, y) = g(x^2 + y^2), %));
  1630. # 2 2
  1631. # 2 D(g)(x + y ) (x + y)
  1632. x, y = symbols('x y', real=True)
  1633. su = diff(f(x, y), x) + diff(f(x, y), y)
  1634. s2 = su.subs(f(x, y), g(x**2 + y**2))
  1635. s3 = s2.doit().factor()
  1636. # Subs not performed, s3 = 2*(x + y)*Subs(Derivative(
  1637. # g(_xi_1), _xi_1), _xi_1, x**2 + y**2)
  1638. # Derivative(g(x*2 + y**2), x**2 + y**2) is not valid in SymPy,
  1639. # and probably will remain that way. You can take derivatives with respect
  1640. # to other expressions only if they are atomic, like a symbol or a
  1641. # function.
  1642. # D operator should be added to SymPy
  1643. # See https://github.com/sympy/sympy/issues/4719.
  1644. assert s3 == (x + y)*Subs(Derivative(g(x), x), x, x**2 + y**2)*2
  1645. def test_U10():
  1646. # see issue 2519:
  1647. assert residue((z**3 + 5)/((z**4 - 1)*(z + 1)), z, -1) == R(-9, 4)
  1648. @XFAIL
  1649. def test_U11():
  1650. # assert (2*dx + dz) ^ (3*dx + dy + dz) ^ (dx + dy + 4*dz) == 8*dx ^ dy ^dz
  1651. raise NotImplementedError
  1652. @XFAIL
  1653. def test_U12():
  1654. # Wester sample case:
  1655. # (c41) /* d(3 x^5 dy /\ dz + 5 x y^2 dz /\ dx + 8 z dx /\ dy)
  1656. # => (15 x^4 + 10 x y + 8) dx /\ dy /\ dz */
  1657. # factor(ext_diff(3*x^5 * dy ~ dz + 5*x*y^2 * dz ~ dx + 8*z * dx ~ dy));
  1658. # 4
  1659. # (d41) (10 x y + 15 x + 8) dx dy dz
  1660. raise NotImplementedError(
  1661. "External diff of differential form not supported")
  1662. def test_U13():
  1663. assert minimum(x**4 - x + 1, x) == -3*2**R(1,3)/8 + 1
  1664. @XFAIL
  1665. def test_U14():
  1666. #f = 1/(x**2 + y**2 + 1)
  1667. #assert [minimize(f), maximize(f)] == [0,1]
  1668. raise NotImplementedError("minimize(), maximize() not supported")
  1669. @XFAIL
  1670. def test_U15():
  1671. raise NotImplementedError("minimize() not supported and also solve does \
  1672. not support multivariate inequalities")
  1673. @XFAIL
  1674. def test_U16():
  1675. raise NotImplementedError("minimize() not supported in SymPy and also \
  1676. solve does not support multivariate inequalities")
  1677. @XFAIL
  1678. def test_U17():
  1679. raise NotImplementedError("Linear programming, symbolic simplex not \
  1680. supported in SymPy")
  1681. def test_V1():
  1682. x = symbols('x', real=True)
  1683. assert integrate(abs(x), x) == Piecewise((-x**2/2, x <= 0), (x**2/2, True))
  1684. def test_V2():
  1685. assert integrate(Piecewise((-x, x < 0), (x, x >= 0)), x
  1686. ) == Piecewise((-x**2/2, x < 0), (x**2/2, True))
  1687. def test_V3():
  1688. assert integrate(1/(x**3 + 2),x).diff().simplify() == 1/(x**3 + 2)
  1689. def test_V4():
  1690. assert integrate(2**x/sqrt(1 + 4**x), x) == asinh(2**x)/log(2)
  1691. @XFAIL
  1692. def test_V5():
  1693. # Returns (-45*x**2 + 80*x - 41)/(5*sqrt(2*x - 1)*(4*x**2 - 4*x + 1))
  1694. assert (integrate((3*x - 5)**2/(2*x - 1)**R(7, 2), x).simplify() ==
  1695. (-41 + 80*x - 45*x**2)/(5*(2*x - 1)**R(5, 2)))
  1696. @XFAIL
  1697. def test_V6():
  1698. # returns RootSum(40*_z**2 - 1, Lambda(_i, _i*log(-4*_i + exp(-m*x))))/m
  1699. assert (integrate(1/(2*exp(m*x) - 5*exp(-m*x)), x) == sqrt(10)*(
  1700. log(2*exp(m*x) - sqrt(10)) - log(2*exp(m*x) + sqrt(10)))/(20*m))
  1701. def test_V7():
  1702. r1 = integrate(sinh(x)**4/cosh(x)**2)
  1703. assert r1.simplify() == x*R(-3, 2) + sinh(x)**3/(2*cosh(x)) + 3*tanh(x)/2
  1704. @XFAIL
  1705. def test_V8_V9():
  1706. #Macsyma test case:
  1707. #(c27) /* This example involves several symbolic parameters
  1708. # => 1/sqrt(b^2 - a^2) log([sqrt(b^2 - a^2) tan(x/2) + a + b]/
  1709. # [sqrt(b^2 - a^2) tan(x/2) - a - b]) (a^2 < b^2)
  1710. # [Gradshteyn and Ryzhik 2.553(3)] */
  1711. #assume(b^2 > a^2)$
  1712. #(c28) integrate(1/(a + b*cos(x)), x);
  1713. #(c29) trigsimp(ratsimp(diff(%, x)));
  1714. # 1
  1715. #(d29) ------------
  1716. # b cos(x) + a
  1717. raise NotImplementedError(
  1718. "Integrate with assumption not supported")
  1719. def test_V10():
  1720. assert integrate(1/(3 + 3*cos(x) + 4*sin(x)), x) == log(4*tan(x/2) + 3)/4
  1721. def test_V11():
  1722. r1 = integrate(1/(4 + 3*cos(x) + 4*sin(x)), x)
  1723. r2 = factor(r1)
  1724. assert (logcombine(r2, force=True) ==
  1725. log(((tan(x/2) + 1)/(tan(x/2) + 7))**R(1, 3)))
  1726. def test_V12():
  1727. r1 = integrate(1/(5 + 3*cos(x) + 4*sin(x)), x)
  1728. assert r1 == -1/(tan(x/2) + 2)
  1729. @XFAIL
  1730. def test_V13():
  1731. r1 = integrate(1/(6 + 3*cos(x) + 4*sin(x)), x)
  1732. # expression not simplified, returns: -sqrt(11)*I*log(tan(x/2) + 4/3
  1733. # - sqrt(11)*I/3)/11 + sqrt(11)*I*log(tan(x/2) + 4/3 + sqrt(11)*I/3)/11
  1734. assert r1.simplify() == 2*sqrt(11)*atan(sqrt(11)*(3*tan(x/2) + 4)/11)/11
  1735. @slow
  1736. @XFAIL
  1737. def test_V14():
  1738. r1 = integrate(log(abs(x**2 - y**2)), x)
  1739. # Piecewise result does not simplify to the desired result.
  1740. assert (r1.simplify() == x*log(abs(x**2 - y**2))
  1741. + y*log(x + y) - y*log(x - y) - 2*x)
  1742. def test_V15():
  1743. r1 = integrate(x*acot(x/y), x)
  1744. assert simplify(r1 - (x*y + (x**2 + y**2)*acot(x/y))/2) == 0
  1745. @XFAIL
  1746. def test_V16():
  1747. # Integral not calculated
  1748. assert integrate(cos(5*x)*Ci(2*x), x) == Ci(2*x)*sin(5*x)/5 - (Si(3*x) + Si(7*x))/10
  1749. @XFAIL
  1750. def test_V17():
  1751. r1 = integrate((diff(f(x), x)*g(x)
  1752. - f(x)*diff(g(x), x))/(f(x)**2 - g(x)**2), x)
  1753. # integral not calculated
  1754. assert simplify(r1 - (f(x) - g(x))/(f(x) + g(x))/2) == 0
  1755. @XFAIL
  1756. def test_W1():
  1757. # The function has a pole at y.
  1758. # The integral has a Cauchy principal value of zero but SymPy returns -I*pi
  1759. # https://github.com/sympy/sympy/issues/7159
  1760. assert integrate(1/(x - y), (x, y - 1, y + 1)) == 0
  1761. @XFAIL
  1762. def test_W2():
  1763. # The function has a pole at y.
  1764. # The integral is divergent but SymPy returns -2
  1765. # https://github.com/sympy/sympy/issues/7160
  1766. # Test case in Macsyma:
  1767. # (c6) errcatch(integrate(1/(x - a)^2, x, a - 1, a + 1));
  1768. # Integral is divergent
  1769. assert integrate(1/(x - y)**2, (x, y - 1, y + 1)) is zoo
  1770. @XFAIL
  1771. @slow
  1772. def test_W3():
  1773. # integral is not calculated
  1774. # https://github.com/sympy/sympy/issues/7161
  1775. assert integrate(sqrt(x + 1/x - 2), (x, 0, 1)) == R(4, 3)
  1776. @XFAIL
  1777. @slow
  1778. def test_W4():
  1779. # integral is not calculated
  1780. assert integrate(sqrt(x + 1/x - 2), (x, 1, 2)) == -2*sqrt(2)/3 + R(4, 3)
  1781. @XFAIL
  1782. @slow
  1783. def test_W5():
  1784. # integral is not calculated
  1785. assert integrate(sqrt(x + 1/x - 2), (x, 0, 2)) == -2*sqrt(2)/3 + R(8, 3)
  1786. @XFAIL
  1787. @slow
  1788. def test_W6():
  1789. # integral is not calculated
  1790. assert integrate(sqrt(2 - 2*cos(2*x))/2, (x, pi*R(-3, 4), -pi/4)) == sqrt(2)
  1791. def test_W7():
  1792. a = symbols('a', positive=True)
  1793. r1 = integrate(cos(x)/(x**2 + a**2), (x, -oo, oo))
  1794. assert r1.simplify() == pi*exp(-a)/a
  1795. @XFAIL
  1796. def test_W8():
  1797. # Test case in Mathematica:
  1798. # In[19]:= Integrate[t^(a - 1)/(1 + t), {t, 0, Infinity},
  1799. # Assumptions -> 0 < a < 1]
  1800. # Out[19]= Pi Csc[a Pi]
  1801. raise NotImplementedError(
  1802. "Integrate with assumption 0 < a < 1 not supported")
  1803. @XFAIL
  1804. @slow
  1805. def test_W9():
  1806. # Integrand with a residue at infinity => -2 pi [sin(pi/5) + sin(2pi/5)]
  1807. # (principal value) [Levinson and Redheffer, p. 234] *)
  1808. r1 = integrate(5*x**3/(1 + x + x**2 + x**3 + x**4), (x, -oo, oo))
  1809. r2 = r1.doit()
  1810. assert r2 == -2*pi*(sqrt(-sqrt(5)/8 + 5/8) + sqrt(sqrt(5)/8 + 5/8))
  1811. @XFAIL
  1812. def test_W10():
  1813. # integrate(1/[1 + x + x^2 + ... + x^(2 n)], x = -infinity..infinity) =
  1814. # 2 pi/(2 n + 1) [1 + cos(pi/[2 n + 1])] csc(2 pi/[2 n + 1])
  1815. # [Levinson and Redheffer, p. 255] => 2 pi/5 [1 + cos(pi/5)] csc(2 pi/5) */
  1816. r1 = integrate(x/(1 + x + x**2 + x**4), (x, -oo, oo))
  1817. r2 = r1.doit()
  1818. assert r2 == 2*pi*(sqrt(5)/4 + 5/4)*csc(pi*R(2, 5))/5
  1819. @XFAIL
  1820. def test_W11():
  1821. # integral not calculated
  1822. assert (integrate(sqrt(1 - x**2)/(1 + x**2), (x, -1, 1)) ==
  1823. pi*(-1 + sqrt(2)))
  1824. def test_W12():
  1825. p = symbols('p', positive=True)
  1826. q = symbols('q', real=True)
  1827. r1 = integrate(x*exp(-p*x**2 + 2*q*x), (x, -oo, oo))
  1828. assert r1.simplify() == sqrt(pi)*q*exp(q**2/p)/p**R(3, 2)
  1829. @XFAIL
  1830. def test_W13():
  1831. # Integral not calculated. Expected result is 2*(Euler_mascheroni_constant)
  1832. r1 = integrate(1/log(x) + 1/(1 - x) - log(log(1/x)), (x, 0, 1))
  1833. assert r1 == 2*EulerGamma
  1834. def test_W14():
  1835. assert integrate(sin(x)/x*exp(2*I*x), (x, -oo, oo)) == 0
  1836. @XFAIL
  1837. def test_W15():
  1838. # integral not calculated
  1839. assert integrate(log(gamma(x))*cos(6*pi*x), (x, 0, 1)) == R(1, 12)
  1840. def test_W16():
  1841. assert integrate((1 + x)**3*legendre_poly(1, x)*legendre_poly(2, x),
  1842. (x, -1, 1)) == R(36, 35)
  1843. def test_W17():
  1844. a, b = symbols('a b', positive=True)
  1845. assert integrate(exp(-a*x)*besselj(0, b*x),
  1846. (x, 0, oo)) == 1/(b*sqrt(a**2/b**2 + 1))
  1847. def test_W18():
  1848. assert integrate((besselj(1, x)/x)**2, (x, 0, oo)) == 4/(3*pi)
  1849. @XFAIL
  1850. def test_W19():
  1851. # Integral not calculated
  1852. # Expected result is (cos 7 - 1)/7 [Gradshteyn and Ryzhik 6.782(3)]
  1853. assert integrate(Ci(x)*besselj(0, 2*sqrt(7*x)), (x, 0, oo)) == (cos(7) - 1)/7
  1854. @XFAIL
  1855. def test_W20():
  1856. # integral not calculated
  1857. assert (integrate(x**2*polylog(3, 1/(x + 1)), (x, 0, 1)) ==
  1858. -pi**2/36 - R(17, 108) + zeta(3)/4 +
  1859. (-pi**2/2 - 4*log(2) + log(2)**2 + 35/3)*log(2)/9)
  1860. def test_W21():
  1861. assert abs(N(integrate(x**2*polylog(3, 1/(x + 1)), (x, 0, 1)))
  1862. - 0.210882859565594) < 1e-15
  1863. def test_W22():
  1864. t, u = symbols('t u', real=True)
  1865. s = Lambda(x, Piecewise((1, And(x >= 1, x <= 2)), (0, True)))
  1866. assert integrate(s(t)*cos(t), (t, 0, u)) == Piecewise(
  1867. (0, u < 0),
  1868. (-sin(Min(1, u)) + sin(Min(2, u)), True))
  1869. @slow
  1870. def test_W23():
  1871. a, b = symbols('a b', positive=True)
  1872. r1 = integrate(integrate(x/(x**2 + y**2), (x, a, b)), (y, -oo, oo))
  1873. assert r1.collect(pi).cancel() == -pi*a + pi*b
  1874. def test_W23b():
  1875. # like W23 but limits are reversed
  1876. a, b = symbols('a b', positive=True)
  1877. r2 = integrate(integrate(x/(x**2 + y**2), (y, -oo, oo)), (x, a, b))
  1878. assert r2.collect(pi) == pi*(-a + b)
  1879. @XFAIL
  1880. @slow
  1881. def test_W24():
  1882. if ON_CI:
  1883. skip("Too slow for CI.")
  1884. # Not that slow, but does not fully evaluate so simplify is slow.
  1885. # Maybe also require doit()
  1886. x, y = symbols('x y', real=True)
  1887. r1 = integrate(integrate(sqrt(x**2 + y**2), (x, 0, 1)), (y, 0, 1))
  1888. assert (r1 - (sqrt(2) + asinh(1))/3).simplify() == 0
  1889. @XFAIL
  1890. @slow
  1891. def test_W25():
  1892. if ON_CI:
  1893. skip("Too slow for CI.")
  1894. a, x, y = symbols('a x y', real=True)
  1895. i1 = integrate(
  1896. sin(a)*sin(y)/sqrt(1 - sin(a)**2*sin(x)**2*sin(y)**2),
  1897. (x, 0, pi/2))
  1898. i2 = integrate(i1, (y, 0, pi/2))
  1899. assert (i2 - pi*a/2).simplify() == 0
  1900. def test_W26():
  1901. x, y = symbols('x y', real=True)
  1902. assert integrate(integrate(abs(y - x**2), (y, 0, 2)),
  1903. (x, -1, 1)) == R(46, 15)
  1904. def test_W27():
  1905. a, b, c = symbols('a b c')
  1906. assert integrate(integrate(integrate(1, (z, 0, c*(1 - x/a - y/b))),
  1907. (y, 0, b*(1 - x/a))),
  1908. (x, 0, a)) == a*b*c/6
  1909. def test_X1():
  1910. v, c = symbols('v c', real=True)
  1911. assert (series(1/sqrt(1 - (v/c)**2), v, x0=0, n=8) ==
  1912. 5*v**6/(16*c**6) + 3*v**4/(8*c**4) + v**2/(2*c**2) + 1 + O(v**8))
  1913. def test_X2():
  1914. v, c = symbols('v c', real=True)
  1915. s1 = series(1/sqrt(1 - (v/c)**2), v, x0=0, n=8)
  1916. assert (1/s1**2).series(v, x0=0, n=8) == -v**2/c**2 + 1 + O(v**8)
  1917. def test_X3():
  1918. s1 = (sin(x).series()/cos(x).series()).series()
  1919. s2 = tan(x).series()
  1920. assert s2 == x + x**3/3 + 2*x**5/15 + O(x**6)
  1921. assert s1 == s2
  1922. def test_X4():
  1923. s1 = log(sin(x)/x).series()
  1924. assert s1 == -x**2/6 - x**4/180 + O(x**6)
  1925. assert log(series(sin(x)/x)).series() == s1
  1926. @XFAIL
  1927. def test_X5():
  1928. # test case in Mathematica syntax:
  1929. # In[21]:= (* => [a f'(a d) + g(b d) + integrate(h(c y), y = 0..d)]
  1930. # + [a^2 f''(a d) + b g'(b d) + h(c d)] (x - d) *)
  1931. # In[22]:= D[f[a*x], x] + g[b*x] + Integrate[h[c*y], {y, 0, x}]
  1932. # Out[22]= g[b x] + Integrate[h[c y], {y, 0, x}] + a f'[a x]
  1933. # In[23]:= Series[%, {x, d, 1}]
  1934. # Out[23]= (g[b d] + Integrate[h[c y], {y, 0, d}] + a f'[a d]) +
  1935. # 2 2
  1936. # (h[c d] + b g'[b d] + a f''[a d]) (-d + x) + O[-d + x]
  1937. h = Function('h')
  1938. a, b, c, d = symbols('a b c d', real=True)
  1939. # series() raises NotImplementedError:
  1940. # The _eval_nseries method should be added to <class
  1941. # 'sympy.core.function.Subs'> to give terms up to O(x**n) at x=0
  1942. series(diff(f(a*x), x) + g(b*x) + integrate(h(c*y), (y, 0, x)),
  1943. x, x0=d, n=2)
  1944. # assert missing, until exception is removed
  1945. def test_X6():
  1946. # Taylor series of nonscalar objects (noncommutative multiplication)
  1947. # expected result => (B A - A B) t^2/2 + O(t^3) [Stanly Steinberg]
  1948. a, b = symbols('a b', commutative=False, scalar=False)
  1949. assert (series(exp((a + b)*x) - exp(a*x) * exp(b*x), x, x0=0, n=3) ==
  1950. x**2*(-a*b/2 + b*a/2) + O(x**3))
  1951. def test_X7():
  1952. # => sum( Bernoulli[k]/k! x^(k - 2), k = 1..infinity )
  1953. # = 1/x^2 - 1/(2 x) + 1/12 - x^2/720 + x^4/30240 + O(x^6)
  1954. # [Levinson and Redheffer, p. 173]
  1955. assert (series(1/(x*(exp(x) - 1)), x, 0, 7) == x**(-2) - 1/(2*x) +
  1956. R(1, 12) - x**2/720 + x**4/30240 - x**6/1209600 + O(x**7))
  1957. def test_X8():
  1958. # Puiseux series (terms with fractional degree):
  1959. # => 1/sqrt(x - 3/2 pi) + (x - 3/2 pi)^(3/2) / 12 + O([x - 3/2 pi]^(7/2))
  1960. # see issue 7167:
  1961. x = symbols('x', real=True)
  1962. assert (series(sqrt(sec(x)), x, x0=pi*3/2, n=4) ==
  1963. 1/sqrt(x - pi*R(3, 2)) + (x - pi*R(3, 2))**R(3, 2)/12 +
  1964. (x - pi*R(3, 2))**R(7, 2)/160 + O((x - pi*R(3, 2))**4, (x, pi*R(3, 2))))
  1965. def test_X9():
  1966. assert (series(x**x, x, x0=0, n=4) == 1 + x*log(x) + x**2*log(x)**2/2 +
  1967. x**3*log(x)**3/6 + O(x**4*log(x)**4))
  1968. def test_X10():
  1969. z, w = symbols('z w')
  1970. assert (series(log(sinh(z)) + log(cosh(z + w)), z, x0=0, n=2) ==
  1971. log(cosh(w)) + log(z) + z*sinh(w)/cosh(w) + O(z**2))
  1972. def test_X11():
  1973. z, w = symbols('z w')
  1974. assert (series(log(sinh(z) * cosh(z + w)), z, x0=0, n=2) ==
  1975. log(cosh(w)) + log(z) + z*sinh(w)/cosh(w) + O(z**2))
  1976. @XFAIL
  1977. def test_X12():
  1978. # Look at the generalized Taylor series around x = 1
  1979. # Result => (x - 1)^a/e^b [1 - (a + 2 b) (x - 1) / 2 + O((x - 1)^2)]
  1980. a, b, x = symbols('a b x', real=True)
  1981. # series returns O(log(x-1)**2)
  1982. # https://github.com/sympy/sympy/issues/7168
  1983. assert (series(log(x)**a*exp(-b*x), x, x0=1, n=2) ==
  1984. (x - 1)**a/exp(b)*(1 - (a + 2*b)*(x - 1)/2 + O((x - 1)**2)))
  1985. def test_X13():
  1986. assert series(sqrt(2*x**2 + 1), x, x0=oo, n=1) == sqrt(2)*x + O(1/x, (x, oo))
  1987. @XFAIL
  1988. def test_X14():
  1989. # Wallis' product => 1/sqrt(pi n) + ... [Knopp, p. 385]
  1990. assert series(1/2**(2*n)*binomial(2*n, n),
  1991. n, x==oo, n=1) == 1/(sqrt(pi)*sqrt(n)) + O(1/x, (x, oo))
  1992. @SKIP("https://github.com/sympy/sympy/issues/7164")
  1993. def test_X15():
  1994. # => 0!/x - 1!/x^2 + 2!/x^3 - 3!/x^4 + O(1/x^5) [Knopp, p. 544]
  1995. x, t = symbols('x t', real=True)
  1996. # raises RuntimeError: maximum recursion depth exceeded
  1997. # https://github.com/sympy/sympy/issues/7164
  1998. # 2019-02-17: Raises
  1999. # PoleError:
  2000. # Asymptotic expansion of Ei around [-oo] is not implemented.
  2001. e1 = integrate(exp(-t)/t, (t, x, oo))
  2002. assert (series(e1, x, x0=oo, n=5) ==
  2003. 6/x**4 + 2/x**3 - 1/x**2 + 1/x + O(x**(-5), (x, oo)))
  2004. def test_X16():
  2005. # Multivariate Taylor series expansion => 1 - (x^2 + 2 x y + y^2)/2 + O(x^4)
  2006. assert (series(cos(x + y), x + y, x0=0, n=4) == 1 - (x + y)**2/2 +
  2007. O(x**4 + x**3*y + x**2*y**2 + x*y**3 + y**4, x, y))
  2008. @XFAIL
  2009. def test_X17():
  2010. # Power series (compute the general formula)
  2011. # (c41) powerseries(log(sin(x)/x), x, 0);
  2012. # /aquarius/data2/opt/local/macsyma_422/library1/trgred.so being loaded.
  2013. # inf
  2014. # ==== i1 2 i1 2 i1
  2015. # \ (- 1) 2 bern(2 i1) x
  2016. # (d41) > ------------------------------
  2017. # / 2 i1 (2 i1)!
  2018. # ====
  2019. # i1 = 1
  2020. # fps does not calculate
  2021. assert fps(log(sin(x)/x)) == \
  2022. Sum((-1)**k*2**(2*k - 1)*bernoulli(2*k)*x**(2*k)/(k*factorial(2*k)), (k, 1, oo))
  2023. @XFAIL
  2024. def test_X18():
  2025. # Power series (compute the general formula). Maple FPS:
  2026. # > FormalPowerSeries(exp(-x)*sin(x), x = 0);
  2027. # infinity
  2028. # ----- (1/2 k) k
  2029. # \ 2 sin(3/4 k Pi) x
  2030. # ) -------------------------
  2031. # / k!
  2032. # -----
  2033. #
  2034. # Now, SymPy returns
  2035. # oo
  2036. # _____
  2037. # \ `
  2038. # \ / k k\
  2039. # \ k |I*(-1 - I) I*(-1 + I) |
  2040. # \ x *|----------- - -----------|
  2041. # / \ 2 2 /
  2042. # / ------------------------------
  2043. # / k!
  2044. # /____,
  2045. # k = 0
  2046. k = Dummy('k')
  2047. assert fps(exp(-x)*sin(x)) == \
  2048. Sum(2**(S.Half*k)*sin(R(3, 4)*k*pi)*x**k/factorial(k), (k, 0, oo))
  2049. @XFAIL
  2050. def test_X19():
  2051. # (c45) /* Derive an explicit Taylor series solution of y as a function of
  2052. # x from the following implicit relation:
  2053. # y = x - 1 + (x - 1)^2/2 + 2/3 (x - 1)^3 + (x - 1)^4 +
  2054. # 17/10 (x - 1)^5 + ...
  2055. # */
  2056. # x = sin(y) + cos(y);
  2057. # Time= 0 msecs
  2058. # (d45) x = sin(y) + cos(y)
  2059. #
  2060. # (c46) taylor_revert(%, y, 7);
  2061. raise NotImplementedError("Solve using series not supported. \
  2062. Inverse Taylor series expansion also not supported")
  2063. @XFAIL
  2064. def test_X20():
  2065. # Pade (rational function) approximation => (2 - x)/(2 + x)
  2066. # > numapprox[pade](exp(-x), x = 0, [1, 1]);
  2067. # bytes used=9019816, alloc=3669344, time=13.12
  2068. # 1 - 1/2 x
  2069. # ---------
  2070. # 1 + 1/2 x
  2071. # mpmath support numeric Pade approximant but there is
  2072. # no symbolic implementation in SymPy
  2073. # https://en.wikipedia.org/wiki/Pad%C3%A9_approximant
  2074. raise NotImplementedError("Symbolic Pade approximant not supported")
  2075. def test_X21():
  2076. """
  2077. Test whether `fourier_series` of x periodical on the [-p, p] interval equals
  2078. `- (2 p / pi) sum( (-1)^n / n sin(n pi x / p), n = 1..infinity )`.
  2079. """
  2080. p = symbols('p', positive=True)
  2081. n = symbols('n', positive=True, integer=True)
  2082. s = fourier_series(x, (x, -p, p))
  2083. # All cosine coefficients are equal to 0
  2084. assert s.an.formula == 0
  2085. # Check for sine coefficients
  2086. assert s.bn.formula.subs(s.bn.variables[0], 0) == 0
  2087. assert s.bn.formula.subs(s.bn.variables[0], n) == \
  2088. -2*p/pi * (-1)**n / n * sin(n*pi*x/p)
  2089. @XFAIL
  2090. def test_X22():
  2091. # (c52) /* => p / 2
  2092. # - (2 p / pi^2) sum( [1 - (-1)^n] cos(n pi x / p) / n^2,
  2093. # n = 1..infinity ) */
  2094. # fourier_series(abs(x), x, p);
  2095. # p
  2096. # (e52) a = -
  2097. # 0 2
  2098. #
  2099. # %nn
  2100. # (2 (- 1) - 2) p
  2101. # (e53) a = ------------------
  2102. # %nn 2 2
  2103. # %pi %nn
  2104. #
  2105. # (e54) b = 0
  2106. # %nn
  2107. #
  2108. # Time= 5290 msecs
  2109. # inf %nn %pi %nn x
  2110. # ==== (2 (- 1) - 2) cos(---------)
  2111. # \ p
  2112. # p > -------------------------------
  2113. # / 2
  2114. # ==== %nn
  2115. # %nn = 1 p
  2116. # (d54) ----------------------------------------- + -
  2117. # 2 2
  2118. # %pi
  2119. raise NotImplementedError("Fourier series not supported")
  2120. def test_Y1():
  2121. t = symbols('t', positive=True)
  2122. w = symbols('w', real=True)
  2123. s = symbols('s')
  2124. F, _, _ = laplace_transform(cos((w - 1)*t), t, s)
  2125. assert F == s/(s**2 + (w - 1)**2)
  2126. def test_Y2():
  2127. t = symbols('t', positive=True)
  2128. w = symbols('w', real=True)
  2129. s = symbols('s')
  2130. f = inverse_laplace_transform(s/(s**2 + (w - 1)**2), s, t, simplify=True)
  2131. assert f == cos(t*(w - 1))
  2132. def test_Y3():
  2133. t = symbols('t', positive=True)
  2134. w = symbols('w', real=True)
  2135. s = symbols('s')
  2136. F, _, _ = laplace_transform(sinh(w*t)*cosh(w*t), t, s, simplify=True)
  2137. assert F == w/(s**2 - 4*w**2)
  2138. def test_Y4():
  2139. t = symbols('t', positive=True)
  2140. s = symbols('s')
  2141. F, _, _ = laplace_transform(erf(3/sqrt(t)), t, s, simplify=True)
  2142. assert F == 1/s - exp(-6*sqrt(s))/s
  2143. def test_Y5_Y6():
  2144. # Solve y'' + y = 4 [H(t - 1) - H(t - 2)], y(0) = 1, y'(0) = 0 where H is the
  2145. # Heaviside (unit step) function (the RHS describes a pulse of magnitude 4 and
  2146. # duration 1). See David A. Sanchez, Richard C. Allen, Jr. and Walter T.
  2147. # Kyner, _Differential Equations: An Introduction_, Addison-Wesley Publishing
  2148. # Company, 1983, p. 211. First, take the Laplace transform of the ODE
  2149. # => s^2 Y(s) - s + Y(s) = 4/s [e^(-s) - e^(-2 s)]
  2150. # where Y(s) is the Laplace transform of y(t)
  2151. t = symbols('t', positive=True)
  2152. s = symbols('s')
  2153. y = Function('y')
  2154. F, _, _ = laplace_transform(diff(y(t), t, 2) + y(t)
  2155. - 4*(Heaviside(t - 1) - Heaviside(t - 2)),
  2156. t, s, simplify=True)
  2157. D = (F - (s**2*LaplaceTransform(y(t), t, s) - s*y(0) +
  2158. LaplaceTransform(y(t), t, s) - Subs(Derivative(y(t), t), t, 0) +
  2159. 4*(1 - exp(s))*exp(-2*s)/s)).simplify(doit=False)
  2160. assert D == 0
  2161. # TODO implement second part of test case
  2162. # Now, solve for Y(s) and then take the inverse Laplace transform
  2163. # => Y(s) = s/(s^2 + 1) + 4 [1/s - s/(s^2 + 1)] [e^(-s) - e^(-2 s)]
  2164. # => y(t) = cos t + 4 {[1 - cos(t - 1)] H(t - 1) - [1 - cos(t - 2)] H(t - 2)}
  2165. @XFAIL
  2166. def test_Y7():
  2167. # What is the Laplace transform of an infinite square wave?
  2168. # => 1/s + 2 sum( (-1)^n e^(- s n a)/s, n = 1..infinity )
  2169. # [Sanchez, Allen and Kyner, p. 213]
  2170. t = symbols('t', positive=True)
  2171. a = symbols('a', real=True)
  2172. s = symbols('s')
  2173. F, _, _ = laplace_transform(1 + 2*Sum((-1)**n*Heaviside(t - n*a),
  2174. (n, 1, oo)), t, s)
  2175. # returns 2*LaplaceTransform(Sum((-1)**n*Heaviside(-a*n + t),
  2176. # (n, 1, oo)), t, s) + 1/s
  2177. # https://github.com/sympy/sympy/issues/7177
  2178. assert F == 2*Sum((-1)**n*exp(-a*n*s)/s, (n, 1, oo)) + 1/s
  2179. @XFAIL
  2180. def test_Y8():
  2181. assert fourier_transform(1, x, z) == DiracDelta(z)
  2182. def test_Y9():
  2183. assert (fourier_transform(exp(-9*x**2), x, z) ==
  2184. sqrt(pi)*exp(-pi**2*z**2/9)/3)
  2185. def test_Y10():
  2186. assert (fourier_transform(abs(x)*exp(-3*abs(x)), x, z).cancel() ==
  2187. (-8*pi**2*z**2 + 18)/(16*pi**4*z**4 + 72*pi**2*z**2 + 81))
  2188. @SKIP("https://github.com/sympy/sympy/issues/7181")
  2189. @slow
  2190. def test_Y11():
  2191. # => pi cot(pi s) (0 < Re s < 1) [Gradshteyn and Ryzhik 17.43(5)]
  2192. x, s = symbols('x s')
  2193. # raises RuntimeError: maximum recursion depth exceeded
  2194. # https://github.com/sympy/sympy/issues/7181
  2195. # Update 2019-02-17 raises:
  2196. # TypeError: cannot unpack non-iterable MellinTransform object
  2197. F, _, _ = mellin_transform(1/(1 - x), x, s)
  2198. assert F == pi*cot(pi*s)
  2199. @XFAIL
  2200. def test_Y12():
  2201. # => 2^(s - 4) gamma(s/2)/gamma(4 - s/2) (0 < Re s < 1)
  2202. # [Gradshteyn and Ryzhik 17.43(16)]
  2203. x, s = symbols('x s')
  2204. # returns Wrong value -2**(s - 4)*gamma(s/2 - 3)/gamma(-s/2 + 1)
  2205. # https://github.com/sympy/sympy/issues/7182
  2206. F, _, _ = mellin_transform(besselj(3, x)/x**3, x, s)
  2207. assert F == -2**(s - 4)*gamma(s/2)/gamma(-s/2 + 4)
  2208. @XFAIL
  2209. def test_Y13():
  2210. # Z[H(t - m T)] => z/[z^m (z - 1)] (H is the Heaviside (unit step) function) z
  2211. raise NotImplementedError("z-transform not supported")
  2212. @XFAIL
  2213. def test_Y14():
  2214. # Z[H(t - m T)] => z/[z^m (z - 1)] (H is the Heaviside (unit step) function)
  2215. raise NotImplementedError("z-transform not supported")
  2216. def test_Z1():
  2217. r = Function('r')
  2218. assert (rsolve(r(n + 2) - 2*r(n + 1) + r(n) - 2, r(n),
  2219. {r(0): 1, r(1): m}).simplify() == n**2 + n*(m - 2) + 1)
  2220. def test_Z2():
  2221. r = Function('r')
  2222. assert (rsolve(r(n) - (5*r(n - 1) - 6*r(n - 2)), r(n), {r(0): 0, r(1): 1})
  2223. == -2**n + 3**n)
  2224. def test_Z3():
  2225. # => r(n) = Fibonacci[n + 1] [Cohen, p. 83]
  2226. r = Function('r')
  2227. # recurrence solution is correct, Wester expects it to be simplified to
  2228. # fibonacci(n+1), but that is quite hard
  2229. expected = ((S(1)/2 - sqrt(5)/2)**n*(S(1)/2 - sqrt(5)/10)
  2230. + (S(1)/2 + sqrt(5)/2)**n*(sqrt(5)/10 + S(1)/2))
  2231. sol = rsolve(r(n) - (r(n - 1) + r(n - 2)), r(n), {r(1): 1, r(2): 2})
  2232. assert sol == expected
  2233. @XFAIL
  2234. def test_Z4():
  2235. # => [c^(n+1) [c^(n+1) - 2 c - 2] + (n+1) c^2 + 2 c - n] / [(c-1)^3 (c+1)]
  2236. # [Joan Z. Yu and Robert Israel in sci.math.symbolic]
  2237. r = Function('r')
  2238. c = symbols('c')
  2239. # raises ValueError: Polynomial or rational function expected,
  2240. # got '(c**2 - c**n)/(c - c**n)
  2241. s = rsolve(r(n) - ((1 + c - c**(n-1) - c**(n+1))/(1 - c**n)*r(n - 1)
  2242. - c*(1 - c**(n-2))/(1 - c**(n-1))*r(n - 2) + 1),
  2243. r(n), {r(1): 1, r(2): (2 + 2*c + c**2)/(1 + c)})
  2244. assert (s - (c*(n + 1)*(c*(n + 1) - 2*c - 2) +
  2245. (n + 1)*c**2 + 2*c - n)/((c-1)**3*(c+1)) == 0)
  2246. @XFAIL
  2247. def test_Z5():
  2248. # Second order ODE with initial conditions---solve directly
  2249. # transform: f(t) = sin(2 t)/8 - t cos(2 t)/4
  2250. C1, C2 = symbols('C1 C2')
  2251. # initial conditions not supported, this is a manual workaround
  2252. # https://github.com/sympy/sympy/issues/4720
  2253. eq = Derivative(f(x), x, 2) + 4*f(x) - sin(2*x)
  2254. sol = dsolve(eq, f(x))
  2255. f0 = Lambda(x, sol.rhs)
  2256. assert f0(x) == C2*sin(2*x) + (C1 - x/4)*cos(2*x)
  2257. f1 = Lambda(x, diff(f0(x), x))
  2258. # TODO: Replace solve with solveset, when it works for solveset
  2259. const_dict = solve((f0(0), f1(0)))
  2260. result = f0(x).subs(C1, const_dict[C1]).subs(C2, const_dict[C2])
  2261. assert result == -x*cos(2*x)/4 + sin(2*x)/8
  2262. # Result is OK, but ODE solving with initial conditions should be
  2263. # supported without all this manual work
  2264. raise NotImplementedError('ODE solving with initial conditions \
  2265. not supported')
  2266. @XFAIL
  2267. def test_Z6():
  2268. # Second order ODE with initial conditions---solve using Laplace
  2269. # transform: f(t) = sin(2 t)/8 - t cos(2 t)/4
  2270. t = symbols('t', positive=True)
  2271. s = symbols('s')
  2272. eq = Derivative(f(t), t, 2) + 4*f(t) - sin(2*t)
  2273. F, _, _ = laplace_transform(eq, t, s)
  2274. # Laplace transform for diff() not calculated
  2275. # https://github.com/sympy/sympy/issues/7176
  2276. assert (F == s**2*LaplaceTransform(f(t), t, s) +
  2277. 4*LaplaceTransform(f(t), t, s) - 2/(s**2 + 4))
  2278. # rest of test case not implemented