source: git/factory/cf_chinese.cc @ 19fc57b

fieker-DuValspielwiese
Last change on this file since 19fc57b was d2cdd65, checked in by Hans Schönemann <hannes@…>, 19 years ago
*hannes: fixed debug outout (level) git-svn-id: file:///usr/local/Singular/svn/trunk@7709 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 4.5 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2/* $Id: cf_chinese.cc,v 1.10 2005-02-08 10:28:46 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
21//{{{ void chineseRemainder ( const CanonicalForm & x1, const CanonicalForm & q1, const CanonicalForm & x2, const CanonicalForm & q2, CanonicalForm & xnew, CanonicalForm & qnew )
22//{{{ docu
23//
24// chineseRemainder - integer chinese remaindering.
25//
26// Calculate xnew such that xnew=x1 (mod q1) and xnew=x2 (mod q2)
27// and qnew = q1*q2.  q1 and q2 should be positive integers,
28// pairwise prime, x1 and x2 should be polynomials with integer
29// coefficients.  If x1 and x2 are polynomials with positive
30// coefficients, the result is guaranteed to have positive
31// coefficients, too.
32//
33// Note: This algorithm is optimized for the case q1>>q2.
34//
35// This is a standard algorithm.  See, for example,
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'.
40//
41// Note: Be sure you are calculating in Z, and not in Q!
42//
43//}}}
44void
45chineseRemainder ( const CanonicalForm & x1, const CanonicalForm & q1, const CanonicalForm & x2, const CanonicalForm & q2, CanonicalForm & xnew, CanonicalForm & qnew )
46{
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        DEBDECLEVEL( cerr, "chineseRemainder" );
76        return;
77    }
78    (void)bextgcd( q1, q2, s, dummy );
79    v2 = mod( d*s, q2 );
80    xnew = v1 + v2*q1;
81
82    // After all, calculate new modulus.  It is important that
83    // this is done at the very end of the algorithm, since q1
84    // and qnew may refer to the same object (same is true for x1
85    // and xnew).
86    qnew = q1 * q2;
87
88    DEBDECLEVEL( cerr, "chineseRemainder" );
89}
90//}}}
91
92//{{{ void chineseRemainder ( const CFArray & x, const CFArray & q, CanonicalForm & xnew, CanonicalForm & qnew )
93//{{{ docu
94//
95// chineseRemainder - integer chinese remaindering.
96//
97// Calculate xnew such that xnew=x[i] (mod q[i]) and qnew is the
98// product of all q[i].  q[i] should be positive integers,
99// pairwise prime.  x[i] should be polynomials with integer
100// coefficients.  If all coefficients of all x[i] are positive
101// integers, the result is guaranteed to have positive
102// coefficients, too.
103//
104// This is a standard algorithm, too, except for the fact that we
105// use a divide-and-conquer method instead of a linear approach
106// to calculate the remainder.
107//
108// Note: Be sure you are calculating in Z, and not in Q!
109//
110//}}}
111void
112chineseRemainder ( const CFArray & x, const CFArray & q, CanonicalForm & xnew, CanonicalForm & qnew )
113{
114    DEBINCLEVEL( cerr, "chineseRemainder( ... CFArray ... )" );
115
116    ASSERT( x.min() == q.min() && x.size() == q.size(), "incompatible arrays" );
117    CFArray X(x), Q(q);
118    int i, j, n = x.size(), start = x.min();
119
120    DEBOUTLN( cerr, "array size = " << n );
121
122    while ( n != 1 ) {
123        i = j = start;
124        while ( i < start + n - 1 ) {
125            // This is a little bit dangerous: X[i] and X[j] (and
126            // Q[i] and Q[j]) may refer to the same object.  But
127            // xnew and qnew in the above function are modified
128            // at the very end of the function, so we do not
129            // modify x1 and q1, resp., by accident.
130            chineseRemainder( X[i], Q[i], X[i+1], Q[i+1], X[j], Q[j] );
131            i += 2;
132            j++;
133        }
134
135        if ( n & 1 ) {
136            X[j] = X[i];
137            Q[j] = Q[i];
138        }
139        // Maybe we would get some memory back at this point if
140        // we would set X[j+1, ..., n] and Q[j+1, ..., n] to zero
141        // at this point?
142
143        n = ( n + 1) / 2;
144    }
145    xnew = X[start];
146    qnew = Q[q.min()];
147
148    DEBDECLEVEL( cerr, "chineseRemainder( ... CFArray ... )" );
149}
150//}}}
Note: See TracBrowser for help on using the repository browser.