(kill(all),
 reset (),
 load("abs_integrate.mac"),
 start : elapsed_real_time(),
 declare(signum,real),
 0);
0$

/* SF bug #2815: abs_integrate leads to infinite recursion
 * put this test first since leftover stuff in VARLIST seems to mask the bug
 */

integrate(log(1-x)*log(1+x)/(1+x),x,0,1);
(2*zeta(3)+log(2)^3-(%pi^2*log(2))/3)/2 -(7*zeta(3))/8-log(2)^3/6+(%pi^2*log(2))/12$

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));
(abs(x-5)*x)/2-(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$

(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))$

block([cntx : newcontext()],
  unwind_protect(
   (assume(x < a, x < b), factor(expand(diff(i,x) - abs((x-a)*(x-b)),0,0))),killcontext(cntx)));
0$

block([cntx : newcontext()],
  unwind_protect(
   (assume(x < a, x > b), factor(expand(diff(i,x) - abs((x-a)*(x-b)),0,0))),killcontext(cntx)));
0$

block([cntx : newcontext()],
  unwind_protect(
   (assume(x > a, x < b), factor(expand(diff(i,x) - abs((x-a)*(x-b)),0,0))),killcontext(cntx)));
0$

block([cntx : newcontext()],
  unwind_protect(
   (assume(x > a, x > b), factor(expand(diff(i,x) - abs((x-a)*(x-b)),0,0))),killcontext(cntx)));
0$

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);
((2-2*cos(x))*signum(x))/2$

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

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

signum_to_abs(integrate(max(x,2*x),x));
(x*abs(x))/4+(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);
(signum(x-1)*((2*x^3-3*x^2)/12+1/12)+1/12)*signum(x)+x^3/6+x^2/4$

block([cntx : newcontext()],
   unwind_protect((assume(0<x, x<1), factor(diff(expand(i,0,0),x)-max(x,x^2))), killcontext(cntx)));
0$

block([cntx : newcontext()],
   unwind_protect((assume(x>1), factor(diff(expand(i,0,0),x)-max(x,x^2))), killcontext(cntx)));
0$

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

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

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

(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)/2+x/2+b/2$

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

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

block([cntx : newcontext()],
   unwind_protect((assume(x > 1), expand(i)), killcontext(cntx)));
x-3/2$

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$

integrate(x^2*abs(x),x,-a,a);
(a^4*signum(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)/2+b/2-abs(a)/2-a/2$

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

diff(i,x);
f(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)$

block([cntx : newcontext()],
   unwind_protect((assume(-1<x,x<1), i : expand(i,0,0), ratsimp(diff(i,x))), killcontext(cntx)));
-((x^2-1)*%e^-x)$   

(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))/4-(3*log(5))/4+10$

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);
signum(x-1)*(log(x^2+1)/2-atan(x)-log(2)/2+%pi/4)$

[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$

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

signum_to_abs(integrate(diff(abs(x) * cos(x),x),x));
cos(x)*abs(x)$

integrate(abs(x) * exp(-abs(x-9)),x,minf,inf);
18 + 2 * exp(-9)$

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)$

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(x+3))/2$

block([cntx : newcontext()],
  unwind_protect(
      (assume(x > -3), ratsimp(demoivre(diff(e,x)))), killcontext(cntx)));
(5*sin(3*x))/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$

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-(signum(x-1)*(x-1))/2+2*signum(x-1)*(1/4-x/4)+2*signum(x-2)*((x-1)/4-1/4)$

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),declare(x, noninteger), 0);
0$

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

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

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$

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

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$

block([extra_integration_methods : endcons('hyper_int, extra_integration_methods)],
e : integrate(x^(a) * (1-x)^(b),x));
-((hypergeometric([-a,b+1],[b+2],1-x)*(1-x)^(b+1))/(b+1))$

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)$

(assume(a > 0) , unwind_protect(integrate(exp(-x) * (signum(x-a) + 1),x,minf,inf), forget(a > 0)));
2 * exp(-a)$

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

integrate(1/(1+abs(x)),x,-a,a),ratsimp;
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)$

/*
 * These no longer work after fixing bug #4265
 
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, 2^(1-b)*hypergeometric([b/2],[b,(b+2)/2],-a^2*x^2/4)*(a*x)^b/(a*gamma(b+1)),bessel_j(b-1,0)*x)$

/*
 * These no longer work after fixing bug #4265

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)$

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

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$

(i : integrate(sqrt(max(5,x)),x),0);
0$

ratsimp(subst(x=6,i) - subst(x=5,i) - integrate(sqrt(x),x,5,6));
0$

radcan(subst(x=5,i) - subst(x=0,i) - integrate(sqrt(5),x,0,5));
0$

/* abs_integrate causes stack overflow - ID: 3533723 */
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))-%i*x^2
                    -2*%i*li[2](%e^(%i*x))-2*%i*li[2](-%e^(%i*x)))
                    /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;

(remvalue(foo),0);
0$

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

/* a purely real answer is better, but this value isn't wrong.*/
integrate(sqrt(x+sqrt(x)),x,0,1);
log(sqrt(2)+1)/8-log(1-sqrt(2))/8+log(-1)/8+2^(3/2)/3-1/(3*2^(3/2))$

(print(facts(x)),0);
0$

(remove(x,noninteger),declare(kk,integer), 0);
0$

baz : expand(integrate(x*ceiling(x),x));
-ceiling(x)^3/6+ceiling(x)^2/4+x^2*ceiling(x)/2-ceiling(x)/12-1/2$

expand(limit(baz,x,kk,minus) - subst(x=kk,baz));
0$

expand(limit(baz,x,kk,plus) - subst(x=kk,baz));
0$

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

/* see SF bug 2853*/
integrate(log(abs(sin(x))),x);
x*log(abs(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))-%i*x^2
                         -2*%i*li[2](%e^(%i*x))-2*%i*li[2](-%e^(%i*x)))
                         /2$

block([ans : integrate(log(abs(sin(x))),x,0,%pi/2)], [ans, expand(ans)]);
[-((6*%pi*log(2)-%i*%pi^2)/12)-(%i*%pi^2)/12,-((%pi*log(2))/2)]$

/* Tests for January 2015 extensions. */

i : 'integrate(atan(tan(x)),x);
x^2/2-(%pi^2*((2*(x/%pi+1/2)-1)*floor(x/%pi+1/2)-floor(x/%pi+1/2)^2))/2$

ratsimp(subst(x=100*%pi + 2/3, diff(i,x)));
2/3$

subst(x=-2015*%pi + 2/3,diff(i,x));
2/3$

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

integrate(triangle_wave(x),x,0,1/2);
1/4$

integrate(triangle_wave(x),x,1/2,1);
-1/4$

integrate(triangle_wave(x),x,0, 42);
0$

integrate(triangle_wave(x),x,0, 42+1/5);
2/25$

(i : integrate(triangle_wave(x),x,0,z), i : diff(i,z) - block([z : mod(z,1)], abs(4*z-3)-abs(4*z-1) + 4*z - 2),
    setify(makelist(subst(z=(k+1/3)/10,i),k,0,29)));
set(0)$

(i : expand(integrate(sawtooth_wave(x),x)));
sawtooth_wave(x)^2/2-sawtooth_wave(x)/2+x/2$

(declare(ni, noninteger), expand(subst(x = ni, diff(i,x)),0,0));
sawtooth_wave(ni)$

expand(integrate(sawtooth_wave(x) - 2015,x));
sawtooth_wave(x)^2/2-sawtooth_wave(x)/2-4029*x/2$

(remvalue(i),remove(ni,noninteger),0);
0$

'integrate(abs(sin(x)),x);
2*floor(x/%pi)-cos(x)*cos(%pi*floor(x/%pi))$

'integrate(x*abs(sin(x^2)),x);
(2*floor(x^2/%pi)-cos(x^2)*cos(%pi*floor(x^2/%pi)))/2$
 
i : intfudu(Ci(x^2)*x,x);
(x^2*Ci(x^2)-sin(x^2))/2$

diff(i,x);
x*Ci(x^2)$

intfudu(bessel_j(1,log(x))/x,x);
-bessel_j(0,log(x))$

intfudu(cos(x)*sin(x),x);
sin(x)^2/2$

intfudu(cos(x)^2*sin(x),x);
 -cos(x)^3/3$
 
integrate(abs(sin(x)),x);
2*floor(x/%pi)-cos(x)*cos(%pi*floor(x/%pi))$

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

integrate(abs(sin(x)),x,0,%pi);
2$

integrate(abs(sin(x)),x,0,2*%pi);
4$

integrate(abs(sin(x)),x,0,-2*%pi);
-4$

integrate(abs(cos(x)),x,0,%pi);
2$

integrate(abs(cos(x)),x,0,2*%pi);
4$

integrate(abs(cos(x)),x,0,-2*%pi);
-4$

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

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

integrate(1/(1+abs(x)),x,-107,107);
(log(108)-log(106)-%i*%pi)/2+log(108)-((-log(108)+log(106)+%i*%pi)/2)+log(106)+%i*%pi$

/* bugs #3519 & #3641 */
integrate(abs(sin(x)),x,0,2*%pi);
4$

integrate(abs(sin(x)),x,0,-2*%pi);
-4$

integrate(abs(sin(x)),x,0,8*%pi);
16$

/* #3548 expand stops working after loading abs_integrate */

(unwind_protect ((assume(L>0), ii : (2*integrate((f(xvar)-B*xvar-A)*sin((%pi*(2*n-1)*xvar)/(2*L)),xvar,0,L))/L),
   (forget(L>0))));

(2*integrate((f(xvar)-B*xvar-A)*sin((%pi*(2*n-1)*xvar)/(2*L)),xvar,0,L))/L$

expand(ii);
(2*integrate(f(xvar)*sin((%pi*n*xvar)/L-(%pi*xvar)/(2*L))-B*xvar*sin((%pi*n*xvar)/L-(%pi*xvar)/(2*L))-A*sin((%pi*n*xvar)/L-(%pi*xvar)/(2*L)),xvar,0,L))/L$

(remvalue(ii),0);
0$

hypergeometric_simp(hyper_int(x*abs(x-a),x));
-(a*x^2*(1-(2*x)/(3*a))*abs(x-a))/(2*(x-a))$

/* #3863 Wrong result for abs_integrate applied to unit_step */
integrate( x*unit_step(x-1), x,0,2 ); 
3/2$

/* #3528 abs_integrate causes integral of sqrt(1+2*sin(x)^2) to give spurious results */
/* abs_integrate can't do this--perviously, the value was bogus, now we
   get a integral nounform.*/

(ii : integrate(sqrt(1+2*sin(x)^2), x),0);
0$

is(inpart(ii,0)= nounify('integrate));
true$

diff(ii,x);
sqrt(2*sin(x)^2+1)$

(remvalue(ii),0);
0$

/* #3442 stack overflow in integration with special settings */
(ii : block([
besselexpand : true,
display2d : false,
domain : complex,
keepfloat : true],
load(to_poly_solve),
load(simplify_sum),
load(abs_integrate),
load(diag),
integrate(x^m/sqrt(a + b*x^(2 - m)),x)),0);
0$

is(inpart(ii,0)= nounify('integrate));
true$

diff(ii,x);
x^m/sqrt(a + b*x^(2 - m))$

(remvalue(ii),0);
0$

/* #3237 regression on a simple integral in abs_integrate */
integrate(1 / (1 + abs(x-5)), x, -5, 6);
log(11) + log(2)$

/* #2520 abs_integrate fails on abs(sin(x)) and abs(cos(x)) */
 integrate(abs(sin(x)),x);
 2*floor(x/%pi)-cos(x)*cos(%pi*floor(x/%pi))$

 integrate(abs(sin(x)),x);
 2*floor(x/%pi)-cos(x)*cos(%pi*floor(x/%pi))$

 integrate(abs(sin(x)),x,0,2*%pi);
 4$

 integrate(abs(cos(x)),x,0,2*%pi);
 4$

integrate(abs(sin(x)),x,0,8*%pi);
 16$

 integrate(abs(cos(x)),x,0,8*%pi);
 16$

/* #3125 with abs_integrate, incorrect limit of expression containing signum */
/* Before Maxima gave a bogus result of 0, now it gives a limit nounform.*/

is(limit(2*((1/2-sin(x)/((cos(x)+1)*(sin(x)^2/(cos(x)+1)^2+1)))
  *signum(sin(x)/(cos(x)+1)-1)
  +1)*signum(sin(x)/(cos(x)+1)+1),x,%pi) # 0);
true$

/* #3122 substitution causes trouble with diff in integral, triggered by abs_integrate */
(declare(f,real), integrate(cos(x)^2*diff(f(x),x),x));
(integrate(cos(2*x)*('diff(f(x),x,1)),x)+f(x))/2$

(remove(f,real),0);
0$

/* #2859 Maxima should integrate abs(sin(x)) correctly */
integrate(abs(sin(x)),x,0,%pi);
2$

integrate(abs(cos(x)),x,0,%pi);
2$

/* #2853 abs_integrate hangs after encountering log(0) */
 ii : integrate(log(abs(sin(x))), x, 0, %pi/2);
 -((6*%pi*log(2)-%i*%pi^2)/12)-(%i*%pi^2)/12$

 expand(ii);
 -(%pi*log(2))/2$

 /* #2823 Yet another abs_integrate problem */
 integrate( sqrt((cos(x) - 1)^2 + sin(x)^2), x, 0, 2*%pi);
 8$

 /* #2815 abs_integrate leads to infinite recursion */
  integrate(log(1-x)*log(1+x)/(1+x),x,0,1);
  (2*zeta(3)+log(2)^3-(%pi^2*log(2))/3)/2-(7*zeta(3))/8-log(2)^3/6+(%pi^2*log(2))/12$

/* #2814 abs_integrate: integration of x*ceiling(x) */
  expand(integrate(x*ceiling(x),x));
  -ceiling(x)^3/6+ceiling(x)^2/4+(x^2*ceiling(x))/2-ceiling(x)/12-1/2$

/* #2781 integral triggers error on ECL+abs_integrate */
(ii : integrate(diff((tan(x)+x)*exp(tan(x)),x),x),0);
0$

/* of course a non-nounform is better, but a nounform is better
than an infinite loop.*/
is(inpart(ii,0)= nounify('integrate));
true$

(remvalue(ii),0);
0$ 

/* #2031 Operator ' does not work with integrate? */
is('integrate(x^3+3,x,1,5) # 168);
true$

/* \#4603 Control stack regression with abs_integrate / 5.48.0 */
/* Of course, a integrate nounform isn't ideal, but at least it isn't a stack 
overflow.*/
block([ans : integrate(sin(k*x)/x*erf(x^2),x,0,inf)],
   [verbify(inpart(ans,0)), args(ans)]);
['integrate,[(sin(k*x)*erf(x^2))/x,x,0,inf]]$


(print("time = ", elapsed_real_time () - start), 0);
0$

(kill(functions), kill(values),remove(x,noninteger,kk,integer), forget(signum,real),0);
0$ 

setify(map(lambda([q], if inpart(q,0)='kind then true else false), facts()));
set(true)$

contexts;
[initial,global]$