test_single.py 98 KB

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