/******************************************************************************
  rtest_integrate_special.mac
  Integrals with special functions
******************************************************************************/

kill(all);
done;

(reset(domain), reset(radexpand), done);
done;

/* Integrals for the Incomplete Gamma function */

integrate(gamma_incomplete(a,z),z);
z*gamma_incomplete(a,z)-gamma_incomplete(a+1,z);

integrate(z^(v-1)*gamma_incomplete(a,z),z);
'integrate(z^(v-1)*gamma_incomplete(a,z),z);

/* Correct noun form if first argument is an expression. 
 * See bug report Bug ID: 2968174 - Integration of hypergeometric bug */
integrate(gamma_incomplete(2*a,z^2)*z^2,z);
'integrate(gamma_incomplete(2*a,z^2)*z^2,z);

/* Integrals for Exponential Integral E(v,z) */

integrate(expintegral_e(v,a*z),z);
-expintegral_e(v+1,a*z)/a;

integrate(expintegral_e(v,z),z);
-expintegral_e(v+1,z);

integrate(z^(p-1)*expintegral_e(v,a*z),z);
'integrate(z^(p-1)*expintegral_e(v,a*z),z);

/* Integrals for Exponential Integral E1(z) */

integrate(expintegral_e1(a*z),z);
-expintegral_e(2,a*z)/a;

integrate(expintegral_e1(z),z);
-expintegral_e(2,z);

integrate(z^(p-1)*expintegral_e1(a*z),z);
'integrate(z^(p-1)*expintegral_e1(a*z),z);

/* Integrals for Exponential Integral Ei(z) */

integrate(expintegral_ei(z),z);
z*expintegral_ei(z)-%e^z;

integrate(expintegral_ei(a*z),z);
(a*z*expintegral_ei(a*z)-%e^(a*z))/a;

integrate(expintegral_ei(a*z+b),z);
((a*z+b)*expintegral_ei(a*z+b)-%e^(a*z+b))/a;

integrate(z^(p-1)*expintegral_ei(a*z),z);
'integrate(z^(p-1)*expintegral_ei(a*z),z);

integrate(z*expintegral_ei(a*z),z);
z^2*expintegral_ei(a*z)/2-(a*z-1)*%e^(a*z)/(2*a^2);

integrate(expintegral_ei(a*z)/z,z);
'integrate(expintegral_ei(a*z)/z,z);

integrate(expintegral_ei(a*z)/z^2,z);
gamma_incomplete(-1,-a*z)*a-expintegral_ei(a*z)/z;

integrate(expintegral_ei(a*z+b)/z^2,z);
'integrate(expintegral_ei(a*z+b)/z^2,z);

integrate(z^(p-1)*expintegral_ei(a*z^r),z);
'integrate(z^(p-1)*expintegral_ei(a*z^r),z);

integrate(z^n*%e^(b*z)*expintegral_ei(a*z),z);
'integrate(z^n*%e^(b*z)*expintegral_ei(a*z),z);

integrate(z*%e^(b*z)*expintegral_ei(a*z),z);
'integrate(z*%e^(b*z)*expintegral_ei(a*z),z);

integrate(z^2*%e^(b*z)*expintegral_ei(a*z),z);
'integrate(z^2*%e^(b*z)*expintegral_ei(a*z),z);

integrate(z^3*%e^(b*z)*expintegral_ei(a*z),z);
integrate(z^3*%e^(b*z)*expintegral_ei(a*z),z);

integrate(%e^(a*z)*expintegral_ei(a*z)/z,z);
1/2*expintegral_ei(a*z)^2;

/* More integrals after extending the integrator
   do look for argument substitution of special functions */

integrate(expintegral_ei(x^2),x);
x*expintegral_ei(x^2)+sqrt(%pi)*%i*erf(%i*x);

integrate(expintegral_ei(x^3),x);
x*expintegral_ei(x^3)-gamma_incomplete(1/3,-x^3);

integrate(expintegral_ei(x^-2),x);
%i*gamma_incomplete(-1/2,-1/x^2)*x/abs(x)+expintegral_ei(1/x^2)*x;

integrate(expintegral_ei(x^(1/2)),x);
2*(expintegral_ei(sqrt(x))*x/2-(sqrt(x)-1)*%e^sqrt(x)/2);

integrate(expintegral_ei(x^(-1/2)),x);
2*(expintegral_ei(1/sqrt(x))*x/2+gamma_incomplete(-2,-1/sqrt(x))/2);

integrate(expintegral_ei(x^(-3/4)),x);
4*(expintegral_ei(1/x^(3/4))*x/4+gamma_incomplete(-4/3,-1/x^(3/4))/4);

integrate(expintegral_ei((x+1)^(1/2)),x);
2*((x+1)*expintegral_ei(sqrt(x+1))/2-(sqrt(x+1)-1)*%e^sqrt(x+1)/2);

/* Integrals for Exponential Integral Li(z) */

integrate(expintegral_li(a*z),z);
(a*z*expintegral_li(a*z)-expintegral_ei(2*log(a*z)))/a;

integrate(expintegral_li(z),z);
z*expintegral_li(z)-expintegral_ei(2*log(z));

integrate(z*expintegral_li(z),z);
z^2*expintegral_li(z)/2+gamma_incomplete(0,-3*log(z))/2;

integrate(sqrt(z)*expintegral_li(z),z),logexpand;
2*(z^(3/2)*expintegral_li(z)/3+gamma_incomplete(0,-5/2*log(z))/3);

/* Integrals for Exponential Integral Si(z) */

integrate(expintegral_si(z),z);
z*expintegral_si(z)+cos(z);

integrate(z*expintegral_si(z),z);
z^2*expintegral_si(z)/2 - (sin(z)-z*cos(z)) /2;

/* Integrals for Exponential Integral Ci(z) */

integrate(expintegral_ci(z),z);
z*expintegral_ci(z)-sin(z);

integrate(z*expintegral_ci(z),z);
z^2*expintegral_ci(z)/2 - (z*sin(z)+cos(z)) /2;

/* Integrals for Exponential Integral Shi(z) */

integrate(expintegral_shi(z),z);
z*expintegral_shi(z)-cosh(z);

integrate(z*expintegral_shi(z),z);
z^2*expintegral_shi(z)/2 - ((z-1)*%e^z-(-z-1)*%e^(-z))/4;

/* Integrals for Exponential Integral Chi(z) */

integrate(expintegral_chi(z),z);
z*expintegral_chi(z)-sinh(z);

integrate(z*expintegral_chi(z),z);
z^2*expintegral_chi(z)/2 - ((z-1)*%e^z+(-z-1)*%e^(-z))/4;

/* Integrals of Lambert W function.  This checks that other parts
   of the integration routines work with the 'integral property */

/* This is tested in rtest_lambert_w.mac */
/* integrate(lambert_w(x),x);
x*(lambert_w(x)^2-lambert_w(x)+1)/lambert_w(x); */

integrate(lambert_w(a*x+b),x);
(a*x+b)*(lambert_w(a*x+b)^2-lambert_w(a*x+b)+1)/(a*lambert_w(a*x+b));

integrate(sin(x)*lambert_w(cos(x)),x);
-(cos(x)*(lambert_w(cos(x))^2-lambert_w(cos(x))+1)/lambert_w(cos(x)));

integrate(diff(f(x),x)*lambert_w(f(x)),x);
 f(x)*(lambert_w(f(x))^2-lambert_w(f(x))+1)/lambert_w(f(x));

/* Integrals where the property is a function */
integrate(bessel_j(0,x),x);
x*(bessel_j(0,x)*(2-%pi*struve_h(1,x))+%pi*bessel_j(1,x)*struve_h(0,x))/2;

integrate(bessel_j(1,x),x);
-bessel_j(0,x);

integrate(bessel_j(2,x),x);
x^3/24*hypergeometric([3/2],[5/2,3],-x^2/4);

integrate(bessel_j(n,x),n);
'integrate(bessel_j(n,x),n);

/* Examples of a subscripted function */
integrate(psi[0](x),x);
log_gamma(x);

integrate(psi[1](x),x);
psi[0](x);
