1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | |
---|
3 | |
---|
4 | #include "config.h" |
---|
5 | |
---|
6 | |
---|
7 | #include "cf_assert.h" |
---|
8 | |
---|
9 | #include "cf_defs.h" |
---|
10 | #include "canonicalform.h" |
---|
11 | #include "cf_iter.h" |
---|
12 | #include "fac_util.h" |
---|
13 | |
---|
14 | static CanonicalForm PK, PKHALF; |
---|
15 | |
---|
16 | static CanonicalForm mappk ( const CanonicalForm& ); |
---|
17 | |
---|
18 | static CanonicalForm mappksymmetric ( const CanonicalForm& ); |
---|
19 | |
---|
20 | |
---|
21 | modpk::modpk() |
---|
22 | { |
---|
23 | p = 0; |
---|
24 | k = 0; |
---|
25 | pk = 1; |
---|
26 | pkhalf = 0; |
---|
27 | } |
---|
28 | |
---|
29 | modpk::modpk( int q, int l ) |
---|
30 | { |
---|
31 | p = q; |
---|
32 | k = l; |
---|
33 | pk = power( CanonicalForm( p ), k ); |
---|
34 | pkhalf = pk / 2; |
---|
35 | } |
---|
36 | |
---|
37 | modpk::modpk( const modpk & m ) |
---|
38 | { |
---|
39 | p = m.p; |
---|
40 | k = m.k; |
---|
41 | pk = m.pk; |
---|
42 | pkhalf = m.pkhalf; |
---|
43 | } |
---|
44 | |
---|
45 | modpk& |
---|
46 | modpk::operator= ( const modpk & m ) |
---|
47 | { |
---|
48 | if ( this != &m ) { |
---|
49 | p = m.p; |
---|
50 | k = m.k; |
---|
51 | pk = m.pk; |
---|
52 | pkhalf = m.pkhalf; |
---|
53 | } |
---|
54 | return *this; |
---|
55 | } |
---|
56 | |
---|
57 | CanonicalForm |
---|
58 | modpk::inverse( const CanonicalForm & f, bool symmetric ) const |
---|
59 | { |
---|
60 | CanonicalForm u, r0 = this->operator()( f, false ), r1 = pk, q0 = 1, q1 = 0; |
---|
61 | while ( ( r0 > 0 ) && ( r1 > 0 ) ) { |
---|
62 | u = r0 / r1; |
---|
63 | r0 = r0 % r1; |
---|
64 | q0 = u*q1 + q0; |
---|
65 | if ( r0 > 0 ) { |
---|
66 | u = r1 / r0; |
---|
67 | r1 = r1 % r0; |
---|
68 | q1 = u*q0 + q1; |
---|
69 | } |
---|
70 | } |
---|
71 | if ( r0 == 0 ) |
---|
72 | return this->operator()( pk-q1, symmetric ); |
---|
73 | else |
---|
74 | return this->operator()( q0, symmetric ); |
---|
75 | } |
---|
76 | |
---|
77 | CanonicalForm |
---|
78 | modpk::operator() ( const CanonicalForm & f, bool symmetric ) const |
---|
79 | { |
---|
80 | PKHALF = pkhalf; |
---|
81 | PK = pk; |
---|
82 | if ( symmetric ) |
---|
83 | return mapdomain( f, mappksymmetric ); |
---|
84 | else |
---|
85 | return mapdomain( f, mappk ); |
---|
86 | } |
---|
87 | |
---|
88 | CanonicalForm |
---|
89 | replaceLc( const CanonicalForm & f, const CanonicalForm & c ) |
---|
90 | { |
---|
91 | if ( f.inCoeffDomain() ) |
---|
92 | return c; |
---|
93 | else |
---|
94 | return f + ( c - LC( f ) ) * power( f.mvar(), degree( f ) ); |
---|
95 | } |
---|
96 | |
---|
97 | CanonicalForm |
---|
98 | mappksymmetric ( const CanonicalForm & f ) |
---|
99 | { |
---|
100 | CanonicalForm result = mod( f, PK ); |
---|
101 | if ( result > PKHALF ) |
---|
102 | return result - PK; |
---|
103 | else |
---|
104 | return result; |
---|
105 | } |
---|
106 | |
---|
107 | CanonicalForm |
---|
108 | mappk ( const CanonicalForm & f ) |
---|
109 | { |
---|
110 | return mod( f, PK ); |
---|
111 | } |
---|
112 | |
---|