]> www.ginac.de Git - ginac.git/commitdiff
* Added output-support for Python bindings and LaTeX printing for
authorRichard Kreckel <Richard.Kreckel@uni-mainz.de>
Wed, 19 Dec 2001 19:16:16 +0000 (19:16 +0000)
committerRichard Kreckel <Richard.Kreckel@uni-mainz.de>
Wed, 19 Dec 2001 19:16:16 +0000 (19:16 +0000)
  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.

18 files changed:
ginac/add.cpp
ginac/constant.cpp
ginac/container.pl
ginac/idx.cpp
ginac/inifcns_trans.cpp
ginac/input_parser.yy
ginac/matrix.cpp
ginac/mul.cpp
ginac/ncmul.cpp
ginac/numeric.cpp
ginac/power.cpp
ginac/print.cpp
ginac/print.h
ginac/pseries.cpp
ginac/relational.cpp
ginac/relational.h
ginac/symbol.cpp
ginac/wildcard.cpp

index b9eca995affd6d9cc295118062f67d79d74f89e2..279933d3ecb3f907e1940e4ebeee83357aefb0c1 100644 (file)
@@ -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<print_python_repr>(c)) {
+
+               c.s << class_name() << '(';
+               unsigned end = nops();
+               if (end)
+                       op(0).print(c);
+               for (unsigned i=1; i<end; ++i) {
+                       c.s << ',';
+                       op(i).print(c);
+               }
+               c.s << ')';
+
        } else {
 
                if (precedence() <= level) {
index 0cf7c77bbdcf4419d144abdf6c5d006506534700..beabc9669369794061c8a29816dd4f86d347b057 100644 (file)
@@ -134,9 +134,14 @@ void constant::print(const print_context & c, unsigned level) const
                c.s << std::string(level, ' ') << name << " (" << class_name() << ")"
                    << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
                    << std::endl;
-       } else if (is_a<print_latex>(c))
+       } else if (is_a<print_latex>(c)) {
                c.s << TeX_name;
-       else
+       } else if (is_a<print_python_repr>(c)) {
+               c.s << class_name() << "('" << name << "'";
+               if (TeX_name != "\\mbox{" + name + "}")
+                       c.s << ",TeX_name='" << TeX_name << "'";
+               c.s << ')';
+       } else
                c.s << name;
 }
 
index 5e4837d17e0faa185285315e4fc7fb67efde5d9c..4603d3f96fab1e8fe7cfe0a81926e3b32145ba9c 100755 (executable)
@@ -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<print_python>(c)) {
+               printseq(c, '[', ',', ']', precedence(), precedence()+1);
+       } else if (is_a<print_python_repr>(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);
index 74754022e63f6f73996404d3e2ea389cd0d2fa5e..578b2bf935da21b0f475a8d281df851739e6aba5 100644 (file)
@@ -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<print_latex>(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<print_latex>(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<print_latex>(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<print_latex>(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 << "}";
        }
 }
 
index cbf370dbb16542f1be19e16566a547a998e90f01..b092b5370aff3eef118d83560f5d04e29009cf64 100644 (file)
@@ -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,
index 8ba5bf42f75544e19755349be17fea1b29733a46..5df99b6639bca69a3bc3b7fd8e8e3cc9c56f74a0 100644 (file)
@@ -94,7 +94,7 @@ exp   : T_NUMBER              {$$ = $1;}
        | T_LITERAL             {$$ = $1;}
        | T_DIGITS              {$$ = $1;}
        | T_SYMBOL '(' exprseq ')' {
-               string n = ex_to<symbol>($1).get_name();
+               std::string n = ex_to<symbol>($1).get_name();
                if (n == "sqrt") {
                        if ($3.nops() != 1)
                                throw (std::runtime_error("too many arguments to sqrt()"));
index 3d735fe46689aa57fc0d973a0b8588e07f2049f8..0bedb6bb1a1e506fc0017ea3243d66c64ed3797a 100644 (file)
@@ -147,6 +147,9 @@ void matrix::print(const print_context & c, unsigned level) const
 
        } else {
 
+               if (is_a<print_python_repr>(c))
+                       c.s << class_name() << '(';
+
                c.s << "[";
                for (unsigned y=0; y<row-1; ++y) {
                        c.s << "[";
@@ -165,6 +168,9 @@ void matrix::print(const print_context & c, unsigned level) const
                m[row*col-1].print(c);
                c.s << "]]";
 
+               if (is_a<print_python_repr>(c))
+                       c.s << ')';
+
        }
 }
 
index 64b090af6ecdd1f721e69617b022625c1ef5df26..47f74f6f4c1b920050adf85a66154029bb42808c 100644 (file)
@@ -169,6 +169,16 @@ void mul::print(const print_context & c, unsigned level) const
                if (precedence() <= level)
                        c.s << ")";
 
+       } else if (is_a<print_python_repr>(c)) {
+               c.s << class_name() << '(';
+               unsigned end = nops();
+               if (end)
+                       op(0).print(c);
+               for (unsigned i=1; i<end; ++i) {
+                       c.s << ',';
+                       op(i).print(c);
+               }
+               c.s << ')';
        } else {
 
                if (precedence() <= level) {
index a12fd6facfa2e7d2168f99ec552beb9d85ca742e..3f0ec97737430b3c7443cfd8ca76826fb0893506 100644 (file)
@@ -111,9 +111,9 @@ void ncmul::print(const print_context & c, unsigned level) const
 
                inherited::print(c, level);
 
-       } else if (is_a<print_csrc>(c)) {
+       } else if (is_a<print_csrc>(c) || is_a<print_python_repr>(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());
index fe51c60795dd7b4e92fc082ceac82b2bace6e787..01c58453363e508d4321362ac663f09219991037 100644 (file)
@@ -389,6 +389,8 @@ void numeric::print(const print_context & c, unsigned level) const
                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())) {
@@ -446,6 +448,8 @@ void numeric::print(const print_context & c, unsigned level) const
                                        c.s << par_close;
                        }
                }
+               if (is_a<print_python_repr>(c))
+                       c.s << "')";
        }
 }
 
index 78c9c9696d14303b468a4f3b1c69b460560a314a..72ec5aa368ae65c3bc8565309e379074e00d134e 100644 (file)
@@ -159,6 +159,14 @@ void power::print(const print_context & c, unsigned level) const
                        c.s << ')';
                }
 
+       } else if (is_a<print_python_repr>(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<print_python>(c))
+                               c.s << "**";
+                       else
+                               c.s << '^';
                        if (is_a<print_latex>(c))
                                c.s << '{';
                        exponent.print(c, precedence());
index bd365cfe794524c59ed85ca01fdb6aa4e23c3025..2b1e323bffad93013b635f483c99220660c5f5a2 100644 (file)
@@ -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)
index 1d99639ea38f0f7d4adb9cd6c251584dfad51356..3e55db5d7fde5e7bd86ffcfc3bbe5533fe0bf706 100644 (file)
@@ -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
 {
index 80433824ed7465e82702958142148a72fac77e27..720c4eca856f1de8315826c056f436ab4c29a24d 100644 (file)
@@ -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<print_python_repr>(c)) {
+               c.s << class_name() << "(relational(";
+               var.print(c);
+               c.s << ',';
+               point.print(c);
+               c.s << "),[";
+               unsigned num = seq.size();
+               for (unsigned i=0; i<num; ++i) {
+                       if (i)
+                               c.s << ',';
+                       c.s << '(';
+                       seq[i].rest.print(c);
+                       c.s << ',';
+                       seq[i].coeff.print(c);
+                       c.s << ')';
+               }
+               c.s << "])";
        } else {
 
                if (precedence() <= level)
@@ -174,7 +191,10 @@ void pseries::print(const print_context & c, unsigned level) const
                                        } else
                                                var.print(c);
                                        if (i->coeff.compare(_ex1)) {
-                                               c.s << '^';
+                                               if (is_a<print_python>(c))
+                                                       c.s << "**";
+                                               else
+                                                       c.s << '^';
                                                if (i->coeff.info(info_flags::negative)) {
                                                        c.s << par_open;
                                                        i->coeff.print(c);
index 69363765c3ffb8b2b0821b29f5ac8d3a28d7ee33..484799d45bd9b7a1a9065e85fab2575c4f7d7373 100644 (file)
@@ -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<print_python_repr>(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<print_python_repr>(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<relational>(other));
-       const relational &oth = static_cast<const relational &>(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
 //////////
index 8f2263e64b4e220e34bf906b59306ce5a2165502..b04d11c70de99003c33816b8c9e947f55e648941 100644 (file)
@@ -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
index 5d2ac8ced8459b84a1df3f3c1a0d43e7a8ca363b..39f2335abcd1911c8e4460aaa1d16c0ba1989e8a 100644 (file)
@@ -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<print_latex>(c))
+       } else if (is_a<print_latex>(c)) {
                c.s << TeX_name;
-       else
+       } else if (is_a<print_python_repr>(c)) {
+               c.s << class_name() << "('" << name;
+               if (TeX_name != default_TeX_name())
+                       c.s << "','" << TeX_name;
+               c.s << "')";
+       } else
                c.s << name;
 }
 
index 37e23e97e6883715373fa258cbe38446ce68ef2c..10aa7e40ac8c9007e0916615e74118720c2650d2 100644 (file)
@@ -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<print_python_repr>(c)) {
+               c.s << class_name() << '(' << label << ')';
        } else
                c.s << "$" << label;
 }