Changeset d57f19 in git
- Timestamp:
- Feb 2, 1998, 9:57:29 AM (26 years ago)
- Branches:
- (u'spielwiese', 'e7cc1ebecb61be8b9ca6c18016352af89940b21a')
- Children:
- f3b98ad1c11ea4bf6d3b9525f75480b2abf56cac
- Parents:
- 9dac21e20830c8d04a26496a2d4e9328a8770887
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
factory/cf_chinese.cc
r9dac21e rd57f19 1 1 /* emacs edit mode for this file is -*- C++ -*- */ 2 /* $Id: cf_chinese.cc,v 1. 8 1997-11-19 17:42:15schmidt Exp $ */2 /* $Id: cf_chinese.cc,v 1.9 1998-02-02 08:57:29 schmidt Exp $ */ 3 3 4 4 //{{{ docu … … 15 15 16 16 #include "assert.h" 17 #include "debug.h" 17 18 18 19 #include "canonicalform.h" … … 30 31 // coefficients, too. 31 32 // 33 // Note: This algorithm is optimized for the case q1>>q2. 34 // 32 35 // This is a standard algorithm. See, for example, 33 // Geddes/Czapor/Labahn - 'Alogorithms for Computer Algebra', 34 // par. 5.6 and 5.8. 36 // Geddes/Czapor/Labahn - 'Algorithms for Computer Algebra', 37 // par. 5.6 and 5.8, or the article of M. Lauer - 'Computing by 38 // Homomorphic Images' in B. Buchberger - 'Computer Algebra - 39 // Symbolic and Algebraic Computation'. 35 40 // 36 // Note: be sure you are calculating in Z, and not in Q!41 // Note: Be sure you are calculating in Z, and not in Q! 37 42 // 38 43 //}}} … … 40 45 chineseRemainder ( const CanonicalForm & x1, const CanonicalForm & q1, const CanonicalForm & x2, const CanonicalForm & q2, CanonicalForm & xnew, CanonicalForm & qnew ) 41 46 { 42 CanonicalForm a1, a2; 43 (void)iextgcd( q1, q2, a1, a2 ); 47 DEBINCLEVEL( cerr, "chineseRemainder" ); 48 49 DEBOUTLN( cerr, "log(q1) = " << q1.ilog2() ); 50 DEBOUTLN( cerr, "log(q2) = " << q2.ilog2() ); 51 52 // We calculate xnew as follows: 53 // xnew = v1 + v2 * q1 54 // where 55 // v1 = x1 (mod q1) 56 // v2 = (x2-v1)/q1 (mod q2) (*) 57 // 58 // We do one extra test to check whether x2-v1 vanishes (mod 59 // q2) in (*) since it is not costly and may save us 60 // from calculating the inverse of q1 (mod q2). 61 // 62 // u: v1 (mod q2) 63 // d: x2-v1 (mod q2) 64 // s: 1/q1 (mod q2) 65 // 66 CanonicalForm v2, v1; 67 CanonicalForm u, d, s, dummy; 68 69 v1 = mod( x1, q1 ); 70 u = mod( v1, q2 ); 71 d = mod( x2-u, q2 ); 72 if ( d.isZero() ) { 73 xnew = v1; 74 qnew = q1 * q2; 75 return; 76 } 77 (void)bextgcd( q1, q2, s, dummy ); 78 v2 = mod( d*s, q2 ); 79 xnew = v1 + v2*q1; 80 81 // After all, calculate new modulus. It is important that 82 // this is done at the very end of the algorithm, since q1 83 // and qnew may refer to the same object (same is true for x1 84 // and xnew). 44 85 qnew = q1 * q2; 45 xnew = mod( q1*a1*x2 + q2*a2*x1, qnew ); 86 87 DEBDECLEVEL( cerr, "chineseRemainder" ); 46 88 } 47 89 //}}} … … 63 105 // to calculate the remainder. 64 106 // 65 // Note: be sure you are calculating in Z, and not in Q!107 // Note: Be sure you are calculating in Z, and not in Q! 66 108 // 67 109 //}}} … … 69 111 chineseRemainder ( const CFArray & x, const CFArray & q, CanonicalForm & xnew, CanonicalForm & qnew ) 70 112 { 113 DEBINCLEVEL( cerr, "chineseRemainder( ... CFArray ... )" ); 114 71 115 ASSERT( x.min() == q.min() && x.size() == q.size(), "incompatible arrays" ); 72 116 CFArray X(x), Q(q); 73 int i, j, n = x.size(); 117 int i, j, n = x.size(), start = x.min(); 118 119 DEBOUTLN( cerr, "array size = " << n ); 74 120 75 121 while ( n != 1 ) { 76 i = j = x.min(); 77 while ( i < x.min() + n - 1 ) { 122 i = j = start; 123 while ( i < start + n - 1 ) { 124 // This is a little bit dangerous: X[i] and X[j] (and 125 // Q[i] and Q[j]) may refer to the same object. But 126 // xnew and qnew in the above function are modified 127 // at the very end of the function, so we do not 128 // modify x1 and q1, resp., by accident. 78 129 chineseRemainder( X[i], Q[i], X[i+1], Q[i+1], X[j], Q[j] ); 79 130 i += 2; 80 131 j++; 81 132 } 133 82 134 if ( n & 1 ) { 83 135 X[j] = X[i]; 84 136 Q[j] = Q[i]; 85 137 } 138 // Maybe we would get some memory back at this point if 139 // we would set X[j+1, ..., n] and Q[j+1, ..., n] to zero 140 // at this point? 141 86 142 n = ( n + 1) / 2; 87 143 } 88 xnew = X[ x.min()];144 xnew = X[start]; 89 145 qnew = Q[q.min()]; 146 147 DEBDECLEVEL( cerr, "chineseRemainder( ... CFArray ... )" ); 90 148 } 91 149 //}}}
Note: See TracChangeset
for help on using the changeset viewer.