]> www.ginac.de Git - ginac.git/blob - check/exam_cra.cpp
expairseq::match(): no side effects if match failed. Fixes spurious match failures.
[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-2011 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 using namespace std;
36
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);
40
41 static std::vector<cln::cl_I>
42 calc_residues(const cln::cl_I& x, const std::vector<cln::cl_I>& moduli);
43
44 static void dump(const std::vector<cln::cl_I>& v);
45
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)
51 {
52         std::vector<cln::cl_I> moduli = make_random_moduli(lim);
53         cln::cl_I x = random_I(lim) + 1;
54
55         if (x > (lim >> 1))
56                 x = x - lim;
57
58         std::vector<cln::cl_I> residues = calc_residues(x, moduli);
59         cln::cl_I x_test;
60
61         bool error = false;
62         try {
63                 x_test = integer_cra(residues, moduli);
64         } catch (std::exception& oops) {
65                 std::cerr << "Oops: " << oops.what() << std::endl;
66                 error = true;
67         }
68
69         if (x != x_test)
70                 error = true;
71
72         if (error) {
73                 std::cerr << "Expected x = " << x << ", got " <<
74                         x_test << " instead" << std::endl;
75                 std::cerr << "moduli = ";
76                 dump(moduli);
77                 std::cerr << std::endl;
78                 std::cerr << "residues = ";
79                 dump(residues);
80                 std::cerr << std::endl;
81                 throw std::logic_error("bug in integer_cra?");
82         }
83 }
84
85 static void run_test(const cln::cl_I& limit, const std::size_t ntimes)
86 {
87         for (std::size_t i = 0; i < ntimes; ++i)
88                 run_test_once(limit);
89 }
90
91 int main(int argc, char** argv)
92 {
93         typedef std::map<cln::cl_I, std::size_t> map_t;
94         map_t the_map;
95         // Run 1024 tests with native 32-bit numbers
96         the_map[cln::cl_I(std::numeric_limits<int>::max())] = 1024;
97
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;
101
102         // Run 32 tests with a bit bigger numbers
103         the_map[cln::cl_I("987654321098765432109876543210")] = 32;
104
105         std::cout << "examining Garner's integer chinese remainder algorithm " << std::flush;
106
107         for (map_t::const_iterator i = the_map.begin(); i != the_map.end(); ++i)
108                 run_test(i->first, i->second);
109
110         return 0;
111 }
112
113 static std::vector<cln::cl_I>
114 calc_residues(const cln::cl_I& x, const std::vector<cln::cl_I>& moduli)
115 {
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]);
119         return residues;
120 }
121
122 static std::vector<cln::cl_I>
123 make_random_moduli(const cln::cl_I& limit)
124 {
125         std::vector<cln::cl_I> moduli;
126         cln::cl_I prod(1);
127         cln::cl_I next = random_I(std::min(limit >> 1, cln::cl_I(128)));
128         unsigned count = 0;
129         do {
130                 cln::cl_I tmp = nextprobprime(next);
131                 next = tmp + random_I(cln::cl_I(10)) + 1;
132                 prod = prod*tmp;
133                 moduli.push_back(tmp);
134                 ++count;
135         } while (prod < limit || (count < 2));
136         return moduli;
137 }
138
139 static void dump(const std::vector<cln::cl_I>& v)
140 {
141         std::cerr << "[ ";
142         for (std::size_t i = 0; i < v.size(); ++i)
143                 std::cerr << v[i] << " ";
144         std::cerr << "]";
145 }