3 * Test of Chinese remainder algorithm. */
6 * GiNaC Copyright (C) 1999-2015 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>
37 /// Generate a sequences of primes p_i such that \prod_i p_i < limit
38 static std::vector<cln::cl_I>
39 make_random_moduli(const cln::cl_I& limit);
41 static std::vector<cln::cl_I>
42 calc_residues(const cln::cl_I& x, const std::vector<cln::cl_I>& moduli);
44 static void dump(const std::vector<cln::cl_I>& v);
46 /// Make @a n random relatively prime moduli, each < limit, make a
47 /// random number x < \prod_{i=0}{n-1}, calculate residues, and
48 /// compute x' by chinese remainder algorithm. Check if the result
49 /// of computation matches the original value x.
50 static void run_test_once(const cln::cl_I& lim)
52 std::vector<cln::cl_I> moduli = make_random_moduli(lim);
53 cln::cl_I x = random_I(lim) + 1;
58 std::vector<cln::cl_I> residues = calc_residues(x, moduli);
63 x_test = integer_cra(residues, moduli);
64 } catch (std::exception& oops) {
65 std::cerr << "Oops: " << oops.what() << std::endl;
73 std::cerr << "Expected x = " << x << ", got " <<
74 x_test << " instead" << std::endl;
75 std::cerr << "moduli = ";
77 std::cerr << std::endl;
78 std::cerr << "residues = ";
80 std::cerr << std::endl;
81 throw std::logic_error("bug in integer_cra?");
85 static void run_test(const cln::cl_I& limit, const std::size_t ntimes)
87 for (std::size_t i = 0; i < ntimes; ++i)
91 int main(int argc, char** argv)
93 typedef std::map<cln::cl_I, std::size_t> map_t;
95 // Run 1024 tests with native 32-bit numbers
96 the_map[cln::cl_I(std::numeric_limits<int>::max())] = 1024;
98 // Run 512 tests with native 64-bit integers
99 if (sizeof(long) > sizeof(int))
100 the_map[cln::cl_I(std::numeric_limits<long>::max())] = 512;
102 // Run 32 tests with a bit bigger numbers
103 the_map[cln::cl_I("987654321098765432109876543210")] = 32;
105 std::cout << "examining Garner's integer chinese remainder algorithm " << std::flush;
107 for (map_t::const_iterator i = the_map.begin(); i != the_map.end(); ++i)
108 run_test(i->first, i->second);
113 static std::vector<cln::cl_I>
114 calc_residues(const cln::cl_I& x, const std::vector<cln::cl_I>& moduli)
116 std::vector<cln::cl_I> residues(moduli.size());
117 for (std::size_t i = moduli.size(); i-- != 0; )
118 residues[i] = mod(x, moduli[i]);
122 static std::vector<cln::cl_I>
123 make_random_moduli(const cln::cl_I& limit)
125 std::vector<cln::cl_I> moduli;
127 cln::cl_I next = random_I(std::min(limit >> 1, cln::cl_I(128)));
130 cln::cl_I tmp = nextprobprime(next);
131 next = tmp + random_I(cln::cl_I(10)) + 1;
133 moduli.push_back(tmp);
135 } while (prod < limit || (count < 2));
139 static void dump(const std::vector<cln::cl_I>& v)
142 for (std::size_t i = 0; i < v.size(); ++i)
143 std::cerr << v[i] << " ";