3 * Test of Chinese remainder algorithm. */
6 * GiNaC Copyright (C) 1999-2016 Johannes Gutenberg University Mainz, Germany
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.
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.
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
23 #include "polynomial/cra_garner.h"
25 #include <cln/integer.h>
26 #include <cln/integer_io.h>
27 #include <cln/random.h>
28 #include <cln/numtheory.h>
38 /// Generate a sequences of primes p_i such that \prod_i p_i < limit
39 static std::vector<cln::cl_I>
40 make_random_moduli(const cln::cl_I& limit);
42 static std::vector<cln::cl_I>
43 calc_residues(const cln::cl_I& x, const std::vector<cln::cl_I>& moduli);
45 static void dump(const std::vector<cln::cl_I>& v);
47 /// Make @a n random relatively prime moduli, each < limit, make a
48 /// random number x < \prod_{i=0}{n-1}, calculate residues, and
49 /// compute x' by chinese remainder algorithm. Check if the result
50 /// of computation matches the original value x.
51 static void run_test_once(const cln::cl_I& lim)
53 std::vector<cln::cl_I> moduli = make_random_moduli(lim);
54 cln::cl_I x = random_I(lim) + 1;
59 std::vector<cln::cl_I> residues = calc_residues(x, moduli);
64 x_test = integer_cra(residues, moduli);
65 } catch (std::exception& oops) {
66 std::cerr << "Oops: " << oops.what() << std::endl;
74 std::cerr << "Expected x = " << x << ", got " <<
75 x_test << " instead" << std::endl;
76 std::cerr << "moduli = ";
78 std::cerr << std::endl;
79 std::cerr << "residues = ";
81 std::cerr << std::endl;
82 throw std::logic_error("bug in integer_cra?");
86 static void run_test(const cln::cl_I& limit, const std::size_t ntimes)
88 for (std::size_t i = 0; i < ntimes; ++i)
92 int main(int argc, char** argv)
94 typedef std::map<cln::cl_I, std::size_t> map_t;
96 // Run 1024 tests with native 32-bit numbers
97 the_map[cln::cl_I(std::numeric_limits<int>::max())] = 1024;
99 // Run 512 tests with native 64-bit integers
100 if (sizeof(long) > sizeof(int))
101 the_map[cln::cl_I(std::numeric_limits<long>::max())] = 512;
103 // Run 32 tests with a bit bigger numbers
104 the_map[cln::cl_I("987654321098765432109876543210")] = 32;
106 std::cout << "examining Garner's integer chinese remainder algorithm " << std::flush;
108 for (map_t::const_iterator i = the_map.begin(); i != the_map.end(); ++i)
109 run_test(i->first, i->second);
114 static std::vector<cln::cl_I>
115 calc_residues(const cln::cl_I& x, const std::vector<cln::cl_I>& moduli)
117 std::vector<cln::cl_I> residues(moduli.size());
118 for (std::size_t i = moduli.size(); i-- != 0; )
119 residues[i] = mod(x, moduli[i]);
123 static std::vector<cln::cl_I>
124 make_random_moduli(const cln::cl_I& limit)
126 std::vector<cln::cl_I> moduli;
128 cln::cl_I next = random_I(std::min(limit >> 1, cln::cl_I(128)));
131 cln::cl_I tmp = nextprobprime(next);
132 next = tmp + random_I(cln::cl_I(10)) + 1;
134 moduli.push_back(tmp);
136 } while (prod < limit || (count < 2));
140 static void dump(const std::vector<cln::cl_I>& v)
143 for (std::size_t i = 0; i < v.size(); ++i)
144 std::cerr << v[i] << " ";