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