+
+ if (arg.info(info_flags::nonnegative))
+ return arg;
+
+ if (arg.info(info_flags::negative) || (-arg).info(info_flags::nonnegative))
+ return -arg;
+
+ if (is_ex_the_function(arg, abs))
+ return arg;
+
+ if (is_ex_the_function(arg, exp))
+ return exp(arg.op(0).real_part());
+
+ if (is_exactly_a<power>(arg)) {
+ const ex& base = arg.op(0);
+ const ex& exponent = arg.op(1);
+ if (base.info(info_flags::positive) || exponent.info(info_flags::real))
+ return pow(abs(base), exponent.real_part());
+ }
+
+ if (is_ex_the_function(arg, conjugate_function))
+ return abs(arg.op(0));
+
+ if (is_ex_the_function(arg, step))
+ return arg;
+
+ return abs(arg).hold();
+}
+
+static ex abs_expand(const ex & arg, unsigned options)
+{
+ if ((options & expand_options::expand_transcendental)
+ && is_exactly_a<mul>(arg)) {
+ exvector prodseq;
+ prodseq.reserve(arg.nops());
+ for (const_iterator i = arg.begin(); i != arg.end(); ++i) {
+ if (options & expand_options::expand_function_args)
+ prodseq.push_back(abs(i->expand(options)));
+ else
+ prodseq.push_back(abs(*i));
+ }
+ return (new mul(prodseq))->setflag(status_flags::dynallocated | status_flags::expanded);
+ }
+
+ if (options & expand_options::expand_function_args)
+ return abs(arg.expand(options)).hold();