source: git/Singular/fglm.cc @ dc0898

fieker-DuValspielwiese
Last change on this file since dc0898 was df83c0, checked in by Tim Wichmann <wichmann@…>, 24 years ago
* added fglmquot git-svn-id: file:///usr/local/Singular/svn/trunk@3905 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 15.3 KB
Line 
1// emacs edit mode for this file is -*- C++ -*-
2// $Id: fglm.cc,v 1.20 1999-11-24 12:29:38 wichmann Exp $
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 "mod2.h"
20
21#ifdef HAVE_FGLM
22#include "tok.h"
23#include "structs.h"
24#include "polys.h"
25#include "ideals.h"
26#include "ring.h"
27#include "ipid.h"
28#include "ipshell.h"
29#include "febase.h"
30#include "maps.h"
31#include "mmemory.h"
32#include "kstd1.h"
33#include "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        if ( (currQuotient->m)[l] != NULL ) {
65            found= FALSE;
66            for ( k= IDELEMS( sourceIdeal )-1; (k >= 0) && (found == FALSE); k-- )
67                if ( pDivisibleBy( (sourceIdeal->m)[k], (currQuotient->m)[l] ) )
68                    found= TRUE;
69            if ( ! found ) {
70                (newSource->m)[offset]= pCopy( (currQuotient->m)[l] );
71                offset++;
72            }
73        }
74    }
75    idSkipZeroes( newSource );
76    return newSource;
77}
78
79// Has to be called, if currQuotient != NULL, i.e. in qring-case.
80// Gets rid of the elements of result which are contained in
81// currQuotient and skips Zeroes.
82// Assumes that currRing == destRing
83void
84fglmUpdateresult( ideal & result )
85{
86    int k, l;
87    BOOLEAN found;
88    for ( k= IDELEMS( result )-1; k >=0; k-- ) {
89        if ( (result->m)[k] != NULL ) {
90            found= FALSE;
91            for ( l= IDELEMS( currQuotient )-1; (l >= 0) && ( found == FALSE ); l-- )
92                if ( pDivisibleBy( (currQuotient->m)[l], (result->m)[k] ) )
93                    found= TRUE;
94            if ( found ) pDelete( & ((result->m)[k]) );
95        }
96    }
97    idSkipZeroes( result );
98}
99
100// Checks if the two rings sringHdl and dringHdl are compatible enough to
101// be used for the fglm. This means:
102//  1) Same Characteristic, 2) globalOrderings in both rings,
103//  3) Same number of variables, 4) same number of parameters
104//  5) variables in one ring are permutated variables of the other one
105//  6) parameters in one ring are permutated parameters of the other one
106//  7) either both rings are rings or both rings are qrings
107//  8) if they are qrings, the quotientIdeals of both must coincide.
108// vperm must be a vector of length pVariables+1, initialized by 0.
109// If both rings are compatible, it stores the permutation of the
110// variables if mapped from sringHdl to dringHdl.
111// if the rings are compatible, it returns FglmOk.
112// Should be called with currRing= IDRING( sringHdl );
113FglmState
114fglmConsistency( idhdl sringHdl, idhdl dringHdl, int * vperm )
115{
116    int k;
117    FglmState state= FglmOk;
118    ring dring = IDRING( dringHdl );
119    ring sring = IDRING( sringHdl );
120
121    if ( rChar(sring) != rChar(dring) ) {
122        WerrorS( "rings must have same characteristic" );
123        state= FglmIncompatibleRings;
124    }
125    if ( (sring->OrdSgn != 1) || (dring->OrdSgn != 1) ) {
126        WerrorS( "only works for global orderings" );
127        state= FglmIncompatibleRings;
128    }
129    if ( sring->N != dring->N )
130    {
131        WerrorS( "rings must have same number of variables" );
132        state= FglmIncompatibleRings;
133    }
134    if ( rPar(sring) != rPar(dring) )
135    {
136        WerrorS( "rings must have same number of parameters" );
137        state= FglmIncompatibleRings;
138    }
139    if ( state != FglmOk ) return state;
140    // now the rings have the same number of variables resp. parameters.
141    // check if the names of the variables resp. parameters do agree:
142    int nvar = sring->N;
143    int npar = rPar(sring);
144    int * pperm;
145    if ( npar > 0 )
146        pperm= (int *)Alloc0( (npar+1)*sizeof( int ) );
147    else
148        pperm= NULL;
149    maFindPerm( sring->names, nvar, sring->parameter, npar, 
150                dring->names, nvar, dring->parameter, npar, vperm, pperm, 
151                dring->ch);
152    for ( k= nvar; (k > 0) && (state == FglmOk); k-- )
153        if ( vperm[k] <= 0 ) {
154            WerrorS( "variable names do not agree" );
155            state= FglmIncompatibleRings;
156        }
157    for ( k= npar-1; (k >= 0) && (state == FglmOk); k-- )
158        if ( pperm[k] >= 0 ) {
159            WerrorS( "paramater names do not agree" );
160            state= FglmIncompatibleRings;
161        }
162    Free( (ADDRESS)pperm, (npar+1)*sizeof( int ) );
163    if ( state != FglmOk ) return state;
164    // check if both rings are qrings or not
165    if ( sring->qideal != NULL ) {
166        if ( dring->qideal == NULL ) {
167            Werror( "%s is a qring, current ring not", sringHdl->id );
168            return FglmIncompatibleRings;
169        }
170        // both rings are qrings, now check if both quotients define the same ideal.
171        // check if sring->qideal is contained in dring->qideal:
172        rSetHdl( dringHdl, TRUE );
173        //nSetMap( rInternalChar(sring), sring->parameter, npar, sring->minpoly );
174        nSetMap( sring );
175        ideal sqind = idInit( IDELEMS( sring->qideal ), 1 );
176        for ( k= IDELEMS( sring->qideal )-1; k >= 0; k-- )
177            (sqind->m)[k]= pPermPoly( (sring->qideal->m)[k], vperm, sring);
178        ideal sqindred = kNF( dring->qideal, NULL, sqind );
179        if ( ! idIs0( sqindred ) ) {
180            WerrorS( "the quotients do not agree" );
181            state= FglmIncompatibleRings;
182        }
183        idDelete( & sqind );
184        idDelete( & sqindred );
185        rSetHdl( sringHdl, TRUE );
186        if ( state != FglmOk ) return state;
187        // check if dring->qideal is contained in sring->qideal:
188        int * dsvperm = (int *)Alloc0( (nvar+1)*sizeof( int ) );
189        maFindPerm( dring->names, nvar, NULL, 0, sring->names, nvar, NULL, 0, 
190                    dsvperm, NULL, sring->ch);
191        //nSetMap(rInternalChar(dring), dring->parameter, npar, dring->minpoly);
192        nSetMap(dring);
193        ideal dqins = idInit( IDELEMS( dring->qideal ), 1 );
194        for ( k= IDELEMS( dring->qideal )-1; k >= 0; k-- )
195            (dqins->m)[k]= pPermPoly( (dring->qideal->m)[k], dsvperm, sring);
196        ideal dqinsred = kNF( sring->qideal, NULL, dqins );
197        if ( ! idIs0( dqinsred ) ) {
198            WerrorS( "the quotients do not agree" );
199            state= FglmIncompatibleRings;
200        }
201        idDelete( & dqins );
202        idDelete( & dqinsred );
203        Free( (ADDRESS)dsvperm, (nvar+1)*sizeof( int ) );
204        if ( state != FglmOk ) return state;
205    }
206    else {
207        if ( dring->qideal != NULL ) {
208            Werror( "current ring is a qring, %s not", sringHdl->id );
209            return FglmIncompatibleRings;
210        }
211    }
212    return FglmOk;
213}
214
215// Checks if the ideal "theIdeal" is zero-dimensional and minimal. It does
216//  not check, if it is reduced.
217// returns FglmOk if we can use theIdeal for CalculateFunctionals (this
218//                 function reports an error if theIdeal is not reduced,
219//                 so this need not to be tested here)
220//         FglmNotReduced if theIdeal is not minimal
221//         FglmNotZeroDim if it is not zero-dimensional
222//         FglmHasOne if 1 belongs to theIdeal
223FglmState
224fglmIdealcheck( const ideal theIdeal )
225{
226    FglmState state = FglmOk;
227    int power;
228    int k;
229    BOOLEAN * purePowers = (BOOLEAN *)Alloc( pVariables*sizeof( BOOLEAN ) );
230    for ( k= pVariables-1; k >= 0; k-- )
231        purePowers[k]= FALSE;
232
233    for ( k= IDELEMS( theIdeal ) - 1; (state == FglmOk) && (k >= 0); k-- ) {
234        poly p = (theIdeal->m)[k];
235        if ( pIsConstant( p ) ) state= FglmHasOne;
236        else if ( (power= pIsPurePower( p )) > 0 ) {
237            fglmASSERT( 0 < power && power <= pVariables, "illegal power" );
238            if ( purePowers[power-1] == TRUE  ) state= FglmNotReduced;
239            else purePowers[power-1]= TRUE;
240        }
241        for ( int l = IDELEMS( theIdeal ) - 1; state == FglmOk && l >= 0; l-- )
242            if ( (k != l) && pDivisibleBy( p, (theIdeal->m)[l] ) )
243                state= FglmNotReduced;
244    }
245    if ( state == FglmOk ) {
246        for ( k= pVariables-1 ; (state == FglmOk) && (k >= 0); k-- )
247            if ( purePowers[k] == FALSE ) state= FglmNotZeroDim;
248    }
249    Free( (ADDRESS)purePowers, pVariables*sizeof( BOOLEAN ) );
250    return state;
251}
252
253// The main function for the fglm-Algorithm.
254// Checks the input-data, and calls fglmzero (see fglmzero.cc).
255// Returns the new groebnerbasis or 0 if an error occoured.
256BOOLEAN
257fglmProc( leftv result, leftv first, leftv second )
258{
259    FglmState state = FglmOk;
260
261    idhdl destRingHdl = currRingHdl;
262    ring destRing = currRing;
263    ideal destIdeal = NULL;
264    idhdl sourceRingHdl = (idhdl)first->data;
265    rSetHdl( sourceRingHdl, TRUE );
266    ring sourceRing = currRing;
267
268    int * vperm = (int *)Alloc0( (pVariables+1)*sizeof( int ) );
269    state= fglmConsistency( sourceRingHdl, destRingHdl, vperm );
270    Free( (ADDRESS)vperm, (pVariables+1)*sizeof(int) );
271
272    if ( state == FglmOk ) {
273        idhdl ih = currRing->idroot->get( second->Name(), myynest );
274        if ( (ih != NULL) && (IDTYP(ih)==IDEAL_CMD) ) {
275            ideal sourceIdeal;
276            if ( currQuotient != NULL )
277                sourceIdeal= fglmUpdatesource( IDIDEAL( ih ) );
278            else
279                sourceIdeal = IDIDEAL( ih );
280            state= fglmIdealcheck( sourceIdeal );
281            if ( state == FglmOk ) {
282                // Now the settings are compatible with FGLM
283                assumeStdFlag( (leftv)ih );
284                if ( fglmzero( sourceRingHdl, sourceIdeal, destRingHdl, destIdeal, FALSE, (currQuotient != NULL) ) == FALSE )
285                    state= FglmNotReduced;
286            }
287        } else state= FglmNoIdeal;
288    }
289    if ( currRingHdl != destRingHdl )
290        rSetHdl( destRingHdl, TRUE );
291    switch (state) {
292        case FglmOk:
293            if ( currQuotient != NULL ) fglmUpdateresult( destIdeal );
294            break;
295        case FglmHasOne:
296            destIdeal= idInit(1,1);
297            (destIdeal->m)[0]= pOne();
298            state= FglmOk;
299            break;
300        case FglmIncompatibleRings:
301            Werror( "ring %s and current ring are incompatible", first->Name() );
302            destIdeal= idInit(0,0);
303            break;
304        case FglmNoIdeal:
305            Werror( "Can't find ideal %s in ring %s", second->Name(), first->Name() );
306            destIdeal= idInit(0,0);
307            break;
308        case FglmNotZeroDim:
309            Werror( "The ideal %s has to be 0-dimensional", second->Name() );
310            destIdeal= idInit(0,0);
311            break;
312        case FglmNotReduced:
313            Werror( "The ideal %s has to be reduced", second->Name() );
314            destIdeal= idInit(0,0);
315            break;
316        default:
317            destIdeal= idInit(1,1);
318    }
319
320    result->rtyp = IDEAL_CMD;
321    result->data= (void *)destIdeal;
322    setFlag( result, FLAG_STD );
323    return (state != FglmOk);
324}
325
326// fglmQuotProc: Calculate I:f with FGLM methods.
327// Checks the input-data, and calls fglmquot (see fglmzero.cc).
328// Returns the new groebnerbasis if I:f or 0 if an error occoured.
329BOOLEAN
330fglmQuotProc( leftv result, leftv first, leftv second )
331{
332    FglmState state = FglmOk;
333
334    //    STICKYPROT("quotstart\n");
335    ideal sourceIdeal = IDIDEAL( (idhdl)first->data );
336    poly quot = (poly)second->Data();
337    ideal destIdeal = NULL;
338
339    state = fglmIdealcheck( sourceIdeal );
340    if ( state == FglmOk ) {
341      if ( quot == NULL ) state= FglmPolyIsZero;
342      else if ( pIsConstant( quot ) ) state= FglmPolyIsOne;
343    }
344   
345    if ( state == FglmOk ) {
346      assumeStdFlag( first );
347      if ( fglmquot( sourceIdeal, quot, destIdeal ) == FALSE )
348        state= FglmNotReduced;
349    }
350
351    switch (state) {
352        case FglmOk:
353            break;
354        case FglmHasOne:
355            destIdeal= idInit(1,1);
356            (destIdeal->m)[0]= pOne();
357            state= FglmOk;
358            break;
359        case FglmNotZeroDim:
360            Werror( "The ideal %s has to be 0-dimensional", first->Name() );
361            destIdeal= idInit(0,0);
362            break;
363        case FglmNotReduced:
364            Werror( "The poly %s has to be reduced", second->Name() );
365            destIdeal= idInit(0,0);
366            break;
367        case FglmPolyIsOne:
368            int k;
369            destIdeal= idInit( IDELEMS(sourceIdeal), 1 );
370            for ( k= IDELEMS( sourceIdeal )-1; k >=0; k-- )
371              (destIdeal->m)[k]= pCopy( (sourceIdeal->m)[k] );
372            state= FglmOk;
373            break;
374        case FglmPolyIsZero:
375            destIdeal= idInit(1,1);
376            (destIdeal->m)[0]= pOne();
377            state= FglmOk;
378            break;
379        default:
380            destIdeal= idInit(1,1);
381    }
382
383    result->rtyp = IDEAL_CMD;
384    result->data= (void *)destIdeal;
385    setFlag( result, FLAG_STD );
386    // STICKYPROT("quotend\n");
387    return (state != FglmOk);
388} // fglmQuotProt
389
390// The main function for finduni().
391// Checks the input-data, and calls FindUnivariateWrapper (see fglmzero.cc).
392// Returns an ideal containing the univariate Polynomials or 0 if an error
393// has occoured.
394BOOLEAN
395findUniProc( leftv result, leftv first )
396{
397    ideal sourceIdeal;
398    ideal destIdeal = NULL;
399    FglmState state;
400
401    idhdl sourceIdealHdl = (idhdl)first->data;
402    sourceIdeal= IDIDEAL(sourceIdealHdl);
403
404    assumeStdFlag( first );
405    state= fglmIdealcheck( sourceIdeal );
406    if ( state == FglmOk ) {
407        if ( FindUnivariateWrapper( sourceIdeal, destIdeal ) == FALSE )
408            state = FglmNotReduced;
409    }
410    switch (state) {
411        case FglmOk:
412            break;
413        case FglmHasOne:
414            destIdeal= idInit(1,1);
415            (destIdeal->m)[0]= pOne();
416            state= FglmOk;
417            break;
418        case FglmNotZeroDim:
419            Werror( "The ideal %s has to be 0-dimensional", first->Name() );
420            destIdeal= idInit(0,0);
421            break;
422        case FglmNotReduced:
423            Werror( "The ideal %s has to be reduced", first->Name() );
424            destIdeal= idInit(0,0);
425            break;
426        default:
427            destIdeal= idInit(1,1);
428    }
429
430    result->rtyp = IDEAL_CMD;
431    result->data= (void *)destIdeal;
432
433    return FALSE;
434}
435#endif
436// ----------------------------------------------------------------------------
437// Local Variables: ***
438// compile-command: "make Singular" ***
439// page-delimiter: "^\\(\\|//!\\)" ***
440// fold-internal-margins: nil ***
441// End: ***
Note: See TracBrowser for help on using the repository browser.