Integration in terms of exponential and incomplete gamma functions

Waldemar Hebisch Wrocªaw University [email protected]

13 July 2016 Introduction

What is FriCAS?

I FriCAS is an advanced computer algebra system

I forked from Axiom in 2007

I about 30% of mathematical code is new compared to Axiom

I about 200000 lines of mathematical code

I math code written in Spad (high level strongly typed language very similar to FriCAS interactive language)

I runtime system currently based on Lisp Some functionality added to FriCAS after fork:

I several improvements to integrator

I limits via Gruntz algorithm

I knows about most classical

I guessing package

I package for computations in quantum probability

I noncommutative Groebner bases

I asymptotically fast arbitrary precision computation of elliptic functions and elliptic integrals

I new user interfaces (Emacs mode and Texmacs interface) FriCAS inherited from Axiom good (probably the best at that time) implementation of (Bronstein, . . . )

I strong for elementary integration

I when needed special functions used pattern matching Part of motivation for current work came from Rubi testsuite: Rubi showed that there is a lot of functions which are integrable in terms of relatively few special functions.

I Adapting Rubi looked dicult

I Previous work on adding special functions to Risch integrator was widely considered impractical My conclusion: Need new algorithmic approach. Ex post I claim that for considered here class of functions extension to Risch algorithm is much more powerful than pattern matching. Informally Ei(u)0 = u0 exp(u)/u Γ(a, u)0 = u0um/k exp(−u) for a = (m + k)/k and −k < m < 0. Our extension is very natural: 1 0 log(x − x0) = x − x0 exp x x 0 ( − 0) Ei(x − x0) = x − x0 Exponential integral plays the same role for integrals containing as logarithm plays for rational functions! Incomplete Gamma functions is has related role: it allows integrating functions of form xk+m−1 exp(xk ). Simple examples

integrate(exp(x)/x^2, x)

−ex + x Ei (x) x Needs preprocessing (Hermite reduction) to see Ei term.

integrate((x-1)*exp(x)/x^2, x)

ex x Similar looking, but elementary. integrate(exp(x)/(x^2 - 2), x)

√  √ 2 √ Ei − 2 + x e 2 − Ei 2 + x √ √ 2 2 e 2 Single exponential and denominator irreducible over integers, but two Ei terms. x*exp(x^2)/(x^4-5)

√ √  √ 2 √ √ 5 Ei − 5 + x2 e 5 − 5 Ei 5 + x2 √ 20 e 5 Similar, but relation between complex factors of denominator and Ei terms is more complicated. integrate(((x+1)*exp(x))/log(x*exp(x)), x)

li (x ex ) This shows that result may appear as logarithmic integral. integrate(((x+1)*exp(x))/(x + log(x)), x)

li (x ex ) This shows that system can perform necessary simplications. integrate(exp(-x^2+2*x), x) √ e erf (x − 1) π 2 D(%, x)

e e−x2+2 x−1 erf term with a shift. integrate((2*x+4)*exp(-x^2-2*x-1)/(x+1), x) √ erf (x + 1) π + Ei −x2 − 2 x − 1 This shows that Ei and erf may be mixed together. integrate((1 + exp(x))*exp(exp(x)^2), x) √ π er (ex ) + Ei ex 2 2 Similar, but no denominator to signal presence of Ei term. Also, we get er when appropriate. integrate(x^3*exp(-x^3), x)

1 3 −x3 −Γ 3 , x − 3 x e 9 Gamma plus elementary part. integrate((x+1)*exp(-x^3-3*x^2-3*x), x)

2 3 2  e Γ 3 , x + 3 x + 3 x + 1 − 3 Gamma with a shift. integrate(x^2*exp(-(x+1)^3), x)

2 3 2  1 3 2  −x3−3 x2−3 x−1 2 Γ 3 , x + 3 x + 3 x + 1 − Γ 3 , x + 3 x + 3 x + 1 − e 3 Single term in the input, two Gamma terms plus elementary part in the output. integrate(sin(x+1)/x^2, x)

−2 sin (x + 1) − 2 x sin (1) Si (x) + x cos (1) Ci (x) + x cos (1) Ci (−x) 2 x Si and Ci terms with shift. integrate(x*sin(x^4), x)

 q  fresnelS x2 2 π q 2 2 π can generate Fresnel integrals. integrate(exp(x + sqrt(x+1)), x)

5 √ √ x+1+x √  2 x+1+1  2 e 4 e − π er 2 5 2 e 4 we can handle some algebraics by cheating (change of variable). f := (2*x^4-x^3+3*x^2+2*x+2)*exp(x/(x^2+2))/(x^3+2*x) integrate(f, x)

x  x  2  2 x + 2 e x +2 + Ei x2 + 2 Hard to separate elementary and Ei part. Peek inside )trace INTEF )math integrate(sin(x^2), x)

1exit ElementaryIntegration.lfintegrate,43 : +---+ | 2 fresnelS(x |--- ) \|%pi ------+---+ | 2 |--- \|%pi Theoretical background

If K is a dierential eld, v, u ∈ K and v 0 = u0/u we say that u is exponential element and that v is logarithmic element. The set of exponential elements in K is an abelian group with respect to multiplication. The set of logarithmic elements in K is an abelian group with respect to addition. Main examples:

I explicit exponential is exponential element

I argument of logarithm is exponential element k I if a is an exponential element, then a is also an exponential element Example: If K = F (θ), θ is Lambert W over F , that is θ is transcendental and there exist z F such that 0 z0θ , then ∈ θ = z(θ+1) θ/z is an exponential element in K and −θ is corresponding logarithmic element. Theorem If K is nitely generated dierential eld, then group of logarithmic elements modulo additive constants is a nite rank free abelian group. The same for group of exponential elements modulo multiplicative constants. In our elds we can eectively test if element is an exponential element and nd associated logarithmic element and vice versa. Gamma extensions

We say that a dierential eld E is a gamma extension of F if there exists θ1, . . . , θn ∈ E such that E = F (θ1, . . . , θn) and for each i, 1 ≤ i ≤ n, one of the following holds 1. θi is algebraic over Fi 0 θi 0 2. = u for some u ∈ Fi θi 0 u0 3. θi = u for some u ∈ Fi 0 u0 0 0 4. θi = v for some u, v ∈ Fi such that u = v u 0 0 5. there are w, u, v ∈ Fi and integers k, m such that θi = wu , u0 = v 0u, w k = v m, −k < m < 0.

where Fi = F (θ1, . . . , θi−i ). Liouville principle

Let f ∈ K(θ) where θ is a exponential or a primitive. Under appropriate technical assumptions, if f has integral in a Gamma extension of K(θ), then there is an algebraic extension K¯ of K such that v 0 u 0 X i X 0 i X 0 f = v0 + ci + ci vi + ci vi wi ui (1) vi vi i∈I1 i∈I2 i∈I3 0 0 where ci ∈ K are constants, ui = vi ui , vi ∈ K(θ) for i ∈ I2 ∪ I3, ui ∈ K(θ) for i ∈ I2, ui , wi ∈ K¯ (θ), wi ui ∈ K(θ) for i ∈ I3, ki mi wi = vi . The main case is integration of functions of form f θ where θ is an exponential. We get equation: u 0 X 0 i θ X 0 f θ = (gθ) + vi + vi wi ui θ vi i∈I2 i∈I3

0 0 where vi ui θ = (ui θ) . We will denote logarithm of θ as v, that is θ0 = v 0θ. Then we can write u 0 0 X 0 i X 0 f = g + v g + vi + vi wi ui (2) vi i∈I2 i∈I3

We will call terms of the sum over I2 Ei terms and terms of the sum over I3 gamma terms. Formula 2 generalises Risch dierential equation (RDE). Parametric RDE

Problem: given fi , u nd set of g, ci such that

0 X f0 = g + ug + ci fi

and ci are constants. We assume availability of solver for parametric RDE. In generic case we need to nd nite number of candidates for possible Ei and gamma terms and use solver for parametric RDE. In few special cases we have innite number of candidate Ei or gamma terms and determine which terms actually occur during solving RDE. The following cases may appear:

I regular Ei terms, depend only on denominator of f

I special Ei terms, depend only on structure of dierential eld

I innite family of Ei terms in case of v linear in logarithm

I regular gamma terms, depend only on structure of dierential eld

I innite families of gamma (erf) terms General setup

Let ψ be top transcendental in K and M be a dierential subeld such that K is algebraic over M(ψ). By assumption we can write vi = v + ri ψ + ai where ai ∈ M and ri ∈ Q 6= 0 only when ψ is a logarithm. When ψ is not an exponential let S be integral closure of M[ψ] in K. When ψ is an exponential let S be integral closure of M[ψ, ψ−1]. (In general we put inverses of all special elements in S). We can write elements of f as quotient with numerator in S and denominator in M[ψ]. When ψ is an exponential we additionally may assume that denominator is relatively prime to ψ. Notation:

vr,a = v + rψ + a

Nr,a is norm of numerator of vr,a. Lr is norm of numerator of ∂ψvr,a. P is norm of numerator of 2 v. ∂ψ Ei terms

Theorem If v 0 ui is an Ei term appearing in formula 2 and p is a prime divisor i vi such that ordp(vi ) > 0. Then ordp(E) > 0.

Corollary: Res(Nr,a, E) = 0. Problem: If ψ is a logarithm we need more equations. Solution: If denominator of v is divisible by two prime divisors we combine constraints from both divisors.

In transcendental case we just can use Res(Nr,a, E) = 0 and ∂ψRes(Nr,a, E) = 0. Ei cases

Algebraic constants √ √ √ √ exp(x)/(x2 −5) = e 5Ei(x − 5)0 −e− 5Ei(x + 5)0

In general, if p is irreducible polynomial with constant coecients, ci are roots of p, di are nonzero constants and Ei(φ)0 has denominator D, then 0 P di Ei(φ + ci ) has denominator Q(D + ci ). In our method we get polynomial equation q(a) = 0 for possible shifts. Each irreducible factor qi of q corresponds to orbit of single logarithmic element by algebraic shifts. The second highest coecient of qi gives trace ˜a of a. Shifting arguments of qi by −˜a we get equation for algebraic shifts. Factors in higher powers

x exp(log(x)2)/(log(x) + 1) = Ei((log(x) + 1)2)0/(2e)

extra equation comes from condition that vr,a is divisible by a square. Overlapped factors (x2(2 log(x)2 + 3 log(x)) + (2 log(x)2 + 5 log(x) + 2)) exp(log(x)2) = log(x)(log(x) + 1)(log(x) + 2) (Ei((log(x) + 1)(log(x) + 2))0/e2 + Ei(log(x)(log(x) + 1))0. Actually, no problem for our method but may confuse systems using partial fraction decomposition. Special terms

 ex 0  1 0 exp(exp(x)/(exp(x)+1)) = Ei −eEi − ex + 1 ex + 1

When ψ is an exponential than we check if v + a can reduce to a monomial.

exp(x − log(x)) = Ei(x)0

When ψ is a logarithm degree at innity can drop to 0. In transcendental case this can only happen if v is a polynomial of degree 1 in ψ with integer leading coecient. In such case we can reduce integral to integral with v ∈ M. In algebraic case there are nitely many values of r which may lead to zeros at innity, we need to handle them separately. Innite family exp((c + k) log(x)) xk−1 exp(c log(x)) Ei((c + k) log(x))0 = = x log(x) log(x) so we get innite family of Ei terms with the same denominator. Can happen only if v is linear in ψ. We can reduce the problem to f ∈ M and then to equation

X k f˜ = ck z with constant ck . Completing powers

To nd Gamma terms we search for r, v such that vr,a is a power modulo multiplication by exponentials. Main idea is that

exponentials have no nontrivial divisors, so that if vr,a is divisible by a nontrivial divisor p and vr,a is a k-th power, then k is divisible by pk . To test for divisibility by power of divisors we use GCD with 2 derivative. More precisely, if vr,a is divisible by p than Nr,a and Lr have common factor, that is Res(Nr,a, Lr ) = 0. This works if ψ is not a logarithm. For k ≥ 3 we get second equation using second derivative. For k = 2 (that is erf terms) we need to check for presence of two zeros of multiplicity 2. In case of single zero of multiplicity 2 there is possibility for innite family ("movable zero"). exp(−x2 − erf(x)2) 2 2 ψ = erf(x), va = −ψ − x + a, ∂ψva = −2ψ, 2 2 Res(va, ∂ψva) = 4(a − x ), Res(va, ∂ψva) = 0 i a = x , so 2 0 va = −ψ and the corresponding term is erf(erf(x)) . It may happen that there are no nontrivial zeros:

f = Γ(1/3, exp(x))0 = exp(− exp(x) + x/3).

We have u1 = exp(− exp(x)), v1 = − exp(x) and w1 = exp(x/3). Variation: f = erf(exp(3x/2))0 = exp(− exp(x)3 + 3x/2). The a term is uniquely determined by condition that v + a contains only one term. Consequently the gamma term is uniquely determined and can appear only if v is a monomial or a binomial. √ √ √ πerf( x)0 = exp(−x)/ x = exp(−x − log(x)/2) Non reduced case. Movable zeros

There are four cases where we get innite families divisible by a square of prime divisor: 2 I v = bψ + cψ + d is quadratic polynomial 2 p I v = bψ + c + dy where y = eψ + f 2 p 2 I v = bψ + c + dy where y = eψ + f ψ + g

I bψ2 + cψ + d v = ψ + e In integration context there are extra constraints and they eliminate the second family. For the three other families we show via examples that they may appear during integration. Innite families

√ πerf(log(x) + k)0 = 2x−1 exp(− log(x)2 − 2k log(x) − k2)

= 2 exp(−k2)x2k−1 exp(− log(x)2). This one is due to Cherry and he also gave method to handle it. √ k log(log(x)) + log(x)/2 πer( )0 = plog(log(x)) 2(log(x) + k) log(log(x)) − log(x) × 2x log(x) log(log(x))plog(log(x)) 4k2 log(log(x))2 + 4k log(x) log(log(x)) + log(x)2  exp = 4 log(log(x))

2 ((log(x) + k) log(log(x)) − log(x)/2) log(x)k −1xk−1×  log(x)2  exp − 3 log(log(log(x)))/2 4 log(log(x)) In general can happen when ψ is a logarithm and v = (bψ2 + cψ + d)/ψ − 3/2 log(ψ). Then b is a constant. We can reduce problem to

X 0 Aψ + B = vi wi ui . I3 This is the most complicated case. We get a quadratic relation between a and r. Depending on this relation we have further subcases:

I change of variables reduces problem to dierent case

I a is uniquely determined by r. Can use method similar to quadratic case P r I a is a constant. Need to solve C = dr z where dr are unknown constants and z and C are known

I can use one coordinate of a to parametrise solutions. Need to P r k solve C = dk z1z2 where dr are unknown constants, z1 and z2 are known and r is quadratic function of k. The last subcase is most tricky to solve. We use a single pole of z1 to get bound on k. More precisely, the problem is that there may r ki be two values of k1, k2 such that z1(ki )z2 have pole of the same maximal order and the constants are such that

d zr k zk1 d zr k zk2 k1 1( 1) 2 + k2 1( 2) 2 is of lower order. However we can look at highest order term in such combination  dierence between order of linear combination and order of terms is bounded by a constant. Since orders of terms grow quadratically there is gap between orders of consecutive terms growing linearly in |k|. So for |k| large enough the can be no cancellation between highest order term of linear combination above and lower order terms. Examples were known, but there were no theory how to handle them. √ If y = plog(x)2 − 1, φ = log(x) + y, v = y, γ = k2 − 1/k, then

√ k(φ + γ) πer( √ )0 = (kγ(log(x) − 1) − k(γ log(x) − 1)y) × φ

exp((2k2 − 1) log(x) + 2k2γ + y)/pφ =

2 (kγ(log(x) − 1) − k(γ log(x) − 1)y) x2k −1× exp(2k2γ) exp(y + log(φ)/2) so we get innite family of erf terms. The example above contains new constants γ and exp(2k2γ). exp(2k2γ) is a multiplicative factor so we can simply omit it. γ plays nontrivial role but taking trace with respect to γ we get example with rational coecients:

2 k(−1 + y)x2k −1 exp(y + log(φ)/2). In general we have v = bφ + c + dy + log(φ)/2 with y 2 = eψ2 + f ψ + g, φ = h(ψ + ty), et2 = 1, h ∈ M. Method of solving is similar to previous examples. In particular, innite family is possible only when b, d 2e and (f 2 − 4eg)/e2 are constants and we have linear relation between r and a0. Thank you