heurisch.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. from __future__ import annotations
  2. from collections import defaultdict
  3. from functools import reduce
  4. from itertools import permutations
  5. from sympy.core.add import Add
  6. from sympy.core.basic import Basic
  7. from sympy.core.mul import Mul
  8. from sympy.core.symbol import Wild, Dummy, Symbol
  9. from sympy.core.basic import sympify
  10. from sympy.core.numbers import Rational, pi, I
  11. from sympy.core.relational import Eq, Ne
  12. from sympy.core.singleton import S
  13. from sympy.core.sorting import ordered
  14. from sympy.core.traversal import iterfreeargs
  15. from sympy.functions import exp, sin, cos, tan, cot, asin, atan
  16. from sympy.functions import log, sinh, cosh, tanh, coth, asinh
  17. from sympy.functions import sqrt, erf, erfi, li, Ei
  18. from sympy.functions import besselj, bessely, besseli, besselk
  19. from sympy.functions import hankel1, hankel2, jn, yn
  20. from sympy.functions.elementary.complexes import Abs, re, im, sign, arg
  21. from sympy.functions.elementary.exponential import LambertW
  22. from sympy.functions.elementary.integers import floor, ceiling
  23. from sympy.functions.elementary.piecewise import Piecewise
  24. from sympy.functions.special.delta_functions import Heaviside, DiracDelta
  25. from sympy.simplify.radsimp import collect
  26. from sympy.logic.boolalg import And, Or
  27. from sympy.utilities.iterables import uniq
  28. from sympy.polys import quo, gcd, lcm, factor_list, cancel, PolynomialError
  29. from sympy.polys.monomials import itermonomials
  30. from sympy.polys.polyroots import root_factors
  31. from sympy.polys.rings import PolyRing
  32. from sympy.polys.solvers import solve_lin_sys
  33. from sympy.polys.constructor import construct_domain
  34. from sympy.integrals.integrals import integrate
  35. def components(f, x):
  36. """
  37. Returns a set of all functional components of the given expression
  38. which includes symbols, function applications and compositions and
  39. non-integer powers. Fractional powers are collected with
  40. minimal, positive exponents.
  41. Examples
  42. ========
  43. >>> from sympy import cos, sin
  44. >>> from sympy.abc import x
  45. >>> from sympy.integrals.heurisch import components
  46. >>> components(sin(x)*cos(x)**2, x)
  47. {x, sin(x), cos(x)}
  48. See Also
  49. ========
  50. heurisch
  51. """
  52. result = set()
  53. if f.has_free(x):
  54. if f.is_symbol and f.is_commutative:
  55. result.add(f)
  56. elif f.is_Function or f.is_Derivative:
  57. for g in f.args:
  58. result |= components(g, x)
  59. result.add(f)
  60. elif f.is_Pow:
  61. result |= components(f.base, x)
  62. if not f.exp.is_Integer:
  63. if f.exp.is_Rational:
  64. result.add(f.base**Rational(1, f.exp.q))
  65. else:
  66. result |= components(f.exp, x) | {f}
  67. else:
  68. for g in f.args:
  69. result |= components(g, x)
  70. return result
  71. # name -> [] of symbols
  72. _symbols_cache: dict[str, list[Dummy]] = {}
  73. # NB @cacheit is not convenient here
  74. def _symbols(name, n):
  75. """get vector of symbols local to this module"""
  76. try:
  77. lsyms = _symbols_cache[name]
  78. except KeyError:
  79. lsyms = []
  80. _symbols_cache[name] = lsyms
  81. while len(lsyms) < n:
  82. lsyms.append( Dummy('%s%i' % (name, len(lsyms))) )
  83. return lsyms[:n]
  84. def heurisch_wrapper(f, x, rewrite=False, hints=None, mappings=None, retries=3,
  85. degree_offset=0, unnecessary_permutations=None,
  86. _try_heurisch=None):
  87. """
  88. A wrapper around the heurisch integration algorithm.
  89. Explanation
  90. ===========
  91. This method takes the result from heurisch and checks for poles in the
  92. denominator. For each of these poles, the integral is reevaluated, and
  93. the final integration result is given in terms of a Piecewise.
  94. Examples
  95. ========
  96. >>> from sympy import cos, symbols
  97. >>> from sympy.integrals.heurisch import heurisch, heurisch_wrapper
  98. >>> n, x = symbols('n x')
  99. >>> heurisch(cos(n*x), x)
  100. sin(n*x)/n
  101. >>> heurisch_wrapper(cos(n*x), x)
  102. Piecewise((sin(n*x)/n, Ne(n, 0)), (x, True))
  103. See Also
  104. ========
  105. heurisch
  106. """
  107. from sympy.solvers.solvers import solve, denoms
  108. f = sympify(f)
  109. if not f.has_free(x):
  110. return f*x
  111. res = heurisch(f, x, rewrite, hints, mappings, retries, degree_offset,
  112. unnecessary_permutations, _try_heurisch)
  113. if not isinstance(res, Basic):
  114. return res
  115. # We consider each denominator in the expression, and try to find
  116. # cases where one or more symbolic denominator might be zero. The
  117. # conditions for these cases are stored in the list slns.
  118. #
  119. # Since denoms returns a set we use ordered. This is important because the
  120. # ordering of slns determines the order of the resulting Piecewise so we
  121. # need a deterministic order here to make the output deterministic.
  122. slns = []
  123. for d in ordered(denoms(res)):
  124. try:
  125. slns += solve([d], dict=True, exclude=(x,))
  126. except NotImplementedError:
  127. pass
  128. if not slns:
  129. return res
  130. slns = list(uniq(slns))
  131. # Remove the solutions corresponding to poles in the original expression.
  132. slns0 = []
  133. for d in denoms(f):
  134. try:
  135. slns0 += solve([d], dict=True, exclude=(x,))
  136. except NotImplementedError:
  137. pass
  138. slns = [s for s in slns if s not in slns0]
  139. if not slns:
  140. return res
  141. if len(slns) > 1:
  142. eqs = []
  143. for sub_dict in slns:
  144. eqs.extend([Eq(key, value) for key, value in sub_dict.items()])
  145. slns = solve(eqs, dict=True, exclude=(x,)) + slns
  146. # For each case listed in the list slns, we reevaluate the integral.
  147. pairs = []
  148. for sub_dict in slns:
  149. expr = heurisch(f.subs(sub_dict), x, rewrite, hints, mappings, retries,
  150. degree_offset, unnecessary_permutations,
  151. _try_heurisch)
  152. cond = And(*[Eq(key, value) for key, value in sub_dict.items()])
  153. generic = Or(*[Ne(key, value) for key, value in sub_dict.items()])
  154. if expr is None:
  155. expr = integrate(f.subs(sub_dict),x)
  156. pairs.append((expr, cond))
  157. # If there is one condition, put the generic case first. Otherwise,
  158. # doing so may lead to longer Piecewise formulas
  159. if len(pairs) == 1:
  160. pairs = [(heurisch(f, x, rewrite, hints, mappings, retries,
  161. degree_offset, unnecessary_permutations,
  162. _try_heurisch),
  163. generic),
  164. (pairs[0][0], True)]
  165. else:
  166. pairs.append((heurisch(f, x, rewrite, hints, mappings, retries,
  167. degree_offset, unnecessary_permutations,
  168. _try_heurisch),
  169. True))
  170. return Piecewise(*pairs)
  171. class BesselTable:
  172. """
  173. Derivatives of Bessel functions of orders n and n-1
  174. in terms of each other.
  175. See the docstring of DiffCache.
  176. """
  177. def __init__(self):
  178. self.table = {}
  179. self.n = Dummy('n')
  180. self.z = Dummy('z')
  181. self._create_table()
  182. def _create_table(t):
  183. table, n, z = t.table, t.n, t.z
  184. for f in (besselj, bessely, hankel1, hankel2):
  185. table[f] = (f(n-1, z) - n*f(n, z)/z,
  186. (n-1)*f(n-1, z)/z - f(n, z))
  187. f = besseli
  188. table[f] = (f(n-1, z) - n*f(n, z)/z,
  189. (n-1)*f(n-1, z)/z + f(n, z))
  190. f = besselk
  191. table[f] = (-f(n-1, z) - n*f(n, z)/z,
  192. (n-1)*f(n-1, z)/z - f(n, z))
  193. for f in (jn, yn):
  194. table[f] = (f(n-1, z) - (n+1)*f(n, z)/z,
  195. (n-1)*f(n-1, z)/z - f(n, z))
  196. def diffs(t, f, n, z):
  197. if f in t.table:
  198. diff0, diff1 = t.table[f]
  199. repl = [(t.n, n), (t.z, z)]
  200. return (diff0.subs(repl), diff1.subs(repl))
  201. def has(t, f):
  202. return f in t.table
  203. _bessel_table = None
  204. class DiffCache:
  205. """
  206. Store for derivatives of expressions.
  207. Explanation
  208. ===========
  209. The standard form of the derivative of a Bessel function of order n
  210. contains two Bessel functions of orders n-1 and n+1, respectively.
  211. Such forms cannot be used in parallel Risch algorithm, because
  212. there is a linear recurrence relation between the three functions
  213. while the algorithm expects that functions and derivatives are
  214. represented in terms of algebraically independent transcendentals.
  215. The solution is to take two of the functions, e.g., those of orders
  216. n and n-1, and to express the derivatives in terms of the pair.
  217. To guarantee that the proper form is used the two derivatives are
  218. cached as soon as one is encountered.
  219. Derivatives of other functions are also cached at no extra cost.
  220. All derivatives are with respect to the same variable `x`.
  221. """
  222. def __init__(self, x):
  223. self.cache = {}
  224. self.x = x
  225. global _bessel_table
  226. if not _bessel_table:
  227. _bessel_table = BesselTable()
  228. def get_diff(self, f):
  229. cache = self.cache
  230. if f in cache:
  231. pass
  232. elif (not hasattr(f, 'func') or
  233. not _bessel_table.has(f.func)):
  234. cache[f] = cancel(f.diff(self.x))
  235. else:
  236. n, z = f.args
  237. d0, d1 = _bessel_table.diffs(f.func, n, z)
  238. dz = self.get_diff(z)
  239. cache[f] = d0*dz
  240. cache[f.func(n-1, z)] = d1*dz
  241. return cache[f]
  242. def heurisch(f, x, rewrite=False, hints=None, mappings=None, retries=3,
  243. degree_offset=0, unnecessary_permutations=None,
  244. _try_heurisch=None):
  245. """
  246. Compute indefinite integral using heuristic Risch algorithm.
  247. Explanation
  248. ===========
  249. This is a heuristic approach to indefinite integration in finite
  250. terms using the extended heuristic (parallel) Risch algorithm, based
  251. on Manuel Bronstein's "Poor Man's Integrator".
  252. The algorithm supports various classes of functions including
  253. transcendental elementary or special functions like Airy,
  254. Bessel, Whittaker and Lambert.
  255. Note that this algorithm is not a decision procedure. If it isn't
  256. able to compute the antiderivative for a given function, then this is
  257. not a proof that such a functions does not exist. One should use
  258. recursive Risch algorithm in such case. It's an open question if
  259. this algorithm can be made a full decision procedure.
  260. This is an internal integrator procedure. You should use top level
  261. 'integrate' function in most cases, as this procedure needs some
  262. preprocessing steps and otherwise may fail.
  263. Specification
  264. =============
  265. heurisch(f, x, rewrite=False, hints=None)
  266. where
  267. f : expression
  268. x : symbol
  269. rewrite -> force rewrite 'f' in terms of 'tan' and 'tanh'
  270. hints -> a list of functions that may appear in anti-derivate
  271. - hints = None --> no suggestions at all
  272. - hints = [ ] --> try to figure out
  273. - hints = [f1, ..., fn] --> we know better
  274. Examples
  275. ========
  276. >>> from sympy import tan
  277. >>> from sympy.integrals.heurisch import heurisch
  278. >>> from sympy.abc import x, y
  279. >>> heurisch(y*tan(x), x)
  280. y*log(tan(x)**2 + 1)/2
  281. See Manuel Bronstein's "Poor Man's Integrator":
  282. References
  283. ==========
  284. .. [1] https://www-sop.inria.fr/cafe/Manuel.Bronstein/pmint/index.html
  285. For more information on the implemented algorithm refer to:
  286. .. [2] K. Geddes, L. Stefanus, On the Risch-Norman Integration
  287. Method and its Implementation in Maple, Proceedings of
  288. ISSAC'89, ACM Press, 212-217.
  289. .. [3] J. H. Davenport, On the Parallel Risch Algorithm (I),
  290. Proceedings of EUROCAM'82, LNCS 144, Springer, 144-157.
  291. .. [4] J. H. Davenport, On the Parallel Risch Algorithm (III):
  292. Use of Tangents, SIGSAM Bulletin 16 (1982), 3-6.
  293. .. [5] J. H. Davenport, B. M. Trager, On the Parallel Risch
  294. Algorithm (II), ACM Transactions on Mathematical
  295. Software 11 (1985), 356-362.
  296. See Also
  297. ========
  298. sympy.integrals.integrals.Integral.doit
  299. sympy.integrals.integrals.Integral
  300. sympy.integrals.heurisch.components
  301. """
  302. f = sympify(f)
  303. # There are some functions that Heurisch cannot currently handle,
  304. # so do not even try.
  305. # Set _try_heurisch=True to skip this check
  306. if _try_heurisch is not True:
  307. if f.has(Abs, re, im, sign, Heaviside, DiracDelta, floor, ceiling, arg):
  308. return
  309. if not f.has_free(x):
  310. return f*x
  311. if not f.is_Add:
  312. indep, f = f.as_independent(x)
  313. else:
  314. indep = S.One
  315. rewritables = {
  316. (sin, cos, cot): tan,
  317. (sinh, cosh, coth): tanh,
  318. }
  319. if rewrite:
  320. for candidates, rule in rewritables.items():
  321. f = f.rewrite(candidates, rule)
  322. else:
  323. for candidates in rewritables.keys():
  324. if f.has(*candidates):
  325. break
  326. else:
  327. rewrite = True
  328. terms = components(f, x)
  329. dcache = DiffCache(x)
  330. if hints is not None:
  331. if not hints:
  332. a = Wild('a', exclude=[x])
  333. b = Wild('b', exclude=[x])
  334. c = Wild('c', exclude=[x])
  335. for g in set(terms): # using copy of terms
  336. if g.is_Function:
  337. if isinstance(g, li):
  338. M = g.args[0].match(a*x**b)
  339. if M is not None:
  340. terms.add( x*(li(M[a]*x**M[b]) - (M[a]*x**M[b])**(-1/M[b])*Ei((M[b]+1)*log(M[a]*x**M[b])/M[b])) )
  341. #terms.add( x*(li(M[a]*x**M[b]) - (x**M[b])**(-1/M[b])*Ei((M[b]+1)*log(M[a]*x**M[b])/M[b])) )
  342. #terms.add( x*(li(M[a]*x**M[b]) - x*Ei((M[b]+1)*log(M[a]*x**M[b])/M[b])) )
  343. #terms.add( li(M[a]*x**M[b]) - Ei((M[b]+1)*log(M[a]*x**M[b])/M[b]) )
  344. elif isinstance(g, exp):
  345. M = g.args[0].match(a*x**2)
  346. if M is not None:
  347. if M[a].is_positive:
  348. terms.add(erfi(sqrt(M[a])*x))
  349. else: # M[a].is_negative or unknown
  350. terms.add(erf(sqrt(-M[a])*x))
  351. M = g.args[0].match(a*x**2 + b*x + c)
  352. if M is not None:
  353. if M[a].is_positive:
  354. terms.add(sqrt(pi/4*(-M[a]))*exp(M[c] - M[b]**2/(4*M[a]))*
  355. erfi(sqrt(M[a])*x + M[b]/(2*sqrt(M[a]))))
  356. elif M[a].is_negative:
  357. terms.add(sqrt(pi/4*(-M[a]))*exp(M[c] - M[b]**2/(4*M[a]))*
  358. erf(sqrt(-M[a])*x - M[b]/(2*sqrt(-M[a]))))
  359. M = g.args[0].match(a*log(x)**2)
  360. if M is not None:
  361. if M[a].is_positive:
  362. terms.add(erfi(sqrt(M[a])*log(x) + 1/(2*sqrt(M[a]))))
  363. if M[a].is_negative:
  364. terms.add(erf(sqrt(-M[a])*log(x) - 1/(2*sqrt(-M[a]))))
  365. elif g.is_Pow:
  366. if g.exp.is_Rational and g.exp.q == 2:
  367. M = g.base.match(a*x**2 + b)
  368. if M is not None and M[b].is_positive:
  369. if M[a].is_positive:
  370. terms.add(asinh(sqrt(M[a]/M[b])*x))
  371. elif M[a].is_negative:
  372. terms.add(asin(sqrt(-M[a]/M[b])*x))
  373. M = g.base.match(a*x**2 - b)
  374. if M is not None and M[b].is_positive:
  375. if M[a].is_positive:
  376. dF = 1/sqrt(M[a]*x**2 - M[b])
  377. F = log(2*sqrt(M[a])*sqrt(M[a]*x**2 - M[b]) + 2*M[a]*x)/sqrt(M[a])
  378. dcache.cache[F] = dF # hack: F.diff(x) doesn't automatically simplify to f
  379. terms.add(F)
  380. elif M[a].is_negative:
  381. terms.add(-M[b]/2*sqrt(-M[a])*
  382. atan(sqrt(-M[a])*x/sqrt(M[a]*x**2 - M[b])))
  383. else:
  384. terms |= set(hints)
  385. for g in set(terms): # using copy of terms
  386. terms |= components(dcache.get_diff(g), x)
  387. # XXX: The commented line below makes heurisch more deterministic wrt
  388. # PYTHONHASHSEED and the iteration order of sets. There are other places
  389. # where sets are iterated over but this one is possibly the most important.
  390. # Theoretically the order here should not matter but different orderings
  391. # can expose potential bugs in the different code paths so potentially it
  392. # is better to keep the non-determinism.
  393. #
  394. # terms = list(ordered(terms))
  395. # TODO: caching is significant factor for why permutations work at all. Change this.
  396. V = _symbols('x', len(terms))
  397. # sort mapping expressions from largest to smallest (last is always x).
  398. mapping = list(reversed(list(zip(*ordered( #
  399. [(a[0].as_independent(x)[1], a) for a in zip(terms, V)])))[1])) #
  400. rev_mapping = {v: k for k, v in mapping} #
  401. if mappings is None: #
  402. # optimizing the number of permutations of mapping #
  403. assert mapping[-1][0] == x # if not, find it and correct this comment
  404. unnecessary_permutations = [mapping.pop(-1)]
  405. # permute types of objects
  406. types = defaultdict(list)
  407. for i in mapping:
  408. e, _ = i
  409. types[type(e)].append(i)
  410. mapping = [types[i] for i in types]
  411. def _iter_mappings():
  412. for i in permutations(mapping):
  413. # make the expression of a given type be ordered
  414. yield [j for i in i for j in ordered(i)]
  415. mappings = _iter_mappings()
  416. else:
  417. unnecessary_permutations = unnecessary_permutations or []
  418. def _substitute(expr):
  419. return expr.subs(mapping)
  420. for mapping in mappings:
  421. mapping = list(mapping)
  422. mapping = mapping + unnecessary_permutations
  423. diffs = [ _substitute(dcache.get_diff(g)) for g in terms ]
  424. denoms = [ g.as_numer_denom()[1] for g in diffs ]
  425. if all(h.is_polynomial(*V) for h in denoms) and _substitute(f).is_rational_function(*V):
  426. denom = reduce(lambda p, q: lcm(p, q, *V), denoms)
  427. break
  428. else:
  429. if not rewrite:
  430. result = heurisch(f, x, rewrite=True, hints=hints,
  431. unnecessary_permutations=unnecessary_permutations)
  432. if result is not None:
  433. return indep*result
  434. return None
  435. numers = [ cancel(denom*g) for g in diffs ]
  436. def _derivation(h):
  437. return Add(*[ d * h.diff(v) for d, v in zip(numers, V) ])
  438. def _deflation(p):
  439. for y in V:
  440. if not p.has(y):
  441. continue
  442. if _derivation(p) is not S.Zero:
  443. c, q = p.as_poly(y).primitive()
  444. return _deflation(c)*gcd(q, q.diff(y)).as_expr()
  445. return p
  446. def _splitter(p):
  447. for y in V:
  448. if not p.has(y):
  449. continue
  450. if _derivation(y) is not S.Zero:
  451. c, q = p.as_poly(y).primitive()
  452. q = q.as_expr()
  453. h = gcd(q, _derivation(q), y)
  454. s = quo(h, gcd(q, q.diff(y), y), y)
  455. c_split = _splitter(c)
  456. if s.as_poly(y).degree() == 0:
  457. return (c_split[0], q * c_split[1])
  458. q_split = _splitter(cancel(q / s))
  459. return (c_split[0]*q_split[0]*s, c_split[1]*q_split[1])
  460. return (S.One, p)
  461. special = {}
  462. for term in terms:
  463. if term.is_Function:
  464. if isinstance(term, tan):
  465. special[1 + _substitute(term)**2] = False
  466. elif isinstance(term, tanh):
  467. special[1 + _substitute(term)] = False
  468. special[1 - _substitute(term)] = False
  469. elif isinstance(term, LambertW):
  470. special[_substitute(term)] = True
  471. F = _substitute(f)
  472. P, Q = F.as_numer_denom()
  473. u_split = _splitter(denom)
  474. v_split = _splitter(Q)
  475. polys = set(list(v_split) + [ u_split[0] ] + list(special.keys()))
  476. s = u_split[0] * Mul(*[ k for k, v in special.items() if v ])
  477. polified = [ p.as_poly(*V) for p in [s, P, Q] ]
  478. if None in polified:
  479. return None
  480. #--- definitions for _integrate
  481. a, b, c = [ p.total_degree() for p in polified ]
  482. poly_denom = (s * v_split[0] * _deflation(v_split[1])).as_expr()
  483. def _exponent(g):
  484. if g.is_Pow:
  485. if g.exp.is_Rational and g.exp.q != 1:
  486. if g.exp.p > 0:
  487. return g.exp.p + g.exp.q - 1
  488. else:
  489. return abs(g.exp.p + g.exp.q)
  490. else:
  491. return 1
  492. elif not g.is_Atom and g.args:
  493. return max(_exponent(h) for h in g.args)
  494. else:
  495. return 1
  496. A, B = _exponent(f), a + max(b, c)
  497. if A > 1 and B > 1:
  498. monoms = tuple(ordered(itermonomials(V, A + B - 1 + degree_offset)))
  499. else:
  500. monoms = tuple(ordered(itermonomials(V, A + B + degree_offset)))
  501. poly_coeffs = _symbols('A', len(monoms))
  502. poly_part = Add(*[ poly_coeffs[i]*monomial
  503. for i, monomial in enumerate(monoms) ])
  504. reducibles = set()
  505. for poly in ordered(polys):
  506. coeff, factors = factor_list(poly, *V)
  507. reducibles.add(coeff)
  508. reducibles.update(fact for fact, mul in factors)
  509. def _integrate(field=None):
  510. atans = set()
  511. pairs = set()
  512. if field == 'Q':
  513. irreducibles = set(reducibles)
  514. else:
  515. setV = set(V)
  516. irreducibles = set()
  517. for poly in ordered(reducibles):
  518. zV = setV & set(iterfreeargs(poly))
  519. for z in ordered(zV):
  520. s = set(root_factors(poly, z, filter=field))
  521. irreducibles |= s
  522. break
  523. log_part, atan_part = [], []
  524. for poly in ordered(irreducibles):
  525. m = collect(poly, I, evaluate=False)
  526. y = m.get(I, S.Zero)
  527. if y:
  528. x = m.get(S.One, S.Zero)
  529. if x.has(I) or y.has(I):
  530. continue # nontrivial x + I*y
  531. pairs.add((x, y))
  532. irreducibles.remove(poly)
  533. while pairs:
  534. x, y = pairs.pop()
  535. if (x, -y) in pairs:
  536. pairs.remove((x, -y))
  537. # Choosing b with no minus sign
  538. if y.could_extract_minus_sign():
  539. y = -y
  540. irreducibles.add(x*x + y*y)
  541. atans.add(atan(x/y))
  542. else:
  543. irreducibles.add(x + I*y)
  544. B = _symbols('B', len(irreducibles))
  545. C = _symbols('C', len(atans))
  546. # Note: the ordering matters here
  547. for poly, b in reversed(list(zip(ordered(irreducibles), B))):
  548. if poly.has(*V):
  549. poly_coeffs.append(b)
  550. log_part.append(b * log(poly))
  551. for poly, c in reversed(list(zip(ordered(atans), C))):
  552. if poly.has(*V):
  553. poly_coeffs.append(c)
  554. atan_part.append(c * poly)
  555. # TODO: Currently it's better to use symbolic expressions here instead
  556. # of rational functions, because it's simpler and FracElement doesn't
  557. # give big speed improvement yet. This is because cancellation is slow
  558. # due to slow polynomial GCD algorithms. If this gets improved then
  559. # revise this code.
  560. candidate = poly_part/poly_denom + Add(*log_part) + Add(*atan_part)
  561. h = F - _derivation(candidate) / denom
  562. raw_numer = h.as_numer_denom()[0]
  563. # Rewrite raw_numer as a polynomial in K[coeffs][V] where K is a field
  564. # that we have to determine. We can't use simply atoms() because log(3),
  565. # sqrt(y) and similar expressions can appear, leading to non-trivial
  566. # domains.
  567. syms = set(poly_coeffs) | set(V)
  568. non_syms = set()
  569. def find_non_syms(expr):
  570. if expr.is_Integer or expr.is_Rational:
  571. pass # ignore trivial numbers
  572. elif expr in syms:
  573. pass # ignore variables
  574. elif not expr.has_free(*syms):
  575. non_syms.add(expr)
  576. elif expr.is_Add or expr.is_Mul or expr.is_Pow:
  577. list(map(find_non_syms, expr.args))
  578. else:
  579. # TODO: Non-polynomial expression. This should have been
  580. # filtered out at an earlier stage.
  581. raise PolynomialError
  582. try:
  583. find_non_syms(raw_numer)
  584. except PolynomialError:
  585. return None
  586. else:
  587. ground, _ = construct_domain(non_syms, field=True)
  588. coeff_ring = PolyRing(poly_coeffs, ground)
  589. ring = PolyRing(V, coeff_ring)
  590. try:
  591. numer = ring.from_expr(raw_numer)
  592. except ValueError:
  593. raise PolynomialError
  594. solution = solve_lin_sys(numer.coeffs(), coeff_ring, _raw=False)
  595. if solution is None:
  596. return None
  597. else:
  598. return candidate.xreplace(solution).xreplace(
  599. dict(zip(poly_coeffs, [S.Zero]*len(poly_coeffs))))
  600. if all(isinstance(_, Symbol) for _ in V):
  601. more_free = F.free_symbols - set(V)
  602. else:
  603. Fd = F.as_dummy()
  604. more_free = Fd.xreplace(dict(zip(V, (Dummy() for _ in V)))
  605. ).free_symbols & Fd.free_symbols
  606. if not more_free:
  607. # all free generators are identified in V
  608. solution = _integrate('Q')
  609. if solution is None:
  610. solution = _integrate()
  611. else:
  612. solution = _integrate()
  613. if solution is not None:
  614. antideriv = solution.subs(rev_mapping)
  615. antideriv = cancel(antideriv).expand()
  616. if antideriv.is_Add:
  617. antideriv = antideriv.as_independent(x)[1]
  618. return indep*antideriv
  619. else:
  620. if retries >= 0:
  621. result = heurisch(f, x, mappings=mappings, rewrite=rewrite, hints=hints, retries=retries - 1, unnecessary_permutations=unnecessary_permutations)
  622. if result is not None:
  623. return indep*result
  624. return None