source: git/libfac/factor/homogfactor.cc @ 924d8f

spielwiese
Last change on this file since 924d8f was 924d8f, checked in by Hans Schoenemann <hannes@…>, 14 years ago
svn does not support $ git-svn-id: file:///usr/local/Singular/svn/trunk@12915 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 5.9 KB
Line 
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
26void out_cf(char *s1,const CanonicalForm &f,char *s2);
27
28CFFList Factorize2(CanonicalForm F, const CanonicalForm & minpoly );
29
30#ifdef HFACTORDEBUG
31#  define DEBUGOUTPUT
32#else
33#  undef DEBUGOUTPUT
34#endif
35
36#include "debug.h"
37#include "timing.h"
38TIMING_DEFINE_PRINT(hfactorize_time);
39
40#if 0
41///////////////////////////////////////////////////////////////
42// get_Terms: Split the polynomial in the containing terms.  //
43// getTerms: the real work is done here.                     //
44///////////////////////////////////////////////////////////////
45static void
46getTerms( const CanonicalForm & f, const CanonicalForm & t, CFList & result ){
47
48  if ( getNumVars(f) == 0 ) result.append(f*t);
49  else{
50    Variable x(level(f));
51    for ( CFIterator i=f; i.hasTerms(); i++ )
52      getTerms( i.coeff(), t*power(x,i.exp()), result);
53  }
54}
55
56CFList
57get_Terms( const CanonicalForm & f ){
58  CFList result,dummy,dummy2;
59  CFIterator i;
60  CFListIterator j;
61
62  if ( getNumVars(f) == 0 ) result.append(f);
63  else{
64    Variable _x(level(f));
65    for ( i=f; i.hasTerms(); i++ ){
66      getTerms(i.coeff(), 1, dummy);
67      for ( j=dummy; j.hasItem(); j++ )
68        result.append(j.getItem() * power(_x, i.exp()));
69
70      dummy= dummy2; // have to initalize new
71    }
72  }
73  return result;
74}
75#endif
76
77#if 0
78///////////////////////////////////////////////////////////////
79// is_homogeneous returns 1 iff f is homogeneous, 0 otherwise//
80///////////////////////////////////////////////////////////////
81bool
82is_homogeneous( const CanonicalForm & f){
83  CFList termlist= get_Terms(f);
84  CFListIterator i;
85  int deg= totaldegree(termlist.getFirst());
86
87  for ( i=termlist; i.hasItem(); i++ )
88    if ( totaldegree(i.getItem()) != deg ) return 0;
89  return 1;
90  // now: return f.isHomogeneous();
91}
92#endif
93
94#if 0
95///////////////////////////////////////////////////////////////
96// get_max_degree_Variable returns Variable with             //
97// highest degree. We assume f is *not* a constant!          //
98///////////////////////////////////////////////////////////////
99static Variable
100get_max_degree_Variable(const CanonicalForm & f){
101  ASSERT( ( ! f.inCoeffDomain() ), "no constants" );
102  int max=0, maxlevel=0, n=level(f);
103  for ( int i=1; i<=n; i++ )
104    if (degree(f,Variable(i)) >= max) {
105      max= degree(f,Variable(i)); maxlevel= i;
106    }
107  return Variable(maxlevel);
108}
109#endif
110
111#if 0
112///////////////////////////////////////////////////////////////
113// homogenize homogenizes f with Variable x                  //
114///////////////////////////////////////////////////////////////
115static CanonicalForm
116homogenize( const CanonicalForm & f, const Variable & x){
117  CFList Newlist, Termlist= get_Terms(f);
118  int maxdeg=totaldegree(f), deg;
119  CFListIterator i;
120  CanonicalForm elem, result=f.genZero();
121 
122  for (i=Termlist; i.hasItem(); i++){
123    elem= i.getItem();
124    deg = totaldegree(elem);
125    if ( deg < maxdeg )
126      Newlist.append(elem * power(x,maxdeg-deg));
127    else
128      Newlist.append(elem);
129  }
130  for (i=Newlist; i.hasItem(); i++) // rebuild
131    result += i.getItem();
132   
133  return result;
134}
135#endif
136
137// we assume g is square-free
138CFFList
139HomogFactor( const CanonicalForm & g, const CanonicalForm  & minpoly, const int Mainvar )
140{
141  DEBINCLEVEL(CERR, "HomogFactor");
142  Variable xn = get_max_degree_Variable(g);
143  //out_cf("HomogFactor:",g,"\n");
144  int d_xn = degree(g,xn);
145  CanonicalForm F = g(1,xn);
146
147  DEBOUTLN(CERR, "xn= ", xn);
148  DEBOUTLN(CERR, "d_xn=   ", d_xn);
149  DEBOUTLN(CERR, "F= ", F); 
150  //out_cf("HomogFactor:subst ",F,"\n");
151
152  // should we do this for low degree polys g ? e.g. quadratic?
153  //
154  CFFList Intermediatelist;
155  CFFList Homoglist;
156  CFFListIterator j;
157  if ( getCharacteristic() > 0 )
158  {
159     CFMap n;
160     if (minpoly.isZero())
161       Intermediatelist = Factorize(compress(F,n), 1);
162     else
163       Intermediatelist = Factorized(compress(F,n), minpoly, Mainvar);
164     for ( j=Intermediatelist; j.hasItem(); j++ )
165       Homoglist.append(CFFactor( n(j.getItem().factor()), j.getItem().exp()) );
166  }
167  else
168  {
169     if (minpoly.isZero())
170       Homoglist = factorize(F);
171     else
172       Homoglist = Factorize2(F,minpoly);
173  }
174  // Now we have uncompressed factors in Homoglist
175  DEBOUTLN(CERR, "F factors as: ", Homoglist);
176  CFFList Unhomoglist;
177  CanonicalForm unhomogelem;
178  if ((!(minpoly.isZero())) &&(getCharacteristic() == 0) )
179  {
180    for ( j=Homoglist; j.hasItem(); j++ )
181    {
182      // assume that minpoly.level() < level of all "real" variables of g
183      DEBOUTLN(CERR, "Homogenizing ",j.getItem().factor()); 
184      unhomogelem= homogenize(j.getItem().factor(),xn,
185                         Variable(minpoly.level()+1),g.mvar());
186      DEBOUTLN(CERR, "      that is ", unhomogelem);
187      Unhomoglist.append(CFFactor(unhomogelem,j.getItem().exp()));
188      d_xn -= degree(unhomogelem,xn)*j.getItem().exp();
189    }
190  }
191  else
192  {
193    for ( j=Homoglist; j.hasItem(); j++ )
194    {
195      DEBOUTLN(CERR, "Homogenizing ",j.getItem().factor()); 
196      unhomogelem= homogenize(j.getItem().factor(),xn);
197      DEBOUTLN(CERR, "      that is ", unhomogelem);
198      //out_cf("unhomogelem:",unhomogelem,"\n");
199      Unhomoglist.append(CFFactor(unhomogelem,j.getItem().exp()));
200      d_xn -= degree(unhomogelem,xn)*j.getItem().exp();
201    }
202  }
203  DEBOUTLN(CERR, "Power of xn to append is ", d_xn);
204  if ( d_xn != 0 ) // have to append xn^(d_xn)
205    Unhomoglist.append(CFFactor(CanonicalForm(xn),d_xn));
206
207  DEBDECLEVEL(CERR, "HomogFactor");
208  return Unhomoglist;
209}
210
Note: See TracBrowser for help on using the repository browser.