1 /** @file exam_misc.cpp
7 * GiNaC Copyright (C) 1999-2009 Johannes Gutenberg University Mainz, Germany
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <cln/random.h>
29 #include "polynomial/upoly.h"
30 #include "polynomial/upoly_io.h"
31 #include "polynomial/mod_gcd.h"
33 using namespace GiNaC;
35 static upoly ex_to_upoly(const ex& e, const symbol& x);
36 static ex upoly_to_ex(const upoly& p, const symbol& x);
38 // make a univariate polynomial \in Z[x] of degree deg
39 static upoly make_random_upoly(const std::size_t deg);
41 static void run_test_once(const std::size_t deg)
43 static const symbol xsym("x");
45 const upoly a = make_random_upoly(deg);
46 const upoly b = make_random_upoly(deg);
51 ex ea = upoly_to_ex(a, xsym);
52 ex eb = upoly_to_ex(b, xsym);
55 const upoly g_check = ex_to_upoly(eg, xsym);
57 std::cerr << "a = " << a << std::endl;
58 std::cerr << "b = " << b << std::endl;
59 std::cerr << "mod_gcd(a, b) = " << g << std::endl;
60 std::cerr << "sr_gcd(a, b) = " << g_check << std::endl;
61 throw std::logic_error("bug in mod_gcd");
65 int main(int argc, char** argv)
67 std::cout << "examining modular gcd. ";
68 std::map<std::size_t, std::size_t> n_map;
69 // run 256 tests with polynomials of degree 10
71 // run 32 tests with polynomials of degree 100
73 std::map<std::size_t, std::size_t>::const_iterator i = n_map.begin();
74 for (; i != n_map.end(); ++i) {
75 for (std::size_t k = 0; k < i->second; ++k)
76 run_test_once(i->first);
81 static upoly ex_to_upoly(const ex& e, const symbol& x)
83 upoly p(e.degree(x) + 1);
84 for (int i = 0; i <= e.degree(x); ++i)
85 p[i] = cln::the<cln::cl_I>(ex_to<numeric>(e.coeff(x, i)).to_cl_N());
89 static ex upoly_to_ex(const upoly& p, const symbol& x)
91 exvector tv(p.size());
92 for (std::size_t i = 0; i < p.size(); ++i)
93 tv[i] = pow(x, i)*numeric(p[i]);
94 return (new add(tv))->setflag(status_flags::dynallocated);
97 static upoly make_random_upoly(const std::size_t deg)
99 static const cln::cl_I biggish("98765432109876543210");
101 for (std::size_t i = 0; i <= deg; ++i)
102 p[i] = cln::random_I(biggish);
104 // Make sure the leading coefficient is non-zero
105 while (zerop(p[deg]))
106 p[deg] = cln::random_I(biggish);