-void numeric::print(std::ostream & os, unsigned upper_precedence) const
-{
- debugmsg("numeric print", LOGLEVEL_PRINT);
- if (this->is_real()) {
- // case 1, real: x or -x
- if ((precedence<=upper_precedence) && (!this->is_nonneg_integer())) {
- os << "(";
- print_real_number(os, The(::cl_R)(*value));
- os << ")";
- } else {
- print_real_number(os, The(::cl_R)(*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 << "(";
- print_real_number(os, The(::cl_R)(::imagpart(*value)));
- os << "*I)";
- }
- } else {
- if (::imagpart(*value) == 1) {
- os << "I";
- } else {
- if (::imagpart (*value) == -1) {
- os << "-I";
- } else {
- print_real_number(os, The(::cl_R)(::imagpart(*value)));
- os << "*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 << "(";
- print_real_number(os, The(::cl_R)(::realpart(*value)));
- if (::imagpart(*value) < 0) {
- if (::imagpart(*value) == -1) {
- os << "-I";
- } else {
- print_real_number(os, The(::cl_R)(::imagpart(*value)));
- os << "*I";
- }
- } else {
- if (::imagpart(*value) == 1) {
- os << "+I";
- } else {
- os << "+";
- print_real_number(os, The(::cl_R)(::imagpart(*value)));
- os << "*I";
- }
- }
- if (precedence <= upper_precedence)
- os << ")";
- }
- }
-}
-
-
-void numeric::printraw(std::ostream & os) const
-{
- // The method printraw doesn't do much, it simply uses CLN's operator<<()
- // for output, which is ugly but reliable. e.g: 2+2i
- debugmsg("numeric printraw", LOGLEVEL_PRINT);
- os << "numeric(" << *value << ")";
-}
-
-
-void numeric::printtree(std::ostream & os, unsigned indent) const
-{
- debugmsg("numeric printtree", LOGLEVEL_PRINT);
- os << std::string(indent,' ') << *value
- << " (numeric): "
- << "hash=" << hashvalue
- << " (0x" << std::hex << hashvalue << std::dec << ")"
- << ", flags=" << flags << std::endl;
-}
-
-
-void numeric::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
-{
- debugmsg("numeric print csrc", LOGLEVEL_PRINT);
- ios::fmtflags oldflags = os.flags();
- os.setf(ios::scientific);
- if (this->is_rational() && !this->is_integer()) {
- if (compare(_num0()) > 0) {
- os << "(";
- if (type == csrc_types::ctype_cl_N)
- os << "cl_F(\"" << numer().evalf() << "\")";
- else
- os << numer().to_double();
- } else {
- os << "-(";
- if (type == csrc_types::ctype_cl_N)
- os << "cl_F(\"" << -numer().evalf() << "\")";
- else
- os << -numer().to_double();
- }
- os << "/";
- if (type == csrc_types::ctype_cl_N)
- os << "cl_F(\"" << denom().evalf() << "\")";
- else
- os << denom().to_double();
- os << ")";
- } else {
- if (type == csrc_types::ctype_cl_N)
- os << "cl_F(\"" << evalf() << "\")";
- else
- os << to_double();
- }
- os.flags(oldflags);
+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_cl_N>(c)) {
+
+ // CLN output
+ if (this->is_real()) {
+
+ // Real number
+ print_real_cl_N(c, cln::the<cln::cl_R>(value));
+
+ } else {
+
+ // Complex number
+ c.s << "cln::complex(";
+ print_real_cl_N(c, cln::realpart(cln::the<cln::cl_N>(value)));
+ c.s << ",";
+ print_real_cl_N(c, cln::imagpart(cln::the<cln::cl_N>(value)));
+ c.s << ")";
+ }
+
+ } else if (is_a<print_csrc>(c)) {
+
+ // C++ source output
+ 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 {
+
+ // Complex number
+ c.s << "std::complex<";
+ if (is_a<print_csrc_double>(c))
+ c.s << "double>(";
+ else
+ c.s << "float>(";
+
+ print_real_csrc(c, cln::realpart(cln::the<cln::cl_N>(value)));
+ c.s << ",";
+ print_real_csrc(c, cln::imagpart(cln::the<cln::cl_N>(value)));
+ c.s << ")";
+ }
+
+ 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 << "')";
+ }