1 // This file is part of the FXT library.
2 // Copyright (C) 2010, 2011, 2012, 2018 Joerg Arndt
3 // License: GNU General Public License version 3 or later,
4 // see the file COPYING.txt in the main directory.
5 
6 #include "fxtio.h"
7 
8 #include "bits/bithigh.h"  // highest_one_idx()
9 #include "bits/print-bin.h"
10 
11 #include "bits/bitsperlong.h"
12 #include "fxttypes.h"  // ulong
13 
14 
15 void
bitpol_print(const char * bla,ulong c,bool sq)16 bitpol_print(const char *bla, ulong c, bool sq/*=true*/)
17 // print as:
18 //  x^7 + x^4 + x^3 + 1  if sq==true
19 //  x^7+x^4+x^3+1  if sq==false
20 {
21     cout << bla;
22 
23     ulong i = BITS_PER_LONG-1;
24     ulong t = 1UL<<i;
25     while ( c )
26     {
27         ulong q = c & t;
28         if ( q )
29         {
30             c ^= q;
31             if ( i<=1 )  cout << (i ? "x" : "1");
32             else
33             {
34                 cout << "x^" << i;
35             }
36             if ( c )
37             {
38                 if ( sq )  cout << " + ";
39                 else       cout << "+";
40             }
41         }
42         t>>=1;
43         --i;
44     }
45 }
46 // -------------------------
47 
48 
49 void
bitpol_print_bin(const char * bla,ulong c)50 bitpol_print_bin(const char *bla, ulong c)
51 // print as:  1..11.1
52 {
53     ulong d = highest_one_idx(c);
54     print_bin(bla, c, d+1);
55 }
56 // -------------------------
57 
58 
59 void
bitpol_print_coeff(const char * bla,ulong c)60 bitpol_print_coeff(const char *bla, ulong c)
61 // print as:  [7, 4, 3, 0]
62 {
63     cout << bla;
64     cout << "[";
65     ulong i = BITS_PER_LONG-1;
66     ulong t = 1UL<<i;
67     while ( c )
68     {
69         ulong q = c & t;
70         if ( q )
71         {
72             c ^= q;
73             cout << i;
74             if ( c )  cout << ",";
75         }
76         t>>=1;
77         --i;
78     }
79     cout << "]";
80 }
81 // -------------------------
82 
83 void
bitpol_print_tex(const char * bla,ulong c)84 bitpol_print_tex(const char *bla, ulong c)
85 // print as:  x^{7} + x^{4} + x^{3} + 1
86 {
87     cout << bla;
88     ulong i = BITS_PER_LONG-1;
89     ulong t = 1UL<<i;
90     while ( c )
91     {
92         ulong q = c & t;
93         if ( q )
94         {
95             c ^= q;
96             if ( i<=1 )  cout << (i ? "x" : "1");
97             else
98             {
99                 cout << "x^" << "{" << i << "}";
100             }
101             if ( c )  cout << " + ";
102         }
103         t>>=1;
104         --i;
105     }
106 }
107 // -------------------------
108 
109 
110 void
bitpol_print_factorization(const char * bla,const ulong * f,const ulong * e,ulong fct)111 bitpol_print_factorization(const char *bla, const ulong *f, const ulong *e, ulong fct)
112 // print as:  (x+1)^5 *  (x^2+x+1)
113 {
114     cout << bla;
115     for (ulong i=0; i<fct; ++i)
116     {
117         cout << " (";
118         bitpol_print("", f[i], false);
119         cout << ")";
120         if ( e[i]>1 )  cout << "^" << e[i];
121         if ( i<fct-1 )  cout << " * ";
122     }
123 }
124 // -------------------------
125 
126 void
bitpol_print_bin_factorization(const char * bla,const ulong * f,const ulong * e,ulong fct)127 bitpol_print_bin_factorization(const char *bla, const ulong *f, const ulong *e, ulong fct)
128 // print as:  (1.)^5 (111)
129 {
130     cout << bla;
131     for (ulong i=0; i<fct; ++i)
132     {
133         cout << " (";
134         bitpol_print_bin("", f[i]);
135         cout << ")";
136         if ( e[i]>1 )  cout << "^" << e[i];
137 //        if ( i<fct-1 )  cout << " * ";
138     }
139 }
140 // -------------------------
141 
142 
143 void
bitpol_print_coeff_factorization(const char * bla,const ulong * f,const ulong * e,ulong fct)144 bitpol_print_coeff_factorization(const char *bla, const ulong *f, const ulong *e, ulong fct)
145 // print as:  [1,0]^5 * [2,1,0]
146 {
147     cout << bla;
148     for (ulong i=0; i<fct; ++i)
149     {
150         bitpol_print_coeff("", f[i]);
151         if ( e[i]>1 )  cout << "^" << e[i];
152         if ( i<fct-1 )  cout << " * ";
153     }
154 }
155 // -------------------------
156 
157 
158 
159 void
bitpol_print_tex_factorization(const char * bla,const ulong * f,const ulong * e,ulong fct)160 bitpol_print_tex_factorization(const char *bla, const ulong *f, const ulong *e, ulong fct)
161 // print as:  \left(x + 1\right)^{5} \cdot  \left(x^{2} + x + 1\right)
162 {
163     cout << bla;
164     for (ulong i=0; i<fct; ++i)
165     {
166         cout << " \\left(";
167         bitpol_print_tex("", f[i]);
168         cout << "\\right)";
169         if ( e[i]>1 )  cout << "^{" << e[i] << "}";
170         if ( i<fct-1 )  cout << " \\cdot ";
171     }
172 }
173 // -------------------------
174 
175 void
bitpol_print_short_factorization(const char * bla,const ulong * f,const ulong * e,ulong fct)176 bitpol_print_short_factorization(const char *bla, const ulong *f, const ulong *e, ulong fct)
177 // print as:  [deg1 ex1] [deg2 ex2] ... [degk exk]
178 {
179     cout << bla;
180     for (ulong i=0; i<fct; ++i)
181     {
182         cout << " [";
183         cout << highest_one_idx(f[i]);
184         cout << " " << e[i];
185         cout << "]";
186 //        if ( i<fct-1 )  cout << "  * ";
187     }
188 }
189 // -------------------------
190 
191