+//////////
+// complex conjugate
+//////////
+
+static ex conjugate_evalf(const ex & arg)
+{
+ if (is_exactly_a<numeric>(arg)) {
+ return ex_to<numeric>(arg).conjugate();
+ }
+ return conjugate_function(arg).hold();
+}
+
+static ex conjugate_eval(const ex & arg)
+{
+ return arg.conjugate();
+}
+
+static void conjugate_print_latex(const ex & arg, const print_context & c)
+{
+ c.s << "\\bar{"; arg.print(c); c.s << "}";
+}
+
+static ex conjugate_conjugate(const ex & arg)
+{
+ return arg;
+}
+
+REGISTER_FUNCTION(conjugate_function, eval_func(conjugate_eval).
+ evalf_func(conjugate_evalf).
+ print_func<print_latex>(conjugate_print_latex).
+ conjugate_func(conjugate_conjugate).
+ set_name("conjugate","conjugate"));
+
+//////////
+// absolute value
+//////////
+
+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));
+ else
+ 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);
+}
+
+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));
+
+
+//////////
+// 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);
+}
+
+REGISTER_FUNCTION(csgn, eval_func(csgn_eval).
+ evalf_func(csgn_evalf).
+ series_func(csgn_series).
+ conjugate_func(csgn_conjugate));
+
+
+//////////
+// 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)
+{
+ // It seems like we basically have to replicate the eval function here,
+ // since the expression might not be fully evaluated yet.
+ if (x.info(info_flags::positive) || y.info(info_flags::positive))
+ return _ex0;
+
+ if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
+ const numeric nx = ex_to<numeric>(x);
+ const numeric ny = ex_to<numeric>(y);
+ const numeric nxy = ex_to<numeric>(x*y);
+ int cut = 0;
+ if (nx.is_real() && nx.is_negative())
+ cut -= 4;
+ if (ny.is_real() && ny.is_negative())
+ cut -= 4;
+ if (nxy.is_real() && nxy.is_negative())
+ cut += 4;
+ return evalf(I/4*Pi)*((csgn(-imag(nx))+1)*(csgn(-imag(ny))+1)*(csgn(imag(nxy))+1)-
+ (csgn(imag(nx))+1)*(csgn(imag(ny))+1)*(csgn(-imag(nxy))+1)+cut);
+ }
+
+ return eta(x,y).hold();
+}
+
+static ex eta_eval(const ex &x, const ex &y)
+{
+ // trivial: eta(x,c) -> 0 if c is real and positive
+ if (x.info(info_flags::positive) || y.info(info_flags::positive))
+ return _ex0;
+
+ if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
+ // don't call eta_evalf here because it would call Pi.evalf()!
+ const numeric nx = ex_to<numeric>(x);
+ const numeric ny = ex_to<numeric>(y);
+ const numeric nxy = ex_to<numeric>(x*y);
+ int cut = 0;
+ if (nx.is_real() && nx.is_negative())
+ cut -= 4;
+ if (ny.is_real() && ny.is_negative())
+ cut -= 4;
+ if (nxy.is_real() && nxy.is_negative())
+ cut += 4;
+ return (I/4)*Pi*((csgn(-imag(nx))+1)*(csgn(-imag(ny))+1)*(csgn(imag(nxy))+1)-
+ (csgn(imag(nx))+1)*(csgn(imag(ny))+1)*(csgn(-imag(nxy))+1)+cut);
+ }
+
+ return eta(x,y).hold();
+}
+
+static ex eta_series(const ex & x, const ex & y,
+ const relational & rel,
+ int order,
+ unsigned options)
+{
+ const ex x_pt = x.subs(rel, subs_options::no_pattern);
+ const ex y_pt = y.subs(rel, subs_options::no_pattern);
+ if ((x_pt.info(info_flags::numeric) && x_pt.info(info_flags::negative)) ||
+ (y_pt.info(info_flags::numeric) && y_pt.info(info_flags::negative)) ||
+ ((x_pt*y_pt).info(info_flags::numeric) && (x_pt*y_pt).info(info_flags::negative)))
+ throw (std::domain_error("eta_series(): on discontinuity"));
+ epvector seq;
+ seq.push_back(expair(eta(x_pt,y_pt), _ex0));
+ return pseries(rel,seq);
+}
+
+static ex eta_conjugate(const ex & x, const ex & y)
+{
+ return -eta(x,y);
+}
+
+REGISTER_FUNCTION(eta, eval_func(eta_eval).
+ evalf_func(eta_evalf).
+ series_func(eta_series).
+ latex_name("\\eta").
+ set_symmetry(sy_symm(0, 1)).
+ conjugate_func(eta_conjugate));
+
+