[1a80b4] | 1 | //////////////////////////////////////////////////////////// |
---|
[341696] | 2 | // $Id$ |
---|
[1a80b4] | 3 | //////////////////////////////////////////////////////////// |
---|
| 4 | // FACTORY - Includes |
---|
| 5 | #include <factory.h> |
---|
[e2ca88] | 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 | |
---|
[1a80b4] | 20 | // Factor - Includes |
---|
| 21 | #include "tmpl_inst.h" |
---|
| 22 | #include "Factor.h" |
---|
[4a81ec] | 23 | // some CC's need it: |
---|
| 24 | #include "homogfactor.h" |
---|
[1a80b4] | 25 | |
---|
[a38d45] | 26 | void out_cf(char *s1,const CanonicalForm &f,char *s2); |
---|
| 27 | |
---|
[1a80b4] | 28 | #ifdef HFACTORDEBUG |
---|
| 29 | # define DEBUGOUTPUT |
---|
| 30 | #else |
---|
| 31 | # undef DEBUGOUTPUT |
---|
| 32 | #endif |
---|
| 33 | |
---|
[d92d71] | 34 | #include <libfac/factor/debug.h> |
---|
[1a80b4] | 35 | #include "timing.h" |
---|
[be5dff] | 36 | TIMING_DEFINE_PRINT(hfactorize_time) |
---|
[1a80b4] | 37 | |
---|
[172b7ae] | 38 | #if 0 |
---|
[1a80b4] | 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 | } |
---|
[172b7ae] | 73 | #endif |
---|
[1a80b4] | 74 | |
---|
[e2a912] | 75 | #if 0 |
---|
[1a80b4] | 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; |
---|
[e2a912] | 88 | // now: return f.isHomogeneous(); |
---|
[1a80b4] | 89 | } |
---|
[e2a912] | 90 | #endif |
---|
[1a80b4] | 91 | |
---|
[172b7ae] | 92 | #if 0 |
---|
[1a80b4] | 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 | } |
---|
[172b7ae] | 107 | #endif |
---|
[1a80b4] | 108 | |
---|
[172b7ae] | 109 | #if 0 |
---|
[1a80b4] | 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 | } |
---|
[172b7ae] | 133 | #endif |
---|
[1a80b4] | 134 | |
---|
| 135 | // we assume g is square-free |
---|
| 136 | CFFList |
---|
[a38d45] | 137 | HomogFactor( const CanonicalForm & g, const CanonicalForm & minpoly, const int Mainvar ) |
---|
| 138 | { |
---|
[e2ca88] | 139 | DEBINCLEVEL(CERR, "HomogFactor"); |
---|
[1a80b4] | 140 | Variable xn = get_max_degree_Variable(g); |
---|
[b22f61] | 141 | //out_cf("HomogFactor:",g,"\n"); |
---|
[1a80b4] | 142 | int d_xn = degree(g,xn); |
---|
[a38d45] | 143 | CanonicalForm F = g(1,xn); |
---|
[1a80b4] | 144 | |
---|
[e2ca88] | 145 | DEBOUTLN(CERR, "xn= ", xn); |
---|
| 146 | DEBOUTLN(CERR, "d_xn= ", d_xn); |
---|
| 147 | DEBOUTLN(CERR, "F= ", F); |
---|
[b22f61] | 148 | //out_cf("HomogFactor:subst ",F,"\n"); |
---|
[1a80b4] | 149 | |
---|
| 150 | // should we do this for low degree polys g ? e.g. quadratic? |
---|
| 151 | // |
---|
| 152 | CFFList Homoglist; |
---|
| 153 | CFFListIterator j; |
---|
[f152c5] | 154 | if (minpoly.isZero()) |
---|
| 155 | Homoglist = factorize(F, 1); |
---|
[a38d45] | 156 | else |
---|
[9b87a3] | 157 | { |
---|
[f152c5] | 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()) ); |
---|
[9b87a3] | 163 | } |
---|
[1a80b4] | 164 | // Now we have uncompressed factors in Homoglist |
---|
[e2ca88] | 165 | DEBOUTLN(CERR, "F factors as: ", Homoglist); |
---|
[1a80b4] | 166 | CFFList Unhomoglist; |
---|
| 167 | CanonicalForm unhomogelem; |
---|
[9b87a3] | 168 | if ((!(minpoly.isZero())) &&(getCharacteristic() == 0) ) |
---|
[a38d45] | 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); |
---|
[b22f61] | 188 | //out_cf("unhomogelem:",unhomogelem,"\n"); |
---|
[a38d45] | 189 | Unhomoglist.append(CFFactor(unhomogelem,j.getItem().exp())); |
---|
| 190 | d_xn -= degree(unhomogelem,xn)*j.getItem().exp(); |
---|
| 191 | } |
---|
[1a80b4] | 192 | } |
---|
[e2ca88] | 193 | DEBOUTLN(CERR, "Power of xn to append is ", d_xn); |
---|
[1a80b4] | 194 | if ( d_xn != 0 ) // have to append xn^(d_xn) |
---|
| 195 | Unhomoglist.append(CFFactor(CanonicalForm(xn),d_xn)); |
---|
| 196 | |
---|
[e2ca88] | 197 | DEBDECLEVEL(CERR, "HomogFactor"); |
---|
[1a80b4] | 198 | return Unhomoglist; |
---|
| 199 | } |
---|
| 200 | |
---|