]> www.ginac.de Git - ginac.git/blob - check/exam_cra.cpp
Fixed a problem in polynomial divide by deactivating a recursion that lead to a
[ginac.git] / check / exam_cra.cpp
1 #include <iostream>
2 #include <cln/integer.h>
3 #include <cln/integer_io.h>
4 #include <cln/random.h>
5 #include <cln/numtheory.h>
6 #include "polynomial/cra_garner.hpp"
7 #include <map>
8 #include <vector>
9 #include <stdexcept>
10 #include <limits>
11 using namespace cln;
12 using namespace std;
13
14 /// Generate a sequences of primes p_i such that \prod_i p_i < limit
15 static std::vector<cln::cl_I>
16 make_random_moduli(const cln::cl_I& limit);
17
18 static std::vector<cln::cl_I>
19 calc_residues(const cln::cl_I& x, const std::vector<cln::cl_I>& moduli);
20
21 static void dump(const std::vector<cln::cl_I>& v);
22
23 /// Make @a n random relatively prime moduli, each < limit, make a 
24 /// random number x < \prod_{i=0}{n-1}, calculate residues, and 
25 /// compute x' by chinese remainder algorithm. Check if the result
26 /// of computation matches the original value x.
27 static void run_test_once(const cln::cl_I& lim)
28 {
29         std::vector<cln::cl_I> moduli = make_random_moduli(lim);
30         cln::cl_I x = random_I(lim) + 1;
31
32         if (x > (lim >> 1))
33                 x = x - lim;
34
35         std::vector<cln::cl_I> residues = calc_residues(x, moduli);
36         cln::cl_I x_test;
37
38         bool error = false;
39         try {
40                 x_test = integer_cra(residues, moduli);
41         } catch (std::exception& oops) {
42                 std::cerr << "Oops: " << oops.what() << std::endl;
43                 error = true;
44         }
45
46         if (x != x_test)
47                 error = true;
48
49         if (error) {
50                 std::cerr << "Expected x = " << x << ", got " <<
51                         x_test << " instead" << std::endl;
52                 std::cerr << "moduli = ";
53                 dump(moduli);
54                 std::cerr << std::endl;
55                 std::cerr << "residues = ";
56                 dump(residues);
57                 std::cerr << std::endl;
58                 throw std::logic_error("bug in integer_cra?");
59         }
60 }
61
62 static void run_test(const cln::cl_I& limit, const std::size_t ntimes)
63 {
64         for (std::size_t i = 0; i < ntimes; ++i)
65                 run_test_once(limit);
66 }
67
68 int main(int argc, char** argv)
69 {
70         typedef std::map<cln::cl_I, std::size_t> map_t;
71         map_t the_map;
72         // Run 1024 tests with native 32-bit numbers
73         the_map[cln::cl_I(std::numeric_limits<int>::max())] = 1024;
74
75         // Run 512 tests with native 64-bit integers
76         if (sizeof(long) > sizeof(int)) 
77                 the_map[cln::cl_I(std::numeric_limits<long>::max())] = 512;
78
79         // Run 32 tests with a bit bigger numbers
80         the_map[cln::cl_I("987654321098765432109876543210")] = 32;
81
82         std::cout << "examining Garner's integer chinese remainder algorithm " << std::flush;
83
84         for (map_t::const_iterator i = the_map.begin(); i != the_map.end(); ++i)
85                 run_test(i->first, i->second);
86
87         return 0;
88 }
89
90 static std::vector<cln::cl_I>
91 calc_residues(const cln::cl_I& x, const std::vector<cln::cl_I>& moduli)
92 {
93         std::vector<cln::cl_I> residues(moduli.size());
94         for (std::size_t i = moduli.size(); i-- != 0; )
95                 residues[i] = mod(x, moduli[i]);
96         return residues;
97 }
98
99 static std::vector<cln::cl_I>
100 make_random_moduli(const cln::cl_I& limit)
101 {
102         std::vector<cln::cl_I> moduli;
103         cln::cl_I prod(1);
104         cln::cl_I next = random_I(std::min(limit >> 1, cln::cl_I(128)));
105         unsigned count = 0;
106         do {
107                 cln::cl_I tmp = nextprobprime(next);
108                 next = tmp + random_I(cln::cl_I(10)) + 1;
109                 prod = prod*tmp;
110                 moduli.push_back(tmp);
111                 ++count;
112         } while (prod < limit || (count < 2));
113         return moduli;
114 }
115
116 static void dump(const std::vector<cln::cl_I>& v)
117 {
118         std::cerr << "[ ";
119         for (std::size_t i = 0; i < v.size(); ++i)
120                 std::cerr << v[i] << " ";
121         std::cerr << "]";
122 }
123