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_map.h" |
---|
12 | #include "cf_algorithm.h" |
---|
13 | |
---|
14 | static int |
---|
15 | compareFactors( const CFFactor & f, const CFFactor & g ) |
---|
16 | { |
---|
17 | return f.exp() > g.exp(); |
---|
18 | } |
---|
19 | |
---|
20 | CFFList |
---|
21 | sortCFFList( CFFList & F ) |
---|
22 | { |
---|
23 | F.sort( compareFactors ); |
---|
24 | |
---|
25 | int exp; |
---|
26 | CanonicalForm f; |
---|
27 | CFFListIterator I = F; |
---|
28 | CFFList result; |
---|
29 | |
---|
30 | // join elements with the same degree |
---|
31 | while ( I.hasItem() ) { |
---|
32 | f = I.getItem().factor(); |
---|
33 | exp = I.getItem().exp(); |
---|
34 | I++; |
---|
35 | while ( I.hasItem() && I.getItem().exp() == exp ) { |
---|
36 | f *= I.getItem().factor(); |
---|
37 | I++; |
---|
38 | } |
---|
39 | result.append( CFFactor( f, exp ) ); |
---|
40 | } |
---|
41 | |
---|
42 | return result; |
---|
43 | } |
---|
44 | |
---|
45 | CFFList sqrFreeZ ( const CanonicalForm & a ) |
---|
46 | { |
---|
47 | if ( a.inCoeffDomain() ) |
---|
48 | return CFFactor( a, 1 ); |
---|
49 | CanonicalForm aa, LcA; |
---|
50 | if (isOn (SW_RATIONAL)) |
---|
51 | { |
---|
52 | LcA= bCommonDen (a); |
---|
53 | aa= a*LcA; |
---|
54 | } |
---|
55 | else |
---|
56 | { |
---|
57 | LcA= icontent (a); |
---|
58 | if (lc (a).sign() < 0) |
---|
59 | LcA= -LcA; |
---|
60 | aa= a/LcA; |
---|
61 | } |
---|
62 | CanonicalForm cont = content( aa ); |
---|
63 | aa /= cont; |
---|
64 | CanonicalForm b = aa.deriv(), c = gcd( aa, b ); |
---|
65 | CanonicalForm y, z, w = aa / c; |
---|
66 | int i = 1; |
---|
67 | CFFList F; |
---|
68 | Variable v = aa.mvar(); |
---|
69 | CanonicalForm lcinv; |
---|
70 | while ( ! c.degree(v) == 0 ) |
---|
71 | { |
---|
72 | y = gcd( w, c ); z = w / y; |
---|
73 | if ( degree( z, v ) > 0 ) |
---|
74 | { |
---|
75 | if (isOn (SW_RATIONAL)) |
---|
76 | { |
---|
77 | lcinv= 1/Lc (z); |
---|
78 | z *= lcinv; |
---|
79 | z *= bCommonDen (z); |
---|
80 | } |
---|
81 | if (lc (z).sign() < 0) |
---|
82 | z= -z; |
---|
83 | F.append( CFFactor( z, i ) ); |
---|
84 | } |
---|
85 | i++; |
---|
86 | w = y; c = c / y; |
---|
87 | } |
---|
88 | if ( degree( w,v ) > 0 ) |
---|
89 | { |
---|
90 | if (isOn (SW_RATIONAL)) |
---|
91 | { |
---|
92 | lcinv= 1/Lc (w); |
---|
93 | w *= lcinv; |
---|
94 | w *= bCommonDen (w); |
---|
95 | } |
---|
96 | if (lc (w).sign() < 0) |
---|
97 | w= -w; |
---|
98 | F.append( CFFactor( w, i ) ); |
---|
99 | } |
---|
100 | if ( ! cont.isOne() ) |
---|
101 | { |
---|
102 | CFFList buf= sqrFreeZ (cont); |
---|
103 | buf.removeFirst(); |
---|
104 | F = Union( F, buf ); |
---|
105 | } |
---|
106 | F.insert (CFFactor (LcA, 1)); |
---|
107 | return F; |
---|
108 | } |
---|
109 | |
---|
110 | CanonicalForm |
---|
111 | sqrfPart (const CanonicalForm& F) |
---|
112 | { |
---|
113 | if (F.inCoeffDomain()) |
---|
114 | return F; |
---|
115 | CFMap M; |
---|
116 | CanonicalForm A= compress (F, M); |
---|
117 | CanonicalForm w, v, b; |
---|
118 | CanonicalForm result; |
---|
119 | int i= 1; |
---|
120 | for (; i <= A.level(); i++) |
---|
121 | { |
---|
122 | if (!deriv (A, Variable (i)).isZero()) |
---|
123 | break; |
---|
124 | } |
---|
125 | |
---|
126 | w= gcd (A, deriv (A, Variable (i))); |
---|
127 | b= A/w; |
---|
128 | result= b; |
---|
129 | if (degree (w) < 1) |
---|
130 | return M (result); |
---|
131 | i++; |
---|
132 | for (; i <= A.level(); i++) |
---|
133 | { |
---|
134 | if (!deriv (w, Variable (i)).isZero()) |
---|
135 | { |
---|
136 | b= w; |
---|
137 | w= gcd (w, deriv (w, Variable (i))); |
---|
138 | b /= w; |
---|
139 | if (degree (b) < 1) |
---|
140 | break; |
---|
141 | CanonicalForm g= gcd (b, result); |
---|
142 | if (degree (g) > 0) |
---|
143 | result *= b/g; |
---|
144 | if (degree (g) <= 0) |
---|
145 | result *= b; |
---|
146 | } |
---|
147 | } |
---|
148 | result= M (result); |
---|
149 | return result; |
---|
150 | } |
---|
151 | |
---|