test_systems.py 126 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560
  1. from sympy.core.function import (Derivative, Function, diff)
  2. from sympy.core.mul import Mul
  3. from sympy.core.numbers import (I, Rational, pi)
  4. from sympy.core.relational import Eq
  5. from sympy.core.singleton import S
  6. from sympy.core.symbol import (Symbol, symbols)
  7. from sympy.functions.elementary.hyperbolic import sinh
  8. from sympy.functions.elementary.miscellaneous import sqrt
  9. from sympy.matrices.dense import Matrix
  10. from sympy.core.containers import Tuple
  11. from sympy.functions import exp, cos, sin, log, Ci, Si, erf, erfi
  12. from sympy.matrices import dotprodsimp, NonSquareMatrixError
  13. from sympy.solvers.ode import dsolve
  14. from sympy.solvers.ode.ode import constant_renumber
  15. from sympy.solvers.ode.subscheck import checksysodesol
  16. from sympy.solvers.ode.systems import (_classify_linear_system, linear_ode_to_matrix,
  17. ODEOrderError, ODENonlinearError, _simpsol,
  18. _is_commutative_anti_derivative, linodesolve,
  19. canonical_odes, dsolve_system, _component_division,
  20. _eqs2dict, _dict2graph)
  21. from sympy.functions import airyai, airybi
  22. from sympy.integrals.integrals import Integral
  23. from sympy.simplify.ratsimp import ratsimp
  24. from sympy.testing.pytest import ON_CI, raises, slow, skip, XFAIL
  25. C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10 = symbols('C0:11')
  26. x = symbols('x')
  27. f = Function('f')
  28. g = Function('g')
  29. h = Function('h')
  30. def test_linear_ode_to_matrix():
  31. f, g, h = symbols("f, g, h", cls=Function)
  32. t = Symbol("t")
  33. funcs = [f(t), g(t), h(t)]
  34. f1 = f(t).diff(t)
  35. g1 = g(t).diff(t)
  36. h1 = h(t).diff(t)
  37. f2 = f(t).diff(t, 2)
  38. g2 = g(t).diff(t, 2)
  39. h2 = h(t).diff(t, 2)
  40. eqs_1 = [Eq(f1, g(t)), Eq(g1, f(t))]
  41. sol_1 = ([Matrix([[1, 0], [0, 1]]), Matrix([[ 0, 1], [1, 0]])], Matrix([[0],[0]]))
  42. assert linear_ode_to_matrix(eqs_1, funcs[:-1], t, 1) == sol_1
  43. eqs_2 = [Eq(f1, f(t) + 2*g(t)), Eq(g1, h(t)), Eq(h1, g(t) + h(t) + f(t))]
  44. sol_2 = ([Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]]), Matrix([[1, 2, 0], [ 0, 0, 1], [1, 1, 1]])],
  45. Matrix([[0], [0], [0]]))
  46. assert linear_ode_to_matrix(eqs_2, funcs, t, 1) == sol_2
  47. eqs_3 = [Eq(2*f1 + 3*h1, f(t) + g(t)), Eq(4*h1 + 5*g1, f(t) + h(t)), Eq(5*f1 + 4*g1, g(t) + h(t))]
  48. sol_3 = ([Matrix([[2, 0, 3], [0, 5, 4], [5, 4, 0]]), Matrix([[1, 1, 0], [1, 0, 1], [0, 1, 1]])],
  49. Matrix([[0], [0], [0]]))
  50. assert linear_ode_to_matrix(eqs_3, funcs, t, 1) == sol_3
  51. eqs_4 = [Eq(f2 + h(t), f1 + g(t)), Eq(2*h2 + g2 + g1 + g(t), 0), Eq(3*h1, 4)]
  52. sol_4 = ([Matrix([[1, 0, 0], [0, 1, 2], [0, 0, 0]]), Matrix([[1, 0, 0], [0, -1, 0], [0, 0, -3]]),
  53. Matrix([[0, 1, -1], [0, -1, 0], [0, 0, 0]])], Matrix([[0], [0], [4]]))
  54. assert linear_ode_to_matrix(eqs_4, funcs, t, 2) == sol_4
  55. eqs_5 = [Eq(f2, g(t)), Eq(f1 + g1, f(t))]
  56. raises(ODEOrderError, lambda: linear_ode_to_matrix(eqs_5, funcs[:-1], t, 1))
  57. eqs_6 = [Eq(f1, f(t)**2), Eq(g1, f(t) + g(t))]
  58. raises(ODENonlinearError, lambda: linear_ode_to_matrix(eqs_6, funcs[:-1], t, 1))
  59. def test__classify_linear_system():
  60. x, y, z, w = symbols('x, y, z, w', cls=Function)
  61. t, k, l = symbols('t k l')
  62. x1 = diff(x(t), t)
  63. y1 = diff(y(t), t)
  64. z1 = diff(z(t), t)
  65. w1 = diff(w(t), t)
  66. x2 = diff(x(t), t, t)
  67. y2 = diff(y(t), t, t)
  68. funcs = [x(t), y(t)]
  69. funcs_2 = funcs + [z(t), w(t)]
  70. eqs_1 = (5 * x1 + 12 * x(t) - 6 * (y(t)), (2 * y1 - 11 * t * x(t) + 3 * y(t) + t))
  71. assert _classify_linear_system(eqs_1, funcs, t) is None
  72. eqs_2 = (5 * (x1**2) + 12 * x(t) - 6 * (y(t)), (2 * y1 - 11 * t * x(t) + 3 * y(t) + t))
  73. sol2 = {'is_implicit': True,
  74. 'canon_eqs': [[Eq(Derivative(x(t), t), -sqrt(-12*x(t)/5 + 6*y(t)/5)),
  75. Eq(Derivative(y(t), t), 11*t*x(t)/2 - t/2 - 3*y(t)/2)],
  76. [Eq(Derivative(x(t), t), sqrt(-12*x(t)/5 + 6*y(t)/5)),
  77. Eq(Derivative(y(t), t), 11*t*x(t)/2 - t/2 - 3*y(t)/2)]]}
  78. assert _classify_linear_system(eqs_2, funcs, t) == sol2
  79. eqs_2_1 = [Eq(Derivative(x(t), t), -sqrt(-12*x(t)/5 + 6*y(t)/5)),
  80. Eq(Derivative(y(t), t), 11*t*x(t)/2 - t/2 - 3*y(t)/2)]
  81. assert _classify_linear_system(eqs_2_1, funcs, t) is None
  82. eqs_2_2 = [Eq(Derivative(x(t), t), sqrt(-12*x(t)/5 + 6*y(t)/5)),
  83. Eq(Derivative(y(t), t), 11*t*x(t)/2 - t/2 - 3*y(t)/2)]
  84. assert _classify_linear_system(eqs_2_2, funcs, t) is None
  85. eqs_3 = (5 * x1 + 12 * x(t) - 6 * (y(t)), (2 * y1 - 11 * x(t) + 3 * y(t)), (5 * w1 + z(t)), (z1 + w(t)))
  86. answer_3 = {'no_of_equation': 4,
  87. 'eq': (12*x(t) - 6*y(t) + 5*Derivative(x(t), t),
  88. -11*x(t) + 3*y(t) + 2*Derivative(y(t), t),
  89. z(t) + 5*Derivative(w(t), t),
  90. w(t) + Derivative(z(t), t)),
  91. 'func': [x(t), y(t), z(t), w(t)],
  92. 'order': {x(t): 1, y(t): 1, z(t): 1, w(t): 1},
  93. 'is_linear': True,
  94. 'is_constant': True,
  95. 'is_homogeneous': True,
  96. 'func_coeff': -Matrix([
  97. [Rational(12, 5), Rational(-6, 5), 0, 0],
  98. [Rational(-11, 2), Rational(3, 2), 0, 0],
  99. [0, 0, 0, 1],
  100. [0, 0, Rational(1, 5), 0]]),
  101. 'type_of_equation': 'type1',
  102. 'is_general': True}
  103. assert _classify_linear_system(eqs_3, funcs_2, t) == answer_3
  104. eqs_4 = (5 * x1 + 12 * x(t) - 6 * (y(t)), (2 * y1 - 11 * x(t) + 3 * y(t)), (z1 - w(t)), (w1 - z(t)))
  105. answer_4 = {'no_of_equation': 4,
  106. 'eq': (12 * x(t) - 6 * y(t) + 5 * Derivative(x(t), t),
  107. -11 * x(t) + 3 * y(t) + 2 * Derivative(y(t), t),
  108. -w(t) + Derivative(z(t), t),
  109. -z(t) + Derivative(w(t), t)),
  110. 'func': [x(t), y(t), z(t), w(t)],
  111. 'order': {x(t): 1, y(t): 1, z(t): 1, w(t): 1},
  112. 'is_linear': True,
  113. 'is_constant': True,
  114. 'is_homogeneous': True,
  115. 'func_coeff': -Matrix([
  116. [Rational(12, 5), Rational(-6, 5), 0, 0],
  117. [Rational(-11, 2), Rational(3, 2), 0, 0],
  118. [0, 0, 0, -1],
  119. [0, 0, -1, 0]]),
  120. 'type_of_equation': 'type1',
  121. 'is_general': True}
  122. assert _classify_linear_system(eqs_4, funcs_2, t) == answer_4
  123. eqs_5 = (5*x1 + 12*x(t) - 6*(y(t)) + x2, (2*y1 - 11*x(t) + 3*y(t)), (z1 - w(t)), (w1 - z(t)))
  124. answer_5 = {'no_of_equation': 4, 'eq': (12*x(t) - 6*y(t) + 5*Derivative(x(t), t) + Derivative(x(t), (t, 2)),
  125. -11*x(t) + 3*y(t) + 2*Derivative(y(t), t), -w(t) + Derivative(z(t), t), -z(t) + Derivative(w(t),
  126. t)), 'func': [x(t), y(t), z(t), w(t)], 'order': {x(t): 2, y(t): 1, z(t): 1, w(t): 1}, 'is_linear':
  127. True, 'is_homogeneous': True, 'is_general': True, 'type_of_equation': 'type0', 'is_higher_order': True}
  128. assert _classify_linear_system(eqs_5, funcs_2, t) == answer_5
  129. eqs_6 = (Eq(x1, 3*y(t) - 11*z(t)), Eq(y1, 7*z(t) - 3*x(t)), Eq(z1, 11*x(t) - 7*y(t)))
  130. answer_6 = {'no_of_equation': 3, 'eq': (Eq(Derivative(x(t), t), 3*y(t) - 11*z(t)), Eq(Derivative(y(t), t), -3*x(t) + 7*z(t)),
  131. Eq(Derivative(z(t), t), 11*x(t) - 7*y(t))), 'func': [x(t), y(t), z(t)], 'order': {x(t): 1, y(t): 1, z(t): 1},
  132. 'is_linear': True, 'is_constant': True, 'is_homogeneous': True,
  133. 'func_coeff': -Matrix([
  134. [ 0, -3, 11],
  135. [ 3, 0, -7],
  136. [-11, 7, 0]]),
  137. 'type_of_equation': 'type1', 'is_general': True}
  138. assert _classify_linear_system(eqs_6, funcs_2[:-1], t) == answer_6
  139. eqs_7 = (Eq(x1, y(t)), Eq(y1, x(t)))
  140. answer_7 = {'no_of_equation': 2, 'eq': (Eq(Derivative(x(t), t), y(t)), Eq(Derivative(y(t), t), x(t))),
  141. 'func': [x(t), y(t)], 'order': {x(t): 1, y(t): 1}, 'is_linear': True, 'is_constant': True,
  142. 'is_homogeneous': True, 'func_coeff': -Matrix([
  143. [ 0, -1],
  144. [-1, 0]]),
  145. 'type_of_equation': 'type1', 'is_general': True}
  146. assert _classify_linear_system(eqs_7, funcs, t) == answer_7
  147. eqs_8 = (Eq(x1, 21*x(t)), Eq(y1, 17*x(t) + 3*y(t)), Eq(z1, 5*x(t) + 7*y(t) + 9*z(t)))
  148. answer_8 = {'no_of_equation': 3, 'eq': (Eq(Derivative(x(t), t), 21*x(t)), Eq(Derivative(y(t), t), 17*x(t) + 3*y(t)),
  149. Eq(Derivative(z(t), t), 5*x(t) + 7*y(t) + 9*z(t))), 'func': [x(t), y(t), z(t)], 'order': {x(t): 1, y(t): 1, z(t): 1},
  150. 'is_linear': True, 'is_constant': True, 'is_homogeneous': True,
  151. 'func_coeff': -Matrix([
  152. [-21, 0, 0],
  153. [-17, -3, 0],
  154. [ -5, -7, -9]]),
  155. 'type_of_equation': 'type1', 'is_general': True}
  156. assert _classify_linear_system(eqs_8, funcs_2[:-1], t) == answer_8
  157. eqs_9 = (Eq(x1, 4*x(t) + 5*y(t) + 2*z(t)), Eq(y1, x(t) + 13*y(t) + 9*z(t)), Eq(z1, 32*x(t) + 41*y(t) + 11*z(t)))
  158. answer_9 = {'no_of_equation': 3, 'eq': (Eq(Derivative(x(t), t), 4*x(t) + 5*y(t) + 2*z(t)),
  159. Eq(Derivative(y(t), t), x(t) + 13*y(t) + 9*z(t)), Eq(Derivative(z(t), t), 32*x(t) + 41*y(t) + 11*z(t))),
  160. 'func': [x(t), y(t), z(t)], 'order': {x(t): 1, y(t): 1, z(t): 1}, 'is_linear': True,
  161. 'is_constant': True, 'is_homogeneous': True,
  162. 'func_coeff': -Matrix([
  163. [ -4, -5, -2],
  164. [ -1, -13, -9],
  165. [-32, -41, -11]]),
  166. 'type_of_equation': 'type1', 'is_general': True}
  167. assert _classify_linear_system(eqs_9, funcs_2[:-1], t) == answer_9
  168. eqs_10 = (Eq(3*x1, 4*5*(y(t) - z(t))), Eq(4*y1, 3*5*(z(t) - x(t))), Eq(5*z1, 3*4*(x(t) - y(t))))
  169. answer_10 = {'no_of_equation': 3, 'eq': (Eq(3*Derivative(x(t), t), 20*y(t) - 20*z(t)),
  170. Eq(4*Derivative(y(t), t), -15*x(t) + 15*z(t)), Eq(5*Derivative(z(t), t), 12*x(t) - 12*y(t))),
  171. 'func': [x(t), y(t), z(t)], 'order': {x(t): 1, y(t): 1, z(t): 1}, 'is_linear': True,
  172. 'is_constant': True, 'is_homogeneous': True,
  173. 'func_coeff': -Matrix([
  174. [ 0, Rational(-20, 3), Rational(20, 3)],
  175. [Rational(15, 4), 0, Rational(-15, 4)],
  176. [Rational(-12, 5), Rational(12, 5), 0]]),
  177. 'type_of_equation': 'type1', 'is_general': True}
  178. assert _classify_linear_system(eqs_10, funcs_2[:-1], t) == answer_10
  179. eq11 = (Eq(x1, 3*y(t) - 11*z(t)), Eq(y1, 7*z(t) - 3*x(t)), Eq(z1, 11*x(t) - 7*y(t)))
  180. sol11 = {'no_of_equation': 3, 'eq': (Eq(Derivative(x(t), t), 3*y(t) - 11*z(t)), Eq(Derivative(y(t), t), -3*x(t) + 7*z(t)),
  181. Eq(Derivative(z(t), t), 11*x(t) - 7*y(t))), 'func': [x(t), y(t), z(t)], 'order': {x(t): 1, y(t): 1, z(t): 1},
  182. 'is_linear': True, 'is_constant': True, 'is_homogeneous': True, 'func_coeff': -Matrix([
  183. [ 0, -3, 11], [ 3, 0, -7], [-11, 7, 0]]), 'type_of_equation': 'type1', 'is_general': True}
  184. assert _classify_linear_system(eq11, funcs_2[:-1], t) == sol11
  185. eq12 = (Eq(Derivative(x(t), t), y(t)), Eq(Derivative(y(t), t), x(t)))
  186. sol12 = {'no_of_equation': 2, 'eq': (Eq(Derivative(x(t), t), y(t)), Eq(Derivative(y(t), t), x(t))),
  187. 'func': [x(t), y(t)], 'order': {x(t): 1, y(t): 1}, 'is_linear': True, 'is_constant': True,
  188. 'is_homogeneous': True, 'func_coeff': -Matrix([
  189. [0, -1],
  190. [-1, 0]]), 'type_of_equation': 'type1', 'is_general': True}
  191. assert _classify_linear_system(eq12, [x(t), y(t)], t) == sol12
  192. eq13 = (Eq(Derivative(x(t), t), 21*x(t)), Eq(Derivative(y(t), t), 17*x(t) + 3*y(t)),
  193. Eq(Derivative(z(t), t), 5*x(t) + 7*y(t) + 9*z(t)))
  194. sol13 = {'no_of_equation': 3, 'eq': (
  195. Eq(Derivative(x(t), t), 21 * x(t)), Eq(Derivative(y(t), t), 17 * x(t) + 3 * y(t)),
  196. Eq(Derivative(z(t), t), 5 * x(t) + 7 * y(t) + 9 * z(t))), 'func': [x(t), y(t), z(t)],
  197. 'order': {x(t): 1, y(t): 1, z(t): 1}, 'is_linear': True, 'is_constant': True, 'is_homogeneous': True,
  198. 'func_coeff': -Matrix([
  199. [-21, 0, 0],
  200. [-17, -3, 0],
  201. [-5, -7, -9]]), 'type_of_equation': 'type1', 'is_general': True}
  202. assert _classify_linear_system(eq13, [x(t), y(t), z(t)], t) == sol13
  203. eq14 = (
  204. Eq(Derivative(x(t), t), 4*x(t) + 5*y(t) + 2*z(t)), Eq(Derivative(y(t), t), x(t) + 13*y(t) + 9*z(t)),
  205. Eq(Derivative(z(t), t), 32*x(t) + 41*y(t) + 11*z(t)))
  206. sol14 = {'no_of_equation': 3, 'eq': (
  207. Eq(Derivative(x(t), t), 4 * x(t) + 5 * y(t) + 2 * z(t)), Eq(Derivative(y(t), t), x(t) + 13 * y(t) + 9 * z(t)),
  208. Eq(Derivative(z(t), t), 32 * x(t) + 41 * y(t) + 11 * z(t))), 'func': [x(t), y(t), z(t)],
  209. 'order': {x(t): 1, y(t): 1, z(t): 1}, 'is_linear': True, 'is_constant': True, 'is_homogeneous': True,
  210. 'func_coeff': -Matrix([
  211. [-4, -5, -2],
  212. [-1, -13, -9],
  213. [-32, -41, -11]]), 'type_of_equation': 'type1', 'is_general': True}
  214. assert _classify_linear_system(eq14, [x(t), y(t), z(t)], t) == sol14
  215. eq15 = (Eq(3*Derivative(x(t), t), 20*y(t) - 20*z(t)), Eq(4*Derivative(y(t), t), -15*x(t) + 15*z(t)),
  216. Eq(5*Derivative(z(t), t), 12*x(t) - 12*y(t)))
  217. sol15 = {'no_of_equation': 3, 'eq': (
  218. Eq(3 * Derivative(x(t), t), 20 * y(t) - 20 * z(t)), Eq(4 * Derivative(y(t), t), -15 * x(t) + 15 * z(t)),
  219. Eq(5 * Derivative(z(t), t), 12 * x(t) - 12 * y(t))), 'func': [x(t), y(t), z(t)],
  220. 'order': {x(t): 1, y(t): 1, z(t): 1}, 'is_linear': True, 'is_constant': True, 'is_homogeneous': True,
  221. 'func_coeff': -Matrix([
  222. [0, Rational(-20, 3), Rational(20, 3)],
  223. [Rational(15, 4), 0, Rational(-15, 4)],
  224. [Rational(-12, 5), Rational(12, 5), 0]]), 'type_of_equation': 'type1', 'is_general': True}
  225. assert _classify_linear_system(eq15, [x(t), y(t), z(t)], t) == sol15
  226. # Constant coefficient homogeneous ODEs
  227. eq1 = (Eq(diff(x(t), t), x(t) + y(t) + 9), Eq(diff(y(t), t), 2*x(t) + 5*y(t) + 23))
  228. sol1 = {'no_of_equation': 2, 'eq': (Eq(Derivative(x(t), t), x(t) + y(t) + 9),
  229. Eq(Derivative(y(t), t), 2*x(t) + 5*y(t) + 23)), 'func': [x(t), y(t)],
  230. 'order': {x(t): 1, y(t): 1}, 'is_linear': True, 'is_constant': True, 'is_homogeneous': False, 'is_general': True,
  231. 'func_coeff': -Matrix([[-1, -1], [-2, -5]]), 'rhs': Matrix([[ 9], [23]]), 'type_of_equation': 'type2'}
  232. assert _classify_linear_system(eq1, funcs, t) == sol1
  233. # Non constant coefficient homogeneous ODEs
  234. eq1 = (Eq(diff(x(t), t), 5*t*x(t) + 2*y(t)), Eq(diff(y(t), t), 2*x(t) + 5*t*y(t)))
  235. sol1 = {'no_of_equation': 2, 'eq': (Eq(Derivative(x(t), t), 5*t*x(t) + 2*y(t)), Eq(Derivative(y(t), t), 5*t*y(t) + 2*x(t))),
  236. 'func': [x(t), y(t)], 'order': {x(t): 1, y(t): 1}, 'is_linear': True, 'is_constant': False,
  237. 'is_homogeneous': True, 'func_coeff': -Matrix([ [-5*t, -2], [ -2, -5*t]]), 'commutative_antiderivative': Matrix([
  238. [5*t**2/2, 2*t], [ 2*t, 5*t**2/2]]), 'type_of_equation': 'type3', 'is_general': True}
  239. assert _classify_linear_system(eq1, funcs, t) == sol1
  240. # Non constant coefficient non-homogeneous ODEs
  241. eq1 = [Eq(x1, x(t) + t*y(t) + t), Eq(y1, t*x(t) + y(t))]
  242. sol1 = {'no_of_equation': 2, 'eq': [Eq(Derivative(x(t), t), t*y(t) + t + x(t)), Eq(Derivative(y(t), t),
  243. t*x(t) + y(t))], 'func': [x(t), y(t)], 'order': {x(t): 1, y(t): 1}, 'is_linear': True,
  244. 'is_constant': False, 'is_homogeneous': False, 'is_general': True, 'func_coeff': -Matrix([ [-1, -t],
  245. [-t, -1]]), 'commutative_antiderivative': Matrix([ [ t, t**2/2], [t**2/2, t]]), 'rhs':
  246. Matrix([ [t], [0]]), 'type_of_equation': 'type4'}
  247. assert _classify_linear_system(eq1, funcs, t) == sol1
  248. eq2 = [Eq(x1, t*x(t) + t*y(t) + t), Eq(y1, t*x(t) + t*y(t) + cos(t))]
  249. sol2 = {'no_of_equation': 2, 'eq': [Eq(Derivative(x(t), t), t*x(t) + t*y(t) + t), Eq(Derivative(y(t), t),
  250. t*x(t) + t*y(t) + cos(t))], 'func': [x(t), y(t)], 'order': {x(t): 1, y(t): 1}, 'is_linear': True,
  251. 'is_homogeneous': False, 'is_general': True, 'rhs': Matrix([ [ t], [cos(t)]]), 'func_coeff':
  252. Matrix([ [t, t], [t, t]]), 'is_constant': False, 'type_of_equation': 'type4',
  253. 'commutative_antiderivative': Matrix([ [t**2/2, t**2/2], [t**2/2, t**2/2]])}
  254. assert _classify_linear_system(eq2, funcs, t) == sol2
  255. eq3 = [Eq(x1, t*(x(t) + y(t) + z(t) + 1)), Eq(y1, t*(x(t) + y(t) + z(t))), Eq(z1, t*(x(t) + y(t) + z(t)))]
  256. sol3 = {'no_of_equation': 3, 'eq': [Eq(Derivative(x(t), t), t*(x(t) + y(t) + z(t) + 1)),
  257. Eq(Derivative(y(t), t), t*(x(t) + y(t) + z(t))), Eq(Derivative(z(t), t), t*(x(t) + y(t) + z(t)))],
  258. 'func': [x(t), y(t), z(t)], 'order': {x(t): 1, y(t): 1, z(t): 1}, 'is_linear': True, 'is_constant':
  259. False, 'is_homogeneous': False, 'is_general': True, 'func_coeff': -Matrix([ [-t, -t, -t], [-t, -t,
  260. -t], [-t, -t, -t]]), 'commutative_antiderivative': Matrix([ [t**2/2, t**2/2, t**2/2], [t**2/2,
  261. t**2/2, t**2/2], [t**2/2, t**2/2, t**2/2]]), 'rhs': Matrix([ [t], [0], [0]]), 'type_of_equation':
  262. 'type4'}
  263. assert _classify_linear_system(eq3, funcs_2[:-1], t) == sol3
  264. eq4 = [Eq(x1, x(t) + y(t) + t*z(t) + 1), Eq(y1, x(t) + t*y(t) + z(t) + 10), Eq(z1, t*x(t) + y(t) + z(t) + t)]
  265. sol4 = {'no_of_equation': 3, 'eq': [Eq(Derivative(x(t), t), t*z(t) + x(t) + y(t) + 1), Eq(Derivative(y(t),
  266. t), t*y(t) + x(t) + z(t) + 10), Eq(Derivative(z(t), t), t*x(t) + t + y(t) + z(t))], 'func': [x(t),
  267. y(t), z(t)], 'order': {x(t): 1, y(t): 1, z(t): 1}, 'is_linear': True, 'is_constant': False,
  268. 'is_homogeneous': False, 'is_general': True, 'func_coeff': -Matrix([ [-1, -1, -t], [-1, -t, -1], [-t,
  269. -1, -1]]), 'commutative_antiderivative': Matrix([ [ t, t, t**2/2], [ t, t**2/2,
  270. t], [t**2/2, t, t]]), 'rhs': Matrix([ [ 1], [10], [ t]]), 'type_of_equation': 'type4'}
  271. assert _classify_linear_system(eq4, funcs_2[:-1], t) == sol4
  272. sum_terms = t*(x(t) + y(t) + z(t) + w(t))
  273. eq5 = [Eq(x1, sum_terms), Eq(y1, sum_terms), Eq(z1, sum_terms + 1), Eq(w1, sum_terms)]
  274. sol5 = {'no_of_equation': 4, 'eq': [Eq(Derivative(x(t), t), t*(w(t) + x(t) + y(t) + z(t))),
  275. Eq(Derivative(y(t), t), t*(w(t) + x(t) + y(t) + z(t))), Eq(Derivative(z(t), t), t*(w(t) + x(t) +
  276. y(t) + z(t)) + 1), Eq(Derivative(w(t), t), t*(w(t) + x(t) + y(t) + z(t)))], 'func': [x(t), y(t),
  277. z(t), w(t)], 'order': {x(t): 1, y(t): 1, z(t): 1, w(t): 1}, 'is_linear': True, 'is_constant': False,
  278. 'is_homogeneous': False, 'is_general': True, 'func_coeff': -Matrix([ [-t, -t, -t, -t], [-t, -t, -t,
  279. -t], [-t, -t, -t, -t], [-t, -t, -t, -t]]), 'commutative_antiderivative': Matrix([ [t**2/2, t**2/2,
  280. t**2/2, t**2/2], [t**2/2, t**2/2, t**2/2, t**2/2], [t**2/2, t**2/2, t**2/2, t**2/2], [t**2/2,
  281. t**2/2, t**2/2, t**2/2]]), 'rhs': Matrix([ [0], [0], [1], [0]]), 'type_of_equation': 'type4'}
  282. assert _classify_linear_system(eq5, funcs_2, t) == sol5
  283. # Second Order
  284. t_ = symbols("t_")
  285. eq1 = (Eq(9*x(t) + 7*y(t) + 4*Derivative(x(t), t) + Derivative(x(t), (t, 2)) + 3*Derivative(y(t), t), 11*exp(I*t)),
  286. Eq(3*x(t) + 12*y(t) + 5*Derivative(x(t), t) + 8*Derivative(y(t), t) + Derivative(y(t), (t, 2)), 2*exp(I*t)))
  287. sol1 = {'no_of_equation': 2, 'eq': (Eq(9*x(t) + 7*y(t) + 4*Derivative(x(t), t) + Derivative(x(t), (t, 2)) +
  288. 3*Derivative(y(t), t), 11*exp(I*t)), Eq(3*x(t) + 12*y(t) + 5*Derivative(x(t), t) +
  289. 8*Derivative(y(t), t) + Derivative(y(t), (t, 2)), 2*exp(I*t))), 'func': [x(t), y(t)], 'order':
  290. {x(t): 2, y(t): 2}, 'is_linear': True, 'is_homogeneous': False, 'is_general': True, 'rhs': Matrix([
  291. [11*exp(I*t)], [ 2*exp(I*t)]]), 'type_of_equation': 'type0', 'is_second_order': True,
  292. 'is_higher_order': True}
  293. assert _classify_linear_system(eq1, funcs, t) == sol1
  294. eq2 = (Eq((4*t**2 + 7*t + 1)**2*Derivative(x(t), (t, 2)), 5*x(t) + 35*y(t)),
  295. Eq((4*t**2 + 7*t + 1)**2*Derivative(y(t), (t, 2)), x(t) + 9*y(t)))
  296. sol2 = {'no_of_equation': 2, 'eq': (Eq((4*t**2 + 7*t + 1)**2*Derivative(x(t), (t, 2)), 5*x(t) + 35*y(t)),
  297. Eq((4*t**2 + 7*t + 1)**2*Derivative(y(t), (t, 2)), x(t) + 9*y(t))), 'func': [x(t), y(t)], 'order':
  298. {x(t): 2, y(t): 2}, 'is_linear': True, 'is_homogeneous': True, 'is_general': True,
  299. 'type_of_equation': 'type2', 'A0': Matrix([ [Rational(53, 4), 35], [ 1, Rational(69, 4)]]), 'g(t)': sqrt(4*t**2 + 7*t
  300. + 1), 'tau': sqrt(33)*log(t - sqrt(33)/8 + Rational(7, 8))/33 - sqrt(33)*log(t + sqrt(33)/8 + Rational(7, 8))/33,
  301. 'is_transformed': True, 't_': t_, 'is_second_order': True, 'is_higher_order': True}
  302. assert _classify_linear_system(eq2, funcs, t) == sol2
  303. eq3 = ((t*Derivative(x(t), t) - x(t))*log(t) + (t*Derivative(y(t), t) - y(t))*exp(t) + Derivative(x(t), (t, 2)),
  304. t**2*(t*Derivative(x(t), t) - x(t)) + t*(t*Derivative(y(t), t) - y(t)) + Derivative(y(t), (t, 2)))
  305. sol3 = {'no_of_equation': 2, 'eq': ((t*Derivative(x(t), t) - x(t))*log(t) + (t*Derivative(y(t), t) -
  306. y(t))*exp(t) + Derivative(x(t), (t, 2)), t**2*(t*Derivative(x(t), t) - x(t)) + t*(t*Derivative(y(t),
  307. t) - y(t)) + Derivative(y(t), (t, 2))), 'func': [x(t), y(t)], 'order': {x(t): 2, y(t): 2},
  308. 'is_linear': True, 'is_homogeneous': True, 'is_general': True, 'type_of_equation': 'type1', 'A1':
  309. Matrix([ [-t*log(t), -t*exp(t)], [ -t**3, -t**2]]), 'is_second_order': True,
  310. 'is_higher_order': True}
  311. assert _classify_linear_system(eq3, funcs, t) == sol3
  312. eq4 = (Eq(x2, k*x(t) - l*y1), Eq(y2, l*x1 + k*y(t)))
  313. sol4 = {'no_of_equation': 2, 'eq': (Eq(Derivative(x(t), (t, 2)), k*x(t) - l*Derivative(y(t), t)),
  314. Eq(Derivative(y(t), (t, 2)), k*y(t) + l*Derivative(x(t), t))), 'func': [x(t), y(t)], 'order': {x(t):
  315. 2, y(t): 2}, 'is_linear': True, 'is_homogeneous': True, 'is_general': True, 'type_of_equation':
  316. 'type0', 'is_second_order': True, 'is_higher_order': True}
  317. assert _classify_linear_system(eq4, funcs, t) == sol4
  318. # Multiple matches
  319. f, g = symbols("f g", cls=Function)
  320. y, t_ = symbols("y t_")
  321. funcs = [f(t), g(t)]
  322. eq1 = [Eq(Derivative(f(t), t)**2 - 2*Derivative(f(t), t) + 1, 4),
  323. Eq(-y*f(t) + Derivative(g(t), t), 0)]
  324. sol1 = {'is_implicit': True,
  325. 'canon_eqs': [[Eq(Derivative(f(t), t), -1), Eq(Derivative(g(t), t), y*f(t))],
  326. [Eq(Derivative(f(t), t), 3), Eq(Derivative(g(t), t), y*f(t))]]}
  327. assert _classify_linear_system(eq1, funcs, t) == sol1
  328. raises(ValueError, lambda: _classify_linear_system(eq1, funcs[:1], t))
  329. eq2 = [Eq(Derivative(f(t), t), (2*f(t) + g(t) + 1)/t), Eq(Derivative(g(t), t), (f(t) + 2*g(t))/t)]
  330. sol2 = {'no_of_equation': 2, 'eq': [Eq(Derivative(f(t), t), (2*f(t) + g(t) + 1)/t), Eq(Derivative(g(t), t),
  331. (f(t) + 2*g(t))/t)], 'func': [f(t), g(t)], 'order': {f(t): 1, g(t): 1}, 'is_linear': True,
  332. 'is_homogeneous': False, 'is_general': True, 'rhs': Matrix([ [1], [0]]), 'func_coeff': Matrix([ [2,
  333. 1], [1, 2]]), 'is_constant': False, 'type_of_equation': 'type6', 't_': t_, 'tau': log(t),
  334. 'commutative_antiderivative': Matrix([ [2*log(t), log(t)], [ log(t), 2*log(t)]])}
  335. assert _classify_linear_system(eq2, funcs, t) == sol2
  336. eq3 = [Eq(Derivative(f(t), t), (2*f(t) + g(t))/t), Eq(Derivative(g(t), t), (f(t) + 2*g(t))/t)]
  337. sol3 = {'no_of_equation': 2, 'eq': [Eq(Derivative(f(t), t), (2*f(t) + g(t))/t), Eq(Derivative(g(t), t),
  338. (f(t) + 2*g(t))/t)], 'func': [f(t), g(t)], 'order': {f(t): 1, g(t): 1}, 'is_linear': True,
  339. 'is_homogeneous': True, 'is_general': True, 'func_coeff': Matrix([ [2, 1], [1, 2]]), 'is_constant':
  340. False, 'type_of_equation': 'type5', 't_': t_, 'rhs': Matrix([ [0], [0]]), 'tau': log(t),
  341. 'commutative_antiderivative': Matrix([ [2*log(t), log(t)], [ log(t), 2*log(t)]])}
  342. assert _classify_linear_system(eq3, funcs, t) == sol3
  343. def test_matrix_exp():
  344. from sympy.matrices.dense import Matrix, eye, zeros
  345. from sympy.solvers.ode.systems import matrix_exp
  346. t = Symbol('t')
  347. for n in range(1, 6+1):
  348. assert matrix_exp(zeros(n), t) == eye(n)
  349. for n in range(1, 6+1):
  350. A = eye(n)
  351. expAt = exp(t) * eye(n)
  352. assert matrix_exp(A, t) == expAt
  353. for n in range(1, 6+1):
  354. A = Matrix(n, n, lambda i,j: i+1 if i==j else 0)
  355. expAt = Matrix(n, n, lambda i,j: exp((i+1)*t) if i==j else 0)
  356. assert matrix_exp(A, t) == expAt
  357. A = Matrix([[0, 1], [-1, 0]])
  358. expAt = Matrix([[cos(t), sin(t)], [-sin(t), cos(t)]])
  359. assert matrix_exp(A, t) == expAt
  360. A = Matrix([[2, -5], [2, -4]])
  361. expAt = Matrix([
  362. [3*exp(-t)*sin(t) + exp(-t)*cos(t), -5*exp(-t)*sin(t)],
  363. [2*exp(-t)*sin(t), -3*exp(-t)*sin(t) + exp(-t)*cos(t)]
  364. ])
  365. assert matrix_exp(A, t) == expAt
  366. A = Matrix([[21, 17, 6], [-5, -1, -6], [4, 4, 16]])
  367. # TO update this.
  368. # expAt = Matrix([
  369. # [(8*t*exp(12*t) + 5*exp(12*t) - 1)*exp(4*t)/4,
  370. # (8*t*exp(12*t) + 5*exp(12*t) - 5)*exp(4*t)/4,
  371. # (exp(12*t) - 1)*exp(4*t)/2],
  372. # [(-8*t*exp(12*t) - exp(12*t) + 1)*exp(4*t)/4,
  373. # (-8*t*exp(12*t) - exp(12*t) + 5)*exp(4*t)/4,
  374. # (-exp(12*t) + 1)*exp(4*t)/2],
  375. # [4*t*exp(16*t), 4*t*exp(16*t), exp(16*t)]])
  376. expAt = Matrix([
  377. [2*t*exp(16*t) + 5*exp(16*t)/4 - exp(4*t)/4, 2*t*exp(16*t) + 5*exp(16*t)/4 - 5*exp(4*t)/4, exp(16*t)/2 - exp(4*t)/2],
  378. [ -2*t*exp(16*t) - exp(16*t)/4 + exp(4*t)/4, -2*t*exp(16*t) - exp(16*t)/4 + 5*exp(4*t)/4, -exp(16*t)/2 + exp(4*t)/2],
  379. [ 4*t*exp(16*t), 4*t*exp(16*t), exp(16*t)]
  380. ])
  381. assert matrix_exp(A, t) == expAt
  382. A = Matrix([[1, 1, 0, 0],
  383. [0, 1, 1, 0],
  384. [0, 0, 1, -S(1)/8],
  385. [0, 0, S(1)/2, S(1)/2]])
  386. expAt = Matrix([
  387. [exp(t), t*exp(t), 4*t*exp(3*t/4) + 8*t*exp(t) + 48*exp(3*t/4) - 48*exp(t),
  388. -2*t*exp(3*t/4) - 2*t*exp(t) - 16*exp(3*t/4) + 16*exp(t)],
  389. [0, exp(t), -t*exp(3*t/4) - 8*exp(3*t/4) + 8*exp(t), t*exp(3*t/4)/2 + 2*exp(3*t/4) - 2*exp(t)],
  390. [0, 0, t*exp(3*t/4)/4 + exp(3*t/4), -t*exp(3*t/4)/8],
  391. [0, 0, t*exp(3*t/4)/2, -t*exp(3*t/4)/4 + exp(3*t/4)]
  392. ])
  393. assert matrix_exp(A, t) == expAt
  394. A = Matrix([
  395. [ 0, 1, 0, 0],
  396. [-1, 0, 0, 0],
  397. [ 0, 0, 0, 1],
  398. [ 0, 0, -1, 0]])
  399. expAt = Matrix([
  400. [ cos(t), sin(t), 0, 0],
  401. [-sin(t), cos(t), 0, 0],
  402. [ 0, 0, cos(t), sin(t)],
  403. [ 0, 0, -sin(t), cos(t)]])
  404. assert matrix_exp(A, t) == expAt
  405. A = Matrix([
  406. [ 0, 1, 1, 0],
  407. [-1, 0, 0, 1],
  408. [ 0, 0, 0, 1],
  409. [ 0, 0, -1, 0]])
  410. expAt = Matrix([
  411. [ cos(t), sin(t), t*cos(t), t*sin(t)],
  412. [-sin(t), cos(t), -t*sin(t), t*cos(t)],
  413. [ 0, 0, cos(t), sin(t)],
  414. [ 0, 0, -sin(t), cos(t)]])
  415. assert matrix_exp(A, t) == expAt
  416. # This case is unacceptably slow right now but should be solvable...
  417. #a, b, c, d, e, f = symbols('a b c d e f')
  418. #A = Matrix([
  419. #[-a, b, c, d],
  420. #[ a, -b, e, 0],
  421. #[ 0, 0, -c - e - f, 0],
  422. #[ 0, 0, f, -d]])
  423. A = Matrix([[0, I], [I, 0]])
  424. expAt = Matrix([
  425. [exp(I*t)/2 + exp(-I*t)/2, exp(I*t)/2 - exp(-I*t)/2],
  426. [exp(I*t)/2 - exp(-I*t)/2, exp(I*t)/2 + exp(-I*t)/2]])
  427. assert matrix_exp(A, t) == expAt
  428. # Testing Errors
  429. M = Matrix([[1, 2, 3], [4, 5, 6], [7, 7, 7]])
  430. M1 = Matrix([[t, 1], [1, 1]])
  431. raises(ValueError, lambda: matrix_exp(M[:, :2], t))
  432. raises(ValueError, lambda: matrix_exp(M[:2, :], t))
  433. raises(ValueError, lambda: matrix_exp(M1, t))
  434. raises(ValueError, lambda: matrix_exp(M1[:1, :1], t))
  435. def test_canonical_odes():
  436. f, g, h = symbols('f g h', cls=Function)
  437. x = symbols('x')
  438. funcs = [f(x), g(x), h(x)]
  439. eqs1 = [Eq(f(x).diff(x, x), f(x) + 2*g(x)), Eq(g(x) + 1, g(x).diff(x) + f(x))]
  440. sol1 = [[Eq(Derivative(f(x), (x, 2)), f(x) + 2*g(x)), Eq(Derivative(g(x), x), -f(x) + g(x) + 1)]]
  441. assert canonical_odes(eqs1, funcs[:2], x) == sol1
  442. eqs2 = [Eq(f(x).diff(x), h(x).diff(x) + f(x)), Eq(g(x).diff(x)**2, f(x) + h(x)), Eq(h(x).diff(x), f(x))]
  443. sol2 = [[Eq(Derivative(f(x), x), 2*f(x)), Eq(Derivative(g(x), x), -sqrt(f(x) + h(x))), Eq(Derivative(h(x), x), f(x))],
  444. [Eq(Derivative(f(x), x), 2*f(x)), Eq(Derivative(g(x), x), sqrt(f(x) + h(x))), Eq(Derivative(h(x), x), f(x))]]
  445. assert canonical_odes(eqs2, funcs, x) == sol2
  446. def test_sysode_linear_neq_order1_type1():
  447. f, g, x, y, h = symbols('f g x y h', cls=Function)
  448. a, b, c, t = symbols('a b c t')
  449. eqs1 = [Eq(Derivative(x(t), t), x(t)),
  450. Eq(Derivative(y(t), t), y(t))]
  451. sol1 = [Eq(x(t), C1*exp(t)),
  452. Eq(y(t), C2*exp(t))]
  453. assert dsolve(eqs1) == sol1
  454. assert checksysodesol(eqs1, sol1) == (True, [0, 0])
  455. eqs2 = [Eq(Derivative(x(t), t), 2*x(t)),
  456. Eq(Derivative(y(t), t), 3*y(t))]
  457. sol2 = [Eq(x(t), C1*exp(2*t)),
  458. Eq(y(t), C2*exp(3*t))]
  459. assert dsolve(eqs2) == sol2
  460. assert checksysodesol(eqs2, sol2) == (True, [0, 0])
  461. eqs3 = [Eq(Derivative(x(t), t), a*x(t)),
  462. Eq(Derivative(y(t), t), a*y(t))]
  463. sol3 = [Eq(x(t), C1*exp(a*t)),
  464. Eq(y(t), C2*exp(a*t))]
  465. assert dsolve(eqs3) == sol3
  466. assert checksysodesol(eqs3, sol3) == (True, [0, 0])
  467. # Regression test case for issue #15474
  468. # https://github.com/sympy/sympy/issues/15474
  469. eqs4 = [Eq(Derivative(x(t), t), a*x(t)),
  470. Eq(Derivative(y(t), t), b*y(t))]
  471. sol4 = [Eq(x(t), C1*exp(a*t)),
  472. Eq(y(t), C2*exp(b*t))]
  473. assert dsolve(eqs4) == sol4
  474. assert checksysodesol(eqs4, sol4) == (True, [0, 0])
  475. eqs5 = [Eq(Derivative(x(t), t), -y(t)),
  476. Eq(Derivative(y(t), t), x(t))]
  477. sol5 = [Eq(x(t), -C1*sin(t) - C2*cos(t)),
  478. Eq(y(t), C1*cos(t) - C2*sin(t))]
  479. assert dsolve(eqs5) == sol5
  480. assert checksysodesol(eqs5, sol5) == (True, [0, 0])
  481. eqs6 = [Eq(Derivative(x(t), t), -2*y(t)),
  482. Eq(Derivative(y(t), t), 2*x(t))]
  483. sol6 = [Eq(x(t), -C1*sin(2*t) - C2*cos(2*t)),
  484. Eq(y(t), C1*cos(2*t) - C2*sin(2*t))]
  485. assert dsolve(eqs6) == sol6
  486. assert checksysodesol(eqs6, sol6) == (True, [0, 0])
  487. eqs7 = [Eq(Derivative(x(t), t), I*y(t)),
  488. Eq(Derivative(y(t), t), I*x(t))]
  489. sol7 = [Eq(x(t), -C1*exp(-I*t) + C2*exp(I*t)),
  490. Eq(y(t), C1*exp(-I*t) + C2*exp(I*t))]
  491. assert dsolve(eqs7) == sol7
  492. assert checksysodesol(eqs7, sol7) == (True, [0, 0])
  493. eqs8 = [Eq(Derivative(x(t), t), -a*y(t)),
  494. Eq(Derivative(y(t), t), a*x(t))]
  495. sol8 = [Eq(x(t), -I*C1*exp(-I*a*t) + I*C2*exp(I*a*t)),
  496. Eq(y(t), C1*exp(-I*a*t) + C2*exp(I*a*t))]
  497. assert dsolve(eqs8) == sol8
  498. assert checksysodesol(eqs8, sol8) == (True, [0, 0])
  499. eqs9 = [Eq(Derivative(x(t), t), x(t) + y(t)),
  500. Eq(Derivative(y(t), t), x(t) - y(t))]
  501. sol9 = [Eq(x(t), C1*(1 - sqrt(2))*exp(-sqrt(2)*t) + C2*(1 + sqrt(2))*exp(sqrt(2)*t)),
  502. Eq(y(t), C1*exp(-sqrt(2)*t) + C2*exp(sqrt(2)*t))]
  503. assert dsolve(eqs9) == sol9
  504. assert checksysodesol(eqs9, sol9) == (True, [0, 0])
  505. eqs10 = [Eq(Derivative(x(t), t), x(t) + y(t)),
  506. Eq(Derivative(y(t), t), x(t) + y(t))]
  507. sol10 = [Eq(x(t), -C1 + C2*exp(2*t)),
  508. Eq(y(t), C1 + C2*exp(2*t))]
  509. assert dsolve(eqs10) == sol10
  510. assert checksysodesol(eqs10, sol10) == (True, [0, 0])
  511. eqs11 = [Eq(Derivative(x(t), t), 2*x(t) + y(t)),
  512. Eq(Derivative(y(t), t), -x(t) + 2*y(t))]
  513. sol11 = [Eq(x(t), C1*exp(2*t)*sin(t) + C2*exp(2*t)*cos(t)),
  514. Eq(y(t), C1*exp(2*t)*cos(t) - C2*exp(2*t)*sin(t))]
  515. assert dsolve(eqs11) == sol11
  516. assert checksysodesol(eqs11, sol11) == (True, [0, 0])
  517. eqs12 = [Eq(Derivative(x(t), t), x(t) + 2*y(t)),
  518. Eq(Derivative(y(t), t), 2*x(t) + y(t))]
  519. sol12 = [Eq(x(t), -C1*exp(-t) + C2*exp(3*t)),
  520. Eq(y(t), C1*exp(-t) + C2*exp(3*t))]
  521. assert dsolve(eqs12) == sol12
  522. assert checksysodesol(eqs12, sol12) == (True, [0, 0])
  523. eqs13 = [Eq(Derivative(x(t), t), 4*x(t) + y(t)),
  524. Eq(Derivative(y(t), t), -x(t) + 2*y(t))]
  525. sol13 = [Eq(x(t), C2*t*exp(3*t) + (C1 + C2)*exp(3*t)),
  526. Eq(y(t), -C1*exp(3*t) - C2*t*exp(3*t))]
  527. assert dsolve(eqs13) == sol13
  528. assert checksysodesol(eqs13, sol13) == (True, [0, 0])
  529. eqs14 = [Eq(Derivative(x(t), t), a*y(t)),
  530. Eq(Derivative(y(t), t), a*x(t))]
  531. sol14 = [Eq(x(t), -C1*exp(-a*t) + C2*exp(a*t)),
  532. Eq(y(t), C1*exp(-a*t) + C2*exp(a*t))]
  533. assert dsolve(eqs14) == sol14
  534. assert checksysodesol(eqs14, sol14) == (True, [0, 0])
  535. eqs15 = [Eq(Derivative(x(t), t), a*y(t)),
  536. Eq(Derivative(y(t), t), b*x(t))]
  537. sol15 = [Eq(x(t), -C1*a*exp(-t*sqrt(a*b))/sqrt(a*b) + C2*a*exp(t*sqrt(a*b))/sqrt(a*b)),
  538. Eq(y(t), C1*exp(-t*sqrt(a*b)) + C2*exp(t*sqrt(a*b)))]
  539. assert dsolve(eqs15) == sol15
  540. assert checksysodesol(eqs15, sol15) == (True, [0, 0])
  541. eqs16 = [Eq(Derivative(x(t), t), a*x(t) + b*y(t)),
  542. Eq(Derivative(y(t), t), c*x(t))]
  543. sol16 = [Eq(x(t), -2*C1*b*exp(t*(a + sqrt(a**2 + 4*b*c))/2)/(a - sqrt(a**2 + 4*b*c)) - 2*C2*b*exp(t*(a -
  544. sqrt(a**2 + 4*b*c))/2)/(a + sqrt(a**2 + 4*b*c))),
  545. Eq(y(t), C1*exp(t*(a + sqrt(a**2 + 4*b*c))/2) + C2*exp(t*(a - sqrt(a**2 + 4*b*c))/2))]
  546. assert dsolve(eqs16) == sol16
  547. assert checksysodesol(eqs16, sol16) == (True, [0, 0])
  548. # Regression test case for issue #18562
  549. # https://github.com/sympy/sympy/issues/18562
  550. eqs17 = [Eq(Derivative(x(t), t), a*y(t) + x(t)),
  551. Eq(Derivative(y(t), t), a*x(t) - y(t))]
  552. sol17 = [Eq(x(t), C1*a*exp(t*sqrt(a**2 + 1))/(sqrt(a**2 + 1) - 1) - C2*a*exp(-t*sqrt(a**2 + 1))/(sqrt(a**2 +
  553. 1) + 1)),
  554. Eq(y(t), C1*exp(t*sqrt(a**2 + 1)) + C2*exp(-t*sqrt(a**2 + 1)))]
  555. assert dsolve(eqs17) == sol17
  556. assert checksysodesol(eqs17, sol17) == (True, [0, 0])
  557. eqs18 = [Eq(Derivative(x(t), t), 0),
  558. Eq(Derivative(y(t), t), 0)]
  559. sol18 = [Eq(x(t), C1),
  560. Eq(y(t), C2)]
  561. assert dsolve(eqs18) == sol18
  562. assert checksysodesol(eqs18, sol18) == (True, [0, 0])
  563. eqs19 = [Eq(Derivative(x(t), t), 2*x(t) - y(t)),
  564. Eq(Derivative(y(t), t), x(t))]
  565. sol19 = [Eq(x(t), C2*t*exp(t) + (C1 + C2)*exp(t)),
  566. Eq(y(t), C1*exp(t) + C2*t*exp(t))]
  567. assert dsolve(eqs19) == sol19
  568. assert checksysodesol(eqs19, sol19) == (True, [0, 0])
  569. eqs20 = [Eq(Derivative(x(t), t), x(t)),
  570. Eq(Derivative(y(t), t), x(t) + y(t))]
  571. sol20 = [Eq(x(t), C1*exp(t)),
  572. Eq(y(t), C1*t*exp(t) + C2*exp(t))]
  573. assert dsolve(eqs20) == sol20
  574. assert checksysodesol(eqs20, sol20) == (True, [0, 0])
  575. eqs21 = [Eq(Derivative(x(t), t), 3*x(t)),
  576. Eq(Derivative(y(t), t), x(t) + y(t))]
  577. sol21 = [Eq(x(t), 2*C1*exp(3*t)),
  578. Eq(y(t), C1*exp(3*t) + C2*exp(t))]
  579. assert dsolve(eqs21) == sol21
  580. assert checksysodesol(eqs21, sol21) == (True, [0, 0])
  581. eqs22 = [Eq(Derivative(x(t), t), 3*x(t)),
  582. Eq(Derivative(y(t), t), y(t))]
  583. sol22 = [Eq(x(t), C1*exp(3*t)),
  584. Eq(y(t), C2*exp(t))]
  585. assert dsolve(eqs22) == sol22
  586. assert checksysodesol(eqs22, sol22) == (True, [0, 0])
  587. @slow
  588. def test_sysode_linear_neq_order1_type1_slow():
  589. t = Symbol('t')
  590. Z0 = Function('Z0')
  591. Z1 = Function('Z1')
  592. Z2 = Function('Z2')
  593. Z3 = Function('Z3')
  594. k01, k10, k20, k21, k23, k30 = symbols('k01 k10 k20 k21 k23 k30')
  595. eqs1 = [Eq(Derivative(Z0(t), t), -k01*Z0(t) + k10*Z1(t) + k20*Z2(t) + k30*Z3(t)),
  596. Eq(Derivative(Z1(t), t), k01*Z0(t) - k10*Z1(t) + k21*Z2(t)),
  597. Eq(Derivative(Z2(t), t), (-k20 - k21 - k23)*Z2(t)),
  598. Eq(Derivative(Z3(t), t), k23*Z2(t) - k30*Z3(t))]
  599. sol1 = [Eq(Z0(t), C1*k10/k01 - C2*(k10 - k30)*exp(-k30*t)/(k01 + k10 - k30) - C3*(k10*(k20 + k21 - k30) -
  600. k20**2 - k20*(k21 + k23 - k30) + k23*k30)*exp(-t*(k20 + k21 + k23))/(k23*(-k01 - k10 + k20 + k21 +
  601. k23)) - C4*exp(-t*(k01 + k10))),
  602. Eq(Z1(t), C1 - C2*k01*exp(-k30*t)/(k01 + k10 - k30) + C3*(-k01*(k20 + k21 - k30) + k20*k21 + k21**2
  603. + k21*(k23 - k30))*exp(-t*(k20 + k21 + k23))/(k23*(-k01 - k10 + k20 + k21 + k23)) + C4*exp(-t*(k01 +
  604. k10))),
  605. Eq(Z2(t), -C3*(k20 + k21 + k23 - k30)*exp(-t*(k20 + k21 + k23))/k23),
  606. Eq(Z3(t), C2*exp(-k30*t) + C3*exp(-t*(k20 + k21 + k23)))]
  607. assert dsolve(eqs1) == sol1
  608. assert checksysodesol(eqs1, sol1) == (True, [0, 0, 0, 0])
  609. x, y, z, u, v, w = symbols('x y z u v w', cls=Function)
  610. k2, k3 = symbols('k2 k3')
  611. a_b, a_c = symbols('a_b a_c', real=True)
  612. eqs2 = [Eq(Derivative(z(t), t), k2*y(t)),
  613. Eq(Derivative(x(t), t), k3*y(t)),
  614. Eq(Derivative(y(t), t), (-k2 - k3)*y(t))]
  615. sol2 = [Eq(z(t), C1 - C2*k2*exp(-t*(k2 + k3))/(k2 + k3)),
  616. Eq(x(t), -C2*k3*exp(-t*(k2 + k3))/(k2 + k3) + C3),
  617. Eq(y(t), C2*exp(-t*(k2 + k3)))]
  618. assert dsolve(eqs2) == sol2
  619. assert checksysodesol(eqs2, sol2) == (True, [0, 0, 0])
  620. eqs3 = [4*u(t) - v(t) - 2*w(t) + Derivative(u(t), t),
  621. 2*u(t) + v(t) - 2*w(t) + Derivative(v(t), t),
  622. 5*u(t) + v(t) - 3*w(t) + Derivative(w(t), t)]
  623. sol3 = [Eq(u(t), C3*exp(-2*t) + (C1/2 + sqrt(3)*C2/6)*cos(sqrt(3)*t) + sin(sqrt(3)*t)*(sqrt(3)*C1/6 +
  624. C2*Rational(-1, 2))),
  625. Eq(v(t), (C1/2 + sqrt(3)*C2/6)*cos(sqrt(3)*t) + sin(sqrt(3)*t)*(sqrt(3)*C1/6 + C2*Rational(-1, 2))),
  626. Eq(w(t), C1*cos(sqrt(3)*t) - C2*sin(sqrt(3)*t) + C3*exp(-2*t))]
  627. assert dsolve(eqs3) == sol3
  628. assert checksysodesol(eqs3, sol3) == (True, [0, 0, 0])
  629. eqs4 = [Eq(Derivative(x(t), t), w(t)*Rational(-2, 9) + 2*x(t) + y(t) + z(t)*Rational(-8, 9)),
  630. Eq(Derivative(y(t), t), w(t)*Rational(4, 9) + 2*y(t) + z(t)*Rational(16, 9)),
  631. Eq(Derivative(z(t), t), w(t)*Rational(-2, 9) + z(t)*Rational(37, 9)),
  632. Eq(Derivative(w(t), t), w(t)*Rational(44, 9) + z(t)*Rational(-4, 9))]
  633. sol4 = [Eq(x(t), C1*exp(2*t) + C2*t*exp(2*t)),
  634. Eq(y(t), C2*exp(2*t) + 2*C3*exp(4*t)),
  635. Eq(z(t), 2*C3*exp(4*t) + C4*exp(5*t)*Rational(-1, 4)),
  636. Eq(w(t), C3*exp(4*t) + C4*exp(5*t))]
  637. assert dsolve(eqs4) == sol4
  638. assert checksysodesol(eqs4, sol4) == (True, [0, 0, 0, 0])
  639. # Regression test case for issue #15574
  640. # https://github.com/sympy/sympy/issues/15574
  641. eq5 = [Eq(x(t).diff(t), x(t)), Eq(y(t).diff(t), y(t)), Eq(z(t).diff(t), z(t)), Eq(w(t).diff(t), w(t))]
  642. sol5 = [Eq(x(t), C1*exp(t)), Eq(y(t), C2*exp(t)), Eq(z(t), C3*exp(t)), Eq(w(t), C4*exp(t))]
  643. assert dsolve(eq5) == sol5
  644. assert checksysodesol(eq5, sol5) == (True, [0, 0, 0, 0])
  645. eqs6 = [Eq(Derivative(x(t), t), x(t) + y(t)),
  646. Eq(Derivative(y(t), t), y(t) + z(t)),
  647. Eq(Derivative(z(t), t), w(t)*Rational(-1, 8) + z(t)),
  648. Eq(Derivative(w(t), t), w(t)/2 + z(t)/2)]
  649. sol6 = [Eq(x(t), C1*exp(t) + C2*t*exp(t) + 4*C4*t*exp(t*Rational(3, 4)) + (4*C3 + 48*C4)*exp(t*Rational(3,
  650. 4))),
  651. Eq(y(t), C2*exp(t) - C4*t*exp(t*Rational(3, 4)) - (C3 + 8*C4)*exp(t*Rational(3, 4))),
  652. Eq(z(t), C4*t*exp(t*Rational(3, 4))/4 + (C3/4 + C4)*exp(t*Rational(3, 4))),
  653. Eq(w(t), C3*exp(t*Rational(3, 4))/2 + C4*t*exp(t*Rational(3, 4))/2)]
  654. assert dsolve(eqs6) == sol6
  655. assert checksysodesol(eqs6, sol6) == (True, [0, 0, 0, 0])
  656. # Regression test case for issue #15574
  657. # https://github.com/sympy/sympy/issues/15574
  658. eq7 = [Eq(Derivative(x(t), t), x(t)), Eq(Derivative(y(t), t), y(t)), Eq(Derivative(z(t), t), z(t)),
  659. Eq(Derivative(w(t), t), w(t)), Eq(Derivative(u(t), t), u(t))]
  660. sol7 = [Eq(x(t), C1*exp(t)), Eq(y(t), C2*exp(t)), Eq(z(t), C3*exp(t)), Eq(w(t), C4*exp(t)),
  661. Eq(u(t), C5*exp(t))]
  662. assert dsolve(eq7) == sol7
  663. assert checksysodesol(eq7, sol7) == (True, [0, 0, 0, 0, 0])
  664. eqs8 = [Eq(Derivative(x(t), t), 2*x(t) + y(t)),
  665. Eq(Derivative(y(t), t), 2*y(t)),
  666. Eq(Derivative(z(t), t), 4*z(t)),
  667. Eq(Derivative(w(t), t), u(t) + 5*w(t)),
  668. Eq(Derivative(u(t), t), 5*u(t))]
  669. sol8 = [Eq(x(t), C1*exp(2*t) + C2*t*exp(2*t)),
  670. Eq(y(t), C2*exp(2*t)),
  671. Eq(z(t), C3*exp(4*t)),
  672. Eq(w(t), C4*exp(5*t) + C5*t*exp(5*t)),
  673. Eq(u(t), C5*exp(5*t))]
  674. assert dsolve(eqs8) == sol8
  675. assert checksysodesol(eqs8, sol8) == (True, [0, 0, 0, 0, 0])
  676. # Regression test case for issue #15574
  677. # https://github.com/sympy/sympy/issues/15574
  678. eq9 = [Eq(Derivative(x(t), t), x(t)), Eq(Derivative(y(t), t), y(t)), Eq(Derivative(z(t), t), z(t))]
  679. sol9 = [Eq(x(t), C1*exp(t)), Eq(y(t), C2*exp(t)), Eq(z(t), C3*exp(t))]
  680. assert dsolve(eq9) == sol9
  681. assert checksysodesol(eq9, sol9) == (True, [0, 0, 0])
  682. # Regression test case for issue #15407
  683. # https://github.com/sympy/sympy/issues/15407
  684. eqs10 = [Eq(Derivative(x(t), t), (-a_b - a_c)*x(t)),
  685. Eq(Derivative(y(t), t), a_b*y(t)),
  686. Eq(Derivative(z(t), t), a_c*x(t))]
  687. sol10 = [Eq(x(t), -C1*(a_b + a_c)*exp(-t*(a_b + a_c))/a_c),
  688. Eq(y(t), C2*exp(a_b*t)),
  689. Eq(z(t), C1*exp(-t*(a_b + a_c)) + C3)]
  690. assert dsolve(eqs10) == sol10
  691. assert checksysodesol(eqs10, sol10) == (True, [0, 0, 0])
  692. # Regression test case for issue #14312
  693. # https://github.com/sympy/sympy/issues/14312
  694. eqs11 = [Eq(Derivative(x(t), t), k3*y(t)),
  695. Eq(Derivative(y(t), t), (-k2 - k3)*y(t)),
  696. Eq(Derivative(z(t), t), k2*y(t))]
  697. sol11 = [Eq(x(t), C1 + C2*k3*exp(-t*(k2 + k3))/k2),
  698. Eq(y(t), -C2*(k2 + k3)*exp(-t*(k2 + k3))/k2),
  699. Eq(z(t), C2*exp(-t*(k2 + k3)) + C3)]
  700. assert dsolve(eqs11) == sol11
  701. assert checksysodesol(eqs11, sol11) == (True, [0, 0, 0])
  702. # Regression test case for issue #14312
  703. # https://github.com/sympy/sympy/issues/14312
  704. eqs12 = [Eq(Derivative(z(t), t), k2*y(t)),
  705. Eq(Derivative(x(t), t), k3*y(t)),
  706. Eq(Derivative(y(t), t), (-k2 - k3)*y(t))]
  707. sol12 = [Eq(z(t), C1 - C2*k2*exp(-t*(k2 + k3))/(k2 + k3)),
  708. Eq(x(t), -C2*k3*exp(-t*(k2 + k3))/(k2 + k3) + C3),
  709. Eq(y(t), C2*exp(-t*(k2 + k3)))]
  710. assert dsolve(eqs12) == sol12
  711. assert checksysodesol(eqs12, sol12) == (True, [0, 0, 0])
  712. f, g, h = symbols('f, g, h', cls=Function)
  713. a, b, c = symbols('a, b, c')
  714. # Regression test case for issue #15474
  715. # https://github.com/sympy/sympy/issues/15474
  716. eqs13 = [Eq(Derivative(f(t), t), 2*f(t) + g(t)),
  717. Eq(Derivative(g(t), t), a*f(t))]
  718. sol13 = [Eq(f(t), C1*exp(t*(sqrt(a + 1) + 1))/(sqrt(a + 1) - 1) - C2*exp(-t*(sqrt(a + 1) - 1))/(sqrt(a + 1) +
  719. 1)),
  720. Eq(g(t), C1*exp(t*(sqrt(a + 1) + 1)) + C2*exp(-t*(sqrt(a + 1) - 1)))]
  721. assert dsolve(eqs13) == sol13
  722. assert checksysodesol(eqs13, sol13) == (True, [0, 0])
  723. eqs14 = [Eq(Derivative(f(t), t), 2*g(t) - 3*h(t)),
  724. Eq(Derivative(g(t), t), -2*f(t) + 4*h(t)),
  725. Eq(Derivative(h(t), t), 3*f(t) - 4*g(t))]
  726. sol14 = [Eq(f(t), 2*C1 - sin(sqrt(29)*t)*(sqrt(29)*C2*Rational(3, 25) + C3*Rational(-8, 25)) -
  727. cos(sqrt(29)*t)*(C2*Rational(8, 25) + sqrt(29)*C3*Rational(3, 25))),
  728. Eq(g(t), C1*Rational(3, 2) + sin(sqrt(29)*t)*(sqrt(29)*C2*Rational(4, 25) + C3*Rational(6, 25)) -
  729. cos(sqrt(29)*t)*(C2*Rational(6, 25) + sqrt(29)*C3*Rational(-4, 25))),
  730. Eq(h(t), C1 + C2*cos(sqrt(29)*t) - C3*sin(sqrt(29)*t))]
  731. assert dsolve(eqs14) == sol14
  732. assert checksysodesol(eqs14, sol14) == (True, [0, 0, 0])
  733. eqs15 = [Eq(2*Derivative(f(t), t), 12*g(t) - 12*h(t)),
  734. Eq(3*Derivative(g(t), t), -8*f(t) + 8*h(t)),
  735. Eq(4*Derivative(h(t), t), 6*f(t) - 6*g(t))]
  736. sol15 = [Eq(f(t), C1 - sin(sqrt(29)*t)*(sqrt(29)*C2*Rational(6, 13) + C3*Rational(-16, 13)) -
  737. cos(sqrt(29)*t)*(C2*Rational(16, 13) + sqrt(29)*C3*Rational(6, 13))),
  738. Eq(g(t), C1 + sin(sqrt(29)*t)*(sqrt(29)*C2*Rational(8, 39) + C3*Rational(16, 13)) -
  739. cos(sqrt(29)*t)*(C2*Rational(16, 13) + sqrt(29)*C3*Rational(-8, 39))),
  740. Eq(h(t), C1 + C2*cos(sqrt(29)*t) - C3*sin(sqrt(29)*t))]
  741. assert dsolve(eqs15) == sol15
  742. assert checksysodesol(eqs15, sol15) == (True, [0, 0, 0])
  743. eq16 = (Eq(diff(x(t), t), 21*x(t)), Eq(diff(y(t), t), 17*x(t) + 3*y(t)),
  744. Eq(diff(z(t), t), 5*x(t) + 7*y(t) + 9*z(t)))
  745. sol16 = [Eq(x(t), 216*C1*exp(21*t)/209),
  746. Eq(y(t), 204*C1*exp(21*t)/209 - 6*C2*exp(3*t)/7),
  747. Eq(z(t), C1*exp(21*t) + C2*exp(3*t) + C3*exp(9*t))]
  748. assert dsolve(eq16) == sol16
  749. assert checksysodesol(eq16, sol16) == (True, [0, 0, 0])
  750. eqs17 = [Eq(Derivative(x(t), t), 3*y(t) - 11*z(t)),
  751. Eq(Derivative(y(t), t), -3*x(t) + 7*z(t)),
  752. Eq(Derivative(z(t), t), 11*x(t) - 7*y(t))]
  753. sol17 = [Eq(x(t), C1*Rational(7, 3) - sin(sqrt(179)*t)*(sqrt(179)*C2*Rational(11, 170) + C3*Rational(-21,
  754. 170)) - cos(sqrt(179)*t)*(C2*Rational(21, 170) + sqrt(179)*C3*Rational(11, 170))),
  755. Eq(y(t), C1*Rational(11, 3) + sin(sqrt(179)*t)*(sqrt(179)*C2*Rational(7, 170) + C3*Rational(33,
  756. 170)) - cos(sqrt(179)*t)*(C2*Rational(33, 170) + sqrt(179)*C3*Rational(-7, 170))),
  757. Eq(z(t), C1 + C2*cos(sqrt(179)*t) - C3*sin(sqrt(179)*t))]
  758. assert dsolve(eqs17) == sol17
  759. assert checksysodesol(eqs17, sol17) == (True, [0, 0, 0])
  760. eqs18 = [Eq(3*Derivative(x(t), t), 20*y(t) - 20*z(t)),
  761. Eq(4*Derivative(y(t), t), -15*x(t) + 15*z(t)),
  762. Eq(5*Derivative(z(t), t), 12*x(t) - 12*y(t))]
  763. sol18 = [Eq(x(t), C1 - sin(5*sqrt(2)*t)*(sqrt(2)*C2*Rational(4, 3) - C3) - cos(5*sqrt(2)*t)*(C2 +
  764. sqrt(2)*C3*Rational(4, 3))),
  765. Eq(y(t), C1 + sin(5*sqrt(2)*t)*(sqrt(2)*C2*Rational(3, 4) + C3) - cos(5*sqrt(2)*t)*(C2 +
  766. sqrt(2)*C3*Rational(-3, 4))),
  767. Eq(z(t), C1 + C2*cos(5*sqrt(2)*t) - C3*sin(5*sqrt(2)*t))]
  768. assert dsolve(eqs18) == sol18
  769. assert checksysodesol(eqs18, sol18) == (True, [0, 0, 0])
  770. eqs19 = [Eq(Derivative(x(t), t), 4*x(t) - z(t)),
  771. Eq(Derivative(y(t), t), 2*x(t) + 2*y(t) - z(t)),
  772. Eq(Derivative(z(t), t), 3*x(t) + y(t))]
  773. sol19 = [Eq(x(t), C2*t**2*exp(2*t)/2 + t*(2*C2 + C3)*exp(2*t) + (C1 + C2 + 2*C3)*exp(2*t)),
  774. Eq(y(t), C2*t**2*exp(2*t)/2 + t*(2*C2 + C3)*exp(2*t) + (C1 + 2*C3)*exp(2*t)),
  775. Eq(z(t), C2*t**2*exp(2*t) + t*(3*C2 + 2*C3)*exp(2*t) + (2*C1 + 3*C3)*exp(2*t))]
  776. assert dsolve(eqs19) == sol19
  777. assert checksysodesol(eqs19, sol19) == (True, [0, 0, 0])
  778. eqs20 = [Eq(Derivative(x(t), t), 4*x(t) - y(t) - 2*z(t)),
  779. Eq(Derivative(y(t), t), 2*x(t) + y(t) - 2*z(t)),
  780. Eq(Derivative(z(t), t), 5*x(t) - 3*z(t))]
  781. sol20 = [Eq(x(t), C1*exp(2*t) - sin(t)*(C2*Rational(3, 5) + C3/5) - cos(t)*(C2/5 + C3*Rational(-3, 5))),
  782. Eq(y(t), -sin(t)*(C2*Rational(3, 5) + C3/5) - cos(t)*(C2/5 + C3*Rational(-3, 5))),
  783. Eq(z(t), C1*exp(2*t) - C2*sin(t) + C3*cos(t))]
  784. assert dsolve(eqs20) == sol20
  785. assert checksysodesol(eqs20, sol20) == (True, [0, 0, 0])
  786. eq21 = (Eq(diff(x(t), t), 9*y(t)), Eq(diff(y(t), t), 12*x(t)))
  787. sol21 = [Eq(x(t), -sqrt(3)*C1*exp(-6*sqrt(3)*t)/2 + sqrt(3)*C2*exp(6*sqrt(3)*t)/2),
  788. Eq(y(t), C1*exp(-6*sqrt(3)*t) + C2*exp(6*sqrt(3)*t))]
  789. assert dsolve(eq21) == sol21
  790. assert checksysodesol(eq21, sol21) == (True, [0, 0])
  791. eqs22 = [Eq(Derivative(x(t), t), 2*x(t) + 4*y(t)),
  792. Eq(Derivative(y(t), t), 12*x(t) + 41*y(t))]
  793. sol22 = [Eq(x(t), C1*(39 - sqrt(1713))*exp(t*(sqrt(1713) + 43)/2)*Rational(-1, 24) + C2*(39 +
  794. sqrt(1713))*exp(t*(43 - sqrt(1713))/2)*Rational(-1, 24)),
  795. Eq(y(t), C1*exp(t*(sqrt(1713) + 43)/2) + C2*exp(t*(43 - sqrt(1713))/2))]
  796. assert dsolve(eqs22) == sol22
  797. assert checksysodesol(eqs22, sol22) == (True, [0, 0])
  798. eqs23 = [Eq(Derivative(x(t), t), x(t) + y(t)),
  799. Eq(Derivative(y(t), t), -2*x(t) + 2*y(t))]
  800. sol23 = [Eq(x(t), (C1/4 + sqrt(7)*C2/4)*cos(sqrt(7)*t/2)*exp(t*Rational(3, 2)) +
  801. sin(sqrt(7)*t/2)*(sqrt(7)*C1/4 + C2*Rational(-1, 4))*exp(t*Rational(3, 2))),
  802. Eq(y(t), C1*cos(sqrt(7)*t/2)*exp(t*Rational(3, 2)) - C2*sin(sqrt(7)*t/2)*exp(t*Rational(3, 2)))]
  803. assert dsolve(eqs23) == sol23
  804. assert checksysodesol(eqs23, sol23) == (True, [0, 0])
  805. # Regression test case for issue #15474
  806. # https://github.com/sympy/sympy/issues/15474
  807. a = Symbol("a", real=True)
  808. eq24 = [x(t).diff(t) - a*y(t), y(t).diff(t) + a*x(t)]
  809. sol24 = [Eq(x(t), C1*sin(a*t) + C2*cos(a*t)), Eq(y(t), C1*cos(a*t) - C2*sin(a*t))]
  810. assert dsolve(eq24) == sol24
  811. assert checksysodesol(eq24, sol24) == (True, [0, 0])
  812. # Regression test case for issue #19150
  813. # https://github.com/sympy/sympy/issues/19150
  814. eqs25 = [Eq(Derivative(f(t), t), 0),
  815. Eq(Derivative(g(t), t), (f(t) - 2*g(t) + x(t))/(b*c)),
  816. Eq(Derivative(x(t), t), (g(t) - 2*x(t) + y(t))/(b*c)),
  817. Eq(Derivative(y(t), t), (h(t) + x(t) - 2*y(t))/(b*c)),
  818. Eq(Derivative(h(t), t), 0)]
  819. sol25 = [Eq(f(t), -3*C1 + 4*C2),
  820. Eq(g(t), -2*C1 + 3*C2 - C3*exp(-2*t/(b*c)) + C4*exp(-t*(sqrt(2) + 2)/(b*c)) + C5*exp(-t*(2 -
  821. sqrt(2))/(b*c))),
  822. Eq(x(t), -C1 + 2*C2 - sqrt(2)*C4*exp(-t*(sqrt(2) + 2)/(b*c)) + sqrt(2)*C5*exp(-t*(2 -
  823. sqrt(2))/(b*c))),
  824. Eq(y(t), C2 + C3*exp(-2*t/(b*c)) + C4*exp(-t*(sqrt(2) + 2)/(b*c)) + C5*exp(-t*(2 - sqrt(2))/(b*c))),
  825. Eq(h(t), C1)]
  826. assert dsolve(eqs25) == sol25
  827. assert checksysodesol(eqs25, sol25) == (True, [0, 0, 0, 0, 0])
  828. eq26 = [Eq(Derivative(f(t), t), 2*f(t)), Eq(Derivative(g(t), t), 3*f(t) + 7*g(t))]
  829. sol26 = [Eq(f(t), -5*C1*exp(2*t)/3), Eq(g(t), C1*exp(2*t) + C2*exp(7*t))]
  830. assert dsolve(eq26) == sol26
  831. assert checksysodesol(eq26, sol26) == (True, [0, 0])
  832. eq27 = [Eq(Derivative(f(t), t), -9*I*f(t) - 4*g(t)), Eq(Derivative(g(t), t), -4*I*g(t))]
  833. sol27 = [Eq(f(t), 4*I*C1*exp(-4*I*t)/5 + C2*exp(-9*I*t)), Eq(g(t), C1*exp(-4*I*t))]
  834. assert dsolve(eq27) == sol27
  835. assert checksysodesol(eq27, sol27) == (True, [0, 0])
  836. eq28 = [Eq(Derivative(f(t), t), -9*I*f(t)), Eq(Derivative(g(t), t), -4*I*g(t))]
  837. sol28 = [Eq(f(t), C1*exp(-9*I*t)), Eq(g(t), C2*exp(-4*I*t))]
  838. assert dsolve(eq28) == sol28
  839. assert checksysodesol(eq28, sol28) == (True, [0, 0])
  840. eq29 = [Eq(Derivative(f(t), t), 0), Eq(Derivative(g(t), t), 0)]
  841. sol29 = [Eq(f(t), C1), Eq(g(t), C2)]
  842. assert dsolve(eq29) == sol29
  843. assert checksysodesol(eq29, sol29) == (True, [0, 0])
  844. eq30 = [Eq(Derivative(f(t), t), f(t)), Eq(Derivative(g(t), t), 0)]
  845. sol30 = [Eq(f(t), C1*exp(t)), Eq(g(t), C2)]
  846. assert dsolve(eq30) == sol30
  847. assert checksysodesol(eq30, sol30) == (True, [0, 0])
  848. eq31 = [Eq(Derivative(f(t), t), g(t)), Eq(Derivative(g(t), t), 0)]
  849. sol31 = [Eq(f(t), C1 + C2*t), Eq(g(t), C2)]
  850. assert dsolve(eq31) == sol31
  851. assert checksysodesol(eq31, sol31) == (True, [0, 0])
  852. eq32 = [Eq(Derivative(f(t), t), 0), Eq(Derivative(g(t), t), f(t))]
  853. sol32 = [Eq(f(t), C1), Eq(g(t), C1*t + C2)]
  854. assert dsolve(eq32) == sol32
  855. assert checksysodesol(eq32, sol32) == (True, [0, 0])
  856. eq33 = [Eq(Derivative(f(t), t), 0), Eq(Derivative(g(t), t), g(t))]
  857. sol33 = [Eq(f(t), C1), Eq(g(t), C2*exp(t))]
  858. assert dsolve(eq33) == sol33
  859. assert checksysodesol(eq33, sol33) == (True, [0, 0])
  860. eq34 = [Eq(Derivative(f(t), t), f(t)), Eq(Derivative(g(t), t), I*g(t))]
  861. sol34 = [Eq(f(t), C1*exp(t)), Eq(g(t), C2*exp(I*t))]
  862. assert dsolve(eq34) == sol34
  863. assert checksysodesol(eq34, sol34) == (True, [0, 0])
  864. eq35 = [Eq(Derivative(f(t), t), I*f(t)), Eq(Derivative(g(t), t), -I*g(t))]
  865. sol35 = [Eq(f(t), C1*exp(I*t)), Eq(g(t), C2*exp(-I*t))]
  866. assert dsolve(eq35) == sol35
  867. assert checksysodesol(eq35, sol35) == (True, [0, 0])
  868. eq36 = [Eq(Derivative(f(t), t), I*g(t)), Eq(Derivative(g(t), t), 0)]
  869. sol36 = [Eq(f(t), I*C1 + I*C2*t), Eq(g(t), C2)]
  870. assert dsolve(eq36) == sol36
  871. assert checksysodesol(eq36, sol36) == (True, [0, 0])
  872. eq37 = [Eq(Derivative(f(t), t), I*g(t)), Eq(Derivative(g(t), t), I*f(t))]
  873. sol37 = [Eq(f(t), -C1*exp(-I*t) + C2*exp(I*t)), Eq(g(t), C1*exp(-I*t) + C2*exp(I*t))]
  874. assert dsolve(eq37) == sol37
  875. assert checksysodesol(eq37, sol37) == (True, [0, 0])
  876. # Multiple systems
  877. eq1 = [Eq(Derivative(f(t), t)**2, g(t)**2), Eq(-f(t) + Derivative(g(t), t), 0)]
  878. sol1 = [[Eq(f(t), -C1*sin(t) - C2*cos(t)),
  879. Eq(g(t), C1*cos(t) - C2*sin(t))],
  880. [Eq(f(t), -C1*exp(-t) + C2*exp(t)),
  881. Eq(g(t), C1*exp(-t) + C2*exp(t))]]
  882. assert dsolve(eq1) == sol1
  883. for sol in sol1:
  884. assert checksysodesol(eq1, sol) == (True, [0, 0])
  885. def test_sysode_linear_neq_order1_type2():
  886. f, g, h, k = symbols('f g h k', cls=Function)
  887. x, t, a, b, c, d, y = symbols('x t a b c d y')
  888. k1, k2 = symbols('k1 k2')
  889. eqs1 = [Eq(Derivative(f(x), x), f(x) + g(x) + 5),
  890. Eq(Derivative(g(x), x), -f(x) - g(x) + 7)]
  891. sol1 = [Eq(f(x), C1 + C2 + 6*x**2 + x*(C2 + 5)),
  892. Eq(g(x), -C1 - 6*x**2 - x*(C2 - 7))]
  893. assert dsolve(eqs1) == sol1
  894. assert checksysodesol(eqs1, sol1) == (True, [0, 0])
  895. eqs2 = [Eq(Derivative(f(x), x), f(x) + g(x) + 5),
  896. Eq(Derivative(g(x), x), f(x) + g(x) + 7)]
  897. sol2 = [Eq(f(x), -C1 + C2*exp(2*x) - x - 3),
  898. Eq(g(x), C1 + C2*exp(2*x) + x - 3)]
  899. assert dsolve(eqs2) == sol2
  900. assert checksysodesol(eqs2, sol2) == (True, [0, 0])
  901. eqs3 = [Eq(Derivative(f(x), x), f(x) + 5),
  902. Eq(Derivative(g(x), x), f(x) + 7)]
  903. sol3 = [Eq(f(x), C1*exp(x) - 5),
  904. Eq(g(x), C1*exp(x) + C2 + 2*x - 5)]
  905. assert dsolve(eqs3) == sol3
  906. assert checksysodesol(eqs3, sol3) == (True, [0, 0])
  907. eqs4 = [Eq(Derivative(f(x), x), f(x) + exp(x)),
  908. Eq(Derivative(g(x), x), x*exp(x) + f(x) + g(x))]
  909. sol4 = [Eq(f(x), C1*exp(x) + x*exp(x)),
  910. Eq(g(x), C1*x*exp(x) + C2*exp(x) + x**2*exp(x))]
  911. assert dsolve(eqs4) == sol4
  912. assert checksysodesol(eqs4, sol4) == (True, [0, 0])
  913. eqs5 = [Eq(Derivative(f(x), x), 5*x + f(x) + g(x)),
  914. Eq(Derivative(g(x), x), f(x) - g(x))]
  915. sol5 = [Eq(f(x), C1*(1 + sqrt(2))*exp(sqrt(2)*x) + C2*(1 - sqrt(2))*exp(-sqrt(2)*x) + x*Rational(-5, 2) +
  916. Rational(-5, 2)),
  917. Eq(g(x), C1*exp(sqrt(2)*x) + C2*exp(-sqrt(2)*x) + x*Rational(-5, 2))]
  918. assert dsolve(eqs5) == sol5
  919. assert checksysodesol(eqs5, sol5) == (True, [0, 0])
  920. eqs6 = [Eq(Derivative(f(x), x), -9*f(x) - 4*g(x)),
  921. Eq(Derivative(g(x), x), -4*g(x)),
  922. Eq(Derivative(h(x), x), h(x) + exp(x))]
  923. sol6 = [Eq(f(x), C2*exp(-4*x)*Rational(-4, 5) + C1*exp(-9*x)),
  924. Eq(g(x), C2*exp(-4*x)),
  925. Eq(h(x), C3*exp(x) + x*exp(x))]
  926. assert dsolve(eqs6) == sol6
  927. assert checksysodesol(eqs6, sol6) == (True, [0, 0, 0])
  928. # Regression test case for issue #8859
  929. # https://github.com/sympy/sympy/issues/8859
  930. eqs7 = [Eq(Derivative(f(t), t), 3*t + f(t)),
  931. Eq(Derivative(g(t), t), g(t))]
  932. sol7 = [Eq(f(t), C1*exp(t) - 3*t - 3),
  933. Eq(g(t), C2*exp(t))]
  934. assert dsolve(eqs7) == sol7
  935. assert checksysodesol(eqs7, sol7) == (True, [0, 0])
  936. # Regression test case for issue #8567
  937. # https://github.com/sympy/sympy/issues/8567
  938. eqs8 = [Eq(Derivative(f(t), t), f(t) + 2*g(t)),
  939. Eq(Derivative(g(t), t), -2*f(t) + g(t) + 2*exp(t))]
  940. sol8 = [Eq(f(t), C1*exp(t)*sin(2*t) + C2*exp(t)*cos(2*t)
  941. + exp(t)*sin(2*t)**2 + exp(t)*cos(2*t)**2),
  942. Eq(g(t), C1*exp(t)*cos(2*t) - C2*exp(t)*sin(2*t))]
  943. assert dsolve(eqs8) == sol8
  944. assert checksysodesol(eqs8, sol8) == (True, [0, 0])
  945. # Regression test case for issue #19150
  946. # https://github.com/sympy/sympy/issues/19150
  947. eqs9 = [Eq(Derivative(f(t), t), (c - 2*f(t) + g(t))/(a*b)),
  948. Eq(Derivative(g(t), t), (f(t) - 2*g(t) + h(t))/(a*b)),
  949. Eq(Derivative(h(t), t), (d + g(t) - 2*h(t))/(a*b))]
  950. sol9 = [Eq(f(t), -C1*exp(-2*t/(a*b)) + C2*exp(-t*(sqrt(2) + 2)/(a*b)) + C3*exp(-t*(2 - sqrt(2))/(a*b)) +
  951. Mul(Rational(1, 4), 3*c + d, evaluate=False)),
  952. Eq(g(t), -sqrt(2)*C2*exp(-t*(sqrt(2) + 2)/(a*b)) + sqrt(2)*C3*exp(-t*(2 - sqrt(2))/(a*b)) +
  953. Mul(Rational(1, 2), c + d, evaluate=False)),
  954. Eq(h(t), C1*exp(-2*t/(a*b)) + C2*exp(-t*(sqrt(2) + 2)/(a*b)) + C3*exp(-t*(2 - sqrt(2))/(a*b)) +
  955. Mul(Rational(1, 4), c + 3*d, evaluate=False))]
  956. assert dsolve(eqs9) == sol9
  957. assert checksysodesol(eqs9, sol9) == (True, [0, 0, 0])
  958. # Regression test case for issue #16635
  959. # https://github.com/sympy/sympy/issues/16635
  960. eqs10 = [Eq(Derivative(f(t), t), 15*t + f(t) - g(t) - 10),
  961. Eq(Derivative(g(t), t), -15*t + f(t) - g(t) - 5)]
  962. sol10 = [Eq(f(t), C1 + C2 + 5*t**3 + 5*t**2 + t*(C2 - 10)),
  963. Eq(g(t), C1 + 5*t**3 - 10*t**2 + t*(C2 - 5))]
  964. assert dsolve(eqs10) == sol10
  965. assert checksysodesol(eqs10, sol10) == (True, [0, 0])
  966. # Multiple solutions
  967. eqs11 = [Eq(Derivative(f(t), t)**2 - 2*Derivative(f(t), t) + 1, 4),
  968. Eq(-y*f(t) + Derivative(g(t), t), 0)]
  969. sol11 = [[Eq(f(t), C1 - t), Eq(g(t), C1*t*y + C2*y + t**2*y*Rational(-1, 2))],
  970. [Eq(f(t), C1 + 3*t), Eq(g(t), C1*t*y + C2*y + t**2*y*Rational(3, 2))]]
  971. assert dsolve(eqs11) == sol11
  972. for s11 in sol11:
  973. assert checksysodesol(eqs11, s11) == (True, [0, 0])
  974. # test case for issue #19831
  975. # https://github.com/sympy/sympy/issues/19831
  976. n = symbols('n', positive=True)
  977. x0 = symbols('x_0')
  978. t0 = symbols('t_0')
  979. x_0 = symbols('x_0')
  980. t_0 = symbols('t_0')
  981. t = symbols('t')
  982. x = Function('x')
  983. y = Function('y')
  984. T = symbols('T')
  985. eqs12 = [Eq(Derivative(y(t), t), x(t)),
  986. Eq(Derivative(x(t), t), n*(y(t) + 1))]
  987. sol12 = [Eq(y(t), C1*exp(sqrt(n)*t)*n**Rational(-1, 2) - C2*exp(-sqrt(n)*t)*n**Rational(-1, 2) - 1),
  988. Eq(x(t), C1*exp(sqrt(n)*t) + C2*exp(-sqrt(n)*t))]
  989. assert dsolve(eqs12) == sol12
  990. assert checksysodesol(eqs12, sol12) == (True, [0, 0])
  991. sol12b = [
  992. Eq(y(t), (T*exp(-sqrt(n)*t_0)/2 + exp(-sqrt(n)*t_0)/2 +
  993. x_0*exp(-sqrt(n)*t_0)/(2*sqrt(n)))*exp(sqrt(n)*t) +
  994. (T*exp(sqrt(n)*t_0)/2 + exp(sqrt(n)*t_0)/2 -
  995. x_0*exp(sqrt(n)*t_0)/(2*sqrt(n)))*exp(-sqrt(n)*t) - 1),
  996. Eq(x(t), (T*sqrt(n)*exp(-sqrt(n)*t_0)/2 + sqrt(n)*exp(-sqrt(n)*t_0)/2
  997. + x_0*exp(-sqrt(n)*t_0)/2)*exp(sqrt(n)*t)
  998. - (T*sqrt(n)*exp(sqrt(n)*t_0)/2 + sqrt(n)*exp(sqrt(n)*t_0)/2 -
  999. x_0*exp(sqrt(n)*t_0)/2)*exp(-sqrt(n)*t))
  1000. ]
  1001. assert dsolve(eqs12, ics={y(t0): T, x(t0): x0}) == sol12b
  1002. assert checksysodesol(eqs12, sol12b) == (True, [0, 0])
  1003. #Test cases added for the issue 19763
  1004. #https://github.com/sympy/sympy/issues/19763
  1005. eq13 = [Eq(Derivative(f(t), t), f(t) + g(t) + 9),
  1006. Eq(Derivative(g(t), t), 2*f(t) + 5*g(t) + 23)]
  1007. sol13 = [Eq(f(t), -C1*(2 + sqrt(6))*exp(t*(3 - sqrt(6)))/2 - C2*(2 - sqrt(6))*exp(t*(sqrt(6) + 3))/2 -
  1008. Rational(22,3)),
  1009. Eq(g(t), C1*exp(t*(3 - sqrt(6))) + C2*exp(t*(sqrt(6) + 3)) - Rational(5,3))]
  1010. assert dsolve(eq13) == sol13
  1011. assert checksysodesol(eq13, sol13) == (True, [0, 0])
  1012. eq14 = [Eq(Derivative(f(t), t), f(t) + g(t) + 81),
  1013. Eq(Derivative(g(t), t), -2*f(t) + g(t) + 23)]
  1014. sol14 = [Eq(f(t), sqrt(2)*C1*exp(t)*sin(sqrt(2)*t)/2
  1015. + sqrt(2)*C2*exp(t)*cos(sqrt(2)*t)/2
  1016. - 58*sin(sqrt(2)*t)**2/3 - 58*cos(sqrt(2)*t)**2/3),
  1017. Eq(g(t), C1*exp(t)*cos(sqrt(2)*t) - C2*exp(t)*sin(sqrt(2)*t)
  1018. - 185*sin(sqrt(2)*t)**2/3 - 185*cos(sqrt(2)*t)**2/3)]
  1019. assert dsolve(eq14) == sol14
  1020. assert checksysodesol(eq14, sol14) == (True, [0,0])
  1021. eq15 = [Eq(Derivative(f(t), t), f(t) + 2*g(t) + k1),
  1022. Eq(Derivative(g(t), t), 3*f(t) + 4*g(t) + k2)]
  1023. sol15 = [Eq(f(t), -C1*(3 - sqrt(33))*exp(t*(5 + sqrt(33))/2)/6 -
  1024. C2*(3 + sqrt(33))*exp(t*(5 - sqrt(33))/2)/6 + 2*k1 - k2),
  1025. Eq(g(t), C1*exp(t*(5 + sqrt(33))/2) + C2*exp(t*(5 - sqrt(33))/2) -
  1026. Mul(Rational(1,2), 3*k1 - k2, evaluate = False))]
  1027. assert dsolve(eq15) == sol15
  1028. assert checksysodesol(eq15, sol15) == (True, [0,0])
  1029. eq16 = [Eq(Derivative(f(t), t), k1),
  1030. Eq(Derivative(g(t), t), k2)]
  1031. sol16 = [Eq(f(t), C1 + k1*t),
  1032. Eq(g(t), C2 + k2*t)]
  1033. assert dsolve(eq16) == sol16
  1034. assert checksysodesol(eq16, sol16) == (True, [0,0])
  1035. eq17 = [Eq(Derivative(f(t), t), 0),
  1036. Eq(Derivative(g(t), t), c*f(t) + k2)]
  1037. sol17 = [Eq(f(t), C1),
  1038. Eq(g(t), C2*c + t*(C1*c + k2))]
  1039. assert dsolve(eq17) == sol17
  1040. assert checksysodesol(eq17 , sol17) == (True , [0,0])
  1041. eq18 = [Eq(Derivative(f(t), t), k1),
  1042. Eq(Derivative(g(t), t), f(t) + k2)]
  1043. sol18 = [Eq(f(t), C1 + k1*t),
  1044. Eq(g(t), C2 + k1*t**2/2 + t*(C1 + k2))]
  1045. assert dsolve(eq18) == sol18
  1046. assert checksysodesol(eq18 , sol18) == (True , [0,0])
  1047. eq19 = [Eq(Derivative(f(t), t), k1),
  1048. Eq(Derivative(g(t), t), f(t) + 2*g(t) + k2)]
  1049. sol19 = [Eq(f(t), -2*C1 + k1*t),
  1050. Eq(g(t), C1 + C2*exp(2*t) - k1*t/2 - Mul(Rational(1,4), k1 + 2*k2 , evaluate = False))]
  1051. assert dsolve(eq19) == sol19
  1052. assert checksysodesol(eq19 , sol19) == (True , [0,0])
  1053. eq20 = [Eq(diff(f(t), t), f(t) + k1),
  1054. Eq(diff(g(t), t), k2)]
  1055. sol20 = [Eq(f(t), C1*exp(t) - k1),
  1056. Eq(g(t), C2 + k2*t)]
  1057. assert dsolve(eq20) == sol20
  1058. assert checksysodesol(eq20 , sol20) == (True , [0,0])
  1059. eq21 = [Eq(diff(f(t), t), g(t) + k1),
  1060. Eq(diff(g(t), t), 0)]
  1061. sol21 = [Eq(f(t), C1 + t*(C2 + k1)),
  1062. Eq(g(t), C2)]
  1063. assert dsolve(eq21) == sol21
  1064. assert checksysodesol(eq21 , sol21) == (True , [0,0])
  1065. eq22 = [Eq(Derivative(f(t), t), f(t) + 2*g(t) + k1),
  1066. Eq(Derivative(g(t), t), k2)]
  1067. sol22 = [Eq(f(t), -2*C1 + C2*exp(t) - k1 - 2*k2*t - 2*k2),
  1068. Eq(g(t), C1 + k2*t)]
  1069. assert dsolve(eq22) == sol22
  1070. assert checksysodesol(eq22 , sol22) == (True , [0,0])
  1071. eq23 = [Eq(Derivative(f(t), t), g(t) + k1),
  1072. Eq(Derivative(g(t), t), 2*g(t) + k2)]
  1073. sol23 = [Eq(f(t), C1 + C2*exp(2*t)/2 - k2/4 + t*(2*k1 - k2)/2),
  1074. Eq(g(t), C2*exp(2*t) - k2/2)]
  1075. assert dsolve(eq23) == sol23
  1076. assert checksysodesol(eq23 , sol23) == (True , [0,0])
  1077. eq24 = [Eq(Derivative(f(t), t), f(t) + k1),
  1078. Eq(Derivative(g(t), t), 2*f(t) + k2)]
  1079. sol24 = [Eq(f(t), C1*exp(t)/2 - k1),
  1080. Eq(g(t), C1*exp(t) + C2 - 2*k1 - t*(2*k1 - k2))]
  1081. assert dsolve(eq24) == sol24
  1082. assert checksysodesol(eq24 , sol24) == (True , [0,0])
  1083. eq25 = [Eq(Derivative(f(t), t), f(t) + 2*g(t) + k1),
  1084. Eq(Derivative(g(t), t), 3*f(t) + 6*g(t) + k2)]
  1085. sol25 = [Eq(f(t), -2*C1 + C2*exp(7*t)/3 + 2*t*(3*k1 - k2)/7 -
  1086. Mul(Rational(1,49), k1 + 2*k2 , evaluate = False)),
  1087. Eq(g(t), C1 + C2*exp(7*t) - t*(3*k1 - k2)/7 -
  1088. Mul(Rational(3,49), k1 + 2*k2 , evaluate = False))]
  1089. assert dsolve(eq25) == sol25
  1090. assert checksysodesol(eq25 , sol25) == (True , [0,0])
  1091. eq26 = [Eq(Derivative(f(t), t), 2*f(t) - g(t) + k1),
  1092. Eq(Derivative(g(t), t), 4*f(t) - 2*g(t) + 2*k1)]
  1093. sol26 = [Eq(f(t), C1 + 2*C2 + t*(2*C1 + k1)),
  1094. Eq(g(t), 4*C2 + t*(4*C1 + 2*k1))]
  1095. assert dsolve(eq26) == sol26
  1096. assert checksysodesol(eq26 , sol26) == (True , [0,0])
  1097. # Test Case added for issue #22715
  1098. # https://github.com/sympy/sympy/issues/22715
  1099. eq27 = [Eq(diff(x(t),t),-1*y(t)+10), Eq(diff(y(t),t),5*x(t)-2*y(t)+3)]
  1100. sol27 = [Eq(x(t), (C1/5 - 2*C2/5)*exp(-t)*cos(2*t)
  1101. - (2*C1/5 + C2/5)*exp(-t)*sin(2*t)
  1102. + 17*sin(2*t)**2/5 + 17*cos(2*t)**2/5),
  1103. Eq(y(t), C1*exp(-t)*cos(2*t) - C2*exp(-t)*sin(2*t)
  1104. + 10*sin(2*t)**2 + 10*cos(2*t)**2)]
  1105. assert dsolve(eq27) == sol27
  1106. assert checksysodesol(eq27 , sol27) == (True , [0,0])
  1107. def test_sysode_linear_neq_order1_type3():
  1108. f, g, h, k, x0 , y0 = symbols('f g h k x0 y0', cls=Function)
  1109. x, t, a = symbols('x t a')
  1110. r = symbols('r', real=True)
  1111. eqs1 = [Eq(Derivative(f(r), r), r*g(r) + f(r)),
  1112. Eq(Derivative(g(r), r), -r*f(r) + g(r))]
  1113. sol1 = [Eq(f(r), C1*exp(r)*sin(r**2/2) + C2*exp(r)*cos(r**2/2)),
  1114. Eq(g(r), C1*exp(r)*cos(r**2/2) - C2*exp(r)*sin(r**2/2))]
  1115. assert dsolve(eqs1) == sol1
  1116. assert checksysodesol(eqs1, sol1) == (True, [0, 0])
  1117. eqs2 = [Eq(Derivative(f(x), x), x**2*g(x) + x*f(x)),
  1118. Eq(Derivative(g(x), x), 2*x**2*f(x) + (3*x**2 + x)*g(x))]
  1119. sol2 = [Eq(f(x), (sqrt(17)*C1/17 + C2*(17 - 3*sqrt(17))/34)*exp(x**3*(3 + sqrt(17))/6 + x**2/2) -
  1120. exp(x**3*(3 - sqrt(17))/6 + x**2/2)*(sqrt(17)*C1/17 + C2*(3*sqrt(17) + 17)*Rational(-1, 34))),
  1121. Eq(g(x), exp(x**3*(3 - sqrt(17))/6 + x**2/2)*(C1*(17 - 3*sqrt(17))/34 + sqrt(17)*C2*Rational(-2,
  1122. 17)) + exp(x**3*(3 + sqrt(17))/6 + x**2/2)*(C1*(3*sqrt(17) + 17)/34 + sqrt(17)*C2*Rational(2, 17)))]
  1123. assert dsolve(eqs2) == sol2
  1124. assert checksysodesol(eqs2, sol2) == (True, [0, 0])
  1125. eqs3 = [Eq(f(x).diff(x), x*f(x) + g(x)),
  1126. Eq(g(x).diff(x), -f(x) + x*g(x))]
  1127. sol3 = [Eq(f(x), (C1/2 + I*C2/2)*exp(x**2/2 - I*x) + exp(x**2/2 + I*x)*(C1/2 + I*C2*Rational(-1, 2))),
  1128. Eq(g(x), (I*C1/2 + C2/2)*exp(x**2/2 + I*x) - exp(x**2/2 - I*x)*(I*C1/2 + C2*Rational(-1, 2)))]
  1129. assert dsolve(eqs3) == sol3
  1130. assert checksysodesol(eqs3, sol3) == (True, [0, 0])
  1131. eqs4 = [Eq(f(x).diff(x), x*(f(x) + g(x) + h(x))), Eq(g(x).diff(x), x*(f(x) + g(x) + h(x))),
  1132. Eq(h(x).diff(x), x*(f(x) + g(x) + h(x)))]
  1133. sol4 = [Eq(f(x), -C1/3 - C2/3 + 2*C3/3 + (C1/3 + C2/3 + C3/3)*exp(3*x**2/2)),
  1134. Eq(g(x), 2*C1/3 - C2/3 - C3/3 + (C1/3 + C2/3 + C3/3)*exp(3*x**2/2)),
  1135. Eq(h(x), -C1/3 + 2*C2/3 - C3/3 + (C1/3 + C2/3 + C3/3)*exp(3*x**2/2))]
  1136. assert dsolve(eqs4) == sol4
  1137. assert checksysodesol(eqs4, sol4) == (True, [0, 0, 0])
  1138. eqs5 = [Eq(f(x).diff(x), x**2*(f(x) + g(x) + h(x))), Eq(g(x).diff(x), x**2*(f(x) + g(x) + h(x))),
  1139. Eq(h(x).diff(x), x**2*(f(x) + g(x) + h(x)))]
  1140. sol5 = [Eq(f(x), -C1/3 - C2/3 + 2*C3/3 + (C1/3 + C2/3 + C3/3)*exp(x**3)),
  1141. Eq(g(x), 2*C1/3 - C2/3 - C3/3 + (C1/3 + C2/3 + C3/3)*exp(x**3)),
  1142. Eq(h(x), -C1/3 + 2*C2/3 - C3/3 + (C1/3 + C2/3 + C3/3)*exp(x**3))]
  1143. assert dsolve(eqs5) == sol5
  1144. assert checksysodesol(eqs5, sol5) == (True, [0, 0, 0])
  1145. eqs6 = [Eq(Derivative(f(x), x), x*(f(x) + g(x) + h(x) + k(x))),
  1146. Eq(Derivative(g(x), x), x*(f(x) + g(x) + h(x) + k(x))),
  1147. Eq(Derivative(h(x), x), x*(f(x) + g(x) + h(x) + k(x))),
  1148. Eq(Derivative(k(x), x), x*(f(x) + g(x) + h(x) + k(x)))]
  1149. sol6 = [Eq(f(x), -C1/4 - C2/4 - C3/4 + 3*C4/4 + (C1/4 + C2/4 + C3/4 + C4/4)*exp(2*x**2)),
  1150. Eq(g(x), 3*C1/4 - C2/4 - C3/4 - C4/4 + (C1/4 + C2/4 + C3/4 + C4/4)*exp(2*x**2)),
  1151. Eq(h(x), -C1/4 + 3*C2/4 - C3/4 - C4/4 + (C1/4 + C2/4 + C3/4 + C4/4)*exp(2*x**2)),
  1152. Eq(k(x), -C1/4 - C2/4 + 3*C3/4 - C4/4 + (C1/4 + C2/4 + C3/4 + C4/4)*exp(2*x**2))]
  1153. assert dsolve(eqs6) == sol6
  1154. assert checksysodesol(eqs6, sol6) == (True, [0, 0, 0, 0])
  1155. y = symbols("y", real=True)
  1156. eqs7 = [Eq(Derivative(f(y), y), y*f(y) + g(y)),
  1157. Eq(Derivative(g(y), y), y*g(y) - f(y))]
  1158. sol7 = [Eq(f(y), C1*exp(y**2/2)*sin(y) + C2*exp(y**2/2)*cos(y)),
  1159. Eq(g(y), C1*exp(y**2/2)*cos(y) - C2*exp(y**2/2)*sin(y))]
  1160. assert dsolve(eqs7) == sol7
  1161. assert checksysodesol(eqs7, sol7) == (True, [0, 0])
  1162. #Test cases added for the issue 19763
  1163. #https://github.com/sympy/sympy/issues/19763
  1164. eqs8 = [Eq(Derivative(f(t), t), 5*t*f(t) + 2*h(t)),
  1165. Eq(Derivative(h(t), t), 2*f(t) + 5*t*h(t))]
  1166. sol8 = [Eq(f(t), Mul(-1, (C1/2 - C2/2), evaluate = False)*exp(5*t**2/2 - 2*t) + (C1/2 + C2/2)*exp(5*t**2/2 + 2*t)),
  1167. Eq(h(t), (C1/2 - C2/2)*exp(5*t**2/2 - 2*t) + (C1/2 + C2/2)*exp(5*t**2/2 + 2*t))]
  1168. assert dsolve(eqs8) == sol8
  1169. assert checksysodesol(eqs8, sol8) == (True, [0, 0])
  1170. eqs9 = [Eq(diff(f(t), t), 5*t*f(t) + t**2*g(t)),
  1171. Eq(diff(g(t), t), -t**2*f(t) + 5*t*g(t))]
  1172. sol9 = [Eq(f(t), (C1/2 - I*C2/2)*exp(I*t**3/3 + 5*t**2/2) + (C1/2 + I*C2/2)*exp(-I*t**3/3 + 5*t**2/2)),
  1173. Eq(g(t), Mul(-1, (I*C1/2 - C2/2) , evaluate = False)*exp(-I*t**3/3 + 5*t**2/2) + (I*C1/2 + C2/2)*exp(I*t**3/3 + 5*t**2/2))]
  1174. assert dsolve(eqs9) == sol9
  1175. assert checksysodesol(eqs9 , sol9) == (True , [0,0])
  1176. eqs10 = [Eq(diff(f(t), t), t**2*g(t) + 5*t*f(t)),
  1177. Eq(diff(g(t), t), -t**2*f(t) + (9*t**2 + 5*t)*g(t))]
  1178. sol10 = [Eq(f(t), (C1*(77 - 9*sqrt(77))/154 + sqrt(77)*C2/77)*exp(t**3*(sqrt(77) + 9)/6 + 5*t**2/2) + (C1*(77 + 9*sqrt(77))/154 - sqrt(77)*C2/77)*exp(t**3*(9 - sqrt(77))/6 + 5*t**2/2)),
  1179. Eq(g(t), (sqrt(77)*C1/77 + C2*(77 - 9*sqrt(77))/154)*exp(t**3*(9 - sqrt(77))/6 + 5*t**2/2) - (sqrt(77)*C1/77 - C2*(77 + 9*sqrt(77))/154)*exp(t**3*(sqrt(77) + 9)/6 + 5*t**2/2))]
  1180. assert dsolve(eqs10) == sol10
  1181. assert checksysodesol(eqs10 , sol10) == (True , [0,0])
  1182. eqs11 = [Eq(diff(f(t), t), 5*t*f(t) + t**2*g(t)),
  1183. Eq(diff(g(t), t), (1-t**2)*f(t) + (5*t + 9*t**2)*g(t))]
  1184. sol11 = [Eq(f(t), C1*x0(t) + C2*x0(t)*Integral(t**2*exp(Integral(5*t, t))*exp(Integral(9*t**2 + 5*t, t))/x0(t)**2, t)),
  1185. Eq(g(t), C1*y0(t) + C2*(y0(t)*Integral(t**2*exp(Integral(5*t, t))*exp(Integral(9*t**2 + 5*t, t))/x0(t)**2, t) + exp(Integral(5*t, t))*exp(Integral(9*t**2 + 5*t, t))/x0(t)))]
  1186. assert dsolve(eqs11) == sol11
  1187. @slow
  1188. def test_sysode_linear_neq_order1_type4():
  1189. f, g, h, k = symbols('f g h k', cls=Function)
  1190. x, t, a = symbols('x t a')
  1191. r = symbols('r', real=True)
  1192. eqs1 = [Eq(diff(f(r), r), f(r) + r*g(r) + r**2), Eq(diff(g(r), r), -r*f(r) + g(r) + r)]
  1193. sol1 = [Eq(f(r), C1*exp(r)*sin(r**2/2) + C2*exp(r)*cos(r**2/2) + exp(r)*sin(r**2/2)*Integral(r**2*exp(-r)*sin(r**2/2) +
  1194. r*exp(-r)*cos(r**2/2), r) + exp(r)*cos(r**2/2)*Integral(r**2*exp(-r)*cos(r**2/2) - r*exp(-r)*sin(r**2/2), r)),
  1195. Eq(g(r), C1*exp(r)*cos(r**2/2) - C2*exp(r)*sin(r**2/2) - exp(r)*sin(r**2/2)*Integral(r**2*exp(-r)*cos(r**2/2) -
  1196. r*exp(-r)*sin(r**2/2), r) + exp(r)*cos(r**2/2)*Integral(r**2*exp(-r)*sin(r**2/2) + r*exp(-r)*cos(r**2/2), r))]
  1197. assert dsolve(eqs1) == sol1
  1198. assert checksysodesol(eqs1, sol1) == (True, [0, 0])
  1199. eqs2 = [Eq(diff(f(r), r), f(r) + r*g(r) + r), Eq(diff(g(r), r), -r*f(r) + g(r) + log(r))]
  1200. sol2 = [Eq(f(r), C1*exp(r)*sin(r**2/2) + C2*exp(r)*cos(r**2/2) + exp(r)*sin(r**2/2)*Integral(r*exp(-r)*sin(r**2/2) +
  1201. exp(-r)*log(r)*cos(r**2/2), r) + exp(r)*cos(r**2/2)*Integral(r*exp(-r)*cos(r**2/2) - exp(-r)*log(r)*sin(
  1202. r**2/2), r)),
  1203. Eq(g(r), C1*exp(r)*cos(r**2/2) - C2*exp(r)*sin(r**2/2) - exp(r)*sin(r**2/2)*Integral(r*exp(-r)*cos(r**2/2) -
  1204. exp(-r)*log(r)*sin(r**2/2), r) + exp(r)*cos(r**2/2)*Integral(r*exp(-r)*sin(r**2/2) + exp(-r)*log(r)*cos(
  1205. r**2/2), r))]
  1206. # XXX: dsolve hangs for this in integration
  1207. assert dsolve_system(eqs2, simplify=False, doit=False) == [sol2]
  1208. assert checksysodesol(eqs2, sol2) == (True, [0, 0])
  1209. eqs3 = [Eq(Derivative(f(x), x), x*(f(x) + g(x) + h(x)) + x),
  1210. Eq(Derivative(g(x), x), x*(f(x) + g(x) + h(x)) + x),
  1211. Eq(Derivative(h(x), x), x*(f(x) + g(x) + h(x)) + 1)]
  1212. sol3 = [Eq(f(x), C1*Rational(-1, 3) + C2*Rational(-1, 3) + C3*Rational(2, 3) + x**2/6 + x*Rational(-1, 3) +
  1213. (C1/3 + C2/3 + C3/3)*exp(x**2*Rational(3, 2)) +
  1214. sqrt(6)*sqrt(pi)*erf(sqrt(6)*x/2)*exp(x**2*Rational(3, 2))/18 + Rational(-2, 9)),
  1215. Eq(g(x), C1*Rational(2, 3) + C2*Rational(-1, 3) + C3*Rational(-1, 3) + x**2/6 + x*Rational(-1, 3) +
  1216. (C1/3 + C2/3 + C3/3)*exp(x**2*Rational(3, 2)) +
  1217. sqrt(6)*sqrt(pi)*erf(sqrt(6)*x/2)*exp(x**2*Rational(3, 2))/18 + Rational(-2, 9)),
  1218. Eq(h(x), C1*Rational(-1, 3) + C2*Rational(2, 3) + C3*Rational(-1, 3) + x**2*Rational(-1, 3) +
  1219. x*Rational(2, 3) + (C1/3 + C2/3 + C3/3)*exp(x**2*Rational(3, 2)) +
  1220. sqrt(6)*sqrt(pi)*erf(sqrt(6)*x/2)*exp(x**2*Rational(3, 2))/18 + Rational(-2, 9))]
  1221. assert dsolve(eqs3) == sol3
  1222. assert checksysodesol(eqs3, sol3) == (True, [0, 0, 0])
  1223. eqs4 = [Eq(Derivative(f(x), x), x*(f(x) + g(x) + h(x)) + sin(x)),
  1224. Eq(Derivative(g(x), x), x*(f(x) + g(x) + h(x)) + sin(x)),
  1225. Eq(Derivative(h(x), x), x*(f(x) + g(x) + h(x)) + sin(x))]
  1226. sol4 = [Eq(f(x), C1*Rational(-1, 3) + C2*Rational(-1, 3) + C3*Rational(2, 3) + (C1/3 + C2/3 +
  1227. C3/3)*exp(x**2*Rational(3, 2)) + Integral(sin(x)*exp(x**2*Rational(-3, 2)), x)*exp(x**2*Rational(3,
  1228. 2))),
  1229. Eq(g(x), C1*Rational(2, 3) + C2*Rational(-1, 3) + C3*Rational(-1, 3) + (C1/3 + C2/3 +
  1230. C3/3)*exp(x**2*Rational(3, 2)) + Integral(sin(x)*exp(x**2*Rational(-3, 2)), x)*exp(x**2*Rational(3,
  1231. 2))),
  1232. Eq(h(x), C1*Rational(-1, 3) + C2*Rational(2, 3) + C3*Rational(-1, 3) + (C1/3 + C2/3 +
  1233. C3/3)*exp(x**2*Rational(3, 2)) + Integral(sin(x)*exp(x**2*Rational(-3, 2)), x)*exp(x**2*Rational(3,
  1234. 2)))]
  1235. assert dsolve(eqs4) == sol4
  1236. assert checksysodesol(eqs4, sol4) == (True, [0, 0, 0])
  1237. eqs5 = [Eq(Derivative(f(x), x), x*(f(x) + g(x) + h(x) + k(x) + 1)),
  1238. Eq(Derivative(g(x), x), x*(f(x) + g(x) + h(x) + k(x) + 1)),
  1239. Eq(Derivative(h(x), x), x*(f(x) + g(x) + h(x) + k(x) + 1)),
  1240. Eq(Derivative(k(x), x), x*(f(x) + g(x) + h(x) + k(x) + 1))]
  1241. sol5 = [Eq(f(x), C1*Rational(-1, 4) + C2*Rational(-1, 4) + C3*Rational(-1, 4) + C4*Rational(3, 4) + (C1/4 +
  1242. C2/4 + C3/4 + C4/4)*exp(2*x**2) + Rational(-1, 4)),
  1243. Eq(g(x), C1*Rational(3, 4) + C2*Rational(-1, 4) + C3*Rational(-1, 4) + C4*Rational(-1, 4) + (C1/4 +
  1244. C2/4 + C3/4 + C4/4)*exp(2*x**2) + Rational(-1, 4)),
  1245. Eq(h(x), C1*Rational(-1, 4) + C2*Rational(3, 4) + C3*Rational(-1, 4) + C4*Rational(-1, 4) + (C1/4 +
  1246. C2/4 + C3/4 + C4/4)*exp(2*x**2) + Rational(-1, 4)),
  1247. Eq(k(x), C1*Rational(-1, 4) + C2*Rational(-1, 4) + C3*Rational(3, 4) + C4*Rational(-1, 4) + (C1/4 +
  1248. C2/4 + C3/4 + C4/4)*exp(2*x**2) + Rational(-1, 4))]
  1249. assert dsolve(eqs5) == sol5
  1250. assert checksysodesol(eqs5, sol5) == (True, [0, 0, 0, 0])
  1251. eqs6 = [Eq(Derivative(f(x), x), x**2*(f(x) + g(x) + h(x) + k(x) + 1)),
  1252. Eq(Derivative(g(x), x), x**2*(f(x) + g(x) + h(x) + k(x) + 1)),
  1253. Eq(Derivative(h(x), x), x**2*(f(x) + g(x) + h(x) + k(x) + 1)),
  1254. Eq(Derivative(k(x), x), x**2*(f(x) + g(x) + h(x) + k(x) + 1))]
  1255. sol6 = [Eq(f(x), C1*Rational(-1, 4) + C2*Rational(-1, 4) + C3*Rational(-1, 4) + C4*Rational(3, 4) + (C1/4 +
  1256. C2/4 + C3/4 + C4/4)*exp(x**3*Rational(4, 3)) + Rational(-1, 4)),
  1257. Eq(g(x), C1*Rational(3, 4) + C2*Rational(-1, 4) + C3*Rational(-1, 4) + C4*Rational(-1, 4) + (C1/4 +
  1258. C2/4 + C3/4 + C4/4)*exp(x**3*Rational(4, 3)) + Rational(-1, 4)),
  1259. Eq(h(x), C1*Rational(-1, 4) + C2*Rational(3, 4) + C3*Rational(-1, 4) + C4*Rational(-1, 4) + (C1/4 +
  1260. C2/4 + C3/4 + C4/4)*exp(x**3*Rational(4, 3)) + Rational(-1, 4)),
  1261. Eq(k(x), C1*Rational(-1, 4) + C2*Rational(-1, 4) + C3*Rational(3, 4) + C4*Rational(-1, 4) + (C1/4 +
  1262. C2/4 + C3/4 + C4/4)*exp(x**3*Rational(4, 3)) + Rational(-1, 4))]
  1263. assert dsolve(eqs6) == sol6
  1264. assert checksysodesol(eqs6, sol6) == (True, [0, 0, 0, 0])
  1265. eqs7 = [Eq(Derivative(f(x), x), (f(x) + g(x) + h(x))*log(x) + sin(x)), Eq(Derivative(g(x), x), (f(x) + g(x)
  1266. + h(x))*log(x) + sin(x)), Eq(Derivative(h(x), x), (f(x) + g(x) + h(x))*log(x) + sin(x))]
  1267. sol7 = [Eq(f(x), -C1/3 - C2/3 + 2*C3/3 + (C1/3 + C2/3 +
  1268. C3/3)*exp(x*(3*log(x) - 3)) + exp(x*(3*log(x) -
  1269. 3))*Integral(exp(3*x)*exp(-3*x*log(x))*sin(x), x)),
  1270. Eq(g(x), 2*C1/3 - C2/3 - C3/3 + (C1/3 + C2/3 +
  1271. C3/3)*exp(x*(3*log(x) - 3)) + exp(x*(3*log(x) -
  1272. 3))*Integral(exp(3*x)*exp(-3*x*log(x))*sin(x), x)),
  1273. Eq(h(x), -C1/3 + 2*C2/3 - C3/3 + (C1/3 + C2/3 +
  1274. C3/3)*exp(x*(3*log(x) - 3)) + exp(x*(3*log(x) -
  1275. 3))*Integral(exp(3*x)*exp(-3*x*log(x))*sin(x), x))]
  1276. with dotprodsimp(True):
  1277. assert dsolve(eqs7, simplify=False, doit=False) == sol7
  1278. assert checksysodesol(eqs7, sol7) == (True, [0, 0, 0])
  1279. eqs8 = [Eq(Derivative(f(x), x), (f(x) + g(x) + h(x) + k(x))*log(x) + sin(x)), Eq(Derivative(g(x), x), (f(x)
  1280. + g(x) + h(x) + k(x))*log(x) + sin(x)), Eq(Derivative(h(x), x), (f(x) + g(x) + h(x) + k(x))*log(x) +
  1281. sin(x)), Eq(Derivative(k(x), x), (f(x) + g(x) + h(x) + k(x))*log(x) + sin(x))]
  1282. sol8 = [Eq(f(x), -C1/4 - C2/4 - C3/4 + 3*C4/4 + (C1/4 + C2/4 + C3/4 +
  1283. C4/4)*exp(x*(4*log(x) - 4)) + exp(x*(4*log(x) -
  1284. 4))*Integral(exp(4*x)*exp(-4*x*log(x))*sin(x), x)),
  1285. Eq(g(x), 3*C1/4 - C2/4 - C3/4 - C4/4 + (C1/4 + C2/4 + C3/4 +
  1286. C4/4)*exp(x*(4*log(x) - 4)) + exp(x*(4*log(x) -
  1287. 4))*Integral(exp(4*x)*exp(-4*x*log(x))*sin(x), x)),
  1288. Eq(h(x), -C1/4 + 3*C2/4 - C3/4 - C4/4 + (C1/4 + C2/4 + C3/4 +
  1289. C4/4)*exp(x*(4*log(x) - 4)) + exp(x*(4*log(x) -
  1290. 4))*Integral(exp(4*x)*exp(-4*x*log(x))*sin(x), x)),
  1291. Eq(k(x), -C1/4 - C2/4 + 3*C3/4 - C4/4 + (C1/4 + C2/4 + C3/4 +
  1292. C4/4)*exp(x*(4*log(x) - 4)) + exp(x*(4*log(x) -
  1293. 4))*Integral(exp(4*x)*exp(-4*x*log(x))*sin(x), x))]
  1294. with dotprodsimp(True):
  1295. assert dsolve(eqs8) == sol8
  1296. assert checksysodesol(eqs8, sol8) == (True, [0, 0, 0, 0])
  1297. def test_sysode_linear_neq_order1_type5_type6():
  1298. f, g = symbols("f g", cls=Function)
  1299. x, x_ = symbols("x x_")
  1300. # Type 5
  1301. eqs1 = [Eq(Derivative(f(x), x), (2*f(x) + g(x))/x), Eq(Derivative(g(x), x), (f(x) + 2*g(x))/x)]
  1302. sol1 = [Eq(f(x), -C1*x + C2*x**3), Eq(g(x), C1*x + C2*x**3)]
  1303. assert dsolve(eqs1) == sol1
  1304. assert checksysodesol(eqs1, sol1) == (True, [0, 0])
  1305. # Type 6
  1306. eqs2 = [Eq(Derivative(f(x), x), (2*f(x) + g(x) + 1)/x),
  1307. Eq(Derivative(g(x), x), (x + f(x) + 2*g(x))/x)]
  1308. sol2 = [Eq(f(x), C2*x**3 - x*(C1 + Rational(1, 4)) + x*log(x)*Rational(-1, 2) + Rational(-2, 3)),
  1309. Eq(g(x), C2*x**3 + x*log(x)/2 + x*(C1 + Rational(-1, 4)) + Rational(1, 3))]
  1310. assert dsolve(eqs2) == sol2
  1311. assert checksysodesol(eqs2, sol2) == (True, [0, 0])
  1312. def test_higher_order_to_first_order():
  1313. f, g = symbols('f g', cls=Function)
  1314. x = symbols('x')
  1315. eqs1 = [Eq(Derivative(f(x), (x, 2)), 2*f(x) + g(x)),
  1316. Eq(Derivative(g(x), (x, 2)), -f(x))]
  1317. sol1 = [Eq(f(x), -C2*x*exp(-x) + C3*x*exp(x) - (C1 - C2)*exp(-x) + (C3 + C4)*exp(x)),
  1318. Eq(g(x), C2*x*exp(-x) - C3*x*exp(x) + (C1 + C2)*exp(-x) + (C3 - C4)*exp(x))]
  1319. assert dsolve(eqs1) == sol1
  1320. assert checksysodesol(eqs1, sol1) == (True, [0, 0])
  1321. eqs2 = [Eq(f(x).diff(x, 2), 0), Eq(g(x).diff(x, 2), f(x))]
  1322. sol2 = [Eq(f(x), C1 + C2*x), Eq(g(x), C1*x**2/2 + C2*x**3/6 + C3 + C4*x)]
  1323. assert dsolve(eqs2) == sol2
  1324. assert checksysodesol(eqs2, sol2) == (True, [0, 0])
  1325. eqs3 = [Eq(Derivative(f(x), (x, 2)), 2*f(x)),
  1326. Eq(Derivative(g(x), (x, 2)), -f(x) + 2*g(x))]
  1327. sol3 = [Eq(f(x), 4*C1*exp(-sqrt(2)*x) + 4*C2*exp(sqrt(2)*x)),
  1328. Eq(g(x), sqrt(2)*C1*x*exp(-sqrt(2)*x) - sqrt(2)*C2*x*exp(sqrt(2)*x) + (C1 +
  1329. sqrt(2)*C4)*exp(-sqrt(2)*x) + (C2 - sqrt(2)*C3)*exp(sqrt(2)*x))]
  1330. assert dsolve(eqs3) == sol3
  1331. assert checksysodesol(eqs3, sol3) == (True, [0, 0])
  1332. eqs4 = [Eq(Derivative(f(x), (x, 2)), 2*f(x) + g(x)),
  1333. Eq(Derivative(g(x), (x, 2)), 2*g(x))]
  1334. sol4 = [Eq(f(x), C1*x*exp(sqrt(2)*x)/4 + C3*x*exp(-sqrt(2)*x)/4 + (C2/4 + sqrt(2)*C3/8)*exp(-sqrt(2)*x) -
  1335. exp(sqrt(2)*x)*(sqrt(2)*C1/8 + C4*Rational(-1, 4))),
  1336. Eq(g(x), sqrt(2)*C1*exp(sqrt(2)*x)/2 + sqrt(2)*C3*exp(-sqrt(2)*x)*Rational(-1, 2))]
  1337. assert dsolve(eqs4) == sol4
  1338. assert checksysodesol(eqs4, sol4) == (True, [0, 0])
  1339. eqs5 = [Eq(f(x).diff(x, 2), f(x)), Eq(g(x).diff(x, 2), f(x))]
  1340. sol5 = [Eq(f(x), -C1*exp(-x) + C2*exp(x)), Eq(g(x), -C1*exp(-x) + C2*exp(x) + C3 + C4*x)]
  1341. assert dsolve(eqs5) == sol5
  1342. assert checksysodesol(eqs5, sol5) == (True, [0, 0])
  1343. eqs6 = [Eq(Derivative(f(x), (x, 2)), f(x) + g(x)),
  1344. Eq(Derivative(g(x), (x, 2)), -f(x) - g(x))]
  1345. sol6 = [Eq(f(x), C1 + C2*x**2/2 + C2 + C4*x**3/6 + x*(C3 + C4)),
  1346. Eq(g(x), -C1 + C2*x**2*Rational(-1, 2) - C3*x + C4*x**3*Rational(-1, 6))]
  1347. assert dsolve(eqs6) == sol6
  1348. assert checksysodesol(eqs6, sol6) == (True, [0, 0])
  1349. eqs7 = [Eq(Derivative(f(x), (x, 2)), f(x) + g(x) + 1),
  1350. Eq(Derivative(g(x), (x, 2)), f(x) + g(x) + 1)]
  1351. sol7 = [Eq(f(x), -C1 - C2*x + sqrt(2)*C3*exp(sqrt(2)*x)/2 + sqrt(2)*C4*exp(-sqrt(2)*x)*Rational(-1, 2) +
  1352. Rational(-1, 2)),
  1353. Eq(g(x), C1 + C2*x + sqrt(2)*C3*exp(sqrt(2)*x)/2 + sqrt(2)*C4*exp(-sqrt(2)*x)*Rational(-1, 2) +
  1354. Rational(-1, 2))]
  1355. assert dsolve(eqs7) == sol7
  1356. assert checksysodesol(eqs7, sol7) == (True, [0, 0])
  1357. eqs8 = [Eq(Derivative(f(x), (x, 2)), f(x) + g(x) + 1),
  1358. Eq(Derivative(g(x), (x, 2)), -f(x) - g(x) + 1)]
  1359. sol8 = [Eq(f(x), C1 + C2 + C4*x**3/6 + x**4/12 + x**2*(C2/2 + Rational(1, 2)) + x*(C3 + C4)),
  1360. Eq(g(x), -C1 - C3*x + C4*x**3*Rational(-1, 6) + x**4*Rational(-1, 12) - x**2*(C2/2 + Rational(-1,
  1361. 2)))]
  1362. assert dsolve(eqs8) == sol8
  1363. assert checksysodesol(eqs8, sol8) == (True, [0, 0])
  1364. x, y = symbols('x, y', cls=Function)
  1365. t, l = symbols('t, l')
  1366. eqs10 = [Eq(Derivative(x(t), (t, 2)), 5*x(t) + 43*y(t)),
  1367. Eq(Derivative(y(t), (t, 2)), x(t) + 9*y(t))]
  1368. sol10 = [Eq(x(t), C1*(61 - 9*sqrt(47))*sqrt(sqrt(47) + 7)*exp(-t*sqrt(sqrt(47) + 7))/2 + C2*sqrt(7 -
  1369. sqrt(47))*(61 + 9*sqrt(47))*exp(-t*sqrt(7 - sqrt(47)))/2 + C3*(61 - 9*sqrt(47))*sqrt(sqrt(47) +
  1370. 7)*exp(t*sqrt(sqrt(47) + 7))*Rational(-1, 2) + C4*sqrt(7 - sqrt(47))*(61 + 9*sqrt(47))*exp(t*sqrt(7
  1371. - sqrt(47)))*Rational(-1, 2)),
  1372. Eq(y(t), C1*(7 - sqrt(47))*sqrt(sqrt(47) + 7)*exp(-t*sqrt(sqrt(47) + 7))*Rational(-1, 2) + C2*sqrt(7
  1373. - sqrt(47))*(sqrt(47) + 7)*exp(-t*sqrt(7 - sqrt(47)))*Rational(-1, 2) + C3*(7 -
  1374. sqrt(47))*sqrt(sqrt(47) + 7)*exp(t*sqrt(sqrt(47) + 7))/2 + C4*sqrt(7 - sqrt(47))*(sqrt(47) +
  1375. 7)*exp(t*sqrt(7 - sqrt(47)))/2)]
  1376. assert dsolve(eqs10) == sol10
  1377. assert checksysodesol(eqs10, sol10) == (True, [0, 0])
  1378. eqs11 = [Eq(7*x(t) + Derivative(x(t), (t, 2)) - 9*Derivative(y(t), t), 0),
  1379. Eq(7*y(t) + 9*Derivative(x(t), t) + Derivative(y(t), (t, 2)), 0)]
  1380. sol11 = [Eq(y(t), C1*(9 - sqrt(109))*sin(sqrt(2)*t*sqrt(9*sqrt(109) + 95)/2)/14 + C2*(9 -
  1381. sqrt(109))*cos(sqrt(2)*t*sqrt(9*sqrt(109) + 95)/2)*Rational(-1, 14) + C3*(9 +
  1382. sqrt(109))*sin(sqrt(2)*t*sqrt(95 - 9*sqrt(109))/2)/14 + C4*(9 + sqrt(109))*cos(sqrt(2)*t*sqrt(95 -
  1383. 9*sqrt(109))/2)*Rational(-1, 14)),
  1384. Eq(x(t), C1*(9 - sqrt(109))*cos(sqrt(2)*t*sqrt(9*sqrt(109) + 95)/2)*Rational(-1, 14) + C2*(9 -
  1385. sqrt(109))*sin(sqrt(2)*t*sqrt(9*sqrt(109) + 95)/2)*Rational(-1, 14) + C3*(9 +
  1386. sqrt(109))*cos(sqrt(2)*t*sqrt(95 - 9*sqrt(109))/2)/14 + C4*(9 + sqrt(109))*sin(sqrt(2)*t*sqrt(95 -
  1387. 9*sqrt(109))/2)/14)]
  1388. assert dsolve(eqs11) == sol11
  1389. assert checksysodesol(eqs11, sol11) == (True, [0, 0])
  1390. # Euler Systems
  1391. # Note: To add examples of euler systems solver with non-homogeneous term.
  1392. eqs13 = [Eq(Derivative(f(t), (t, 2)), Derivative(f(t), t)/t + f(t)/t**2 + g(t)/t**2),
  1393. Eq(Derivative(g(t), (t, 2)), g(t)/t**2)]
  1394. sol13 = [Eq(f(t), C1*(sqrt(5) + 3)*Rational(-1, 2)*t**(Rational(1, 2) +
  1395. sqrt(5)*Rational(-1, 2)) + C2*t**(Rational(1, 2) +
  1396. sqrt(5)/2)*(3 - sqrt(5))*Rational(-1, 2) - C3*t**(1 -
  1397. sqrt(2))*(1 + sqrt(2)) - C4*t**(1 + sqrt(2))*(1 - sqrt(2))),
  1398. Eq(g(t), C1*(1 + sqrt(5))*Rational(-1, 2)*t**(Rational(1, 2) +
  1399. sqrt(5)*Rational(-1, 2)) + C2*t**(Rational(1, 2) +
  1400. sqrt(5)/2)*(1 - sqrt(5))*Rational(-1, 2))]
  1401. assert dsolve(eqs13) == sol13
  1402. assert checksysodesol(eqs13, sol13) == (True, [0, 0])
  1403. # Solving systems using dsolve separately
  1404. eqs14 = [Eq(Derivative(f(t), (t, 2)), t*f(t)),
  1405. Eq(Derivative(g(t), (t, 2)), t*g(t))]
  1406. sol14 = [Eq(f(t), C1*airyai(t) + C2*airybi(t)),
  1407. Eq(g(t), C3*airyai(t) + C4*airybi(t))]
  1408. assert dsolve(eqs14) == sol14
  1409. assert checksysodesol(eqs14, sol14) == (True, [0, 0])
  1410. eqs15 = [Eq(Derivative(x(t), (t, 2)), t*(4*Derivative(x(t), t) + 8*Derivative(y(t), t))),
  1411. Eq(Derivative(y(t), (t, 2)), t*(12*Derivative(x(t), t) - 6*Derivative(y(t), t)))]
  1412. sol15 = [Eq(x(t), C1 - erf(sqrt(6)*t)*(sqrt(6)*sqrt(pi)*C2/33 + sqrt(6)*sqrt(pi)*C3*Rational(-1, 44)) +
  1413. erfi(sqrt(5)*t)*(sqrt(5)*sqrt(pi)*C2*Rational(2, 55) + sqrt(5)*sqrt(pi)*C3*Rational(4, 55))),
  1414. Eq(y(t), C4 + erf(sqrt(6)*t)*(sqrt(6)*sqrt(pi)*C2*Rational(2, 33) + sqrt(6)*sqrt(pi)*C3*Rational(-1,
  1415. 22)) + erfi(sqrt(5)*t)*(sqrt(5)*sqrt(pi)*C2*Rational(3, 110) + sqrt(5)*sqrt(pi)*C3*Rational(3, 55)))]
  1416. assert dsolve(eqs15) == sol15
  1417. assert checksysodesol(eqs15, sol15) == (True, [0, 0])
  1418. @slow
  1419. def test_higher_order_to_first_order_9():
  1420. f, g = symbols('f g', cls=Function)
  1421. x = symbols('x')
  1422. eqs9 = [f(x) + g(x) - 2*exp(I*x) + 2*Derivative(f(x), x) + Derivative(f(x), (x, 2)),
  1423. f(x) + g(x) - 2*exp(I*x) + 2*Derivative(g(x), x) + Derivative(g(x), (x, 2))]
  1424. sol9 = [Eq(f(x), -C1 + C4*exp(-2*x)/2 - (C2/2 - C3/2)*exp(-x)*cos(x)
  1425. + (C2/2 + C3/2)*exp(-x)*sin(x) + 2*((1 - 2*I)*exp(I*x)*sin(x)**2/5)
  1426. + 2*((1 - 2*I)*exp(I*x)*cos(x)**2/5)),
  1427. Eq(g(x), C1 - C4*exp(-2*x)/2 - (C2/2 - C3/2)*exp(-x)*cos(x)
  1428. + (C2/2 + C3/2)*exp(-x)*sin(x) + 2*((1 - 2*I)*exp(I*x)*sin(x)**2/5)
  1429. + 2*((1 - 2*I)*exp(I*x)*cos(x)**2/5))]
  1430. assert dsolve(eqs9) == sol9
  1431. assert checksysodesol(eqs9, sol9) == (True, [0, 0])
  1432. def test_higher_order_to_first_order_12():
  1433. f, g = symbols('f g', cls=Function)
  1434. x = symbols('x')
  1435. x, y = symbols('x, y', cls=Function)
  1436. t, l = symbols('t, l')
  1437. eqs12 = [Eq(4*x(t) + Derivative(x(t), (t, 2)) + 8*Derivative(y(t), t), 0),
  1438. Eq(4*y(t) - 8*Derivative(x(t), t) + Derivative(y(t), (t, 2)), 0)]
  1439. sol12 = [Eq(y(t), C1*(2 - sqrt(5))*sin(2*t*sqrt(4*sqrt(5) + 9))*Rational(-1, 2) + C2*(2 -
  1440. sqrt(5))*cos(2*t*sqrt(4*sqrt(5) + 9))/2 + C3*(2 + sqrt(5))*sin(2*t*sqrt(9 - 4*sqrt(5)))*Rational(-1,
  1441. 2) + C4*(2 + sqrt(5))*cos(2*t*sqrt(9 - 4*sqrt(5)))/2),
  1442. Eq(x(t), C1*(2 - sqrt(5))*cos(2*t*sqrt(4*sqrt(5) + 9))*Rational(-1, 2) + C2*(2 -
  1443. sqrt(5))*sin(2*t*sqrt(4*sqrt(5) + 9))*Rational(-1, 2) + C3*(2 + sqrt(5))*cos(2*t*sqrt(9 -
  1444. 4*sqrt(5)))/2 + C4*(2 + sqrt(5))*sin(2*t*sqrt(9 - 4*sqrt(5)))/2)]
  1445. assert dsolve(eqs12) == sol12
  1446. assert checksysodesol(eqs12, sol12) == (True, [0, 0])
  1447. def test_second_order_to_first_order_2():
  1448. f, g = symbols("f g", cls=Function)
  1449. x, t, x_, t_, d, a, m = symbols("x t x_ t_ d a m")
  1450. eqs2 = [Eq(f(x).diff(x, 2), 2*(x*g(x).diff(x) - g(x))),
  1451. Eq(g(x).diff(x, 2),-2*(x*f(x).diff(x) - f(x)))]
  1452. sol2 = [Eq(f(x), C1*x + x*Integral(C2*exp(-x_)*exp(I*exp(2*x_))/2 + C2*exp(-x_)*exp(-I*exp(2*x_))/2 -
  1453. I*C3*exp(-x_)*exp(I*exp(2*x_))/2 + I*C3*exp(-x_)*exp(-I*exp(2*x_))/2, (x_, log(x)))),
  1454. Eq(g(x), C4*x + x*Integral(I*C2*exp(-x_)*exp(I*exp(2*x_))/2 - I*C2*exp(-x_)*exp(-I*exp(2*x_))/2 +
  1455. C3*exp(-x_)*exp(I*exp(2*x_))/2 + C3*exp(-x_)*exp(-I*exp(2*x_))/2, (x_, log(x))))]
  1456. # XXX: dsolve hangs for this in integration
  1457. assert dsolve_system(eqs2, simplify=False, doit=False) == [sol2]
  1458. assert checksysodesol(eqs2, sol2) == (True, [0, 0])
  1459. eqs3 = (Eq(diff(f(t),t,t), 9*t*diff(g(t),t)-9*g(t)), Eq(diff(g(t),t,t),7*t*diff(f(t),t)-7*f(t)))
  1460. sol3 = [Eq(f(t), C1*t + t*Integral(C2*exp(-t_)*exp(3*sqrt(7)*exp(2*t_)/2)/2 + C2*exp(-t_)*
  1461. exp(-3*sqrt(7)*exp(2*t_)/2)/2 + 3*sqrt(7)*C3*exp(-t_)*exp(3*sqrt(7)*exp(2*t_)/2)/14 -
  1462. 3*sqrt(7)*C3*exp(-t_)*exp(-3*sqrt(7)*exp(2*t_)/2)/14, (t_, log(t)))),
  1463. Eq(g(t), C4*t + t*Integral(sqrt(7)*C2*exp(-t_)*exp(3*sqrt(7)*exp(2*t_)/2)/6 - sqrt(7)*C2*exp(-t_)*
  1464. exp(-3*sqrt(7)*exp(2*t_)/2)/6 + C3*exp(-t_)*exp(3*sqrt(7)*exp(2*t_)/2)/2 + C3*exp(-t_)*exp(-3*sqrt(7)*
  1465. exp(2*t_)/2)/2, (t_, log(t))))]
  1466. # XXX: dsolve hangs for this in integration
  1467. assert dsolve_system(eqs3, simplify=False, doit=False) == [sol3]
  1468. assert checksysodesol(eqs3, sol3) == (True, [0, 0])
  1469. # Regression Test case for sympy#19238
  1470. # https://github.com/sympy/sympy/issues/19238
  1471. # Note: When the doit method is removed, these particular types of systems
  1472. # can be divided first so that we have lesser number of big matrices.
  1473. eqs5 = [Eq(Derivative(g(t), (t, 2)), a*m),
  1474. Eq(Derivative(f(t), (t, 2)), 0)]
  1475. sol5 = [Eq(g(t), C1 + C2*t + a*m*t**2/2),
  1476. Eq(f(t), C3 + C4*t)]
  1477. assert dsolve(eqs5) == sol5
  1478. assert checksysodesol(eqs5, sol5) == (True, [0, 0])
  1479. # Type 2
  1480. eqs6 = [Eq(Derivative(f(t), (t, 2)), f(t)/t**4),
  1481. Eq(Derivative(g(t), (t, 2)), d*g(t)/t**4)]
  1482. sol6 = [Eq(f(t), C1*sqrt(t**2)*exp(-1/t) - C2*sqrt(t**2)*exp(1/t)),
  1483. Eq(g(t), C3*sqrt(t**2)*exp(-sqrt(d)/t)*d**Rational(-1, 2) -
  1484. C4*sqrt(t**2)*exp(sqrt(d)/t)*d**Rational(-1, 2))]
  1485. assert dsolve(eqs6) == sol6
  1486. assert checksysodesol(eqs6, sol6) == (True, [0, 0])
  1487. @slow
  1488. def test_second_order_to_first_order_slow1():
  1489. f, g = symbols("f g", cls=Function)
  1490. x, t, x_, t_, d, a, m = symbols("x t x_ t_ d a m")
  1491. # Type 1
  1492. eqs1 = [Eq(f(x).diff(x, 2), 2/x *(x*g(x).diff(x) - g(x))),
  1493. Eq(g(x).diff(x, 2),-2/x *(x*f(x).diff(x) - f(x)))]
  1494. sol1 = [Eq(f(x), C1*x + 2*C2*x*Ci(2*x) - C2*sin(2*x) - 2*C3*x*Si(2*x) - C3*cos(2*x)),
  1495. Eq(g(x), -2*C2*x*Si(2*x) - C2*cos(2*x) - 2*C3*x*Ci(2*x) + C3*sin(2*x) + C4*x)]
  1496. assert dsolve(eqs1) == sol1
  1497. assert checksysodesol(eqs1, sol1) == (True, [0, 0])
  1498. def test_second_order_to_first_order_slow4():
  1499. f, g = symbols("f g", cls=Function)
  1500. x, t, x_, t_, d, a, m = symbols("x t x_ t_ d a m")
  1501. eqs4 = [Eq(Derivative(f(t), (t, 2)), t*sin(t)*Derivative(g(t), t) - g(t)*sin(t)),
  1502. Eq(Derivative(g(t), (t, 2)), t*sin(t)*Derivative(f(t), t) - f(t)*sin(t))]
  1503. sol4 = [Eq(f(t), C1*t + t*Integral(C2*exp(-t_)*exp(exp(t_)*cos(exp(t_)))*exp(-sin(exp(t_)))/2 +
  1504. C2*exp(-t_)*exp(-exp(t_)*cos(exp(t_)))*exp(sin(exp(t_)))/2 - C3*exp(-t_)*exp(exp(t_)*cos(exp(t_)))*
  1505. exp(-sin(exp(t_)))/2 +
  1506. C3*exp(-t_)*exp(-exp(t_)*cos(exp(t_)))*exp(sin(exp(t_)))/2, (t_, log(t)))),
  1507. Eq(g(t), C4*t + t*Integral(-C2*exp(-t_)*exp(exp(t_)*cos(exp(t_)))*exp(-sin(exp(t_)))/2 +
  1508. C2*exp(-t_)*exp(-exp(t_)*cos(exp(t_)))*exp(sin(exp(t_)))/2 + C3*exp(-t_)*exp(exp(t_)*cos(exp(t_)))*
  1509. exp(-sin(exp(t_)))/2 + C3*exp(-t_)*exp(-exp(t_)*cos(exp(t_)))*exp(sin(exp(t_)))/2, (t_, log(t))))]
  1510. # XXX: dsolve hangs for this in integration
  1511. assert dsolve_system(eqs4, simplify=False, doit=False) == [sol4]
  1512. assert checksysodesol(eqs4, sol4) == (True, [0, 0])
  1513. def test_component_division():
  1514. f, g, h, k = symbols('f g h k', cls=Function)
  1515. x = symbols("x")
  1516. funcs = [f(x), g(x), h(x), k(x)]
  1517. eqs1 = [Eq(Derivative(f(x), x), 2*f(x)),
  1518. Eq(Derivative(g(x), x), f(x)),
  1519. Eq(Derivative(h(x), x), h(x)),
  1520. Eq(Derivative(k(x), x), h(x)**4 + k(x))]
  1521. sol1 = [Eq(f(x), 2*C1*exp(2*x)),
  1522. Eq(g(x), C1*exp(2*x) + C2),
  1523. Eq(h(x), C3*exp(x)),
  1524. Eq(k(x), C3**4*exp(4*x)/3 + C4*exp(x))]
  1525. assert dsolve(eqs1) == sol1
  1526. assert checksysodesol(eqs1, sol1) == (True, [0, 0, 0, 0])
  1527. components1 = {((Eq(Derivative(f(x), x), 2*f(x)),), (Eq(Derivative(g(x), x), f(x)),)),
  1528. ((Eq(Derivative(h(x), x), h(x)),), (Eq(Derivative(k(x), x), h(x)**4 + k(x)),))}
  1529. eqsdict1 = ({f(x): set(), g(x): {f(x)}, h(x): set(), k(x): {h(x)}},
  1530. {f(x): Eq(Derivative(f(x), x), 2*f(x)),
  1531. g(x): Eq(Derivative(g(x), x), f(x)),
  1532. h(x): Eq(Derivative(h(x), x), h(x)),
  1533. k(x): Eq(Derivative(k(x), x), h(x)**4 + k(x))})
  1534. graph1 = [{f(x), g(x), h(x), k(x)}, {(g(x), f(x)), (k(x), h(x))}]
  1535. assert {tuple(tuple(scc) for scc in wcc) for wcc in _component_division(eqs1, funcs, x)} == components1
  1536. assert _eqs2dict(eqs1, funcs) == eqsdict1
  1537. assert [set(element) for element in _dict2graph(eqsdict1[0])] == graph1
  1538. eqs2 = [Eq(Derivative(f(x), x), 2*f(x)),
  1539. Eq(Derivative(g(x), x), f(x)),
  1540. Eq(Derivative(h(x), x), h(x)),
  1541. Eq(Derivative(k(x), x), f(x)**4 + k(x))]
  1542. sol2 = [Eq(f(x), C1*exp(2*x)),
  1543. Eq(g(x), C1*exp(2*x)/2 + C2),
  1544. Eq(h(x), C3*exp(x)),
  1545. Eq(k(x), C1**4*exp(8*x)/7 + C4*exp(x))]
  1546. assert dsolve(eqs2) == sol2
  1547. assert checksysodesol(eqs2, sol2) == (True, [0, 0, 0, 0])
  1548. components2 = {frozenset([(Eq(Derivative(f(x), x), 2*f(x)),),
  1549. (Eq(Derivative(g(x), x), f(x)),),
  1550. (Eq(Derivative(k(x), x), f(x)**4 + k(x)),)]),
  1551. frozenset([(Eq(Derivative(h(x), x), h(x)),)])}
  1552. eqsdict2 = ({f(x): set(), g(x): {f(x)}, h(x): set(), k(x): {f(x)}},
  1553. {f(x): Eq(Derivative(f(x), x), 2*f(x)),
  1554. g(x): Eq(Derivative(g(x), x), f(x)),
  1555. h(x): Eq(Derivative(h(x), x), h(x)),
  1556. k(x): Eq(Derivative(k(x), x), f(x)**4 + k(x))})
  1557. graph2 = [{f(x), g(x), h(x), k(x)}, {(g(x), f(x)), (k(x), f(x))}]
  1558. assert {frozenset(tuple(scc) for scc in wcc) for wcc in _component_division(eqs2, funcs, x)} == components2
  1559. assert _eqs2dict(eqs2, funcs) == eqsdict2
  1560. assert [set(element) for element in _dict2graph(eqsdict2[0])] == graph2
  1561. eqs3 = [Eq(Derivative(f(x), x), 2*f(x)),
  1562. Eq(Derivative(g(x), x), x + f(x)),
  1563. Eq(Derivative(h(x), x), h(x)),
  1564. Eq(Derivative(k(x), x), f(x)**4 + k(x))]
  1565. sol3 = [Eq(f(x), C1*exp(2*x)),
  1566. Eq(g(x), C1*exp(2*x)/2 + C2 + x**2/2),
  1567. Eq(h(x), C3*exp(x)),
  1568. Eq(k(x), C1**4*exp(8*x)/7 + C4*exp(x))]
  1569. assert dsolve(eqs3) == sol3
  1570. assert checksysodesol(eqs3, sol3) == (True, [0, 0, 0, 0])
  1571. components3 = {frozenset([(Eq(Derivative(f(x), x), 2*f(x)),),
  1572. (Eq(Derivative(g(x), x), x + f(x)),),
  1573. (Eq(Derivative(k(x), x), f(x)**4 + k(x)),)]),
  1574. frozenset([(Eq(Derivative(h(x), x), h(x)),),])}
  1575. eqsdict3 = ({f(x): set(), g(x): {f(x)}, h(x): set(), k(x): {f(x)}},
  1576. {f(x): Eq(Derivative(f(x), x), 2*f(x)),
  1577. g(x): Eq(Derivative(g(x), x), x + f(x)),
  1578. h(x): Eq(Derivative(h(x), x), h(x)),
  1579. k(x): Eq(Derivative(k(x), x), f(x)**4 + k(x))})
  1580. graph3 = [{f(x), g(x), h(x), k(x)}, {(g(x), f(x)), (k(x), f(x))}]
  1581. assert {frozenset(tuple(scc) for scc in wcc) for wcc in _component_division(eqs3, funcs, x)} == components3
  1582. assert _eqs2dict(eqs3, funcs) == eqsdict3
  1583. assert [set(l) for l in _dict2graph(eqsdict3[0])] == graph3
  1584. # Note: To be uncommented when the default option to call dsolve first for
  1585. # single ODE system can be rearranged. This can be done after the doit
  1586. # option in dsolve is made False by default.
  1587. eqs4 = [Eq(Derivative(f(x), x), x*f(x) + 2*g(x)),
  1588. Eq(Derivative(g(x), x), f(x) + x*g(x) + x),
  1589. Eq(Derivative(h(x), x), h(x)),
  1590. Eq(Derivative(k(x), x), f(x)**4 + k(x))]
  1591. sol4 = [Eq(f(x), (C1/2 - sqrt(2)*C2/2 - sqrt(2)*Integral(x*exp(-x**2/2 - sqrt(2)*x)/2 + x*exp(-x**2/2 +\
  1592. sqrt(2)*x)/2, x)/2 + Integral(sqrt(2)*x*exp(-x**2/2 - sqrt(2)*x)/2 - sqrt(2)*x*exp(-x**2/2 +\
  1593. sqrt(2)*x)/2, x)/2)*exp(x**2/2 - sqrt(2)*x) + (C1/2 + sqrt(2)*C2/2 + sqrt(2)*Integral(x*exp(-x**2/2
  1594. - sqrt(2)*x)/2 + x*exp(-x**2/2 + sqrt(2)*x)/2, x)/2 + Integral(sqrt(2)*x*exp(-x**2/2 - sqrt(2)*x)/2
  1595. - sqrt(2)*x*exp(-x**2/2 + sqrt(2)*x)/2, x)/2)*exp(x**2/2 + sqrt(2)*x)),
  1596. Eq(g(x), (-sqrt(2)*C1/4 + C2/2 + Integral(x*exp(-x**2/2 - sqrt(2)*x)/2 + x*exp(-x**2/2 + sqrt(2)*x)/2, x)/2 -\
  1597. sqrt(2)*Integral(sqrt(2)*x*exp(-x**2/2 - sqrt(2)*x)/2 - sqrt(2)*x*exp(-x**2/2 + sqrt(2)*x)/2,
  1598. x)/4)*exp(x**2/2 - sqrt(2)*x) + (sqrt(2)*C1/4 + C2/2 + Integral(x*exp(-x**2/2 - sqrt(2)*x)/2 +
  1599. x*exp(-x**2/2 + sqrt(2)*x)/2, x)/2 + sqrt(2)*Integral(sqrt(2)*x*exp(-x**2/2 - sqrt(2)*x)/2 -
  1600. sqrt(2)*x*exp(-x**2/2 + sqrt(2)*x)/2, x)/4)*exp(x**2/2 + sqrt(2)*x)),
  1601. Eq(h(x), C3*exp(x)),
  1602. Eq(k(x), C4*exp(x) + exp(x)*Integral((C1*exp(x**2/2 - sqrt(2)*x)/2 + C1*exp(x**2/2 + sqrt(2)*x)/2 -
  1603. sqrt(2)*C2*exp(x**2/2 - sqrt(2)*x)/2 + sqrt(2)*C2*exp(x**2/2 + sqrt(2)*x)/2 - sqrt(2)*exp(x**2/2 -
  1604. sqrt(2)*x)*Integral(x*exp(-x**2/2 - sqrt(2)*x)/2 + x*exp(-x**2/2 + sqrt(2)*x)/2, x)/2 + exp(x**2/2 -
  1605. sqrt(2)*x)*Integral(sqrt(2)*x*exp(-x**2/2 - sqrt(2)*x)/2 - sqrt(2)*x*exp(-x**2/2 + sqrt(2)*x)/2,
  1606. x)/2 + sqrt(2)*exp(x**2/2 + sqrt(2)*x)*Integral(x*exp(-x**2/2 - sqrt(2)*x)/2 + x*exp(-x**2/2 +
  1607. sqrt(2)*x)/2, x)/2 + exp(x**2/2 + sqrt(2)*x)*Integral(sqrt(2)*x*exp(-x**2/2 - sqrt(2)*x)/2 -
  1608. sqrt(2)*x*exp(-x**2/2 + sqrt(2)*x)/2, x)/2)**4*exp(-x), x))]
  1609. components4 = {(frozenset([Eq(Derivative(f(x), x), x*f(x) + 2*g(x)),
  1610. Eq(Derivative(g(x), x), x*g(x) + x + f(x))]),
  1611. frozenset([Eq(Derivative(k(x), x), f(x)**4 + k(x)),])),
  1612. (frozenset([Eq(Derivative(h(x), x), h(x)),]),)}
  1613. eqsdict4 = ({f(x): {g(x)}, g(x): {f(x)}, h(x): set(), k(x): {f(x)}},
  1614. {f(x): Eq(Derivative(f(x), x), x*f(x) + 2*g(x)),
  1615. g(x): Eq(Derivative(g(x), x), x*g(x) + x + f(x)),
  1616. h(x): Eq(Derivative(h(x), x), h(x)),
  1617. k(x): Eq(Derivative(k(x), x), f(x)**4 + k(x))})
  1618. graph4 = [{f(x), g(x), h(x), k(x)}, {(f(x), g(x)), (g(x), f(x)), (k(x), f(x))}]
  1619. assert {tuple(frozenset(scc) for scc in wcc) for wcc in _component_division(eqs4, funcs, x)} == components4
  1620. assert _eqs2dict(eqs4, funcs) == eqsdict4
  1621. assert [set(element) for element in _dict2graph(eqsdict4[0])] == graph4
  1622. # XXX: dsolve hangs in integration here:
  1623. assert dsolve_system(eqs4, simplify=False, doit=False) == [sol4]
  1624. assert checksysodesol(eqs4, sol4) == (True, [0, 0, 0, 0])
  1625. eqs5 = [Eq(Derivative(f(x), x), x*f(x) + 2*g(x)),
  1626. Eq(Derivative(g(x), x), x*g(x) + f(x)),
  1627. Eq(Derivative(h(x), x), h(x)),
  1628. Eq(Derivative(k(x), x), f(x)**4 + k(x))]
  1629. sol5 = [Eq(f(x), (C1/2 - sqrt(2)*C2/2)*exp(x**2/2 - sqrt(2)*x) + (C1/2 + sqrt(2)*C2/2)*exp(x**2/2 + sqrt(2)*x)),
  1630. Eq(g(x), (-sqrt(2)*C1/4 + C2/2)*exp(x**2/2 - sqrt(2)*x) + (sqrt(2)*C1/4 + C2/2)*exp(x**2/2 + sqrt(2)*x)),
  1631. Eq(h(x), C3*exp(x)),
  1632. Eq(k(x), C4*exp(x) + exp(x)*Integral((C1*exp(x**2/2 - sqrt(2)*x)/2 + C1*exp(x**2/2 + sqrt(2)*x)/2 -
  1633. sqrt(2)*C2*exp(x**2/2 - sqrt(2)*x)/2 + sqrt(2)*C2*exp(x**2/2 + sqrt(2)*x)/2)**4*exp(-x), x))]
  1634. components5 = {(frozenset([Eq(Derivative(f(x), x), x*f(x) + 2*g(x)),
  1635. Eq(Derivative(g(x), x), x*g(x) + f(x))]),
  1636. frozenset([Eq(Derivative(k(x), x), f(x)**4 + k(x)),])),
  1637. (frozenset([Eq(Derivative(h(x), x), h(x)),]),)}
  1638. eqsdict5 = ({f(x): {g(x)}, g(x): {f(x)}, h(x): set(), k(x): {f(x)}},
  1639. {f(x): Eq(Derivative(f(x), x), x*f(x) + 2*g(x)),
  1640. g(x): Eq(Derivative(g(x), x), x*g(x) + f(x)),
  1641. h(x): Eq(Derivative(h(x), x), h(x)),
  1642. k(x): Eq(Derivative(k(x), x), f(x)**4 + k(x))})
  1643. graph5 = [{f(x), g(x), h(x), k(x)}, {(f(x), g(x)), (g(x), f(x)), (k(x), f(x))}]
  1644. assert {tuple(frozenset(scc) for scc in wcc) for wcc in _component_division(eqs5, funcs, x)} == components5
  1645. assert _eqs2dict(eqs5, funcs) == eqsdict5
  1646. assert [set(element) for element in _dict2graph(eqsdict5[0])] == graph5
  1647. # XXX: dsolve hangs in integration here:
  1648. assert dsolve_system(eqs5, simplify=False, doit=False) == [sol5]
  1649. assert checksysodesol(eqs5, sol5) == (True, [0, 0, 0, 0])
  1650. def test_linodesolve():
  1651. t, x, a = symbols("t x a")
  1652. f, g, h = symbols("f g h", cls=Function)
  1653. # Testing the Errors
  1654. raises(ValueError, lambda: linodesolve(1, t))
  1655. raises(ValueError, lambda: linodesolve(a, t))
  1656. A1 = Matrix([[1, 2], [2, 4], [4, 6]])
  1657. raises(NonSquareMatrixError, lambda: linodesolve(A1, t))
  1658. A2 = Matrix([[1, 2, 1], [3, 1, 2]])
  1659. raises(NonSquareMatrixError, lambda: linodesolve(A2, t))
  1660. # Testing auto functionality
  1661. func = [f(t), g(t)]
  1662. eq = [Eq(f(t).diff(t) + g(t).diff(t), g(t)), Eq(g(t).diff(t), f(t))]
  1663. ceq = canonical_odes(eq, func, t)
  1664. (A1, A0), b = linear_ode_to_matrix(ceq[0], func, t, 1)
  1665. A = A0
  1666. sol = [C1*(-Rational(1, 2) + sqrt(5)/2)*exp(t*(-Rational(1, 2) + sqrt(5)/2)) + C2*(-sqrt(5)/2 - Rational(1, 2))*
  1667. exp(t*(-sqrt(5)/2 - Rational(1, 2))),
  1668. C1*exp(t*(-Rational(1, 2) + sqrt(5)/2)) + C2*exp(t*(-sqrt(5)/2 - Rational(1, 2)))]
  1669. assert constant_renumber(linodesolve(A, t), variables=Tuple(*eq).free_symbols) == sol
  1670. # Testing the Errors
  1671. raises(ValueError, lambda: linodesolve(1, t, b=Matrix([t+1])))
  1672. raises(ValueError, lambda: linodesolve(a, t, b=Matrix([log(t) + sin(t)])))
  1673. raises(ValueError, lambda: linodesolve(Matrix([7]), t, b=t**2))
  1674. raises(ValueError, lambda: linodesolve(Matrix([a+10]), t, b=log(t)*cos(t)))
  1675. raises(ValueError, lambda: linodesolve(7, t, b=t**2))
  1676. raises(ValueError, lambda: linodesolve(a, t, b=log(t) + sin(t)))
  1677. A1 = Matrix([[1, 2], [2, 4], [4, 6]])
  1678. b1 = Matrix([t, 1, t**2])
  1679. raises(NonSquareMatrixError, lambda: linodesolve(A1, t, b=b1))
  1680. A2 = Matrix([[1, 2, 1], [3, 1, 2]])
  1681. b2 = Matrix([t, t**2])
  1682. raises(NonSquareMatrixError, lambda: linodesolve(A2, t, b=b2))
  1683. raises(ValueError, lambda: linodesolve(A1[:2, :], t, b=b1))
  1684. raises(ValueError, lambda: linodesolve(A1[:2, :], t, b=b1[:1]))
  1685. # DOIT check
  1686. A1 = Matrix([[1, -1], [1, -1]])
  1687. b1 = Matrix([15*t - 10, -15*t - 5])
  1688. sol1 = [C1 + C2*t + C2 - 10*t**3 + 10*t**2 + t*(15*t**2 - 5*t) - 10*t,
  1689. C1 + C2*t - 10*t**3 - 5*t**2 + t*(15*t**2 - 5*t) - 5*t]
  1690. assert constant_renumber(linodesolve(A1, t, b=b1, type="type2", doit=True),
  1691. variables=[t]) == sol1
  1692. # Testing auto functionality
  1693. func = [f(t), g(t)]
  1694. eq = [Eq(f(t).diff(t) + g(t).diff(t), g(t) + t), Eq(g(t).diff(t), f(t))]
  1695. ceq = canonical_odes(eq, func, t)
  1696. (A1, A0), b = linear_ode_to_matrix(ceq[0], func, t, 1)
  1697. A = A0
  1698. sol = [-C1*exp(-t/2 + sqrt(5)*t/2)/2 + sqrt(5)*C1*exp(-t/2 + sqrt(5)*t/2)/2 - sqrt(5)*C2*exp(-sqrt(5)*t/2 -
  1699. t/2)/2 - C2*exp(-sqrt(5)*t/2 - t/2)/2 - exp(-t/2 + sqrt(5)*t/2)*Integral(t*exp(-sqrt(5)*t/2 +
  1700. t/2)/(-5 + sqrt(5)) - sqrt(5)*t*exp(-sqrt(5)*t/2 + t/2)/(-5 + sqrt(5)), t)/2 + sqrt(5)*exp(-t/2 +
  1701. sqrt(5)*t/2)*Integral(t*exp(-sqrt(5)*t/2 + t/2)/(-5 + sqrt(5)) - sqrt(5)*t*exp(-sqrt(5)*t/2 +
  1702. t/2)/(-5 + sqrt(5)), t)/2 - sqrt(5)*exp(-sqrt(5)*t/2 - t/2)*Integral(-sqrt(5)*t*exp(t/2 +
  1703. sqrt(5)*t/2)/5, t)/2 - exp(-sqrt(5)*t/2 - t/2)*Integral(-sqrt(5)*t*exp(t/2 + sqrt(5)*t/2)/5, t)/2,
  1704. C1*exp(-t/2 + sqrt(5)*t/2) + C2*exp(-sqrt(5)*t/2 - t/2) + exp(-t/2 +
  1705. sqrt(5)*t/2)*Integral(t*exp(-sqrt(5)*t/2 + t/2)/(-5 + sqrt(5)) - sqrt(5)*t*exp(-sqrt(5)*t/2 +
  1706. t/2)/(-5 + sqrt(5)), t) + exp(-sqrt(5)*t/2 -
  1707. t/2)*Integral(-sqrt(5)*t*exp(t/2 + sqrt(5)*t/2)/5, t)]
  1708. assert constant_renumber(linodesolve(A, t, b=b), variables=[t]) == sol
  1709. # non-homogeneous term assumed to be 0
  1710. sol1 = [-C1*exp(-t/2 + sqrt(5)*t/2)/2 + sqrt(5)*C1*exp(-t/2 + sqrt(5)*t/2)/2 - sqrt(5)*C2*exp(-sqrt(5)*t/2
  1711. - t/2)/2 - C2*exp(-sqrt(5)*t/2 - t/2)/2,
  1712. C1*exp(-t/2 + sqrt(5)*t/2) + C2*exp(-sqrt(5)*t/2 - t/2)]
  1713. assert constant_renumber(linodesolve(A, t, type="type2"), variables=[t]) == sol1
  1714. # Testing the Errors
  1715. raises(ValueError, lambda: linodesolve(t+10, t))
  1716. raises(ValueError, lambda: linodesolve(a*t, t))
  1717. A1 = Matrix([[1, t], [-t, 1]])
  1718. B1, _ = _is_commutative_anti_derivative(A1, t)
  1719. raises(NonSquareMatrixError, lambda: linodesolve(A1[:, :1], t, B=B1))
  1720. raises(ValueError, lambda: linodesolve(A1, t, B=1))
  1721. A2 = Matrix([[t, t, t], [t, t, t], [t, t, t]])
  1722. B2, _ = _is_commutative_anti_derivative(A2, t)
  1723. raises(NonSquareMatrixError, lambda: linodesolve(A2, t, B=B2[:2, :]))
  1724. raises(ValueError, lambda: linodesolve(A2, t, B=2))
  1725. raises(ValueError, lambda: linodesolve(A2, t, B=B2, type="type31"))
  1726. raises(ValueError, lambda: linodesolve(A1, t, B=B2))
  1727. raises(ValueError, lambda: linodesolve(A2, t, B=B1))
  1728. # Testing auto functionality
  1729. func = [f(t), g(t)]
  1730. eq = [Eq(f(t).diff(t), f(t) + t*g(t)), Eq(g(t).diff(t), -t*f(t) + g(t))]
  1731. ceq = canonical_odes(eq, func, t)
  1732. (A1, A0), b = linear_ode_to_matrix(ceq[0], func, t, 1)
  1733. A = A0
  1734. sol = [(C1/2 - I*C2/2)*exp(I*t**2/2 + t) + (C1/2 + I*C2/2)*exp(-I*t**2/2 + t),
  1735. (-I*C1/2 + C2/2)*exp(-I*t**2/2 + t) + (I*C1/2 + C2/2)*exp(I*t**2/2 + t)]
  1736. assert constant_renumber(linodesolve(A, t), variables=Tuple(*eq).free_symbols) == sol
  1737. assert constant_renumber(linodesolve(A, t, type="type3"), variables=Tuple(*eq).free_symbols) == sol
  1738. A1 = Matrix([[t, 1], [t, -1]])
  1739. raises(NotImplementedError, lambda: linodesolve(A1, t))
  1740. # Testing the Errors
  1741. raises(ValueError, lambda: linodesolve(t+10, t, b=Matrix([t+1])))
  1742. raises(ValueError, lambda: linodesolve(a*t, t, b=Matrix([log(t) + sin(t)])))
  1743. raises(ValueError, lambda: linodesolve(Matrix([7*t]), t, b=t**2))
  1744. raises(ValueError, lambda: linodesolve(Matrix([a + 10*log(t)]), t, b=log(t)*cos(t)))
  1745. raises(ValueError, lambda: linodesolve(7*t, t, b=t**2))
  1746. raises(ValueError, lambda: linodesolve(a*t**2, t, b=log(t) + sin(t)))
  1747. A1 = Matrix([[1, t], [-t, 1]])
  1748. b1 = Matrix([t, t ** 2])
  1749. B1, _ = _is_commutative_anti_derivative(A1, t)
  1750. raises(NonSquareMatrixError, lambda: linodesolve(A1[:, :1], t, b=b1))
  1751. A2 = Matrix([[t, t, t], [t, t, t], [t, t, t]])
  1752. b2 = Matrix([t, 1, t**2])
  1753. B2, _ = _is_commutative_anti_derivative(A2, t)
  1754. raises(NonSquareMatrixError, lambda: linodesolve(A2[:2, :], t, b=b2))
  1755. raises(ValueError, lambda: linodesolve(A1, t, b=b2))
  1756. raises(ValueError, lambda: linodesolve(A2, t, b=b1))
  1757. raises(ValueError, lambda: linodesolve(A1, t, b=b1, B=B2))
  1758. raises(ValueError, lambda: linodesolve(A2, t, b=b2, B=B1))
  1759. # Testing auto functionality
  1760. func = [f(x), g(x), h(x)]
  1761. eq = [Eq(f(x).diff(x), x*(f(x) + g(x) + h(x)) + x),
  1762. Eq(g(x).diff(x), x*(f(x) + g(x) + h(x)) + x),
  1763. Eq(h(x).diff(x), x*(f(x) + g(x) + h(x)) + 1)]
  1764. ceq = canonical_odes(eq, func, x)
  1765. (A1, A0), b = linear_ode_to_matrix(ceq[0], func, x, 1)
  1766. A = A0
  1767. _x1 = exp(-3*x**2/2)
  1768. _x2 = exp(3*x**2/2)
  1769. _x3 = Integral(2*_x1*x/3 + _x1/3 + x/3 - Rational(1, 3), x)
  1770. _x4 = 2*_x2*_x3/3
  1771. _x5 = Integral(2*_x1*x/3 + _x1/3 - 2*x/3 + Rational(2, 3), x)
  1772. sol = [
  1773. C1*_x2/3 - C1/3 + C2*_x2/3 - C2/3 + C3*_x2/3 + 2*C3/3 + _x2*_x5/3 + _x3/3 + _x4 - _x5/3,
  1774. C1*_x2/3 + 2*C1/3 + C2*_x2/3 - C2/3 + C3*_x2/3 - C3/3 + _x2*_x5/3 + _x3/3 + _x4 - _x5/3,
  1775. C1*_x2/3 - C1/3 + C2*_x2/3 + 2*C2/3 + C3*_x2/3 - C3/3 + _x2*_x5/3 - 2*_x3/3 + _x4 + 2*_x5/3,
  1776. ]
  1777. assert constant_renumber(linodesolve(A, x, b=b), variables=Tuple(*eq).free_symbols) == sol
  1778. assert constant_renumber(linodesolve(A, x, b=b, type="type4"),
  1779. variables=Tuple(*eq).free_symbols) == sol
  1780. A1 = Matrix([[t, 1], [t, -1]])
  1781. raises(NotImplementedError, lambda: linodesolve(A1, t, b=b1))
  1782. # non-homogeneous term not passed
  1783. sol1 = [-C1/3 - C2/3 + 2*C3/3 + (C1/3 + C2/3 + C3/3)*exp(3*x**2/2), 2*C1/3 - C2/3 - C3/3 + (C1/3 + C2/3 + C3/3)*exp(3*x**2/2),
  1784. -C1/3 + 2*C2/3 - C3/3 + (C1/3 + C2/3 + C3/3)*exp(3*x**2/2)]
  1785. assert constant_renumber(linodesolve(A, x, type="type4", doit=True), variables=Tuple(*eq).free_symbols) == sol1
  1786. @slow
  1787. def test_linear_3eq_order1_type4_slow():
  1788. x, y, z = symbols('x, y, z', cls=Function)
  1789. t = Symbol('t')
  1790. f = t ** 3 + log(t)
  1791. g = t ** 2 + sin(t)
  1792. eq1 = (Eq(diff(x(t), t), (4 * f + g) * x(t) - f * y(t) - 2 * f * z(t)),
  1793. Eq(diff(y(t), t), 2 * f * x(t) + (f + g) * y(t) - 2 * f * z(t)), Eq(diff(z(t), t), 5 * f * x(t) + f * y(
  1794. t) + (-3 * f + g) * z(t)))
  1795. with dotprodsimp(True):
  1796. dsolve(eq1)
  1797. @slow
  1798. def test_linear_neq_order1_type2_slow1():
  1799. i, r1, c1, r2, c2, t = symbols('i, r1, c1, r2, c2, t')
  1800. x1 = Function('x1')
  1801. x2 = Function('x2')
  1802. eq1 = r1*c1*Derivative(x1(t), t) + x1(t) - x2(t) - r1*i
  1803. eq2 = r2*c1*Derivative(x1(t), t) + r2*c2*Derivative(x2(t), t) + x2(t) - r2*i
  1804. eq = [eq1, eq2]
  1805. # XXX: Solution is too complicated
  1806. [sol] = dsolve_system(eq, simplify=False, doit=False)
  1807. assert checksysodesol(eq, sol) == (True, [0, 0])
  1808. # Regression test case for issue #9204
  1809. # https://github.com/sympy/sympy/issues/9204
  1810. @slow
  1811. def test_linear_new_order1_type2_de_lorentz_slow_check():
  1812. if ON_CI:
  1813. skip("Too slow for CI.")
  1814. m = Symbol("m", real=True)
  1815. q = Symbol("q", real=True)
  1816. t = Symbol("t", real=True)
  1817. e1, e2, e3 = symbols("e1:4", real=True)
  1818. b1, b2, b3 = symbols("b1:4", real=True)
  1819. v1, v2, v3 = symbols("v1:4", cls=Function, real=True)
  1820. eqs = [
  1821. -e1*q + m*Derivative(v1(t), t) - q*(-b2*v3(t) + b3*v2(t)),
  1822. -e2*q + m*Derivative(v2(t), t) - q*(b1*v3(t) - b3*v1(t)),
  1823. -e3*q + m*Derivative(v3(t), t) - q*(-b1*v2(t) + b2*v1(t))
  1824. ]
  1825. sol = dsolve(eqs)
  1826. assert checksysodesol(eqs, sol) == (True, [0, 0, 0])
  1827. # Regression test case for issue #14001
  1828. # https://github.com/sympy/sympy/issues/14001
  1829. @slow
  1830. def test_linear_neq_order1_type2_slow_check():
  1831. RC, t, C, Vs, L, R1, V0, I0 = symbols("RC t C Vs L R1 V0 I0")
  1832. V = Function("V")
  1833. I = Function("I")
  1834. system = [Eq(V(t).diff(t), -1/RC*V(t) + I(t)/C), Eq(I(t).diff(t), -R1/L*I(t) - 1/L*V(t) + Vs/L)]
  1835. [sol] = dsolve_system(system, simplify=False, doit=False)
  1836. assert checksysodesol(system, sol) == (True, [0, 0])
  1837. def _linear_3eq_order1_type4_long():
  1838. x, y, z = symbols('x, y, z', cls=Function)
  1839. t = Symbol('t')
  1840. f = t ** 3 + log(t)
  1841. g = t ** 2 + sin(t)
  1842. eq1 = (Eq(diff(x(t), t), (4*f + g)*x(t) - f*y(t) - 2*f*z(t)),
  1843. Eq(diff(y(t), t), 2*f*x(t) + (f + g)*y(t) - 2*f*z(t)), Eq(diff(z(t), t), 5*f*x(t) + f*y(
  1844. t) + (-3*f + g)*z(t)))
  1845. dsolve_sol = dsolve(eq1)
  1846. dsolve_sol1 = [_simpsol(sol) for sol in dsolve_sol]
  1847. x_1 = sqrt(-t**6 - 8*t**3*log(t) + 8*t**3 - 16*log(t)**2 + 32*log(t) - 16)
  1848. x_2 = sqrt(3)
  1849. x_3 = 8324372644*C1*x_1*x_2 + 4162186322*C2*x_1*x_2 - 8324372644*C3*x_1*x_2
  1850. x_4 = 1 / (1903457163*t**3 + 3825881643*x_1*x_2 + 7613828652*log(t) - 7613828652)
  1851. x_5 = exp(t**3/3 + t*x_1*x_2/4 - cos(t))
  1852. x_6 = exp(t**3/3 - t*x_1*x_2/4 - cos(t))
  1853. x_7 = exp(t**4/2 + t**3/3 + 2*t*log(t) - 2*t - cos(t))
  1854. x_8 = 91238*C1*x_1*x_2 + 91238*C2*x_1*x_2 - 91238*C3*x_1*x_2
  1855. x_9 = 1 / (66049*t**3 - 50629*x_1*x_2 + 264196*log(t) - 264196)
  1856. x_10 = 50629 * C1 / 25189 + 37909*C2/25189 - 50629*C3/25189 - x_3*x_4
  1857. x_11 = -50629*C1/25189 - 12720*C2/25189 + 50629*C3/25189 + x_3*x_4
  1858. sol = [Eq(x(t), x_10*x_5 + x_11*x_6 + x_7*(C1 - C2)), Eq(y(t), x_10*x_5 + x_11*x_6), Eq(z(t), x_5*(
  1859. -424*C1/257 - 167*C2/257 + 424*C3/257 - x_8*x_9) + x_6*(167*C1/257 + 424*C2/257 -
  1860. 167*C3/257 + x_8*x_9) + x_7*(C1 - C2))]
  1861. assert dsolve_sol1 == sol
  1862. assert checksysodesol(eq1, dsolve_sol1) == (True, [0, 0, 0])
  1863. @slow
  1864. def test_neq_order1_type4_slow_check1():
  1865. f, g = symbols("f g", cls=Function)
  1866. x = symbols("x")
  1867. eqs = [Eq(diff(f(x), x), x*f(x) + x**2*g(x) + x),
  1868. Eq(diff(g(x), x), 2*x**2*f(x) + (x + 3*x**2)*g(x) + 1)]
  1869. sol = dsolve(eqs)
  1870. assert checksysodesol(eqs, sol) == (True, [0, 0])
  1871. @slow
  1872. def test_neq_order1_type4_slow_check2():
  1873. f, g, h = symbols("f, g, h", cls=Function)
  1874. x = Symbol("x")
  1875. eqs = [
  1876. Eq(Derivative(f(x), x), x*h(x) + f(x) + g(x) + 1),
  1877. Eq(Derivative(g(x), x), x*g(x) + f(x) + h(x) + 10),
  1878. Eq(Derivative(h(x), x), x*f(x) + x + g(x) + h(x))
  1879. ]
  1880. with dotprodsimp(True):
  1881. sol = dsolve(eqs)
  1882. assert checksysodesol(eqs, sol) == (True, [0, 0, 0])
  1883. def _neq_order1_type4_slow3():
  1884. f, g = symbols("f g", cls=Function)
  1885. x = symbols("x")
  1886. eqs = [
  1887. Eq(Derivative(f(x), x), x*f(x) + g(x) + sin(x)),
  1888. Eq(Derivative(g(x), x), x**2 + x*g(x) - f(x))
  1889. ]
  1890. sol = [
  1891. Eq(f(x), (C1/2 - I*C2/2 - I*Integral(x**2*exp(-x**2/2 - I*x)/2 +
  1892. x**2*exp(-x**2/2 + I*x)/2 + I*exp(-x**2/2 - I*x)*sin(x)/2 -
  1893. I*exp(-x**2/2 + I*x)*sin(x)/2, x)/2 + Integral(-I*x**2*exp(-x**2/2
  1894. - I*x)/2 + I*x**2*exp(-x**2/2 + I*x)/2 + exp(-x**2/2 -
  1895. I*x)*sin(x)/2 + exp(-x**2/2 + I*x)*sin(x)/2, x)/2)*exp(x**2/2 +
  1896. I*x) + (C1/2 + I*C2/2 + I*Integral(x**2*exp(-x**2/2 - I*x)/2 +
  1897. x**2*exp(-x**2/2 + I*x)/2 + I*exp(-x**2/2 - I*x)*sin(x)/2 -
  1898. I*exp(-x**2/2 + I*x)*sin(x)/2, x)/2 + Integral(-I*x**2*exp(-x**2/2
  1899. - I*x)/2 + I*x**2*exp(-x**2/2 + I*x)/2 + exp(-x**2/2 -
  1900. I*x)*sin(x)/2 + exp(-x**2/2 + I*x)*sin(x)/2, x)/2)*exp(x**2/2 -
  1901. I*x)),
  1902. Eq(g(x), (-I*C1/2 + C2/2 + Integral(x**2*exp(-x**2/2 - I*x)/2 +
  1903. x**2*exp(-x**2/2 + I*x)/2 + I*exp(-x**2/2 - I*x)*sin(x)/2 -
  1904. I*exp(-x**2/2 + I*x)*sin(x)/2, x)/2 -
  1905. I*Integral(-I*x**2*exp(-x**2/2 - I*x)/2 + I*x**2*exp(-x**2/2 +
  1906. I*x)/2 + exp(-x**2/2 - I*x)*sin(x)/2 + exp(-x**2/2 +
  1907. I*x)*sin(x)/2, x)/2)*exp(x**2/2 - I*x) + (I*C1/2 + C2/2 +
  1908. Integral(x**2*exp(-x**2/2 - I*x)/2 + x**2*exp(-x**2/2 + I*x)/2 +
  1909. I*exp(-x**2/2 - I*x)*sin(x)/2 - I*exp(-x**2/2 + I*x)*sin(x)/2,
  1910. x)/2 + I*Integral(-I*x**2*exp(-x**2/2 - I*x)/2 +
  1911. I*x**2*exp(-x**2/2 + I*x)/2 + exp(-x**2/2 - I*x)*sin(x)/2 +
  1912. exp(-x**2/2 + I*x)*sin(x)/2, x)/2)*exp(x**2/2 + I*x))
  1913. ]
  1914. return eqs, sol
  1915. def test_neq_order1_type4_slow3():
  1916. eqs, sol = _neq_order1_type4_slow3()
  1917. assert dsolve_system(eqs, simplify=False, doit=False) == [sol]
  1918. # XXX: dsolve gives an error in integration:
  1919. # assert dsolve(eqs) == sol
  1920. # https://github.com/sympy/sympy/issues/20155
  1921. @slow
  1922. def test_neq_order1_type4_slow_check3():
  1923. eqs, sol = _neq_order1_type4_slow3()
  1924. assert checksysodesol(eqs, sol) == (True, [0, 0])
  1925. @XFAIL
  1926. @slow
  1927. def test_linear_3eq_order1_type4_long_dsolve_slow_xfail():
  1928. if ON_CI:
  1929. skip("Too slow for CI.")
  1930. eq, sol = _linear_3eq_order1_type4_long()
  1931. dsolve_sol = dsolve(eq)
  1932. dsolve_sol1 = [_simpsol(sol) for sol in dsolve_sol]
  1933. assert dsolve_sol1 == sol
  1934. @slow
  1935. def test_linear_3eq_order1_type4_long_dsolve_dotprodsimp():
  1936. if ON_CI:
  1937. skip("Too slow for CI.")
  1938. eq, sol = _linear_3eq_order1_type4_long()
  1939. # XXX: Only works with dotprodsimp see
  1940. # test_linear_3eq_order1_type4_long_dsolve_slow_xfail which is too slow
  1941. with dotprodsimp(True):
  1942. dsolve_sol = dsolve(eq)
  1943. dsolve_sol1 = [_simpsol(sol) for sol in dsolve_sol]
  1944. assert dsolve_sol1 == sol
  1945. @slow
  1946. def test_linear_3eq_order1_type4_long_check():
  1947. if ON_CI:
  1948. skip("Too slow for CI.")
  1949. eq, sol = _linear_3eq_order1_type4_long()
  1950. assert checksysodesol(eq, sol) == (True, [0, 0, 0])
  1951. def test_dsolve_system():
  1952. f, g = symbols("f g", cls=Function)
  1953. x = symbols("x")
  1954. eqs = [Eq(f(x).diff(x), f(x) + g(x)), Eq(g(x).diff(x), f(x) + g(x))]
  1955. funcs = [f(x), g(x)]
  1956. sol = [[Eq(f(x), -C1 + C2*exp(2*x)), Eq(g(x), C1 + C2*exp(2*x))]]
  1957. assert dsolve_system(eqs, funcs=funcs, t=x, doit=True) == sol
  1958. raises(ValueError, lambda: dsolve_system(1))
  1959. raises(ValueError, lambda: dsolve_system(eqs, 1))
  1960. raises(ValueError, lambda: dsolve_system(eqs, funcs, 1))
  1961. raises(ValueError, lambda: dsolve_system(eqs, funcs[:1], x))
  1962. eq = (Eq(f(x).diff(x), 12 * f(x) - 6 * g(x)), Eq(g(x).diff(x) ** 2, 11 * f(x) + 3 * g(x)))
  1963. raises(NotImplementedError, lambda: dsolve_system(eq) == ([], []))
  1964. raises(NotImplementedError, lambda: dsolve_system(eq, funcs=[f(x), g(x)]) == ([], []))
  1965. raises(NotImplementedError, lambda: dsolve_system(eq, funcs=[f(x), g(x)], t=x) == ([], []))
  1966. raises(NotImplementedError, lambda: dsolve_system(eq, funcs=[f(x), g(x)], t=x, ics={f(0): 1, g(0): 1}) == ([], []))
  1967. raises(NotImplementedError, lambda: dsolve_system(eq, t=x, ics={f(0): 1, g(0): 1}) == ([], []))
  1968. raises(NotImplementedError, lambda: dsolve_system(eq, ics={f(0): 1, g(0): 1}) == ([], []))
  1969. raises(NotImplementedError, lambda: dsolve_system(eq, funcs=[f(x), g(x)], ics={f(0): 1, g(0): 1}) == ([], []))
  1970. def test_dsolve():
  1971. f, g = symbols('f g', cls=Function)
  1972. x, y = symbols('x y')
  1973. eqs = [f(x).diff(x) - x, f(x).diff(x) + x]
  1974. with raises(ValueError):
  1975. dsolve(eqs)
  1976. eqs = [f(x, y).diff(x)]
  1977. with raises(ValueError):
  1978. dsolve(eqs)
  1979. eqs = [f(x, y).diff(x)+g(x).diff(x), g(x).diff(x)]
  1980. with raises(ValueError):
  1981. dsolve(eqs)
  1982. @slow
  1983. def test_higher_order1_slow1():
  1984. x, y = symbols("x y", cls=Function)
  1985. t = symbols("t")
  1986. eq = [
  1987. Eq(diff(x(t),t,t), (log(t)+t**2)*diff(x(t),t)+(log(t)+t**2)*3*diff(y(t),t)),
  1988. Eq(diff(y(t),t,t), (log(t)+t**2)*2*diff(x(t),t)+(log(t)+t**2)*9*diff(y(t),t))
  1989. ]
  1990. sol, = dsolve_system(eq, simplify=False, doit=False)
  1991. # The solution is too long to write out explicitly and checkodesol is too
  1992. # slow so we test for particular values of t:
  1993. for e in eq:
  1994. res = (e.lhs - e.rhs).subs({sol[0].lhs:sol[0].rhs, sol[1].lhs:sol[1].rhs})
  1995. res = res.subs({d: d.doit(deep=False) for d in res.atoms(Derivative)})
  1996. assert ratsimp(res.subs(t, 1)) == 0
  1997. def test_second_order_type2_slow1():
  1998. x, y, z = symbols('x, y, z', cls=Function)
  1999. t, l = symbols('t, l')
  2000. eqs1 = [Eq(Derivative(x(t), (t, 2)), t*(2*x(t) + y(t))),
  2001. Eq(Derivative(y(t), (t, 2)), t*(-x(t) + 2*y(t)))]
  2002. sol1 = [Eq(x(t), I*C1*airyai(t*(2 - I)**(S(1)/3)) + I*C2*airybi(t*(2 - I)**(S(1)/3)) - I*C3*airyai(t*(2 +
  2003. I)**(S(1)/3)) - I*C4*airybi(t*(2 + I)**(S(1)/3))),
  2004. Eq(y(t), C1*airyai(t*(2 - I)**(S(1)/3)) + C2*airybi(t*(2 - I)**(S(1)/3)) + C3*airyai(t*(2 + I)**(S(1)/3)) +
  2005. C4*airybi(t*(2 + I)**(S(1)/3)))]
  2006. assert dsolve(eqs1) == sol1
  2007. assert checksysodesol(eqs1, sol1) == (True, [0, 0])
  2008. @slow
  2009. @XFAIL
  2010. def test_nonlinear_3eq_order1_type1():
  2011. if ON_CI:
  2012. skip("Too slow for CI.")
  2013. a, b, c = symbols('a b c')
  2014. eqs = [
  2015. a * f(x).diff(x) - (b - c) * g(x) * h(x),
  2016. b * g(x).diff(x) - (c - a) * h(x) * f(x),
  2017. c * h(x).diff(x) - (a - b) * f(x) * g(x),
  2018. ]
  2019. assert dsolve(eqs) # NotImplementedError
  2020. @XFAIL
  2021. def test_nonlinear_3eq_order1_type4():
  2022. eqs = [
  2023. Eq(f(x).diff(x), (2*h(x)*g(x) - 3*g(x)*h(x))),
  2024. Eq(g(x).diff(x), (4*f(x)*h(x) - 2*h(x)*f(x))),
  2025. Eq(h(x).diff(x), (3*g(x)*f(x) - 4*f(x)*g(x))),
  2026. ]
  2027. dsolve(eqs) # KeyError when matching
  2028. # sol = ?
  2029. # assert dsolve_sol == sol
  2030. # assert checksysodesol(eqs, dsolve_sol) == (True, [0, 0, 0])
  2031. @slow
  2032. @XFAIL
  2033. def test_nonlinear_3eq_order1_type3():
  2034. if ON_CI:
  2035. skip("Too slow for CI.")
  2036. eqs = [
  2037. Eq(f(x).diff(x), (2*f(x)**2 - 3 )),
  2038. Eq(g(x).diff(x), (4 - 2*h(x) )),
  2039. Eq(h(x).diff(x), (3*h(x) - 4*f(x)**2)),
  2040. ]
  2041. dsolve(eqs) # Not sure if this finishes...
  2042. # sol = ?
  2043. # assert dsolve_sol == sol
  2044. # assert checksysodesol(eqs, dsolve_sol) == (True, [0, 0, 0])
  2045. @XFAIL
  2046. def test_nonlinear_3eq_order1_type5():
  2047. eqs = [
  2048. Eq(f(x).diff(x), f(x)*(2*f(x) - 3*g(x))),
  2049. Eq(g(x).diff(x), g(x)*(4*g(x) - 2*h(x))),
  2050. Eq(h(x).diff(x), h(x)*(3*h(x) - 4*f(x))),
  2051. ]
  2052. dsolve(eqs) # KeyError
  2053. # sol = ?
  2054. # assert dsolve_sol == sol
  2055. # assert checksysodesol(eqs, dsolve_sol) == (True, [0, 0, 0])
  2056. def test_linear_2eq_order1():
  2057. x, y, z = symbols('x, y, z', cls=Function)
  2058. k, l, m, n = symbols('k, l, m, n', Integer=True)
  2059. t = Symbol('t')
  2060. x0, y0 = symbols('x0, y0', cls=Function)
  2061. eq1 = (Eq(diff(x(t),t), x(t) + y(t) + 9), Eq(diff(y(t),t), 2*x(t) + 5*y(t) + 23))
  2062. sol1 = [Eq(x(t), C1*exp(t*(sqrt(6) + 3)) + C2*exp(t*(-sqrt(6) + 3)) - Rational(22, 3)), \
  2063. Eq(y(t), C1*(2 + sqrt(6))*exp(t*(sqrt(6) + 3)) + C2*(-sqrt(6) + 2)*exp(t*(-sqrt(6) + 3)) - Rational(5, 3))]
  2064. assert checksysodesol(eq1, sol1) == (True, [0, 0])
  2065. eq2 = (Eq(diff(x(t),t), x(t) + y(t) + 81), Eq(diff(y(t),t), -2*x(t) + y(t) + 23))
  2066. sol2 = [Eq(x(t), (C1*cos(sqrt(2)*t) + C2*sin(sqrt(2)*t))*exp(t) - Rational(58, 3)), \
  2067. Eq(y(t), (-sqrt(2)*C1*sin(sqrt(2)*t) + sqrt(2)*C2*cos(sqrt(2)*t))*exp(t) - Rational(185, 3))]
  2068. assert checksysodesol(eq2, sol2) == (True, [0, 0])
  2069. eq3 = (Eq(diff(x(t),t), 5*t*x(t) + 2*y(t)), Eq(diff(y(t),t), 2*x(t) + 5*t*y(t)))
  2070. sol3 = [Eq(x(t), (C1*exp(2*t) + C2*exp(-2*t))*exp(Rational(5, 2)*t**2)), \
  2071. Eq(y(t), (C1*exp(2*t) - C2*exp(-2*t))*exp(Rational(5, 2)*t**2))]
  2072. assert checksysodesol(eq3, sol3) == (True, [0, 0])
  2073. eq4 = (Eq(diff(x(t),t), 5*t*x(t) + t**2*y(t)), Eq(diff(y(t),t), -t**2*x(t) + 5*t*y(t)))
  2074. sol4 = [Eq(x(t), (C1*cos((t**3)/3) + C2*sin((t**3)/3))*exp(Rational(5, 2)*t**2)), \
  2075. Eq(y(t), (-C1*sin((t**3)/3) + C2*cos((t**3)/3))*exp(Rational(5, 2)*t**2))]
  2076. assert checksysodesol(eq4, sol4) == (True, [0, 0])
  2077. eq5 = (Eq(diff(x(t),t), 5*t*x(t) + t**2*y(t)), Eq(diff(y(t),t), -t**2*x(t) + (5*t+9*t**2)*y(t)))
  2078. sol5 = [Eq(x(t), (C1*exp((sqrt(77)/2 + Rational(9, 2))*(t**3)/3) + \
  2079. C2*exp((-sqrt(77)/2 + Rational(9, 2))*(t**3)/3))*exp(Rational(5, 2)*t**2)), \
  2080. Eq(y(t), (C1*(sqrt(77)/2 + Rational(9, 2))*exp((sqrt(77)/2 + Rational(9, 2))*(t**3)/3) + \
  2081. C2*(-sqrt(77)/2 + Rational(9, 2))*exp((-sqrt(77)/2 + Rational(9, 2))*(t**3)/3))*exp(Rational(5, 2)*t**2))]
  2082. assert checksysodesol(eq5, sol5) == (True, [0, 0])
  2083. eq6 = (Eq(diff(x(t),t), 5*t*x(t) + t**2*y(t)), Eq(diff(y(t),t), (1-t**2)*x(t) + (5*t+9*t**2)*y(t)))
  2084. sol6 = [Eq(x(t), C1*x0(t) + C2*x0(t)*Integral(t**2*exp(Integral(5*t, t))*exp(Integral(9*t**2 + 5*t, t))/x0(t)**2, t)), \
  2085. Eq(y(t), C1*y0(t) + C2*(y0(t)*Integral(t**2*exp(Integral(5*t, t))*exp(Integral(9*t**2 + 5*t, t))/x0(t)**2, t) + \
  2086. exp(Integral(5*t, t))*exp(Integral(9*t**2 + 5*t, t))/x0(t)))]
  2087. s = dsolve(eq6)
  2088. assert s == sol6 # too complicated to test with subs and simplify
  2089. # assert checksysodesol(eq10, sol10) == (True, [0, 0]) # this one fails
  2090. def test_nonlinear_2eq_order1():
  2091. x, y, z = symbols('x, y, z', cls=Function)
  2092. t = Symbol('t')
  2093. eq1 = (Eq(diff(x(t),t),x(t)*y(t)**3), Eq(diff(y(t),t),y(t)**5))
  2094. sol1 = [
  2095. Eq(x(t), C1*exp((-1/(4*C2 + 4*t))**(Rational(-1, 4)))),
  2096. Eq(y(t), -(-1/(4*C2 + 4*t))**Rational(1, 4)),
  2097. Eq(x(t), C1*exp(-1/(-1/(4*C2 + 4*t))**Rational(1, 4))),
  2098. Eq(y(t), (-1/(4*C2 + 4*t))**Rational(1, 4)),
  2099. Eq(x(t), C1*exp(-I/(-1/(4*C2 + 4*t))**Rational(1, 4))),
  2100. Eq(y(t), -I*(-1/(4*C2 + 4*t))**Rational(1, 4)),
  2101. Eq(x(t), C1*exp(I/(-1/(4*C2 + 4*t))**Rational(1, 4))),
  2102. Eq(y(t), I*(-1/(4*C2 + 4*t))**Rational(1, 4))]
  2103. assert dsolve(eq1) == sol1
  2104. assert checksysodesol(eq1, sol1) == (True, [0, 0])
  2105. eq2 = (Eq(diff(x(t),t), exp(3*x(t))*y(t)**3),Eq(diff(y(t),t), y(t)**5))
  2106. sol2 = [
  2107. Eq(x(t), -log(C1 - 3/(-1/(4*C2 + 4*t))**Rational(1, 4))/3),
  2108. Eq(y(t), -(-1/(4*C2 + 4*t))**Rational(1, 4)),
  2109. Eq(x(t), -log(C1 + 3/(-1/(4*C2 + 4*t))**Rational(1, 4))/3),
  2110. Eq(y(t), (-1/(4*C2 + 4*t))**Rational(1, 4)),
  2111. Eq(x(t), -log(C1 + 3*I/(-1/(4*C2 + 4*t))**Rational(1, 4))/3),
  2112. Eq(y(t), -I*(-1/(4*C2 + 4*t))**Rational(1, 4)),
  2113. Eq(x(t), -log(C1 - 3*I/(-1/(4*C2 + 4*t))**Rational(1, 4))/3),
  2114. Eq(y(t), I*(-1/(4*C2 + 4*t))**Rational(1, 4))]
  2115. assert dsolve(eq2) == sol2
  2116. assert checksysodesol(eq2, sol2) == (True, [0, 0])
  2117. eq3 = (Eq(diff(x(t),t), y(t)*x(t)), Eq(diff(y(t),t), x(t)**3))
  2118. tt = Rational(2, 3)
  2119. sol3 = [
  2120. Eq(x(t), 6**tt/(6*(-sinh(sqrt(C1)*(C2 + t)/2)/sqrt(C1))**tt)),
  2121. Eq(y(t), sqrt(C1 + C1/sinh(sqrt(C1)*(C2 + t)/2)**2)/3)]
  2122. assert dsolve(eq3) == sol3
  2123. # FIXME: assert checksysodesol(eq3, sol3) == (True, [0, 0])
  2124. eq4 = (Eq(diff(x(t),t),x(t)*y(t)*sin(t)**2), Eq(diff(y(t),t),y(t)**2*sin(t)**2))
  2125. sol4 = {Eq(x(t), -2*exp(C1)/(C2*exp(C1) + t - sin(2*t)/2)), Eq(y(t), -2/(C1 + t - sin(2*t)/2))}
  2126. assert dsolve(eq4) == sol4
  2127. # FIXME: assert checksysodesol(eq4, sol4) == (True, [0, 0])
  2128. eq5 = (Eq(x(t),t*diff(x(t),t)+diff(x(t),t)*diff(y(t),t)), Eq(y(t),t*diff(y(t),t)+diff(y(t),t)**2))
  2129. sol5 = {Eq(x(t), C1*C2 + C1*t), Eq(y(t), C2**2 + C2*t)}
  2130. assert dsolve(eq5) == sol5
  2131. assert checksysodesol(eq5, sol5) == (True, [0, 0])
  2132. eq6 = (Eq(diff(x(t),t),x(t)**2*y(t)**3), Eq(diff(y(t),t),y(t)**5))
  2133. sol6 = [
  2134. Eq(x(t), 1/(C1 - 1/(-1/(4*C2 + 4*t))**Rational(1, 4))),
  2135. Eq(y(t), -(-1/(4*C2 + 4*t))**Rational(1, 4)),
  2136. Eq(x(t), 1/(C1 + (-1/(4*C2 + 4*t))**(Rational(-1, 4)))),
  2137. Eq(y(t), (-1/(4*C2 + 4*t))**Rational(1, 4)),
  2138. Eq(x(t), 1/(C1 + I/(-1/(4*C2 + 4*t))**Rational(1, 4))),
  2139. Eq(y(t), -I*(-1/(4*C2 + 4*t))**Rational(1, 4)),
  2140. Eq(x(t), 1/(C1 - I/(-1/(4*C2 + 4*t))**Rational(1, 4))),
  2141. Eq(y(t), I*(-1/(4*C2 + 4*t))**Rational(1, 4))]
  2142. assert dsolve(eq6) == sol6
  2143. assert checksysodesol(eq6, sol6) == (True, [0, 0])
  2144. @slow
  2145. def test_nonlinear_3eq_order1():
  2146. x, y, z = symbols('x, y, z', cls=Function)
  2147. t, u = symbols('t u')
  2148. eq1 = (4*diff(x(t),t) + 2*y(t)*z(t), 3*diff(y(t),t) - z(t)*x(t), 5*diff(z(t),t) - x(t)*y(t))
  2149. sol1 = [Eq(4*Integral(1/(sqrt(-4*u**2 - 3*C1 + C2)*sqrt(-4*u**2 + 5*C1 - C2)), (u, x(t))),
  2150. C3 - sqrt(15)*t/15), Eq(3*Integral(1/(sqrt(-6*u**2 - C1 + 5*C2)*sqrt(3*u**2 + C1 - 4*C2)),
  2151. (u, y(t))), C3 + sqrt(5)*t/10), Eq(5*Integral(1/(sqrt(-10*u**2 - 3*C1 + C2)*
  2152. sqrt(5*u**2 + 4*C1 - C2)), (u, z(t))), C3 + sqrt(3)*t/6)]
  2153. assert [i.dummy_eq(j) for i, j in zip(dsolve(eq1), sol1)]
  2154. # FIXME: assert checksysodesol(eq1, sol1) == (True, [0, 0, 0])
  2155. eq2 = (4*diff(x(t),t) + 2*y(t)*z(t)*sin(t), 3*diff(y(t),t) - z(t)*x(t)*sin(t), 5*diff(z(t),t) - x(t)*y(t)*sin(t))
  2156. sol2 = [Eq(3*Integral(1/(sqrt(-6*u**2 - C1 + 5*C2)*sqrt(3*u**2 + C1 - 4*C2)), (u, x(t))), C3 +
  2157. sqrt(5)*cos(t)/10), Eq(4*Integral(1/(sqrt(-4*u**2 - 3*C1 + C2)*sqrt(-4*u**2 + 5*C1 - C2)),
  2158. (u, y(t))), C3 - sqrt(15)*cos(t)/15), Eq(5*Integral(1/(sqrt(-10*u**2 - 3*C1 + C2)*
  2159. sqrt(5*u**2 + 4*C1 - C2)), (u, z(t))), C3 + sqrt(3)*cos(t)/6)]
  2160. assert [i.dummy_eq(j) for i, j in zip(dsolve(eq2), sol2)]
  2161. # FIXME: assert checksysodesol(eq2, sol2) == (True, [0, 0, 0])
  2162. def test_C1_function_9239():
  2163. t = Symbol('t')
  2164. C1 = Function('C1')
  2165. C2 = Function('C2')
  2166. C3 = Symbol('C3')
  2167. C4 = Symbol('C4')
  2168. eq = (Eq(diff(C1(t), t), 9*C2(t)), Eq(diff(C2(t), t), 12*C1(t)))
  2169. sol = [Eq(C1(t), 9*C3*exp(6*sqrt(3)*t) + 9*C4*exp(-6*sqrt(3)*t)),
  2170. Eq(C2(t), 6*sqrt(3)*C3*exp(6*sqrt(3)*t) - 6*sqrt(3)*C4*exp(-6*sqrt(3)*t))]
  2171. assert checksysodesol(eq, sol) == (True, [0, 0])
  2172. def test_dsolve_linsystem_symbol():
  2173. eps = Symbol('epsilon', positive=True)
  2174. eq1 = (Eq(diff(f(x), x), -eps*g(x)), Eq(diff(g(x), x), eps*f(x)))
  2175. sol1 = [Eq(f(x), -C1*eps*cos(eps*x) - C2*eps*sin(eps*x)),
  2176. Eq(g(x), -C1*eps*sin(eps*x) + C2*eps*cos(eps*x))]
  2177. assert checksysodesol(eq1, sol1) == (True, [0, 0])