-basic * numeric::duplicate() const
-{
- debugmsg("numeric duplicate", LOGLEVEL_DUPLICATE);
- return new numeric(*this);
-}
-
-// The method printraw doesn't do much, it simply uses CLN's operator<<() for
-// output, which is ugly but reliable. Examples:
-// 2+2i
-void numeric::printraw(ostream & os) const
-{
- debugmsg("numeric printraw", LOGLEVEL_PRINT);
- os << "numeric(" << *value << ")";
-}
-
-// The method print adds to the output so it blends more consistently together
-// with the other routines and produces something compatible to Maple input.
-void numeric::print(ostream & os, unsigned upper_precedence) const
-{
- debugmsg("numeric print", LOGLEVEL_PRINT);
- if (is_real()) {
- // case 1, real: x or -x
- if ((precedence<=upper_precedence) && (!is_pos_integer())) {
- os << "(" << *value << ")";
- } else {
- os << *value;
- }
- } else {
- // case 2, imaginary: y*I or -y*I
- if (realpart(*value) == 0) {
- if ((precedence<=upper_precedence) && (imagpart(*value) < 0)) {
- if (imagpart(*value) == -1) {
- os << "(-I)";
- } else {
- os << "(" << imagpart(*value) << "*I)";
- }
- } else {
- if (imagpart(*value) == 1) {
- os << "I";
- } else {
- if (imagpart (*value) == -1) {
- os << "-I";
- } else {
- os << imagpart(*value) << "*I";
- }
- }
- }
- } else {
- // case 3, complex: x+y*I or x-y*I or -x+y*I or -x-y*I
- if (precedence <= upper_precedence) os << "(";
- os << realpart(*value);
- if (imagpart(*value) < 0) {
- if (imagpart(*value) == -1) {
- os << "-I";
- } else {
- os << imagpart(*value) << "*I";
- }
- } else {
- if (imagpart(*value) == 1) {
- os << "+I";
- } else {
- os << "+" << imagpart(*value) << "*I";
- }
- }
- if (precedence <= upper_precedence) os << ")";
- }
- }
+//////////
+// 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);
+ }
+}
+
+/** This method adds to the output so it blends more consistently together
+ * with the other routines and produces something compatible to ginsh input.
+ *
+ * @see print_real_number() */
+void numeric::print(const print_context & c, unsigned level) const
+{
+ if (is_a<print_tree>(c)) {
+
+ c.s << std::string(level, ' ') << cln::the<cln::cl_N>(value)
+ << " (" << class_name() << ")"
+ << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
+ << std::endl;
+
+ } else if (is_a<print_csrc>(c)) {
+
+ std::ios::fmtflags oldflags = c.s.flags();
+ c.s.setf(std::ios::scientific);
+ int oldprec = c.s.precision();
+ if (is_a<print_csrc_double>(c))
+ c.s.precision(16);
+ else
+ c.s.precision(7);
+ if (is_a<print_csrc_cl_N>(c) && this->is_integer()) {
+ c.s << "cln::cl_I(\"";
+ const cln::cl_R r = cln::realpart(cln::the<cln::cl_N>(value));
+ print_real_number(c,r);
+ c.s << "\")";
+ } else if (this->is_rational() && !this->is_integer()) {
+ if (compare(_num0) > 0) {
+ c.s << "(";
+ if (is_a<print_csrc_cl_N>(c))
+ c.s << "cln::cl_F(\"" << numer().evalf() << "\")";
+ else
+ c.s << numer().to_double();
+ } else {
+ c.s << "-(";
+ if (is_a<print_csrc_cl_N>(c))
+ c.s << "cln::cl_F(\"" << -numer().evalf() << "\")";
+ else
+ c.s << -numer().to_double();
+ }
+ c.s << "/";
+ if (is_a<print_csrc_cl_N>(c))
+ c.s << "cln::cl_F(\"" << denom().evalf() << "\")";
+ else
+ c.s << denom().to_double();
+ c.s << ")";
+ } else {
+ if (is_a<print_csrc_cl_N>(c))
+ c.s << "cln::cl_F(\"" << evalf() << "_" << Digits << "\")";
+ else
+ c.s << to_double();
+ }
+ c.s.flags(oldflags);
+ c.s.precision(oldprec);
+
+ } else {
+ const std::string par_open = is_a<print_latex>(c) ? "{(" : "(";
+ const std::string par_close = is_a<print_latex>(c) ? ")}" : ")";
+ const std::string imag_sym = is_a<print_latex>(c) ? "i" : "I";
+ const std::string mul_sym = is_a<print_latex>(c) ? " " : "*";
+ 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 (is_a<print_python_repr>(c))
+ c.s << class_name() << "('";
+ 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;
+ }
+ }
+ if (is_a<print_python_repr>(c))
+ c.s << "')";
+ }