1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id: cf_chinese.cc,v 1.11 2007-09-26 09:17:39 Singular Exp $ */ |
---|
3 | |
---|
4 | //{{{ docu |
---|
5 | // |
---|
6 | // cf_chinese.cc - algorithms for chinese remaindering. |
---|
7 | // |
---|
8 | // Used by: cf_gcd.cc, cf_linsys.cc, sm_util.cc |
---|
9 | // |
---|
10 | // Header file: cf_algorithm.h |
---|
11 | // |
---|
12 | //}}} |
---|
13 | |
---|
14 | #include <config.h> |
---|
15 | |
---|
16 | #include "assert.h" |
---|
17 | #include "debug.h" |
---|
18 | |
---|
19 | #include "canonicalform.h" |
---|
20 | #include "cf_iter.h" |
---|
21 | |
---|
22 | |
---|
23 | //{{{ void chineseRemainder ( const CanonicalForm & x1, const CanonicalForm & q1, const CanonicalForm & x2, const CanonicalForm & q2, CanonicalForm & xnew, CanonicalForm & qnew ) |
---|
24 | //{{{ docu |
---|
25 | // |
---|
26 | // chineseRemainder - integer chinese remaindering. |
---|
27 | // |
---|
28 | // Calculate xnew such that xnew=x1 (mod q1) and xnew=x2 (mod q2) |
---|
29 | // and qnew = q1*q2. q1 and q2 should be positive integers, |
---|
30 | // pairwise prime, x1 and x2 should be polynomials with integer |
---|
31 | // coefficients. If x1 and x2 are polynomials with positive |
---|
32 | // coefficients, the result is guaranteed to have positive |
---|
33 | // coefficients, too. |
---|
34 | // |
---|
35 | // Note: This algorithm is optimized for the case q1>>q2. |
---|
36 | // |
---|
37 | // This is a standard algorithm. See, for example, |
---|
38 | // Geddes/Czapor/Labahn - 'Algorithms for Computer Algebra', |
---|
39 | // par. 5.6 and 5.8, or the article of M. Lauer - 'Computing by |
---|
40 | // Homomorphic Images' in B. Buchberger - 'Computer Algebra - |
---|
41 | // Symbolic and Algebraic Computation'. |
---|
42 | // |
---|
43 | // Note: Be sure you are calculating in Z, and not in Q! |
---|
44 | // |
---|
45 | //}}} |
---|
46 | void |
---|
47 | chineseRemainder ( const CanonicalForm & x1, const CanonicalForm & q1, const CanonicalForm & x2, const CanonicalForm & q2, CanonicalForm & xnew, CanonicalForm & qnew ) |
---|
48 | { |
---|
49 | DEBINCLEVEL( cerr, "chineseRemainder" ); |
---|
50 | |
---|
51 | DEBOUTLN( cerr, "log(q1) = " << q1.ilog2() ); |
---|
52 | DEBOUTLN( cerr, "log(q2) = " << q2.ilog2() ); |
---|
53 | |
---|
54 | // We calculate xnew as follows: |
---|
55 | // xnew = v1 + v2 * q1 |
---|
56 | // where |
---|
57 | // v1 = x1 (mod q1) |
---|
58 | // v2 = (x2-v1)/q1 (mod q2) (*) |
---|
59 | // |
---|
60 | // We do one extra test to check whether x2-v1 vanishes (mod |
---|
61 | // q2) in (*) since it is not costly and may save us |
---|
62 | // from calculating the inverse of q1 (mod q2). |
---|
63 | // |
---|
64 | // u: v1 (mod q2) |
---|
65 | // d: x2-v1 (mod q2) |
---|
66 | // s: 1/q1 (mod q2) |
---|
67 | // |
---|
68 | CanonicalForm v2, v1; |
---|
69 | CanonicalForm u, d, s, dummy; |
---|
70 | |
---|
71 | v1 = mod( x1, q1 ); |
---|
72 | u = mod( v1, q2 ); |
---|
73 | d = mod( x2-u, q2 ); |
---|
74 | if ( d.isZero() ) |
---|
75 | { |
---|
76 | xnew = v1; |
---|
77 | qnew = q1 * q2; |
---|
78 | DEBDECLEVEL( cerr, "chineseRemainder" ); |
---|
79 | return; |
---|
80 | } |
---|
81 | (void)bextgcd( q1, q2, s, dummy ); |
---|
82 | v2 = mod( d*s, q2 ); |
---|
83 | xnew = v1 + v2*q1; |
---|
84 | |
---|
85 | // After all, calculate new modulus. It is important that |
---|
86 | // this is done at the very end of the algorithm, since q1 |
---|
87 | // and qnew may refer to the same object (same is true for x1 |
---|
88 | // and xnew). |
---|
89 | qnew = q1 * q2; |
---|
90 | |
---|
91 | DEBDECLEVEL( cerr, "chineseRemainder" ); |
---|
92 | } |
---|
93 | //}}} |
---|
94 | |
---|
95 | //{{{ void chineseRemainder ( const CFArray & x, const CFArray & q, CanonicalForm & xnew, CanonicalForm & qnew ) |
---|
96 | //{{{ docu |
---|
97 | // |
---|
98 | // chineseRemainder - integer chinese remaindering. |
---|
99 | // |
---|
100 | // Calculate xnew such that xnew=x[i] (mod q[i]) and qnew is the |
---|
101 | // product of all q[i]. q[i] should be positive integers, |
---|
102 | // pairwise prime. x[i] should be polynomials with integer |
---|
103 | // coefficients. If all coefficients of all x[i] are positive |
---|
104 | // integers, the result is guaranteed to have positive |
---|
105 | // coefficients, too. |
---|
106 | // |
---|
107 | // This is a standard algorithm, too, except for the fact that we |
---|
108 | // use a divide-and-conquer method instead of a linear approach |
---|
109 | // to calculate the remainder. |
---|
110 | // |
---|
111 | // Note: Be sure you are calculating in Z, and not in Q! |
---|
112 | // |
---|
113 | //}}} |
---|
114 | void |
---|
115 | chineseRemainder ( const CFArray & x, const CFArray & q, CanonicalForm & xnew, CanonicalForm & qnew ) |
---|
116 | { |
---|
117 | DEBINCLEVEL( cerr, "chineseRemainder( ... CFArray ... )" ); |
---|
118 | |
---|
119 | ASSERT( x.min() == q.min() && x.size() == q.size(), "incompatible arrays" ); |
---|
120 | CFArray X(x), Q(q); |
---|
121 | int i, j, n = x.size(), start = x.min(); |
---|
122 | |
---|
123 | DEBOUTLN( cerr, "array size = " << n ); |
---|
124 | |
---|
125 | while ( n != 1 ) |
---|
126 | { |
---|
127 | i = j = start; |
---|
128 | while ( i < start + n - 1 ) |
---|
129 | { |
---|
130 | // This is a little bit dangerous: X[i] and X[j] (and |
---|
131 | // Q[i] and Q[j]) may refer to the same object. But |
---|
132 | // xnew and qnew in the above function are modified |
---|
133 | // at the very end of the function, so we do not |
---|
134 | // modify x1 and q1, resp., by accident. |
---|
135 | chineseRemainder( X[i], Q[i], X[i+1], Q[i+1], X[j], Q[j] ); |
---|
136 | i += 2; |
---|
137 | j++; |
---|
138 | } |
---|
139 | |
---|
140 | if ( n & 1 ) |
---|
141 | { |
---|
142 | X[j] = X[i]; |
---|
143 | Q[j] = Q[i]; |
---|
144 | } |
---|
145 | // Maybe we would get some memory back at this point if |
---|
146 | // we would set X[j+1, ..., n] and Q[j+1, ..., n] to zero |
---|
147 | // at this point? |
---|
148 | |
---|
149 | n = ( n + 1) / 2; |
---|
150 | } |
---|
151 | xnew = X[start]; |
---|
152 | qnew = Q[q.min()]; |
---|
153 | |
---|
154 | DEBDECLEVEL( cerr, "chineseRemainder( ... CFArray ... )" ); |
---|
155 | } |
---|
156 | //}}} |
---|
157 | |
---|
158 | CanonicalForm Farey_n (CanonicalForm N, const CanonicalForm P) |
---|
159 | //"USAGE: Farey_n (N,P); P, N number; |
---|
160 | //RETURN: a rational number a/b such that a/b=N mod P |
---|
161 | // and |a|,|b|<(P/2)^{1/2} |
---|
162 | { |
---|
163 | //assume(P>0); |
---|
164 | if (N<0){N=N+P;} |
---|
165 | CanonicalForm A,B,C,D,E; |
---|
166 | E=P; |
---|
167 | B=1; |
---|
168 | while (N!=0) |
---|
169 | { |
---|
170 | if (2*N*N<P) |
---|
171 | { |
---|
172 | return(N/B); |
---|
173 | } |
---|
174 | D=E % N; |
---|
175 | C=A-(E-E % N)/N*B; |
---|
176 | E=N; |
---|
177 | N=D; |
---|
178 | A=B; |
---|
179 | B=C; |
---|
180 | } |
---|
181 | return(0); |
---|
182 | } |
---|
183 | |
---|
184 | CanonicalForm Farey ( const CanonicalForm & f, const CanonicalForm & q ) |
---|
185 | { |
---|
186 | Variable x = f.mvar(); |
---|
187 | CanonicalForm result = 0; |
---|
188 | CanonicalForm c; |
---|
189 | CFIterator i; |
---|
190 | for ( i = f; i.hasTerms(); i++ ) |
---|
191 | { |
---|
192 | c = i.coeff(); |
---|
193 | if ( c.inCoeffDomain()) |
---|
194 | { |
---|
195 | result += power( x, i.exp() ) * Farey_n(c,q); |
---|
196 | } |
---|
197 | else |
---|
198 | result += power( x, i.exp() ) * Farey(c,q); |
---|
199 | } |
---|
200 | return result; |
---|
201 | } |
---|
202 | |
---|