source: git/libpolys/polys/clapsing.cc @ 9f7665

spielwiese
Last change on this file since 9f7665 was 9f7665, checked in by Oleksandr Motsak <motsak@…>, 10 years ago
Removed HAVE_CONFIG guards fix: fixed the inclusion of configure-generated *config.h's
  • Property mode set to 100644
File size: 40.7 KB
Line 
1// emacs edit mode for this file is -*- C++ -*-
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5/*
6* ABSTRACT: interface between Singular and factory
7*/
8
9//#define FACTORIZE2_DEBUG
10
11
12
13
14
15
16
17#include <misc/auxiliary.h>
18#include "clapsing.h"
19
20#include <factory/factory.h>
21
22#    include <factory/libfac/libfac.h>
23
24
25#include <omalloc/omalloc.h>
26#include <coeffs/numbers.h>
27#include <coeffs/coeffs.h>
28#include <coeffs/bigintmat.h>
29
30// #include <kernel/ffields.h>
31
32#include "monomials/ring.h"
33#include "simpleideals.h"
34
35
36//#include "polys.h"
37#define TRANSEXT_PRIVATES
38
39#include "ext_fields/transext.h"
40
41
42#include "clapconv.h"
43// #include <kernel/clapconv.h>
44
45#include <polys/monomials/p_polys.h>
46#include <polys/monomials/ring.h>
47#include <polys/simpleideals.h>
48#include <misc/intvec.h>
49#include <polys/matpol.h>
50#include <coeffs/bigintmat.h>
51
52
53void out_cf(const char *s1,const CanonicalForm &f,const char *s2);
54
55poly singclap_gcd_r ( poly f, poly g, const ring r )
56{
57  poly res=NULL;
58
59  assume(f!=NULL);
60  assume(g!=NULL);
61
62  if(pNext(f)==NULL) 
63  {
64    poly p=p_One(r);
65    if (pNext(g)==NULL)
66    {
67      for(int i=rVar(r);i>0;i--)
68        p_SetExp(p,i,si_min(p_GetExp(f,i,r),p_GetExp(g,i,r)),r);
69      p_Setm(p,r);
70      return p;
71    }
72#if 0
73    else
74    {
75      poly h=g;
76      for(int i=rVar(r);i>0;i--)
77        p_SetExp(p,i,p_GetExp(f,i,r),r);
78      while(h!=NULL)
79      {
80        for(int i=rVar(r);i>0;i--)
81          p_SetExp(p,i,si_min(p_GetExp(p,i,r),p_GetExp(h,i,r)),r);
82        pIter(h);
83      }
84      p_Setm(p,r);
85      return p;
86    }
87#endif
88  }
89#if 0
90  else if (pNext(g)==NULL)
91  {
92    poly p=p_One(r);
93    poly h=f;
94    for(int i=rVar(r);i>0;i--)
95      p_SetExp(p,i,p_GetExp(g,i,r),r);
96    while(h!=NULL)
97    {
98      for(int i=rVar(r);i>0;i--)
99        p_SetExp(p,i,si_min(p_GetExp(p,i,r),p_GetExp(h,i,r)),r);
100      pIter(h);
101    }
102    p_Setm(p,r);
103    return p;
104  }
105#endif
106
107  Off(SW_RATIONAL);
108  if (rField_is_Q(r) || (rField_is_Zp(r)))
109  {
110    setCharacteristic( rChar(r) );
111    CanonicalForm F( convSingPFactoryP( f,r ) ), G( convSingPFactoryP( g, r ) );
112    res=convFactoryPSingP( gcd( F, G ) , r);
113  }
114  // and over Q(a) / Fp(a)
115  else if ( r->cf->extRing!=NULL )
116  {
117    if ( rField_is_Q_a(r)) setCharacteristic( 0 );
118    else                   setCharacteristic( rChar(r) );
119    if (r->cf->extRing->qideal!=NULL)
120    {
121      bool b1=isOn(SW_USE_QGCD);
122      if ( rField_is_Q_a(r) ) On(SW_USE_QGCD);
123      CanonicalForm mipo=convSingPFactoryP(r->cf->extRing->qideal->m[0],
124                                           r->cf->extRing);
125      Variable a=rootOf(mipo);
126      CanonicalForm F( convSingAPFactoryAP( f,a,r ) ),
127                    G( convSingAPFactoryAP( g,a,r ) );
128      res= convFactoryAPSingAP( gcd( F, G ),r );
129      if (!b1) Off(SW_USE_QGCD);
130    }
131    else
132    {
133      CanonicalForm F( convSingTrPFactoryP( f,r ) ), G( convSingTrPFactoryP( g,r ) );
134      res= convFactoryPSingTrP( gcd( F, G ),r );
135    }
136  }
137  else
138    WerrorS( feNotImplemented );
139  Off(SW_RATIONAL);
140  return res;
141}
142
143poly singclap_gcd_and_divide ( poly& f, poly& g, const ring r)
144{
145  poly res=NULL;
146
147  if (g == NULL)
148  {
149    res= p_Copy (f,r);
150    p_Delete (&f, r);
151    f=p_One (r);
152    return res;
153  }
154  if (f==NULL)
155  {
156    res= p_Copy (g,r);
157    p_Delete (&g, r);
158    g=p_One (r);
159    return res;
160  }
161
162  Off(SW_RATIONAL);
163  CanonicalForm F,G,GCD;
164  if (rField_is_Q(r) || (rField_is_Zp(r)))
165  {
166    bool b1=isOn(SW_USE_EZGCD_P);
167    setCharacteristic( rChar(r) );
168    F=convSingPFactoryP( f,r );
169    G=convSingPFactoryP( g,r );
170    GCD=gcd(F,G);
171    if (!GCD.isOne())
172    {
173      p_Delete(&f,r);
174      p_Delete(&g,r);
175      if (getCharacteristic() == 0)
176        On (SW_RATIONAL);
177      F /= GCD;
178      G /= GCD;
179      if (getCharacteristic() == 0)
180      {
181        CanonicalForm denF= bCommonDen (F);
182        CanonicalForm denG= bCommonDen (G);
183        G *= denG;
184        F *= denF;
185        Off (SW_RATIONAL);
186        CanonicalForm gcddenFdenG= gcd (denG, denF);
187        denG /= gcddenFdenG;
188        denF /= gcddenFdenG;
189        On (SW_RATIONAL);
190        G *= denF;
191        F *= denG;
192      }
193      f=convFactoryPSingP( F, r);
194      g=convFactoryPSingP( G, r);
195    }
196    res=convFactoryPSingP( GCD , r);
197    if (!b1) Off (SW_USE_EZGCD_P);
198  }
199  // and over Q(a) / Fp(a)
200  else if ( r->cf->extRing )
201  {
202    if ( rField_is_Q_a(r)) setCharacteristic( 0 );
203    else                   setCharacteristic( rChar(r) );
204    if (r->cf->extRing->qideal!=NULL)
205    {
206      bool b1=isOn(SW_USE_QGCD);
207      if ( rField_is_Q_a(r) ) On(SW_USE_QGCD);
208      CanonicalForm mipo=convSingPFactoryP(r->cf->extRing->qideal->m[0],
209                                           r->cf->extRing);
210      Variable a=rootOf(mipo);
211      F=( convSingAPFactoryAP( f,a,r ) );
212      G=( convSingAPFactoryAP( g,a,r ) );
213      GCD=gcd(F,G);
214      if (!GCD.isOne())
215      {
216        p_Delete(&f,r);
217        p_Delete(&g,r);
218        if (getCharacteristic() == 0)
219          On (SW_RATIONAL);
220        F /= GCD;
221        G /= GCD;
222        if (getCharacteristic() == 0)
223        {
224          CanonicalForm denF= bCommonDen (F);
225          CanonicalForm denG= bCommonDen (G);
226          G *= denG;
227          F *= denF;
228          Off (SW_RATIONAL);
229          CanonicalForm gcddenFdenG= gcd (denG, denF);
230          denG /= gcddenFdenG;
231          denF /= gcddenFdenG;
232          On (SW_RATIONAL);
233          G *= denF;
234          F *= denG;
235        }
236        f= convFactoryAPSingAP( F,r );
237        g= convFactoryAPSingAP( G,r );
238      }
239      res= convFactoryAPSingAP( GCD,r );
240      if (!b1) Off(SW_USE_QGCD);
241    }
242    else
243    {
244      F=( convSingTrPFactoryP( f,r ) );
245      G=( convSingTrPFactoryP( g,r ) );
246      GCD=gcd(F,G);
247      if (!GCD.isOne())
248      {
249        p_Delete(&f,r);
250        p_Delete(&g,r);
251        if (getCharacteristic() == 0)
252          On (SW_RATIONAL);
253        F /= GCD;
254        G /= GCD;
255        if (getCharacteristic() == 0)
256        {
257          CanonicalForm denF= bCommonDen (F);
258          CanonicalForm denG= bCommonDen (G);
259          G *= denG;
260          F *= denF;
261          Off (SW_RATIONAL);
262          CanonicalForm gcddenFdenG= gcd (denG, denF);
263          denG /= gcddenFdenG;
264          denF /= gcddenFdenG;
265          On (SW_RATIONAL);
266          G *= denF;
267          F *= denG;
268        }
269        f= convFactoryPSingTrP( F,r );
270        g= convFactoryPSingTrP( G,r );
271      }
272      res= convFactoryPSingTrP( GCD,r );
273    }
274  }
275  else
276    WerrorS( feNotImplemented );
277  Off(SW_RATIONAL);
278  return res;
279}
280
281poly singclap_gcd ( poly f, poly g, const ring r)
282{
283  poly res=NULL;
284
285  if (f!=NULL) p_Cleardenom(f, r);             
286  if (g!=NULL) p_Cleardenom(g, r);
287  else         return f; // g==0 => gcd=f (but do a p_Cleardenom)
288  if (f==NULL) return g; // f==0 => gcd=g (but do a p_Cleardenom)
289
290  res=singclap_gcd_r(f,g,r);
291  p_Delete(&f, r);
292  p_Delete(&g, r);
293  return res;
294}
295
296/*2 find the maximal exponent of var(i) in poly p*/
297int pGetExp_Var(poly p, int i, const ring r)
298{
299  int m=0;
300  int mm;
301  while (p!=NULL)
302  {
303    mm=p_GetExp(p,i,r);
304    if (mm>m) m=mm;
305    pIter(p);
306  }
307  return m;
308}
309
310// destroys f,g,x
311poly singclap_resultant ( poly f, poly g , poly x, const ring r)
312{
313  poly res=NULL;
314  int i=p_IsPurePower(x, r);
315  if (i==0)
316  {
317    WerrorS("3rd argument must be a ring variable");
318    goto resultant_returns_res;
319  }
320  if ((f==NULL) || (g==NULL))
321    goto resultant_returns_res;
322  // for now there is only the possibility to handle polynomials over
323  // Q and Fp ...
324  if (rField_is_Zp(r) || rField_is_Q(r))
325  {
326    Variable X(i);
327    setCharacteristic( rChar(r) );
328    CanonicalForm F( convSingPFactoryP( f,r ) ), G( convSingPFactoryP( g,r ) );
329    res=convFactoryPSingP( resultant( F, G, X),r );
330    Off(SW_RATIONAL);
331    goto resultant_returns_res;
332  }
333  // and over Q(a) / Fp(a)
334  else if (r->cf->extRing!=NULL)
335  {
336    if (rField_is_Q_a(r)) setCharacteristic( 0 );
337    else               setCharacteristic( rChar(r) );
338    Variable X(i+rPar(r));
339    if (r->cf->extRing->qideal!=NULL)
340    {
341      //Variable X(i);
342      CanonicalForm mipo=convSingPFactoryP(r->cf->extRing->qideal->m[0],
343                                           r->cf->extRing);
344      Variable a=rootOf(mipo);
345      CanonicalForm F( convSingAPFactoryAP( f,a,r ) ),
346                    G( convSingAPFactoryAP( g,a,r ) );
347      res= convFactoryAPSingAP( resultant( F, G, X ),r );
348    }
349    else
350    {
351      //Variable X(i+rPar(currRing));
352      number nf,ng;
353      p_Cleardenom_n(f,r,nf);p_Cleardenom_n(g,r,ng);
354      int ef,eg;
355      ef=pGetExp_Var(f,i,r);
356      eg=pGetExp_Var(g,i,r);
357      CanonicalForm F( convSingTrPFactoryP( f,r ) ), G( convSingTrPFactoryP( g,r ) );
358      res= convFactoryPSingTrP( resultant( F, G, X ),r );
359      if ((nf!=NULL)&&(!n_IsOne(nf,r->cf)))
360      {
361        number n=n_Invers(nf,r->cf);
362        while(eg>0)
363        {
364          res=p_Mult_nn(res,n,r);
365          eg--;
366        }
367        n_Delete(&n,r->cf);
368      }
369      n_Delete(&nf,r->cf);
370      if ((ng!=NULL)&&(!n_IsOne(ng,r->cf)))
371      {
372        number n=n_Invers(ng,r->cf);
373        while(ef>0)
374        {
375          res=p_Mult_nn(res,n,r);
376          ef--;
377        }
378        n_Delete(&n,r->cf);
379      }
380      n_Delete(&ng,r->cf);
381    }
382    Off(SW_RATIONAL);
383    goto resultant_returns_res;
384  }
385  else
386    WerrorS( feNotImplemented );
387resultant_returns_res:
388  p_Delete(&f,r);
389  p_Delete(&g,r);
390  p_Delete(&x,r);
391  return res;
392}
393//poly singclap_resultant ( poly f, poly g , poly x)
394//{
395//  int i=pVar(x);
396//  if (i==0)
397//  {
398//    WerrorS("ringvar expected");
399//    return NULL;
400//  }
401//  ideal I=idInit(1,1);
402//
403//  // get the coeffs von f wrt. x:
404//  I->m[0]=pCopy(f);
405//  matrix ffi=mpCoeffs(I,i);
406//  ffi->rank=1;
407//  ffi->ncols=ffi->nrows;
408//  ffi->nrows=1;
409//  ideal fi=(ideal)ffi;
410//
411//  // get the coeffs von g wrt. x:
412//  I->m[0]=pCopy(g);
413//  matrix ggi=mpCoeffs(I,i);
414//  ggi->rank=1;
415//  ggi->ncols=ggi->nrows;
416//  ggi->nrows=1;
417//  ideal gi=(ideal)ggi;
418//
419//  // contruct the matrix:
420//  int fn=IDELEMS(fi); //= deg(f,x)+1
421//  int gn=IDELEMS(gi); //= deg(g,x)+1
422//  matrix m=mpNew(fn+gn-2,fn+gn-2);
423//  if(m==NULL)
424//  {
425//    return NULL;
426//  }
427//
428//  // enter the coeffs into m:
429//  int j;
430//  for(i=0;i<gn-1;i++)
431//  {
432//    for(j=0;j<fn;j++)
433//    {
434//      MATELEM(m,i+1,fn-j+i)=pCopy(fi->m[j]);
435//    }
436//  }
437//  for(i=0;i<fn-1;i++)
438//  {
439//    for(j=0;j<gn;j++)
440//    {
441//      MATELEM(m,gn+i,gn-j+i)=pCopy(gi->m[j]);
442//    }
443//  }
444//
445//  poly r=mpDet(m);
446//
447//  idDelete(&fi);
448//  idDelete(&gi);
449//  idDelete((ideal *)&m);
450//  return r;
451//}
452
453BOOLEAN singclap_extgcd ( poly f, poly g, poly &res, poly &pa, poly &pb , const ring r)
454{
455  // for now there is only the possibility to handle univariate
456  // polynomials over
457  // Q and Fp ...
458  res=NULL;pa=NULL;pb=NULL;
459  On(SW_SYMMETRIC_FF);
460  if ( rField_is_Q(r) || rField_is_Zp(r) )
461  {
462    setCharacteristic( rChar(r) );
463    CanonicalForm F( convSingPFactoryP( f,r ) ), G( convSingPFactoryP( g,r) );
464    CanonicalForm FpG=F+G;
465    if (!(FpG.isUnivariate()|| FpG.inCoeffDomain()))
466    //if (!F.isUnivariate() || !G.isUnivariate() || F.mvar()!=G.mvar())
467    {
468      Off(SW_RATIONAL);
469      WerrorS("not univariate");
470      return TRUE;
471    }
472    CanonicalForm Fa,Gb;
473    On(SW_RATIONAL);
474    res=convFactoryPSingP( extgcd( F, G, Fa, Gb ),r );
475    pa=convFactoryPSingP(Fa,r);
476    pb=convFactoryPSingP(Gb,r);
477    Off(SW_RATIONAL);
478  }
479  // and over Q(a) / Fp(a)
480  else if ( r->cf->extRing!=NULL )
481  {
482    if (rField_is_Q_a(r)) setCharacteristic( 0 );
483    else                 setCharacteristic( rChar(r) );
484    CanonicalForm Fa,Gb;
485    if (r->cf->extRing->qideal!=NULL)
486    {
487      CanonicalForm mipo=convSingPFactoryP(r->cf->extRing->qideal->m[0],
488                                           r->cf->extRing);
489      Variable a=rootOf(mipo);
490      CanonicalForm F( convSingAPFactoryAP( f,a,r ) ),
491                    G( convSingAPFactoryAP( g,a,r ) );
492      CanonicalForm FpG=F+G;
493      if (!(FpG.isUnivariate()|| FpG.inCoeffDomain()))
494      //if (!F.isUnivariate() || !G.isUnivariate() || F.mvar()!=G.mvar())
495      {
496        WerrorS("not univariate");
497        return TRUE;
498      }
499      res= convFactoryAPSingAP( extgcd( F, G, Fa, Gb ),r );
500      pa=convFactoryAPSingAP(Fa,r);
501      pb=convFactoryAPSingAP(Gb,r);
502    }
503    else
504    {
505      CanonicalForm F( convSingTrPFactoryP( f, r ) ), G( convSingTrPFactoryP( g, r ) );
506      CanonicalForm FpG=F+G;
507      if (!(FpG.isUnivariate()|| FpG.inCoeffDomain()))
508      //if (!F.isUnivariate() || !G.isUnivariate() || F.mvar()!=G.mvar())
509      {
510        Off(SW_RATIONAL);
511        WerrorS("not univariate");
512        return TRUE;
513      }
514      res= convFactoryPSingTrP( extgcd( F, G, Fa, Gb ), r );
515      pa=convFactoryPSingTrP(Fa, r);
516      pb=convFactoryPSingTrP(Gb, r);
517    }
518    Off(SW_RATIONAL);
519  }
520  else
521  {
522    WerrorS( feNotImplemented );
523    return TRUE;
524  }
525#ifndef SING_NDEBUG
526  // checking the result of extgcd:
527  poly dummy;
528  dummy=p_Sub(p_Add_q(pp_Mult_qq(f,pa,r),pp_Mult_qq(g,pb,r),r),p_Copy(res,r),r);
529  if (dummy!=NULL)
530  {
531    PrintS("extgcd( ");p_Write(f,r);p_Write0(g,r);PrintS(" )\n");
532    PrintS("extgcd( ");p_Write(f,r);p_Write0(g,r);PrintS(" )\n");
533    p_Delete(&dummy,r);
534  }
535#endif
536  return FALSE;
537}
538
539poly singclap_pdivide ( poly f, poly g, const ring r )
540{
541  poly res=NULL;
542  On(SW_RATIONAL);
543  if (rField_is_Zp(r) || rField_is_Q(r))
544  {
545    setCharacteristic( rChar(r) );
546    CanonicalForm F( convSingPFactoryP( f,r ) ), G( convSingPFactoryP( g,r ) );
547    res = convFactoryPSingP( F / G,r );
548  }
549  else if (r->cf->extRing!=NULL)
550  {
551    if (rField_is_Q_a(r)) setCharacteristic( 0 );
552    else               setCharacteristic( rChar(r) );
553    if (r->cf->extRing->qideal!=NULL)
554    {
555      CanonicalForm mipo=convSingPFactoryP(r->cf->extRing->qideal->m[0],
556                                                 r->cf->extRing);
557      Variable a=rootOf(mipo);
558      CanonicalForm F( convSingAPFactoryAP( f,a,r ) ),
559                    G( convSingAPFactoryAP( g,a,r ) );
560      res= convFactoryAPSingAP(  F / G, r  );
561    }
562    else
563    {
564      CanonicalForm F( convSingTrPFactoryP( f,r ) ), G( convSingTrPFactoryP( g,r ) );
565      res= convFactoryPSingTrP(  F / G,);
566    }
567  }
568#if 0 // not yet working
569  else if (rField_is_GF())
570  {
571    //Print("GF(%d^%d)\n",nfCharP,nfMinPoly[0]);
572    setCharacteristic( nfCharP,nfMinPoly[0], currRing->parameter[0][0] );
573    CanonicalForm F( convSingGFFactoryGF( f ) ), G( convSingGFFactoryGF( g ) );
574    res = convFactoryGFSingGF( F / G );
575  }
576#endif
577  else
578    WerrorS( feNotImplemented );
579  Off(SW_RATIONAL);
580  return res;
581}
582
583void singclap_divide_content ( poly f, const ring r )
584{
585  if ( f==NULL )
586  {
587    return;
588  }
589  else  if ( pNext( f ) == NULL )
590  {
591    p_SetCoeff( f, n_Init( 1, r->cf ), r );
592    return;
593  }
594  else
595  {
596    if ( rField_is_Q_a(r) )
597      setCharacteristic( 0 );
598    else  if ( rField_is_Zp_a(r) )
599      setCharacteristic( -rChar(r) );
600    else
601      return; /* not implemented*/
602
603    CFList L;
604    CanonicalForm g, h;
605    poly p = pNext(f);
606
607    // first attemp: find 2 smallest g:
608
609    number g1=pGetCoeff(f);
610    number g2=pGetCoeff(p); // p==pNext(f);
611    pIter(p);
612    int sz1=n_Size(g1, r->cf);
613    int sz2=n_Size(g2, r->cf);
614    if (sz1>sz2)
615    {
616      number gg=g1;
617      g1=g2; g2=gg;
618      int sz=sz1;
619      sz1=sz2; sz2=sz;
620    }
621    while (p!=NULL)
622    {
623      int n_sz=n_Size(pGetCoeff(p),r->cf);
624      if (n_sz<sz1)
625      {
626        sz2=sz1;
627        g2=g1;
628        g1=pGetCoeff(p);
629        sz1=n_sz;
630        if (sz1<=3) break;
631      }
632      else if(n_sz<sz2)
633      {
634        sz2=n_sz;
635        g2=pGetCoeff(p);
636        sz2=n_sz;
637      }
638      pIter(p);
639    }
640    g = convSingPFactoryP( NUM(((fraction)g1)), r->cf->extRing );
641    g = gcd( g, convSingPFactoryP( NUM(((fraction)g2)) , r->cf->extRing));
642
643    // second run: gcd's
644
645    p = f;
646    while ( (p != NULL) && (g != 1)  && ( g != 0))
647    {
648      h = convSingPFactoryP( NUM(((fraction)pGetCoeff(p))), r->cf->extRing );
649      pIter( p );
650
651      g = gcd( g, h );
652
653      L.append( h );
654    }
655    if (( g == 1 ) || (g == 0))
656    {
657      // pTest(f);
658      return;
659    }
660    else
661    {
662      CFListIterator i;
663      for ( i = L, p = f; i.hasItem(); i++, p=pNext(p) )
664      {
665        fraction c=(fraction)pGetCoeff(p);
666        p_Delete(&(NUM(c)),r->cf->extRing); // 2nd arg used to be nacRing
667        NUM(c)=convFactoryPSingP( i.getItem() / g, r->cf->extRing );
668        //nTest((number)c);
669        //#ifdef LDEBUG
670        //number cn=(number)c;
671        //StringSetS(""); nWrite(nt); StringAppend(" ==> ");
672        //nWrite(cn);PrintS(StringEndS("\n")); // NOTE/TODO: use StringAppendS("\n"); omFree(s);
673        //#endif
674      }
675    }
676    // pTest(f);
677  }
678}
679
680BOOLEAN count_Factors(ideal I, intvec *v,int j, poly &f, poly fac, const ring r)
681{
682  p_Test(f,r);
683  p_Test(fac,r);
684  int e=0;
685  if (!p_IsConstantPoly(fac,r))
686  {
687#ifdef FACTORIZE2_DEBUG
688    printf("start count_Factors(%d), Fdeg=%d, factor deg=%d\n",j,p_Totaldegree(f,r),p_Totaldegree(fac,r));
689    p_wrp(fac,r);PrintLn();
690#endif
691    On(SW_RATIONAL);
692    CanonicalForm F, FAC,Q,R;
693    Variable a;
694    if (rField_is_Zp(r) || rField_is_Q(r))
695    {
696      F=convSingPFactoryP( f,r );
697      FAC=convSingPFactoryP( fac,r );
698    }
699    else if (r->cf->extRing!=NULL)
700    {
701      if (r->cf->extRing->qideal!=NULL)
702      {
703        CanonicalForm mipo=convSingPFactoryP(r->cf->extRing->qideal->m[0],
704                                    r->cf->extRing);
705        a=rootOf(mipo);
706        F=convSingAPFactoryAP( f,a,r );
707        FAC=convSingAPFactoryAP( fac,a,r );
708      }
709      else
710      {
711        F=convSingTrPFactoryP( f,r );
712        FAC=convSingTrPFactoryP( fac,r );
713      }
714    }
715    else
716      WerrorS( feNotImplemented );
717
718    poly q;
719    loop
720    {
721      Q=F;
722      Q/=FAC;
723      R=Q;
724      R*=FAC;
725      R-=F;
726      if (R.isZero())
727      {
728        if (rField_is_Zp(r) || rField_is_Q(r))
729        {
730          q = convFactoryPSingP( Q,r );
731        }
732        else if (r->cf->extRing!=NULL)
733        {
734          if (r->cf->extRing->qideal!=NULL)
735          {
736            q= convFactoryAPSingAP( Q,r );
737          }
738          else
739          {
740            q= convFactoryPSingTrP( Q,r );
741          }
742        }
743        e++; p_Delete(&f,r); f=q; q=NULL; F=Q;
744      }
745      else
746      {
747        break;
748      }
749    }
750    if (e==0)
751    {
752      Off(SW_RATIONAL);
753      return FALSE;
754    }
755  }
756  else e=1;
757  I->m[j]=fac;
758  if (v!=NULL) (*v)[j]=e;
759  Off(SW_RATIONAL);
760  return TRUE;
761}
762
763int singclap_factorize_retry;
764
765ideal singclap_factorize ( poly f, intvec ** v , int with_exps, const ring r)
766/* destroys f, sets *v */
767{
768  p_Test(f,r);
769#ifdef FACTORIZE2_DEBUG
770  printf("singclap_factorize, degree %ld\n",p_Totaldegree(f,r));
771#endif
772  // with_exps: 3,1 return only true factors, no exponents
773  //            2 return true factors and exponents
774  //            0 return coeff, factors and exponents
775  BOOLEAN save_errorreported=errorreported;
776
777  ideal res=NULL;
778
779  // handle factorize(0) =========================================
780  if (f==NULL)
781  {
782    res=idInit(1,1);
783    if (with_exps!=1)
784    {
785      (*v)=new intvec(1);
786      (**v)[0]=1;
787    }
788    return res;
789  }
790  // handle factorize(mon) =========================================
791  if (pNext(f)==NULL)
792  {
793    int i=0;
794    int n=0;
795    int e;
796    for(i=rVar(r);i>0;i--) if(p_GetExp(f,i,r)!=0) n++;
797    if (with_exps==0) n++; // with coeff
798    res=idInit(si_max(n,1),1);
799    switch(with_exps)
800    {
801      case 0: // with coef & exp.
802        res->m[0]=p_NSet(n_Copy(pGetCoeff(f),r->cf),r);
803        // no break
804      case 2: // with exp.
805        (*v)=new intvec(si_max(1,n));
806        (**v)[0]=1;
807        // no break
808      case 1: ;
809#ifdef TEST
810      default: ;
811#endif
812    }
813    if (n==0)
814    {
815      res->m[0]=p_One(r);
816      // (**v)[0]=1; is already done
817    }
818    else
819    {
820      for(i=rVar(r);i>0;i--)
821      {
822        e=p_GetExp(f,i,r);
823        if(e!=0)
824        {
825          n--;
826          poly p=p_One(r);
827          p_SetExp(p,i,1,r);
828          p_Setm(p,r);
829          res->m[n]=p;
830          if (with_exps!=1) (**v)[n]=e;
831        }
832      }
833    }
834    p_Delete(&f,r);
835    return res;
836  }
837  //PrintS("S:");p_Write(f,r);PrintLn();
838  // use factory/libfac in general ==============================
839  Off(SW_RATIONAL);
840  On(SW_SYMMETRIC_FF);
841  CFFList L;
842  number N=NULL;
843  number NN=NULL;
844  number old_lead_coeff=n_Copy(pGetCoeff(f), r->cf);
845
846  if (!rField_is_Zp(r) && !rField_is_Zp_a(r)) /* Q, Q(a) */
847  {
848    //if (f!=NULL) // already tested at start of routine
849    {
850      number n0=n_Copy(pGetCoeff(f),r->cf);
851      if (with_exps==0)
852        N=n_Copy(n0,r->cf);
853      p_Cleardenom(f, r);
854      //after here f should not have a denominator!!
855      //PrintS("S:");p_Write(f,r);PrintLn();
856      NN=n_Div(n0,pGetCoeff(f),r->cf);
857      n_Delete(&n0,r->cf);
858      if (with_exps==0)
859      {
860        n_Delete(&N,r->cf);
861        N=n_Copy(NN,r->cf);
862      }
863    }
864  }
865  else if (rField_is_Zp_a(r))
866  {
867    //if (f!=NULL) // already tested at start of routine
868    if (singclap_factorize_retry==0)
869    {
870      number n0=n_Copy(pGetCoeff(f),r->cf);
871      if (with_exps==0)
872        N=n_Copy(n0,r->cf);
873      p_Norm(f,r);
874      p_Cleardenom(f, r);
875      NN=n_Div(n0,pGetCoeff(f),r->cf);
876      n_Delete(&n0,r->cf);
877      if (with_exps==0)
878      {
879        n_Delete(&N,r->cf);
880        N=n_Copy(NN,r->cf);
881      }
882    }
883  }
884  if (rField_is_Q(r) || rField_is_Zp(r))
885  {
886    setCharacteristic( rChar(r) );
887    CanonicalForm F( convSingPFactoryP( f,r ) );
888    L = factorize( F );
889  }
890  // and over Q(a) / Fp(a)
891  else if (r->cf->extRing!=NULL)
892  {
893    if (rField_is_Q_a (r)) setCharacteristic (0);
894    else                   setCharacteristic( rChar(r) );
895    if (r->cf->extRing->qideal!=NULL)
896    {
897      CanonicalForm mipo=convSingPFactoryP(r->cf->extRing->qideal->m[0],
898                                           r->cf->extRing);
899      Variable a=rootOf(mipo);
900      CanonicalForm F( convSingAPFactoryAP( f, a, r ) );
901      if (rField_is_Zp_a(r))
902      {
903        L = factorize( F, a );
904      }
905      else
906      {
907        //  over Q(a)
908        L= factorize (F, a);
909      }
910    }
911    else
912    {
913      CanonicalForm F( convSingTrPFactoryP( f,r ) );
914      L = factorize( F );
915    }
916  }
917  else
918  {
919    goto notImpl;
920  }
921  {
922    poly ff=p_Copy(f,r); // a copy for the retry stuff
923    // the first factor should be a constant
924    if ( ! L.getFirst().factor().inCoeffDomain() )
925      L.insert(CFFactor(1,1));
926    // convert into ideal
927    int n = L.length();
928    if (n==0) n=1;
929    CFFListIterator J=L;
930    int j=0;
931    if (with_exps!=1)
932    {
933      if ((with_exps==2)&&(n>1))
934      {
935        n--;
936        J++;
937      }
938      *v = new intvec( n );
939    }
940    res = idInit( n ,1);
941    for ( ; J.hasItem(); J++, j++ )
942    {
943      if (with_exps!=1) (**v)[j] = J.getItem().exp();
944      if (rField_is_Zp(r) || rField_is_Q(r))           /* Q, Fp */
945      {
946        //count_Factors(res,*v,f, j, convFactoryPSingP( J.getItem().factor() );
947        res->m[j] = convFactoryPSingP( J.getItem().factor(),r );
948      }
949#if 0
950      else if (rField_is_GF())
951        res->m[j] = convFactoryGFSingGF( J.getItem().factor() );
952#endif
953      else if (r->cf->extRing!=NULL)     /* Q(a), Fp(a) */
954      {
955#ifndef SING_NDEBUG
956        intvec *w=NULL;
957        if (v!=NULL) w=*v;
958#endif
959        if (r->cf->extRing->qideal==NULL)
960        {
961#ifdef SING_NDEBUG
962          res->m[j]= convFactoryPSingTrP( J.getItem().factor(),r );
963#else
964          if(!count_Factors(res,w,j,ff,convFactoryPSingTrP( J.getItem().factor(),r ),r))
965          {
966            if (w!=NULL)
967              (*w)[j]=1;
968            res->m[j]=p_One(r);
969          }
970#endif
971        }
972        else
973        {
974#ifdef SING_NDEBUG
975          res->m[j]= convFactoryAPSingAP( J.getItem().factor(),r );
976#else
977          if (!count_Factors(res,w,j,ff,convFactoryAPSingAP( J.getItem().factor(),r ),r))
978          {
979            if (w!=NULL)
980              (*w)[j]=1;
981            res->m[j]=p_One(r);
982          }
983#endif
984        }
985      }
986    }
987#ifndef SING_NDEBUG
988    if ((r->cf->extRing!=NULL) && (!p_IsConstantPoly(ff,r)))
989    {
990      singclap_factorize_retry++;
991      if (singclap_factorize_retry<3)
992      {
993        int jj;
994#ifdef FACTORIZE2_DEBUG
995        printf("factorize_retry\n");
996#endif
997        intvec *ww=NULL;
998        id_Test(res,r);
999        ideal h=singclap_factorize ( ff, &ww , with_exps, r );
1000        id_Test(h,r);
1001        int l=(*v)->length();
1002        (*v)->resize(l+ww->length());
1003        for(jj=0;jj<ww->length();jj++)
1004          (**v)[jj+l]=(*ww)[jj];
1005        delete ww;
1006        ideal hh=idInit(IDELEMS(res)+IDELEMS(h),1);
1007        for(jj=IDELEMS(res)-1;jj>=0;jj--)
1008        {
1009          hh->m[jj]=res->m[jj];
1010          res->m[jj]=NULL;
1011        }
1012        for(jj=IDELEMS(h)-1;jj>=0;jj--)
1013        {
1014          hh->m[jj+IDELEMS(res)]=h->m[jj];
1015          h->m[jj]=NULL;
1016        }
1017        id_Delete(&res,r);
1018        id_Delete(&h,r);
1019        res=hh;
1020        id_Test(res,r);
1021        ff=NULL;
1022      }
1023      else
1024      {
1025        WarnS("problem with factorize");
1026#if 0
1027        pWrite(ff);
1028        idShow(res);
1029#endif
1030        id_Delete(&res,r);
1031        res=idInit(2,1);
1032        res->m[0]=p_One(r);
1033        res->m[1]=ff; ff=NULL;
1034      }
1035    }
1036#endif
1037    p_Delete(&ff,r);
1038    if (N!=NULL)
1039    {
1040      p_Mult_nn(res->m[0],N,r);
1041      n_Delete(&N,r->cf);
1042      N=NULL;
1043    }
1044    // delete constants
1045    if (res!=NULL)
1046    {
1047      int i=IDELEMS(res)-1;
1048      int j=0;
1049      for(;i>=0;i--)
1050      {
1051        if ((res->m[i]!=NULL)
1052        && (pNext(res->m[i])==NULL)
1053        && (p_IsConstant(res->m[i],r)))
1054        {
1055          if (with_exps!=0)
1056          {
1057            p_Delete(&(res->m[i]),r);
1058            if ((v!=NULL) && ((*v)!=NULL))
1059              (**v)[i]=0;
1060            j++;
1061          }
1062          else if (i!=0)
1063          {
1064            while ((v!=NULL) && ((*v)!=NULL) && ((**v)[i]>1))
1065            {
1066              res->m[0]=p_Mult_q(res->m[0],p_Copy(res->m[i],r),r);
1067              (**v)[i]--;
1068            }
1069            res->m[0]=p_Mult_q(res->m[0],res->m[i],r);
1070            res->m[i]=NULL;
1071            if ((v!=NULL) && ((*v)!=NULL))
1072              (**v)[i]=1;
1073            j++;
1074          }
1075        }
1076      }
1077      if (j>0)
1078      {
1079        idSkipZeroes(res);
1080        if ((v!=NULL) && ((*v)!=NULL))
1081        {
1082          intvec *w=*v;
1083          int len=IDELEMS(res);
1084          *v = new intvec( len );
1085          for (i=0,j=0;i<si_min(w->length(),len);i++)
1086          {
1087            if((*w)[i]!=0)
1088            {
1089              (**v)[j]=(*w)[i]; j++;
1090            }
1091          }
1092          delete w;
1093        }
1094      }
1095      if (res->m[0]==NULL)
1096      {
1097        res->m[0]=p_One(r);
1098      }
1099    }
1100  }
1101  if (rField_is_Q_a(r) && (r->cf->extRing->qideal!=NULL))
1102  {
1103    int i=IDELEMS(res)-1;
1104    int stop=1;
1105    if (with_exps!=0) stop=0;
1106    for(;i>=stop;i--)
1107    {
1108      p_Norm(res->m[i],r);
1109    }
1110    if (with_exps==0) p_SetCoeff(res->m[0],old_lead_coeff,r);
1111    else n_Delete(&old_lead_coeff,r->cf);
1112  }
1113  else
1114    n_Delete(&old_lead_coeff,r->cf);
1115  errorreported=save_errorreported;
1116notImpl:
1117  if (res==NULL)
1118    WerrorS( feNotImplemented );
1119  if (NN!=NULL)
1120  {
1121    n_Delete(&NN,r->cf);
1122  }
1123  if (N!=NULL)
1124  {
1125    n_Delete(&N,r->cf);
1126  }
1127  if (f!=NULL) p_Delete(&f,r);
1128  //PrintS("......S\n");
1129  return res;
1130}
1131
1132ideal singclap_sqrfree ( poly f, intvec ** v , int with_exps, const ring r)
1133{
1134  p_Test(f,r);
1135#ifdef FACTORIZE2_DEBUG
1136  printf("singclap_sqrfree, degree %d\n",pTotaldegree(f));
1137#endif
1138  // with_exps: 3,1 return only true factors, no exponents
1139  //            2 return true factors and exponents
1140  //            0 return coeff, factors and exponents
1141  BOOLEAN save_errorreported=errorreported;
1142
1143  ideal res=NULL;
1144
1145  // handle factorize(0) =========================================
1146  if (f==NULL)
1147  {
1148    res=idInit(1,1);
1149    if (with_exps!=1 && with_exps!=3)
1150    {
1151      (*v)=new intvec(1);
1152      (**v)[0]=1;
1153    }
1154    return res;
1155  }
1156  // handle factorize(mon) =========================================
1157  if (pNext(f)==NULL)
1158  {
1159    int i=0;
1160    int n=0;
1161    int e;
1162    for(i=rVar(r);i>0;i--) if(p_GetExp(f,i,r)!=0) n++;
1163    if (with_exps==0 || with_exps==3) n++; // with coeff
1164    res=idInit(si_max(n,1),1);
1165    switch(with_exps)
1166    {
1167      case 0: // with coef & exp.
1168        res->m[0]=p_NSet(n_Copy(pGetCoeff(f),r->cf),r);
1169        // no break
1170      case 3: // with coef & exp.
1171        res->m[0]=p_NSet(n_Copy(pGetCoeff(f),r->cf),r);
1172        // no break
1173      case 2: // with exp.
1174        (*v)=new intvec(si_max(1,n));
1175        (**v)[0]=1;
1176        // no break
1177      case 1: ;
1178      #ifdef TEST
1179      default: ;
1180      #endif
1181    }
1182    res->m[0]=p_NSet(n_Copy(pGetCoeff(f),r->cf),r);
1183    if (n==0)
1184    {
1185      res->m[0]=p_One(r);
1186      // (**v)[0]=1; is already done
1187    }
1188    else
1189    {
1190      for(i=rVar(r);i>0;i--)
1191      {
1192        e=p_GetExp(f,i,r);
1193        if(e!=0)
1194        {
1195          n--;
1196          poly p=p_One(r);
1197          p_SetExp(p,i,1,r);
1198          p_Setm(p,r);
1199          res->m[n]=p;
1200          if (with_exps!=1) (**v)[n]=e;
1201        }
1202      }
1203    }
1204    p_Delete(&f,r);
1205    return res;
1206  }
1207  //PrintS("S:");pWrite(f);PrintLn();
1208  // use factory/libfac in general ==============================
1209  Off(SW_RATIONAL);
1210  On(SW_SYMMETRIC_FF);
1211  CFFList L;
1212  number N=NULL;
1213  number NN=NULL;
1214  number old_lead_coeff=n_Copy(pGetCoeff(f), r->cf);
1215
1216  if (!rField_is_Zp(r) && !rField_is_Zp_a(r)) /* Q, Q(a) */
1217  {
1218    //if (f!=NULL) // already tested at start of routine
1219    number n0=n_Copy(pGetCoeff(f),r->cf);
1220    if (with_exps==0 || with_exps==3)
1221      N=n_Copy(n0,r->cf);
1222    p_Cleardenom(f, r);
1223    //after here f should not have a denominator!!
1224    //PrintS("S:");p_Write(f,r);PrintLn();
1225    NN=n_Div(n0,pGetCoeff(f),r->cf);
1226    n_Delete(&n0,r->cf);
1227    if (with_exps==0 || with_exps==3)
1228    {
1229      n_Delete(&N,r->cf);
1230      N=n_Copy(NN,r->cf);
1231    }
1232  }
1233  else if (rField_is_Zp_a(r))
1234  {
1235    //if (f!=NULL) // already tested at start of routine
1236    if (singclap_factorize_retry==0)
1237    {
1238      number n0=n_Copy(pGetCoeff(f),r->cf);
1239      if (with_exps==0 || with_exps==3)
1240        N=n_Copy(n0,r->cf);
1241      p_Norm(f,r);
1242      p_Cleardenom(f, r);
1243      NN=n_Div(n0,pGetCoeff(f),r->cf);
1244      n_Delete(&n0,r->cf);
1245      if (with_exps==0 || with_exps==3)
1246      {
1247        n_Delete(&N,r->cf);
1248        N=n_Copy(NN,r->cf);
1249      }
1250    }
1251  }
1252  if (rField_is_Q(r) || rField_is_Zp(r))
1253  {
1254    setCharacteristic( rChar(r) );
1255    CanonicalForm F( convSingPFactoryP( f,r ) );
1256    L = sqrFree( F );
1257  }
1258  else if (r->cf->extRing!=NULL)
1259  {
1260    if (rField_is_Q_a (r)) setCharacteristic (0);
1261    else                   setCharacteristic( rChar(r) );
1262    if (r->cf->extRing->qideal!=NULL)
1263    {
1264      CanonicalForm mipo=convSingPFactoryP(r->cf->extRing->qideal->m[0],
1265                                           r->cf->extRing);
1266      Variable a=rootOf(mipo);
1267      CanonicalForm F( convSingAPFactoryAP( f, a, r ) );
1268      L= sqrFree (F);
1269    }
1270    else
1271    {
1272      CanonicalForm F( convSingTrPFactoryP( f,r ) );
1273      L = sqrFree( F );
1274    }
1275  }
1276  #if 0
1277  else if (rField_is_GF())
1278  {
1279    int c=rChar(r);
1280    setCharacteristic( c, primepower(c) );
1281    CanonicalForm F( convSingGFFactoryGF( f ) );
1282    if (F.isUnivariate())
1283    {
1284      L = factorize( F );
1285    }
1286    else
1287    {
1288      goto notImpl;
1289    }
1290  }
1291  #endif
1292  else
1293  {
1294    goto notImpl;
1295  }
1296  {
1297    // convert into ideal
1298    int n = L.length();
1299    if (n==0) n=1;
1300    CFFListIterator J=L;
1301    int j=0;
1302    if (with_exps!=1)
1303    {
1304      if ((with_exps==2)&&(n>1))
1305      {
1306        n--;
1307        J++;
1308      }
1309      *v = new intvec( n );
1310    }
1311    else if (L.getFirst().factor().inCoeffDomain() && with_exps!=3)
1312    {
1313      n--;
1314      J++;
1315    }
1316    res = idInit( n ,1);
1317    for ( ; J.hasItem(); J++, j++ )
1318    {
1319      if (with_exps!=1 && with_exps!=3) (**v)[j] = J.getItem().exp();
1320      if (rField_is_Zp(r) || rField_is_Q(r))
1321        res->m[j] = convFactoryPSingP( J.getItem().factor(),r );
1322      else if (r->cf->extRing!=NULL)     /* Q(a), Fp(a) */
1323      {
1324        if (r->cf->extRing->qideal==NULL)
1325          res->m[j]=convFactoryPSingTrP( J.getItem().factor(),r );
1326        else
1327          res->m[j]=convFactoryAPSingAP( J.getItem().factor(),r );
1328      }
1329    }
1330    if (res->m[0]==NULL)
1331    {
1332      res->m[0]=p_One(r);
1333    }
1334    if (N!=NULL)
1335    {
1336      p_Mult_nn(res->m[0],N,r);
1337      n_Delete(&N,r->cf);
1338      N=NULL;
1339    }
1340  }
1341  if (rField_is_Q_a(r) && (r->cf->extRing->qideal!=NULL))
1342  {
1343    int i=IDELEMS(res)-1;
1344    int stop=1;
1345    if (with_exps!=0 || with_exps==3) stop=0;
1346    for(;i>=stop;i--)
1347    {
1348      p_Norm(res->m[i],r);
1349    }
1350    if (with_exps==0 || with_exps==3) p_SetCoeff(res->m[0],old_lead_coeff,r);
1351    else n_Delete(&old_lead_coeff,r->cf);
1352  }
1353  else
1354    n_Delete(&old_lead_coeff,r->cf);
1355  p_Delete(&f,r);
1356  errorreported=save_errorreported;
1357notImpl:
1358  if (res==NULL)
1359    WerrorS( feNotImplemented );
1360  if (NN!=NULL)
1361  {
1362    n_Delete(&NN,r->cf);
1363  }
1364  if (N!=NULL)
1365  {
1366    n_Delete(&N,r->cf);
1367  }
1368  return res;
1369}
1370
1371matrix singclap_irrCharSeries ( ideal I, const ring r)
1372{
1373  if (idIs0(I)) return mpNew(1,1);
1374
1375  // for now there is only the possibility to handle polynomials over
1376  // Q and Fp ...
1377  matrix res=NULL;
1378  int i;
1379  Off(SW_RATIONAL);
1380  On(SW_SYMMETRIC_FF);
1381  CFList L;
1382  ListCFList LL;
1383  if (rField_is_Q(r) || rField_is_Zp(r))
1384  {
1385    setCharacteristic( rChar(r) );
1386    for(i=0;i<IDELEMS(I);i++)
1387    {
1388      poly p=I->m[i];
1389      if (p!=NULL)
1390      {
1391        p=p_Copy(p,r);
1392        p_Cleardenom(p, r);
1393        L.append(convSingPFactoryP(p,r));
1394      }
1395    }
1396  }
1397  // and over Q(a) / Fp(a)
1398  else if (nCoeff_is_transExt (r->cf))
1399  {
1400    setCharacteristic( rChar(r) );
1401    for(i=0;i<IDELEMS(I);i++)
1402    {
1403      poly p=I->m[i];
1404      if (p!=NULL)
1405      {
1406        p=p_Copy(p,r);
1407        p_Cleardenom(p, r);
1408        L.append(convSingTrPFactoryP(p,r));
1409      }
1410    }
1411  }
1412  else
1413  {
1414    WerrorS( feNotImplemented );
1415    return res;
1416  }
1417
1418  // a very bad work-around --- FIX IT in libfac
1419  // should be fixed as of 2001/6/27
1420  int tries=0;
1421  int m,n;
1422  ListIterator<CFList> LLi;
1423  loop
1424  {
1425    LL=IrrCharSeries(L);
1426    m= LL.length(); // Anzahl Zeilen
1427    n=0;
1428    for ( LLi = LL; LLi.hasItem(); LLi++ )
1429    {
1430      n = si_max(LLi.getItem().length(),n);
1431    }
1432    if ((m!=0) && (n!=0)) break;
1433    tries++;
1434    if (tries>=5) break;
1435  }
1436  if ((m==0) || (n==0))
1437  {
1438    Warn("char_series returns %d x %d matrix from %d input polys (%d)",
1439      m,n,IDELEMS(I)+1,LL.length());
1440    iiWriteMatrix((matrix)I,"I",2,r,0);
1441    m=si_max(m,1);
1442    n=si_max(n,1);
1443  }
1444  res=mpNew(m,n);
1445  CFListIterator Li;
1446  for ( m=1, LLi = LL; LLi.hasItem(); LLi++, m++ )
1447  {
1448    for (n=1, Li = LLi.getItem(); Li.hasItem(); Li++, n++)
1449    {
1450      if (rField_is_Q(r) || rField_is_Zp(r))
1451        MATELEM(res,m,n)=convFactoryPSingP(Li.getItem(),r);
1452      else
1453        MATELEM(res,m,n)=convFactoryPSingTrP(Li.getItem(),r);
1454    }
1455  }
1456  Off(SW_RATIONAL);
1457  return res;
1458}
1459
1460char* singclap_neworder ( ideal I, const ring r)
1461{
1462  int i;
1463  Off(SW_RATIONAL);
1464  On(SW_SYMMETRIC_FF);
1465  CFList L;
1466  if (rField_is_Q(r) || rField_is_Zp(r))
1467  {
1468    setCharacteristic( rChar(r) );
1469    for(i=0;i<IDELEMS(I);i++)
1470    {
1471      poly p=I->m[i];
1472      if (p!=NULL)
1473      {
1474        p=p_Copy(p,r);
1475        p_Cleardenom(p, r);
1476        L.append(convSingPFactoryP(p,r));
1477      }
1478    }
1479  }
1480  // and over Q(a) / Fp(a)
1481  else if (nCoeff_is_transExt (r->cf))
1482  {
1483    setCharacteristic( rChar(r) );
1484    for(i=0;i<IDELEMS(I);i++)
1485    {
1486      poly p=I->m[i];
1487      if (p!=NULL)
1488      {
1489        p=p_Copy(p,r);
1490        p_Cleardenom(p, r);
1491        L.append(convSingTrPFactoryP(p,r));
1492      }
1493    }
1494  }
1495  else
1496  {
1497    WerrorS( feNotImplemented );
1498    return NULL;
1499  }
1500
1501  List<int> IL=neworderint(L);
1502  ListIterator<int> Li;
1503  StringSetS("");
1504  Li = IL;
1505  int offs=rPar(r);
1506  int* mark=(int*)omAlloc0((rVar(r)+offs)*sizeof(int));
1507  int cnt=rVar(r)+offs;
1508  loop
1509  {
1510    if(! Li.hasItem()) break;
1511    BOOLEAN done=TRUE;
1512    i=Li.getItem()-1;
1513    mark[i]=1;
1514    if (i<offs)
1515    {
1516      done=FALSE;
1517      //StringAppendS(r->parameter[i]);
1518    }
1519    else
1520    {
1521      StringAppendS(r->names[i-offs]);
1522    }
1523    Li++;
1524    cnt--;
1525    if(cnt==0) break;
1526    if (done) StringAppendS(",");
1527  }
1528  for(i=0;i<rVar(r)+offs;i++)
1529  {
1530    BOOLEAN done=TRUE;
1531    if(mark[i]==0)
1532    {
1533      if (i<offs)
1534      {
1535        done=FALSE;
1536        //StringAppendS(r->parameter[i]);
1537      }
1538      else
1539      {
1540        StringAppendS(r->names[i-offs]);
1541      }
1542      cnt--;
1543      if(cnt==0) break;
1544      if (done) StringAppendS(",");
1545    }
1546  }
1547  char * s=StringEndS();
1548  if (s[strlen(s)-1]==',') s[strlen(s)-1]='\0';
1549  return s;
1550}
1551
1552poly singclap_det( const matrix m, const ring s )
1553{
1554  int r=m->rows();
1555  if (r!=m->cols())
1556  {
1557    Werror("det of %d x %d matrix",r,m->cols());
1558    return NULL;
1559  }
1560  poly res=NULL;
1561  CFMatrix M(r,r);
1562  int i,j;
1563  for(i=r;i>0;i--)
1564  {
1565    for(j=r;j>0;j--)
1566    {
1567      M(i,j)=convSingPFactoryP(MATELEM(m,i,j),s);
1568    }
1569  }
1570  res= convFactoryPSingP( determinant(M,r),s ) ;
1571  Off(SW_RATIONAL);
1572  return res;
1573}
1574
1575int singclap_det_i( intvec * m, const ring /*r*/)
1576{
1577//  assume( r == currRing ); // Anything else is not guaranted to work!
1578   
1579  setCharacteristic( 0 ); // ?
1580  CFMatrix M(m->rows(),m->cols());
1581  int i,j;
1582  for(i=m->rows();i>0;i--)
1583  {
1584    for(j=m->cols();j>0;j--)
1585    {
1586      M(i,j)=IMATELEM(*m,i,j);
1587    }
1588  }
1589  int res= convFactoryISingI( determinant(M,m->rows()) ) ;
1590  return res;
1591}
1592
1593number singclap_det_bi( bigintmat * m, const coeffs cf)
1594{
1595  assume(m->basecoeffs()==cf);
1596  CFMatrix M(m->rows(),m->cols());
1597  int i,j;
1598  BOOLEAN setchar=TRUE;
1599  for(i=m->rows();i>0;i--)
1600  {
1601    for(j=m->cols();j>0;j--)
1602    {
1603      M(i,j)=cf->convSingNFactoryN(BIMATELEM(*m,i,j),setchar,cf);
1604      setchar=FALSE;
1605    }
1606  }
1607  number res= cf->convFactoryNSingN( determinant(M,m->rows()),cf ) ;
1608  return res;
1609}
1610
1611#ifdef HAVE_NTL
1612#if 1
1613matrix singntl_HNF(matrix  m, const ring s )
1614{
1615  int r=m->rows();
1616  if (r!=m->cols())
1617  {
1618    Werror("HNF of %d x %d matrix",r,m->cols());
1619    return NULL;
1620  }
1621   
1622  matrix res=mp_New(r,r);
1623   
1624  if (rField_is_Q(s))
1625  {
1626
1627    CFMatrix M(r,r);
1628    int i,j;
1629    for(i=r;i>0;i--)
1630    {
1631      for(j=r;j>0;j--)
1632      {
1633        M(i,j)=convSingPFactoryP(MATELEM(m,i,j),s );
1634      }
1635    }
1636    CFMatrix *MM=cf_HNF(M);
1637    for(i=r;i>0;i--)
1638    {
1639      for(j=r;j>0;j--)
1640      {
1641        MATELEM(res,i,j)=convFactoryPSingP((*MM)(i,j),s);
1642      }
1643    }
1644    delete MM;
1645  }
1646  return res;
1647}
1648
1649intvec* singntl_HNF(intvec*  m, const ring)
1650{
1651  int r=m->rows();
1652  if (r!=m->cols())
1653  {
1654    Werror("HNF of %d x %d matrix",r,m->cols());
1655    return NULL;
1656  }
1657  setCharacteristic( 0 );
1658  CFMatrix M(r,r);
1659  int i,j;
1660  for(i=r;i>0;i--)
1661  {
1662    for(j=r;j>0;j--)
1663    {
1664      M(i,j)=IMATELEM(*m,i,j);
1665    }
1666  }
1667  CFMatrix *MM=cf_HNF(M);
1668  intvec *mm=ivCopy(m);
1669  for(i=r;i>0;i--)
1670  {
1671    for(j=r;j>0;j--)
1672    {
1673      IMATELEM(*mm,i,j)=convFactoryISingI((*MM)(i,j));
1674    }
1675  }
1676  delete MM;
1677  return mm;
1678}
1679
1680matrix singntl_LLL(matrix  m, const ring s )
1681{
1682  int r=m->rows();
1683  int c=m->cols();
1684  matrix res=mp_New(r,c);
1685  if (rField_is_Q(s))
1686  {
1687    CFMatrix M(r,c);
1688    int i,j;
1689    for(i=r;i>0;i--)
1690    {
1691      for(j=c;j>0;j--)
1692      {
1693        M(i,j)=convSingPFactoryP(MATELEM(m,i,j),s);
1694      }
1695    }
1696    CFMatrix *MM=cf_LLL(M);
1697    for(i=r;i>0;i--)
1698    {
1699      for(j=c;j>0;j--)
1700      {
1701        MATELEM(res,i,j)=convFactoryPSingP((*MM)(i,j),s);
1702      }
1703    }
1704    delete MM;
1705  }
1706  return res;
1707}
1708
1709intvec* singntl_LLL(intvec*  m, const ring)
1710{
1711  int r=m->rows();
1712  int c=m->cols();
1713  setCharacteristic( 0 );
1714  CFMatrix M(r,c);
1715  int i,j;
1716  for(i=r;i>0;i--)
1717  {
1718    for(j=r;j>0;j--)
1719    {
1720      M(i,j)=IMATELEM(*m,i,j);
1721    }
1722  }
1723  CFMatrix *MM=cf_LLL(M);
1724  intvec *mm=ivCopy(m);
1725  for(i=r;i>0;i--)
1726  {
1727    for(j=c;j>0;j--)
1728    {
1729      IMATELEM(*mm,i,j)=convFactoryISingI((*MM)(i,j));
1730    }
1731  }
1732  delete MM;
1733  return mm;
1734}
1735
1736ideal singclap_absFactorize ( poly f, ideal & mipos, intvec ** exps, int & numFactors, const ring r)
1737{
1738  p_Test(f, r);
1739
1740  ideal res=NULL;
1741
1742  int offs = rPar(r);
1743  if (f==NULL)
1744  {
1745    res= idInit (1, 1);
1746    mipos= idInit (1, 1);
1747    mipos->m[0]= convFactoryPSingTrP (Variable (offs), r); //overkill
1748    (*exps)=new intvec (1);
1749    (**exps)[0]= 1;
1750    numFactors= 0;
1751    return res;
1752  }
1753  CanonicalForm F( convSingTrPFactoryP( f, r) );
1754
1755  bool isRat= isOn (SW_RATIONAL);
1756  if (!isRat)
1757    On (SW_RATIONAL);
1758
1759  CFAFList absFactors= absFactorize (F);
1760
1761  int n= absFactors.length();
1762  *exps=new intvec (n);
1763
1764  res= idInit (n, 1);
1765
1766  mipos= idInit (n, 1);
1767
1768  Variable x= Variable (offs);
1769  Variable alpha;
1770  int i= 0;
1771  numFactors= 0;
1772  int count;
1773  CFAFListIterator iter= absFactors;
1774  CanonicalForm lead= iter.getItem().factor();
1775  if (iter.getItem().factor().inCoeffDomain())
1776  {
1777    i++;
1778    iter++;
1779  }
1780  for (; iter.hasItem(); iter++, i++)
1781  {
1782    (**exps)[i]= iter.getItem().exp();
1783    alpha= iter.getItem().minpoly().mvar();
1784    if (iter.getItem().minpoly().isOne())
1785      lead /= power (bCommonDen (iter.getItem().factor()), iter.getItem().exp());
1786    else
1787      lead /= power (power (bCommonDen (iter.getItem().factor()), degree (iter.getItem().minpoly())), iter.getItem().exp());
1788    res->m[i]= convFactoryPSingTrP (replacevar (iter.getItem().factor()*bCommonDen (iter.getItem().factor()), alpha, x), r);
1789    if (iter.getItem().minpoly().isOne())
1790    {
1791      count= iter.getItem().exp();
1792      mipos->m[i]= convFactoryPSingTrP (x,r);
1793    }
1794    else
1795    {
1796      count= iter.getItem().exp()*degree (iter.getItem().minpoly());
1797      mipos->m[i]= convFactoryPSingTrP (replacevar (iter.getItem().minpoly(), alpha, x), r);
1798    }
1799    numFactors += count;
1800  }
1801  if (!isRat)
1802    Off (SW_RATIONAL);
1803
1804  (**exps)[0]= 1;
1805  res->m[0]= convFactoryPSingTrP (lead, r);
1806  mipos->m[0]= convFactoryPSingTrP (x, r);
1807  return res;
1808}
1809
1810#endif
1811#endif /* HAVE_NTL */
1812
Note: See TracBrowser for help on using the repository browser.