source: git/Singular/clapsing.cc @ 6ae4f5

spielwiese
Last change on this file since 6ae4f5 was 6ae4f5, checked in by Hans Schönemann <hannes@…>, 27 years ago
* hannes: - corrected scanner.l: parsing of strings in blocks: if (1) { write("","}"); } - corrected ipassign.cc: assignment of "dummy" types: DEF, NONE - corrected sleftv::Print(_), initialisation of _ - added conversion int->def - added CopyD(DEF) - in insert(..): object should not be of type NONE (lists.cc:lInsert0) - added int*intvec, int*intmat to iparith.cc git-svn-id: file:///usr/local/Singular/svn/trunk@145 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 16.7 KB
Line 
1// emacs edit mode for this file is -*- C++ -*-
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5// $Id: clapsing.cc,v 1.3 1997-04-09 12:19:40 Singular Exp $
6/*
7* ABSTRACT: interface between Singular and factory
8*/
9
10
11#include "mod2.h"
12#ifdef HAVE_LIBFACTORY
13#define SI_DONT_HAVE_GLOBAL_VARS
14#include "tok.h"
15#include "clapsing.h"
16#include "ipid.h"
17#include "numbers.h"
18#include "subexpr.h"
19#include "ipshell.h"
20#include <singfactory.h>
21#include "clapconv.h"
22#ifdef HAVE_LIBFAC_P
23#include <factor.h>
24#endif
25
26poly singclap_gcd ( poly f, poly g )
27{
28  // for now there is only the possibility to handle polynomials over
29  // Q and Fp ...
30  if ( nGetChar() == 0 || nGetChar() > 1 )
31  {
32    setCharacteristic( nGetChar() );
33    CanonicalForm F( convSingPClapP( f ) ), G( convSingPClapP( g ) );
34    poly res=convClapPSingP( gcd( F, G ) );
35    Off(SW_RATIONAL);
36    return res;
37  }
38  // and over Q(a) / Fp(a)
39  else if (( nGetChar()==1 ) /* Q(a) */
40  || (nGetChar() <-1))       /* Fp(a) */
41  {
42    if (nGetChar()==1) setCharacteristic( 0 );
43    else               setCharacteristic( -nGetChar() );
44    poly res;
45    if (currRing->minpoly!=NULL)
46    {
47      CanonicalForm mipo=convSingTrClapP(((lnumber)currRing->minpoly)->z);
48      Variable a=rootOf(mipo);
49      CanonicalForm F( convSingAPClapAP( f,a ) ), G( convSingAPClapAP( g,a ) );
50      res= convClapAPSingAP( gcd( F, G ) );
51    }
52    else
53    {
54      CanonicalForm F( convSingTrPClapP( f ) ), G( convSingTrPClapP( g ) );
55      res= convClapPSingTrP( gcd( F, G ) );
56    }
57    Off(SW_RATIONAL);
58    return res;
59  }
60  else
61    WerrorS( "not implemented" );
62  return NULL;
63}
64
65poly singclap_resultant ( poly f, poly g , poly x)
66{
67  int i=pIsPurePower(x);
68  if (i==0)
69  {
70    WerrorS("3rd argument must be a ring variable");
71    return NULL;
72  }
73  Variable X(i);
74  // for now there is only the possibility to handle polynomials over
75  // Q and Fp ...
76  if ( nGetChar() == 0 || nGetChar() > 1 )
77  {
78    setCharacteristic( nGetChar() );
79    CanonicalForm F( convSingPClapP( f ) ), G( convSingPClapP( g ) );
80    poly res=convClapPSingP( resultant( F, G, X ) );
81    Off(SW_RATIONAL);
82    return res;
83  }
84  // and over Q(a) / Fp(a)
85  else if (( nGetChar()==1 ) /* Q(a) */
86  || (nGetChar() <-1))       /* Fp(a) */
87  {
88    if (nGetChar()==1) setCharacteristic( 0 );
89    else               setCharacteristic( -nGetChar() );
90    poly res;
91    if (currRing->minpoly!=NULL)
92    {
93      CanonicalForm mipo=convSingTrClapP(((lnumber)currRing->minpoly)->z);
94      Variable a=rootOf(mipo);
95      CanonicalForm F( convSingAPClapAP( f,a ) ), G( convSingAPClapAP( g,a ) );
96      res= convClapAPSingAP( resultant( F, G, X ) );
97    }
98    else
99    {
100      CanonicalForm F( convSingTrPClapP( f ) ), G( convSingTrPClapP( g ) );
101      res= convClapPSingTrP( resultant( F, G, X ) );
102    }
103    Off(SW_RATIONAL);
104    return res;
105  }
106  else
107    WerrorS( "not implemented" );
108  return NULL;
109}
110
111lists singclap_extgcd ( poly f, poly g )
112{
113  // for now there is only the possibility to handle univariate
114  // polynomials over
115  // Q and Fp ...
116  poly res=NULL,pa=NULL,pb=NULL;
117  On(SW_SYMMETRIC_FF);
118  if ( nGetChar() == 0 || nGetChar() > 1 )
119  {
120    setCharacteristic( nGetChar() );
121    CanonicalForm F( convSingPClapP( f ) ), G( convSingPClapP( g ) );
122    if (!F.isUnivariate() || !G.isUnivariate() || F.mvar()!=G.mvar())
123    {
124      Off(SW_RATIONAL);
125      WerrorS("not univariate");
126      return NULL;
127    }
128    CanonicalForm Fa,Gb;
129    res=convClapPSingP( extgcd( F, G, Fa, Gb ) );
130    pa=convClapPSingP(Fa);
131    pb=convClapPSingP(Gb);
132    Off(SW_RATIONAL);
133  }
134  // and over Q(a) / Fp(a)
135  else if (( nGetChar()==1 ) /* Q(a) */
136  || (nGetChar() <-1))       /* Fp(a) */
137  {
138    if (nGetChar()==1) setCharacteristic( 0 );
139    else               setCharacteristic( -nGetChar() );
140    CanonicalForm Fa,Gb;
141    if (currRing->minpoly!=NULL)
142    {
143      CanonicalForm mipo=convSingTrClapP(((lnumber)currRing->minpoly)->z);
144      Variable a=rootOf(mipo);
145      CanonicalForm F( convSingAPClapAP( f,a ) ), G( convSingAPClapAP( g,a ) );
146      if (!F.isUnivariate() || !G.isUnivariate() || F.mvar()!=G.mvar())
147      {
148        WerrorS("not univariate");
149        return NULL;
150      }
151      res= convClapAPSingAP( extgcd( F, G, Fa, Gb ) );
152      pa=convClapAPSingAP(Fa);
153      pb=convClapAPSingAP(Gb);
154    }
155    else
156    {
157      CanonicalForm F( convSingTrPClapP( f ) ), G( convSingTrPClapP( g ) );
158      if (!F.isUnivariate() || !G.isUnivariate() || F.mvar()!=G.mvar())
159      {
160        Off(SW_RATIONAL);
161        WerrorS("not univariate");
162        return NULL;
163      }
164      res= convClapPSingTrP( extgcd( F, G, Fa, Gb ) );
165      pa=convClapPSingTrP(Fa);
166      pb=convClapPSingTrP(Gb);
167    }
168    Off(SW_RATIONAL);
169  }
170  else
171  {
172    WerrorS( "not implemented" );
173    return NULL;
174  }
175  lists L=(lists)Alloc(sizeof(slists));
176  L->Init(3);
177  L->m[0].rtyp=POLY_CMD;
178  L->m[0].data=(void *)res;
179  L->m[1].rtyp=POLY_CMD;
180  L->m[1].data=(void *)pa;
181  L->m[2].rtyp=POLY_CMD;
182  L->m[2].data=(void *)pb;
183  return L;
184}
185
186poly singclap_pdivide ( poly f, poly g )
187{
188  // for now there is only the possibility to handle polynomials over
189  // Q and Fp ...
190  if ( nGetChar() == 0 || nGetChar() > 1 )
191  {
192    setCharacteristic( nGetChar() );
193    CanonicalForm F( convSingPClapP( f ) ), G( convSingPClapP( g ) );
194    return convClapPSingP( F / G );
195  }
196  // and over Q(a) / Fp(a)
197  else if (( nGetChar()==1 ) /* Q(a) */
198  || (nGetChar() <-1))       /* Fp(a) */
199  {
200    if (nGetChar()==1) setCharacteristic( 0 );
201    else               setCharacteristic( -nGetChar() );
202    poly res;
203    if (currRing->minpoly!=NULL)
204    {
205      CanonicalForm mipo=convSingTrClapP(((lnumber)currRing->minpoly)->z);
206      Variable a=rootOf(mipo);
207      CanonicalForm F( convSingAPClapAP( f,a ) ), G( convSingAPClapAP( g,a ) );
208      res= convClapAPSingAP(  F / G  );
209    }
210    else
211    {
212      CanonicalForm F( convSingTrPClapP( f ) ), G( convSingTrPClapP( g ) );
213      res= convClapPSingTrP(  F / G  );
214    }
215    Off(SW_RATIONAL);
216    return res;
217  }
218  else
219    WerrorS( "not implemented" );
220  return NULL;
221}
222
223void singclap_divide_content ( poly f )
224{
225  if ( nGetChar() == 1 )
226    setCharacteristic( 0 );
227  else  if ( nGetChar() < 0 )
228    setCharacteristic( -nGetChar() );
229  else
230    setCharacteristic( nGetChar() );
231  if ( f==NULL )
232  {
233    return;
234  }
235  else  if ( pNext( f ) == NULL )
236  {
237    pSetCoeff( f, nInit( 1 ) );
238      pTest(f);
239    return;
240  }
241  else
242  {
243    CFList L;
244    CanonicalForm g, h;
245    poly p = pNext(f);
246    g = convSingTrClapP( ((lnumber)pGetCoeff(f))->z );
247    L.append( g );
248    while ( p && (g != 1) )
249    {
250      h = convSingTrClapP( ((lnumber)pGetCoeff(p))->z );
251      p = pNext( p );
252      g = gcd( g, h );
253      L.append( h );
254    }
255    if ( g == 1 )
256    {
257      pTest(f);
258      return;
259    } 
260    else
261    {
262      CFListIterator i;
263      for ( i = L, p = f; i.hasItem(); i++, p=pNext(p) )
264      {
265        lnumber c=(lnumber)pGetCoeff(p);
266        napDelete(&c->z);
267        c->z=convClapPSingTr( i.getItem() / g );
268      }
269    }
270    pTest(f);
271  }
272}
273
274ideal singclap_factorize ( poly f, intvec ** v , int with_exps)
275{
276  // with_exps: 1 return only true factors
277  //            2 return true factors and exponents
278  //            0 return factors and exponents
279
280  ideal res=NULL;
281  Off(SW_RATIONAL);
282  On(SW_SYMMETRIC_FF);
283  CFFList L;
284  if ( (nGetChar() == 0) || (nGetChar() > 1) )
285  {
286    setCharacteristic( nGetChar() );
287    if (nGetChar()==0) /* Q */
288    {
289      pContent(f);
290    }
291    CanonicalForm F( convSingPClapP( f ) );
292    if (nGetChar()==0) /* Q */
293    {
294      L = factorize( F );
295    }
296    else /* Fp */
297    {
298#ifdef HAVE_LIBFAC_P
299      L = Factorize( F );
300#else
301      return NULL;
302#endif
303    }
304  }
305  // and over Q(a) / Fp(a)
306  else if (( nGetChar()==1 ) /* Q(a) */
307  || (nGetChar() <-1))       /* Fp(a) */
308  {
309    if (nGetChar()==1) setCharacteristic( 0 );
310    else               setCharacteristic( -nGetChar() );
311    if (currRing->minpoly!=NULL)
312    {
313      //if (nGetChar()==1)
314      //{
315      //  WerrorS("not implemented");
316      //  return NULL;
317      //}
318      CanonicalForm mipo=convSingTrClapP(((lnumber)currRing->minpoly)->z);
319      Variable a=rootOf(mipo);
320      CanonicalForm F( convSingAPClapAP( f,a ) );
321      L = factorize( F, a );
322    }
323    else
324    {
325      CanonicalForm F( convSingTrPClapP( f ) );
326      if (nGetChar()==1) /* Q(a) */
327      {
328        L = factorize( F );
329      }
330      else /* Fp(a) */
331      {
332#ifdef HAVE_LIBFAC_P
333        L = Factorize( F );
334#else
335        return NULL;
336#endif
337      }
338    }
339  }
340  else
341  {
342    WerrorS( "not implemented" );
343    goto end;
344  }
345  {
346    // the first factor should be a constant
347    if ( getNumVars(L.getFirst().factor()) != 0 )
348      L.insert(CFFactor(1,1));
349    // convert into ideal
350    int n = L.length();
351    CFFListIterator J=L;
352    int j=0;
353    if (with_exps!=1)
354    {
355      if (with_exps==2)
356      {
357        n--;
358        J++;
359      }
360      *v = new intvec( n );
361    }
362    res = idInit( n ,1);
363    for ( ; J.hasItem(); J++, j++ )
364    {
365      if (with_exps!=1) (**v)[j] = J.getItem().exp();
366      if ((nGetChar()==0)||(nGetChar()>1))           /* Q, Fp */
367        res->m[j] = convClapPSingP( J.getItem().factor() );
368      else if ((nGetChar()==1)||(nGetChar()<-1))     /* Q(a), Fp(a) */
369      {
370        if (currRing->minpoly==NULL)
371          res->m[j] = convClapPSingTrP( J.getItem().factor() );
372        else
373          res->m[j] = convClapAPSingAP( J.getItem().factor() );
374      }
375    }
376    // delete constants
377    if ((with_exps!=0) && (res!=NULL))
378    {
379      int i=IDELEMS(res)-1;
380      for(;i>=0;i--)
381      {
382        if (pIsConstant(res->m[i]))
383          pDelete(&(res->m[i]));
384      }
385      idSkipZeroes(res);
386      if (res->m[0]==NULL)
387      {
388        res->m[0]=pOne();
389      }
390    }
391  }
392end:
393  return res;
394}
395
396matrix singclap_irrCharSeries ( ideal I)
397{
398#ifdef HAVE_LIBFAC_P
399  // for now there is only the possibility to handle polynomials over
400  // Q and Fp ...
401  matrix res=NULL;
402  int i;
403  Off(SW_RATIONAL);
404  On(SW_SYMMETRIC_FF);
405  CFList L;
406  ListCFList LL;
407  if ( (nGetChar() == 0) || (nGetChar() > 1) )
408  {
409    setCharacteristic( nGetChar() );
410    for(i=0;i<IDELEMS(I);i++)
411    {
412      L.append(convSingPClapP(I->m[i]));
413    }
414  }
415  // and over Q(a) / Fp(a)
416  else if (( nGetChar()==1 ) /* Q(a) */
417  || (nGetChar() <-1))       /* Fp(a) */
418  {
419    if (nGetChar()==1) setCharacteristic( 0 );
420    else               setCharacteristic( -nGetChar() );
421    for(i=0;i<IDELEMS(I);i++)
422    {
423      L.append(convSingTrPClapP(I->m[i]));
424    }
425  }
426  else
427  {
428    WerrorS("not implemented");
429    return res;
430  }
431
432  LL=IrrCharSeries(L);
433  int m= LL.length(); // Anzahl Zeilen
434  int n=0;
435  ListIterator<CFList> LLi;
436  CFListIterator Li;
437  for ( LLi = LL; LLi.hasItem(); LLi++ )
438  {
439    n = max(LLi.getItem().length(),n);
440  }
441  res=mpNew(m,n);
442  if ((m==0) || (n==0))
443  {
444    Warn("char_series returns %d x %d matrix from %d input polys (%d)\n",m,n,IDELEMS(I)+1,LL.length());
445    iiWriteMatrix((matrix)I,"I",2,0);
446  }
447  for ( m=1, LLi = LL; LLi.hasItem(); LLi++, m++ )
448  {
449    for (n=1, Li = LLi.getItem(); Li.hasItem(); Li++, n++)
450    {
451      if ( (nGetChar() == 0) || (nGetChar() > 1) )
452        MATELEM(res,m,n)=convClapPSingP(Li.getItem());
453      else
454        MATELEM(res,m,n)=convClapPSingTrP(Li.getItem());
455    }
456  }
457  Off(SW_RATIONAL);
458  return res;
459#else
460  return NULL;
461#endif
462}
463
464char* singclap_neworder ( ideal I)
465{
466#ifdef HAVE_LIBFAC_P
467  int i;
468  Off(SW_RATIONAL);
469  On(SW_SYMMETRIC_FF);
470  CFList L;
471  if ( (nGetChar() == 0) || (nGetChar() > 1) )
472  {
473    setCharacteristic( nGetChar() );
474    for(i=0;i<IDELEMS(I);i++)
475    {
476      L.append(convSingPClapP(I->m[i]));
477    }
478  }
479  // and over Q(a) / Fp(a)
480  else if (( nGetChar()==1 ) /* Q(a) */
481  || (nGetChar() <-1))       /* Fp(a) */
482  {
483    if (nGetChar()==1) setCharacteristic( 0 );
484    else               setCharacteristic( -nGetChar() );
485    for(i=0;i<IDELEMS(I);i++)
486    {
487      L.append(convSingTrPClapP(I->m[i]));
488    }
489  }
490  else
491  {
492    WerrorS("not implemented");
493    return NULL;
494  }
495
496  List<int> IL=neworderint(L);
497  ListIterator<int> Li;
498  StringSet("");
499  Li = IL;
500  int* mark=(int*)Alloc0(pVariables*sizeof(int));
501  int cnt=pVariables;
502  loop
503  {
504    i=Li.getItem()-1;
505    mark[i]=1;
506    StringAppend(currRing->names[i]);
507    Li++;
508    cnt--;
509    if(cnt==0) break;
510    StringAppend(",");
511    if(! Li.hasItem()) break;
512  }
513  for(i=0;i<pVariables;i++)
514  {
515    if(mark[i]==0)
516    {
517      StringAppend(currRing->names[i]);
518      cnt--;
519      if(cnt==0) break;
520      StringAppend(",");
521    }
522  }
523  return mstrdup(StringAppend(""));
524#else
525  return NULL;
526#endif
527}
528
529BOOLEAN singclap_isSqrFree(poly f)
530{
531  BOOLEAN b=FALSE;
532  Off(SW_RATIONAL);
533  //  Q / Fp
534  if ( (nGetChar() == 0) || (nGetChar() > 1) )
535  {
536    setCharacteristic( nGetChar() );
537    CanonicalForm F( convSingPClapP( f ) );
538    if((nGetChar()>1)&&(!F.isUnivariate()))
539      goto err;
540    b=(BOOLEAN)isSqrFree(F);
541  }
542  // and over Q(a) / Fp(a)
543  else if (( nGetChar()==1 ) /* Q(a) */
544  || (nGetChar() <-1))       /* Fp(a) */
545  {
546    if (nGetChar()==1) setCharacteristic( 0 );
547    else               setCharacteristic( -nGetChar() );
548    //if (currRing->minpoly!=NULL)
549    //{
550    //  CanonicalForm mipo=convSingTrClapP(((lnumber)currRing->minpoly)->z);
551    //  Variable a=rootOf(mipo);
552    //  CanonicalForm F( convSingAPClapAP( f,a ) );
553    //  ...
554    //}
555    //else
556    {
557      CanonicalForm F( convSingTrPClapP( f ) );
558      b=(BOOLEAN)isSqrFree(F);
559    }
560    Off(SW_RATIONAL);
561  }
562  else
563  {
564err:
565    WerrorS( "not implemented" );
566  }
567  return b;
568}
569
570poly singclap_det( const matrix m )
571{
572  poly res=NULL;
573  if ( nGetChar() == 0 || nGetChar() > 1 )
574  {
575    setCharacteristic( nGetChar() );
576    CFMatrix M(m->rows(),m->cols());
577    int i,j;
578    for(i=1;i<=m->rows();i++)
579    {
580      for(j=1;j<=m->cols();j++)
581      {
582        M(i,j)=convSingPClapP(MATELEM(m,i,j));
583      }
584    }
585    res= convClapPSingP( determinant(M,m->rows())) ;
586  }
587  // and over Q(a) / Fp(a)
588  else if (( nGetChar()==1 ) /* Q(a) */
589  || (nGetChar() <-1))       /* Fp(a) */
590  {
591    if (nGetChar()==1) setCharacteristic( 0 );
592    else               setCharacteristic( -nGetChar() );
593    CFMatrix M(m->rows(),m->cols());
594    poly res;
595    if (currRing->minpoly!=NULL)
596    {
597      CanonicalForm mipo=convSingTrClapP(((lnumber)currRing->minpoly)->z);
598      Variable a=rootOf(mipo);
599      int i,j;
600      for(i=1;i<=m->rows();i++)
601      {
602        for(j=1;j<=m->cols();j++)
603        {
604          M(i,j)=convSingAPClapAP(MATELEM(m,i,j),a);
605        }
606      }
607      res= convClapAPSingAP( determinant(M,m->rows())) ;
608    }
609    else
610    {
611      int i,j;
612      for(i=1;i<=m->rows();i++)
613      {
614        for(j=1;j<=m->cols();j++)
615        {
616          M(i,j)=convSingTrPClapP(MATELEM(m,i,j));
617        }
618      }
619      res= convClapPSingTrP( determinant(M,m->rows()));
620    }
621  }
622  else
623    WerrorS( "not implemented" );
624  Off(SW_RATIONAL);
625  return res;
626}
627
628int singclap_det_i( intvec * m )
629{
630  setCharacteristic( 0 );
631  CFMatrix M(m->rows(),m->cols());
632  int i,j;
633  for(i=1;i<=m->rows();i++)
634  {
635    for(j=1;j<=m->cols();j++)
636    {
637      M(i,j)=IMATELEM(*m,i,j);
638    }
639  }
640  int res= convClapISingI( determinant(M,m->rows())) ;
641  Off(SW_RATIONAL);
642  return res;
643}
644/*==============================================================*/
645/* interpreter interface : */
646BOOLEAN jjGCD_P(leftv res, leftv u, leftv v)
647{
648  res->data=(void *)singclap_gcd((poly)(u->Data()),((poly)v->Data()));
649  return FALSE;
650}
651
652BOOLEAN jjFAC_P(leftv res, leftv u)
653{
654  intvec *v=NULL;
655  ideal f=singclap_factorize((poly)(u->Data()), &v, 0);
656#ifndef HAVE_LIBFAC_P
657  if (f==NULL) return TRUE;
658#endif
659  lists l=(lists)Alloc(sizeof(slists));
660  l->Init(2);
661  l->m[0].rtyp=IDEAL_CMD;
662  l->m[0].data=(void *)f;
663  l->m[1].rtyp=INTVEC_CMD;
664  l->m[1].data=(void *)v;
665  res->data=(void *)l;
666  return FALSE;
667}
668
669BOOLEAN jjSQR_FREE_DEC(leftv res, leftv u,leftv dummy)
670{
671  intvec *v=NULL;
672  int sw=(int)dummy->Data();
673  ideal f=singclap_factorize((poly)(u->Data()), &v, sw);
674  switch(sw)
675  {
676    case 0:
677    case 2:
678    {
679      lists l=(lists)Alloc(sizeof(slists));
680      l->Init(2);
681      l->m[0].rtyp=IDEAL_CMD;
682      l->m[0].data=(void *)f;
683      l->m[1].rtyp=INTVEC_CMD;
684      l->m[1].data=(void *)v;
685      res->data=(void *)l;
686      res->rtyp=LIST_CMD;
687      return FALSE;
688    }
689    case 1:
690      res->data=(void *)f;
691      return f==NULL;
692  }
693  WerrorS("invalid switch");
694  return TRUE;
695}
696
697#if 0
698BOOLEAN jjIS_SQR_FREE(leftv res, leftv u)
699{
700  BOOLEAN b=singclap_factorize((poly)(u->Data()), &v, 0);
701  res->data=(void *)b;
702}
703#endif
704
705BOOLEAN jjEXTGCD_P(leftv res, leftv u, leftv v)
706{
707  res->data=singclap_extgcd((poly)u->Data(),(poly)v->Data());
708  return (res->data==NULL);
709}
710BOOLEAN jjRESULTANT(leftv res, leftv u, leftv v, leftv w)
711{
712  res->data=singclap_resultant((poly)u->Data(),(poly)v->Data(), (poly)w->Data());
713  return (res->data==NULL);
714}
715BOOLEAN jjCHARSERIES(leftv res, leftv u)
716{
717  res->data=singclap_irrCharSeries((ideal)u->Data());
718  return (res->data==NULL);
719}
720#endif
Note: See TracBrowser for help on using the repository browser.