source: git/factory/facSparseHensel.h @ ba5e9e

spielwiese
Last change on this file since ba5e9e was f377d6, checked in by Martin Lee <martinlee84@…>, 11 years ago
chg: leave Lucks sparse lifting earlier if input is too dense
  • Property mode set to 100644
File size: 14.6 KB
Line 
1/*****************************************************************************\
2 * Computer Algebra System SINGULAR
3\*****************************************************************************/
4/** @file facFqFactorize.h
5 *
6 * This file provides functions for sparse heuristic Hensel lifting
7 *
8 * @author Martin Lee
9 *
10 **/
11/*****************************************************************************/
12
13#ifndef FAC_SPARSE_HENSEL_H
14#define FAC_SPARSE_HENSEL_H
15
16#include "canonicalform.h"
17#include "cf_map_ext.h"
18#include "cf_iter.h"
19#include "templates/ftmpl_functions.h"
20#include "cf_algorithm.h"
21#include "cf_map.h"
22
23/// compare polynomials
24inline
25int comp (const CanonicalForm& A, const CanonicalForm& B)
26{
27  if (A.inCoeffDomain() && !B.inCoeffDomain())
28    return -1;
29  else if (!A.inCoeffDomain() && B.inCoeffDomain())
30    return 1;
31  else if (A.inCoeffDomain() && B.inCoeffDomain())
32    return 0;
33  else if (degree (A, 1) > degree (B, 1))
34    return 1;
35  else if (degree (A, 1) < degree (B, 1))
36    return -1;
37  // here A and B are not in CoeffDomain
38  int n= tmax (A.level(), B.level());
39  for (int i= 2; i <= n; i++)
40  {
41    if (degree (A,i) > degree (B,i))
42      return 1;
43    else if (degree (A,i) < degree (B,i))
44      return -1;
45  }
46  return 0;
47}
48
49/// compare two polynomials up to level @a level
50inline
51int comp (const CanonicalForm& A, const CanonicalForm& B, int level)
52{
53  if (A.inCoeffDomain() && !B.inCoeffDomain() && B.level() <= level)
54    return -1;
55  else if (!A.inCoeffDomain() && A.level() <= level && B.inCoeffDomain())
56    return 1;
57  else if (A.inCoeffDomain() && B.inCoeffDomain())
58    return 0;
59  else if (degree (A, 1) > degree (B, 1))
60    return 1;
61  else if (degree (A, 1) < degree (B, 1))
62    return -1;
63  // here A and B are not in coeffdomain
64  for (int i= 2; i <= level; i++)
65  {
66    if (degree (A,i) > degree (B,i))
67      return 1;
68    else if (degree (A,i) < degree (B,i))
69      return -1;
70  }
71  return 0;
72}
73
74/// swap entry @a i and @a j in @a A
75inline
76void swap (CFArray& A, int i, int j)
77{
78  CanonicalForm tmp= A[i];
79  A[i]= A[j];
80  A[j]= tmp;
81}
82
83/// quick sort helper function
84inline
85void quickSort (int lo, int hi, CFArray& A, int l)
86{
87  int i= lo, j= hi;
88  CanonicalForm tmp= A[(lo+hi)/2];
89  while (i <= j)
90  {
91    if (l > 0)
92    {
93      while (comp (A [i], tmp, l) < 0 && i < hi) i++;
94      while (comp (tmp, A[j], l) < 0 && j > lo) j--;
95    }
96    else
97    {
98      while (comp (A [i], tmp) < 0 && i < hi) i++;
99      while (comp (tmp, A[j]) < 0 && j > lo) j--;
100    }
101    if (i <= j)
102    {
103      swap (A, i, j);
104      i++;
105      j--;
106    }
107  }
108  if (lo < j) quickSort (lo, j, A, l);
109  if (i < hi) quickSort (i, hi, A, l);
110}
111
112/// quick sort @a A
113inline
114void sort (CFArray& A, int l= 0)
115{
116  quickSort (0, A.size() - 1, A, l);
117}
118
119
120/// find normalizing factors for @a biFactors and build monic univariate factors
121/// from @a biFactors
122inline CFList
123findNormalizingFactor1 (const CFList& biFactors, const CanonicalForm& evalPoint,
124                        CFList& uniFactors)
125{
126  CFList result;
127  CanonicalForm tmp;
128  for (CFListIterator i= biFactors; i.hasItem(); i++)
129  {
130    tmp= i.getItem() (evalPoint);
131    uniFactors.append (tmp /Lc (tmp));
132    result.append (Lc (tmp));
133  }
134  return result;
135}
136
137/// find normalizing factors for @a biFactors and sort @a biFactors s.t.
138/// the returned @a biFactors evaluated at evalPoint coincide with @a uniFactors
139inline CFList
140findNormalizingFactor2 (CFList& biFactors, const CanonicalForm& evalPoint,
141                        const CFList& uniFactors)
142{
143  CFList result;
144  CFList uniBiFactors= biFactors;
145  CFList newBiFactors;
146  CFList l;
147  int pos;
148  CFListIterator iter;
149  for (iter= uniBiFactors; iter.hasItem(); iter++)
150  {
151    iter.getItem()= iter.getItem() (evalPoint);
152    l.append (Lc (iter.getItem()));
153    iter.getItem() /= Lc (iter.getItem());
154  }
155  for (CFListIterator i= uniFactors; i.hasItem(); i++)
156  {
157    pos= findItem (uniBiFactors, i.getItem());
158    newBiFactors.append (getItem (biFactors, pos));
159    result.append (getItem (l, pos));
160  }
161  biFactors= newBiFactors;
162  return result;
163}
164
165/// get terms of @a F
166inline CFArray
167getTerms (const CanonicalForm& F)
168{
169  if (F.inCoeffDomain())
170  {
171    CFArray result= CFArray (1);
172    result [0]= F;
173    return result;
174  }
175  if (F.isUnivariate())
176  {
177    CFArray result= CFArray (size(F));
178    int j= 0;
179    for (CFIterator i= F; i.hasTerms(); i++, j++)
180      result[j]= i.coeff()*power (F.mvar(), i.exp());
181    return result;
182  }
183  int numMon= size (F);
184  CFArray result= CFArray (numMon);
185  int j= 0;
186  CFArray recResult;
187  Variable x= F.mvar();
188  CanonicalForm powX;
189  for (CFIterator i= F; i.hasTerms(); i++)
190  {
191    powX= power (x, i.exp());
192    recResult= getTerms (i.coeff());
193    for (int k= 0; k < recResult.size(); k++)
194      result[j+k]= powX*recResult[k];
195    j += recResult.size();
196  }
197  return result;
198}
199
200/// helper function for getBiTerms
201inline CFArray
202getBiTerms_helper (const CanonicalForm& F, const CFMap& M, int threshold)
203{
204  CFArray buf= CFArray (size (F));
205  int k= 0, level= F.level() - 1;
206  Variable x= F.mvar();
207  Variable y= Variable (F.level() - 1);
208  Variable one= Variable (1);
209  Variable two= Variable (2);
210  CFIterator j;
211  for (CFIterator i= F; i.hasTerms(); i++)
212  {
213    if (i.coeff().level() < level)
214    {
215      buf[k]= M (i.coeff())*power (one,i.exp());
216      k++;
217      if (k > threshold)
218        break;
219      continue;
220    }
221    j= i.coeff();
222    for (;j.hasTerms() && k <= threshold; j++, k++)
223      buf[k]= power (one,i.exp())*power (two,j.exp())*M (j.coeff());
224    if (k > threshold)
225      break;
226  }
227  CFArray result= CFArray (k);
228  for (int i= 0; i < k && k <= threshold; i++)
229    result[i]= buf[i];
230  return result;
231}
232
233/// get terms of @a F where F is considered a bivariate poly in Variable(1),
234/// Variable (2)
235inline CFArray
236getBiTerms (const CanonicalForm& F, int threshold)
237{
238  if (F.inCoeffDomain())
239  {
240    CFArray result= CFArray (1);
241    result [0]= F;
242    return result;
243  }
244  if (F.isUnivariate())
245  {
246    CFArray result= CFArray (size(F));
247    int j= 0;
248    for (CFIterator i= F; i.hasTerms(); i++, j++)
249      result[j]= i.coeff()*power (F.mvar(), i.exp());
250    return result;
251  }
252
253  CanonicalForm G= F;
254
255  CFMap M;
256  M.newpair (Variable (1), F.mvar());
257  M.newpair (Variable (2), Variable (F.level() - 1));
258  G= swapvar (F, Variable (1), F.mvar());
259  G= swapvar (G, Variable (2), Variable (F.level() - 1));
260
261  CFArray result= getBiTerms_helper (G, M, threshold);
262  return result;
263}
264
265/// build a poly from entries in @a A
266inline CanonicalForm
267buildPolyFromArray (const CFArray& A)
268{
269  CanonicalForm result= 0;
270  for (int i= A.size() - 1; i > -1; i--)
271    result += A[i];
272  return result;
273}
274
275/// group together elements in @a A, where entries in @a A are put together
276/// if they coincide up to level @a level
277inline void
278groupTogether (CFArray& A, int level)
279{
280  int n= A.size() - 1;
281  int k= A.size();
282  for (int i= 0; i < n; i++)
283  {
284    if (comp (A[i],A[i+1], level) == 0)
285    {
286      A[i+1] += A[i];
287      A[i]= 0;
288      k--;
289    }
290  }
291  if (A[n].isZero())
292    k--;
293  CFArray B= CFArray (k);
294  n++;
295  k= 0;
296  for (int i= 0; i < n; i++)
297  {
298    if (!A[i].isZero())
299    {
300      B[k]= A[i];
301      k++;
302    }
303  }
304  A= B;
305}
306
307/// strip off those parts of entries in @a F whose level is less than or equal
308/// than @a level and store the stripped off parts in @a G
309inline void
310strip (CFArray& F, CFArray& G, int level)
311{
312  int n, m, i, j;
313  CanonicalForm g;
314  m= F.size();
315  G= CFArray (m);
316  for (j= 0; j < m; j++)
317  {
318    g= 1;
319    for (i= 1; i <= level; i++)
320    {
321      if ((n= degree (F[j],i)) > 0)
322        g *= power (Variable (i), n);
323    }
324    F[j] /= g;
325    G[j]= g;
326  }
327}
328
329/// s.a. stripped off parts are not returned
330inline void
331strip (CFArray& F, int level)
332{
333  int n, m, i;
334  CanonicalForm g;
335  m= F.size();
336  for (int j= 0; j < m; j++)
337  {
338    g= 1;
339    for (i= 1; i <= level; i++)
340    {
341      if ((n= degree (F[j],i)) > 0)
342        g *= power (Variable (i), n);
343    }
344    F[j] /= g;
345  }
346}
347
348/// get equations for LucksWangSparseHeuristic
349inline
350CFArray getEquations (const CFArray& A, const CFArray& B)
351{
352  ASSERT (A.size() == B.size(), "size of A and B has to coincide");
353  CFArray result= CFArray (A.size());
354  int n= A.size();
355  for (int i= 0; i < n; i++)
356    result[i]= A[i]-B[i];
357  return result;
358}
359
360/// evaluate every entry of @a A at @a B and level @a level
361inline void
362evaluate (CFArray& A, const CanonicalForm& B, int level)
363{
364  int n= A.size();
365  for (int i= 0; i < n; i++)
366  {
367    if (A[i].level() < level)
368      continue;
369    else
370      A[i]= A[i] (B, level);
371  }
372}
373
374/// evaluate every entry of @a A at every entry of @a B starting at level @a
375/// level
376inline void
377evaluate (CFArray& A, const CFArray& B, int level)
378{
379  int n= B.size();
380  for (int i= 0; i < n; i++)
381  {
382    if (!B[i].isZero())
383      evaluate (A, B[i], level + i);
384  }
385}
386
387/// simplify @a A if possible, i.e. @a A consists of 2 terms and contains only
388/// one variable of level greater or equal than @a level
389inline CanonicalForm
390simplify (const CanonicalForm& A, int level)
391{
392  CanonicalForm F= 0;
393  if (size (A, A.level()) == 2)
394  {
395    CanonicalForm C= getVars (A);
396    if ((C/C.mvar()).level() < level)
397    {
398      CanonicalForm B= LC (A);
399      if (B.level() < level)
400        F= -tailcoeff (A/B);
401    }
402  }
403  return F;
404}
405
406///  if possible simplify @a A as described above and store result in @a B
407inline bool
408simplify (CFArray& A, CFArray& B, int level)
409{
410  int n= A.size();
411  CanonicalForm f;
412  int index;
413  for (int i= 0; i < n; i++)
414  {
415    if (!A[i].isZero())
416    {
417      f= simplify (A[i], level);
418      if (!f.isZero())
419      {
420        index= A[i].level() - level;
421        if (index < 0 || index >= B.size())
422          return false;
423        if (!B[index].isZero() && B[index] != f)
424          return false;
425        else if (B[index].isZero())
426        {
427          B[index]= f;
428          A[i]= 0;
429        }
430      }
431    }
432  }
433  return true;
434}
435
436/// merge @a B into @a A if possible, i.e. every non-zero entry in @a A should
437/// be zero in @a B
438inline bool
439merge (CFArray& A, CFArray& B)
440{
441  if (A.size() != B.size())
442    return false;
443  int n= A.size();
444  for (int i= 0; i < n; i++)
445  {
446    if (!B[i].isZero())
447    {
448      if (A[i].isZero())
449      {
450        A[i]= B[i];
451        B[i]= 0;
452      }
453      else if (A[i] == B[i])
454        B[i]= 0;
455      else
456        return false;
457    }
458  }
459  return true;
460}
461
462/// checks if entries of @a A are zero
463inline bool
464isZero (const CFArray& A)
465{
466  int n= A.size();
467  for (int i= 0; i < n; i++)
468    if (!A[i].isZero())
469      return false;
470  return true;
471}
472
473/// checks if @a A equals @a B
474inline bool
475isEqual (const CFArray& A, const CFArray& B)
476{
477  if (A.size() != B.size())
478    return false;
479  int i, n= B.size();
480  for (i= 0; i < n; i++)
481    if (A[i] != B[i])
482      return false;
483  return true;
484}
485
486/// get terms of @a F wrt. Variable (1)
487inline CFArray
488getTerms2 (const CanonicalForm& F)
489{
490  if (F.inCoeffDomain())
491  {
492    CFArray result= CFArray (1);
493    result[0]= F;
494    return result;
495  }
496  CFArray result= CFArray (size (F));
497  int j= 0;
498  Variable x= F.mvar();
499  Variable y= Variable (1);
500  CFIterator k;
501  for (CFIterator i= F; i.hasTerms(); i++)
502  {
503    if (i.coeff().inCoeffDomain())
504    {
505      result[j]= i.coeff()*power (x,i.exp());
506      j++;
507    }
508    else
509    {
510      for (k= i.coeff(); k.hasTerms(); k++, j++)
511        result[j]= k.coeff()*power (x,i.exp())*power (y,k.exp());
512    }
513  }
514  sort (result);
515  return result;
516}
517
518/// get terms of entries in @a F and put them in @a result
519inline
520void getTerms2 (const CFList& F, CFArray* result)
521{
522  int j= 0;
523  for (CFListIterator i= F; i.hasItem(); i++, j++)
524    result[j]= getTerms2 (i.getItem());
525}
526
527/// evaluate entries in @a A at @a eval and @a y
528inline CFArray
529evaluate (const CFArray& A, const CanonicalForm& eval, const Variable& y)
530{
531  int j= A.size();
532  CFArray result= CFArray (j);
533  for (int i= 0; i < j; i++)
534    result [i]= A[i] (eval, y);
535  return result;
536}
537
538/// s.a.
539inline CFArray*
540evaluate (CFArray* const& A, int sizeA, const CanonicalForm& eval,
541          const Variable& y)
542{
543  CFArray* result= new CFArray [sizeA];
544  for (int i= 0; i < sizeA; i++)
545    result[i]= evaluate (A[i], eval, y);
546  return result;
547}
548
549/// normalize entries in @a L with @a normalizingFactor
550inline
551CFList normalize (const CFList& L, const CFList& normalizingFactor)
552{
553  CFList result;
554  CFListIterator j= normalizingFactor;
555  for (CFListIterator i= L; i.hasItem(); i++, j++)
556    result.append (i.getItem() / j.getItem());
557  return result;
558}
559
560/// search for @a F in @a A between index @a i and @a j
561inline
562int search (const CFArray& A, const CanonicalForm& F, int i, int j)
563{
564  for (; i < j; i++)
565    if (A[i] == F)
566      return i;
567  return -1;
568}
569
570/// patch together @a F1 and @a F2 and normalize by a power of @a eval
571/// @a F1 and @a F2 are assumed to be bivariate with one variable having level 1
572inline
573CanonicalForm patch (const CanonicalForm& F1, const CanonicalForm& F2,
574                     const CanonicalForm& eval)
575{
576  CanonicalForm result= F1;
577  if (F2.level() != 1 && !F2.inCoeffDomain())
578  {
579    int d= degree (F2);
580    result *= power (F2.mvar(), d);
581    result /= power (eval, d);
582  }
583  return result;
584}
585
586/// sparse heuristic lifting by Wang and Lucks
587///
588/// @return @a LucksWangSparseHeuristic returns true if it was successful
589int
590LucksWangSparseHeuristic (const CanonicalForm& F,     ///<[in] polynomial to be
591                                                      ///< factored
592                          const CFList& factors,      ///<[in] factors of F
593                                                      ///< lifted to level
594                          int level,                  ///<[in] level of lifted
595                                                      ///< factors
596                          const CFList& leadingCoeffs,///<[in] leading
597                                                      ///< coefficients of
598                                                      ///< factors
599                          CFList& result              ///<[in,out] result
600                         );
601
602/// sparse heuristic which patches together bivariate factors of @a A wrt.
603/// different second variables by their univariate images
604///
605/// @return @a sparseHeuristic returns a list of found factors of A
606CFList
607sparseHeuristic (const CanonicalForm& A,  ///<[in] polynomial to be factored
608                 const CFList& biFactors, ///<[in] bivariate factors of A where
609                                          ///< the second variable has level 2
610                 CFList*& moreBiFactors,  ///<[in] more bivariate factorizations
611                                          ///< wrt. different second variables
612                 const CFList& evaluation,///<[in] evaluation point
613                 int minFactorsLength     ///<[in] minimal length of bivariate
614                                          ///< factorizations
615                );
616
617#endif
Note: See TracBrowser for help on using the repository browser.