]> www.ginac.de Git - ginac.git/blob - ginac/polynomial/upoly_io.cpp
Update copyright notice.
[ginac.git] / ginac / polynomial / upoly_io.cpp
1 /** @file upoly_io.cpp
2  *
3  *  Input/Output function for univariate polynomials. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2010 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #include "upoly.h"
24 #include "upoly_io.h"
25
26 #include <cln/integer_io.h>
27 #include <iostream>
28 #include <string>
29
30 namespace GiNaC {
31
32 using std::ostream;
33 using std::string;
34
35 template<typename T> static void 
36 print(const T& p, ostream& os, const string& varname = string("x"))
37 {
38         if (p.size() == 0)
39                 os << '0';
40
41         bool seen_nonzero = false;
42
43         for (std::size_t i = p.size(); i-- != 0;  ) {
44                 if (zerop(p[i])) {
45                         if (seen_nonzero)
46                                 continue;
47                         os << "+ [WARNING: 0]*" << varname << "^" << i << "]";
48                         continue;
49                 }
50                 seen_nonzero = true;
51                 os << "+ (" << p[i] << ")";
52                 if (i != 0)
53                         os << "*" << varname;
54                 if (i > 1)
55                         os << '^' << i;
56                 os << " ";
57         }
58 }
59
60 #define DEFINE_OPERATOR_OUT(type)                         \
61 std::ostream& operator<<(std::ostream& os, const type& p) \
62 {                                                         \
63         print(p, os);                                         \
64         return os;                                            \
65 }                                                         \
66 void dbgprint(const type& p)                              \
67 {                                                         \
68         print(p, std::cerr);                                  \
69 }
70
71 DEFINE_OPERATOR_OUT(upoly);
72 DEFINE_OPERATOR_OUT(umodpoly);
73 #undef DEFINE_OPERATOR_OUT
74
75 } // namespace GiNaC