test_single.py 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897
  1. #
  2. # The main tests for the code in single.py are currently located in
  3. # sympy/solvers/tests/test_ode.py
  4. #
  5. r"""
  6. This File contains test functions for the individual hints used for solving ODEs.
  7. Examples of each solver will be returned by _get_examples_ode_sol_name_of_solver.
  8. Examples should have a key 'XFAIL' which stores the list of hints if they are
  9. expected to fail for that hint.
  10. Functions that are for internal use:
  11. 1) _ode_solver_test(ode_examples) - It takes a dictionary of examples returned by
  12. _get_examples method and tests them with their respective hints.
  13. 2) _test_particular_example(our_hint, example_name) - It tests the ODE example corresponding
  14. to the hint provided.
  15. 3) _test_all_hints(runxfail=False) - It is used to test all the examples with all the hints
  16. currently implemented. It calls _test_all_examples_for_one_hint() which outputs whether the
  17. given hint functions properly if it classifies the ODE example.
  18. If runxfail flag is set to True then it will only test the examples which are expected to fail.
  19. Everytime the ODE of a particular solver is added, _test_all_hints() is to be executed to find
  20. the possible failures of different solver hints.
  21. 4) _test_all_examples_for_one_hint(our_hint, all_examples) - It takes hint as argument and checks
  22. this hint against all the ODE examples and gives output as the number of ODEs matched, number
  23. of ODEs which were solved correctly, list of ODEs which gives incorrect solution and list of
  24. ODEs which raises exception.
  25. """
  26. from sympy.core.function import (Derivative, diff)
  27. from sympy.core.mul import Mul
  28. from sympy.core.numbers import (E, I, Rational, pi)
  29. from sympy.core.relational import (Eq, Ne)
  30. from sympy.core.singleton import S
  31. from sympy.core.symbol import (Dummy, symbols)
  32. from sympy.functions.elementary.complexes import (im, re)
  33. from sympy.functions.elementary.exponential import (LambertW, exp, log)
  34. from sympy.functions.elementary.hyperbolic import (asinh, cosh, sinh, tanh)
  35. from sympy.functions.elementary.miscellaneous import (cbrt, sqrt)
  36. from sympy.functions.elementary.piecewise import Piecewise
  37. from sympy.functions.elementary.trigonometric import (acos, asin, atan, cos, sec, sin, tan)
  38. from sympy.functions.special.error_functions import (Ei, erfi)
  39. from sympy.functions.special.hyper import hyper
  40. from sympy.integrals.integrals import (Integral, integrate)
  41. from sympy.polys.rootoftools import rootof
  42. from sympy.core import Function, Symbol
  43. from sympy.functions import airyai, airybi, besselj, bessely, lowergamma
  44. from sympy.integrals.risch import NonElementaryIntegral
  45. from sympy.solvers.ode import classify_ode, dsolve
  46. from sympy.solvers.ode.ode import allhints, _remove_redundant_solutions
  47. from sympy.solvers.ode.single import (FirstLinear, ODEMatchError,
  48. SingleODEProblem, SingleODESolver, NthOrderReducible)
  49. from sympy.solvers.ode.subscheck import checkodesol
  50. from sympy.testing.pytest import raises, slow, ON_CI
  51. import traceback
  52. x = Symbol('x')
  53. u = Symbol('u')
  54. _u = Dummy('u')
  55. y = Symbol('y')
  56. f = Function('f')
  57. g = Function('g')
  58. C1, C2, C3, C4, C5, C6, C7, C8, C9, C10 = symbols('C1:11')
  59. hint_message = """\
  60. Hint did not match the example {example}.
  61. The ODE is:
  62. {eq}.
  63. The expected hint was
  64. {our_hint}\
  65. """
  66. expected_sol_message = """\
  67. Different solution found from dsolve for example {example}.
  68. The ODE is:
  69. {eq}
  70. The expected solution was
  71. {sol}
  72. What dsolve returned is:
  73. {dsolve_sol}\
  74. """
  75. checkodesol_msg = """\
  76. solution found is not correct for example {example}.
  77. The ODE is:
  78. {eq}\
  79. """
  80. dsol_incorrect_msg = """\
  81. solution returned by dsolve is incorrect when using {hint}.
  82. The ODE is:
  83. {eq}
  84. The expected solution was
  85. {sol}
  86. what dsolve returned is:
  87. {dsolve_sol}
  88. You can test this with:
  89. eq = {eq}
  90. sol = dsolve(eq, hint='{hint}')
  91. print(sol)
  92. print(checkodesol(eq, sol))
  93. """
  94. exception_msg = """\
  95. dsolve raised exception : {e}
  96. when using {hint} for the example {example}
  97. You can test this with:
  98. from sympy.solvers.ode.tests.test_single import _test_an_example
  99. _test_an_example('{hint}', example_name = '{example}')
  100. The ODE is:
  101. {eq}
  102. \
  103. """
  104. check_hint_msg = """\
  105. Tested hint was : {hint}
  106. Total of {matched} examples matched with this hint.
  107. Out of which {solve} gave correct results.
  108. Examples which gave incorrect results are {unsolve}.
  109. Examples which raised exceptions are {exceptions}
  110. \
  111. """
  112. def _add_example_keys(func):
  113. def inner():
  114. solver=func()
  115. examples=[]
  116. for example in solver['examples']:
  117. temp={
  118. 'eq': solver['examples'][example]['eq'],
  119. 'sol': solver['examples'][example]['sol'],
  120. 'XFAIL': solver['examples'][example].get('XFAIL', []),
  121. 'func': solver['examples'][example].get('func',solver['func']),
  122. 'example_name': example,
  123. 'slow': solver['examples'][example].get('slow', False),
  124. 'simplify_flag':solver['examples'][example].get('simplify_flag',True),
  125. 'checkodesol_XFAIL': solver['examples'][example].get('checkodesol_XFAIL', False),
  126. 'dsolve_too_slow':solver['examples'][example].get('dsolve_too_slow',False),
  127. 'checkodesol_too_slow':solver['examples'][example].get('checkodesol_too_slow',False),
  128. 'hint': solver['hint']
  129. }
  130. examples.append(temp)
  131. return examples
  132. return inner()
  133. def _ode_solver_test(ode_examples, run_slow_test=False):
  134. for example in ode_examples:
  135. if ((not run_slow_test) and example['slow']) or (run_slow_test and (not example['slow'])):
  136. continue
  137. result = _test_particular_example(example['hint'], example, solver_flag=True)
  138. if result['xpass_msg'] != "":
  139. print(result['xpass_msg'])
  140. def _test_all_hints(runxfail=False):
  141. all_hints = list(allhints)+["default"]
  142. all_examples = _get_all_examples()
  143. for our_hint in all_hints:
  144. if our_hint.endswith('_Integral') or 'series' in our_hint:
  145. continue
  146. _test_all_examples_for_one_hint(our_hint, all_examples, runxfail)
  147. def _test_dummy_sol(expected_sol,dsolve_sol):
  148. if type(dsolve_sol)==list:
  149. return any(expected_sol.dummy_eq(sub_dsol) for sub_dsol in dsolve_sol)
  150. else:
  151. return expected_sol.dummy_eq(dsolve_sol)
  152. def _test_an_example(our_hint, example_name):
  153. all_examples = _get_all_examples()
  154. for example in all_examples:
  155. if example['example_name'] == example_name:
  156. _test_particular_example(our_hint, example)
  157. def _test_particular_example(our_hint, ode_example, solver_flag=False):
  158. eq = ode_example['eq']
  159. expected_sol = ode_example['sol']
  160. example = ode_example['example_name']
  161. xfail = our_hint in ode_example['XFAIL']
  162. func = ode_example['func']
  163. result = {'msg': '', 'xpass_msg': ''}
  164. simplify_flag=ode_example['simplify_flag']
  165. checkodesol_XFAIL = ode_example['checkodesol_XFAIL']
  166. dsolve_too_slow = ode_example['dsolve_too_slow']
  167. checkodesol_too_slow = ode_example['checkodesol_too_slow']
  168. xpass = True
  169. if solver_flag:
  170. if our_hint not in classify_ode(eq, func):
  171. message = hint_message.format(example=example, eq=eq, our_hint=our_hint)
  172. raise AssertionError(message)
  173. if our_hint in classify_ode(eq, func):
  174. result['match_list'] = example
  175. try:
  176. if not (dsolve_too_slow):
  177. dsolve_sol = dsolve(eq, func, simplify=simplify_flag,hint=our_hint)
  178. else:
  179. if len(expected_sol)==1:
  180. dsolve_sol = expected_sol[0]
  181. else:
  182. dsolve_sol = expected_sol
  183. except Exception as e:
  184. dsolve_sol = []
  185. result['exception_list'] = example
  186. if not solver_flag:
  187. traceback.print_exc()
  188. result['msg'] = exception_msg.format(e=str(e), hint=our_hint, example=example, eq=eq)
  189. if solver_flag and not xfail:
  190. print(result['msg'])
  191. raise
  192. xpass = False
  193. if solver_flag and dsolve_sol!=[]:
  194. expect_sol_check = False
  195. if type(dsolve_sol)==list:
  196. for sub_sol in expected_sol:
  197. if sub_sol.has(Dummy):
  198. expect_sol_check = not _test_dummy_sol(sub_sol, dsolve_sol)
  199. else:
  200. expect_sol_check = sub_sol not in dsolve_sol
  201. if expect_sol_check:
  202. break
  203. else:
  204. expect_sol_check = dsolve_sol not in expected_sol
  205. for sub_sol in expected_sol:
  206. if sub_sol.has(Dummy):
  207. expect_sol_check = not _test_dummy_sol(sub_sol, dsolve_sol)
  208. if expect_sol_check:
  209. message = expected_sol_message.format(example=example, eq=eq, sol=expected_sol, dsolve_sol=dsolve_sol)
  210. raise AssertionError(message)
  211. expected_checkodesol = [(True, 0) for i in range(len(expected_sol))]
  212. if len(expected_sol) == 1:
  213. expected_checkodesol = (True, 0)
  214. if not (checkodesol_too_slow and ON_CI):
  215. if not checkodesol_XFAIL:
  216. if checkodesol(eq, dsolve_sol, func, solve_for_func=False) != expected_checkodesol:
  217. result['unsolve_list'] = example
  218. xpass = False
  219. message = dsol_incorrect_msg.format(hint=our_hint, eq=eq, sol=expected_sol,dsolve_sol=dsolve_sol)
  220. if solver_flag:
  221. message = checkodesol_msg.format(example=example, eq=eq)
  222. raise AssertionError(message)
  223. else:
  224. result['msg'] = 'AssertionError: ' + message
  225. if xpass and xfail:
  226. result['xpass_msg'] = example + "is now passing for the hint" + our_hint
  227. return result
  228. def _test_all_examples_for_one_hint(our_hint, all_examples=[], runxfail=None):
  229. if all_examples == []:
  230. all_examples = _get_all_examples()
  231. match_list, unsolve_list, exception_list = [], [], []
  232. for ode_example in all_examples:
  233. xfail = our_hint in ode_example['XFAIL']
  234. if runxfail and not xfail:
  235. continue
  236. if xfail:
  237. continue
  238. result = _test_particular_example(our_hint, ode_example)
  239. match_list += result.get('match_list',[])
  240. unsolve_list += result.get('unsolve_list',[])
  241. exception_list += result.get('exception_list',[])
  242. if runxfail is not None:
  243. msg = result['msg']
  244. if msg!='':
  245. print(result['msg'])
  246. # print(result.get('xpass_msg',''))
  247. if runxfail is None:
  248. match_count = len(match_list)
  249. solved = len(match_list)-len(unsolve_list)-len(exception_list)
  250. msg = check_hint_msg.format(hint=our_hint, matched=match_count, solve=solved, unsolve=unsolve_list, exceptions=exception_list)
  251. print(msg)
  252. def test_SingleODESolver():
  253. # Test that not implemented methods give NotImplementedError
  254. # Subclasses should override these methods.
  255. problem = SingleODEProblem(f(x).diff(x), f(x), x)
  256. solver = SingleODESolver(problem)
  257. raises(NotImplementedError, lambda: solver.matches())
  258. raises(NotImplementedError, lambda: solver.get_general_solution())
  259. raises(NotImplementedError, lambda: solver._matches())
  260. raises(NotImplementedError, lambda: solver._get_general_solution())
  261. # This ODE can not be solved by the FirstLinear solver. Here we test that
  262. # it does not match and the asking for a general solution gives
  263. # ODEMatchError
  264. problem = SingleODEProblem(f(x).diff(x) + f(x)*f(x), f(x), x)
  265. solver = FirstLinear(problem)
  266. raises(ODEMatchError, lambda: solver.get_general_solution())
  267. solver = FirstLinear(problem)
  268. assert solver.matches() is False
  269. #These are just test for order of ODE
  270. problem = SingleODEProblem(f(x).diff(x) + f(x), f(x), x)
  271. assert problem.order == 1
  272. problem = SingleODEProblem(f(x).diff(x,4) + f(x).diff(x,2) - f(x).diff(x,3), f(x), x)
  273. assert problem.order == 4
  274. problem = SingleODEProblem(f(x).diff(x, 3) + f(x).diff(x, 2) - f(x)**2, f(x), x)
  275. assert problem.is_autonomous == True
  276. problem = SingleODEProblem(f(x).diff(x, 3) + x*f(x).diff(x, 2) - f(x)**2, f(x), x)
  277. assert problem.is_autonomous == False
  278. def test_linear_coefficients():
  279. _ode_solver_test(_get_examples_ode_sol_linear_coefficients)
  280. @slow
  281. def test_1st_homogeneous_coeff_ode():
  282. #These were marked as test_1st_homogeneous_coeff_corner_case
  283. eq1 = f(x).diff(x) - f(x)/x
  284. c1 = classify_ode(eq1, f(x))
  285. eq2 = x*f(x).diff(x) - f(x)
  286. c2 = classify_ode(eq2, f(x))
  287. sdi = "1st_homogeneous_coeff_subs_dep_div_indep"
  288. sid = "1st_homogeneous_coeff_subs_indep_div_dep"
  289. assert sid not in c1 and sdi not in c1
  290. assert sid not in c2 and sdi not in c2
  291. _ode_solver_test(_get_examples_ode_sol_1st_homogeneous_coeff_subs_dep_div_indep)
  292. _ode_solver_test(_get_examples_ode_sol_1st_homogeneous_coeff_best)
  293. @slow
  294. def test_slow_examples_1st_homogeneous_coeff_ode():
  295. _ode_solver_test(_get_examples_ode_sol_1st_homogeneous_coeff_subs_dep_div_indep, run_slow_test=True)
  296. _ode_solver_test(_get_examples_ode_sol_1st_homogeneous_coeff_best, run_slow_test=True)
  297. @slow
  298. def test_nth_linear_constant_coeff_homogeneous():
  299. _ode_solver_test(_get_examples_ode_sol_nth_linear_constant_coeff_homogeneous)
  300. @slow
  301. def test_slow_examples_nth_linear_constant_coeff_homogeneous():
  302. _ode_solver_test(_get_examples_ode_sol_nth_linear_constant_coeff_homogeneous, run_slow_test=True)
  303. def test_Airy_equation():
  304. _ode_solver_test(_get_examples_ode_sol_2nd_linear_airy)
  305. @slow
  306. def test_lie_group():
  307. _ode_solver_test(_get_examples_ode_sol_lie_group)
  308. @slow
  309. def test_separable_reduced():
  310. df = f(x).diff(x)
  311. eq = (x / f(x))*df + tan(x**2*f(x) / (x**2*f(x) - 1))
  312. assert classify_ode(eq) == ('factorable', 'separable_reduced', 'lie_group',
  313. 'separable_reduced_Integral')
  314. _ode_solver_test(_get_examples_ode_sol_separable_reduced)
  315. @slow
  316. def test_slow_examples_separable_reduced():
  317. _ode_solver_test(_get_examples_ode_sol_separable_reduced, run_slow_test=True)
  318. @slow
  319. def test_2nd_2F1_hypergeometric():
  320. _ode_solver_test(_get_examples_ode_sol_2nd_2F1_hypergeometric)
  321. def test_2nd_2F1_hypergeometric_integral():
  322. eq = x*(x-1)*f(x).diff(x, 2) + (-1+ S(7)/2*x)*f(x).diff(x) + f(x)
  323. sol = Eq(f(x), (C1 + C2*Integral(exp(Integral((1 - x/2)/(x*(x - 1)), x))/(1 -
  324. x/2)**2, x))*exp(Integral(1/(x - 1), x)/4)*exp(-Integral(7/(x -
  325. 1), x)/4)*hyper((S(1)/2, -1), (1,), x))
  326. assert sol == dsolve(eq, hint='2nd_hypergeometric_Integral')
  327. assert checkodesol(eq, sol) == (True, 0)
  328. @slow
  329. def test_2nd_nonlinear_autonomous_conserved():
  330. _ode_solver_test(_get_examples_ode_sol_2nd_nonlinear_autonomous_conserved)
  331. def test_2nd_nonlinear_autonomous_conserved_integral():
  332. eq = f(x).diff(x, 2) + asin(f(x))
  333. actual = [Eq(Integral(1/sqrt(C1 - 2*Integral(asin(_u), _u)), (_u, f(x))), C2 + x),
  334. Eq(Integral(1/sqrt(C1 - 2*Integral(asin(_u), _u)), (_u, f(x))), C2 - x)]
  335. solved = dsolve(eq, hint='2nd_nonlinear_autonomous_conserved_Integral', simplify=False)
  336. for a,s in zip(actual, solved):
  337. assert a.dummy_eq(s)
  338. # checkodesol unable to simplify solutions with f(x) in an integral equation
  339. assert checkodesol(eq, [s.doit() for s in solved]) == [(True, 0), (True, 0)]
  340. @slow
  341. def test_2nd_linear_bessel_equation():
  342. _ode_solver_test(_get_examples_ode_sol_2nd_linear_bessel)
  343. @slow
  344. def test_nth_algebraic():
  345. eqn = f(x) + f(x)*f(x).diff(x)
  346. solns = [Eq(f(x), exp(x)),
  347. Eq(f(x), C1*exp(C2*x))]
  348. solns_final = _remove_redundant_solutions(eqn, solns, 2, x)
  349. assert solns_final == [Eq(f(x), C1*exp(C2*x))]
  350. _ode_solver_test(_get_examples_ode_sol_nth_algebraic)
  351. @slow
  352. def test_slow_examples_nth_linear_constant_coeff_var_of_parameters():
  353. _ode_solver_test(_get_examples_ode_sol_nth_linear_var_of_parameters, run_slow_test=True)
  354. def test_nth_linear_constant_coeff_var_of_parameters():
  355. _ode_solver_test(_get_examples_ode_sol_nth_linear_var_of_parameters)
  356. @slow
  357. def test_nth_linear_constant_coeff_variation_of_parameters__integral():
  358. # solve_variation_of_parameters shouldn't attempt to simplify the
  359. # Wronskian if simplify=False. If wronskian() ever gets good enough
  360. # to simplify the result itself, this test might fail.
  361. our_hint = 'nth_linear_constant_coeff_variation_of_parameters_Integral'
  362. eq = f(x).diff(x, 5) + 2*f(x).diff(x, 3) + f(x).diff(x) - 2*x - exp(I*x)
  363. sol_simp = dsolve(eq, f(x), hint=our_hint, simplify=True)
  364. sol_nsimp = dsolve(eq, f(x), hint=our_hint, simplify=False)
  365. assert sol_simp != sol_nsimp
  366. assert checkodesol(eq, sol_simp, order=5, solve_for_func=False) == (True, 0)
  367. assert checkodesol(eq, sol_simp, order=5, solve_for_func=False) == (True, 0)
  368. @slow
  369. def test_slow_examples_1st_exact():
  370. _ode_solver_test(_get_examples_ode_sol_1st_exact, run_slow_test=True)
  371. @slow
  372. def test_1st_exact():
  373. _ode_solver_test(_get_examples_ode_sol_1st_exact)
  374. def test_1st_exact_integral():
  375. eq = cos(f(x)) - (x*sin(f(x)) - f(x)**2)*f(x).diff(x)
  376. sol_1 = dsolve(eq, f(x), simplify=False, hint='1st_exact_Integral')
  377. assert checkodesol(eq, sol_1, order=1, solve_for_func=False)
  378. @slow
  379. def test_slow_examples_nth_order_reducible():
  380. _ode_solver_test(_get_examples_ode_sol_nth_order_reducible, run_slow_test=True)
  381. @slow
  382. def test_slow_examples_nth_linear_constant_coeff_undetermined_coefficients():
  383. _ode_solver_test(_get_examples_ode_sol_nth_linear_undetermined_coefficients, run_slow_test=True)
  384. @slow
  385. def test_slow_examples_separable():
  386. _ode_solver_test(_get_examples_ode_sol_separable, run_slow_test=True)
  387. @slow
  388. def test_nth_linear_constant_coeff_undetermined_coefficients():
  389. #issue-https://github.com/sympy/sympy/issues/5787
  390. # This test case is to show the classification of imaginary constants under
  391. # nth_linear_constant_coeff_undetermined_coefficients
  392. eq = Eq(diff(f(x), x), I*f(x) + S.Half - I)
  393. our_hint = 'nth_linear_constant_coeff_undetermined_coefficients'
  394. assert our_hint in classify_ode(eq)
  395. _ode_solver_test(_get_examples_ode_sol_nth_linear_undetermined_coefficients)
  396. def test_nth_order_reducible():
  397. F = lambda eq: NthOrderReducible(SingleODEProblem(eq, f(x), x))._matches()
  398. D = Derivative
  399. assert F(D(y*f(x), x, y) + D(f(x), x)) == False
  400. assert F(D(y*f(y), y, y) + D(f(y), y)) == False
  401. assert F(f(x)*D(f(x), x) + D(f(x), x, 2))== False
  402. assert F(D(x*f(y), y, 2) + D(u*y*f(x), x, 3)) == False # no simplification by design
  403. assert F(D(f(y), y, 2) + D(f(y), y, 3) + D(f(x), x, 4)) == False
  404. assert F(D(f(x), x, 2) + D(f(x), x, 3)) == True
  405. _ode_solver_test(_get_examples_ode_sol_nth_order_reducible)
  406. @slow
  407. def test_separable():
  408. _ode_solver_test(_get_examples_ode_sol_separable)
  409. @slow
  410. def test_factorable():
  411. assert integrate(-asin(f(2*x)+pi), x) == -Integral(asin(pi + f(2*x)), x)
  412. _ode_solver_test(_get_examples_ode_sol_factorable)
  413. @slow
  414. def test_slow_examples_factorable():
  415. _ode_solver_test(_get_examples_ode_sol_factorable, run_slow_test=True)
  416. def test_Riccati_special_minus2():
  417. _ode_solver_test(_get_examples_ode_sol_riccati)
  418. @slow
  419. def test_1st_rational_riccati():
  420. _ode_solver_test(_get_examples_ode_sol_1st_rational_riccati)
  421. def test_Bernoulli():
  422. _ode_solver_test(_get_examples_ode_sol_bernoulli)
  423. def test_1st_linear():
  424. _ode_solver_test(_get_examples_ode_sol_1st_linear)
  425. def test_almost_linear():
  426. _ode_solver_test(_get_examples_ode_sol_almost_linear)
  427. @slow
  428. def test_Liouville_ODE():
  429. hint = 'Liouville'
  430. not_Liouville1 = classify_ode(diff(f(x), x)/x + f(x)*diff(f(x), x, x)/2 -
  431. diff(f(x), x)**2/2, f(x))
  432. not_Liouville2 = classify_ode(diff(f(x), x)/x + diff(f(x), x, x)/2 -
  433. x*diff(f(x), x)**2/2, f(x))
  434. assert hint not in not_Liouville1
  435. assert hint not in not_Liouville2
  436. assert hint + '_Integral' not in not_Liouville1
  437. assert hint + '_Integral' not in not_Liouville2
  438. _ode_solver_test(_get_examples_ode_sol_liouville)
  439. def test_nth_order_linear_euler_eq_homogeneous():
  440. x, t, a, b, c = symbols('x t a b c')
  441. y = Function('y')
  442. our_hint = "nth_linear_euler_eq_homogeneous"
  443. eq = diff(f(t), t, 4)*t**4 - 13*diff(f(t), t, 2)*t**2 + 36*f(t)
  444. assert our_hint in classify_ode(eq)
  445. eq = a*y(t) + b*t*diff(y(t), t) + c*t**2*diff(y(t), t, 2)
  446. assert our_hint in classify_ode(eq)
  447. _ode_solver_test(_get_examples_ode_sol_euler_homogeneous)
  448. def test_nth_order_linear_euler_eq_nonhomogeneous_undetermined_coefficients():
  449. x, t = symbols('x t')
  450. a, b, c, d = symbols('a b c d', integer=True)
  451. our_hint = "nth_linear_euler_eq_nonhomogeneous_undetermined_coefficients"
  452. eq = x**4*diff(f(x), x, 4) - 13*x**2*diff(f(x), x, 2) + 36*f(x) + x
  453. assert our_hint in classify_ode(eq, f(x))
  454. eq = a*x**2*diff(f(x), x, 2) + b*x*diff(f(x), x) + c*f(x) + d*log(x)
  455. assert our_hint in classify_ode(eq, f(x))
  456. _ode_solver_test(_get_examples_ode_sol_euler_undetermined_coeff)
  457. @slow
  458. def test_nth_order_linear_euler_eq_nonhomogeneous_variation_of_parameters():
  459. x, t = symbols('x, t')
  460. a, b, c, d = symbols('a, b, c, d', integer=True)
  461. our_hint = "nth_linear_euler_eq_nonhomogeneous_variation_of_parameters"
  462. eq = Eq(x**2*diff(f(x),x,2) - 8*x*diff(f(x),x) + 12*f(x), x**2)
  463. assert our_hint in classify_ode(eq, f(x))
  464. eq = Eq(a*x**3*diff(f(x),x,3) + b*x**2*diff(f(x),x,2) + c*x*diff(f(x),x) + d*f(x), x*log(x))
  465. assert our_hint in classify_ode(eq, f(x))
  466. _ode_solver_test(_get_examples_ode_sol_euler_var_para)
  467. @_add_example_keys
  468. def _get_examples_ode_sol_euler_homogeneous():
  469. r1, r2, r3, r4, r5 = [rootof(x**5 - 14*x**4 + 71*x**3 - 154*x**2 + 120*x - 1, n) for n in range(5)]
  470. return {
  471. 'hint': "nth_linear_euler_eq_homogeneous",
  472. 'func': f(x),
  473. 'examples':{
  474. 'euler_hom_01': {
  475. 'eq': Eq(-3*diff(f(x), x)*x + 2*x**2*diff(f(x), x, x), 0),
  476. 'sol': [Eq(f(x), C1 + C2*x**Rational(5, 2))],
  477. },
  478. 'euler_hom_02': {
  479. 'eq': Eq(3*f(x) - 5*diff(f(x), x)*x + 2*x**2*diff(f(x), x, x), 0),
  480. 'sol': [Eq(f(x), C1*sqrt(x) + C2*x**3)]
  481. },
  482. 'euler_hom_03': {
  483. 'eq': Eq(4*f(x) + 5*diff(f(x), x)*x + x**2*diff(f(x), x, x), 0),
  484. 'sol': [Eq(f(x), (C1 + C2*log(x))/x**2)]
  485. },
  486. 'euler_hom_04': {
  487. 'eq': Eq(6*f(x) - 6*diff(f(x), x)*x + 1*x**2*diff(f(x), x, x) + x**3*diff(f(x), x, x, x), 0),
  488. 'sol': [Eq(f(x), C1/x**2 + C2*x + C3*x**3)]
  489. },
  490. 'euler_hom_05': {
  491. 'eq': Eq(-125*f(x) + 61*diff(f(x), x)*x - 12*x**2*diff(f(x), x, x) + x**3*diff(f(x), x, x, x), 0),
  492. 'sol': [Eq(f(x), x**5*(C1 + C2*log(x) + C3*log(x)**2))]
  493. },
  494. 'euler_hom_06': {
  495. 'eq': x**2*diff(f(x), x, 2) + x*diff(f(x), x) - 9*f(x),
  496. 'sol': [Eq(f(x), C1*x**-3 + C2*x**3)]
  497. },
  498. 'euler_hom_07': {
  499. 'eq': sin(x)*x**2*f(x).diff(x, 2) + sin(x)*x*f(x).diff(x) + sin(x)*f(x),
  500. 'sol': [Eq(f(x), C1*sin(log(x)) + C2*cos(log(x)))],
  501. 'XFAIL': ['2nd_power_series_regular','nth_linear_euler_eq_nonhomogeneous_undetermined_coefficients']
  502. },
  503. 'euler_hom_08': {
  504. 'eq': x**6 * f(x).diff(x, 6) - x*f(x).diff(x) + f(x),
  505. 'sol': [Eq(f(x), C1*x + C2*x**r1 + C3*x**r2 + C4*x**r3 + C5*x**r4 + C6*x**r5)],
  506. 'checkodesol_XFAIL':True
  507. },
  508. #This example is from issue: https://github.com/sympy/sympy/issues/15237 #This example is from issue:
  509. # https://github.com/sympy/sympy/issues/15237
  510. 'euler_hom_09': {
  511. 'eq': Derivative(x*f(x), x, x, x),
  512. 'sol': [Eq(f(x), C1 + C2/x + C3*x)],
  513. },
  514. }
  515. }
  516. @_add_example_keys
  517. def _get_examples_ode_sol_euler_undetermined_coeff():
  518. return {
  519. 'hint': "nth_linear_euler_eq_nonhomogeneous_undetermined_coefficients",
  520. 'func': f(x),
  521. 'examples':{
  522. 'euler_undet_01': {
  523. 'eq': Eq(x**2*diff(f(x), x, x) + x*diff(f(x), x), 1),
  524. 'sol': [Eq(f(x), C1 + C2*log(x) + log(x)**2/2)]
  525. },
  526. 'euler_undet_02': {
  527. 'eq': Eq(x**2*diff(f(x), x, x) - 2*x*diff(f(x), x) + 2*f(x), x**3),
  528. 'sol': [Eq(f(x), x*(C1 + C2*x + Rational(1, 2)*x**2))]
  529. },
  530. 'euler_undet_03': {
  531. 'eq': Eq(x**2*diff(f(x), x, x) - x*diff(f(x), x) - 3*f(x), log(x)/x),
  532. 'sol': [Eq(f(x), (C1 + C2*x**4 - log(x)**2/8 - log(x)/16)/x)]
  533. },
  534. 'euler_undet_04': {
  535. 'eq': Eq(x**2*diff(f(x), x, x) + 3*x*diff(f(x), x) - 8*f(x), log(x)**3 - log(x)),
  536. 'sol': [Eq(f(x), C1/x**4 + C2*x**2 - Rational(1,8)*log(x)**3 - Rational(3,32)*log(x)**2 - Rational(1,64)*log(x) - Rational(7, 256))]
  537. },
  538. 'euler_undet_05': {
  539. 'eq': Eq(x**3*diff(f(x), x, x, x) - 3*x**2*diff(f(x), x, x) + 6*x*diff(f(x), x) - 6*f(x), log(x)),
  540. 'sol': [Eq(f(x), C1*x + C2*x**2 + C3*x**3 - Rational(1, 6)*log(x) - Rational(11, 36))]
  541. },
  542. #Below examples were added for the issue: https://github.com/sympy/sympy/issues/5096
  543. 'euler_undet_06': {
  544. 'eq': 2*x**2*f(x).diff(x, 2) + f(x) + sqrt(2*x)*sin(log(2*x)/2),
  545. 'sol': [Eq(f(x), sqrt(x)*(C1*sin(log(x)/2) + C2*cos(log(x)/2) + sqrt(2)*log(x)*cos(log(2*x)/2)/2))]
  546. },
  547. 'euler_undet_07': {
  548. 'eq': 2*x**2*f(x).diff(x, 2) + f(x) + sin(log(2*x)/2),
  549. 'sol': [Eq(f(x), C1*sqrt(x)*sin(log(x)/2) + C2*sqrt(x)*cos(log(x)/2) - 2*sin(log(2*x)/2)/5 - 4*cos(log(2*x)/2)/5)]
  550. },
  551. }
  552. }
  553. @_add_example_keys
  554. def _get_examples_ode_sol_euler_var_para():
  555. return {
  556. 'hint': "nth_linear_euler_eq_nonhomogeneous_variation_of_parameters",
  557. 'func': f(x),
  558. 'examples':{
  559. 'euler_var_01': {
  560. 'eq': Eq(x**2*Derivative(f(x), x, x) - 2*x*Derivative(f(x), x) + 2*f(x), x**4),
  561. 'sol': [Eq(f(x), x*(C1 + C2*x + x**3/6))]
  562. },
  563. 'euler_var_02': {
  564. 'eq': Eq(3*x**2*diff(f(x), x, x) + 6*x*diff(f(x), x) - 6*f(x), x**3*exp(x)),
  565. 'sol': [Eq(f(x), C1/x**2 + C2*x + x*exp(x)/3 - 4*exp(x)/3 + 8*exp(x)/(3*x) - 8*exp(x)/(3*x**2))]
  566. },
  567. 'euler_var_03': {
  568. 'eq': Eq(x**2*Derivative(f(x), x, x) - 2*x*Derivative(f(x), x) + 2*f(x), x**4*exp(x)),
  569. 'sol': [Eq(f(x), x*(C1 + C2*x + x*exp(x) - 2*exp(x)))]
  570. },
  571. 'euler_var_04': {
  572. 'eq': x**2*Derivative(f(x), x, x) - 2*x*Derivative(f(x), x) + 2*f(x) - log(x),
  573. 'sol': [Eq(f(x), C1*x + C2*x**2 + log(x)/2 + Rational(3, 4))]
  574. },
  575. 'euler_var_05': {
  576. 'eq': -exp(x) + (x*Derivative(f(x), (x, 2)) + Derivative(f(x), x))/x,
  577. 'sol': [Eq(f(x), C1 + C2*log(x) + exp(x) - Ei(x))]
  578. },
  579. 'euler_var_06': {
  580. 'eq': x**2 * f(x).diff(x, 2) + x * f(x).diff(x) + 4 * f(x) - 1/x,
  581. 'sol': [Eq(f(x), C1*sin(2*log(x)) + C2*cos(2*log(x)) + 1/(5*x))]
  582. },
  583. }
  584. }
  585. @_add_example_keys
  586. def _get_examples_ode_sol_bernoulli():
  587. # Type: Bernoulli, f'(x) + p(x)*f(x) == q(x)*f(x)**n
  588. return {
  589. 'hint': "Bernoulli",
  590. 'func': f(x),
  591. 'examples':{
  592. 'bernoulli_01': {
  593. 'eq': Eq(x*f(x).diff(x) + f(x) - f(x)**2, 0),
  594. 'sol': [Eq(f(x), 1/(C1*x + 1))],
  595. 'XFAIL': ['separable_reduced']
  596. },
  597. 'bernoulli_02': {
  598. 'eq': f(x).diff(x) - y*f(x),
  599. 'sol': [Eq(f(x), C1*exp(x*y))]
  600. },
  601. 'bernoulli_03': {
  602. 'eq': f(x)*f(x).diff(x) - 1,
  603. 'sol': [Eq(f(x), -sqrt(C1 + 2*x)), Eq(f(x), sqrt(C1 + 2*x))]
  604. },
  605. }
  606. }
  607. @_add_example_keys
  608. def _get_examples_ode_sol_riccati():
  609. # Type: Riccati special alpha = -2, a*dy/dx + b*y**2 + c*y/x +d/x**2
  610. return {
  611. 'hint': "Riccati_special_minus2",
  612. 'func': f(x),
  613. 'examples':{
  614. 'riccati_01': {
  615. 'eq': 2*f(x).diff(x) + f(x)**2 - f(x)/x + 3*x**(-2),
  616. 'sol': [Eq(f(x), (-sqrt(3)*tan(C1 + sqrt(3)*log(x)/4) + 3)/(2*x))],
  617. },
  618. },
  619. }
  620. @_add_example_keys
  621. def _get_examples_ode_sol_1st_rational_riccati():
  622. # Type: 1st Order Rational Riccati, dy/dx = a + b*y + c*y**2,
  623. # a, b, c are rational functions of x
  624. return {
  625. 'hint': "1st_rational_riccati",
  626. 'func': f(x),
  627. 'examples':{
  628. # a(x) is a constant
  629. "rational_riccati_01": {
  630. "eq": Eq(f(x).diff(x) + f(x)**2 - 2, 0),
  631. "sol": [Eq(f(x), sqrt(2)*(-C1 - exp(2*sqrt(2)*x))/(C1 - exp(2*sqrt(2)*x)))]
  632. },
  633. # a(x) is a constant
  634. "rational_riccati_02": {
  635. "eq": f(x)**2 + Derivative(f(x), x) + 4*f(x)/x + 2/x**2,
  636. "sol": [Eq(f(x), (-2*C1 - x)/(x*(C1 + x)))]
  637. },
  638. # a(x) is a constant
  639. "rational_riccati_03": {
  640. "eq": 2*x**2*Derivative(f(x), x) - x*(4*f(x) + Derivative(f(x), x) - 4) + (f(x) - 1)*f(x),
  641. "sol": [Eq(f(x), (C1 + 2*x**2)/(C1 + x))]
  642. },
  643. # Constant coefficients
  644. "rational_riccati_04": {
  645. "eq": f(x).diff(x) - 6 - 5*f(x) - f(x)**2,
  646. "sol": [Eq(f(x), (-2*C1 + 3*exp(x))/(C1 - exp(x)))]
  647. },
  648. # One pole of multiplicity 2
  649. "rational_riccati_05": {
  650. "eq": x**2 - (2*x + 1/x)*f(x) + f(x)**2 + Derivative(f(x), x),
  651. "sol": [Eq(f(x), x*(C1 + x**2 + 1)/(C1 + x**2 - 1))]
  652. },
  653. # One pole of multiplicity 2
  654. "rational_riccati_06": {
  655. "eq": x**4*Derivative(f(x), x) + x**2 - x*(2*f(x)**2 + Derivative(f(x), x)) + f(x),
  656. "sol": [Eq(f(x), x*(C1*x - x + 1)/(C1 + x**2 - 1))]
  657. },
  658. # Multiple poles of multiplicity 2
  659. "rational_riccati_07": {
  660. "eq": -f(x)**2 + Derivative(f(x), x) + (15*x**2 - 20*x + 7)/((x - 1)**2*(2*x \
  661. - 1)**2),
  662. "sol": [Eq(f(x), (9*C1*x - 6*C1 - 15*x**5 + 60*x**4 - 94*x**3 + 72*x**2 - \
  663. 33*x + 8)/(6*C1*x**2 - 9*C1*x + 3*C1 + 6*x**6 - 29*x**5 + 57*x**4 - \
  664. 58*x**3 + 28*x**2 - 3*x - 1))]
  665. },
  666. # Imaginary poles
  667. "rational_riccati_08": {
  668. "eq": Derivative(f(x), x) + (3*x**2 + 1)*f(x)**2/x + (6*x**2 - x + 3)*f(x)/(x*(x \
  669. - 1)) + (3*x**2 - 2*x + 2)/(x*(x - 1)**2),
  670. "sol": [Eq(f(x), (-C1 - x**3 + x**2 - 2*x + 1)/(C1*x - C1 + x**4 - x**3 + x**2 - \
  671. 2*x + 1))],
  672. },
  673. # Imaginary coefficients in equation
  674. "rational_riccati_09": {
  675. "eq": Derivative(f(x), x) - 2*I*(f(x)**2 + 1)/x,
  676. "sol": [Eq(f(x), (-I*C1 + I*x**4 + I)/(C1 + x**4 - 1))]
  677. },
  678. # Regression: linsolve returning empty solution
  679. # Large value of m (> 10)
  680. "rational_riccati_10": {
  681. "eq": Eq(Derivative(f(x), x), x*f(x)/(S(3)/2 - 2*x) + (x/2 - S(1)/3)*f(x)**2/\
  682. (2*x/3 - S(1)/2) - S(5)/4 + (281*x**2 - 1260*x + 756)/(16*x**3 - 12*x**2)),
  683. "sol": [Eq(f(x), (40*C1*x**14 + 28*C1*x**13 + 420*C1*x**12 + 2940*C1*x**11 + \
  684. 18480*C1*x**10 + 103950*C1*x**9 + 519750*C1*x**8 + 2286900*C1*x**7 + \
  685. 8731800*C1*x**6 + 28378350*C1*x**5 + 76403250*C1*x**4 + 163721250*C1*x**3 \
  686. + 261954000*C1*x**2 + 278326125*C1*x + 147349125*C1 + x*exp(2*x) - 9*exp(2*x) \
  687. )/(x*(24*C1*x**13 + 140*C1*x**12 + 840*C1*x**11 + 4620*C1*x**10 + 23100*C1*x**9 \
  688. + 103950*C1*x**8 + 415800*C1*x**7 + 1455300*C1*x**6 + 4365900*C1*x**5 + \
  689. 10914750*C1*x**4 + 21829500*C1*x**3 + 32744250*C1*x**2 + 32744250*C1*x + \
  690. 16372125*C1 - exp(2*x))))]
  691. }
  692. }
  693. }
  694. @_add_example_keys
  695. def _get_examples_ode_sol_1st_linear():
  696. # Type: first order linear form f'(x)+p(x)f(x)=q(x)
  697. return {
  698. 'hint': "1st_linear",
  699. 'func': f(x),
  700. 'examples':{
  701. 'linear_01': {
  702. 'eq': Eq(f(x).diff(x) + x*f(x), x**2),
  703. 'sol': [Eq(f(x), (C1 + x*exp(x**2/2)- sqrt(2)*sqrt(pi)*erfi(sqrt(2)*x/2)/2)*exp(-x**2/2))],
  704. },
  705. },
  706. }
  707. @_add_example_keys
  708. def _get_examples_ode_sol_factorable():
  709. """ some hints are marked as xfail for examples because they missed additional algebraic solution
  710. which could be found by Factorable hint. Fact_01 raise exception for
  711. nth_linear_constant_coeff_undetermined_coefficients"""
  712. y = Dummy('y')
  713. a0,a1,a2,a3,a4 = symbols('a0, a1, a2, a3, a4')
  714. return {
  715. 'hint': "factorable",
  716. 'func': f(x),
  717. 'examples':{
  718. 'fact_01': {
  719. 'eq': f(x) + f(x)*f(x).diff(x),
  720. 'sol': [Eq(f(x), 0), Eq(f(x), C1 - x)],
  721. 'XFAIL': ['separable', '1st_exact', '1st_linear', 'Bernoulli', '1st_homogeneous_coeff_best',
  722. '1st_homogeneous_coeff_subs_indep_div_dep', '1st_homogeneous_coeff_subs_dep_div_indep',
  723. 'lie_group', 'nth_linear_euler_eq_nonhomogeneous_undetermined_coefficients',
  724. 'nth_linear_constant_coeff_variation_of_parameters',
  725. 'nth_linear_euler_eq_nonhomogeneous_variation_of_parameters',
  726. 'nth_linear_constant_coeff_undetermined_coefficients']
  727. },
  728. 'fact_02': {
  729. 'eq': f(x)*(f(x).diff(x)+f(x)*x+2),
  730. 'sol': [Eq(f(x), (C1 - sqrt(2)*sqrt(pi)*erfi(sqrt(2)*x/2))*exp(-x**2/2)), Eq(f(x), 0)],
  731. 'XFAIL': ['Bernoulli', '1st_linear', 'lie_group']
  732. },
  733. 'fact_03': {
  734. 'eq': (f(x).diff(x)+f(x)*x**2)*(f(x).diff(x, 2) + x*f(x)),
  735. 'sol': [Eq(f(x), C1*airyai(-x) + C2*airybi(-x)),Eq(f(x), C1*exp(-x**3/3))]
  736. },
  737. 'fact_04': {
  738. 'eq': (f(x).diff(x)+f(x)*x**2)*(f(x).diff(x, 2) + f(x)),
  739. 'sol': [Eq(f(x), C1*exp(-x**3/3)), Eq(f(x), C1*sin(x) + C2*cos(x))]
  740. },
  741. 'fact_05': {
  742. 'eq': (f(x).diff(x)**2-1)*(f(x).diff(x)**2-4),
  743. 'sol': [Eq(f(x), C1 - x), Eq(f(x), C1 + x), Eq(f(x), C1 + 2*x), Eq(f(x), C1 - 2*x)]
  744. },
  745. 'fact_06': {
  746. 'eq': (f(x).diff(x, 2)-exp(f(x)))*f(x).diff(x),
  747. 'sol': [
  748. Eq(f(x), log(-C1/(cos(sqrt(-C1)*(C2 + x)) + 1))),
  749. Eq(f(x), log(-C1/(cos(sqrt(-C1)*(C2 - x)) + 1))),
  750. Eq(f(x), C1)
  751. ],
  752. 'slow': True,
  753. },
  754. 'fact_07': {
  755. 'eq': (f(x).diff(x)**2-1)*(f(x)*f(x).diff(x)-1),
  756. 'sol': [Eq(f(x), C1 - x), Eq(f(x), -sqrt(C1 + 2*x)),Eq(f(x), sqrt(C1 + 2*x)), Eq(f(x), C1 + x)]
  757. },
  758. 'fact_08': {
  759. 'eq': Derivative(f(x), x)**4 - 2*Derivative(f(x), x)**2 + 1,
  760. 'sol': [Eq(f(x), C1 - x), Eq(f(x), C1 + x)]
  761. },
  762. 'fact_09': {
  763. 'eq': f(x)**2*Derivative(f(x), x)**6 - 2*f(x)**2*Derivative(f(x),
  764. x)**4 + f(x)**2*Derivative(f(x), x)**2 - 2*f(x)*Derivative(f(x),
  765. x)**5 + 4*f(x)*Derivative(f(x), x)**3 - 2*f(x)*Derivative(f(x),
  766. x) + Derivative(f(x), x)**4 - 2*Derivative(f(x), x)**2 + 1,
  767. 'sol': [
  768. Eq(f(x), C1 - x), Eq(f(x), -sqrt(C1 + 2*x)),
  769. Eq(f(x), sqrt(C1 + 2*x)), Eq(f(x), C1 + x)
  770. ]
  771. },
  772. 'fact_10': {
  773. 'eq': x**4*f(x)**2 + 2*x**4*f(x)*Derivative(f(x), (x, 2)) + x**4*Derivative(f(x),
  774. (x, 2))**2 + 2*x**3*f(x)*Derivative(f(x), x) + 2*x**3*Derivative(f(x),
  775. x)*Derivative(f(x), (x, 2)) - 7*x**2*f(x)**2 - 7*x**2*f(x)*Derivative(f(x),
  776. (x, 2)) + x**2*Derivative(f(x), x)**2 - 7*x*f(x)*Derivative(f(x), x) + 12*f(x)**2,
  777. 'sol': [
  778. Eq(f(x), C1*besselj(2, x) + C2*bessely(2, x)),
  779. Eq(f(x), C1*besselj(sqrt(3), x) + C2*bessely(sqrt(3), x))
  780. ],
  781. 'slow': True,
  782. },
  783. 'fact_11': {
  784. 'eq': (f(x).diff(x, 2)-exp(f(x)))*(f(x).diff(x, 2)+exp(f(x))),
  785. 'sol': [
  786. Eq(f(x), log(C1/(cos(C1*sqrt(-1/C1)*(C2 + x)) - 1))),
  787. Eq(f(x), log(C1/(cos(C1*sqrt(-1/C1)*(C2 - x)) - 1))),
  788. Eq(f(x), log(C1/(1 - cos(C1*sqrt(-1/C1)*(C2 + x))))),
  789. Eq(f(x), log(C1/(1 - cos(C1*sqrt(-1/C1)*(C2 - x)))))
  790. ],
  791. 'dsolve_too_slow': True,
  792. },
  793. #Below examples were added for the issue: https://github.com/sympy/sympy/issues/15889
  794. 'fact_12': {
  795. 'eq': exp(f(x).diff(x))-f(x)**2,
  796. 'sol': [Eq(NonElementaryIntegral(1/log(y**2), (y, f(x))), C1 + x)],
  797. 'XFAIL': ['lie_group'] #It shows not implemented error for lie_group.
  798. },
  799. 'fact_13': {
  800. 'eq': f(x).diff(x)**2 - f(x)**3,
  801. 'sol': [Eq(f(x), 4/(C1**2 - 2*C1*x + x**2))],
  802. 'XFAIL': ['lie_group'] #It shows not implemented error for lie_group.
  803. },
  804. 'fact_14': {
  805. 'eq': f(x).diff(x)**2 - f(x),
  806. 'sol': [Eq(f(x), C1**2/4 - C1*x/2 + x**2/4)]
  807. },
  808. 'fact_15': {
  809. 'eq': f(x).diff(x)**2 - f(x)**2,
  810. 'sol': [Eq(f(x), C1*exp(x)), Eq(f(x), C1*exp(-x))]
  811. },
  812. 'fact_16': {
  813. 'eq': f(x).diff(x)**2 - f(x)**3,
  814. 'sol': [Eq(f(x), 4/(C1**2 - 2*C1*x + x**2))],
  815. },
  816. # kamke ode 1.1
  817. 'fact_17': {
  818. 'eq': f(x).diff(x)-(a4*x**4 + a3*x**3 + a2*x**2 + a1*x + a0)**(-1/2),
  819. 'sol': [Eq(f(x), C1 + Integral(1/sqrt(a0 + a1*x + a2*x**2 + a3*x**3 + a4*x**4), x))],
  820. 'slow': True
  821. },
  822. # This is from issue: https://github.com/sympy/sympy/issues/9446
  823. 'fact_18':{
  824. 'eq': Eq(f(2 * x), sin(Derivative(f(x)))),
  825. 'sol': [Eq(f(x), C1 + Integral(pi - asin(f(2*x)), x)), Eq(f(x), C1 + Integral(asin(f(2*x)), x))],
  826. 'checkodesol_XFAIL':True
  827. },
  828. # This is from issue: https://github.com/sympy/sympy/issues/7093
  829. 'fact_19': {
  830. 'eq': Derivative(f(x), x)**2 - x**3,
  831. 'sol': [Eq(f(x), C1 - 2*x**Rational(5,2)/5), Eq(f(x), C1 + 2*x**Rational(5,2)/5)],
  832. },
  833. 'fact_20': {
  834. 'eq': x*f(x).diff(x, 2) - x*f(x),
  835. 'sol': [Eq(f(x), C1*exp(-x) + C2*exp(x))],
  836. },
  837. }
  838. }
  839. @_add_example_keys
  840. def _get_examples_ode_sol_almost_linear():
  841. from sympy.functions.special.error_functions import Ei
  842. A = Symbol('A', positive=True)
  843. f = Function('f')
  844. d = f(x).diff(x)
  845. return {
  846. 'hint': "almost_linear",
  847. 'func': f(x),
  848. 'examples':{
  849. 'almost_lin_01': {
  850. 'eq': x**2*f(x)**2*d + f(x)**3 + 1,
  851. 'sol': [Eq(f(x), (C1*exp(3/x) - 1)**Rational(1, 3)),
  852. Eq(f(x), (-1 - sqrt(3)*I)*(C1*exp(3/x) - 1)**Rational(1, 3)/2),
  853. Eq(f(x), (-1 + sqrt(3)*I)*(C1*exp(3/x) - 1)**Rational(1, 3)/2)],
  854. },
  855. 'almost_lin_02': {
  856. 'eq': x*f(x)*d + 2*x*f(x)**2 + 1,
  857. 'sol': [Eq(f(x), -sqrt((C1 - 2*Ei(4*x))*exp(-4*x))), Eq(f(x), sqrt((C1 - 2*Ei(4*x))*exp(-4*x)))]
  858. },
  859. 'almost_lin_03': {
  860. 'eq': x*d + x*f(x) + 1,
  861. 'sol': [Eq(f(x), (C1 - Ei(x))*exp(-x))]
  862. },
  863. 'almost_lin_04': {
  864. 'eq': x*exp(f(x))*d + exp(f(x)) + 3*x,
  865. 'sol': [Eq(f(x), log(C1/x - x*Rational(3, 2)))],
  866. },
  867. 'almost_lin_05': {
  868. 'eq': x + A*(x + diff(f(x), x) + f(x)) + diff(f(x), x) + f(x) + 2,
  869. 'sol': [Eq(f(x), (C1 + Piecewise(
  870. (x, Eq(A + 1, 0)), ((-A*x + A - x - 1)*exp(x)/(A + 1), True)))*exp(-x))],
  871. },
  872. }
  873. }
  874. @_add_example_keys
  875. def _get_examples_ode_sol_liouville():
  876. n = Symbol('n')
  877. _y = Dummy('y')
  878. return {
  879. 'hint': "Liouville",
  880. 'func': f(x),
  881. 'examples':{
  882. 'liouville_01': {
  883. 'eq': diff(f(x), x)/x + diff(f(x), x, x)/2 - diff(f(x), x)**2/2,
  884. 'sol': [Eq(f(x), log(x/(C1 + C2*x)))],
  885. },
  886. 'liouville_02': {
  887. 'eq': diff(x*exp(-f(x)), x, x),
  888. 'sol': [Eq(f(x), log(x/(C1 + C2*x)))]
  889. },
  890. 'liouville_03': {
  891. 'eq': ((diff(f(x), x)/x + diff(f(x), x, x)/2 - diff(f(x), x)**2/2)*exp(-f(x))/exp(f(x))).expand(),
  892. 'sol': [Eq(f(x), log(x/(C1 + C2*x)))]
  893. },
  894. 'liouville_04': {
  895. 'eq': diff(f(x), x, x) + 1/f(x)*(diff(f(x), x))**2 + 1/x*diff(f(x), x),
  896. 'sol': [Eq(f(x), -sqrt(C1 + C2*log(x))), Eq(f(x), sqrt(C1 + C2*log(x)))],
  897. },
  898. 'liouville_05': {
  899. 'eq': x*diff(f(x), x, x) + x/f(x)*diff(f(x), x)**2 + x*diff(f(x), x),
  900. 'sol': [Eq(f(x), -sqrt(C1 + C2*exp(-x))), Eq(f(x), sqrt(C1 + C2*exp(-x)))],
  901. },
  902. 'liouville_06': {
  903. 'eq': Eq((x*exp(f(x))).diff(x, x), 0),
  904. 'sol': [Eq(f(x), log(C1 + C2/x))],
  905. },
  906. 'liouville_07': {
  907. 'eq': (diff(f(x), x)/x + diff(f(x), x, x)/2 - diff(f(x), x)**2/2)*exp(-f(x))/exp(f(x)),
  908. 'sol': [Eq(f(x), log(x/(C1 + C2*x)))],
  909. },
  910. 'liouville_08': {
  911. 'eq': x**2*diff(f(x),x) + (n*f(x) + f(x)**2)*diff(f(x),x)**2 + diff(f(x), (x, 2)),
  912. 'sol': [Eq(C1 + C2*lowergamma(Rational(1,3), x**3/3) + NonElementaryIntegral(exp(_y**3/3)*exp(_y**2*n/2), (_y, f(x))), 0)],
  913. },
  914. }
  915. }
  916. @_add_example_keys
  917. def _get_examples_ode_sol_nth_algebraic():
  918. M, m, r, t = symbols('M m r t')
  919. phi = Function('phi')
  920. k = Symbol('k')
  921. # This one needs a substitution f' = g.
  922. # 'algeb_12': {
  923. # 'eq': -exp(x) + (x*Derivative(f(x), (x, 2)) + Derivative(f(x), x))/x,
  924. # 'sol': [Eq(f(x), C1 + C2*log(x) + exp(x) - Ei(x))],
  925. # },
  926. return {
  927. 'hint': "nth_algebraic",
  928. 'func': f(x),
  929. 'examples':{
  930. 'algeb_01': {
  931. 'eq': f(x) * f(x).diff(x) * f(x).diff(x, x) * (f(x) - 1) * (f(x).diff(x) - x),
  932. 'sol': [Eq(f(x), C1 + x**2/2), Eq(f(x), C1 + C2*x)]
  933. },
  934. 'algeb_02': {
  935. 'eq': f(x) * f(x).diff(x) * f(x).diff(x, x) * (f(x) - 1),
  936. 'sol': [Eq(f(x), C1 + C2*x)]
  937. },
  938. 'algeb_03': {
  939. 'eq': f(x) * f(x).diff(x) * f(x).diff(x, x),
  940. 'sol': [Eq(f(x), C1 + C2*x)]
  941. },
  942. 'algeb_04': {
  943. 'eq': Eq(-M * phi(t).diff(t),
  944. Rational(3, 2) * m * r**2 * phi(t).diff(t) * phi(t).diff(t,t)),
  945. 'sol': [Eq(phi(t), C1), Eq(phi(t), C1 + C2*t - M*t**2/(3*m*r**2))],
  946. 'func': phi(t)
  947. },
  948. 'algeb_05': {
  949. 'eq': (1 - sin(f(x))) * f(x).diff(x),
  950. 'sol': [Eq(f(x), C1)],
  951. 'XFAIL': ['separable'] #It raised exception.
  952. },
  953. 'algeb_06': {
  954. 'eq': (diff(f(x)) - x)*(diff(f(x)) + x),
  955. 'sol': [Eq(f(x), C1 - x**2/2), Eq(f(x), C1 + x**2/2)]
  956. },
  957. 'algeb_07': {
  958. 'eq': Eq(Derivative(f(x), x), Derivative(g(x), x)),
  959. 'sol': [Eq(f(x), C1 + g(x))],
  960. },
  961. 'algeb_08': {
  962. 'eq': f(x).diff(x) - C1, #this example is from issue 15999
  963. 'sol': [Eq(f(x), C1*x + C2)],
  964. },
  965. 'algeb_09': {
  966. 'eq': f(x)*f(x).diff(x),
  967. 'sol': [Eq(f(x), C1)],
  968. },
  969. 'algeb_10': {
  970. 'eq': (diff(f(x)) - x)*(diff(f(x)) + x),
  971. 'sol': [Eq(f(x), C1 - x**2/2), Eq(f(x), C1 + x**2/2)],
  972. },
  973. 'algeb_11': {
  974. 'eq': f(x) + f(x)*f(x).diff(x),
  975. 'sol': [Eq(f(x), 0), Eq(f(x), C1 - x)],
  976. 'XFAIL': ['separable', '1st_exact', '1st_linear', 'Bernoulli', '1st_homogeneous_coeff_best',
  977. '1st_homogeneous_coeff_subs_indep_div_dep', '1st_homogeneous_coeff_subs_dep_div_indep',
  978. 'lie_group', 'nth_linear_constant_coeff_undetermined_coefficients',
  979. 'nth_linear_euler_eq_nonhomogeneous_undetermined_coefficients',
  980. 'nth_linear_constant_coeff_variation_of_parameters',
  981. 'nth_linear_euler_eq_nonhomogeneous_variation_of_parameters']
  982. #nth_linear_constant_coeff_undetermined_coefficients raises exception rest all of them misses a solution.
  983. },
  984. 'algeb_12': {
  985. 'eq': Derivative(x*f(x), x, x, x),
  986. 'sol': [Eq(f(x), (C1 + C2*x + C3*x**2) / x)],
  987. 'XFAIL': ['nth_algebraic'] # It passes only when prep=False is set in dsolve.
  988. },
  989. 'algeb_13': {
  990. 'eq': Eq(Derivative(x*Derivative(f(x), x), x)/x, exp(x)),
  991. 'sol': [Eq(f(x), C1 + C2*log(x) + exp(x) - Ei(x))],
  992. 'XFAIL': ['nth_algebraic'] # It passes only when prep=False is set in dsolve.
  993. },
  994. # These are simple tests from the old ode module example 14-18
  995. 'algeb_14': {
  996. 'eq': Eq(f(x).diff(x), 0),
  997. 'sol': [Eq(f(x), C1)],
  998. },
  999. 'algeb_15': {
  1000. 'eq': Eq(3*f(x).diff(x) - 5, 0),
  1001. 'sol': [Eq(f(x), C1 + x*Rational(5, 3))],
  1002. },
  1003. 'algeb_16': {
  1004. 'eq': Eq(3*f(x).diff(x), 5),
  1005. 'sol': [Eq(f(x), C1 + x*Rational(5, 3))],
  1006. },
  1007. # Type: 2nd order, constant coefficients (two complex roots)
  1008. 'algeb_17': {
  1009. 'eq': Eq(3*f(x).diff(x) - 1, 0),
  1010. 'sol': [Eq(f(x), C1 + x/3)],
  1011. },
  1012. 'algeb_18': {
  1013. 'eq': Eq(x*f(x).diff(x) - 1, 0),
  1014. 'sol': [Eq(f(x), C1 + log(x))],
  1015. },
  1016. # https://github.com/sympy/sympy/issues/6989
  1017. 'algeb_19': {
  1018. 'eq': f(x).diff(x) - x*exp(-k*x),
  1019. 'sol': [Eq(f(x), C1 + Piecewise(((-k*x - 1)*exp(-k*x)/k**2, Ne(k**2, 0)),(x**2/2, True)))],
  1020. },
  1021. 'algeb_20': {
  1022. 'eq': -f(x).diff(x) + x*exp(-k*x),
  1023. 'sol': [Eq(f(x), C1 + Piecewise(((-k*x - 1)*exp(-k*x)/k**2, Ne(k**2, 0)),(x**2/2, True)))],
  1024. },
  1025. # https://github.com/sympy/sympy/issues/10867
  1026. 'algeb_21': {
  1027. 'eq': Eq(g(x).diff(x).diff(x), (x-2)**2 + (x-3)**3),
  1028. 'sol': [Eq(g(x), C1 + C2*x + x**5/20 - 2*x**4/3 + 23*x**3/6 - 23*x**2/2)],
  1029. 'func': g(x),
  1030. },
  1031. # https://github.com/sympy/sympy/issues/13691
  1032. 'algeb_22': {
  1033. 'eq': f(x).diff(x) - C1*g(x).diff(x),
  1034. 'sol': [Eq(f(x), C2 + C1*g(x))],
  1035. 'func': f(x),
  1036. },
  1037. # https://github.com/sympy/sympy/issues/4838
  1038. 'algeb_23': {
  1039. 'eq': f(x).diff(x) - 3*C1 - 3*x**2,
  1040. 'sol': [Eq(f(x), C2 + 3*C1*x + x**3)],
  1041. },
  1042. }
  1043. }
  1044. @_add_example_keys
  1045. def _get_examples_ode_sol_nth_order_reducible():
  1046. return {
  1047. 'hint': "nth_order_reducible",
  1048. 'func': f(x),
  1049. 'examples':{
  1050. 'reducible_01': {
  1051. 'eq': Eq(x*Derivative(f(x), x)**2 + Derivative(f(x), x, 2), 0),
  1052. 'sol': [Eq(f(x),C1 - sqrt(-1/C2)*log(-C2*sqrt(-1/C2) + x) +
  1053. sqrt(-1/C2)*log(C2*sqrt(-1/C2) + x))],
  1054. 'slow': True,
  1055. },
  1056. 'reducible_02': {
  1057. 'eq': -exp(x) + (x*Derivative(f(x), (x, 2)) + Derivative(f(x), x))/x,
  1058. 'sol': [Eq(f(x), C1 + C2*log(x) + exp(x) - Ei(x))],
  1059. 'slow': True,
  1060. },
  1061. 'reducible_03': {
  1062. 'eq': Eq(sqrt(2) * f(x).diff(x,x,x) + f(x).diff(x), 0),
  1063. 'sol': [Eq(f(x), C1 + C2*sin(2**Rational(3, 4)*x/2) + C3*cos(2**Rational(3, 4)*x/2))],
  1064. 'slow': True,
  1065. },
  1066. 'reducible_04': {
  1067. 'eq': f(x).diff(x, 2) + 2*f(x).diff(x),
  1068. 'sol': [Eq(f(x), C1 + C2*exp(-2*x))],
  1069. },
  1070. 'reducible_05': {
  1071. 'eq': f(x).diff(x, 3) + f(x).diff(x, 2) - 6*f(x).diff(x),
  1072. 'sol': [Eq(f(x), C1 + C2*exp(-3*x) + C3*exp(2*x))],
  1073. 'slow': True,
  1074. },
  1075. 'reducible_06': {
  1076. 'eq': f(x).diff(x, 4) - f(x).diff(x, 3) - 4*f(x).diff(x, 2) + \
  1077. 4*f(x).diff(x),
  1078. 'sol': [Eq(f(x), C1 + C2*exp(-2*x) + C3*exp(x) + C4*exp(2*x))],
  1079. 'slow': True,
  1080. },
  1081. 'reducible_07': {
  1082. 'eq': f(x).diff(x, 4) + 3*f(x).diff(x, 3),
  1083. 'sol': [Eq(f(x), C1 + C2*x + C3*x**2 + C4*exp(-3*x))],
  1084. 'slow': True,
  1085. },
  1086. 'reducible_08': {
  1087. 'eq': f(x).diff(x, 4) - 2*f(x).diff(x, 2),
  1088. 'sol': [Eq(f(x), C1 + C2*x + C3*exp(-sqrt(2)*x) + C4*exp(sqrt(2)*x))],
  1089. 'slow': True,
  1090. },
  1091. 'reducible_09': {
  1092. 'eq': f(x).diff(x, 4) + 4*f(x).diff(x, 2),
  1093. 'sol': [Eq(f(x), C1 + C2*x + C3*sin(2*x) + C4*cos(2*x))],
  1094. 'slow': True,
  1095. },
  1096. 'reducible_10': {
  1097. 'eq': f(x).diff(x, 5) + 2*f(x).diff(x, 3) + f(x).diff(x),
  1098. 'sol': [Eq(f(x), C1 + C2*x*sin(x) + C2*cos(x) - C3*x*cos(x) + C3*sin(x) + C4*sin(x) + C5*cos(x))],
  1099. 'slow': True,
  1100. },
  1101. 'reducible_11': {
  1102. 'eq': f(x).diff(x, 2) - f(x).diff(x)**3,
  1103. 'sol': [Eq(f(x), C1 - sqrt(2)*sqrt(-1/(C2 + x))*(C2 + x)),
  1104. Eq(f(x), C1 + sqrt(2)*sqrt(-1/(C2 + x))*(C2 + x))],
  1105. 'slow': True,
  1106. },
  1107. # Needs to be a way to know how to combine derivatives in the expression
  1108. 'reducible_12': {
  1109. 'eq': Derivative(x*f(x), x, x, x) + Derivative(f(x), x, x, x),
  1110. 'sol': [Eq(f(x), C1 + C3/Mul(2, (x**2 + 2*x + 1), evaluate=False) +
  1111. x*(C2 + C3/Mul(2, (x**2 + 2*x + 1), evaluate=False)))], # 2-arg Mul!
  1112. 'slow': True,
  1113. },
  1114. }
  1115. }
  1116. @_add_example_keys
  1117. def _get_examples_ode_sol_nth_linear_undetermined_coefficients():
  1118. # examples 3-27 below are from Ordinary Differential Equations,
  1119. # Tenenbaum and Pollard, pg. 231
  1120. g = exp(-x)
  1121. f2 = f(x).diff(x, 2)
  1122. c = 3*f(x).diff(x, 3) + 5*f2 + f(x).diff(x) - f(x) - x
  1123. t = symbols("t")
  1124. u = symbols("u",cls=Function)
  1125. R, L, C, E_0, alpha = symbols("R L C E_0 alpha",positive=True)
  1126. omega = Symbol('omega')
  1127. return {
  1128. 'hint': "nth_linear_constant_coeff_undetermined_coefficients",
  1129. 'func': f(x),
  1130. 'examples':{
  1131. 'undet_01': {
  1132. 'eq': c - x*g,
  1133. 'sol': [Eq(f(x), C3*exp(x/3) - x + (C1 + x*(C2 - x**2/24 - 3*x/32))*exp(-x) - 1)],
  1134. 'slow': True,
  1135. },
  1136. 'undet_02': {
  1137. 'eq': c - g,
  1138. 'sol': [Eq(f(x), C3*exp(x/3) - x + (C1 + x*(C2 - x/8))*exp(-x) - 1)],
  1139. 'slow': True,
  1140. },
  1141. 'undet_03': {
  1142. 'eq': f2 + 3*f(x).diff(x) + 2*f(x) - 4,
  1143. 'sol': [Eq(f(x), C1*exp(-2*x) + C2*exp(-x) + 2)],
  1144. 'slow': True,
  1145. },
  1146. 'undet_04': {
  1147. 'eq': f2 + 3*f(x).diff(x) + 2*f(x) - 12*exp(x),
  1148. 'sol': [Eq(f(x), C1*exp(-2*x) + C2*exp(-x) + 2*exp(x))],
  1149. 'slow': True,
  1150. },
  1151. 'undet_05': {
  1152. 'eq': f2 + 3*f(x).diff(x) + 2*f(x) - exp(I*x),
  1153. 'sol': [Eq(f(x), (S(3)/10 + I/10)*(C1*exp(-2*x) + C2*exp(-x) - I*exp(I*x)))],
  1154. 'slow': True,
  1155. },
  1156. 'undet_06': {
  1157. 'eq': f2 + 3*f(x).diff(x) + 2*f(x) - sin(x),
  1158. 'sol': [Eq(f(x), C1*exp(-2*x) + C2*exp(-x) + sin(x)/10 - 3*cos(x)/10)],
  1159. 'slow': True,
  1160. },
  1161. 'undet_07': {
  1162. 'eq': f2 + 3*f(x).diff(x) + 2*f(x) - cos(x),
  1163. 'sol': [Eq(f(x), C1*exp(-2*x) + C2*exp(-x) + 3*sin(x)/10 + cos(x)/10)],
  1164. 'slow': True,
  1165. },
  1166. 'undet_08': {
  1167. 'eq': f2 + 3*f(x).diff(x) + 2*f(x) - (8 + 6*exp(x) + 2*sin(x)),
  1168. 'sol': [Eq(f(x), C1*exp(-2*x) + C2*exp(-x) + exp(x) + sin(x)/5 - 3*cos(x)/5 + 4)],
  1169. 'slow': True,
  1170. },
  1171. 'undet_09': {
  1172. 'eq': f2 + f(x).diff(x) + f(x) - x**2,
  1173. 'sol': [Eq(f(x), -2*x + x**2 + (C1*sin(x*sqrt(3)/2) + C2*cos(x*sqrt(3)/2))*exp(-x/2))],
  1174. 'slow': True,
  1175. },
  1176. 'undet_10': {
  1177. 'eq': f2 - 2*f(x).diff(x) - 8*f(x) - 9*x*exp(x) - 10*exp(-x),
  1178. 'sol': [Eq(f(x), -x*exp(x) - 2*exp(-x) + C1*exp(-2*x) + C2*exp(4*x))],
  1179. 'slow': True,
  1180. },
  1181. 'undet_11': {
  1182. 'eq': f2 - 3*f(x).diff(x) - 2*exp(2*x)*sin(x),
  1183. 'sol': [Eq(f(x), C1 + C2*exp(3*x) - 3*exp(2*x)*sin(x)/5 - exp(2*x)*cos(x)/5)],
  1184. 'slow': True,
  1185. },
  1186. 'undet_12': {
  1187. 'eq': f(x).diff(x, 4) - 2*f2 + f(x) - x + sin(x),
  1188. 'sol': [Eq(f(x), x - sin(x)/4 + (C1 + C2*x)*exp(-x) + (C3 + C4*x)*exp(x))],
  1189. 'slow': True,
  1190. },
  1191. 'undet_13': {
  1192. 'eq': f2 + f(x).diff(x) - x**2 - 2*x,
  1193. 'sol': [Eq(f(x), C1 + x**3/3 + C2*exp(-x))],
  1194. 'slow': True,
  1195. },
  1196. 'undet_14': {
  1197. 'eq': f2 + f(x).diff(x) - x - sin(2*x),
  1198. 'sol': [Eq(f(x), C1 - x - sin(2*x)/5 - cos(2*x)/10 + x**2/2 + C2*exp(-x))],
  1199. 'slow': True,
  1200. },
  1201. 'undet_15': {
  1202. 'eq': f2 + f(x) - 4*x*sin(x),
  1203. 'sol': [Eq(f(x), (C1 - x**2)*cos(x) + (C2 + x)*sin(x))],
  1204. 'slow': True,
  1205. },
  1206. 'undet_16': {
  1207. 'eq': f2 + 4*f(x) - x*sin(2*x),
  1208. 'sol': [Eq(f(x), (C1 - x**2/8)*cos(2*x) + (C2 + x/16)*sin(2*x))],
  1209. 'slow': True,
  1210. },
  1211. 'undet_17': {
  1212. 'eq': f2 + 2*f(x).diff(x) + f(x) - x**2*exp(-x),
  1213. 'sol': [Eq(f(x), (C1 + x*(C2 + x**3/12))*exp(-x))],
  1214. 'slow': True,
  1215. },
  1216. 'undet_18': {
  1217. 'eq': f(x).diff(x, 3) + 3*f2 + 3*f(x).diff(x) + f(x) - 2*exp(-x) + \
  1218. x**2*exp(-x),
  1219. 'sol': [Eq(f(x), (C1 + x*(C2 + x*(C3 - x**3/60 + x/3)))*exp(-x))],
  1220. 'slow': True,
  1221. },
  1222. 'undet_19': {
  1223. 'eq': f2 + 3*f(x).diff(x) + 2*f(x) - exp(-2*x) - x**2,
  1224. 'sol': [Eq(f(x), C2*exp(-x) + x**2/2 - x*Rational(3,2) + (C1 - x)*exp(-2*x) + Rational(7,4))],
  1225. 'slow': True,
  1226. },
  1227. 'undet_20': {
  1228. 'eq': f2 - 3*f(x).diff(x) + 2*f(x) - x*exp(-x),
  1229. 'sol': [Eq(f(x), C1*exp(x) + C2*exp(2*x) + (6*x + 5)*exp(-x)/36)],
  1230. 'slow': True,
  1231. },
  1232. 'undet_21': {
  1233. 'eq': f2 + f(x).diff(x) - 6*f(x) - x - exp(2*x),
  1234. 'sol': [Eq(f(x), Rational(-1, 36) - x/6 + C2*exp(-3*x) + (C1 + x/5)*exp(2*x))],
  1235. 'slow': True,
  1236. },
  1237. 'undet_22': {
  1238. 'eq': f2 + f(x) - sin(x) - exp(-x),
  1239. 'sol': [Eq(f(x), C2*sin(x) + (C1 - x/2)*cos(x) + exp(-x)/2)],
  1240. 'slow': True,
  1241. },
  1242. 'undet_23': {
  1243. 'eq': f(x).diff(x, 3) - 3*f2 + 3*f(x).diff(x) - f(x) - exp(x),
  1244. 'sol': [Eq(f(x), (C1 + x*(C2 + x*(C3 + x/6)))*exp(x))],
  1245. 'slow': True,
  1246. },
  1247. 'undet_24': {
  1248. 'eq': f2 + f(x) - S.Half - cos(2*x)/2,
  1249. 'sol': [Eq(f(x), S.Half - cos(2*x)/6 + C1*sin(x) + C2*cos(x))],
  1250. 'slow': True,
  1251. },
  1252. 'undet_25': {
  1253. 'eq': f(x).diff(x, 3) - f(x).diff(x) - exp(2*x)*(S.Half - cos(2*x)/2),
  1254. 'sol': [Eq(f(x), C1 + C2*exp(-x) + C3*exp(x) + (-21*sin(2*x) + 27*cos(2*x) + 130)*exp(2*x)/1560)],
  1255. 'slow': True,
  1256. },
  1257. #Note: 'undet_26' is referred in 'undet_37'
  1258. 'undet_26': {
  1259. 'eq': (f(x).diff(x, 5) + 2*f(x).diff(x, 3) + f(x).diff(x) - 2*x -
  1260. sin(x) - cos(x)),
  1261. 'sol': [Eq(f(x), C1 + x**2 + (C2 + x*(C3 - x/8))*sin(x) + (C4 + x*(C5 + x/8))*cos(x))],
  1262. 'slow': True,
  1263. },
  1264. 'undet_27': {
  1265. 'eq': f2 + f(x) - cos(x)/2 + cos(3*x)/2,
  1266. 'sol': [Eq(f(x), cos(3*x)/16 + C2*cos(x) + (C1 + x/4)*sin(x))],
  1267. 'slow': True,
  1268. },
  1269. 'undet_28': {
  1270. 'eq': f(x).diff(x) - 1,
  1271. 'sol': [Eq(f(x), C1 + x)],
  1272. 'slow': True,
  1273. },
  1274. # https://github.com/sympy/sympy/issues/19358
  1275. 'undet_29': {
  1276. 'eq': f2 + f(x).diff(x) + exp(x-C1),
  1277. 'sol': [Eq(f(x), C2 + C3*exp(-x) - exp(-C1 + x)/2)],
  1278. 'slow': True,
  1279. },
  1280. # https://github.com/sympy/sympy/issues/18408
  1281. 'undet_30': {
  1282. 'eq': f(x).diff(x, 3) - f(x).diff(x) - sinh(x),
  1283. 'sol': [Eq(f(x), C1 + C2*exp(-x) + C3*exp(x) + x*sinh(x)/2)],
  1284. },
  1285. 'undet_31': {
  1286. 'eq': f(x).diff(x, 2) - 49*f(x) - sinh(3*x),
  1287. 'sol': [Eq(f(x), C1*exp(-7*x) + C2*exp(7*x) - sinh(3*x)/40)],
  1288. },
  1289. 'undet_32': {
  1290. 'eq': f(x).diff(x, 3) - f(x).diff(x) - sinh(x) - exp(x),
  1291. 'sol': [Eq(f(x), C1 + C3*exp(-x) + x*sinh(x)/2 + (C2 + x/2)*exp(x))],
  1292. },
  1293. # https://github.com/sympy/sympy/issues/5096
  1294. 'undet_33': {
  1295. 'eq': f(x).diff(x, x) + f(x) - x*sin(x - 2),
  1296. 'sol': [Eq(f(x), C1*sin(x) + C2*cos(x) - x**2*cos(x - 2)/4 + x*sin(x - 2)/4)],
  1297. },
  1298. 'undet_34': {
  1299. 'eq': f(x).diff(x, 2) + f(x) - x**4*sin(x-1),
  1300. 'sol': [ Eq(f(x), C1*sin(x) + C2*cos(x) - x**5*cos(x - 1)/10 + x**4*sin(x - 1)/4 + x**3*cos(x - 1)/2 - 3*x**2*sin(x - 1)/4 - 3*x*cos(x - 1)/4)],
  1301. },
  1302. 'undet_35': {
  1303. 'eq': f(x).diff(x, 2) - f(x) - exp(x - 1),
  1304. 'sol': [Eq(f(x), C2*exp(-x) + (C1 + x*exp(-1)/2)*exp(x))],
  1305. },
  1306. 'undet_36': {
  1307. 'eq': f(x).diff(x, 2)+f(x)-(sin(x-2)+1),
  1308. 'sol': [Eq(f(x), C1*sin(x) + C2*cos(x) - x*cos(x - 2)/2 + 1)],
  1309. },
  1310. # Equivalent to example_name 'undet_26'.
  1311. # This previously failed because the algorithm for undetermined coefficients
  1312. # didn't know to multiply exp(I*x) by sufficient x because it is linearly
  1313. # dependent on sin(x) and cos(x).
  1314. 'undet_37': {
  1315. 'eq': f(x).diff(x, 5) + 2*f(x).diff(x, 3) + f(x).diff(x) - 2*x - exp(I*x),
  1316. 'sol': [Eq(f(x), C1 + x**2*(I*exp(I*x)/8 + 1) + (C2 + C3*x)*sin(x) + (C4 + C5*x)*cos(x))],
  1317. },
  1318. # https://github.com/sympy/sympy/issues/12623
  1319. 'undet_38': {
  1320. 'eq': Eq( u(t).diff(t,t) + R /L*u(t).diff(t) + 1/(L*C)*u(t), alpha),
  1321. 'sol': [Eq(u(t), C*L*alpha + C2*exp(-t*(R + sqrt(C*R**2 - 4*L)/sqrt(C))/(2*L))
  1322. + C1*exp(t*(-R + sqrt(C*R**2 - 4*L)/sqrt(C))/(2*L)))],
  1323. 'func': u(t)
  1324. },
  1325. 'undet_39': {
  1326. 'eq': Eq( L*C*u(t).diff(t,t) + R*C*u(t).diff(t) + u(t), E_0*exp(I*omega*t) ),
  1327. 'sol': [Eq(u(t), C2*exp(-t*(R + sqrt(C*R**2 - 4*L)/sqrt(C))/(2*L))
  1328. + C1*exp(t*(-R + sqrt(C*R**2 - 4*L)/sqrt(C))/(2*L))
  1329. - E_0*exp(I*omega*t)/(C*L*omega**2 - I*C*R*omega - 1))],
  1330. 'func': u(t),
  1331. },
  1332. # https://github.com/sympy/sympy/issues/6879
  1333. 'undet_40': {
  1334. 'eq': Eq(Derivative(f(x), x, 2) - 2*Derivative(f(x), x) + f(x), sin(x)),
  1335. 'sol': [Eq(f(x), (C1 + C2*x)*exp(x) + cos(x)/2)],
  1336. },
  1337. }
  1338. }
  1339. @_add_example_keys
  1340. def _get_examples_ode_sol_separable():
  1341. # test_separable1-5 are from Ordinary Differential Equations, Tenenbaum and
  1342. # Pollard, pg. 55
  1343. t,a = symbols('a,t')
  1344. m = 96
  1345. g = 9.8
  1346. k = .2
  1347. f1 = g * m
  1348. v = Function('v')
  1349. return {
  1350. 'hint': "separable",
  1351. 'func': f(x),
  1352. 'examples':{
  1353. 'separable_01': {
  1354. 'eq': f(x).diff(x) - f(x),
  1355. 'sol': [Eq(f(x), C1*exp(x))],
  1356. },
  1357. 'separable_02': {
  1358. 'eq': x*f(x).diff(x) - f(x),
  1359. 'sol': [Eq(f(x), C1*x)],
  1360. },
  1361. 'separable_03': {
  1362. 'eq': f(x).diff(x) + sin(x),
  1363. 'sol': [Eq(f(x), C1 + cos(x))],
  1364. },
  1365. 'separable_04': {
  1366. 'eq': f(x)**2 + 1 - (x**2 + 1)*f(x).diff(x),
  1367. 'sol': [Eq(f(x), tan(C1 + atan(x)))],
  1368. },
  1369. 'separable_05': {
  1370. 'eq': f(x).diff(x)/tan(x) - f(x) - 2,
  1371. 'sol': [Eq(f(x), C1/cos(x) - 2)],
  1372. },
  1373. 'separable_06': {
  1374. 'eq': f(x).diff(x) * (1 - sin(f(x))) - 1,
  1375. 'sol': [Eq(-x + f(x) + cos(f(x)), C1)],
  1376. },
  1377. 'separable_07': {
  1378. 'eq': f(x)*x**2*f(x).diff(x) - f(x)**3 - 2*x**2*f(x).diff(x),
  1379. 'sol': [Eq(f(x), (-x - sqrt(x*(4*C1*x + x - 4)))/(C1*x - 1)/2),
  1380. Eq(f(x), (-x + sqrt(x*(4*C1*x + x - 4)))/(C1*x - 1)/2)],
  1381. 'slow': True,
  1382. },
  1383. 'separable_08': {
  1384. 'eq': f(x)**2 - 1 - (2*f(x) + x*f(x))*f(x).diff(x),
  1385. 'sol': [Eq(f(x), -sqrt(C1*x**2 + 4*C1*x + 4*C1 + 1)),
  1386. Eq(f(x), sqrt(C1*x**2 + 4*C1*x + 4*C1 + 1))],
  1387. 'slow': True,
  1388. },
  1389. 'separable_09': {
  1390. 'eq': x*log(x)*f(x).diff(x) + sqrt(1 + f(x)**2),
  1391. 'sol': [Eq(f(x), sinh(C1 - log(log(x))))], #One more solution is f(x)=I
  1392. 'slow': True,
  1393. 'checkodesol_XFAIL': True,
  1394. },
  1395. 'separable_10': {
  1396. 'eq': exp(x + 1)*tan(f(x)) + cos(f(x))*f(x).diff(x),
  1397. 'sol': [Eq(E*exp(x) + log(cos(f(x)) - 1)/2 - log(cos(f(x)) + 1)/2 + cos(f(x)), C1)],
  1398. 'slow': True,
  1399. },
  1400. 'separable_11': {
  1401. 'eq': (x*cos(f(x)) + x**2*sin(f(x))*f(x).diff(x) - a**2*sin(f(x))*f(x).diff(x)),
  1402. 'sol': [
  1403. Eq(f(x), -acos(C1*sqrt(-a**2 + x**2)) + 2*pi),
  1404. Eq(f(x), acos(C1*sqrt(-a**2 + x**2)))
  1405. ],
  1406. 'slow': True,
  1407. },
  1408. 'separable_12': {
  1409. 'eq': f(x).diff(x) - f(x)*tan(x),
  1410. 'sol': [Eq(f(x), C1/cos(x))],
  1411. },
  1412. 'separable_13': {
  1413. 'eq': (x - 1)*cos(f(x))*f(x).diff(x) - 2*x*sin(f(x)),
  1414. 'sol': [
  1415. Eq(f(x), pi - asin(C1*(x**2 - 2*x + 1)*exp(2*x))),
  1416. Eq(f(x), asin(C1*(x**2 - 2*x + 1)*exp(2*x)))
  1417. ],
  1418. },
  1419. 'separable_14': {
  1420. 'eq': f(x).diff(x) - f(x)*log(f(x))/tan(x),
  1421. 'sol': [Eq(f(x), exp(C1*sin(x)))],
  1422. },
  1423. 'separable_15': {
  1424. 'eq': x*f(x).diff(x) + (1 + f(x)**2)*atan(f(x)),
  1425. 'sol': [Eq(f(x), tan(C1/x))], #Two more solutions are f(x)=0 and f(x)=I
  1426. 'slow': True,
  1427. 'checkodesol_XFAIL': True,
  1428. },
  1429. 'separable_16': {
  1430. 'eq': f(x).diff(x) + x*(f(x) + 1),
  1431. 'sol': [Eq(f(x), -1 + C1*exp(-x**2/2))],
  1432. },
  1433. 'separable_17': {
  1434. 'eq': exp(f(x)**2)*(x**2 + 2*x + 1) + (x*f(x) + f(x))*f(x).diff(x),
  1435. 'sol': [
  1436. Eq(f(x), -sqrt(log(1/(C1 + x**2 + 2*x)))),
  1437. Eq(f(x), sqrt(log(1/(C1 + x**2 + 2*x))))
  1438. ],
  1439. },
  1440. 'separable_18': {
  1441. 'eq': f(x).diff(x) + f(x),
  1442. 'sol': [Eq(f(x), C1*exp(-x))],
  1443. },
  1444. 'separable_19': {
  1445. 'eq': sin(x)*cos(2*f(x)) + cos(x)*sin(2*f(x))*f(x).diff(x),
  1446. 'sol': [Eq(f(x), pi - acos(C1/cos(x)**2)/2), Eq(f(x), acos(C1/cos(x)**2)/2)],
  1447. },
  1448. 'separable_20': {
  1449. 'eq': (1 - x)*f(x).diff(x) - x*(f(x) + 1),
  1450. 'sol': [Eq(f(x), (C1*exp(-x) - x + 1)/(x - 1))],
  1451. },
  1452. 'separable_21': {
  1453. 'eq': f(x)*diff(f(x), x) + x - 3*x*f(x)**2,
  1454. 'sol': [Eq(f(x), -sqrt(3)*sqrt(C1*exp(3*x**2) + 1)/3),
  1455. Eq(f(x), sqrt(3)*sqrt(C1*exp(3*x**2) + 1)/3)],
  1456. },
  1457. 'separable_22': {
  1458. 'eq': f(x).diff(x) - exp(x + f(x)),
  1459. 'sol': [Eq(f(x), log(-1/(C1 + exp(x))))],
  1460. 'XFAIL': ['lie_group'] #It shows 'NoneType' object is not subscriptable for lie_group.
  1461. },
  1462. # https://github.com/sympy/sympy/issues/7081
  1463. 'separable_23': {
  1464. 'eq': x*(f(x).diff(x)) + 1 - f(x)**2,
  1465. 'sol': [Eq(f(x), (-C1 - x**2)/(-C1 + x**2))],
  1466. },
  1467. # https://github.com/sympy/sympy/issues/10379
  1468. 'separable_24': {
  1469. 'eq': f(t).diff(t)-(1-51.05*y*f(t)),
  1470. 'sol': [Eq(f(t), (0.019588638589618023*exp(y*(C1 - 51.049999999999997*t)) + 0.019588638589618023)/y)],
  1471. 'func': f(t),
  1472. },
  1473. # https://github.com/sympy/sympy/issues/15999
  1474. 'separable_25': {
  1475. 'eq': f(x).diff(x) - C1*f(x),
  1476. 'sol': [Eq(f(x), C2*exp(C1*x))],
  1477. },
  1478. 'separable_26': {
  1479. 'eq': f1 - k * (v(t) ** 2) - m * Derivative(v(t)),
  1480. 'sol': [Eq(v(t), -68.585712797928991/tanh(C1 - 0.14288690166235204*t))],
  1481. 'func': v(t),
  1482. 'checkodesol_XFAIL': True,
  1483. },
  1484. #https://github.com/sympy/sympy/issues/22155
  1485. 'separable_27': {
  1486. 'eq': f(x).diff(x) - exp(f(x) - x),
  1487. 'sol': [Eq(f(x), log(-exp(x)/(C1*exp(x) - 1)))],
  1488. }
  1489. }
  1490. }
  1491. @_add_example_keys
  1492. def _get_examples_ode_sol_1st_exact():
  1493. # Type: Exact differential equation, p(x,f) + q(x,f)*f' == 0,
  1494. # where dp/df == dq/dx
  1495. '''
  1496. Example 7 is an exact equation that fails under the exact engine. It is caught
  1497. by first order homogeneous albeit with a much contorted solution. The
  1498. exact engine fails because of a poorly simplified integral of q(0,y)dy,
  1499. where q is the function multiplying f'. The solutions should be
  1500. Eq(sqrt(x**2+f(x)**2)**3+y**3, C1). The equation below is
  1501. equivalent, but it is so complex that checkodesol fails, and takes a long
  1502. time to do so.
  1503. '''
  1504. return {
  1505. 'hint': "1st_exact",
  1506. 'func': f(x),
  1507. 'examples':{
  1508. '1st_exact_01': {
  1509. 'eq': sin(x)*cos(f(x)) + cos(x)*sin(f(x))*f(x).diff(x),
  1510. 'sol': [Eq(f(x), -acos(C1/cos(x)) + 2*pi), Eq(f(x), acos(C1/cos(x)))],
  1511. 'slow': True,
  1512. },
  1513. '1st_exact_02': {
  1514. 'eq': (2*x*f(x) + 1)/f(x) + (f(x) - x)/f(x)**2*f(x).diff(x),
  1515. 'sol': [Eq(f(x), exp(C1 - x**2 + LambertW(-x*exp(-C1 + x**2))))],
  1516. 'XFAIL': ['lie_group'], #It shows dsolve raises an exception: List index out of range for lie_group
  1517. 'slow': True,
  1518. 'checkodesol_XFAIL':True
  1519. },
  1520. '1st_exact_03': {
  1521. 'eq': 2*x + f(x)*cos(x) + (2*f(x) + sin(x) - sin(f(x)))*f(x).diff(x),
  1522. 'sol': [Eq(f(x)*sin(x) + cos(f(x)) + x**2 + f(x)**2, C1)],
  1523. 'XFAIL': ['lie_group'], #It goes into infinite loop for lie_group.
  1524. 'slow': True,
  1525. },
  1526. '1st_exact_04': {
  1527. 'eq': cos(f(x)) - (x*sin(f(x)) - f(x)**2)*f(x).diff(x),
  1528. 'sol': [Eq(x*cos(f(x)) + f(x)**3/3, C1)],
  1529. 'slow': True,
  1530. },
  1531. '1st_exact_05': {
  1532. 'eq': 2*x*f(x) + (x**2 + f(x)**2)*f(x).diff(x),
  1533. 'sol': [Eq(x**2*f(x) + f(x)**3/3, C1)],
  1534. 'slow': True,
  1535. 'simplify_flag':False
  1536. },
  1537. # This was from issue: https://github.com/sympy/sympy/issues/11290
  1538. '1st_exact_06': {
  1539. 'eq': cos(f(x)) - (x*sin(f(x)) - f(x)**2)*f(x).diff(x),
  1540. 'sol': [Eq(x*cos(f(x)) + f(x)**3/3, C1)],
  1541. 'simplify_flag':False
  1542. },
  1543. '1st_exact_07': {
  1544. 'eq': x*sqrt(x**2 + f(x)**2) - (x**2*f(x)/(f(x) - sqrt(x**2 + f(x)**2)))*f(x).diff(x),
  1545. 'sol': [Eq(log(x),
  1546. C1 - 9*sqrt(1 + f(x)**2/x**2)*asinh(f(x)/x)/(-27*f(x)/x +
  1547. 27*sqrt(1 + f(x)**2/x**2)) - 9*sqrt(1 + f(x)**2/x**2)*
  1548. log(1 - sqrt(1 + f(x)**2/x**2)*f(x)/x + 2*f(x)**2/x**2)/
  1549. (-27*f(x)/x + 27*sqrt(1 + f(x)**2/x**2)) +
  1550. 9*asinh(f(x)/x)*f(x)/(x*(-27*f(x)/x + 27*sqrt(1 + f(x)**2/x**2))) +
  1551. 9*f(x)*log(1 - sqrt(1 + f(x)**2/x**2)*f(x)/x + 2*f(x)**2/x**2)/
  1552. (x*(-27*f(x)/x + 27*sqrt(1 + f(x)**2/x**2))))],
  1553. 'slow': True,
  1554. 'dsolve_too_slow':True
  1555. },
  1556. # Type: a(x)f'(x)+b(x)*f(x)+c(x)=0
  1557. '1st_exact_08': {
  1558. 'eq': Eq(x**2*f(x).diff(x) + 3*x*f(x) - sin(x)/x, 0),
  1559. 'sol': [Eq(f(x), (C1 - cos(x))/x**3)],
  1560. },
  1561. # these examples are from test_exact_enhancement
  1562. '1st_exact_09': {
  1563. 'eq': f(x)/x**2 + ((f(x)*x - 1)/x)*f(x).diff(x),
  1564. 'sol': [Eq(f(x), (i*sqrt(C1*x**2 + 1) + 1)/x) for i in (-1, 1)],
  1565. },
  1566. '1st_exact_10': {
  1567. 'eq': (x*f(x) - 1) + f(x).diff(x)*(x**2 - x*f(x)),
  1568. 'sol': [Eq(f(x), x - sqrt(C1 + x**2 - 2*log(x))), Eq(f(x), x + sqrt(C1 + x**2 - 2*log(x)))],
  1569. },
  1570. '1st_exact_11': {
  1571. 'eq': (x + 2)*sin(f(x)) + f(x).diff(x)*x*cos(f(x)),
  1572. 'sol': [Eq(f(x), -asin(C1*exp(-x)/x**2) + pi), Eq(f(x), asin(C1*exp(-x)/x**2))],
  1573. },
  1574. }
  1575. }
  1576. @_add_example_keys
  1577. def _get_examples_ode_sol_nth_linear_var_of_parameters():
  1578. g = exp(-x)
  1579. f2 = f(x).diff(x, 2)
  1580. c = 3*f(x).diff(x, 3) + 5*f2 + f(x).diff(x) - f(x) - x
  1581. return {
  1582. 'hint': "nth_linear_constant_coeff_variation_of_parameters",
  1583. 'func': f(x),
  1584. 'examples':{
  1585. 'var_of_parameters_01': {
  1586. 'eq': c - x*g,
  1587. 'sol': [Eq(f(x), C3*exp(x/3) - x + (C1 + x*(C2 - x**2/24 - 3*x/32))*exp(-x) - 1)],
  1588. 'slow': True,
  1589. },
  1590. 'var_of_parameters_02': {
  1591. 'eq': c - g,
  1592. 'sol': [Eq(f(x), C3*exp(x/3) - x + (C1 + x*(C2 - x/8))*exp(-x) - 1)],
  1593. 'slow': True,
  1594. },
  1595. 'var_of_parameters_03': {
  1596. 'eq': f(x).diff(x) - 1,
  1597. 'sol': [Eq(f(x), C1 + x)],
  1598. 'slow': True,
  1599. },
  1600. 'var_of_parameters_04': {
  1601. 'eq': f2 + 3*f(x).diff(x) + 2*f(x) - 4,
  1602. 'sol': [Eq(f(x), C1*exp(-2*x) + C2*exp(-x) + 2)],
  1603. 'slow': True,
  1604. },
  1605. 'var_of_parameters_05': {
  1606. 'eq': f2 + 3*f(x).diff(x) + 2*f(x) - 12*exp(x),
  1607. 'sol': [Eq(f(x), C1*exp(-2*x) + C2*exp(-x) + 2*exp(x))],
  1608. 'slow': True,
  1609. },
  1610. 'var_of_parameters_06': {
  1611. 'eq': f2 - 2*f(x).diff(x) - 8*f(x) - 9*x*exp(x) - 10*exp(-x),
  1612. 'sol': [Eq(f(x), -x*exp(x) - 2*exp(-x) + C1*exp(-2*x) + C2*exp(4*x))],
  1613. 'slow': True,
  1614. },
  1615. 'var_of_parameters_07': {
  1616. 'eq': f2 + 2*f(x).diff(x) + f(x) - x**2*exp(-x),
  1617. 'sol': [Eq(f(x), (C1 + x*(C2 + x**3/12))*exp(-x))],
  1618. 'slow': True,
  1619. },
  1620. 'var_of_parameters_08': {
  1621. 'eq': f2 - 3*f(x).diff(x) + 2*f(x) - x*exp(-x),
  1622. 'sol': [Eq(f(x), C1*exp(x) + C2*exp(2*x) + (6*x + 5)*exp(-x)/36)],
  1623. 'slow': True,
  1624. },
  1625. 'var_of_parameters_09': {
  1626. 'eq': f(x).diff(x, 3) - 3*f2 + 3*f(x).diff(x) - f(x) - exp(x),
  1627. 'sol': [Eq(f(x), (C1 + x*(C2 + x*(C3 + x/6)))*exp(x))],
  1628. 'slow': True,
  1629. },
  1630. 'var_of_parameters_10': {
  1631. 'eq': f2 + 2*f(x).diff(x) + f(x) - exp(-x)/x,
  1632. 'sol': [Eq(f(x), (C1 + x*(C2 + log(x)))*exp(-x))],
  1633. 'slow': True,
  1634. },
  1635. 'var_of_parameters_11': {
  1636. 'eq': f2 + f(x) - 1/sin(x)*1/cos(x),
  1637. 'sol': [Eq(f(x), (C1 + log(sin(x) - 1)/2 - log(sin(x) + 1)/2
  1638. )*cos(x) + (C2 + log(cos(x) - 1)/2 - log(cos(x) + 1)/2)*sin(x))],
  1639. 'slow': True,
  1640. },
  1641. 'var_of_parameters_12': {
  1642. 'eq': f(x).diff(x, 4) - 1/x,
  1643. 'sol': [Eq(f(x), C1 + C2*x + C3*x**2 + x**3*(C4 + log(x)/6))],
  1644. 'slow': True,
  1645. },
  1646. # These were from issue: https://github.com/sympy/sympy/issues/15996
  1647. 'var_of_parameters_13': {
  1648. 'eq': f(x).diff(x, 5) + 2*f(x).diff(x, 3) + f(x).diff(x) - 2*x - exp(I*x),
  1649. 'sol': [Eq(f(x), C1 + x**2 + (C2 + x*(C3 - x/8 + 3*exp(I*x)/2 + 3*exp(-I*x)/2) + 5*exp(2*I*x)/16 + 2*I*exp(I*x) - 2*I*exp(-I*x))*sin(x) + (C4 + x*(C5 + I*x/8 + 3*I*exp(I*x)/2 - 3*I*exp(-I*x)/2)
  1650. + 5*I*exp(2*I*x)/16 - 2*exp(I*x) - 2*exp(-I*x))*cos(x) - I*exp(I*x))],
  1651. },
  1652. 'var_of_parameters_14': {
  1653. 'eq': f(x).diff(x, 5) + 2*f(x).diff(x, 3) + f(x).diff(x) - exp(I*x),
  1654. 'sol': [Eq(f(x), C1 + (C2 + x*(C3 - x/8) + 5*exp(2*I*x)/16)*sin(x) + (C4 + x*(C5 + I*x/8) + 5*I*exp(2*I*x)/16)*cos(x) - I*exp(I*x))],
  1655. },
  1656. # https://github.com/sympy/sympy/issues/14395
  1657. 'var_of_parameters_15': {
  1658. 'eq': Derivative(f(x), x, x) + 9*f(x) - sec(x),
  1659. 'sol': [Eq(f(x), (C1 - x/3 + sin(2*x)/3)*sin(3*x) + (C2 + log(cos(x))
  1660. - 2*log(cos(x)**2)/3 + 2*cos(x)**2/3)*cos(3*x))],
  1661. 'slow': True,
  1662. },
  1663. }
  1664. }
  1665. @_add_example_keys
  1666. def _get_examples_ode_sol_2nd_linear_bessel():
  1667. return {
  1668. 'hint': "2nd_linear_bessel",
  1669. 'func': f(x),
  1670. 'examples':{
  1671. '2nd_lin_bessel_01': {
  1672. 'eq': x**2*(f(x).diff(x, 2)) + x*(f(x).diff(x)) + (x**2 - 4)*f(x),
  1673. 'sol': [Eq(f(x), C1*besselj(2, x) + C2*bessely(2, x))],
  1674. },
  1675. '2nd_lin_bessel_02': {
  1676. 'eq': x**2*(f(x).diff(x, 2)) + x*(f(x).diff(x)) + (x**2 +25)*f(x),
  1677. 'sol': [Eq(f(x), C1*besselj(5*I, x) + C2*bessely(5*I, x))],
  1678. },
  1679. '2nd_lin_bessel_03': {
  1680. 'eq': x**2*(f(x).diff(x, 2)) + x*(f(x).diff(x)) + (x**2)*f(x),
  1681. 'sol': [Eq(f(x), C1*besselj(0, x) + C2*bessely(0, x))],
  1682. },
  1683. '2nd_lin_bessel_04': {
  1684. 'eq': x**2*(f(x).diff(x, 2)) + x*(f(x).diff(x)) + (81*x**2 -S(1)/9)*f(x),
  1685. 'sol': [Eq(f(x), C1*besselj(S(1)/3, 9*x) + C2*bessely(S(1)/3, 9*x))],
  1686. },
  1687. '2nd_lin_bessel_05': {
  1688. 'eq': x**2*(f(x).diff(x, 2)) + x*(f(x).diff(x)) + (x**4 - 4)*f(x),
  1689. 'sol': [Eq(f(x), C1*besselj(1, x**2/2) + C2*bessely(1, x**2/2))],
  1690. },
  1691. '2nd_lin_bessel_06': {
  1692. 'eq': x**2*(f(x).diff(x, 2)) + 2*x*(f(x).diff(x)) + (x**4 - 4)*f(x),
  1693. 'sol': [Eq(f(x), (C1*besselj(sqrt(17)/4, x**2/2) + C2*bessely(sqrt(17)/4, x**2/2))/sqrt(x))],
  1694. },
  1695. '2nd_lin_bessel_07': {
  1696. 'eq': x**2*(f(x).diff(x, 2)) + x*(f(x).diff(x)) + (x**2 - S(1)/4)*f(x),
  1697. 'sol': [Eq(f(x), C1*besselj(S(1)/2, x) + C2*bessely(S(1)/2, x))],
  1698. },
  1699. '2nd_lin_bessel_08': {
  1700. 'eq': x**2*(f(x).diff(x, 2)) - 3*x*(f(x).diff(x)) + (4*x + 4)*f(x),
  1701. 'sol': [Eq(f(x), x**2*(C1*besselj(0, 4*sqrt(x)) + C2*bessely(0, 4*sqrt(x))))],
  1702. },
  1703. '2nd_lin_bessel_09': {
  1704. 'eq': x*(f(x).diff(x, 2)) - f(x).diff(x) + 4*x**3*f(x),
  1705. 'sol': [Eq(f(x), x*(C1*besselj(S(1)/2, x**2) + C2*bessely(S(1)/2, x**2)))],
  1706. },
  1707. '2nd_lin_bessel_10': {
  1708. 'eq': (x-2)**2*(f(x).diff(x, 2)) - (x-2)*f(x).diff(x) + 4*(x-2)**2*f(x),
  1709. 'sol': [Eq(f(x), (x - 2)*(C1*besselj(1, 2*x - 4) + C2*bessely(1, 2*x - 4)))],
  1710. },
  1711. # https://github.com/sympy/sympy/issues/4414
  1712. '2nd_lin_bessel_11': {
  1713. 'eq': f(x).diff(x, x) + 2/x*f(x).diff(x) + f(x),
  1714. 'sol': [Eq(f(x), (C1*besselj(S(1)/2, x) + C2*bessely(S(1)/2, x))/sqrt(x))],
  1715. },
  1716. }
  1717. }
  1718. @_add_example_keys
  1719. def _get_examples_ode_sol_2nd_2F1_hypergeometric():
  1720. return {
  1721. 'hint': "2nd_hypergeometric",
  1722. 'func': f(x),
  1723. 'examples':{
  1724. '2nd_2F1_hyper_01': {
  1725. 'eq': x*(x-1)*f(x).diff(x, 2) + (S(3)/2 -2*x)*f(x).diff(x) + 2*f(x),
  1726. 'sol': [Eq(f(x), C1*x**(S(5)/2)*hyper((S(3)/2, S(1)/2), (S(7)/2,), x) + C2*hyper((-1, -2), (-S(3)/2,), x))],
  1727. },
  1728. '2nd_2F1_hyper_02': {
  1729. 'eq': x*(x-1)*f(x).diff(x, 2) + (S(7)/2*x)*f(x).diff(x) + f(x),
  1730. 'sol': [Eq(f(x), (C1*(1 - x)**(S(5)/2)*hyper((S(1)/2, 2), (S(7)/2,), 1 - x) +
  1731. C2*hyper((-S(1)/2, -2), (-S(3)/2,), 1 - x))/(x - 1)**(S(5)/2))],
  1732. },
  1733. '2nd_2F1_hyper_03': {
  1734. 'eq': x*(x-1)*f(x).diff(x, 2) + (S(3)+ S(7)/2*x)*f(x).diff(x) + f(x),
  1735. 'sol': [Eq(f(x), (C1*(1 - x)**(S(11)/2)*hyper((S(1)/2, 2), (S(13)/2,), 1 - x) +
  1736. C2*hyper((-S(7)/2, -5), (-S(9)/2,), 1 - x))/(x - 1)**(S(11)/2))],
  1737. },
  1738. '2nd_2F1_hyper_04': {
  1739. 'eq': -x**(S(5)/7)*(-416*x**(S(9)/7)/9 - 2385*x**(S(5)/7)/49 + S(298)*x/3)*f(x)/(196*(-x**(S(6)/7) +
  1740. x)**2*(x**(S(6)/7) + x)**2) + Derivative(f(x), (x, 2)),
  1741. 'sol': [Eq(f(x), x**(S(45)/98)*(C1*x**(S(4)/49)*hyper((S(1)/3, -S(1)/2), (S(9)/7,), x**(S(2)/7)) +
  1742. C2*hyper((S(1)/21, -S(11)/14), (S(5)/7,), x**(S(2)/7)))/(x**(S(2)/7) - 1)**(S(19)/84))],
  1743. 'checkodesol_XFAIL':True,
  1744. },
  1745. }
  1746. }
  1747. @_add_example_keys
  1748. def _get_examples_ode_sol_2nd_nonlinear_autonomous_conserved():
  1749. return {
  1750. 'hint': "2nd_nonlinear_autonomous_conserved",
  1751. 'func': f(x),
  1752. 'examples': {
  1753. '2nd_nonlinear_autonomous_conserved_01': {
  1754. 'eq': f(x).diff(x, 2) + exp(f(x)) + log(f(x)),
  1755. 'sol': [
  1756. Eq(Integral(1/sqrt(C1 - 2*_u*log(_u) + 2*_u - 2*exp(_u)), (_u, f(x))), C2 + x),
  1757. Eq(Integral(1/sqrt(C1 - 2*_u*log(_u) + 2*_u - 2*exp(_u)), (_u, f(x))), C2 - x)
  1758. ],
  1759. 'simplify_flag': False,
  1760. },
  1761. '2nd_nonlinear_autonomous_conserved_02': {
  1762. 'eq': f(x).diff(x, 2) + cbrt(f(x)) + 1/f(x),
  1763. 'sol': [
  1764. Eq(sqrt(2)*Integral(1/sqrt(2*C1 - 3*_u**Rational(4, 3) - 4*log(_u)), (_u, f(x))), C2 + x),
  1765. Eq(sqrt(2)*Integral(1/sqrt(2*C1 - 3*_u**Rational(4, 3) - 4*log(_u)), (_u, f(x))), C2 - x)
  1766. ],
  1767. 'simplify_flag': False,
  1768. },
  1769. '2nd_nonlinear_autonomous_conserved_03': {
  1770. 'eq': f(x).diff(x, 2) + sin(f(x)),
  1771. 'sol': [
  1772. Eq(Integral(1/sqrt(C1 + 2*cos(_u)), (_u, f(x))), C2 + x),
  1773. Eq(Integral(1/sqrt(C1 + 2*cos(_u)), (_u, f(x))), C2 - x)
  1774. ],
  1775. 'simplify_flag': False,
  1776. },
  1777. '2nd_nonlinear_autonomous_conserved_04': {
  1778. 'eq': f(x).diff(x, 2) + cosh(f(x)),
  1779. 'sol': [
  1780. Eq(Integral(1/sqrt(C1 - 2*sinh(_u)), (_u, f(x))), C2 + x),
  1781. Eq(Integral(1/sqrt(C1 - 2*sinh(_u)), (_u, f(x))), C2 - x)
  1782. ],
  1783. 'simplify_flag': False,
  1784. },
  1785. '2nd_nonlinear_autonomous_conserved_05': {
  1786. 'eq': f(x).diff(x, 2) + asin(f(x)),
  1787. 'sol': [
  1788. Eq(Integral(1/sqrt(C1 - 2*_u*asin(_u) - 2*sqrt(1 - _u**2)), (_u, f(x))), C2 + x),
  1789. Eq(Integral(1/sqrt(C1 - 2*_u*asin(_u) - 2*sqrt(1 - _u**2)), (_u, f(x))), C2 - x)
  1790. ],
  1791. 'simplify_flag': False,
  1792. 'XFAIL': ['2nd_nonlinear_autonomous_conserved_Integral']
  1793. }
  1794. }
  1795. }
  1796. @_add_example_keys
  1797. def _get_examples_ode_sol_separable_reduced():
  1798. df = f(x).diff(x)
  1799. return {
  1800. 'hint': "separable_reduced",
  1801. 'func': f(x),
  1802. 'examples':{
  1803. 'separable_reduced_01': {
  1804. 'eq': x* df + f(x)* (1 / (x**2*f(x) - 1)),
  1805. 'sol': [Eq(log(x**2*f(x))/3 + log(x**2*f(x) - Rational(3, 2))/6, C1 + log(x))],
  1806. 'simplify_flag': False,
  1807. 'XFAIL': ['lie_group'], #It hangs.
  1808. },
  1809. #Note: 'separable_reduced_02' is referred in 'separable_reduced_11'
  1810. 'separable_reduced_02': {
  1811. 'eq': f(x).diff(x) + (f(x) / (x**4*f(x) - x)),
  1812. 'sol': [Eq(log(x**3*f(x))/4 + log(x**3*f(x) - Rational(4,3))/12, C1 + log(x))],
  1813. 'simplify_flag': False,
  1814. 'checkodesol_XFAIL':True, #It hangs for this.
  1815. },
  1816. 'separable_reduced_03': {
  1817. 'eq': x*df + f(x)*(x**2*f(x)),
  1818. 'sol': [Eq(log(x**2*f(x))/2 - log(x**2*f(x) - 2)/2, C1 + log(x))],
  1819. 'simplify_flag': False,
  1820. },
  1821. 'separable_reduced_04': {
  1822. 'eq': Eq(f(x).diff(x) + f(x)/x * (1 + (x**(S(2)/3)*f(x))**2), 0),
  1823. 'sol': [Eq(-3*log(x**(S(2)/3)*f(x)) + 3*log(3*x**(S(4)/3)*f(x)**2 + 1)/2, C1 + log(x))],
  1824. 'simplify_flag': False,
  1825. },
  1826. 'separable_reduced_05': {
  1827. 'eq': Eq(f(x).diff(x) + f(x)/x * (1 + (x*f(x))**2), 0),
  1828. 'sol': [Eq(f(x), -sqrt(2)*sqrt(1/(C1 + log(x)))/(2*x)),\
  1829. Eq(f(x), sqrt(2)*sqrt(1/(C1 + log(x)))/(2*x))],
  1830. },
  1831. 'separable_reduced_06': {
  1832. 'eq': Eq(f(x).diff(x) + (x**4*f(x)**2 + x**2*f(x))*f(x)/(x*(x**6*f(x)**3 + x**4*f(x)**2)), 0),
  1833. 'sol': [Eq(f(x), C1 + 1/(2*x**2))],
  1834. },
  1835. 'separable_reduced_07': {
  1836. 'eq': Eq(f(x).diff(x) + (f(x)**2)*f(x)/(x), 0),
  1837. 'sol': [
  1838. Eq(f(x), -sqrt(2)*sqrt(1/(C1 + log(x)))/2),
  1839. Eq(f(x), sqrt(2)*sqrt(1/(C1 + log(x)))/2)
  1840. ],
  1841. },
  1842. 'separable_reduced_08': {
  1843. 'eq': Eq(f(x).diff(x) + (f(x)+3)*f(x)/(x*(f(x)+2)), 0),
  1844. 'sol': [Eq(-log(f(x) + 3)/3 - 2*log(f(x))/3, C1 + log(x))],
  1845. 'simplify_flag': False,
  1846. 'XFAIL': ['lie_group'], #It hangs.
  1847. },
  1848. 'separable_reduced_09': {
  1849. 'eq': Eq(f(x).diff(x) + (f(x)+3)*f(x)/x, 0),
  1850. 'sol': [Eq(f(x), 3/(C1*x**3 - 1))],
  1851. },
  1852. 'separable_reduced_10': {
  1853. 'eq': Eq(f(x).diff(x) + (f(x)**2+f(x))*f(x)/(x), 0),
  1854. 'sol': [Eq(- log(x) - log(f(x) + 1) + log(f(x)) + 1/f(x), C1)],
  1855. 'XFAIL': ['lie_group'],#No algorithms are implemented to solve equation -C1 + x*(_y + 1)*exp(-1/_y)/_y
  1856. },
  1857. # Equivalent to example_name 'separable_reduced_02'. Only difference is testing with simplify=True
  1858. 'separable_reduced_11': {
  1859. 'eq': f(x).diff(x) + (f(x) / (x**4*f(x) - x)),
  1860. 'sol': [Eq(f(x), -sqrt(2)*sqrt(3*3**Rational(1,3)*(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3)
  1861. - 3*3**Rational(2,3)*exp(12*C1)/(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3) + 2/x**6)/6
  1862. - sqrt(2)*sqrt(-3*3**Rational(1,3)*(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3)
  1863. + 3*3**Rational(2,3)*exp(12*C1)/(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3) + 4/x**6
  1864. - 4*sqrt(2)/(x**9*sqrt(3*3**Rational(1,3)*(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3)
  1865. - 3*3**Rational(2,3)*exp(12*C1)/(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3) + 2/x**6)))/6 + 1/(3*x**3)),
  1866. Eq(f(x), -sqrt(2)*sqrt(3*3**Rational(1,3)*(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3)
  1867. - 3*3**Rational(2,3)*exp(12*C1)/(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3) + 2/x**6)/6
  1868. + sqrt(2)*sqrt(-3*3**Rational(1,3)*(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3)
  1869. + 3*3**Rational(2,3)*exp(12*C1)/(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3) + 4/x**6
  1870. - 4*sqrt(2)/(x**9*sqrt(3*3**Rational(1,3)*(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3)
  1871. - 3*3**Rational(2,3)*exp(12*C1)/(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3) + 2/x**6)))/6 + 1/(3*x**3)),
  1872. Eq(f(x), sqrt(2)*sqrt(3*3**Rational(1,3)*(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3)
  1873. - 3*3**Rational(2,3)*exp(12*C1)/(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3) + 2/x**6)/6
  1874. - sqrt(2)*sqrt(-3*3**Rational(1,3)*(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3)
  1875. + 3*3**Rational(2,3)*exp(12*C1)/(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3)
  1876. + 4/x**6 + 4*sqrt(2)/(x**9*sqrt(3*3**Rational(1,3)*(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3)
  1877. - 3*3**Rational(2,3)*exp(12*C1)/(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3) + 2/x**6)))/6 + 1/(3*x**3)),
  1878. Eq(f(x), sqrt(2)*sqrt(3*3**Rational(1,3)*(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3)
  1879. - 3*3**Rational(2,3)*exp(12*C1)/(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3) + 2/x**6)/6
  1880. + sqrt(2)*sqrt(-3*3**Rational(1,3)*(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3) + 3*3**Rational(2,3)*exp(12*C1)/(sqrt((3*exp(12*C1)
  1881. + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3) + 4/x**6 + 4*sqrt(2)/(x**9*sqrt(3*3**Rational(1,3)*(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1))
  1882. - exp(12*C1)/x**6)**Rational(1,3) - 3*3**Rational(2,3)*exp(12*C1)/(sqrt((3*exp(12*C1) + x**(-12))*exp(24*C1)) - exp(12*C1)/x**6)**Rational(1,3) + 2/x**6)))/6 + 1/(3*x**3))],
  1883. 'checkodesol_XFAIL':True, #It hangs for this.
  1884. 'slow': True,
  1885. },
  1886. #These were from issue: https://github.com/sympy/sympy/issues/6247
  1887. 'separable_reduced_12': {
  1888. 'eq': x**2*f(x)**2 + x*Derivative(f(x), x),
  1889. 'sol': [Eq(f(x), 2*C1/(C1*x**2 - 1))],
  1890. },
  1891. }
  1892. }
  1893. @_add_example_keys
  1894. def _get_examples_ode_sol_lie_group():
  1895. a, b, c = symbols("a b c")
  1896. return {
  1897. 'hint': "lie_group",
  1898. 'func': f(x),
  1899. 'examples':{
  1900. #Example 1-4 and 19-20 were from issue: https://github.com/sympy/sympy/issues/17322
  1901. 'lie_group_01': {
  1902. 'eq': x*f(x).diff(x)*(f(x)+4) + (f(x)**2) -2*f(x)-2*x,
  1903. 'sol': [],
  1904. 'dsolve_too_slow': True,
  1905. 'checkodesol_too_slow': True,
  1906. },
  1907. 'lie_group_02': {
  1908. 'eq': x*f(x).diff(x)*(f(x)+4) + (f(x)**2) -2*f(x)-2*x,
  1909. 'sol': [],
  1910. 'dsolve_too_slow': True,
  1911. },
  1912. 'lie_group_03': {
  1913. 'eq': Eq(x**7*Derivative(f(x), x) + 5*x**3*f(x)**2 - (2*x**2 + 2)*f(x)**3, 0),
  1914. 'sol': [],
  1915. 'dsolve_too_slow': True,
  1916. },
  1917. 'lie_group_04': {
  1918. 'eq': f(x).diff(x) - (f(x) - x*log(x))**2/x**2 + log(x),
  1919. 'sol': [],
  1920. 'XFAIL': ['lie_group'],
  1921. },
  1922. 'lie_group_05': {
  1923. 'eq': f(x).diff(x)**2,
  1924. 'sol': [Eq(f(x), C1)],
  1925. 'XFAIL': ['factorable'], #It raises Not Implemented error
  1926. },
  1927. 'lie_group_06': {
  1928. 'eq': Eq(f(x).diff(x), x**2*f(x)),
  1929. 'sol': [Eq(f(x), C1*exp(x**3)**Rational(1, 3))],
  1930. },
  1931. 'lie_group_07': {
  1932. 'eq': f(x).diff(x) + a*f(x) - c*exp(b*x),
  1933. 'sol': [Eq(f(x), Piecewise(((-C1*(a + b) + c*exp(x*(a + b)))*exp(-a*x)/(a + b),\
  1934. Ne(a, -b)), ((-C1 + c*x)*exp(-a*x), True)))],
  1935. },
  1936. 'lie_group_08': {
  1937. 'eq': f(x).diff(x) + 2*x*f(x) - x*exp(-x**2),
  1938. 'sol': [Eq(f(x), (C1 + x**2/2)*exp(-x**2))],
  1939. },
  1940. 'lie_group_09': {
  1941. 'eq': (1 + 2*x)*(f(x).diff(x)) + 2 - 4*exp(-f(x)),
  1942. 'sol': [Eq(f(x), log(C1/(2*x + 1) + 2))],
  1943. },
  1944. 'lie_group_10': {
  1945. 'eq': x**2*(f(x).diff(x)) - f(x) + x**2*exp(x - (1/x)),
  1946. 'sol': [Eq(f(x), (C1 - exp(x))*exp(-1/x))],
  1947. 'XFAIL': ['factorable'], #It raises Recursion Error (maixmum depth exceeded)
  1948. },
  1949. 'lie_group_11': {
  1950. 'eq': x**2*f(x)**2 + x*Derivative(f(x), x),
  1951. 'sol': [Eq(f(x), 2/(C1 + x**2))],
  1952. },
  1953. 'lie_group_12': {
  1954. 'eq': diff(f(x),x) + 2*x*f(x) - x*exp(-x**2),
  1955. 'sol': [Eq(f(x), exp(-x**2)*(C1 + x**2/2))],
  1956. },
  1957. 'lie_group_13': {
  1958. 'eq': diff(f(x),x) + f(x)*cos(x) - exp(2*x),
  1959. 'sol': [Eq(f(x), exp(-sin(x))*(C1 + Integral(exp(2*x)*exp(sin(x)), x)))],
  1960. },
  1961. 'lie_group_14': {
  1962. 'eq': diff(f(x),x) + f(x)*cos(x) - sin(2*x)/2,
  1963. 'sol': [Eq(f(x), C1*exp(-sin(x)) + sin(x) - 1)],
  1964. },
  1965. 'lie_group_15': {
  1966. 'eq': x*diff(f(x),x) + f(x) - x*sin(x),
  1967. 'sol': [Eq(f(x), (C1 - x*cos(x) + sin(x))/x)],
  1968. },
  1969. 'lie_group_16': {
  1970. 'eq': x*diff(f(x),x) - f(x) - x/log(x),
  1971. 'sol': [Eq(f(x), x*(C1 + log(log(x))))],
  1972. },
  1973. 'lie_group_17': {
  1974. 'eq': (f(x).diff(x)-f(x)) * (f(x).diff(x)+f(x)),
  1975. 'sol': [Eq(f(x), C1*exp(x)), Eq(f(x), C1*exp(-x))],
  1976. },
  1977. 'lie_group_18': {
  1978. 'eq': f(x).diff(x) * (f(x).diff(x) - f(x)),
  1979. 'sol': [Eq(f(x), C1*exp(x)), Eq(f(x), C1)],
  1980. },
  1981. 'lie_group_19': {
  1982. 'eq': (f(x).diff(x)-f(x)) * (f(x).diff(x)+f(x)),
  1983. 'sol': [Eq(f(x), C1*exp(-x)), Eq(f(x), C1*exp(x))],
  1984. },
  1985. 'lie_group_20': {
  1986. 'eq': f(x).diff(x)*(f(x).diff(x)+f(x)),
  1987. 'sol': [Eq(f(x), C1), Eq(f(x), C1*exp(-x))],
  1988. },
  1989. }
  1990. }
  1991. @_add_example_keys
  1992. def _get_examples_ode_sol_2nd_linear_airy():
  1993. return {
  1994. 'hint': "2nd_linear_airy",
  1995. 'func': f(x),
  1996. 'examples':{
  1997. '2nd_lin_airy_01': {
  1998. 'eq': f(x).diff(x, 2) - x*f(x),
  1999. 'sol': [Eq(f(x), C1*airyai(x) + C2*airybi(x))],
  2000. },
  2001. '2nd_lin_airy_02': {
  2002. 'eq': f(x).diff(x, 2) + 2*x*f(x),
  2003. 'sol': [Eq(f(x), C1*airyai(-2**(S(1)/3)*x) + C2*airybi(-2**(S(1)/3)*x))],
  2004. },
  2005. }
  2006. }
  2007. @_add_example_keys
  2008. def _get_examples_ode_sol_nth_linear_constant_coeff_homogeneous():
  2009. # From Exercise 20, in Ordinary Differential Equations,
  2010. # Tenenbaum and Pollard, pg. 220
  2011. a = Symbol('a', positive=True)
  2012. k = Symbol('k', real=True)
  2013. r1, r2, r3, r4, r5 = [rootof(x**5 + 11*x - 2, n) for n in range(5)]
  2014. r6, r7, r8, r9, r10 = [rootof(x**5 - 3*x + 1, n) for n in range(5)]
  2015. r11, r12, r13, r14, r15 = [rootof(x**5 - 100*x**3 + 1000*x + 1, n) for n in range(5)]
  2016. r16, r17, r18, r19, r20 = [rootof(x**5 - x**4 + 10, n) for n in range(5)]
  2017. r21, r22, r23, r24, r25 = [rootof(x**5 - x + 1, n) for n in range(5)]
  2018. E = exp(1)
  2019. return {
  2020. 'hint': "nth_linear_constant_coeff_homogeneous",
  2021. 'func': f(x),
  2022. 'examples':{
  2023. 'lin_const_coeff_hom_01': {
  2024. 'eq': f(x).diff(x, 2) + 2*f(x).diff(x),
  2025. 'sol': [Eq(f(x), C1 + C2*exp(-2*x))],
  2026. },
  2027. 'lin_const_coeff_hom_02': {
  2028. 'eq': f(x).diff(x, 2) - 3*f(x).diff(x) + 2*f(x),
  2029. 'sol': [Eq(f(x), (C1 + C2*exp(x))*exp(x))],
  2030. },
  2031. 'lin_const_coeff_hom_03': {
  2032. 'eq': f(x).diff(x, 2) - f(x),
  2033. 'sol': [Eq(f(x), C1*exp(-x) + C2*exp(x))],
  2034. },
  2035. 'lin_const_coeff_hom_04': {
  2036. 'eq': f(x).diff(x, 3) + f(x).diff(x, 2) - 6*f(x).diff(x),
  2037. 'sol': [Eq(f(x), C1 + C2*exp(-3*x) + C3*exp(2*x))],
  2038. 'slow': True,
  2039. },
  2040. 'lin_const_coeff_hom_05': {
  2041. 'eq': 6*f(x).diff(x, 2) - 11*f(x).diff(x) + 4*f(x),
  2042. 'sol': [Eq(f(x), C1*exp(x/2) + C2*exp(x*Rational(4, 3)))],
  2043. 'slow': True,
  2044. },
  2045. 'lin_const_coeff_hom_06': {
  2046. 'eq': Eq(f(x).diff(x, 2) + 2*f(x).diff(x) - f(x), 0),
  2047. 'sol': [Eq(f(x), C1*exp(x*(-1 + sqrt(2))) + C2*exp(-x*(sqrt(2) + 1)))],
  2048. 'slow': True,
  2049. },
  2050. 'lin_const_coeff_hom_07': {
  2051. 'eq': diff(f(x), x, 3) + diff(f(x), x, 2) - 10*diff(f(x), x) - 6*f(x),
  2052. 'sol': [Eq(f(x), C1*exp(3*x) + C3*exp(-x*(2 + sqrt(2))) + C2*exp(x*(-2 + sqrt(2))))],
  2053. 'slow': True,
  2054. },
  2055. 'lin_const_coeff_hom_08': {
  2056. 'eq': f(x).diff(x, 4) - f(x).diff(x, 3) - 4*f(x).diff(x, 2) + \
  2057. 4*f(x).diff(x),
  2058. 'sol': [Eq(f(x), C1 + C2*exp(-2*x) + C3*exp(x) + C4*exp(2*x))],
  2059. 'slow': True,
  2060. },
  2061. 'lin_const_coeff_hom_09': {
  2062. 'eq': f(x).diff(x, 4) + 4*f(x).diff(x, 3) + f(x).diff(x, 2) - \
  2063. 4*f(x).diff(x) - 2*f(x),
  2064. 'sol': [Eq(f(x), C3*exp(-x) + C4*exp(x) + (C1*exp(-sqrt(2)*x) + C2*exp(sqrt(2)*x))*exp(-2*x))],
  2065. 'slow': True,
  2066. },
  2067. 'lin_const_coeff_hom_10': {
  2068. 'eq': f(x).diff(x, 4) - a**2*f(x),
  2069. 'sol': [Eq(f(x), C1*exp(-sqrt(a)*x) + C2*exp(sqrt(a)*x) + C3*sin(sqrt(a)*x) + C4*cos(sqrt(a)*x))],
  2070. 'slow': True,
  2071. },
  2072. 'lin_const_coeff_hom_11': {
  2073. 'eq': f(x).diff(x, 2) - 2*k*f(x).diff(x) - 2*f(x),
  2074. 'sol': [Eq(f(x), C1*exp(x*(k - sqrt(k**2 + 2))) + C2*exp(x*(k + sqrt(k**2 + 2))))],
  2075. 'slow': True,
  2076. },
  2077. 'lin_const_coeff_hom_12': {
  2078. 'eq': f(x).diff(x, 2) + 4*k*f(x).diff(x) - 12*k**2*f(x),
  2079. 'sol': [Eq(f(x), C1*exp(-6*k*x) + C2*exp(2*k*x))],
  2080. 'slow': True,
  2081. },
  2082. 'lin_const_coeff_hom_13': {
  2083. 'eq': f(x).diff(x, 4),
  2084. 'sol': [Eq(f(x), C1 + C2*x + C3*x**2 + C4*x**3)],
  2085. 'slow': True,
  2086. },
  2087. 'lin_const_coeff_hom_14': {
  2088. 'eq': f(x).diff(x, 2) + 4*f(x).diff(x) + 4*f(x),
  2089. 'sol': [Eq(f(x), (C1 + C2*x)*exp(-2*x))],
  2090. 'slow': True,
  2091. },
  2092. 'lin_const_coeff_hom_15': {
  2093. 'eq': 3*f(x).diff(x, 3) + 5*f(x).diff(x, 2) + f(x).diff(x) - f(x),
  2094. 'sol': [Eq(f(x), (C1 + C2*x)*exp(-x) + C3*exp(x/3))],
  2095. 'slow': True,
  2096. },
  2097. 'lin_const_coeff_hom_16': {
  2098. 'eq': f(x).diff(x, 3) - 6*f(x).diff(x, 2) + 12*f(x).diff(x) - 8*f(x),
  2099. 'sol': [Eq(f(x), (C1 + x*(C2 + C3*x))*exp(2*x))],
  2100. 'slow': True,
  2101. },
  2102. 'lin_const_coeff_hom_17': {
  2103. 'eq': f(x).diff(x, 2) - 2*a*f(x).diff(x) + a**2*f(x),
  2104. 'sol': [Eq(f(x), (C1 + C2*x)*exp(a*x))],
  2105. 'slow': True,
  2106. },
  2107. 'lin_const_coeff_hom_18': {
  2108. 'eq': f(x).diff(x, 4) + 3*f(x).diff(x, 3),
  2109. 'sol': [Eq(f(x), C1 + C2*x + C3*x**2 + C4*exp(-3*x))],
  2110. 'slow': True,
  2111. },
  2112. 'lin_const_coeff_hom_19': {
  2113. 'eq': f(x).diff(x, 4) - 2*f(x).diff(x, 2),
  2114. 'sol': [Eq(f(x), C1 + C2*x + C3*exp(-sqrt(2)*x) + C4*exp(sqrt(2)*x))],
  2115. 'slow': True,
  2116. },
  2117. 'lin_const_coeff_hom_20': {
  2118. 'eq': f(x).diff(x, 4) + 2*f(x).diff(x, 3) - 11*f(x).diff(x, 2) - \
  2119. 12*f(x).diff(x) + 36*f(x),
  2120. 'sol': [Eq(f(x), (C1 + C2*x)*exp(-3*x) + (C3 + C4*x)*exp(2*x))],
  2121. 'slow': True,
  2122. },
  2123. 'lin_const_coeff_hom_21': {
  2124. 'eq': 36*f(x).diff(x, 4) - 37*f(x).diff(x, 2) + 4*f(x).diff(x) + 5*f(x),
  2125. 'sol': [Eq(f(x), C1*exp(-x) + C2*exp(-x/3) + C3*exp(x/2) + C4*exp(x*Rational(5, 6)))],
  2126. 'slow': True,
  2127. },
  2128. 'lin_const_coeff_hom_22': {
  2129. 'eq': f(x).diff(x, 4) - 8*f(x).diff(x, 2) + 16*f(x),
  2130. 'sol': [Eq(f(x), (C1 + C2*x)*exp(-2*x) + (C3 + C4*x)*exp(2*x))],
  2131. 'slow': True,
  2132. },
  2133. 'lin_const_coeff_hom_23': {
  2134. 'eq': f(x).diff(x, 2) - 2*f(x).diff(x) + 5*f(x),
  2135. 'sol': [Eq(f(x), (C1*sin(2*x) + C2*cos(2*x))*exp(x))],
  2136. 'slow': True,
  2137. },
  2138. 'lin_const_coeff_hom_24': {
  2139. 'eq': f(x).diff(x, 2) - f(x).diff(x) + f(x),
  2140. 'sol': [Eq(f(x), (C1*sin(x*sqrt(3)/2) + C2*cos(x*sqrt(3)/2))*exp(x/2))],
  2141. 'slow': True,
  2142. },
  2143. 'lin_const_coeff_hom_25': {
  2144. 'eq': f(x).diff(x, 4) + 5*f(x).diff(x, 2) + 6*f(x),
  2145. 'sol': [Eq(f(x),
  2146. C1*sin(sqrt(2)*x) + C2*sin(sqrt(3)*x) + C3*cos(sqrt(2)*x) + C4*cos(sqrt(3)*x))],
  2147. 'slow': True,
  2148. },
  2149. 'lin_const_coeff_hom_26': {
  2150. 'eq': f(x).diff(x, 2) - 4*f(x).diff(x) + 20*f(x),
  2151. 'sol': [Eq(f(x), (C1*sin(4*x) + C2*cos(4*x))*exp(2*x))],
  2152. 'slow': True,
  2153. },
  2154. 'lin_const_coeff_hom_27': {
  2155. 'eq': f(x).diff(x, 4) + 4*f(x).diff(x, 2) + 4*f(x),
  2156. 'sol': [Eq(f(x), (C1 + C2*x)*sin(x*sqrt(2)) + (C3 + C4*x)*cos(x*sqrt(2)))],
  2157. 'slow': True,
  2158. },
  2159. 'lin_const_coeff_hom_28': {
  2160. 'eq': f(x).diff(x, 3) + 8*f(x),
  2161. 'sol': [Eq(f(x), (C1*sin(x*sqrt(3)) + C2*cos(x*sqrt(3)))*exp(x) + C3*exp(-2*x))],
  2162. 'slow': True,
  2163. },
  2164. 'lin_const_coeff_hom_29': {
  2165. 'eq': f(x).diff(x, 4) + 4*f(x).diff(x, 2),
  2166. 'sol': [Eq(f(x), C1 + C2*x + C3*sin(2*x) + C4*cos(2*x))],
  2167. 'slow': True,
  2168. },
  2169. 'lin_const_coeff_hom_30': {
  2170. 'eq': f(x).diff(x, 5) + 2*f(x).diff(x, 3) + f(x).diff(x),
  2171. 'sol': [Eq(f(x), C1 + (C2 + C3*x)*sin(x) + (C4 + C5*x)*cos(x))],
  2172. 'slow': True,
  2173. },
  2174. 'lin_const_coeff_hom_31': {
  2175. 'eq': f(x).diff(x, 4) + f(x).diff(x, 2) + f(x),
  2176. 'sol': [Eq(f(x), (C1*sin(sqrt(3)*x/2) + C2*cos(sqrt(3)*x/2))*exp(-x/2)
  2177. + (C3*sin(sqrt(3)*x/2) + C4*cos(sqrt(3)*x/2))*exp(x/2))],
  2178. 'slow': True,
  2179. },
  2180. 'lin_const_coeff_hom_32': {
  2181. 'eq': f(x).diff(x, 4) + 4*f(x).diff(x, 2) + f(x),
  2182. 'sol': [Eq(f(x), C1*sin(x*sqrt(-sqrt(3) + 2)) + C2*sin(x*sqrt(sqrt(3) + 2))
  2183. + C3*cos(x*sqrt(-sqrt(3) + 2)) + C4*cos(x*sqrt(sqrt(3) + 2)))],
  2184. 'slow': True,
  2185. },
  2186. # One real root, two complex conjugate pairs
  2187. 'lin_const_coeff_hom_33': {
  2188. 'eq': f(x).diff(x, 5) + 11*f(x).diff(x) - 2*f(x),
  2189. 'sol': [Eq(f(x),
  2190. C5*exp(r1*x) + exp(re(r2)*x) * (C1*sin(im(r2)*x) + C2*cos(im(r2)*x))
  2191. + exp(re(r4)*x) * (C3*sin(im(r4)*x) + C4*cos(im(r4)*x)))],
  2192. 'checkodesol_XFAIL':True, #It Hangs
  2193. },
  2194. # Three real roots, one complex conjugate pair
  2195. 'lin_const_coeff_hom_34': {
  2196. 'eq': f(x).diff(x,5) - 3*f(x).diff(x) + f(x),
  2197. 'sol': [Eq(f(x),
  2198. C3*exp(r6*x) + C4*exp(r7*x) + C5*exp(r8*x)
  2199. + exp(re(r9)*x) * (C1*sin(im(r9)*x) + C2*cos(im(r9)*x)))],
  2200. 'checkodesol_XFAIL':True, #It Hangs
  2201. },
  2202. # Five distinct real roots
  2203. 'lin_const_coeff_hom_35': {
  2204. 'eq': f(x).diff(x,5) - 100*f(x).diff(x,3) + 1000*f(x).diff(x) + f(x),
  2205. 'sol': [Eq(f(x), C1*exp(r11*x) + C2*exp(r12*x) + C3*exp(r13*x) + C4*exp(r14*x) + C5*exp(r15*x))],
  2206. 'checkodesol_XFAIL':True, #It Hangs
  2207. },
  2208. # Rational root and unsolvable quintic
  2209. 'lin_const_coeff_hom_36': {
  2210. 'eq': f(x).diff(x, 6) - 6*f(x).diff(x, 5) + 5*f(x).diff(x, 4) + 10*f(x).diff(x) - 50 * f(x),
  2211. 'sol': [Eq(f(x),
  2212. C5*exp(5*x)
  2213. + C6*exp(x*r16)
  2214. + exp(re(r17)*x) * (C1*sin(im(r17)*x) + C2*cos(im(r17)*x))
  2215. + exp(re(r19)*x) * (C3*sin(im(r19)*x) + C4*cos(im(r19)*x)))],
  2216. 'checkodesol_XFAIL':True, #It Hangs
  2217. },
  2218. # Five double roots (this is (x**5 - x + 1)**2)
  2219. 'lin_const_coeff_hom_37': {
  2220. 'eq': f(x).diff(x, 10) - 2*f(x).diff(x, 6) + 2*f(x).diff(x, 5)
  2221. + f(x).diff(x, 2) - 2*f(x).diff(x, 1) + f(x),
  2222. 'sol': [Eq(f(x), (C1 + C2*x)*exp(x*r21) + (-((C3 + C4*x)*sin(x*im(r22)))
  2223. + (C5 + C6*x)*cos(x*im(r22)))*exp(x*re(r22)) + (-((C7 + C8*x)*sin(x*im(r24)))
  2224. + (C10*x + C9)*cos(x*im(r24)))*exp(x*re(r24)))],
  2225. 'checkodesol_XFAIL':True, #It Hangs
  2226. },
  2227. 'lin_const_coeff_hom_38': {
  2228. 'eq': Eq(sqrt(2) * f(x).diff(x,x,x) + f(x).diff(x), 0),
  2229. 'sol': [Eq(f(x), C1 + C2*sin(2**Rational(3, 4)*x/2) + C3*cos(2**Rational(3, 4)*x/2))],
  2230. },
  2231. 'lin_const_coeff_hom_39': {
  2232. 'eq': Eq(E * f(x).diff(x,x,x) + f(x).diff(x), 0),
  2233. 'sol': [Eq(f(x), C1 + C2*sin(x/sqrt(E)) + C3*cos(x/sqrt(E)))],
  2234. },
  2235. 'lin_const_coeff_hom_40': {
  2236. 'eq': Eq(pi * f(x).diff(x,x,x) + f(x).diff(x), 0),
  2237. 'sol': [Eq(f(x), C1 + C2*sin(x/sqrt(pi)) + C3*cos(x/sqrt(pi)))],
  2238. },
  2239. 'lin_const_coeff_hom_41': {
  2240. 'eq': Eq(I * f(x).diff(x,x,x) + f(x).diff(x), 0),
  2241. 'sol': [Eq(f(x), C1 + C2*exp(-sqrt(I)*x) + C3*exp(sqrt(I)*x))],
  2242. },
  2243. 'lin_const_coeff_hom_42': {
  2244. 'eq': f(x).diff(x, x) + y*f(x),
  2245. 'sol': [Eq(f(x), C1*exp(-x*sqrt(-y)) + C2*exp(x*sqrt(-y)))],
  2246. },
  2247. 'lin_const_coeff_hom_43': {
  2248. 'eq': Eq(9*f(x).diff(x, x) + f(x), 0),
  2249. 'sol': [Eq(f(x), C1*sin(x/3) + C2*cos(x/3))],
  2250. },
  2251. 'lin_const_coeff_hom_44': {
  2252. 'eq': Eq(9*f(x).diff(x, x), f(x)),
  2253. 'sol': [Eq(f(x), C1*exp(-x/3) + C2*exp(x/3))],
  2254. },
  2255. 'lin_const_coeff_hom_45': {
  2256. 'eq': Eq(f(x).diff(x, x) - 3*diff(f(x), x) + 2*f(x), 0),
  2257. 'sol': [Eq(f(x), (C1 + C2*exp(x))*exp(x))],
  2258. },
  2259. 'lin_const_coeff_hom_46': {
  2260. 'eq': Eq(f(x).diff(x, x) - 4*diff(f(x), x) + 4*f(x), 0),
  2261. 'sol': [Eq(f(x), (C1 + C2*x)*exp(2*x))],
  2262. },
  2263. # Type: 2nd order, constant coefficients (two real equal roots)
  2264. 'lin_const_coeff_hom_47': {
  2265. 'eq': Eq(f(x).diff(x, x) + 2*diff(f(x), x) + 3*f(x), 0),
  2266. 'sol': [Eq(f(x), (C1*sin(x*sqrt(2)) + C2*cos(x*sqrt(2)))*exp(-x))],
  2267. },
  2268. #These were from issue: https://github.com/sympy/sympy/issues/6247
  2269. 'lin_const_coeff_hom_48': {
  2270. 'eq': f(x).diff(x, x) + 4*f(x),
  2271. 'sol': [Eq(f(x), C1*sin(2*x) + C2*cos(2*x))],
  2272. },
  2273. }
  2274. }
  2275. @_add_example_keys
  2276. def _get_examples_ode_sol_1st_homogeneous_coeff_subs_dep_div_indep():
  2277. return {
  2278. 'hint': "1st_homogeneous_coeff_subs_dep_div_indep",
  2279. 'func': f(x),
  2280. 'examples':{
  2281. 'dep_div_indep_01': {
  2282. 'eq': f(x)/x*cos(f(x)/x) - (x/f(x)*sin(f(x)/x) + cos(f(x)/x))*f(x).diff(x),
  2283. 'sol': [Eq(log(x), C1 - log(f(x)*sin(f(x)/x)/x))],
  2284. 'slow': True
  2285. },
  2286. #indep_div_dep actually has a simpler solution for example 2 but it runs too slow.
  2287. 'dep_div_indep_02': {
  2288. 'eq': x*f(x).diff(x) - f(x) - x*sin(f(x)/x),
  2289. 'sol': [Eq(log(x), log(C1) + log(cos(f(x)/x) - 1)/2 - log(cos(f(x)/x) + 1)/2)],
  2290. 'simplify_flag':False,
  2291. },
  2292. 'dep_div_indep_03': {
  2293. 'eq': x*exp(f(x)/x) - f(x)*sin(f(x)/x) + x*sin(f(x)/x)*f(x).diff(x),
  2294. 'sol': [Eq(log(x), C1 + exp(-f(x)/x)*sin(f(x)/x)/2 + exp(-f(x)/x)*cos(f(x)/x)/2)],
  2295. 'slow': True
  2296. },
  2297. 'dep_div_indep_04': {
  2298. 'eq': f(x).diff(x) - f(x)/x + 1/sin(f(x)/x),
  2299. 'sol': [Eq(f(x), x*(-acos(C1 + log(x)) + 2*pi)), Eq(f(x), x*acos(C1 + log(x)))],
  2300. 'slow': True
  2301. },
  2302. # previous code was testing with these other solution:
  2303. # example5_solb = Eq(f(x), log(log(C1/x)**(-x)))
  2304. 'dep_div_indep_05': {
  2305. 'eq': x*exp(f(x)/x) + f(x) - x*f(x).diff(x),
  2306. 'sol': [Eq(f(x), log((1/(C1 - log(x)))**x))],
  2307. 'checkodesol_XFAIL':True, #(because of **x?)
  2308. },
  2309. }
  2310. }
  2311. @_add_example_keys
  2312. def _get_examples_ode_sol_linear_coefficients():
  2313. return {
  2314. 'hint': "linear_coefficients",
  2315. 'func': f(x),
  2316. 'examples':{
  2317. 'linear_coeff_01': {
  2318. 'eq': f(x).diff(x) + (3 + 2*f(x))/(x + 3),
  2319. 'sol': [Eq(f(x), C1/(x**2 + 6*x + 9) - Rational(3, 2))],
  2320. },
  2321. }
  2322. }
  2323. @_add_example_keys
  2324. def _get_examples_ode_sol_1st_homogeneous_coeff_best():
  2325. return {
  2326. 'hint': "1st_homogeneous_coeff_best",
  2327. 'func': f(x),
  2328. 'examples':{
  2329. # previous code was testing this with other solution:
  2330. # example1_solb = Eq(-f(x)/(1 + log(x/f(x))), C1)
  2331. '1st_homogeneous_coeff_best_01': {
  2332. 'eq': f(x) + (x*log(f(x)/x) - 2*x)*diff(f(x), x),
  2333. 'sol': [Eq(f(x), -exp(C1)*LambertW(-x*exp(-C1 + 1)))],
  2334. 'checkodesol_XFAIL':True, #(because of LambertW?)
  2335. },
  2336. '1st_homogeneous_coeff_best_02': {
  2337. 'eq': 2*f(x)*exp(x/f(x)) + f(x)*f(x).diff(x) - 2*x*exp(x/f(x))*f(x).diff(x),
  2338. 'sol': [Eq(log(f(x)), C1 - 2*exp(x/f(x)))],
  2339. },
  2340. # previous code was testing this with other solution:
  2341. # example3_solb = Eq(log(C1*x*sqrt(1/x)*sqrt(f(x))) + x**2/(2*f(x)**2), 0)
  2342. '1st_homogeneous_coeff_best_03': {
  2343. 'eq': 2*x**2*f(x) + f(x)**3 + (x*f(x)**2 - 2*x**3)*f(x).diff(x),
  2344. 'sol': [Eq(f(x), exp(2*C1 + LambertW(-2*x**4*exp(-4*C1))/2)/x)],
  2345. 'checkodesol_XFAIL':True, #(because of LambertW?)
  2346. },
  2347. '1st_homogeneous_coeff_best_04': {
  2348. 'eq': (x + sqrt(f(x)**2 - x*f(x)))*f(x).diff(x) - f(x),
  2349. 'sol': [Eq(log(f(x)), C1 - 2*sqrt(-x/f(x) + 1))],
  2350. 'slow': True,
  2351. },
  2352. '1st_homogeneous_coeff_best_05': {
  2353. 'eq': x + f(x) - (x - f(x))*f(x).diff(x),
  2354. 'sol': [Eq(log(x), C1 - log(sqrt(1 + f(x)**2/x**2)) + atan(f(x)/x))],
  2355. },
  2356. '1st_homogeneous_coeff_best_06': {
  2357. 'eq': x*f(x).diff(x) - f(x) - x*sin(f(x)/x),
  2358. 'sol': [Eq(f(x), 2*x*atan(C1*x))],
  2359. },
  2360. '1st_homogeneous_coeff_best_07': {
  2361. 'eq': x**2 + f(x)**2 - 2*x*f(x)*f(x).diff(x),
  2362. 'sol': [Eq(f(x), -sqrt(x*(C1 + x))), Eq(f(x), sqrt(x*(C1 + x)))],
  2363. },
  2364. '1st_homogeneous_coeff_best_08': {
  2365. 'eq': f(x)**2 + (x*sqrt(f(x)**2 - x**2) - x*f(x))*f(x).diff(x),
  2366. 'sol': [Eq(f(x), -sqrt(-x*exp(2*C1)/(x - 2*exp(C1)))), Eq(f(x), sqrt(-x*exp(2*C1)/(x - 2*exp(C1))))],
  2367. 'checkodesol_XFAIL': True # solutions are valid in a range
  2368. },
  2369. }
  2370. }
  2371. def _get_all_examples():
  2372. all_examples = _get_examples_ode_sol_euler_homogeneous + \
  2373. _get_examples_ode_sol_euler_undetermined_coeff + \
  2374. _get_examples_ode_sol_euler_var_para + \
  2375. _get_examples_ode_sol_factorable + \
  2376. _get_examples_ode_sol_bernoulli + \
  2377. _get_examples_ode_sol_nth_algebraic + \
  2378. _get_examples_ode_sol_riccati + \
  2379. _get_examples_ode_sol_1st_linear + \
  2380. _get_examples_ode_sol_1st_exact + \
  2381. _get_examples_ode_sol_almost_linear + \
  2382. _get_examples_ode_sol_nth_order_reducible + \
  2383. _get_examples_ode_sol_nth_linear_undetermined_coefficients + \
  2384. _get_examples_ode_sol_liouville + \
  2385. _get_examples_ode_sol_separable + \
  2386. _get_examples_ode_sol_1st_rational_riccati + \
  2387. _get_examples_ode_sol_nth_linear_var_of_parameters + \
  2388. _get_examples_ode_sol_2nd_linear_bessel + \
  2389. _get_examples_ode_sol_2nd_2F1_hypergeometric + \
  2390. _get_examples_ode_sol_2nd_nonlinear_autonomous_conserved + \
  2391. _get_examples_ode_sol_separable_reduced + \
  2392. _get_examples_ode_sol_lie_group + \
  2393. _get_examples_ode_sol_2nd_linear_airy + \
  2394. _get_examples_ode_sol_nth_linear_constant_coeff_homogeneous +\
  2395. _get_examples_ode_sol_1st_homogeneous_coeff_best +\
  2396. _get_examples_ode_sol_1st_homogeneous_coeff_subs_dep_div_indep +\
  2397. _get_examples_ode_sol_linear_coefficients
  2398. return all_examples