1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id: fac_sqrfree.cc,v 1.7 1997-12-08 18:24:32 schmidt Exp $ */ |
---|
3 | |
---|
4 | #include <config.h> |
---|
5 | |
---|
6 | #include "assert.h" |
---|
7 | |
---|
8 | #include "cf_defs.h" |
---|
9 | #include "canonicalform.h" |
---|
10 | |
---|
11 | static int divexp = 1; |
---|
12 | |
---|
13 | static void divexpfunc ( CanonicalForm &, int & e ) |
---|
14 | { |
---|
15 | e /= divexp; |
---|
16 | } |
---|
17 | |
---|
18 | static int |
---|
19 | compareFactors( const CFFactor & f, const CFFactor & g ) |
---|
20 | { |
---|
21 | return f.exp() > g.exp(); |
---|
22 | } |
---|
23 | |
---|
24 | CFFList |
---|
25 | sortCFFList( CFFList & F ) |
---|
26 | { |
---|
27 | F.sort( compareFactors ); |
---|
28 | |
---|
29 | int exp; |
---|
30 | CanonicalForm f; |
---|
31 | CFFListIterator I = F; |
---|
32 | CFFList result; |
---|
33 | |
---|
34 | // join elements with the same degree |
---|
35 | while ( I.hasItem() ) { |
---|
36 | f = I.getItem().factor(); |
---|
37 | exp = I.getItem().exp(); |
---|
38 | I++; |
---|
39 | while ( I.hasItem() && I.getItem().exp() == exp ) { |
---|
40 | f *= I.getItem().factor(); |
---|
41 | I++; |
---|
42 | } |
---|
43 | result.append( CFFactor( f, exp ) ); |
---|
44 | } |
---|
45 | |
---|
46 | return result; |
---|
47 | } |
---|
48 | |
---|
49 | CFFList sqrFreeFp ( const CanonicalForm & f ) |
---|
50 | { |
---|
51 | CanonicalForm t0 = f, t, v, w, h; |
---|
52 | CanonicalForm leadcf = t0.lc(); |
---|
53 | Variable x = f.mvar(); |
---|
54 | CFFList F; |
---|
55 | int p = getCharacteristic(); |
---|
56 | int k, e = 1; |
---|
57 | |
---|
58 | if ( ! leadcf.isOne() ) |
---|
59 | t0 /= leadcf; |
---|
60 | |
---|
61 | divexp = p; |
---|
62 | while ( t0.degree(x) > 0 ) { |
---|
63 | t = gcd( t0, t0.deriv() ); |
---|
64 | v = t0 / t; |
---|
65 | k = 0; |
---|
66 | while ( v.degree(x) > 0 ) { |
---|
67 | k = k+1; |
---|
68 | if ( k % p == 0 ) { |
---|
69 | t /= v; |
---|
70 | k = k+1; |
---|
71 | } |
---|
72 | w = gcd( t, v ); |
---|
73 | h = v / w; |
---|
74 | v = w; |
---|
75 | t /= v; |
---|
76 | if ( h.degree(x) > 0 ) |
---|
77 | F.append( CFFactor( h/h.lc(), e*k ) ); |
---|
78 | } |
---|
79 | t0 = apply( t, divexpfunc ); |
---|
80 | e = p * e; |
---|
81 | } |
---|
82 | if ( ! leadcf.isOne() ) { |
---|
83 | if ( F.getFirst().exp() == 1 ) { |
---|
84 | leadcf = F.getFirst().factor() * leadcf; |
---|
85 | F.removeFirst(); |
---|
86 | } |
---|
87 | F.insert( CFFactor( leadcf, 1 ) ); |
---|
88 | } |
---|
89 | return F; |
---|
90 | } |
---|
91 | |
---|
92 | bool isSqrFreeFp( const CanonicalForm & f ) |
---|
93 | { |
---|
94 | CFFList F = sqrFreeFp( f ); |
---|
95 | return ( F.length() == 1 && F.getFirst().exp() == 1 ); |
---|
96 | } |
---|
97 | |
---|
98 | bool isSqrFreeZ ( const CanonicalForm & f ) |
---|
99 | { |
---|
100 | return gcd( f, f.deriv() ).degree() == 0; |
---|
101 | } |
---|
102 | |
---|
103 | /* |
---|
104 | |
---|
105 | CFFList sqrFreeZ ( const CanonicalForm & a ) |
---|
106 | { |
---|
107 | CanonicalForm b = a.deriv(), c = gcd( a, b ); |
---|
108 | CanonicalForm y, z, w = a / c; |
---|
109 | int i = 1; |
---|
110 | CFFList F; |
---|
111 | |
---|
112 | while ( ! c.degree() == 0 ) { |
---|
113 | y = gcd( w, c ); z = w / y; |
---|
114 | if ( degree( z ) > 0 ) |
---|
115 | if ( lc( z ).sign() < 0 ) |
---|
116 | F.append( CFFactor( -z, i ) ); |
---|
117 | else |
---|
118 | F.append( CFFactor( z, i ) ); |
---|
119 | i++; |
---|
120 | w = y; c = c / y; |
---|
121 | } |
---|
122 | if ( degree( w ) > 0 ) |
---|
123 | if ( lc( w ).sign() < 0 ) |
---|
124 | F.append( CFFactor( -w, i ) ); |
---|
125 | else |
---|
126 | F.append( CFFactor( w, i ) ); |
---|
127 | return F; |
---|
128 | } |
---|
129 | */ |
---|
130 | |
---|
131 | CFFList sqrFreeZ ( const CanonicalForm & a ) |
---|
132 | { |
---|
133 | if ( a.inCoeffDomain() ) |
---|
134 | return CFFactor( a, 1 ); |
---|
135 | CanonicalForm cont = content( a ); |
---|
136 | CanonicalForm aa = a / cont; |
---|
137 | CanonicalForm b = aa.deriv(), c = gcd( aa, b ); |
---|
138 | CanonicalForm y, z, w = aa / c; |
---|
139 | int i = 1; |
---|
140 | CFFList F; |
---|
141 | Variable v = aa.mvar(); |
---|
142 | while ( ! c.degree(v) == 0 ) { |
---|
143 | y = gcd( w, c ); z = w / y; |
---|
144 | if ( degree( z, v ) > 0 ) |
---|
145 | if ( lc( z ).sign() < 0 ) |
---|
146 | F.append( CFFactor( -z, i ) ); |
---|
147 | else |
---|
148 | F.append( CFFactor( z, i ) ); |
---|
149 | i++; |
---|
150 | w = y; c = c / y; |
---|
151 | } |
---|
152 | if ( degree( w,v ) > 0 ) |
---|
153 | if ( lc( w ).sign() < 0 ) |
---|
154 | F.append( CFFactor( -w, i ) ); |
---|
155 | else |
---|
156 | F.append( CFFactor( w, i ) ); |
---|
157 | if ( ! cont.isOne() ) |
---|
158 | F = Union( F, sqrFreeZ( cont ) ); |
---|
159 | if ( lc( a ).sign() < 0 ) { |
---|
160 | if ( F.getFirst().exp() == 1 ) { |
---|
161 | CanonicalForm f = F.getFirst().factor(); |
---|
162 | CFFListIterator(F).getItem() = CFFactor( -f, 1 ); |
---|
163 | } |
---|
164 | else |
---|
165 | F.insert( CFFactor( -1, 1 ) ); |
---|
166 | } |
---|
167 | return F; |
---|
168 | } |
---|