1 | //////////////////////////////////////////////////////////// |
---|
2 | // $Id$ |
---|
3 | //////////////////////////////////////////////////////////// |
---|
4 | // FACTORY - Includes |
---|
5 | #include <factory.h> |
---|
6 | #ifdef HAVE_IOSTREAM |
---|
7 | #include <iostream> |
---|
8 | #define OSTREAM std::ostream |
---|
9 | #define ISTREAM std::istream |
---|
10 | #define CERR std::cerr |
---|
11 | #define CIN std::cin |
---|
12 | #elif defined(HAVE_IOSTREAM_H) |
---|
13 | #include <iostream.h> |
---|
14 | #define OSTREAM ostream |
---|
15 | #define ISTREAM istream |
---|
16 | #define CERR cerr |
---|
17 | #define CIN cin |
---|
18 | #endif |
---|
19 | |
---|
20 | // Factor - Includes |
---|
21 | #include "tmpl_inst.h" |
---|
22 | #include "Factor.h" |
---|
23 | // some CC's need it: |
---|
24 | #include "homogfactor.h" |
---|
25 | |
---|
26 | void out_cf(char *s1,const CanonicalForm &f,char *s2); |
---|
27 | |
---|
28 | #ifdef HFACTORDEBUG |
---|
29 | # define DEBUGOUTPUT |
---|
30 | #else |
---|
31 | # undef DEBUGOUTPUT |
---|
32 | #endif |
---|
33 | |
---|
34 | #include "debug.h" |
---|
35 | #include "timing.h" |
---|
36 | TIMING_DEFINE_PRINT(hfactorize_time); |
---|
37 | |
---|
38 | #if 0 |
---|
39 | /////////////////////////////////////////////////////////////// |
---|
40 | // get_Terms: Split the polynomial in the containing terms. // |
---|
41 | // getTerms: the real work is done here. // |
---|
42 | /////////////////////////////////////////////////////////////// |
---|
43 | static void |
---|
44 | getTerms( const CanonicalForm & f, const CanonicalForm & t, CFList & result ){ |
---|
45 | |
---|
46 | if ( getNumVars(f) == 0 ) result.append(f*t); |
---|
47 | else{ |
---|
48 | Variable x(level(f)); |
---|
49 | for ( CFIterator i=f; i.hasTerms(); i++ ) |
---|
50 | getTerms( i.coeff(), t*power(x,i.exp()), result); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | CFList |
---|
55 | get_Terms( const CanonicalForm & f ){ |
---|
56 | CFList result,dummy,dummy2; |
---|
57 | CFIterator i; |
---|
58 | CFListIterator j; |
---|
59 | |
---|
60 | if ( getNumVars(f) == 0 ) result.append(f); |
---|
61 | else{ |
---|
62 | Variable _x(level(f)); |
---|
63 | for ( i=f; i.hasTerms(); i++ ){ |
---|
64 | getTerms(i.coeff(), 1, dummy); |
---|
65 | for ( j=dummy; j.hasItem(); j++ ) |
---|
66 | result.append(j.getItem() * power(_x, i.exp())); |
---|
67 | |
---|
68 | dummy= dummy2; // have to initalize new |
---|
69 | } |
---|
70 | } |
---|
71 | return result; |
---|
72 | } |
---|
73 | #endif |
---|
74 | |
---|
75 | #if 0 |
---|
76 | /////////////////////////////////////////////////////////////// |
---|
77 | // is_homogeneous returns 1 iff f is homogeneous, 0 otherwise// |
---|
78 | /////////////////////////////////////////////////////////////// |
---|
79 | bool |
---|
80 | is_homogeneous( const CanonicalForm & f){ |
---|
81 | CFList termlist= get_Terms(f); |
---|
82 | CFListIterator i; |
---|
83 | int deg= totaldegree(termlist.getFirst()); |
---|
84 | |
---|
85 | for ( i=termlist; i.hasItem(); i++ ) |
---|
86 | if ( totaldegree(i.getItem()) != deg ) return 0; |
---|
87 | return 1; |
---|
88 | // now: return f.isHomogeneous(); |
---|
89 | } |
---|
90 | #endif |
---|
91 | |
---|
92 | #if 0 |
---|
93 | /////////////////////////////////////////////////////////////// |
---|
94 | // get_max_degree_Variable returns Variable with // |
---|
95 | // highest degree. We assume f is *not* a constant! // |
---|
96 | /////////////////////////////////////////////////////////////// |
---|
97 | static Variable |
---|
98 | get_max_degree_Variable(const CanonicalForm & f){ |
---|
99 | ASSERT( ( ! f.inCoeffDomain() ), "no constants" ); |
---|
100 | int max=0, maxlevel=0, n=level(f); |
---|
101 | for ( int i=1; i<=n; i++ ) |
---|
102 | if (degree(f,Variable(i)) >= max) { |
---|
103 | max= degree(f,Variable(i)); maxlevel= i; |
---|
104 | } |
---|
105 | return Variable(maxlevel); |
---|
106 | } |
---|
107 | #endif |
---|
108 | |
---|
109 | #if 0 |
---|
110 | /////////////////////////////////////////////////////////////// |
---|
111 | // homogenize homogenizes f with Variable x // |
---|
112 | /////////////////////////////////////////////////////////////// |
---|
113 | static CanonicalForm |
---|
114 | homogenize( const CanonicalForm & f, const Variable & x){ |
---|
115 | CFList Newlist, Termlist= get_Terms(f); |
---|
116 | int maxdeg=totaldegree(f), deg; |
---|
117 | CFListIterator i; |
---|
118 | CanonicalForm elem, result=f.genZero(); |
---|
119 | |
---|
120 | for (i=Termlist; i.hasItem(); i++){ |
---|
121 | elem= i.getItem(); |
---|
122 | deg = totaldegree(elem); |
---|
123 | if ( deg < maxdeg ) |
---|
124 | Newlist.append(elem * power(x,maxdeg-deg)); |
---|
125 | else |
---|
126 | Newlist.append(elem); |
---|
127 | } |
---|
128 | for (i=Newlist; i.hasItem(); i++) // rebuild |
---|
129 | result += i.getItem(); |
---|
130 | |
---|
131 | return result; |
---|
132 | } |
---|
133 | #endif |
---|
134 | |
---|
135 | // we assume g is square-free |
---|
136 | CFFList |
---|
137 | HomogFactor( const CanonicalForm & g, const CanonicalForm & minpoly, const int Mainvar ) |
---|
138 | { |
---|
139 | DEBINCLEVEL(CERR, "HomogFactor"); |
---|
140 | Variable xn = get_max_degree_Variable(g); |
---|
141 | //out_cf("HomogFactor:",g,"\n"); |
---|
142 | int d_xn = degree(g,xn); |
---|
143 | CanonicalForm F = g(1,xn); |
---|
144 | |
---|
145 | DEBOUTLN(CERR, "xn= ", xn); |
---|
146 | DEBOUTLN(CERR, "d_xn= ", d_xn); |
---|
147 | DEBOUTLN(CERR, "F= ", F); |
---|
148 | //out_cf("HomogFactor:subst ",F,"\n"); |
---|
149 | |
---|
150 | // should we do this for low degree polys g ? e.g. quadratic? |
---|
151 | // |
---|
152 | CFFList Homoglist; |
---|
153 | CFFListIterator j; |
---|
154 | if (minpoly.isZero()) |
---|
155 | Homoglist = factorize(F, 1); |
---|
156 | else |
---|
157 | { |
---|
158 | CFFList Intermediatelist; |
---|
159 | CFMap n; |
---|
160 | Intermediatelist = Factorized(compress(F,n), minpoly, Mainvar); |
---|
161 | for ( j=Intermediatelist; j.hasItem(); j++ ) |
---|
162 | Homoglist.append(CFFactor( n(j.getItem().factor()), j.getItem().exp()) ); |
---|
163 | } |
---|
164 | // Now we have uncompressed factors in Homoglist |
---|
165 | DEBOUTLN(CERR, "F factors as: ", Homoglist); |
---|
166 | CFFList Unhomoglist; |
---|
167 | CanonicalForm unhomogelem; |
---|
168 | if ((!(minpoly.isZero())) &&(getCharacteristic() == 0) ) |
---|
169 | { |
---|
170 | for ( j=Homoglist; j.hasItem(); j++ ) |
---|
171 | { |
---|
172 | // assume that minpoly.level() < level of all "real" variables of g |
---|
173 | DEBOUTLN(CERR, "Homogenizing ",j.getItem().factor()); |
---|
174 | unhomogelem= homogenize(j.getItem().factor(),xn, |
---|
175 | Variable(minpoly.level()+1),g.mvar()); |
---|
176 | DEBOUTLN(CERR, " that is ", unhomogelem); |
---|
177 | Unhomoglist.append(CFFactor(unhomogelem,j.getItem().exp())); |
---|
178 | d_xn -= degree(unhomogelem,xn)*j.getItem().exp(); |
---|
179 | } |
---|
180 | } |
---|
181 | else |
---|
182 | { |
---|
183 | for ( j=Homoglist; j.hasItem(); j++ ) |
---|
184 | { |
---|
185 | DEBOUTLN(CERR, "Homogenizing ",j.getItem().factor()); |
---|
186 | unhomogelem= homogenize(j.getItem().factor(),xn); |
---|
187 | DEBOUTLN(CERR, " that is ", unhomogelem); |
---|
188 | //out_cf("unhomogelem:",unhomogelem,"\n"); |
---|
189 | Unhomoglist.append(CFFactor(unhomogelem,j.getItem().exp())); |
---|
190 | d_xn -= degree(unhomogelem,xn)*j.getItem().exp(); |
---|
191 | } |
---|
192 | } |
---|
193 | DEBOUTLN(CERR, "Power of xn to append is ", d_xn); |
---|
194 | if ( d_xn != 0 ) // have to append xn^(d_xn) |
---|
195 | Unhomoglist.append(CFFactor(CanonicalForm(xn),d_xn)); |
---|
196 | |
---|
197 | DEBDECLEVEL(CERR, "HomogFactor"); |
---|
198 | return Unhomoglist; |
---|
199 | } |
---|
200 | |
---|