(kill(all), load("abs_integrate.mac"), load('hypergeometric), load('hyperint), start : elapsed_real_time (), 0);
0$

(extra_integration_methods : endcons('hyper_int, extra_integration_methods),0);
0$

integrate(abs(x),x);
(x*abs(x))/2$

signum_to_abs(%);
(x*abs(x))/2$

integrate(abs(sqrt(3)*x),x);
(sqrt(3)*x*abs(x))/2$

signum_to_abs(integrate(abs(x - 5),x));
((x-5)*abs(x-5))/2$

integrate(abs(%pi * x + 1),x);
((%pi*x+1)*abs(%pi*x+1))/(2*%pi)$

integrate(x*abs(x),x);
/*(x^3*signum(x))/3$
  A equivalent result because of rev. 1.63 of sin.lisp */
x^2*abs(x)/2-x^3*signum(x)/6;

(assume(n > 2), integrate(abs(x) * x^n,x));
(x^(n+2)*signum(x))/(n+2)$

(forget(n > 2),0);
0$

i : integrate(abs((x-a)*(x-b)),x);
signum(x-a)*(((2*x^3+(-3*b-3*a)*x^2+6*a*b*x)/6-(2*b^3+(-3*b-3*a)*b^2+6*a*b^2)/6)*signum(x-b)+((6*a^2*b+a^2*(-3*b-3*a)+2*a^3)/6-(2*b^3+(-3*b-3*a)*b^2+6*a*b^2)/6)*signum(b-a))$

unwind_protect((assume(notequal(x,a), notequal(x,b)), signum_to_abs(diff(i,x))), forget(notequal(x,a), notequal(x,b)));
abs((x-a)*(x-b))$

factor(signum_to_abs(integrate(abs((x-a)*(x-b)),x,a,b)));
(b-a)^3/6$

i : integrate(abs(x) + abs(1-x),x);
x*abs(x)/2+(x-1)*abs(x-1)/2$

factor(diff(i,x) - (abs(x) + abs(1-x)));
0$

(remvalue(i),0);
0$

integrate(signum(x),x);
abs(x)$

integrate(signum(x - 42),x);
abs(x-42)$

integrate(signum(x) * sin(x),x);
(1-cos(x))*signum(x)$

integrate(max(-x,x),x);
(x^2*signum(x))/2$

integrate(min(-x,x),x);
-(x^2*signum(x))/2$

signum_to_abs(integrate(max(x,2*x),x));
(x*abs(x)+3*x^2)/4$

integrate(x + max(x,0),x);
(x^2*signum(x))/4+(3*x^2)/4$

i : integrate(max(x,x^2),x);
(((2*x^3-3*x^2)/12+1/12)*signum(x-1)+1/12)*signum(x)+(x^3/3+x^2/2)/2$

factor(convert_to_signum(diff(i,x) - max(x,x^2)));
0$

limit(i,x,1, 'minus) - limit(i,x,1, 'plus);
0$

(remvalue(i,di),0);
0$

expand(integrate(unit_step(a * x),x));
(signum(a)*x*signum(x))/2+x/2$

(i : integrate(unit_step(a * x),x,-7,10),0);
0$

subst(a = -1,i);
7$

subst(a = 1,i);
10$

(remvalue(i),0);
0$

signum_to_abs(integrate(unit_step(x + b),x));
(abs(x+b)+x+b)/2$

ratsimp(integrate(unit_step(x) + cos(x^2) * x,x));
(sin(x^2)+abs(x)+x)/2$

integrate(unit_step(x^2-1),x);
((x/2-1/2)*signum(x-1)-1)*signum(x+1)+x/2$

almost_everywhere_simp(scanmap('factor, convert_to_signum(diff(%,x) - unit_step(x^2-1))));
0$ 

integrate(abs(x),x,1,2);
3/2$

signum_to_abs(integrate(abs(x),x,a,b));
(b*abs(b))/2-(a*abs(a))/2$

integrate(x*abs(x),x,-6,6);
0$

integrate(x*abs(x),x,-a,a);
0$

signum_to_abs(integrate(x^2*abs(x),x,-a,a));
(a^3*abs(a))/2$

integrate(unit_step(x),x,-1,0);
0$

integrate(unit_step(x),x,-1,1);
1$

signum_to_abs(integrate(unit_step(x),x,a,b));
(abs(b)+b)/2-(abs(a)+a)/2$

/* With hyper_int, we get an interesting result... */
i : block([extra_integration_methods : ['signum_int]], i : integrate(f(x) * abs(x-a),x), ev(i, f(x) := x, 'integrate));
x^2*abs(x-a)/2-(2*x^3/3-2*a^3/3)*signum(x-a)/4$

unwind_protect((assume(notequal(x,a)), factor(diff(signum_to_abs(i),x))), forget(notequal(x,a)));
x*abs(x-a)$

(i : integrate(f(x) * max(x,-x),x,0,1), ev(i, f(x) := x,integrate));
1/3$

subst([a=-1,b=2/3, c=1], integrate(abs((x-a)*(x-b)*(x-c)),x,b,c));
11/972$

i : integrate(abs(x^2-1)*x,x);
(x^2-1)*abs(x^2-1)/4$

factor(diff(i,x) - abs(x^2-1)*x);
0$

is(equal(limit(i,x,-1,'minus), limit(i,x,-1,'plus)));
true$

is(equal(limit(i,x,1,'minus), limit(i,x,1,'plus)));
true$

(i : integrate(abs(x^2-1)*exp(-x),x));
(((-x^2-2*x-2)*%e^-x+%e^-x+4*%e^-1)*signum(x-1)+4*%e^-1)*signum(x+1)$

unwind_protect((assume(notequal(x,-1), notequal(x,1)), signum_to_abs(diff(i,x))), forget(notequal(x,-1), notequal(x,1)));
%e^(-x)*abs(x-1)*abs(x+1)$

(remvalue(i), 0);
0$

integrate(1/(1 + abs(x)),x,-1,1);
2 * log(2)$

expand(integrate(exp(-abs(x)),x,-1,1));
2-2*%e^(-1)$

is(equal(integrate(exp(-abs(x)),x,-1,1), 2 * integrate(exp(-x),x,0,1)));
true$

integrate(abs(x-1) * (x + abs(x-5)),x,-7,9);
1280/3$

integrate(abs(x-1) / (x + abs(x-5)),x,-7,9);
(3*log(13)+18)/4-(3*log(5)+10)/4+8$

integrate(abs(x-1) / (x + abs(x-5)),x,9,-7) + integrate(abs(x-1) / (x + abs(x-5)),x,-7,9);
0$

nicedummies(integrate(signum(x) * f(x),x));
'integrate(signum(x) * f(x),x)$

nicedummies(integrate(signum(x-a) * f(x),x));
'integrate(signum(x-a) * f(x),x)$

is(equal(op(integrate(signum(x + sqrt(1-x)),x)), nounify('integrate)));
true$

i : integrate(abs(x-1)/ ( 1 + x^2),x);
/* (2*signum(x-1)*log(x^2+1)-4*x*signum(x-1)*atan(x)
     +(%pi-2*log(2))*signum(x-1))/4+abs(x-1)*atan(x)$
   A equivalent result because of rev. 1.63 of sin.lisp */
signum(x-1)*(2*log(x^2+1)-4*atan(x)-2*log(2)+%pi)/4
 +abs(x-1)*atan(x)+(2*x-2)*signum(x-1)*atan(x)/-2;

[unwind_protect((assume(x<1), ratsimp(diff(i,x))), forget(x < 1)), unwind_protect((assume(x>1), ratsimp(diff(i,x))), forget(x>1))];
[-((x-1)/(x^2+1)),(x-1)/(x^2+1)]$

integrate(exp(-abs(x)), x);
((-%e^x-%e^(-x)+2)*signum(x))/2+(%e^x-%e^(-x))/2$

integrate(cos(x + abs(x)),x);
((signum(x)+1)*sin(2*x)-2*x*signum(x)+2*x)/4$

/* We modify the following example because of revision 1.18 of risch.lisp
 * signum_to_abs(expand(integrate(diff(abs(x) * cos(x),x),x))); */
signum_to_abs(integrate(diff(abs(x) * cos(x),x),x)), gamma_expand:true;
cos(x)*abs(x)$

block([extra_integration_methods : ['signum_int]], integrate(abs(x) * exp(-abs(x-9)),x,minf,inf));
18 + 2 * exp(-9)$

block([extra_integration_methods : ['signum_int]], integrate(exp(-abs((x-1)*(x-7))),x,minf,inf));
-%e^(-9)*sqrt(%pi)*%i*erf(3*%i)-%e^9*sqrt(%pi)*erf(3)+%e^9*sqrt(%pi)$

block([extra_integration_methods : ['signum_int]], integrate(exp(-abs((x-1)*(x+1))),x,minf,inf));
-%e^(-1)*sqrt(%pi)*%i*erf(%i)-sqrt(%pi)*%e*erf(1)+sqrt(%pi)*%e$

integrate(1/(1 + abs(x))^2,x,minf,inf);
2$

e : integrate((5*sin(3*x)*signum(x+3))/x,x);

-(5 * (%i*gamma_incomplete(0, 3*%i*x) - %i*gamma_incomplete(0, -3*%i*x)
 + %i*gamma_incomplete(0, 9*%i) - %i*gamma_incomplete(0, -9*%i))*signum(3*x + 9))/2$

ratsimp(demoivre(diff(e,x)));
(5*sin(3*x)*signum(3*x+9))/x$

map('unit_step_mult_simp, [5,5.6, -28.6b0,%i, 1 - sqrt(5), 42]);
[5,5.6,-2.86b1,%i,1-sqrt(5),42]$

unit_step_mult_simp(unit_step(3*x));
unit_step(3*x)$

unit_step_mult_simp(unit_step(x) * unit_step(x - 1));
unit_step(x - 1)$

unit_step_mult_simp(1909 + unit_step(x) * unit_step(x - 1));
1909 + unit_step(x - 1)$

(i : integrate(1/(1 + abs(x) + abs(x-1)),x), sublis([x = 5], i) - sublis([x=-7], i));
log(16)/2+log(5)/2-log(2)/2+1/2$

(i : integrate(1/(1 + abs(x) + abs(x-1)),x,-7,5));
log(16)/2+log(5)/2-log(2)/2+1/2$

expand (integrate(exp(-abs(x)),x,-1,inf));
2-%e^-1$

(convolution(f,g,x) := block([t : gensym()],
 integrate(subst(x = t,f) * subst(x = x - t, g),t,minf,inf)),
unit_box(x) := (unit_step(x) - unit_step(x-1)),0);
0$ 

f1 : convolution(unit_box(x),unit_box(x),x);
(x*signum(x))/2-x*signum(x-1)+signum(x-1)+(x*signum(x-2))/2-signum(x-2)$

integrate(f1,x,minf,inf);
1$

subst(x = 1, f1);
1$

(declare(x,noninteger),0);
0$

ratexpand(integrate(floor(x),x));
 -floor(x)^2/2+x*floor(x)-floor(x)/2$

diff(%,x);
floor(x)$

integrate(x * floor(x^2),x);
(-floor(x^2)+2*x^2-1)*floor(x^2)/4$

ratexpand(integrate(ceiling(x),x));
-ceiling(x)^2/2+x*ceiling(x)+ceiling(x)/2$

diff(%,x);
ceiling(x)$

(assume(x > 0),0);
0$

ratexpand(integrate(xx*floor(xx),xx,0,x));
-(floor(x)^3/6)-(floor(x)^2/4)+x^2*floor(x)/2-(floor(x)/12)$

diff(%,x);
x * floor(x)$

(forget(x > 0),0);
0$

factor(integrate(x*floor(x)^2,x));
-((floor(x)*(3*floor(x)^3+4*floor(x)^2-6*x^2*floor(x)-1))/12)$

diff(%,x);
x*floor(x)^2$

integrate(%if(x < 0,-x,x),x,-5,z);
%if(-z>0,-z^2/2,z^2/2)+25/2$

[subst(z=-5,%),subst(z=0,%), subst(z=5,%)];
[0,25/2,25]$

integrate(%if(a # 0,-x,x),x);
%if(a#0,-x^2/2,x^2/2)$

integrate(%if(x < 1, -x, %if(x < 2, x, 2 * x)),x,0,z);
%if(-(z-1)>0,-z^2/2,%if(-(z-2)>0,z^2/2,z^2-2)-1)$

[subst(z = 0,%), subst(z=1,%), subst(z=2,%), subst(z=3,%)];
[0,-1/2,1,6]$

integrate(max(x^5-x^3,x*(x^4-q-y)*(1-x^2)),x);
'integrate(max(x^5-x^3,x*(x^4-q-y)*(1-x^2)),x)$

(remfunction(convolution, unit_box), remvalue(i,di,f1),0);
0$

e : integrate(x^(a) * (1-x)^(b),x);
-(hypergeometric([-a,b+1],[b+2],1-x)*(1-x)*%e^(a*log(x)+b*log(1-x)))/((b+1)*x^a)$

rat(hypergeometric_simp(subst([a=1,b=3], x^a * (1-x)^b - diff(e,x))),x);
''(rat(0))$

rat(hypergeometric_simp(subst([a=2,b=3], x^a * (1-x)^b - diff(e,x))),x);
''(rat(0))$

rat(hypergeometric_simp(subst([a=2,b=4], x^a * (1-x)^b - diff(e,x))),x);
''(rat(0))$

rat(hypergeometric_simp(subst([a=-4,b=3], x^a * (1-x)^b - diff(e,x))),x);
''(rat(0))$

/* maybe this should test for a constant, not specifically -4/15. */
ratsimp(hypergeometric_simp(hyper_int(sqrt(x)*(1-x),x) - integrate(sqrt(x)*(1-x),x)));
-4/15$

rat(hypergeometric_simp(hyper_int(5*x^14*(1-x^5)^3,x) - integrate(5*x^14*(1-x^5)^3,x)));
''(rat(0))$

e : hyper_int(4*x*sqrt(1-x^4),x);
2*hypergeometric([-1/2,1/2],[3/2],x^4)*x^2$

taylor(diff(e,x),x,0,15) - 4*x*sqrt(1-x^4);
''(taylor(0,x,0,15))$

e : hyper_int(7*x^(5/2)*sqrt(1-x^7),x);
2*hypergeometric([-1/2,1/2],[3/2],x^7)*x^(7/2)$

taylor(diff(e,x),x,0,15) - 7*x^(5/2)*sqrt(1-x^7);
''(taylor(0,x,0,15))$

rat(hypergeometric_simp(hyper_int(((x-1)*(x+1)*(x^2+1))/x^3,x)) - integrate(((x-1)*(x+1)*(x^2+1))/x^3,x));
''(rat(0))$

logcontract(hypergeometric_simp(hyper_int(-x/(x^2-2*x+1),x) - integrate(-x/(x^2-2*x+1),x)));
log(-1)+1$

logcontract(hypergeometric_simp(hyper_int(-x^2/(x^2-2*x+1),x) - integrate(-x^2/(x^2-2*x+1),x)));
1$

logcontract(hypergeometric_simp(hyper_int(-x^3/(x^2-2*x+1),x) - integrate(-x^3/(x^2-2*x+1),x)));
log(-1)+1$

(e : (3*x^2)/((x^3+41)^2*(x^3+42)^2),0);
0$

logcontract(hypergeometric_simp(hyper_int(e,x) - integrate(e,x)));
0$

(e : -(x+1)^2/(2*(x-1)^2*x^2),0);
0$

logcontract(hypergeometric_simp(hyper_int(e,x) - integrate(e,x)));
-((log(16)-3)/2)$

/* This test is very slow with integrate instead of hyper_int. */
hyper_int(sqrt(x) * (x^2 - x + 1)^(1/3) * (11*x^2 - 11)/((x^2+1)^(5/6) * (x^4 + 2*x^2+1)),x);
6*hypergeometric([-11/6,-1/3],[-5/6],(x^2+1)/x)*x^(11/6)/(x^2+1)^(11/6)$ 

hyper_int(4 * (1-x^3)^(1/3),x);
hypergeometric([2/3,4/3],[7/3],-((x-1)*(x^2+x+1))) *(x-1)*(x^2+x+1)*(1-x^3)^(1/3)$

integrate(exp(-x) * (signum(x-a) + 1),x,minf,inf);
2 * exp(-a)$

(assume(0 < a), 0);
0$

integrate(1/(1+abs(x)),x,-a,a);
2*log(a+1)$

(forget(0 < a),0);
0$

i : integrate(1/(1+abs(x)),x);
signum(x)*(log(x+1)+log(1-x))/2+(log(x+1)-log(1-x))/2$

[limit(i,x,-1,minus)-limit(i,x,-1,plus), limit(i,x,1,minus)-limit(i,x,1,plus)];
[0,0]$

integrate(1/ (1 + max(x^2,x)),x,-1,1);
2*(log(4)/2-log(2)/2)+%pi/4$

integrate(x/ (1 + max(x^2,1)^2),x,minf,inf);
0$

integrate(x/ (1 + max(x^2,1)^2),x,0,inf);
%pi/8 + 1/4$

integrate(mod(x,1),x);
x^2/2-(-floor(x)+2*x-1)*floor(x)/2$


/*  Regression tests for abs_integrate bug - ID: 2988544 */

integrate(signum(abs(x)),x,-2,2);
4$

integrate(signum(abs(x)),x,-2,0);
2$

integrate(signum(abs(x)),x,0,2);
2$

/* SF bug ID: 3034415; the antidervative is non-ideal, but at least it no longer signals an error. */

integrate(1/abs(x),x);
'integrate(1/abs(x),x)$

(extra_integration_methods : cons('conditional_integrate,extra_integration_methods),0);
0$

'integrate(cos(m*x),x);
%if(m#0,sin(m*x)/m,x)$

e : 'integrate(cos(m*x)*cos(2*m*x),x);
%if(m#0,(sin(3*m*x)+3*sin(m*x))/(6*m),x)$

e : sublis([x=%pi],e) - sublis([x=0],e);
%if(m#0,(sin(3*%pi*m)+3*sin(%pi*m))/(6*m),%pi)$

[sublis([m=1],e), sublis([m=0],e)];
[0,%pi]$

'integrate(abs(a*x+b),x);
%if(a # 0,(a*x+b)*abs(a*x+b)/(2*a),abs(b)*x)$

'integrate(exp(a*x^2+b*x),x);
%if(a#0,-((sqrt(%pi)*%e^(-b^2/(4*a))*erf((2*a*x+b)/(2*sqrt(-a))))/(2*sqrt(-a))),%if(b#0,%e^(b*x)/b,x))$

'integrate(bessel_j(b-1,a*x),x);
%if((a#0) %and (b#0),(2^(1-b)*hypergeometric([b/2],[b,(b+2)/2],-(a^2*x^2)/4)*(a*x)^b)/(a*b*gamma(b)),
%if((a#0) %and (b=0),%if(a#0,bessel_j(0,a*x)/a,0),%if((a=0) %and (b#0),bessel_j(b-1,0)*x,0)))$

ratexpand(sublis([m=42, n = 42], 'integrate(cos(m*x) * cos(n * x),x)));
sin(84*x)/168+x/2$

'integrate(sqrt(a^2 + x^2),x);
%if(a#0,(a^2*asinh(x/abs(a))+x*sqrt(x^2+a^2))/2,(x*abs(x))/2)$

block([domain : 'complex], 'integrate(1/sqrt(a^2 + x^2),x));
%if(a # 0,asinh(2*x/sqrt(4*a^2)),log(x))$

block([domain : 'complex], 'integrate(x^2*sqrt(a^2 - x^2),x));
%if(a # 0,(a^4*asin(2*x/sqrt(4*a^2))+sqrt(a^2-x^2)*(2*x^3-a^2*x))/8,x^3*sqrt(-x^2)/4)$

'integrate(1/((a+x) * (b+x)),x);
%if(b-a#0,-((log(x+b)-log(x+a))/(b-a)),-1/(x+b))$

'integrate(x^n,x);
%if(n+1#0,x^(n+1)/(n+1),log(x))$

'integrate(floor(a*x),x);
%if(a#0,-((floor(a*x)^2+(1-2*a*x)*floor(a*x))/(2*a)),0)$

'integrate(abs(1 - a*z),z);
%if(a#0,((a*z-1)*abs(a*z-1))/(2*a),z)$

integrate(unit_step(x),x,99,100);
1$

abs_defint(unit_step(x),x,99,100);
1$

abs_defint(unit_step(unit_step(x)),x,-1,0);
0$

integrate(unit_step(unit_step(x)),x,-1,0);
0$

integrate(signum(abs(x)),x,-2,2), prederror : false;
4$

integrate(sqrt(1+abs(x)),x);
(((2*(x+1)^(3/2))/3+(2*(1-x)^(3/2))/3-4/3)*signum(x))/2+((2*(x+1)^(3/2))/3-(2*(1-x)^(3/2))/3)/2$

integrate(sqrt(max(5,x)),x);
(((2*x^(3/2))/3-(sqrt(10)*x)/sqrt(2)+(5*sqrt(10))/sqrt(2)-(2*5^(3/2))/3)*signum(x-5))/2+((2*x^(3/2))/3+(sqrt(10)*x)/sqrt(2)-(5*sqrt(10))/sqrt(2)-(2*5^(3/2))/3)/2$

/* abs_integrate causes stack overflow - ID: 3533723 */
block([extra_integration_methods : ['intfudu, 'intfugudu, 'signum_int, 'abs_integrate_use_if, 'floor_int, 'if_int]],
   integrate(log(sin(x)),x));
x*log(sin(x))-(x*log(sin(x)^2+cos(x)^2+2*cos(x)+1)
              +x*log(sin(x)^2+cos(x)^2-2*cos(x)+1)
              +2*%i*x*atan2(sin(x),cos(x)+1)
              -2*%i*x*atan2(sin(x),1-cos(x))-2*%i*li[2](%e^(%i*x))
              -2*%i*li[2](-%e^(%i*x))-%i*x^2)
              /2$

/* SF bug #2557: "abs_integrate leaks assumptions into enclosing context" */

(kill (foo), foo : copy (facts ()), 0);
0;

defint(exp(-abs(x))/cosh(x),x,minf,inf);
2*log(2);

is (foo = facts ());
true;

defint(exp(-abs(x))/cosh(x),x,minf,inf);
2*log(2);

(print("time = ", elapsed_real_time () - start), is(elapsed_real_time () - start < 70));
true$

(remvalue(e,i,di,f1,start), remove(x,noninteger), remfunction(convolution, unit_box),0);
0$ 

