+numeric::numeric(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
+{
+ cln::cl_N ctorval = 0;
+
+ // Read number as string
+ std::string str;
+ if (n.find_string("number", str)) {
+ std::istringstream s(str);
+ cln::cl_idecoded_float re, im;
+ char c;
+ s.get(c);
+ switch (c) {
+ case 'R': // Integer-decoded real number
+ s >> re.sign >> re.mantissa >> re.exponent;
+ ctorval = re.sign * re.mantissa * cln::expt(cln::cl_float(2.0, cln::default_float_format), re.exponent);
+ break;
+ case 'C': // Integer-decoded complex number
+ s >> re.sign >> re.mantissa >> re.exponent;
+ s >> im.sign >> im.mantissa >> im.exponent;
+ ctorval = cln::complex(re.sign * re.mantissa * cln::expt(cln::cl_float(2.0, cln::default_float_format), re.exponent),
+ im.sign * im.mantissa * cln::expt(cln::cl_float(2.0, cln::default_float_format), im.exponent));
+ break;
+ default: // Ordinary number
+ s.putback(c);
+ s >> ctorval;
+ break;
+ }
+ }
+ value = ctorval;
+ setflag(status_flags::evaluated | status_flags::expanded);
+}
+
+void numeric::archive(archive_node &n) const
+{
+ inherited::archive(n);
+
+ // Write number as string
+ std::ostringstream s;
+ if (this->is_crational())
+ s << cln::the<cln::cl_N>(value);
+ else {
+ // Non-rational numbers are written in an integer-decoded format
+ // to preserve the precision
+ if (this->is_real()) {
+ cln::cl_idecoded_float re = cln::integer_decode_float(cln::the<cln::cl_F>(value));
+ s << "R";
+ s << re.sign << " " << re.mantissa << " " << re.exponent;
+ } else {
+ cln::cl_idecoded_float re = cln::integer_decode_float(cln::the<cln::cl_F>(cln::realpart(cln::the<cln::cl_N>(value))));
+ cln::cl_idecoded_float im = cln::integer_decode_float(cln::the<cln::cl_F>(cln::imagpart(cln::the<cln::cl_N>(value))));
+ s << "C";
+ s << re.sign << " " << re.mantissa << " " << re.exponent << " ";
+ s << im.sign << " " << im.mantissa << " " << im.exponent;
+ }
+ }
+ n.add_string("number", s.str());
+}
+
+DEFAULT_UNARCHIVE(numeric)
+
+//////////
+// functions overriding virtual functions from base classes
+//////////
+
+/** Helper function to print a real number in a nicer way than is CLN's
+ * default. Instead of printing 42.0L0 this just prints 42.0 to ostream os
+ * and instead of 3.99168L7 it prints 3.99168E7. This is fine in GiNaC as
+ * long as it only uses cl_LF and no other floating point types that we might
+ * want to visibly distinguish from cl_LF.
+ *
+ * @see numeric::print() */
+static void print_real_number(const print_context & c, const cln::cl_R & x)
+{
+ cln::cl_print_flags ourflags;
+ if (cln::instanceof(x, cln::cl_RA_ring)) {
+ // case 1: integer or rational
+ if (cln::instanceof(x, cln::cl_I_ring) ||
+ !is_a<print_latex>(c)) {
+ cln::print_real(c.s, ourflags, x);
+ } else { // rational output in LaTeX context
+ if (x < 0)
+ c.s << "-";
+ c.s << "\\frac{";
+ cln::print_real(c.s, ourflags, cln::abs(cln::numerator(cln::the<cln::cl_RA>(x))));
+ c.s << "}{";
+ cln::print_real(c.s, ourflags, cln::denominator(cln::the<cln::cl_RA>(x)));
+ c.s << '}';
+ }
+ } else {
+ // case 2: float
+ // make CLN believe this number has default_float_format, so it prints
+ // 'E' as exponent marker instead of 'L':
+ ourflags.default_float_format = cln::float_format(cln::the<cln::cl_F>(x));
+ cln::print_real(c.s, ourflags, x);
+ }
+}
+
+/** Helper function to print integer number in C++ source format.
+ *
+ * @see numeric::print() */
+static void print_integer_csrc(const print_context & c, const cln::cl_I & x)
+{
+ // Print small numbers in compact float format, but larger numbers in
+ // scientific format
+ const int max_cln_int = 536870911; // 2^29-1
+ if (x >= cln::cl_I(-max_cln_int) && x <= cln::cl_I(max_cln_int))
+ c.s << cln::cl_I_to_int(x) << ".0";
+ else
+ c.s << cln::double_approx(x);
+}
+
+/** Helper function to print real number in C++ source format.
+ *
+ * @see numeric::print() */
+static void print_real_csrc(const print_context & c, const cln::cl_R & x)
+{
+ if (cln::instanceof(x, cln::cl_I_ring)) {
+
+ // Integer number
+ print_integer_csrc(c, cln::the<cln::cl_I>(x));
+
+ } else if (cln::instanceof(x, cln::cl_RA_ring)) {
+
+ // Rational number
+ const cln::cl_I numer = cln::numerator(cln::the<cln::cl_RA>(x));
+ const cln::cl_I denom = cln::denominator(cln::the<cln::cl_RA>(x));
+ if (cln::plusp(x) > 0) {
+ c.s << "(";
+ print_integer_csrc(c, numer);
+ } else {
+ c.s << "-(";
+ print_integer_csrc(c, -numer);
+ }
+ c.s << "/";
+ print_integer_csrc(c, denom);
+ c.s << ")";
+
+ } else {
+
+ // Anything else
+ c.s << cln::double_approx(x);
+ }
+}
+
+/** Helper function to print real number in C++ source format using cl_N types.
+ *
+ * @see numeric::print() */
+static void print_real_cl_N(const print_context & c, const cln::cl_R & x)
+{
+ if (cln::instanceof(x, cln::cl_I_ring)) {
+
+ // Integer number
+ c.s << "cln::cl_I(\"";
+ print_real_number(c, x);
+ c.s << "\")";
+
+ } else if (cln::instanceof(x, cln::cl_RA_ring)) {
+
+ // Rational number
+ cln::cl_print_flags ourflags;
+ c.s << "cln::cl_RA(\"";
+ cln::print_rational(c.s, ourflags, cln::the<cln::cl_RA>(x));
+ c.s << "\")";
+
+ } else {
+
+ // Anything else
+ c.s << "cln::cl_F(\"";
+ print_real_number(c, cln::cl_float(1.0, cln::default_float_format) * x);
+ c.s << "_" << Digits << "\")";
+ }
+}
+
+void numeric::print_numeric(const print_context & c, const char *par_open, const char *par_close, const char *imag_sym, const char *mul_sym, unsigned level) const
+{
+ const cln::cl_R r = cln::realpart(cln::the<cln::cl_N>(value));
+ const cln::cl_R i = cln::imagpart(cln::the<cln::cl_N>(value));
+
+ if (cln::zerop(i)) {
+
+ // case 1, real: x or -x
+ if ((precedence() <= level) && (!this->is_nonneg_integer())) {
+ c.s << par_open;
+ print_real_number(c, r);
+ c.s << par_close;
+ } else {
+ print_real_number(c, r);
+ }
+
+ } else {
+ if (cln::zerop(r)) {
+
+ // case 2, imaginary: y*I or -y*I
+ if (i == 1)
+ c.s << imag_sym;
+ else {
+ if (precedence()<=level)
+ c.s << par_open;
+ if (i == -1)
+ c.s << "-" << imag_sym;
+ else {
+ print_real_number(c, i);
+ c.s << mul_sym << imag_sym;
+ }
+ if (precedence()<=level)
+ c.s << par_close;
+ }
+
+ } else {
+
+ // case 3, complex: x+y*I or x-y*I or -x+y*I or -x-y*I
+ if (precedence() <= level)
+ c.s << par_open;
+ print_real_number(c, r);
+ if (i < 0) {
+ if (i == -1) {
+ c.s << "-" << imag_sym;
+ } else {
+ print_real_number(c, i);
+ c.s << mul_sym << imag_sym;
+ }
+ } else {
+ if (i == 1) {
+ c.s << "+" << imag_sym;
+ } else {
+ c.s << "+";
+ print_real_number(c, i);
+ c.s << mul_sym << imag_sym;
+ }
+ }
+ if (precedence() <= level)
+ c.s << par_close;
+ }
+ }
+}
+
+void numeric::do_print(const print_context & c, unsigned level) const
+{
+ print_numeric(c, "(", ")", "I", "*", level);
+}
+
+void numeric::do_print_latex(const print_latex & c, unsigned level) const
+{
+ print_numeric(c, "{(", ")}", "i", " ", level);
+}
+
+void numeric::do_print_csrc(const print_csrc & c, unsigned level) const
+{
+ std::ios::fmtflags oldflags = c.s.flags();
+ c.s.setf(std::ios::scientific);
+ int oldprec = c.s.precision();
+
+ // Set precision
+ if (is_a<print_csrc_double>(c))
+ c.s.precision(std::numeric_limits<double>::digits10 + 1);
+ else
+ c.s.precision(std::numeric_limits<float>::digits10 + 1);
+
+ if (this->is_real()) {
+
+ // Real number
+ print_real_csrc(c, cln::the<cln::cl_R>(value));
+
+ } else {