1 | /* Copyright 1997 Michael Messollen. All rights reserved. */ |
---|
2 | //////////////////////////////////////////////////////////// |
---|
3 | // static char * rcsid = "$Id: homogfactor.cc,v 1.3 1997-09-12 07:19:57 Singular Exp $ "; |
---|
4 | //////////////////////////////////////////////////////////// |
---|
5 | // FACTORY - Includes |
---|
6 | #include <factory.h> |
---|
7 | // Factor - Includes |
---|
8 | #include "tmpl_inst.h" |
---|
9 | #include "Factor.h" |
---|
10 | // some CC's need it: |
---|
11 | #include "homogfactor.h" |
---|
12 | |
---|
13 | #ifdef HFACTORDEBUG |
---|
14 | # define DEBUGOUTPUT |
---|
15 | #else |
---|
16 | # undef DEBUGOUTPUT |
---|
17 | #endif |
---|
18 | |
---|
19 | #include "debug.h" |
---|
20 | #include "timing.h" |
---|
21 | TIMING_DEFINE_PRINT(hfactorize_time); |
---|
22 | |
---|
23 | /////////////////////////////////////////////////////////////// |
---|
24 | // get_Terms: Split the polynomial in the containing terms. // |
---|
25 | // getTerms: the real work is done here. // |
---|
26 | /////////////////////////////////////////////////////////////// |
---|
27 | static void |
---|
28 | getTerms( const CanonicalForm & f, const CanonicalForm & t, CFList & result ){ |
---|
29 | |
---|
30 | if ( getNumVars(f) == 0 ) result.append(f*t); |
---|
31 | else{ |
---|
32 | Variable x(level(f)); |
---|
33 | for ( CFIterator i=f; i.hasTerms(); i++ ) |
---|
34 | getTerms( i.coeff(), t*power(x,i.exp()), result); |
---|
35 | } |
---|
36 | } |
---|
37 | |
---|
38 | CFList |
---|
39 | get_Terms( const CanonicalForm & f ){ |
---|
40 | CFList result,dummy,dummy2; |
---|
41 | CFIterator i; |
---|
42 | CFListIterator j; |
---|
43 | |
---|
44 | if ( getNumVars(f) == 0 ) result.append(f); |
---|
45 | else{ |
---|
46 | Variable _x(level(f)); |
---|
47 | for ( i=f; i.hasTerms(); i++ ){ |
---|
48 | getTerms(i.coeff(), 1, dummy); |
---|
49 | for ( j=dummy; j.hasItem(); j++ ) |
---|
50 | result.append(j.getItem() * power(_x, i.exp())); |
---|
51 | |
---|
52 | dummy= dummy2; // have to initalize new |
---|
53 | } |
---|
54 | } |
---|
55 | return result; |
---|
56 | } |
---|
57 | |
---|
58 | /////////////////////////////////////////////////////////////// |
---|
59 | // is_homogeneous returns 1 iff f is homogeneous, 0 otherwise// |
---|
60 | /////////////////////////////////////////////////////////////// |
---|
61 | bool |
---|
62 | is_homogeneous( const CanonicalForm & f){ |
---|
63 | CFList termlist= get_Terms(f); |
---|
64 | CFListIterator i; |
---|
65 | int deg= totaldegree(termlist.getFirst()); |
---|
66 | |
---|
67 | for ( i=termlist; i.hasItem(); i++ ) |
---|
68 | if ( totaldegree(i.getItem()) != deg ) return 0; |
---|
69 | return 1; |
---|
70 | } |
---|
71 | |
---|
72 | /////////////////////////////////////////////////////////////// |
---|
73 | // get_max_degree_Variable returns Variable with // |
---|
74 | // highest degree. We assume f is *not* a constant! // |
---|
75 | /////////////////////////////////////////////////////////////// |
---|
76 | static Variable |
---|
77 | get_max_degree_Variable(const CanonicalForm & f){ |
---|
78 | ASSERT( ( ! f.inCoeffDomain() ), "no constants" ); |
---|
79 | int max=0, maxlevel=0, n=level(f); |
---|
80 | for ( int i=1; i<=n; i++ ) |
---|
81 | if (degree(f,Variable(i)) >= max) { |
---|
82 | max= degree(f,Variable(i)); maxlevel= i; |
---|
83 | } |
---|
84 | return Variable(maxlevel); |
---|
85 | } |
---|
86 | |
---|
87 | /////////////////////////////////////////////////////////////// |
---|
88 | // homogenize homogenizes f with Variable x // |
---|
89 | /////////////////////////////////////////////////////////////// |
---|
90 | static CanonicalForm |
---|
91 | homogenize( const CanonicalForm & f, const Variable & x){ |
---|
92 | CFList Newlist, Termlist= get_Terms(f); |
---|
93 | int maxdeg=totaldegree(f), deg; |
---|
94 | CFListIterator i; |
---|
95 | CanonicalForm elem, result=f.genZero(); |
---|
96 | |
---|
97 | for (i=Termlist; i.hasItem(); i++){ |
---|
98 | elem= i.getItem(); |
---|
99 | deg = totaldegree(elem); |
---|
100 | if ( deg < maxdeg ) |
---|
101 | Newlist.append(elem * power(x,maxdeg-deg)); |
---|
102 | else |
---|
103 | Newlist.append(elem); |
---|
104 | } |
---|
105 | for (i=Newlist; i.hasItem(); i++) // rebuild |
---|
106 | result += i.getItem(); |
---|
107 | |
---|
108 | return result; |
---|
109 | } |
---|
110 | |
---|
111 | // we assume g is square-free |
---|
112 | CFFList |
---|
113 | HomogFactor( const CanonicalForm & g, const Variable & minpoly, const int Mainvar ){ |
---|
114 | DEBINCLEVEL(cout, "HomogFactor"); |
---|
115 | Variable xn = get_max_degree_Variable(g); |
---|
116 | int d_xn = degree(g,xn); |
---|
117 | CFMap n; |
---|
118 | CanonicalForm F = compress(g(1,xn),n); // must compress F! |
---|
119 | |
---|
120 | DEBOUTLN(cout, "xn= ", xn); |
---|
121 | DEBOUTLN(cout, "d_xn= ", d_xn); |
---|
122 | DEBOUTLN(cout, "F= ", F); |
---|
123 | |
---|
124 | // should we do this for low degree polys g ? e.g. quadratic? |
---|
125 | // |
---|
126 | CFFList Intermediatelist; |
---|
127 | if ( getCharacteristic() > 0 ) |
---|
128 | Intermediatelist = Factorized(F, minpoly, Mainvar); |
---|
129 | else |
---|
130 | Intermediatelist = factorize(F); // what support for char==0 ? |
---|
131 | CFFList Homoglist; |
---|
132 | CFFListIterator j; |
---|
133 | for ( j=Intermediatelist; j.hasItem(); j++ ) |
---|
134 | Homoglist.append(CFFactor( n(j.getItem().factor()), j.getItem().exp()) ); |
---|
135 | // Now we have uncompressed factors in Homoglist |
---|
136 | DEBOUTLN(cout, "F factors as: ", Homoglist); |
---|
137 | CFFList Unhomoglist; |
---|
138 | CanonicalForm unhomogelem; |
---|
139 | for ( j=Homoglist; j.hasItem(); j++ ){ |
---|
140 | DEBOUTLN(cout, "Homogenizing ",j.getItem().factor()); |
---|
141 | unhomogelem= homogenize(j.getItem().factor(),xn); |
---|
142 | DEBOUTLN(cout, " that is ", unhomogelem); |
---|
143 | Unhomoglist.append(CFFactor(unhomogelem,j.getItem().exp())); |
---|
144 | d_xn -= degree(unhomogelem,xn); |
---|
145 | } |
---|
146 | DEBOUTLN(cout, "Power of xn to append is ", d_xn); |
---|
147 | if ( d_xn != 0 ) // have to append xn^(d_xn) |
---|
148 | Unhomoglist.append(CFFactor(CanonicalForm(xn),d_xn)); |
---|
149 | |
---|
150 | DEBDECLEVEL(cout, "HomogFactor"); |
---|
151 | return Unhomoglist; |
---|
152 | } |
---|
153 | |
---|
154 | /* |
---|
155 | $Log: not supported by cvs2svn $ |
---|
156 | Revision 1.2 1997/04/25 22:35:20 michael |
---|
157 | changed cerr and cout messages for use with Singular |
---|
158 | Version for libfac-0.2.1 |
---|
159 | |
---|
160 | Revision 1.1 1997/01/17 05:05:48 michael |
---|
161 | Initial revision |
---|
162 | |
---|
163 | */ |
---|