From 3a63743e24046766b37c3d1bd38605542ee0a536 Mon Sep 17 00:00:00 2001 From: Richard Kreckel Date: Wed, 19 Dec 2001 19:16:16 +0000 Subject: [PATCH] * Added output-support for Python bindings and LaTeX printing for indexed objects (Pearu Peterson). * relational.*: Removed superfluous is_equal_same_type()... * inifcns_trans.cpp: ...added extern declaration as workaround for compiler-bug triggered by changes to relational.*, * input_parser.yy: added missing namespace specification. --- ginac/add.cpp | 14 +++++++++- ginac/constant.cpp | 9 +++++-- ginac/container.pl | 6 ++++- ginac/idx.cpp | 25 +++++++++++++++--- ginac/inifcns_trans.cpp | 8 ++++++ ginac/input_parser.yy | 2 +- ginac/matrix.cpp | 6 +++++ ginac/mul.cpp | 10 ++++++++ ginac/ncmul.cpp | 4 +-- ginac/numeric.cpp | 4 +++ ginac/power.cpp | 13 +++++++++- ginac/print.cpp | 10 ++++++++ ginac/print.h | 16 ++++++++++++ ginac/pseries.cpp | 22 +++++++++++++++- ginac/relational.cpp | 57 +++++++++++++---------------------------- ginac/relational.h | 1 - ginac/symbol.cpp | 9 +++++-- ginac/wildcard.cpp | 2 ++ 18 files changed, 163 insertions(+), 55 deletions(-) diff --git a/ginac/add.cpp b/ginac/add.cpp index b9eca995..279933d3 100644 --- a/ginac/add.cpp +++ b/ginac/add.cpp @@ -152,10 +152,22 @@ void add::print(const print_context & c, unsigned level) const c.s << '+'; overall_coeff.print(c, precedence()); } - + if (precedence() <= level) c.s << ")"; + } else if (is_a(c)) { + + c.s << class_name() << '('; + unsigned end = nops(); + if (end) + op(0).print(c); + for (unsigned i=1; i(c)) + } else if (is_a(c)) { c.s << TeX_name; - else + } else if (is_a(c)) { + c.s << class_name() << "('" << name << "'"; + if (TeX_name != "\\mbox{" + name + "}") + c.s << ",TeX_name='" << TeX_name << "'"; + c.s << ')'; + } else c.s << name; } diff --git a/ginac/container.pl b/ginac/container.pl index 5e4837d1..4603d3f9 100755 --- a/ginac/container.pl +++ b/ginac/container.pl @@ -430,7 +430,11 @@ void ${CONTAINER}::print(const print_context & c, unsigned level) const ++i; } c.s << std::string(level + delta_indent,' ') << "=====" << std::endl; - + } else if (is_a(c)) { + printseq(c, '[', ',', ']', precedence(), precedence()+1); + } else if (is_a(c)) { + c.s << class_name (); + printseq(c, '(', ',', ')', precedence(), precedence()+1); } else { // always print brackets around seq, ignore upper_precedence printseq(c, '${open_bracket}', ',', '${close_bracket}', precedence(), precedence()+1); diff --git a/ginac/idx.cpp b/ginac/idx.cpp index 74754022..578b2bf9 100644 --- a/ginac/idx.cpp +++ b/ginac/idx.cpp @@ -156,7 +156,9 @@ void idx::print(const print_context & c, unsigned level) const } else { - if (!is_of_type(c, print_latex)) + if (is_a(c)) + c.s << "_{"; + else c.s << "."; bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol)); if (need_parens) @@ -164,6 +166,8 @@ void idx::print(const print_context & c, unsigned level) const value.print(c); if (need_parens) c.s << ")"; + if (is_a(c)) + c.s << "}"; } } @@ -180,8 +184,12 @@ void varidx::print(const print_context & c, unsigned level) const dim.print(c, level + delta_indent); } else { - - if (!is_of_type(c, print_latex)) { + if (is_a(c)) { + if (covariant) + c.s << "_{"; + else + c.s << "^{"; + } else { if (covariant) c.s << "."; else @@ -193,6 +201,8 @@ void varidx::print(const print_context & c, unsigned level) const value.print(c); if (need_parens) c.s << ")"; + if (is_a(c)) + c.s << "}"; } } @@ -212,7 +222,12 @@ void spinidx::print(const print_context & c, unsigned level) const } else { bool is_tex = is_of_type(c, print_latex); - if (!is_tex) { + if (is_tex) { + if (covariant) + c.s << "_{"; + else + c.s << "^{"; + } else { if (covariant) c.s << "."; else @@ -232,6 +247,8 @@ void spinidx::print(const print_context & c, unsigned level) const c.s << ")"; if (is_tex && dotted) c.s << "}"; + if (is_tex) + c.s << "}"; } } diff --git a/ginac/inifcns_trans.cpp b/ginac/inifcns_trans.cpp index cbf370db..b092b537 100644 --- a/ginac/inifcns_trans.cpp +++ b/ginac/inifcns_trans.cpp @@ -141,6 +141,14 @@ static ex log_deriv(const ex & x, unsigned deriv_param) return power(x, _ex_1); } +// This is a strange workaround for a compiliation problem with the try statement +// below. With -O1 the exception is not caucht properly as of GCC-2.95.2, at +// least on i386. Version 2.95.4 seems to have fixed this silly problem, though. +// Funnily, with a simple extern declaration here it mysteriously works again. +#if defined(__GNUC__) && (__GNUC__==2) +extern "C" int putchar(int); +#endif + static ex log_series(const ex &arg, const relational &rel, int order, diff --git a/ginac/input_parser.yy b/ginac/input_parser.yy index 8ba5bf42..5df99b66 100644 --- a/ginac/input_parser.yy +++ b/ginac/input_parser.yy @@ -94,7 +94,7 @@ exp : T_NUMBER {$$ = $1;} | T_LITERAL {$$ = $1;} | T_DIGITS {$$ = $1;} | T_SYMBOL '(' exprseq ')' { - string n = ex_to($1).get_name(); + std::string n = ex_to($1).get_name(); if (n == "sqrt") { if ($3.nops() != 1) throw (std::runtime_error("too many arguments to sqrt()")); diff --git a/ginac/matrix.cpp b/ginac/matrix.cpp index 3d735fe4..0bedb6bb 100644 --- a/ginac/matrix.cpp +++ b/ginac/matrix.cpp @@ -147,6 +147,9 @@ void matrix::print(const print_context & c, unsigned level) const } else { + if (is_a(c)) + c.s << class_name() << '('; + c.s << "["; for (unsigned y=0; y(c)) + c.s << ')'; + } } diff --git a/ginac/mul.cpp b/ginac/mul.cpp index 64b090af..47f74f6f 100644 --- a/ginac/mul.cpp +++ b/ginac/mul.cpp @@ -169,6 +169,16 @@ void mul::print(const print_context & c, unsigned level) const if (precedence() <= level) c.s << ")"; + } else if (is_a(c)) { + c.s << class_name() << '('; + unsigned end = nops(); + if (end) + op(0).print(c); + for (unsigned i=1; i(c)) { + } else if (is_a(c) || is_a(c)) { - c.s << "ncmul("; + c.s << class_name() << "("; exvector::const_iterator it = seq.begin(), itend = seq.end()-1; while (it != itend) { it->print(c, precedence()); diff --git a/ginac/numeric.cpp b/ginac/numeric.cpp index fe51c607..01c58453 100644 --- a/ginac/numeric.cpp +++ b/ginac/numeric.cpp @@ -389,6 +389,8 @@ void numeric::print(const print_context & c, unsigned level) const const std::string mul_sym = is_a(c) ? " " : "*"; const cln::cl_R r = cln::realpart(cln::the(value)); const cln::cl_R i = cln::imagpart(cln::the(value)); + if (is_a(c)) + c.s << class_name() << "('"; if (cln::zerop(i)) { // case 1, real: x or -x if ((precedence() <= level) && (!this->is_nonneg_integer())) { @@ -446,6 +448,8 @@ void numeric::print(const print_context & c, unsigned level) const c.s << par_close; } } + if (is_a(c)) + c.s << "')"; } } diff --git a/ginac/power.cpp b/ginac/power.cpp index 78c9c969..72ec5aa3 100644 --- a/ginac/power.cpp +++ b/ginac/power.cpp @@ -159,6 +159,14 @@ void power::print(const print_context & c, unsigned level) const c.s << ')'; } + } else if (is_a(c)) { + + c.s << class_name() << '('; + basis.print(c); + c.s << ','; + exponent.print(c); + c.s << ')'; + } else { if (exponent.is_equal(_ex1_2)) { @@ -179,7 +187,10 @@ void power::print(const print_context & c, unsigned level) const c.s << "("; } basis.print(c, precedence()); - c.s << '^'; + if (is_a(c)) + c.s << "**"; + else + c.s << '^'; if (is_a(c)) c.s << '{'; exponent.print(c, precedence()); diff --git a/ginac/print.cpp b/ginac/print.cpp index bd365cfe..2b1e323b 100644 --- a/ginac/print.cpp +++ b/ginac/print.cpp @@ -36,6 +36,16 @@ print_latex::print_latex() print_latex::print_latex(std::ostream & os) : print_context(os) {} +print_python::print_python() + : print_context(std::cout) {} +print_python::print_python(std::ostream & os) + : print_context(os) {} + +print_python_repr::print_python_repr() + : print_context(std::cout) {} +print_python_repr::print_python_repr(std::ostream & os) + : print_context(os) {} + print_tree::print_tree(unsigned d) : print_context(std::cout), delta_indent(d) {} print_tree::print_tree(std::ostream & os, unsigned d) diff --git a/ginac/print.h b/ginac/print.h index 1d99639e..3e55db5d 100644 --- a/ginac/print.h +++ b/ginac/print.h @@ -49,6 +49,22 @@ public: print_latex(std::ostream &); }; +/** Context for python pretty-print output. */ +class print_python : public print_context +{ +public: + print_python(); + print_python(std::ostream &); +}; + +/** Context for python-parsable output. */ +class print_python_repr : public print_context +{ +public: + print_python_repr(); + print_python_repr(std::ostream &); +}; + /** Context for tree-like output for debugging. */ class print_tree : public print_context { diff --git a/ginac/pseries.cpp b/ginac/pseries.cpp index 80433824..720c4eca 100644 --- a/ginac/pseries.cpp +++ b/ginac/pseries.cpp @@ -134,6 +134,23 @@ void pseries::print(const print_context & c, unsigned level) const var.print(c, level + delta_indent); point.print(c, level + delta_indent); + } else if (is_a(c)) { + c.s << class_name() << "(relational("; + var.print(c); + c.s << ','; + point.print(c); + c.s << "),["; + unsigned num = seq.size(); + for (unsigned i=0; icoeff.compare(_ex1)) { - c.s << '^'; + if (is_a(c)) + c.s << "**"; + else + c.s << '^'; if (i->coeff.info(info_flags::negative)) { c.s << par_open; i->coeff.print(c); diff --git a/ginac/relational.cpp b/ginac/relational.cpp index 69363765..484799d4 100644 --- a/ginac/relational.cpp +++ b/ginac/relational.cpp @@ -95,9 +95,17 @@ void relational::print(const print_context & c, unsigned level) const } else { - if (precedence() <= level) - c.s << "("; - lh.print(c, precedence()); + if (is_a(c)) { + c.s << class_name() << '('; + lh.print(c); + c.s << ','; + rh.print(c); + c.s << ",'"; + } else { + if (precedence() <= level) + c.s << "("; + lh.print(c, precedence()); + } switch (o) { case equal: c.s << "=="; @@ -120,9 +128,13 @@ void relational::print(const print_context & c, unsigned level) const default: c.s << "(INVALID RELATIONAL OPERATOR)"; } - rh.print(c, precedence()); - if (precedence() <= level) - c.s << ")"; + if (is_a(c)) + c.s << "')"; + else { + rh.print(c, precedence()); + if (precedence() <= level) + c.s << ")"; + } } } @@ -273,39 +285,6 @@ unsigned relational::calchash(void) const return v; } -bool relational::is_equal_same_type(const basic & other) const -{ - GINAC_ASSERT(is_a(other)); - const relational &oth = static_cast(other); - if (o==oth.o && lh.is_equal(oth.lh) && rh.is_equal(oth.rh)) - return true; - switch (o) { - case equal: - case not_equal: - if (oth.o!=o) - return false; - break; - case less: - if (oth.o!=greater) - return false; - break; - case less_or_equal: - if (oth.o!=greater_or_equal) - return false; - break; - case greater: - if (oth.o!=less) - return false; - break; - case greater_or_equal: - if (oth.o!=less_or_equal) - return false; - break; - } - return lh.is_equal(oth.rh) && rh.is_equal(oth.lh); -} - - ////////// // new virtual functions which can be overridden by derived classes ////////// diff --git a/ginac/relational.h b/ginac/relational.h index 8f2263e6..b04d11c7 100644 --- a/ginac/relational.h +++ b/ginac/relational.h @@ -63,7 +63,6 @@ protected: bool match_same_type(const basic & other) const; unsigned return_type(void) const; unsigned return_type_tinfo(void) const; - bool is_equal_same_type(const basic & other) const; unsigned calchash(void) const; // new virtual functions which can be overridden by derived classes diff --git a/ginac/symbol.cpp b/ginac/symbol.cpp index 5d2ac8ce..39f2335a 100644 --- a/ginac/symbol.cpp +++ b/ginac/symbol.cpp @@ -151,9 +151,14 @@ void symbol::print(const print_context & c, unsigned level) const << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec << std::endl; - } else if (is_a(c)) + } else if (is_a(c)) { c.s << TeX_name; - else + } else if (is_a(c)) { + c.s << class_name() << "('" << name; + if (TeX_name != default_TeX_name()) + c.s << "','" << TeX_name; + c.s << "')"; + } else c.s << name; } diff --git a/ginac/wildcard.cpp b/ginac/wildcard.cpp index 37e23e97..10aa7e40 100644 --- a/ginac/wildcard.cpp +++ b/ginac/wildcard.cpp @@ -95,6 +95,8 @@ void wildcard::print(const print_context & c, unsigned level) const c.s << std::string(level, ' ') << class_name() << " (" << label << ")" << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec << std::endl; + } else if (is_a(c)) { + c.s << class_name() << '(' << label << ')'; } else c.s << "$" << label; } -- 2.45.1