use new-style print methods
authorChristian Bauer <Christian.Bauer@uni-mainz.de>
Wed, 6 Aug 2003 19:07:46 +0000 (19:07 +0000)
committerChristian Bauer <Christian.Bauer@uni-mainz.de>
Wed, 6 Aug 2003 19:07:46 +0000 (19:07 +0000)
ginac/idx.cpp
ginac/idx.h
ginac/matrix.cpp
ginac/matrix.h
ginac/relational.cpp
ginac/relational.h

index 1b4bf6f..4dd4d1a 100644 (file)
@@ -134,7 +134,7 @@ DEFAULT_UNARCHIVE(spinidx)
 // functions overriding virtual functions from base classes
 //////////
 
-void idx::do_print_idx(const print_context & c, unsigned level) const
+void idx::print_index(const print_context & c, unsigned level) const
 {
        bool need_parens = !(is_exactly_a<numeric>(value) || is_a<symbol>(value));
        if (need_parens)
@@ -152,13 +152,13 @@ void idx::do_print_idx(const print_context & c, unsigned level) const
 void idx::do_print(const print_context & c, unsigned level) const
 {
        c.s << ".";
-       do_print_idx(c, level);
+       print_index(c, level);
 }
 
 void idx::do_print_latex(const print_latex & c, unsigned level) const
 {
        c.s << "{";
-       do_print_idx(c, level);
+       print_index(c, level);
        c.s << "}";
 }
 
@@ -177,7 +177,7 @@ void varidx::do_print(const print_context & c, unsigned level) const
                c.s << ".";
        else
                c.s << "~";
-       do_print_idx(c, level);
+       print_index(c, level);
 }
 
 void varidx::do_print_tree(const print_tree & c, unsigned level) const
@@ -198,7 +198,7 @@ void spinidx::do_print(const print_context & c, unsigned level) const
                c.s << "~";
        if (dotted)
                c.s << "*";
-       do_print_idx(c, level);
+       print_index(c, level);
 }
 
 void spinidx::do_print_latex(const print_latex & c, unsigned level) const
@@ -207,7 +207,7 @@ void spinidx::do_print_latex(const print_latex & c, unsigned level) const
                c.s << "\\dot{";
        else
                c.s << "{";
-       do_print_idx(c, level);
+       print_index(c, level);
        c.s << "}";
 }
 
index 16c515f..13004fd 100644 (file)
@@ -94,7 +94,7 @@ public:
        ex minimal_dim(const idx & other) const;
 
 protected:
-       void do_print_idx(const print_context & c, unsigned level) const;
+       void print_index(const print_context & c, unsigned level) const;
        void do_print(const print_context & c, unsigned level) const;
        void do_print_latex(const print_latex & c, unsigned level) const;
        void do_print_tree(const print_tree & c, unsigned level) const;
index 4828bb9..c9b3521 100644 (file)
 #include "symbol.h"
 #include "operators.h"
 #include "normal.h"
-#include "print.h"
 #include "archive.h"
 #include "utils.h"
 
 namespace GiNaC {
 
-GINAC_IMPLEMENT_REGISTERED_CLASS(matrix, basic)
+GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(matrix, basic,
+  print_func<print_context>(&matrix::do_print).
+  print_func<print_latex>(&matrix::do_print_latex).
+  print_func<print_tree>(&basic::do_print_tree).
+  print_func<print_python_repr>(&matrix::do_print_python_repr))
 
 //////////
 // default constructor
@@ -134,54 +137,41 @@ DEFAULT_UNARCHIVE(matrix)
 
 // public
 
-void matrix::print(const print_context & c, unsigned level) const
+void matrix::print_elements(const print_context & c, const std::string & row_start, const std::string & row_end, const std::string & row_sep, const std::string & col_sep) const
 {
-       if (is_a<print_tree>(c)) {
-
-               inherited::print(c, level);
-
-       } else {
-
-               if (is_a<print_python_repr>(c))
-                       c.s << class_name() << '(';
-
-               if (is_a<print_latex>(c))
-                       c.s << "\\left(\\begin{array}{" << std::string(col,'c') << "}";
-               else
-                       c.s << "[";
-
-               for (unsigned ro=0; ro<row; ++ro) {
-                       if (!is_a<print_latex>(c))
-                               c.s << "[";
-                       for (unsigned co=0; co<col; ++co) {
-                               m[ro*col+co].print(c);
-                               if (co<col-1) {
-                                       if (is_a<print_latex>(c))
-                                               c.s << "&";
-                                       else
-                                               c.s << ",";
-                               } else {
-                                       if (!is_a<print_latex>(c))
-                                               c.s << "]";
-                               }
-                       }
-                       if (ro<row-1) {
-                               if (is_a<print_latex>(c))
-                                       c.s << "\\\\";
-                               else
-                                       c.s << ",";
-                       }
+       for (unsigned ro=0; ro<row; ++ro) {
+               c.s << row_start;
+               for (unsigned co=0; co<col; ++co) {
+                       m[ro*col+co].print(c);
+                       if (co < col-1)
+                               c.s << col_sep;
+                       else
+                               c.s << row_end;
                }
+               if (ro < row-1)
+                       c.s << row_sep;
+       }
+}
 
-               if (is_a<print_latex>(c))
-                       c.s << "\\end{array}\\right)";
-               else
-                       c.s << "]";
+void matrix::do_print(const print_context & c, unsigned level) const
+{
+       c.s << "[";
+       print_elements(c, "[", "]", ",", ",");
+       c.s << "]";
+}
 
-               if (is_a<print_python_repr>(c))
-                       c.s << ')';
+void matrix::do_print_latex(const print_latex & c, unsigned level) const
+{
+       c.s << "\\left(\\begin{array}{" << std::string(col,'c') << "}";
+       print_elements(c, "", "", "\\\\", "&");
+       c.s << "\\end{array}\\right)";
+}
 
-       }
+void matrix::do_print_python_repr(const print_python_repr & c, unsigned level) const
+{
+       c.s << class_name() << '(';
+       print_elements(c, "[", "]", ",", ",");
+       c.s << ')';
 }
 
 /** nops is defined to be rows x columns. */
index f951850..f5cd5de 100644 (file)
@@ -43,7 +43,6 @@ public:
        
        // functions overriding virtual functions from base classes
 public:
-       void print(const print_context & c, unsigned level = 0) const;
        size_t nops() const;
        ex op(size_t i) const;
        ex & let_op(size_t i);
@@ -87,6 +86,11 @@ protected:
        int division_free_elimination(const bool det = false);
        int fraction_free_elimination(const bool det = false);
        int pivot(unsigned ro, unsigned co, bool symbolic = true);
+
+       void print_elements(const print_context & c, const std::string & row_start, const std::string & row_end, const std::string & row_sep, const std::string & col_sep) const;
+       void do_print(const print_context & c, unsigned level) const;
+       void do_print_latex(const print_latex & c, unsigned level) const;
+       void do_print_python_repr(const print_python_repr & c, unsigned level) const;
        
 // member variables
 protected:
index 453b4c0..05fe56e 100644 (file)
 #include "relational.h"
 #include "operators.h"
 #include "numeric.h"
-#include "print.h"
 #include "archive.h"
 #include "utils.h"
 
 namespace GiNaC {
 
-GINAC_IMPLEMENT_REGISTERED_CLASS(relational, basic)
+GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(relational, basic,
+  print_func<print_context>(&relational::do_print).
+  print_func<print_tree>(&basic::do_print_tree).
+  print_func<print_python_repr>(&relational::do_print_python_repr))
 
 //////////
 // default constructor
@@ -78,57 +80,55 @@ DEFAULT_UNARCHIVE(relational)
 
 // public
 
-void relational::print(const print_context & c, unsigned level) const
+static void print_operator(const print_context & c, relational::operators o)
 {
-       if (is_a<print_tree>(c)) {
-
-               inherited::print(c, level);
-
-       } else {
-
-               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 << "==";
-                               break;
-                       case not_equal:
-                               c.s << "!=";
-                               break;
-                       case less:
-                               c.s << "<";
-                               break;
-                       case less_or_equal:
-                               c.s << "<=";
-                               break;
-                       case greater:
-                               c.s << ">";
-                               break;
-                       case greater_or_equal:
-                               c.s << ">=";
-                               break;
-                       default:
-                               c.s << "(INVALID RELATIONAL OPERATOR)";
-               }
-               if (is_a<print_python_repr>(c))
-                       c.s << "')";
-               else {
-                       rh.print(c, precedence());
-                       if (precedence() <= level)
-                               c.s << ")";
-               }
+       switch (o) {
+       case relational::equal:
+               c.s << "==";
+               break;
+       case relational::not_equal:
+               c.s << "!=";
+               break;
+       case relational::less:
+               c.s << "<";
+               break;
+       case relational::less_or_equal:
+               c.s << "<=";
+               break;
+       case relational::greater:
+               c.s << ">";
+               break;
+       case relational::greater_or_equal:
+               c.s << ">=";
+               break;
+       default:
+               c.s << "(INVALID RELATIONAL OPERATOR)";
+               break;
        }
 }
 
+void relational::do_print(const print_context & c, unsigned level) const
+{
+       if (precedence() <= level)
+               c.s << "(";
+       lh.print(c, precedence());
+       print_operator(c, o);
+       rh.print(c, precedence());
+       if (precedence() <= level)
+               c.s << ")";
+}
+
+void relational::do_print_python_repr(const print_python_repr & c, unsigned level) const
+{
+       c.s << class_name() << '(';
+       lh.print(c);
+       c.s << ',';
+       rh.print(c);
+       c.s << ",'";
+       print_operator(c, o);
+       c.s << "')";
+}
+
 bool relational::info(unsigned inf) const
 {
        switch (inf) {
index cbe5146..a6727de 100644 (file)
@@ -51,7 +51,6 @@ public:
        
        // functions overriding virtual functions from base classes
 public:
-       void print(const print_context & c, unsigned level = 0) const;
        unsigned precedence() const {return 20;}
        bool info(unsigned inf) const;
        size_t nops() const;
@@ -68,6 +67,10 @@ protected:
        unsigned calchash() const;
 
        // new virtual functions which can be overridden by derived classes
+protected:
+       void do_print(const print_context & c, unsigned level) const;
+       void do_print_python_repr(const print_python_repr & c, unsigned level) const;
+
 public:
        virtual ex lhs() const;
        virtual ex rhs() const;