+#include "inifcns.h"
+#include "ex.h"
+#include "constant.h"
+#include "lst.h"
+#include "matrix.h"
+#include "mul.h"
+#include "power.h"
+#include "operators.h"
+#include "relational.h"
+#include "pseries.h"
+#include "symbol.h"
+#include "symmetry.h"
+#include "utils.h"
+
+namespace GiNaC {
+
+//////////
+// 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;
+}
+
+static ex conjugate_real_part(const ex & arg)
+{
+ return arg.real_part();
+}
+
+static ex conjugate_imag_part(const ex & arg)
+{
+ return -arg.imag_part();
+}
+
+REGISTER_FUNCTION(conjugate_function, eval_func(conjugate_eval).
+ evalf_func(conjugate_evalf).
+ print_func<print_latex>(conjugate_print_latex).
+ conjugate_func(conjugate_conjugate).
+ real_part_func(conjugate_real_part).
+ imag_part_func(conjugate_imag_part).
+ set_name("conjugate","conjugate"));
+
+//////////
+// real part
+//////////
+
+static ex real_part_evalf(const ex & arg)
+{
+ if (is_exactly_a<numeric>(arg)) {
+ return ex_to<numeric>(arg).real();
+ }
+ return real_part_function(arg).hold();
+}
+
+static ex real_part_eval(const ex & arg)
+{
+ return arg.real_part();
+}
+
+static void real_part_print_latex(const ex & arg, const print_context & c)
+{
+ c.s << "\\Re"; arg.print(c); c.s << "";
+}
+
+static ex real_part_conjugate(const ex & arg)
+{
+ return real_part_function(arg).hold();
+}
+
+static ex real_part_real_part(const ex & arg)
+{
+ return real_part_function(arg).hold();
+}
+
+static ex real_part_imag_part(const ex & arg)
+{
+ return 0;
+}
+
+REGISTER_FUNCTION(real_part_function, eval_func(real_part_eval).
+ evalf_func(real_part_evalf).
+ print_func<print_latex>(real_part_print_latex).
+ conjugate_func(real_part_conjugate).
+ real_part_func(real_part_real_part).
+ imag_part_func(real_part_imag_part).
+ set_name("real_part","real_part"));
+
+//////////
+// imag part
+//////////
+
+static ex imag_part_evalf(const ex & arg)
+{
+ if (is_exactly_a<numeric>(arg)) {
+ return ex_to<numeric>(arg).imag();
+ }
+ return imag_part_function(arg).hold();
+}
+
+static ex imag_part_eval(const ex & arg)
+{
+ return arg.imag_part();
+}
+
+static void imag_part_print_latex(const ex & arg, const print_context & c)
+{
+ c.s << "\\Im"; arg.print(c); c.s << "";
+}
+
+static ex imag_part_conjugate(const ex & arg)
+{
+ return imag_part_function(arg).hold();
+}
+
+static ex imag_part_real_part(const ex & arg)
+{
+ return imag_part_function(arg).hold();
+}
+
+static ex imag_part_imag_part(const ex & arg)
+{
+ return 0;
+}
+
+REGISTER_FUNCTION(imag_part_function, eval_func(imag_part_eval).
+ evalf_func(imag_part_evalf).
+ print_func<print_latex>(imag_part_print_latex).
+ conjugate_func(imag_part_conjugate).
+ real_part_func(imag_part_real_part).
+ imag_part_func(imag_part_imag_part).
+ set_name("imag_part","imag_part"));
+
+//////////
+// 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));
+
+ 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)
+{
+ // 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);
+}
+
+static ex eta_real_part(const ex & x, const ex & y)
+{
+ return 0;
+}
+
+static ex eta_imag_part(const ex & x, const ex & y)
+{
+ return -I*eta(x, y).hold();
+}
+
+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).
+ real_part_func(eta_real_part).
+ imag_part_func(eta_imag_part));
+