(kill(all),0);
0$

/*--- numbers ----*/

abs(-5);
5$

abs(0);
0$

abs(42);
42$

abs(-7/8);
7/8$

abs(27/5);
27/5$

abs(-2.4);
2.4$

abs(0.0);
0.0$

abs(6.022e23);
6.022e23;

abs(-6.022b23);
6.022b23$

abs(1066.0b3);
1066.0b3$

abs(inf);
inf$

abs(minf);
inf$

abs(infinity);
inf$

abs(und);
und$

abs(ind);
ind$

abs(%pi);
%pi$

abs(%phi);
%phi$

abs(%i);
1$

abs(1 + %i);
sqrt(2)$

abs(cos(1));
cos(1)$

abs((1 + %i) / (1-%i));
1$

abs((1 + %i) * (1-%i));
2$

/*--reflection simplification ----*/

abs(x) - abs(-x);
0$

abs(x*y) - abs(-x*y);
0$

abs(sin(x)) - abs(sin(-x));
0$

expand(abs(14 * a*(b - c)) - abs(-14 * a * (c - b)));
0$

ratsubst(c,b,abs(x*(a+b)) - abs(x*(a+c)));
0$

/*--database facts -----*/

(assume(equal(zip,0)),0);
0$

abs(zip * x);
0$

is(equal(abs(1 + zip),1));
true$

(forget(equal(zip,0)),0);
0$


/*--absolute value of products and quotients ---*/

abs((1+%i) * x);
sqrt(2) * abs(x)$

abs((1+%i) * %pi);
sqrt(2) * %pi$

abs((1+%i) / x);
sqrt(2) / abs(x)$

/*-- absolute value of lists and other mbags---*/

abs([a,b]), listarith : true;
[abs(a),abs(b)]$

abs([%i,-6]), listarith : true;
[1,6]$

abs(x = 1), listarith : true;
abs(x) = 1$

abs(-x = 1), listarith : true;
abs(x) = 1$


/*---- declared complex stuff ---*/

(declare(z, complex),0);
0$

abs(conjugate(z));
abs(z)$

abs(exp(z));
exp(realpart(z))$

subst(z = %i, abs(z^2));
1$

(remove(z,complex),0);
0$

/*--- abs and tellsimp ---- */

(tellsimp(abs(a), a+1), abs_rules : tellsimp(abs(b), b+1), 0);
0$

abs(a);
a+1$

abs(a * x);
(a+1) * abs(x)$

abs([a]), listarith : true;
[a+1]$

abs(a*b);
(a+1)*(b+1)$

abs(a^7);
(a+1)^7$

abs((a*b)^5);
(a+1)^5 * (b + 1)^5$

(remrule(abs, first(abs_rules)), remrule(abs, second(abs_rules)),0);
0$

(remvalue(abs_rules),0);
0$

/*--- abs flatten  ---*/

abs(abs(x));
abs(x)$

abs(abs(-x));
abs(x)$

abs(abs(abs(x)));
abs(x)$

abs(abs(abs(x))+1);
abs(abs(x) + 1)$

/*--- simplify noun form ---*/

'abs(1);
1$

'abs(1+x^2);
1 + x^2$

'abs(abs(x));
abs(x)$


/*-- subst into abs ---*/

ratsubst(x,x^2,abs(1+x^3));
1+x^2$

ratsubst(0,a,abs(%pi + a));
%pi$

/*-- CRE tests ---*/

abs(rat(x) + 1);
abs(1+x)$

abs(rat(1));
1$

/*-- misc -----*/

abs(signum(1/x));
1$

is(equal(op(abs(a.b)), abs));
true$

is(equal(op(abs(a^^7)), abs));
true$

abs(taylor(exp(x),x,0,3)) - taylor(exp(x),x,0,3);
''(taylor(0,x,0,3));

/* bug SF[2631766] abs((1+%i)^%i) is wrong */

abs((1+%i)^%i);
%e^(-%pi/4);

abs((sqrt(3)*%i/2-1/2)/(3*(%i/(6*sqrt(3))-1/6)^(1/3))
            +(%i/(6*sqrt(3))-1/6)^(1/3)*(-sqrt(3)*%i/2-1/2));

/*
sqrt(3*sin(5*%pi/18)^2-2*sqrt(3)*cos(5*%pi/18)*sin(5*%pi/18)
                            +cos(5*%pi/18)^2)
       /sqrt(3);
*/

sin(5*%pi/18)-cos(5*%pi/18)/sqrt(3);

/* Bug ID: 900860 - Simplifications involving abs
 */

/* Examples from the bug report */
abs(x)/x^2;
1/abs(x);

x^2/abs(x);
abs(x);

abs(x)^3;
x^2*abs(x);

/* More examples */
1/x^3*abs(x);
1/(x*abs(x));

1/abs(x)*x^3;
x*abs(x);

abs(x)^3/x^2;
abs(x);

abs(2*x+a)^3/(2*x+a)^2;
abs(2*x+a);

abs(log(x));
abs(log(x))$

subst(x=-1, abs(log(x))), lognegint : true;
%pi$

abs((-1)^x);
1$

(declare(z,complex), abs(log(-z)));
abs(log(-z))$

subst(z=-1, abs(log(-z)));
0$

(remove(z,complex),0);
0$

/* check sign before using multiplicative identity; from x*y > 0 */
(assume(x*y > 0), abs(x*y));
x*y$

abs(-x*y);
x*y$

(forget(x*y > 0),0)$
0$

/* check sign before using abs(x^n) = abs(x)^n. */

abs(x^2);
x^2$

abs(rat(a-b))-abs(rat(b-a));
0$

/* Bug #2230: abs(x)^(2*int) doesn't simplify */

(declare(n, integer, e, even), 0);
0$

abs(x)^(2*n);
x^(2*n)$

abs(x)^e;
x^e$

abs(x)^(2*floor(z));
x^(2*floor(z))$

(remove(n, integer, e, even), 0);
0$

/* SF bug #3152: "invalid simplification of abs(foo)^2 when foo is a complex expression"
 */

(kill(a, foo), declare(a, complex));
done;

domain:real;
real;

foo:abs((a + 1)^2);
abs(a + 1)^2;

foo, a=%i;
2;

abs(a^2);
abs(a)^2;

domain:complex;
complex;

foo:abs((a + 1)^2);
abs(a + 1)^2;

foo, a=%i;
2;

abs(a^2);
abs(a)^2;

/* SF bug #2901: "incorrect simplification of abs((a + b*%i)^2) with a, b real"
 * SF bug #2550: "abs((%i+x)^2) evaluates to (x + %i)^2"
 */

kill(x, y, a, b);
done;

domain:real;
real;

(abs(x + %i * y))^2;
abs(x + %i*y)^2;

abs((x + %i * y)^2);
abs(x + %i*y)^2;

(cabs(x + %i * y))^2;
x^2 + y^2;

cabs((x + %i * y)^2);
x^2 + y^2;

declare ([a, b], real);
done;

abs((a + b*%i)^2);
abs(a + b*%i)^2;

abs((%i+x)^2);
abs(%i + x)^2;

abs(%i^2);
1;

abs((%i+x)^2 + 1);
abs((%i + x)^2 + 1);

domain:complex;
complex;

(abs(x + %i * y))^2;
abs(x + %i*y)^2;

abs((x + %i * y)^2);
abs(x + %i*y)^2;

(cabs(x + %i * y))^2;
x^2 + y^2;

cabs((x + %i * y)^2);
x^2 + y^2;

abs((a + b*%i)^2);
abs(a + b*%i)^2;

abs((%i+x)^2);
abs(%i + x)^2;

abs(%i^2);
1;

abs((%i+x)^2 + 1);
abs((%i + x)^2 + 1);

(reset(domain), 0);
0;

/* SF bug #2549: "function sqrt gives incorrect result"
 */

(kill(z, f), declare (z, complex));
done;

foo:diff(z(q),q)^2*conjugate(diff(z(q),q))^2;
('diff(z(q),q,1))^2*conjugate('diff(z(q),q,1))^2;

sqrt(foo);
/* a better result would be abs('diff(z(q),q,1))^2 but at least this result is not incorrect */
abs('diff(z(q),q,1))*sqrt(conjugate('diff(z(q),q,1))^2);

sqrt(f(z)^2*conjugate(f(z))^2);
abs(f(z))*sqrt(conjugate(f(z))^2);

/* SF bug #2480: "function abs has bug"
 */

kill(t, u);
done;

abs(u-%e^(%i*t)) ^2;
abs(u-%e^(%i*t)) ^2;

/* SF bug #2063: "abs(x^2) with domain:complex"
 */

kill(x, z);
done;

declare (z, complex);
done;

domain:real;
real;

abs(x)^2;
x^2;

abs(x^2);
x^2;

abs(z^2);
abs(z)^2;

domain:complex;
complex;

abs(x)^2;
abs(x)^2;

abs(x^2);
abs(x)^2;

abs(z^2);
abs(z)^2;

(reset(domain), 0);
0;

/* SF merge request 29 */

abs(sqrt(sin(theta) - 1));
abs(sqrt(sin(theta) - 1))$