1 #include "polynomial/upoly.hpp"
2 #include "polynomial/upoly_io.hpp"
3 #include "polynomial/mod_gcd.hpp"
5 #include <cln/random.h>
11 static upoly ex_to_upoly(const ex& e, const symbol& x);
12 static ex upoly_to_ex(const upoly& p, const symbol& x);
14 // make a univariate polynomial \in Z[x] of degree deg
15 static upoly make_random_upoly(const std::size_t deg);
17 static void run_test_once(const std::size_t deg)
19 static const symbol xsym("x");
21 const upoly a = make_random_upoly(deg);
22 const upoly b = make_random_upoly(deg);
27 ex ea = upoly_to_ex(a, xsym);
28 ex eb = upoly_to_ex(b, xsym);
31 const upoly g_check = ex_to_upoly(eg, xsym);
33 std::cerr << "a = " << a << std::endl;
34 std::cerr << "b = " << b << std::endl;
35 std::cerr << "mod_gcd(a, b) = " << g << std::endl;
36 std::cerr << "sr_gcd(a, b) = " << g_check << std::endl;
37 throw std::logic_error("bug in mod_gcd");
41 int main(int argc, char** argv)
43 std::cout << "examining modular gcd. ";
44 std::map<std::size_t, std::size_t> n_map;
45 // run 256 tests with polynomials of degree 10
47 // run 32 tests with polynomials of degree 100
49 std::map<std::size_t, std::size_t>::const_iterator i = n_map.begin();
50 for (; i != n_map.end(); ++i) {
51 for (std::size_t k = 0; k < i->second; ++k)
52 run_test_once(i->first);
57 static upoly ex_to_upoly(const ex& e, const symbol& x)
59 upoly p(e.degree(x) + 1);
60 for (int i = 0; i <= e.degree(x); ++i)
61 p[i] = cln::the<cln::cl_I>(ex_to<numeric>(e.coeff(x, i)).to_cl_N());
65 static ex upoly_to_ex(const upoly& p, const symbol& x)
67 exvector tv(p.size());
68 for (std::size_t i = 0; i < p.size(); ++i)
69 tv[i] = pow(x, i)*numeric(p[i]);
70 return (new add(tv))->setflag(status_flags::dynallocated);
73 static upoly make_random_upoly(const std::size_t deg)
75 static const cln::cl_I biggish("98765432109876543210");
77 for (std::size_t i = 0; i <= deg; ++i)
78 p[i] = cln::random_I(biggish);
80 // Make sure the leading coefficient is non-zero
82 p[deg] = cln::random_I(biggish);