+static ex abs_evalf(const ex & arg)
+{
+ if (is_exactly_a<numeric>(arg))
+ return abs(ex_to<numeric>(arg));
+
+ return abs(arg).hold();
+}
+
+static ex abs_eval(const ex & arg)
+{
+ if (is_exactly_a<numeric>(arg))
+ return abs(ex_to<numeric>(arg));
+
+ if (arg.info(info_flags::nonnegative))
+ return arg;
+
+ if (is_ex_the_function(arg, abs))
+ return arg;
+
+ return abs(arg).hold();
+}
+
+static void abs_print_latex(const ex & arg, const print_context & c)
+{
+ c.s << "{|"; arg.print(c); c.s << "|}";
+}
+
+static void abs_print_csrc_float(const ex & arg, const print_context & c)
+{
+ c.s << "fabs("; arg.print(c); c.s << ")";
+}
+
+static ex abs_conjugate(const ex & arg)
+{
+ return abs(arg);
+}
+
+static ex abs_real_part(const ex & arg)
+{
+ return abs(arg).hold();
+}
+
+static ex abs_imag_part(const ex& arg)
+{
+ return 0;
+}
+
+static ex abs_power(const ex & arg, const ex & exp)
+{
+ if (arg.is_equal(arg.conjugate()) && is_a<numeric>(exp) && ex_to<numeric>(exp).is_even())
+ return power(arg, exp);
+ else
+ return power(abs(arg), exp).hold();
+}
+
+REGISTER_FUNCTION(abs, eval_func(abs_eval).
+ evalf_func(abs_evalf).
+ print_func<print_latex>(abs_print_latex).
+ print_func<print_csrc_float>(abs_print_csrc_float).
+ print_func<print_csrc_double>(abs_print_csrc_float).
+ conjugate_func(abs_conjugate).
+ real_part_func(abs_real_part).
+ imag_part_func(abs_imag_part).
+ power_func(abs_power));
+
+//////////
+// Step function
+//////////
+
+static ex step_evalf(const ex & arg)
+{
+ if (is_exactly_a<numeric>(arg))
+ return step(ex_to<numeric>(arg));
+
+ return step(arg).hold();
+}
+
+static ex step_eval(const ex & arg)
+{
+ if (is_exactly_a<numeric>(arg))
+ return step(ex_to<numeric>(arg));
+
+ else if (is_exactly_a<mul>(arg) &&
+ is_exactly_a<numeric>(arg.op(arg.nops()-1))) {
+ numeric oc = ex_to<numeric>(arg.op(arg.nops()-1));
+ if (oc.is_real()) {
+ if (oc > 0)
+ // step(42*x) -> step(x)
+ return step(arg/oc).hold();
+ else
+ // step(-42*x) -> step(-x)
+ return step(-arg/oc).hold();
+ }
+ if (oc.real().is_zero()) {
+ if (oc.imag() > 0)
+ // step(42*I*x) -> step(I*x)
+ return step(I*arg/oc).hold();
+ else
+ // step(-42*I*x) -> step(-I*x)
+ return step(-I*arg/oc).hold();
+ }
+ }
+
+ return step(arg).hold();
+}
+
+static ex step_series(const ex & arg,
+ const relational & rel,
+ int order,
+ unsigned options)
+{
+ const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
+ if (arg_pt.info(info_flags::numeric)
+ && ex_to<numeric>(arg_pt).real().is_zero()
+ && !(options & series_options::suppress_branchcut))
+ throw (std::domain_error("step_series(): on imaginary axis"));
+
+ epvector seq;
+ seq.push_back(expair(step(arg_pt), _ex0));
+ return pseries(rel,seq);
+}
+
+static ex step_conjugate(const ex& arg)
+{
+ return step(arg).hold();
+}
+
+static ex step_real_part(const ex& arg)
+{
+ return step(arg).hold();
+}
+
+static ex step_imag_part(const ex& arg)
+{
+ return 0;
+}
+
+REGISTER_FUNCTION(step, eval_func(step_eval).
+ evalf_func(step_evalf).
+ series_func(step_series).
+ conjugate_func(step_conjugate).
+ real_part_func(step_real_part).
+ imag_part_func(step_imag_part));
+
+//////////
+// Complex sign
+//////////
+
+static ex csgn_evalf(const ex & arg)
+{
+ if (is_exactly_a<numeric>(arg))
+ return csgn(ex_to<numeric>(arg));
+
+ return csgn(arg).hold();
+}
+
+static ex csgn_eval(const ex & arg)
+{
+ if (is_exactly_a<numeric>(arg))
+ return csgn(ex_to<numeric>(arg));
+
+ else if (is_exactly_a<mul>(arg) &&
+ is_exactly_a<numeric>(arg.op(arg.nops()-1))) {
+ numeric oc = ex_to<numeric>(arg.op(arg.nops()-1));
+ if (oc.is_real()) {
+ if (oc > 0)
+ // csgn(42*x) -> csgn(x)
+ return csgn(arg/oc).hold();
+ else
+ // csgn(-42*x) -> -csgn(x)
+ return -csgn(arg/oc).hold();
+ }
+ if (oc.real().is_zero()) {
+ if (oc.imag() > 0)
+ // csgn(42*I*x) -> csgn(I*x)
+ return csgn(I*arg/oc).hold();
+ else
+ // csgn(-42*I*x) -> -csgn(I*x)
+ return -csgn(I*arg/oc).hold();
+ }
+ }
+
+ return csgn(arg).hold();
+}
+
+static ex csgn_series(const ex & arg,
+ const relational & rel,
+ int order,
+ unsigned options)
+{
+ const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
+ if (arg_pt.info(info_flags::numeric)
+ && ex_to<numeric>(arg_pt).real().is_zero()
+ && !(options & series_options::suppress_branchcut))
+ throw (std::domain_error("csgn_series(): on imaginary axis"));
+
+ epvector seq;
+ seq.push_back(expair(csgn(arg_pt), _ex0));
+ return pseries(rel,seq);
+}
+
+static ex csgn_conjugate(const ex& arg)
+{
+ return csgn(arg).hold();
+}
+
+static ex csgn_real_part(const ex& arg)
+{
+ return csgn(arg).hold();
+}
+
+static ex csgn_imag_part(const ex& arg)
+{
+ return 0;
+}
+
+static ex csgn_power(const ex & arg, const ex & exp)
+{
+ if (is_a<numeric>(exp) && exp.info(info_flags::positive) && ex_to<numeric>(exp).is_integer()) {
+ if (ex_to<numeric>(exp).is_odd())
+ return csgn(arg);
+ else
+ return power(csgn(arg), _ex2).hold();
+ } else
+ return power(csgn(arg), exp).hold();
+}
+
+
+REGISTER_FUNCTION(csgn, eval_func(csgn_eval).
+ evalf_func(csgn_evalf).
+ series_func(csgn_series).
+ conjugate_func(csgn_conjugate).
+ real_part_func(csgn_real_part).
+ imag_part_func(csgn_imag_part).
+ power_func(csgn_power));
+
+
+//////////
+// Eta function: eta(x,y) == log(x*y) - log(x) - log(y).
+// This function is closely related to the unwinding number K, sometimes found
+// in modern literature: K(z) == (z-log(exp(z)))/(2*Pi*I).
+//////////
+
+static ex eta_evalf(const ex &x, const ex &y)