Implemented modular GCD algorithm for univariate polynomials.
[ginac.git] / ginac / polynomial / upoly_io.cpp
1 #include <iostream>
2 #include <string>
3 #include "upoly.hpp"
4 #include "upoly_io.hpp"
5 #include <cln/integer_io.h>
6
7 namespace GiNaC
8 {
9 using std::ostream;
10 using std::string;
11
12 template<typename T> static void 
13 print(const T& p, ostream& os, const string& varname = string("x"))
14 {
15         if (p.size() == 0)
16                 os << '0';
17
18         bool seen_nonzero = false;
19
20         for (std::size_t i = p.size(); i-- != 0;  ) {
21                 if (zerop(p[i])) {
22                         if (seen_nonzero)
23                                 continue;
24                         os << "+ [WARNING: 0]*" << varname << "^" << i << "]";
25                         continue;
26                 }
27                 seen_nonzero = true;
28                 os << "+ (" << p[i] << ")";
29                 if (i != 0)
30                         os << "*" << varname;
31                 if (i > 1)
32                         os << '^' << i;
33                 os << " ";
34         }
35 }
36
37 #define DEFINE_OPERATOR_OUT(type)                                       \
38 std::ostream& operator<<(std::ostream& os, const type& p)               \
39 {                                                                       \
40         print(p, os);                                                   \
41         return os;                                                      \
42 }                                                                       \
43 void dbgprint(const type& p)                                            \
44 {                                                                       \
45         print(p, std::cerr);                                            \
46 }
47
48 DEFINE_OPERATOR_OUT(upoly);
49 DEFINE_OPERATOR_OUT(umodpoly);
50 #undef DEFINE_OPERATOR_OUT
51
52 } // namespace GiNaC
53