source: git/Singular/fglm.cc @ 1085d4

spielwiese
Last change on this file since 1085d4 was 711e26, checked in by Burcin Erocal <burcin@…>, 13 years ago
Fix Singular/fglm.cc.
  • Property mode set to 100644
File size: 16.3 KB
Line 
1// emacs edit mode for this file is -*- C++ -*-
2// $Id$
3
4/****************************************
5*  Computer Algebra System SINGULAR     *
6****************************************/
7/*
8* ABSTRACT - The FGLM-Algorithm plus extension
9*   Calculate a reduced groebner basis for one ordering, given a
10*   reduced groebner basis for another ordering.
11*   In this file the input is checked. Furthermore we decide, if
12*   the input is 0-dimensional ( then fglmzero.cc is used ) or
13*   if the input is homogeneous ( then fglmhom.cc is used. Yet
14*   not implemented ).
15*   The extension (finduni) finds minimal univariate Polynomials
16*   lying in a 0-dimensional ideal.
17*/
18
19#include <kernel/mod2.h>
20
21#ifdef HAVE_FACTORY
22#include <Singular/tok.h>
23#include <misc/options.h>
24#include <polys/polys.h>
25#include <kernel/ideals.h>
26#include <polys/monomials/ring.h>
27#include <Singular/ipid.h>
28#include <Singular/ipshell.h>
29#include <kernel/febase.h>
30#include <polys/monomials/maps.h>
31#include <omalloc/omalloc.h>
32#include <kernel/kstd1.h>
33#include <kernel/fglm.h>
34
35// internal Version: 1.18.1.6
36//     enumeration to handle the various errors to occour.
37enum FglmState{
38    FglmOk,
39    FglmHasOne,
40    FglmNoIdeal,
41    FglmNotReduced,
42    FglmNotZeroDim,
43    FglmIncompatibleRings,
44    // for fglmquot:
45    FglmPolyIsOne,
46    FglmPolyIsZero
47};
48
49// Has to be called, if currQuotient != NULL. ( i.e. qring-case )
50// Then a new ideal is build, consisting of the generators of sourceIdeal
51// and the generators of currQuotient, which are completely reduced by
52// the sourceIdeal. This means: If sourceIdeal is reduced, then the new
53// ideal will be reduced as well.
54// Assumes that currRing == sourceRing
55ideal fglmUpdatesource( const ideal sourceIdeal )
56{
57    int k, l, offset;
58    BOOLEAN found;
59    ideal newSource= idInit( IDELEMS( sourceIdeal ) + IDELEMS( currQuotient ), 1 );
60    for ( k= IDELEMS( sourceIdeal )-1; k >=0; k-- )
61        (newSource->m)[k]= pCopy( (sourceIdeal->m)[k] );
62    offset= IDELEMS( sourceIdeal );
63    for ( l= IDELEMS( currQuotient )-1; l >= 0; l-- )
64    {
65        if ( (currQuotient->m)[l] != NULL )
66        {
67            found= FALSE;
68            for ( k= IDELEMS( sourceIdeal )-1; (k >= 0) && (found == FALSE); k-- )
69                if ( pDivisibleBy( (sourceIdeal->m)[k], (currQuotient->m)[l] ) )
70                    found= TRUE;
71            if ( ! found )
72            {
73                (newSource->m)[offset]= pCopy( (currQuotient->m)[l] );
74                offset++;
75            }
76        }
77    }
78    idSkipZeroes( newSource );
79    return newSource;
80}
81
82// Has to be called, if currQuotient != NULL, i.e. in qring-case.
83// Gets rid of the elements of result which are contained in
84// currQuotient and skips Zeroes.
85// Assumes that currRing == destRing
86void
87fglmUpdateresult( ideal & result )
88{
89    int k, l;
90    BOOLEAN found;
91    for ( k= IDELEMS( result )-1; k >=0; k-- )
92    {
93        if ( (result->m)[k] != NULL )
94        {
95            found= FALSE;
96            for ( l= IDELEMS( currQuotient )-1; (l >= 0) && ( found == FALSE ); l-- )
97                if ( pDivisibleBy( (currQuotient->m)[l], (result->m)[k] ) )
98                    found= TRUE;
99            if ( found ) pDelete( & ((result->m)[k]) );
100        }
101    }
102    idSkipZeroes( result );
103}
104
105// Checks if the two rings sringHdl and dringHdl are compatible enough to
106// be used for the fglm. This means:
107//  1) Same Characteristic, 2) globalOrderings in both rings,
108//  3) Same number of variables, 4) same number of parameters
109//  5) variables in one ring are permutated variables of the other one
110//  6) parameters in one ring are permutated parameters of the other one
111//  7) either both rings are rings or both rings are qrings
112//  8) if they are qrings, the quotientIdeals of both must coincide.
113// vperm must be a vector of length pVariables+1, initialized by 0.
114// If both rings are compatible, it stores the permutation of the
115// variables if mapped from sringHdl to dringHdl.
116// if the rings are compatible, it returns FglmOk.
117// Should be called with currRing= IDRING( sringHdl );
118FglmState
119fglmConsistency( idhdl sringHdl, idhdl dringHdl, int * vperm )
120{
121    int k;
122    FglmState state= FglmOk;
123    ring dring = IDRING( dringHdl );
124    ring sring = IDRING( sringHdl );
125
126    if ( rChar(sring) != rChar(dring) )
127    {
128        WerrorS( "rings must have same characteristic" );
129        state= FglmIncompatibleRings;
130    }
131    if ( (sring->OrdSgn != 1) || (dring->OrdSgn != 1) )
132    {
133        WerrorS( "only works for global orderings" );
134        state= FglmIncompatibleRings;
135    }
136    if ( sring->N != dring->N )
137    {
138        WerrorS( "rings must have same number of variables" );
139        state= FglmIncompatibleRings;
140    }
141    if ( rPar(sring) != rPar(dring) )
142    {
143        WerrorS( "rings must have same number of parameters" );
144        state= FglmIncompatibleRings;
145    }
146    if ( state != FglmOk ) return state;
147    // now the rings have the same number of variables resp. parameters.
148    // check if the names of the variables resp. parameters do agree:
149    int nvar = sring->N;
150    int npar = rPar(sring);
151    int * pperm;
152    if ( npar > 0 )
153        pperm= (int *)omAlloc0( (npar+1)*sizeof( int ) );
154    else
155        pperm= NULL;
156    maFindPerm( sring->names, nvar, rParameter(sring), npar,
157                dring->names, nvar, rParameter(dring), npar, vperm, pperm,
158                dring->cf->type);
159    for ( k= nvar; (k > 0) && (state == FglmOk); k-- )
160        if ( vperm[k] <= 0 )
161        {
162            WerrorS( "variable names do not agree" );
163            state= FglmIncompatibleRings;
164        }
165    for ( k= npar-1; (k >= 0) && (state == FglmOk); k-- )
166        if ( pperm[k] >= 0 )
167        {
168            WerrorS( "paramater names do not agree" );
169            state= FglmIncompatibleRings;
170        }
171    if (pperm != NULL) // OB: ????
172      omFreeSize( (ADDRESS)pperm, (npar+1)*sizeof( int ) );
173    if ( state != FglmOk ) return state;
174    // check if both rings are qrings or not
175    if ( sring->qideal != NULL )
176    {
177        if ( dring->qideal == NULL )
178        {
179            Werror( "%s is a qring, current ring not", sringHdl->id );
180            return FglmIncompatibleRings;
181        }
182        // both rings are qrings, now check if both quotients define the same ideal.
183        // check if sring->qideal is contained in dring->qideal:
184        rSetHdl( dringHdl );
185        nMapFunc nMap=n_SetMap(currRing->cf, sring->cf );
186        ideal sqind = idInit( IDELEMS( sring->qideal ), 1 );
187        for ( k= IDELEMS( sring->qideal )-1; k >= 0; k-- )
188          (sqind->m)[k]= p_PermPoly( (sring->qideal->m)[k], vperm, sring,
189                          currRing, nMap);
190        ideal sqindred = kNF( dring->qideal, NULL, sqind );
191        if ( ! idIs0( sqindred ) )
192        {
193            WerrorS( "the quotients do not agree" );
194            state= FglmIncompatibleRings;
195        }
196        idDelete( & sqind );
197        idDelete( & sqindred );
198        rSetHdl( sringHdl );
199        if ( state != FglmOk ) return state;
200        // check if dring->qideal is contained in sring->qideal:
201        int * dsvperm = (int *)omAlloc0( (nvar+1)*sizeof( int ) );
202        maFindPerm( dring->names, nvar, NULL, 0, sring->names, nvar, NULL, 0,
203                    dsvperm, NULL, sring->cf->type);
204        nMap=n_SetMap(currRing->cf, dring->cf);
205        ideal dqins = idInit( IDELEMS( dring->qideal ), 1 );
206        for ( k= IDELEMS( dring->qideal )-1; k >= 0; k-- )
207          (dqins->m)[k]=p_PermPoly( (dring->qideal->m)[k], dsvperm, sring,
208                         currRing, nMap);
209        ideal dqinsred = kNF( sring->qideal, NULL, dqins );
210        if ( ! idIs0( dqinsred ) )
211        {
212            WerrorS( "the quotients do not agree" );
213            state= FglmIncompatibleRings;
214        }
215        idDelete( & dqins );
216        idDelete( & dqinsred );
217        omFreeSize( (ADDRESS)dsvperm, (nvar+1)*sizeof( int ) );
218        if ( state != FglmOk ) return state;
219    }
220    else
221    {
222        if ( dring->qideal != NULL )
223        {
224            Werror( "current ring is a qring, %s not", sringHdl->id );
225            return FglmIncompatibleRings;
226        }
227    }
228    return FglmOk;
229}
230
231// Checks if the ideal "theIdeal" is zero-dimensional and minimal. It does
232//  not check, if it is reduced.
233// returns FglmOk if we can use theIdeal for CalculateFunctionals (this
234//                 function reports an error if theIdeal is not reduced,
235//                 so this need not to be tested here)
236//         FglmNotReduced if theIdeal is not minimal
237//         FglmNotZeroDim if it is not zero-dimensional
238//         FglmHasOne if 1 belongs to theIdeal
239FglmState
240fglmIdealcheck( const ideal theIdeal )
241{
242    FglmState state = FglmOk;
243    int power;
244    int k;
245    BOOLEAN * purePowers = (BOOLEAN *)omAlloc0( currRing->N*sizeof( BOOLEAN ) );
246
247    for ( k= IDELEMS( theIdeal ) - 1; (state == FglmOk) && (k >= 0); k-- )
248    {
249        poly p = (theIdeal->m)[k];
250        if (p!=NULL)
251        {
252          if( pIsConstant( p ) ) state= FglmHasOne;
253          else if ( (power= pIsPurePower( p )) > 0 )
254          {
255            fglmASSERT( 0 < power && power <= currRing->N, "illegal power" );
256            if ( purePowers[power-1] == TRUE  ) state= FglmNotReduced;
257            else purePowers[power-1]= TRUE;
258          }
259          for ( int l = IDELEMS( theIdeal ) - 1; state == FglmOk && l >= 0; l-- )
260            if ( (k != l) && pDivisibleBy( p, (theIdeal->m)[l] ) )
261                state= FglmNotReduced;
262        }
263    }
264    if ( state == FglmOk )
265    {
266        for ( k= currRing->N-1 ; (state == FglmOk) && (k >= 0); k-- )
267            if ( purePowers[k] == FALSE ) state= FglmNotZeroDim;
268    }
269    omFreeSize( (ADDRESS)purePowers, currRing->N*sizeof( BOOLEAN ) );
270    return state;
271}
272
273// The main function for the fglm-Algorithm.
274// Checks the input-data, and calls fglmzero (see fglmzero.cc).
275// Returns the new groebnerbasis or 0 if an error occoured.
276BOOLEAN
277fglmProc( leftv result, leftv first, leftv second )
278{
279    FglmState state = FglmOk;
280
281    idhdl destRingHdl = currRingHdl;
282    ring destRing = currRing;
283    ideal destIdeal = NULL;
284    idhdl sourceRingHdl = (idhdl)first->data;
285    rSetHdl( sourceRingHdl );
286    ring sourceRing = currRing;
287
288    int * vperm = (int *)omAlloc0( (currRing->N+1)*sizeof( int ) );
289    state= fglmConsistency( sourceRingHdl, destRingHdl, vperm );
290    omFreeSize( (ADDRESS)vperm, (currRing->N+1)*sizeof(int) );
291
292    if ( state == FglmOk )
293    {
294        idhdl ih = currRing->idroot->get( second->Name(), myynest );
295        if ( (ih != NULL) && (IDTYP(ih)==IDEAL_CMD) )
296        {
297            ideal sourceIdeal;
298            if ( currQuotient != NULL )
299                sourceIdeal= fglmUpdatesource( IDIDEAL( ih ) );
300            else
301                sourceIdeal = IDIDEAL( ih );
302            state= fglmIdealcheck( sourceIdeal );
303            if ( state == FglmOk )
304            {
305                // Now the settings are compatible with FGLM
306                assumeStdFlag( (leftv)ih );
307                if ( fglmzero( IDRING(sourceRingHdl), sourceIdeal, destRingHdl, destIdeal, FALSE, (currQuotient != NULL) ) == FALSE )
308                    state= FglmNotReduced;
309            }
310        } else state= FglmNoIdeal;
311    }
312    if ( currRingHdl != destRingHdl )
313        rSetHdl( destRingHdl );
314    switch (state)
315    {
316        case FglmOk:
317            if ( currQuotient != NULL ) fglmUpdateresult( destIdeal );
318            break;
319        case FglmHasOne:
320            destIdeal= idInit(1,1);
321            (destIdeal->m)[0]= pOne();
322            state= FglmOk;
323            break;
324        case FglmIncompatibleRings:
325            Werror( "ring %s and current ring are incompatible", first->Name() );
326            destIdeal= idInit(0,0);
327            break;
328        case FglmNoIdeal:
329            Werror( "Can't find ideal %s in ring %s", second->Name(), first->Name() );
330            destIdeal= idInit(0,0);
331            break;
332        case FglmNotZeroDim:
333            Werror( "The ideal %s has to be 0-dimensional", second->Name() );
334            destIdeal= idInit(0,0);
335            break;
336        case FglmNotReduced:
337            Werror( "The ideal %s has to be given by a reduced SB", second->Name() );
338            destIdeal= idInit(0,0);
339            break;
340        default:
341            destIdeal= idInit(1,1);
342    }
343
344    result->rtyp = IDEAL_CMD;
345    result->data= (void *)destIdeal;
346    setFlag( result, FLAG_STD );
347    return (state != FglmOk);
348}
349
350// fglmQuotProc: Calculate I:f with FGLM methods.
351// Checks the input-data, and calls fglmquot (see fglmzero.cc).
352// Returns the new groebnerbasis if I:f or 0 if an error occoured.
353BOOLEAN
354fglmQuotProc( leftv result, leftv first, leftv second )
355{
356    FglmState state = FglmOk;
357
358    //    STICKYPROT("quotstart\n");
359    ideal sourceIdeal = (ideal)first->Data();
360    poly quot = (poly)second->Data();
361    ideal destIdeal = NULL;
362
363    state = fglmIdealcheck( sourceIdeal );
364    if ( state == FglmOk )
365    {
366      if ( quot == NULL ) state= FglmPolyIsZero;
367      else if ( pIsConstant( quot ) ) state= FglmPolyIsOne;
368    }
369
370    if ( state == FglmOk )
371    {
372      assumeStdFlag( first );
373      if ( fglmquot( sourceIdeal, quot, destIdeal ) == FALSE )
374        state= FglmNotReduced;
375    }
376
377    switch (state)
378    {
379        case FglmOk:
380            break;
381        case FglmHasOne:
382            destIdeal= idInit(1,1);
383            (destIdeal->m)[0]= pOne();
384            state= FglmOk;
385            break;
386        case FglmNotZeroDim:
387            Werror( "The ideal %s has to be 0-dimensional", first->Name() );
388            destIdeal= idInit(0,0);
389            break;
390        case FglmNotReduced:
391            Werror( "The poly %s has to be reduced", second->Name() );
392            destIdeal= idInit(0,0);
393            break;
394        case FglmPolyIsOne:
395            int k;
396            destIdeal= idInit( IDELEMS(sourceIdeal), 1 );
397            for ( k= IDELEMS( sourceIdeal )-1; k >=0; k-- )
398              (destIdeal->m)[k]= pCopy( (sourceIdeal->m)[k] );
399            state= FglmOk;
400            break;
401        case FglmPolyIsZero:
402            destIdeal= idInit(1,1);
403            (destIdeal->m)[0]= pOne();
404            state= FglmOk;
405            break;
406        default:
407            destIdeal= idInit(1,1);
408    }
409
410    result->rtyp = IDEAL_CMD;
411    result->data= (void *)destIdeal;
412    setFlag( result, FLAG_STD );
413    // STICKYPROT("quotend\n");
414    return (state != FglmOk);
415} // fglmQuotProt
416
417// The main function for finduni().
418// Checks the input-data, and calls FindUnivariateWrapper (see fglmzero.cc).
419// Returns an ideal containing the univariate Polynomials or 0 if an error
420// has occoured.
421BOOLEAN
422findUniProc( leftv result, leftv first )
423{
424    ideal sourceIdeal;
425    ideal destIdeal = NULL;
426    FglmState state;
427
428    sourceIdeal = (ideal)first->Data();
429
430    assumeStdFlag( first );
431    state= fglmIdealcheck( sourceIdeal );
432    if ( state == FglmOk )
433    {
434      // check for special cases: if the input contains
435      // univariate polys, try to reduce the problem
436      int i,k;
437      int count=0;
438      BOOLEAN * purePowers = (BOOLEAN *)omAlloc0( currRing->N*sizeof( BOOLEAN ) );
439      for ( k= IDELEMS( sourceIdeal ) - 1; k >= 0; k-- )
440      {
441        if((i=pIsUnivariate(sourceIdeal->m[k]))>0)
442        {
443          if (purePowers[i-1]==0)
444          {
445            purePowers[i-1]=k;
446            count++;
447            if (count==currRing->N) break;
448          }
449        }
450      }
451      if (count==currRing->N)
452      {
453        destIdeal=idInit(currRing->N,1);
454        for(k=currRing->N-1; k>=0; k--) destIdeal->m[k]=pCopy(sourceIdeal->m[purePowers[k]]);
455      }
456      omFreeSize((ADDRESS)purePowers, currRing->N*sizeof( BOOLEAN ) );
457      if (destIdeal!=NULL)
458            state = FglmOk;
459      else if ( FindUnivariateWrapper( sourceIdeal, destIdeal ) == FALSE )
460            state = FglmNotReduced;
461    }
462    switch (state)
463    {
464        case FglmOk:
465            break;
466        case FglmHasOne:
467            destIdeal= idInit(1,1);
468            (destIdeal->m)[0]= pOne();
469            state= FglmOk;
470            break;
471        case FglmNotZeroDim:
472            Werror( "The ideal %s has to be 0-dimensional", first->Name() );
473            destIdeal= idInit(0,0);
474            break;
475        case FglmNotReduced:
476            Werror( "The ideal %s has to be reduced", first->Name() );
477            destIdeal= idInit(0,0);
478            break;
479        default:
480            destIdeal= idInit(1,1);
481    }
482
483    result->rtyp = IDEAL_CMD;
484    result->data= (void *)destIdeal;
485
486    return FALSE;
487}
488#endif
489// ----------------------------------------------------------------------------
490// Local Variables: ***
491// compile-command: "make Singular" ***
492// page-delimiter: "^\\(\\|//!\\)" ***
493// fold-internal-margins: nil ***
494// End: ***
Note: See TracBrowser for help on using the repository browser.