source: git/Singular/clapsing.cc @ 1f5d21d

spielwiese
Last change on this file since 1f5d21d was 1f5d21d, checked in by Hans Schönemann <hannes@…>, 26 years ago
* hannes/jens: gcd in Q(a) retuns "not impl." git-svn-id: file:///usr/local/Singular/svn/trunk@1410 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 25.3 KB
Line 
1// emacs edit mode for this file is -*- C++ -*-
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5// $Id: clapsing.cc,v 1.30 1998-04-21 13:20:39 Singular Exp $
6/*
7* ABSTRACT: interface between Singular and factory
8*/
9
10#include "mod2.h"
11#ifdef HAVE_FACTORY
12#define SI_DONT_HAVE_GLOBAL_VARS
13#include "tok.h"
14#include "clapsing.h"
15#include "ipid.h"
16#include "numbers.h"
17#include "subexpr.h"
18#include "ipshell.h"
19#include "ring.h"
20#include <factory.h>
21#include "clapconv.h"
22#ifdef HAVE_LIBFAC_P
23#include <factor.h>
24#endif
25#include "ring.h"
26
27// FACTORY_GCD_TEST: use new gcd instead of old one.
28// FACTORY_GCD_STAT: print statistics on polynomials.  Works only
29//   with appropriately translated new gcd.
30// FACTORY_GCD_TIMING: accumulate time used for gcd calculations.
31//   For this option, the file `timing.h' from the Factory
32//   distribution has to be copied to the Singular source directory.
33//   Works only with new gcd.
34// FACTORY_GCD_DEBOUT: print polynomials involved in gcd calculations
35
36#ifdef FACTORY_GCD_STAT
37#define FACTORY_GCD_TEST
38#endif
39
40#ifdef FACTORY_GCD_TIMING
41#define FACTORY_GCD_TEST
42#define TIMING
43#include "timing.h"
44TIMING_DEFINE_PRINT( contentTimer );
45TIMING_DEFINE_PRINT( algContentTimer );
46TIMING_DEFINE_PRINT( algLcmTimer );
47#endif
48
49#ifdef FACTORY_GCD_DEBOUT
50#include "longalg.h"
51#include "febase.h"
52#endif
53
54poly singclap_gcd ( poly f, poly g )
55{
56  poly res=NULL;
57 
58  if (f) pCleardenom(f);
59  if (g) pCleardenom(g);
60 
61  // for now there is only the possibility to handle polynomials over
62  // Q and Fp ...
63  if (( nGetChar() == 0 || nGetChar() > 1 )
64  && (currRing->parameter==NULL))
65  {
66    setCharacteristic( nGetChar() );
67    CanonicalForm F( convSingPClapP( f ) ), G( convSingPClapP( g ) );
68    res=convClapPSingP( gcd( F, G ) );
69    Off(SW_RATIONAL);
70  }
71  // and over Q(a) / Fp(a)
72  else if (( nGetChar()==1 ) /* Q(a) */
73  || (nGetChar() <-1))       /* Fp(a) */
74  {
75    if (nGetChar()==1) setCharacteristic( 0 );
76    else               setCharacteristic( -nGetChar() );
77    if (currRing->minpoly!=NULL)
78    {
79      if ( nGetChar()==1 ) /* Q(a) */
80      {
81        WerrorS( feNotImplemented );
82      }
83      else
84      {
85        CanonicalForm mipo=convSingTrClapP(((lnumber)currRing->minpoly)->z);
86        Variable a=rootOf(mipo);
87        CanonicalForm F( convSingAPClapAP( f,a ) ), G( convSingAPClapAP( g,a ) );
88        res= convClapAPSingAP( gcd( F, G ) );
89      } 
90    }
91    else
92    {
93      CanonicalForm F( convSingTrPClapP( f ) ), G( convSingTrPClapP( g ) );
94      res= convClapPSingTrP( gcd( F, G ) );
95    }
96    Off(SW_RATIONAL);
97  }
98  #if 0
99  else if (( nGetChar()>1 )&&(currRing->parameter!=NULL)) /* GF(q) */
100  {
101    int p=rChar(currRing);
102    int n=2;
103    int t=p*p;
104    while (t!=nChar) { t*=p;n++; }
105    setCharacteristic(p,n,'a');
106    CanonicalForm F( convSingGFClapGF( f ) ), G( convSingGFClapGF( g ) );
107    res= convClapGFSingGF( gcd( F, G ) );
108  }
109  #endif
110  else
111    WerrorS( feNotImplemented );
112
113  pDelete(&f);
114  pDelete(&g);
115  return res;
116}
117
118poly singclap_resultant ( poly f, poly g , poly x)
119{
120  int i=pIsPurePower(x);
121  if (i==0)
122  {
123    WerrorS("3rd argument must be a ring variable");
124    return NULL;
125  }
126  // for now there is only the possibility to handle polynomials over
127  // Q and Fp ...
128  if (( nGetChar() == 0 || nGetChar() > 1 )
129  && (currRing->parameter==NULL))
130  {
131    Variable X(i);
132    setCharacteristic( nGetChar() );
133    CanonicalForm F( convSingPClapP( f ) ), G( convSingPClapP( g ) );
134    poly res=convClapPSingP( resultant( F, G, X ) );
135    Off(SW_RATIONAL);
136    return res;
137  }
138  // and over Q(a) / Fp(a)
139  else if (( nGetChar()==1 ) /* Q(a) */
140  || (nGetChar() <-1))       /* Fp(a) */
141  {
142    if (nGetChar()==1) setCharacteristic( 0 );
143    else               setCharacteristic( -nGetChar() );
144    poly res;
145    if (currRing->minpoly!=NULL)
146    {
147      Variable X(i);
148      CanonicalForm mipo=convSingTrClapP(((lnumber)currRing->minpoly)->z);
149      Variable a=rootOf(mipo);
150      CanonicalForm F( convSingAPClapAP( f,a ) ), G( convSingAPClapAP( g,a ) );
151      res= convClapAPSingAP( resultant( F, G, X ) );
152    }
153    else
154    {
155      Variable X(i+rPar(currRing));
156      CanonicalForm F( convSingTrPClapP( f ) ), G( convSingTrPClapP( g ) );
157      res= convClapPSingTrP( resultant( F, G, X ) );
158    }
159    Off(SW_RATIONAL);
160    return res;
161  }
162  else
163    WerrorS( feNotImplemented );
164  return NULL;
165}
166//poly singclap_resultant ( poly f, poly g , poly x)
167//{
168//  int i=pVar(x);
169//  if (i==0)
170//  {
171//    WerrorS("ringvar expected");
172//    return NULL;
173//  }
174//  ideal I=idInit(1,1);
175//
176//  // get the coeffs von f wrt. x:
177//  I->m[0]=pCopy(f);
178//  matrix ffi=mpCoeffs(I,i);
179//  ffi->rank=1;
180//  ffi->ncols=ffi->nrows;
181//  ffi->nrows=1;
182//  ideal fi=(ideal)ffi;
183//
184//  // get the coeffs von g wrt. x:
185//  I->m[0]=pCopy(g);
186//  matrix ggi=mpCoeffs(I,i);
187//  ggi->rank=1;
188//  ggi->ncols=ggi->nrows;
189//  ggi->nrows=1;
190//  ideal gi=(ideal)ggi;
191//
192//  // contruct the matrix:
193//  int fn=IDELEMS(fi); //= deg(f,x)+1
194//  int gn=IDELEMS(gi); //= deg(g,x)+1
195//  matrix m=mpNew(fn+gn-2,fn+gn-2);
196//  if(m==NULL)
197//  {
198//    return NULL;
199//  }
200//
201//  // enter the coeffs into m:
202//  int j;
203//  for(i=0;i<gn-1;i++)
204//  {
205//    for(j=0;j<fn;j++)
206//    {
207//      MATELEM(m,i+1,fn-j+i)=pCopy(fi->m[j]);
208//    }
209//  }
210//  for(i=0;i<fn-1;i++)
211//  {
212//    for(j=0;j<gn;j++)
213//    {
214//      MATELEM(m,gn+i,gn-j+i)=pCopy(gi->m[j]);
215//    }
216//  }
217//
218//  poly r=mpDet(m);
219//
220//  idDelete(&fi);
221//  idDelete(&gi);
222//  idDelete((ideal *)&m);
223//  return r;
224//}
225
226lists singclap_extgcd ( poly f, poly g )
227{
228  // for now there is only the possibility to handle univariate
229  // polynomials over
230  // Q and Fp ...
231  poly res=NULL,pa=NULL,pb=NULL;
232  On(SW_SYMMETRIC_FF);
233  if (( nGetChar() == 0 || nGetChar() > 1 )
234  && (currRing->parameter==NULL))
235  {
236    setCharacteristic( nGetChar() );
237    CanonicalForm F( convSingPClapP( f ) ), G( convSingPClapP( g ) );
238    if (!F.isUnivariate() || !G.isUnivariate() || F.mvar()!=G.mvar())
239    {
240      Off(SW_RATIONAL);
241      WerrorS("not univariate");
242      return NULL;
243    }
244    CanonicalForm Fa,Gb;
245    res=convClapPSingP( extgcd( F, G, Fa, Gb ) );
246    pa=convClapPSingP(Fa);
247    pb=convClapPSingP(Gb);
248    Off(SW_RATIONAL);
249  }
250  // and over Q(a) / Fp(a)
251  else if (( nGetChar()==1 ) /* Q(a) */
252  || (nGetChar() <-1))       /* Fp(a) */
253  {
254    if (nGetChar()==1) setCharacteristic( 0 );
255    else               setCharacteristic( -nGetChar() );
256    CanonicalForm Fa,Gb;
257    if (currRing->minpoly!=NULL)
258    {
259      CanonicalForm mipo=convSingTrClapP(((lnumber)currRing->minpoly)->z);
260      Variable a=rootOf(mipo);
261      CanonicalForm F( convSingAPClapAP( f,a ) ), G( convSingAPClapAP( g,a ) );
262      if (!F.isUnivariate() || !G.isUnivariate() || F.mvar()!=G.mvar())
263      {
264        WerrorS("not univariate");
265        return NULL;
266      }
267      res= convClapAPSingAP( extgcd( F, G, Fa, Gb ) );
268      pa=convClapAPSingAP(Fa);
269      pb=convClapAPSingAP(Gb);
270    }
271    else
272    {
273      CanonicalForm F( convSingTrPClapP( f ) ), G( convSingTrPClapP( g ) );
274      if (!F.isUnivariate() || !G.isUnivariate() || F.mvar()!=G.mvar())
275      {
276        Off(SW_RATIONAL);
277        WerrorS("not univariate");
278        return NULL;
279      }
280      res= convClapPSingTrP( extgcd( F, G, Fa, Gb ) );
281      pa=convClapPSingTrP(Fa);
282      pb=convClapPSingTrP(Gb);
283    }
284    Off(SW_RATIONAL);
285  }
286  else
287  {
288    WerrorS( feNotImplemented );
289    return NULL;
290  }
291  lists L=(lists)Alloc(sizeof(slists));
292  L->Init(3);
293  L->m[0].rtyp=POLY_CMD;
294  L->m[0].data=(void *)res;
295  L->m[1].rtyp=POLY_CMD;
296  L->m[1].data=(void *)pa;
297  L->m[2].rtyp=POLY_CMD;
298  L->m[2].data=(void *)pb;
299  return L;
300}
301
302poly singclap_pdivide ( poly f, poly g )
303{
304  // for now there is only the possibility to handle polynomials over
305  // Q and Fp ...
306  poly res=NULL;
307  On(SW_RATIONAL);
308  if (( nGetChar() == 0 || nGetChar() > 1 )
309  && (currRing->parameter==NULL))
310  {
311    setCharacteristic( nGetChar() );
312    CanonicalForm F( convSingPClapP( f ) ), G( convSingPClapP( g ) );
313    res = convClapPSingP( F / G );
314  }
315  // and over Q(a) / Fp(a)
316  else if (( nGetChar()==1 ) /* Q(a) */
317  || (nGetChar() <-1))       /* Fp(a) */
318  {
319    if (nGetChar()==1) setCharacteristic( 0 );
320    else               setCharacteristic( -nGetChar() );
321    if (currRing->minpoly!=NULL)
322    {
323      CanonicalForm mipo=convSingTrClapP(((lnumber)currRing->minpoly)->z);
324      Variable a=rootOf(mipo);
325      CanonicalForm F( convSingAPClapAP( f,a ) ), G( convSingAPClapAP( g,a ) );
326      res= convClapAPSingAP(  F / G  );
327    }
328    else
329    {
330      CanonicalForm F( convSingTrPClapP( f ) ), G( convSingTrPClapP( g ) );
331      res= convClapPSingTrP(  F / G  );
332    }
333  }
334  else
335    WerrorS( feNotImplemented );
336  Off(SW_RATIONAL);
337  return res;
338}
339
340void singclap_divide_content ( poly f )
341{
342  if ( nGetChar() == 1 )
343    setCharacteristic( 0 );
344  else  if ( nGetChar() == -1 )
345    return; /* not implemented for R */
346  else  if ( nGetChar() < 0 )
347    setCharacteristic( -nGetChar() );
348  else if (currRing->parameter==NULL) /* not GF(q) */
349    setCharacteristic( nGetChar() );
350  else
351    return; /* not implemented*/
352  if ( f==NULL )
353  {
354    return;
355  }
356  else  if ( pNext( f ) == NULL )
357  {
358    pSetCoeff( f, nInit( 1 ) );
359    return;
360  }
361  else
362  {
363    CFList L;
364    CanonicalForm g, h;
365    poly p = pNext(f);
366    nTest(pGetCoeff(f));
367#ifdef FACTORY_GCD_DEBOUT
368    StringSetS("g0 = ");
369    if (f) napWrite(((lnumber)pGetCoeff(f))->z);
370    PrintS(StringAppend("\n"));
371#endif
372    g = convSingTrClapP( ((lnumber)pGetCoeff(f))->z );
373    L.append( g );
374#ifdef FACTORY_GCD_TIMING
375    TIMING_START( contentTimer );
376#endif
377    while ( p && (g != 1) )
378    {
379      nTest(pGetCoeff(p));
380#ifdef FACTORY_GCD_DEBOUT
381      StringSetS("h = ");
382      if (p) napWrite(((lnumber)pGetCoeff(p))->z);
383      PrintS(StringAppend("\n"));
384#endif
385      h = convSingTrClapP( ((lnumber)pGetCoeff(p))->z );
386      p = pNext( p );
387#ifdef FACTORY_GCD_STAT
388      fprintf( stderr, "cont:\t" );
389#endif
390#ifdef FACTORY_GCD_TEST
391      g = CFPrimitiveGcdUtil::gcd( g, h );
392#else
393
394      // this part is translated if there are not any options at all
395      g = gcd( g, h );
396
397#endif
398#ifdef FACTORY_GCD_DEBOUT
399      StringSetS("g = ");
400      napWrite(convClapPSingTr(g));
401      PrintS(StringAppend("\n"));
402#endif
403      L.append( h );
404    }
405#ifdef FACTORY_GCD_TIMING
406    TIMING_END( contentTimer );
407#endif
408    if ( g == 1 )
409    {
410      pTest(f);
411      return;
412    }
413    #ifdef LDEBUG
414    else if ( g == 0 )
415    {
416      pTest(f);
417      pWrite(f);
418      PrintS("=> gcd 0 in divide_content\n");
419      return;
420    }
421    #endif
422    else
423    {
424      CFListIterator i;
425      for ( i = L, p = f; i.hasItem(); i++, p=pNext(p) )
426      {
427        lnumber c=(lnumber)pGetCoeff(p);
428        napDelete(&c->z);
429        #ifdef LDEBUG
430        number nt=(number)Alloc0(sizeof(rnumber));
431        lnumber nnt=(lnumber)nt;
432        nnt->z=convClapPSingTr( i.getItem());
433        nTest(nt);
434        #endif
435        c->z=convClapPSingTr( i.getItem() / g );
436        nTest((number)c);
437        //#ifdef LDEBUG
438        //number cn=(number)c;
439        //StringSet(""); nWrite(nt); StringAppend(" ==> ");
440        //nWrite(cn);PrintS(StringAppend("\n"));
441        //#endif
442      }
443    }
444    pTest(f);
445  }
446}
447
448ideal singclap_factorize ( poly f, intvec ** v , int with_exps)
449{
450  // with_exps: 1 return only true factors
451  //            2 return true factors and exponents
452  //            0 return factors and exponents
453
454  ideal res=NULL;
455  if (f==NULL)
456  {
457    res=idInit(1,1);
458    if (with_exps!=1)
459    {
460      (*v)=new intvec(1);
461    }
462    return res;
463  }
464  Off(SW_RATIONAL);
465  On(SW_SYMMETRIC_FF);
466  CFFList L;
467  number N=NULL;
468
469  if (( (nGetChar() == 0) || (nGetChar() > 1) )
470  && (currRing->parameter==NULL))
471  {
472    setCharacteristic( nGetChar() );
473    if (nGetChar()==0) /* Q */
474    {
475      if (f!=NULL)
476      {
477        if (with_exps==0)
478          N=nCopy(pGetCoeff(f));
479        pCleardenom(f);
480        if (with_exps==0)
481        {
482          number nn=nDiv(N,pGetCoeff(f));
483          nDelete(&N);
484          N=nn;
485        }
486      }
487    }
488    CanonicalForm F( convSingPClapP( f ) );
489    if (nGetChar()==0) /* Q */
490    {
491      L = factorize( F );
492    }
493    else /* Fp */
494    {
495#ifdef HAVE_LIBFAC_P
496      L = Factorize( F );
497#else
498      return NULL;
499#endif
500    }
501  }
502  // and over Q(a) / Fp(a)
503  else if (( nGetChar()==1 ) /* Q(a) */
504  || (nGetChar() <-1))       /* Fp(a) */
505  {
506    if (nGetChar()==1) setCharacteristic( 0 );
507    else               setCharacteristic( -nGetChar() );
508    if (currRing->minpoly!=NULL)
509    {
510      //if (nGetChar()==1)
511      //{
512      //  WerrorS( feNotImplemented );
513      //  return NULL;
514      //}
515      CanonicalForm mipo=convSingTrClapP(((lnumber)currRing->minpoly)->z);
516      Variable a=rootOf(mipo);
517      CanonicalForm F( convSingAPClapAP( f,a ) );
518      L = factorize( F, a );
519    }
520    else
521    {
522      CanonicalForm F( convSingTrPClapP( f ) );
523      if (nGetChar()==1) /* Q(a) */
524      {
525        L = factorize( F );
526      }
527      else /* Fp(a) */
528      {
529#ifdef HAVE_LIBFAC_P
530        L = Factorize( F );
531#else
532        return NULL;
533#endif
534      }
535    }
536  }
537  else
538  {
539    WerrorS( feNotImplemented );
540    goto end;
541  }
542  {
543    // the first factor should be a constant
544    if ( ! L.getFirst().factor().inCoeffDomain() )
545      L.insert(CFFactor(1,1));
546    // convert into ideal
547    int n = L.length();
548    CFFListIterator J=L;
549    int j=0;
550    if (with_exps!=1)
551    {
552      if ((with_exps==2)&&(n>1))
553      {
554        n--;
555        J++;
556      }
557      *v = new intvec( n );
558    }
559    res = idInit( n ,1);
560    for ( ; J.hasItem(); J++, j++ )
561    {
562      if (with_exps!=1) (**v)[j] = J.getItem().exp();
563      if ((nGetChar()==0)||(nGetChar()>1))           /* Q, Fp */
564        res->m[j] = convClapPSingP( J.getItem().factor() );
565      else if ((nGetChar()==1)||(nGetChar()<-1))     /* Q(a), Fp(a) */
566      {
567        if (currRing->minpoly==NULL)
568          res->m[j] = convClapPSingTrP( J.getItem().factor() );
569        else
570          res->m[j] = convClapAPSingAP( J.getItem().factor() );
571      }
572    }
573    if (N!=NULL)
574    {
575      pMultN(res->m[0],N);
576      nDelete(&N);
577    }
578    // delete constants
579    if ((with_exps!=0) && (res!=NULL))
580    {
581      int i=IDELEMS(res)-1;
582      int j=0;
583      for(;i>=0;i--)
584      {
585        if (pIsConstant(res->m[i]))
586        {
587          pDelete(&(res->m[i]));
588          if ((v!=NULL) && ((*v)!=NULL))
589            (**v)[i]=0;
590          j++;
591        }
592      }
593      if (j>0)
594      {
595        idSkipZeroes(res);
596        if ((v!=NULL) && ((*v)!=NULL))
597        {
598          intvec *w=*v;
599          *v = new intvec( max(n-j,1) );
600          for (i=0,j=0;i<w->length();i++)
601          {
602            if((*w)[i]!=0)
603            {
604              (**v)[j]=(*w)[i]; j++;
605            }
606          } 
607          delete w;
608        } 
609      }
610      if (res->m[0]==NULL)
611      {
612        res->m[0]=pOne();
613      }
614    }
615  }
616end:
617  return res;
618}
619
620matrix singclap_irrCharSeries ( ideal I)
621{
622#ifdef HAVE_LIBFAC_P
623  // for now there is only the possibility to handle polynomials over
624  // Q and Fp ...
625  matrix res=NULL;
626  int i;
627  Off(SW_RATIONAL);
628  On(SW_SYMMETRIC_FF);
629  CFList L;
630  ListCFList LL;
631  if (((nGetChar() == 0) || (nGetChar() > 1) )
632  && (currRing->parameter==NULL))
633  {
634    setCharacteristic( nGetChar() );
635    for(i=0;i<IDELEMS(I);i++)
636    {
637      L.append(convSingPClapP(I->m[i]));
638    }
639  }
640  // and over Q(a) / Fp(a)
641  else if (( nGetChar()==1 ) /* Q(a) */
642  || (nGetChar() <-1))       /* Fp(a) */
643  {
644    if (nGetChar()==1) setCharacteristic( 0 );
645    else               setCharacteristic( -nGetChar() );
646    for(i=0;i<IDELEMS(I);i++)
647    {
648      L.append(convSingTrPClapP(I->m[i]));
649    }
650  }
651  else
652  {
653    WerrorS( feNotImplemented );
654    return res;
655  }
656
657  LL=IrrCharSeries(L);
658  int m= LL.length(); // Anzahl Zeilen
659  int n=0;
660  ListIterator<CFList> LLi;
661  CFListIterator Li;
662  for ( LLi = LL; LLi.hasItem(); LLi++ )
663  {
664    n = max(LLi.getItem().length(),n);
665  }
666  res=mpNew(m,n);
667  if ((m==0) || (n==0))
668  {
669    Warn("char_series returns %d x %d matrix from %d input polys (%d)\n",m,n,IDELEMS(I)+1,LL.length());
670    iiWriteMatrix((matrix)I,"I",2,0);
671  }
672  for ( m=1, LLi = LL; LLi.hasItem(); LLi++, m++ )
673  {
674    for (n=1, Li = LLi.getItem(); Li.hasItem(); Li++, n++)
675    {
676      if ( (nGetChar() == 0) || (nGetChar() > 1) )
677        MATELEM(res,m,n)=convClapPSingP(Li.getItem());
678      else
679        MATELEM(res,m,n)=convClapPSingTrP(Li.getItem());
680    }
681  }
682  Off(SW_RATIONAL);
683  return res;
684#else
685  return NULL;
686#endif
687}
688
689char* singclap_neworder ( ideal I)
690{
691#ifdef HAVE_LIBFAC_P
692  int i;
693  Off(SW_RATIONAL);
694  On(SW_SYMMETRIC_FF);
695  CFList L;
696  if (((nGetChar() == 0) || (nGetChar() > 1) )
697  && (currRing->parameter==NULL))
698  {
699    setCharacteristic( nGetChar() );
700    for(i=0;i<IDELEMS(I);i++)
701    {
702      L.append(convSingPClapP(I->m[i]));
703    }
704  }
705  // and over Q(a) / Fp(a)
706  else if (( nGetChar()==1 ) /* Q(a) */
707  || (nGetChar() <-1))       /* Fp(a) */
708  {
709    if (nGetChar()==1) setCharacteristic( 0 );
710    else               setCharacteristic( -nGetChar() );
711    for(i=0;i<IDELEMS(I);i++)
712    {
713      L.append(convSingTrPClapP(I->m[i]));
714    }
715  }
716  else
717  {
718    WerrorS( feNotImplemented );
719    return NULL;
720  }
721
722  List<int> IL=neworderint(L);
723  ListIterator<int> Li;
724  StringSet("");
725  Li = IL;
726  int* mark=(int*)Alloc0(pVariables*sizeof(int));
727  int cnt=pVariables;
728  loop
729  {
730    i=Li.getItem()-1;
731    mark[i]=1;
732    StringAppend(currRing->names[i]);
733    Li++;
734    cnt--;
735    if(cnt==0) break;
736    StringAppend(",");
737    if(! Li.hasItem()) break;
738  }
739  for(i=0;i<pVariables;i++)
740  {
741    if(mark[i]==0)
742    {
743      StringAppend(currRing->names[i]);
744      cnt--;
745      if(cnt==0) break;
746      StringAppend(",");
747    }
748  }
749  return mstrdup(StringAppend(""));
750#else
751  return NULL;
752#endif
753}
754
755BOOLEAN singclap_isSqrFree(poly f)
756{
757  BOOLEAN b=FALSE;
758  Off(SW_RATIONAL);
759  //  Q / Fp
760  if (((nGetChar() == 0) || (nGetChar() > 1) )
761  &&(currRing->parameter==NULL))
762  {
763    setCharacteristic( nGetChar() );
764    CanonicalForm F( convSingPClapP( f ) );
765    if((nGetChar()>1)&&(!F.isUnivariate()))
766      goto err;
767    b=(BOOLEAN)isSqrFree(F);
768  }
769  // and over Q(a) / Fp(a)
770  else if (( nGetChar()==1 ) /* Q(a) */
771  || (nGetChar() <-1))       /* Fp(a) */
772  {
773    if (nGetChar()==1) setCharacteristic( 0 );
774    else               setCharacteristic( -nGetChar() );
775    //if (currRing->minpoly!=NULL)
776    //{
777    //  CanonicalForm mipo=convSingTrClapP(((lnumber)currRing->minpoly)->z);
778    //  Variable a=rootOf(mipo);
779    //  CanonicalForm F( convSingAPClapAP( f,a ) );
780    //  ...
781    //}
782    //else
783    {
784      CanonicalForm F( convSingTrPClapP( f ) );
785      b=(BOOLEAN)isSqrFree(F);
786    }
787    Off(SW_RATIONAL);
788  }
789  else
790  {
791err:
792    WerrorS( feNotImplemented );
793  }
794  return b;
795}
796
797poly singclap_det( const matrix m )
798{
799  int r=m->rows();
800  if (r!=m->cols())
801  {
802    Werror("det of %d x %d matrix",r,m->cols());
803    return NULL;
804  }
805  poly res=NULL;
806  if (( nGetChar() == 0 || nGetChar() > 1 )
807  && (currRing->parameter==NULL))
808  {
809    setCharacteristic( nGetChar() );
810    CFMatrix M(r,r);
811    int i,j;
812    for(i=r;i>0;i--)
813    {
814      for(j=r;j>0;j--)
815      {
816        M(i,j)=convSingPClapP(MATELEM(m,i,j));
817      }
818    }
819    res= convClapPSingP( determinant(M,r) ) ;
820  }
821  // and over Q(a) / Fp(a)
822  else if (( nGetChar()==1 ) /* Q(a) */
823  || (nGetChar() <-1))       /* Fp(a) */
824  {
825    if (nGetChar()==1) setCharacteristic( 0 );
826    else               setCharacteristic( -nGetChar() );
827    CFMatrix M(r,r);
828    poly res;
829    if (currRing->minpoly!=NULL)
830    {
831      CanonicalForm mipo=convSingTrClapP(((lnumber)currRing->minpoly)->z);
832      Variable a=rootOf(mipo);
833      int i,j;
834      for(i=r;i>0;i--)
835      {
836        for(j=r;j>0;j--)
837        {
838          M(i,j)=convSingAPClapAP(MATELEM(m,i,j),a);
839        }
840      }
841      res= convClapAPSingAP( determinant(M,r) ) ;
842    }
843    else
844    {
845      int i,j;
846      for(i=r;i>0;i--)
847      {
848        for(j=r;j>0;j--)
849        {
850          M(i,j)=convSingTrPClapP(MATELEM(m,i,j));
851        }
852      }
853      res= convClapPSingTrP( determinant(M,r) );
854    }
855  }
856  else
857    WerrorS( feNotImplemented );
858  Off(SW_RATIONAL);
859  return res;
860}
861
862int singclap_det_i( intvec * m )
863{
864  setCharacteristic( 0 );
865  CFMatrix M(m->rows(),m->cols());
866  int i,j;
867  for(i=m->rows();i>0;i--)
868  {
869    for(j=m->cols();j>0;j--)
870    {
871      M(i,j)=IMATELEM(*m,i,j);
872    }
873  }
874  int res= convClapISingI( determinant(M,m->rows())) ;
875  Off(SW_RATIONAL);
876  return res;
877}
878/*==============================================================*/
879/* interpreter interface : */
880BOOLEAN jjGCD_P(leftv res, leftv u, leftv v)
881{
882  res->data=(void *)singclap_gcd((poly)(u->CopyD()),((poly)v->CopyD()));
883  return FALSE;
884}
885
886BOOLEAN jjFAC_P(leftv res, leftv u)
887{
888  intvec *v=NULL;
889  ideal f=singclap_factorize((poly)(u->Data()), &v, 0);
890#ifndef HAVE_LIBFAC_P
891  if (f==NULL) return TRUE;
892#endif
893  lists l=(lists)Alloc(sizeof(slists));
894  l->Init(2);
895  l->m[0].rtyp=IDEAL_CMD;
896  l->m[0].data=(void *)f;
897  l->m[1].rtyp=INTVEC_CMD;
898  l->m[1].data=(void *)v;
899  res->data=(void *)l;
900  return FALSE;
901}
902
903BOOLEAN jjSQR_FREE_DEC(leftv res, leftv u,leftv dummy)
904{
905  intvec *v=NULL;
906  int sw=(int)dummy->Data();
907  ideal f=singclap_factorize((poly)(u->Data()), &v, sw);
908  switch(sw)
909  {
910    case 0:
911    case 2:
912    {
913      lists l=(lists)Alloc(sizeof(slists));
914      l->Init(2);
915      l->m[0].rtyp=IDEAL_CMD;
916      l->m[0].data=(void *)f;
917      l->m[1].rtyp=INTVEC_CMD;
918      l->m[1].data=(void *)v;
919      res->data=(void *)l;
920      res->rtyp=LIST_CMD;
921      return FALSE;
922    }
923    case 1:
924      res->data=(void *)f;
925      return f==NULL;
926  }
927  WerrorS("invalid switch");
928  return TRUE;
929}
930
931#if 0
932BOOLEAN jjIS_SQR_FREE(leftv res, leftv u)
933{
934  BOOLEAN b=singclap_factorize((poly)(u->Data()), &v, 0);
935  res->data=(void *)b;
936}
937#endif
938
939BOOLEAN jjEXTGCD_P(leftv res, leftv u, leftv v)
940{
941  res->data=singclap_extgcd((poly)u->Data(),(poly)v->Data());
942  return (res->data==NULL);
943}
944BOOLEAN jjRESULTANT(leftv res, leftv u, leftv v, leftv w)
945{
946  res->data=singclap_resultant((poly)u->Data(),(poly)v->Data(), (poly)w->Data());
947  return errorreported;
948}
949BOOLEAN jjCHARSERIES(leftv res, leftv u)
950{
951  res->data=singclap_irrCharSeries((ideal)u->Data());
952  return (res->data==NULL);
953}
954
955alg singclap_alglcm ( alg f, alg g )
956{
957#ifdef FACTORY_GCD_DEBOUT
958    StringSetS("f = ");
959    napWrite(f);
960    PrintS(StringAppend("\n"));
961    StringSetS("g = ");
962    napWrite(g);
963    PrintS(StringAppend("\n"));
964#endif
965  // over Q(a) / Fp(a)
966 if (nGetChar()==1) setCharacteristic( 0 );
967 else               setCharacteristic( -nGetChar() );
968 alg res;
969#ifdef FACTORY_GCD_STAT
970 fprintf( stderr, "algLcm:\t" );
971#endif
972 if (currRing->minpoly!=NULL)
973 {
974   CanonicalForm mipo=convSingTrClapP(((lnumber)currRing->minpoly)->z);
975   Variable a=rootOf(mipo);
976   CanonicalForm F( convSingAClapA( f,a ) ), G( convSingAClapA( g,a ) );
977#ifdef FACTORY_GCD_TIMING
978   CanonicalForm GCD;
979   TIMING_START( algLcmTimer );
980   GCD = CFPrimitiveGcdUtil::gcd( F, G );
981   TIMING_END( algLcmTimer );
982   res= convClapASingA( (F/ GCD)*G );
983#elif defined( FACTORY_GCD_TEST )
984   res= convClapASingA( (F/ CFPrimitiveGcdUtil::gcd( F, G ))*G );
985#else
986
987   // this part is translated if there are not any options at all
988   res= convClapASingA( (F/ gcd( F, G ))*G );
989
990#endif
991 }
992 else
993 {
994   CanonicalForm F( convSingTrClapP( f ) ), G( convSingTrClapP( g ) );
995#ifdef FACTORY_GCD_TIMING
996   CanonicalForm GCD;
997   TIMING_START( algLcmTimer );
998   GCD = CFPrimitiveGcdUtil::gcd( F, G );
999   TIMING_END( algLcmTimer );
1000   res= convClapPSingTr( (F/GCD)*G );
1001#elif defined( FACTORY_GCD_TEST )
1002   res= convClapPSingTr( (F/CFPrimitiveGcdUtil::gcd( F, G ))*G );
1003#else
1004
1005   // this part is translated if there are not any options at all
1006   res= convClapPSingTr( (F/gcd( F, G ))*G );
1007
1008#endif
1009 }
1010 Off(SW_RATIONAL);
1011#ifdef FACTORY_GCD_DEBOUT
1012    StringSetS("res = ");
1013    napWrite(res);
1014    PrintS(StringAppend("\n"));
1015#endif
1016 return res;
1017}
1018
1019void singclap_algdividecontent ( alg f, alg g, alg &ff, alg &gg )
1020{
1021#ifdef FACTORY_GCD_DEBOUT
1022  alg algGcd;
1023  StringSetS("f = ");
1024  napWrite(f);
1025  PrintS(StringAppend("\n"));
1026  StringSetS("g = ");
1027  napWrite(g);
1028  PrintS(StringAppend("\n"));
1029#endif
1030  // over Q(a) / Fp(a)
1031 if (nGetChar()==1) setCharacteristic( 0 );
1032 else               setCharacteristic( -nGetChar() );
1033 ff=gg=NULL;
1034#ifdef FACTORY_GCD_STAT
1035 fprintf( stderr, "alCont:\t" );
1036#endif
1037 if (currRing->minpoly!=NULL)
1038 {
1039   CanonicalForm mipo=convSingTrClapP(((lnumber)currRing->minpoly)->z);
1040   Variable a=rootOf(mipo);
1041   CanonicalForm F( convSingAClapA( f,a ) ), G( convSingAClapA( g,a ) );
1042#ifdef FACTORY_GCD_TIMING
1043   CanonicalForm GCD;
1044   TIMING_START( algContentTimer );
1045   GCD=CFPrimitiveGcdUtil::gcd( F, G );
1046   TIMING_END( algContentTimer );
1047#elif defined( FACTORY_GCD_TEST )
1048   CanonicalForm GCD=CFPrimitiveGcdUtil::gcd( F, G );
1049#else
1050
1051   // this part is translated if there are not any options at all
1052   CanonicalForm GCD=gcd( F, G );
1053
1054#endif
1055   if (GCD!=1)
1056   {
1057     ff= convClapASingA( F/ GCD );
1058     gg= convClapASingA( G/ GCD );
1059   }
1060#ifdef FACTORY_GCD_DEBOUT
1061   algGcd=convClapASingA( GCD );
1062#endif
1063 }
1064 else
1065 {
1066   CanonicalForm F( convSingTrClapP( f ) ), G( convSingTrClapP( g ) );
1067#ifdef FACTORY_GCD_TIMING
1068   CanonicalForm GCD;
1069   TIMING_START( algContentTimer );
1070   GCD=CFPrimitiveGcdUtil::gcd( F, G );
1071   TIMING_END( algContentTimer );
1072#elif defined( FACTORY_GCD_TEST )
1073   CanonicalForm GCD=CFPrimitiveGcdUtil::gcd( F, G );
1074#else
1075
1076   // this part is translated if there are not any options at all
1077   CanonicalForm GCD=gcd( F, G );
1078
1079#endif
1080   if (GCD!=1)
1081   {
1082     ff= convClapPSingTr( F/ GCD );
1083     gg= convClapPSingTr( G/ GCD );
1084   }
1085#ifdef FACTORY_GCD_DEBOUT
1086   algGcd=convClapPSingTr( GCD );
1087#endif
1088 }
1089 Off(SW_RATIONAL);
1090#ifdef FACTORY_GCD_DEBOUT
1091  StringSetS("gcd = ");
1092  napWrite(algGcd);
1093  PrintS(StringAppend("\n"));
1094  napDelete(&algGcd);
1095#endif
1096}
1097#endif
Note: See TracBrowser for help on using the repository browser.