]> www.ginac.de Git - ginac.git/blob - check/exam_cra.cpp
Remove ex::to_rational(lst) and ex::to_polynomial(lst).
[ginac.git] / check / exam_cra.cpp
1 /** @file exam_cra.cpp
2  *
3  *  Test of Chinese remainder algorithm. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2016 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "polynomial/cra_garner.h"
24
25 #include <cln/integer.h>
26 #include <cln/integer_io.h>
27 #include <cln/random.h>
28 #include <cln/numtheory.h>
29 using namespace cln;
30 #include <iostream>
31 #include <limits>
32 #include <map>
33 #include <stdexcept>
34 #include <vector>
35 #include <algorithm>
36 using namespace std;
37
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);
41
42 static std::vector<cln::cl_I>
43 calc_residues(const cln::cl_I& x, const std::vector<cln::cl_I>& moduli);
44
45 static void dump(const std::vector<cln::cl_I>& v);
46
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)
52 {
53         std::vector<cln::cl_I> moduli = make_random_moduli(lim);
54         cln::cl_I x = random_I(lim) + 1;
55
56         if (x > (lim >> 1))
57                 x = x - lim;
58
59         std::vector<cln::cl_I> residues = calc_residues(x, moduli);
60         cln::cl_I x_test;
61
62         bool error = false;
63         try {
64                 x_test = integer_cra(residues, moduli);
65         } catch (std::exception& oops) {
66                 std::cerr << "Oops: " << oops.what() << std::endl;
67                 error = true;
68         }
69
70         if (x != x_test)
71                 error = true;
72
73         if (error) {
74                 std::cerr << "Expected x = " << x << ", got " <<
75                         x_test << " instead" << std::endl;
76                 std::cerr << "moduli = ";
77                 dump(moduli);
78                 std::cerr << std::endl;
79                 std::cerr << "residues = ";
80                 dump(residues);
81                 std::cerr << std::endl;
82                 throw std::logic_error("bug in integer_cra?");
83         }
84 }
85
86 static void run_test(const cln::cl_I& limit, const std::size_t ntimes)
87 {
88         for (std::size_t i = 0; i < ntimes; ++i)
89                 run_test_once(limit);
90 }
91
92 int main(int argc, char** argv)
93 {
94         typedef std::map<cln::cl_I, std::size_t> map_t;
95         map_t the_map;
96         // Run 1024 tests with native 32-bit numbers
97         the_map[cln::cl_I(std::numeric_limits<int>::max())] = 1024;
98
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;
102
103         // Run 32 tests with a bit bigger numbers
104         the_map[cln::cl_I("987654321098765432109876543210")] = 32;
105
106         std::cout << "examining Garner's integer chinese remainder algorithm " << std::flush;
107
108         for (map_t::const_iterator i = the_map.begin(); i != the_map.end(); ++i)
109                 run_test(i->first, i->second);
110
111         return 0;
112 }
113
114 static std::vector<cln::cl_I>
115 calc_residues(const cln::cl_I& x, const std::vector<cln::cl_I>& moduli)
116 {
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]);
120         return residues;
121 }
122
123 static std::vector<cln::cl_I>
124 make_random_moduli(const cln::cl_I& limit)
125 {
126         std::vector<cln::cl_I> moduli;
127         cln::cl_I prod(1);
128         cln::cl_I next = random_I(std::min(limit >> 1, cln::cl_I(128)));
129         unsigned count = 0;
130         do {
131                 cln::cl_I tmp = nextprobprime(next);
132                 next = tmp + random_I(cln::cl_I(10)) + 1;
133                 prod = prod*tmp;
134                 moduli.push_back(tmp);
135                 ++count;
136         } while (prod < limit || (count < 2));
137         return moduli;
138 }
139
140 static void dump(const std::vector<cln::cl_I>& v)
141 {
142         std::cerr << "[ ";
143         for (std::size_t i = 0; i < v.size(); ++i)
144                 std::cerr << v[i] << " ";
145         std::cerr << "]";
146 }