source: git/Singular/fglm.cc @ d47e6f

fieker-DuValspielwiese
Last change on this file since d47e6f was 416465, checked in by Olaf Bachmann <obachman@…>, 25 years ago
* bug-fixes from work with Thomas git-svn-id: file:///usr/local/Singular/svn/trunk@3826 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 13.3 KB
Line 
1// emacs edit mode for this file is -*- C++ -*-
2// $Id: fglm.cc,v 1.19 1999-11-15 17:19:59 obachman 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};
45
46// Has to be called, if currQuotient != NULL. ( i.e. qring-case )
47// Then a new ideal is build, consisting of the generators of sourceIdeal
48// and the generators of currQuotient, which are completely reduced by
49// the sourceIdeal. This means: If sourceIdeal is reduced, then the new
50// ideal will be reduced as well.
51// Assumes that currRing == sourceRing
52ideal fglmUpdatesource( const ideal sourceIdeal )
53{
54    int k, l, offset;
55    BOOLEAN found;
56    ideal newSource= idInit( IDELEMS( sourceIdeal ) + IDELEMS( currQuotient ), 1 );
57    for ( k= IDELEMS( sourceIdeal )-1; k >=0; k-- )
58        (newSource->m)[k]= pCopy( (sourceIdeal->m)[k] );
59    offset= IDELEMS( sourceIdeal );
60    for ( l= IDELEMS( currQuotient )-1; l >= 0; l-- ) {
61        if ( (currQuotient->m)[l] != NULL ) {
62            found= FALSE;
63            for ( k= IDELEMS( sourceIdeal )-1; (k >= 0) && (found == FALSE); k-- )
64                if ( pDivisibleBy( (sourceIdeal->m)[k], (currQuotient->m)[l] ) )
65                    found= TRUE;
66            if ( ! found ) {
67                (newSource->m)[offset]= pCopy( (currQuotient->m)[l] );
68                offset++;
69            }
70        }
71    }
72    idSkipZeroes( newSource );
73    return newSource;
74}
75
76// Has to be called, if currQuotient != NULL, i.e. in qring-case.
77// Gets rid of the elements of result which are contained in
78// currQuotient and skips Zeroes.
79// Assumes that currRing == destRing
80void
81fglmUpdateresult( ideal & result )
82{
83    int k, l;
84    BOOLEAN found;
85    for ( k= IDELEMS( result )-1; k >=0; k-- ) {
86        if ( (result->m)[k] != NULL ) {
87            found= FALSE;
88            for ( l= IDELEMS( currQuotient )-1; (l >= 0) && ( found == FALSE ); l-- )
89                if ( pDivisibleBy( (currQuotient->m)[l], (result->m)[k] ) )
90                    found= TRUE;
91            if ( found ) pDelete( & ((result->m)[k]) );
92        }
93    }
94    idSkipZeroes( result );
95}
96
97// Checks if the two rings sringHdl and dringHdl are compatible enough to
98// be used for the fglm. This means:
99//  1) Same Characteristic, 2) globalOrderings in both rings,
100//  3) Same number of variables, 4) same number of parameters
101//  5) variables in one ring are permutated variables of the other one
102//  6) parameters in one ring are permutated parameters of the other one
103//  7) either both rings are rings or both rings are qrings
104//  8) if they are qrings, the quotientIdeals of both must coincide.
105// vperm must be a vector of length pVariables+1, initialized by 0.
106// If both rings are compatible, it stores the permutation of the
107// variables if mapped from sringHdl to dringHdl.
108// if the rings are compatible, it returns FglmOk.
109// Should be called with currRing= IDRING( sringHdl );
110FglmState
111fglmConsistency( idhdl sringHdl, idhdl dringHdl, int * vperm )
112{
113    int k;
114    FglmState state= FglmOk;
115    ring dring = IDRING( dringHdl );
116    ring sring = IDRING( sringHdl );
117
118    if ( rChar(sring) != rChar(dring) ) {
119        WerrorS( "rings must have same characteristic" );
120        state= FglmIncompatibleRings;
121    }
122    if ( (sring->OrdSgn != 1) || (dring->OrdSgn != 1) ) {
123        WerrorS( "only works for global orderings" );
124        state= FglmIncompatibleRings;
125    }
126    if ( sring->N != dring->N )
127    {
128        WerrorS( "rings must have same number of variables" );
129        state= FglmIncompatibleRings;
130    }
131    if ( rPar(sring) != rPar(dring) )
132    {
133        WerrorS( "rings must have same number of parameters" );
134        state= FglmIncompatibleRings;
135    }
136    if ( state != FglmOk ) return state;
137    // now the rings have the same number of variables resp. parameters.
138    // check if the names of the variables resp. parameters do agree:
139    int nvar = sring->N;
140    int npar = rPar(sring);
141    int * pperm;
142    if ( npar > 0 )
143        pperm= (int *)Alloc0( (npar+1)*sizeof( int ) );
144    else
145        pperm= NULL;
146    maFindPerm( sring->names, nvar, sring->parameter, npar, 
147                dring->names, nvar, dring->parameter, npar, vperm, pperm, 
148                dring->ch);
149    for ( k= nvar; (k > 0) && (state == FglmOk); k-- )
150        if ( vperm[k] <= 0 ) {
151            WerrorS( "variable names do not agree" );
152            state= FglmIncompatibleRings;
153        }
154    for ( k= npar-1; (k >= 0) && (state == FglmOk); k-- )
155        if ( pperm[k] >= 0 ) {
156            WerrorS( "paramater names do not agree" );
157            state= FglmIncompatibleRings;
158        }
159    Free( (ADDRESS)pperm, (npar+1)*sizeof( int ) );
160    if ( state != FglmOk ) return state;
161    // check if both rings are qrings or not
162    if ( sring->qideal != NULL ) {
163        if ( dring->qideal == NULL ) {
164            Werror( "%s is a qring, current ring not", sringHdl->id );
165            return FglmIncompatibleRings;
166        }
167        // both rings are qrings, now check if both quotients define the same ideal.
168        // check if sring->qideal is contained in dring->qideal:
169        rSetHdl( dringHdl, TRUE );
170        //nSetMap( rInternalChar(sring), sring->parameter, npar, sring->minpoly );
171        nSetMap( sring );
172        ideal sqind = idInit( IDELEMS( sring->qideal ), 1 );
173        for ( k= IDELEMS( sring->qideal )-1; k >= 0; k-- )
174            (sqind->m)[k]= pPermPoly( (sring->qideal->m)[k], vperm, sring);
175        ideal sqindred = kNF( dring->qideal, NULL, sqind );
176        if ( ! idIs0( sqindred ) ) {
177            WerrorS( "the quotients do not agree" );
178            state= FglmIncompatibleRings;
179        }
180        idDelete( & sqind );
181        idDelete( & sqindred );
182        rSetHdl( sringHdl, TRUE );
183        if ( state != FglmOk ) return state;
184        // check if dring->qideal is contained in sring->qideal:
185        int * dsvperm = (int *)Alloc0( (nvar+1)*sizeof( int ) );
186        maFindPerm( dring->names, nvar, NULL, 0, sring->names, nvar, NULL, 0, 
187                    dsvperm, NULL, sring->ch);
188        //nSetMap(rInternalChar(dring), dring->parameter, npar, dring->minpoly);
189        nSetMap(dring);
190        ideal dqins = idInit( IDELEMS( dring->qideal ), 1 );
191        for ( k= IDELEMS( dring->qideal )-1; k >= 0; k-- )
192            (dqins->m)[k]= pPermPoly( (dring->qideal->m)[k], dsvperm, sring);
193        ideal dqinsred = kNF( sring->qideal, NULL, dqins );
194        if ( ! idIs0( dqinsred ) ) {
195            WerrorS( "the quotients do not agree" );
196            state= FglmIncompatibleRings;
197        }
198        idDelete( & dqins );
199        idDelete( & dqinsred );
200        Free( (ADDRESS)dsvperm, (nvar+1)*sizeof( int ) );
201        if ( state != FglmOk ) return state;
202    }
203    else {
204        if ( dring->qideal != NULL ) {
205            Werror( "current ring is a qring, %s not", sringHdl->id );
206            return FglmIncompatibleRings;
207        }
208    }
209    return FglmOk;
210}
211
212// Checks if the ideal "theIdeal" is zero-dimensional and minimal. It does
213//  not check, if it is reduced.
214// returns FglmOk if we can use theIdeal for CalculateFunctionals (this
215//                 function reports an error if theIdeal is not reduced,
216//                 so this need not to be tested here)
217//         FglmNotReduced if theIdeal is not minimal
218//         FglmNotZeroDim if it is not zero-dimensional
219//         FglmHasOne if 1 belongs to theIdeal
220FglmState
221fglmIdealcheck( const ideal theIdeal )
222{
223    FglmState state = FglmOk;
224    int power;
225    int k;
226    BOOLEAN * purePowers = (BOOLEAN *)Alloc( pVariables*sizeof( BOOLEAN ) );
227    for ( k= pVariables-1; k >= 0; k-- )
228        purePowers[k]= FALSE;
229
230    for ( k= IDELEMS( theIdeal ) - 1; (state == FglmOk) && (k >= 0); k-- ) {
231        poly p = (theIdeal->m)[k];
232        if ( pIsConstant( p ) ) state= FglmHasOne;
233        else if ( (power= pIsPurePower( p )) > 0 ) {
234            fglmASSERT( 0 < power && power <= pVariables, "illegal power" );
235            if ( purePowers[power-1] == TRUE  ) state= FglmNotReduced;
236            else purePowers[power-1]= TRUE;
237        }
238        for ( int l = IDELEMS( theIdeal ) - 1; state == FglmOk && l >= 0; l-- )
239            if ( (k != l) && pDivisibleBy( p, (theIdeal->m)[l] ) )
240                state= FglmNotReduced;
241    }
242    if ( state == FglmOk ) {
243        for ( k= pVariables-1 ; (state == FglmOk) && (k >= 0); k-- )
244            if ( purePowers[k] == FALSE ) state= FglmNotZeroDim;
245    }
246    Free( (ADDRESS)purePowers, pVariables*sizeof( BOOLEAN ) );
247    return state;
248}
249
250// The main function for the fglm-Algorithm.
251// Checks the input-data, and calls fglmzero (see fglmzero.cc).
252// Returns the new groebnerbasis or 0 if an error occoured.
253BOOLEAN
254fglmProc( leftv result, leftv first, leftv second )
255{
256    FglmState state = FglmOk;
257
258    idhdl destRingHdl = currRingHdl;
259    ring destRing = currRing;
260    ideal destIdeal = NULL;
261    idhdl sourceRingHdl = (idhdl)first->data;
262    rSetHdl( sourceRingHdl, TRUE );
263    ring sourceRing = currRing;
264
265    int * vperm = (int *)Alloc0( (pVariables+1)*sizeof( int ) );
266    state= fglmConsistency( sourceRingHdl, destRingHdl, vperm );
267    Free( (ADDRESS)vperm, (pVariables+1)*sizeof(int) );
268
269    if ( state == FglmOk ) {
270        idhdl ih = currRing->idroot->get( second->Name(), myynest );
271        if ( (ih != NULL) && (IDTYP(ih)==IDEAL_CMD) ) {
272            ideal sourceIdeal;
273            if ( currQuotient != NULL )
274                sourceIdeal= fglmUpdatesource( IDIDEAL( ih ) );
275            else
276                sourceIdeal = IDIDEAL( ih );
277            state= fglmIdealcheck( sourceIdeal );
278            if ( state == FglmOk ) {
279                // Now the settings are compatible with FGLM
280                assumeStdFlag( (leftv)ih );
281                if ( fglmzero( sourceRingHdl, sourceIdeal, destRingHdl, destIdeal, FALSE, (currQuotient != NULL) ) == FALSE )
282                    state= FglmNotReduced;
283            }
284        } else state= FglmNoIdeal;
285    }
286    if ( currRingHdl != destRingHdl )
287        rSetHdl( destRingHdl, TRUE );
288    switch (state) {
289        case FglmOk:
290            if ( currQuotient != NULL ) fglmUpdateresult( destIdeal );
291            break;
292        case FglmHasOne:
293            destIdeal= idInit(1,1);
294            (destIdeal->m)[0]= pOne();
295            state= FglmOk;
296            break;
297        case FglmIncompatibleRings:
298            Werror( "ring %s and current ring are incompatible", first->Name() );
299            destIdeal= idInit(0,0);
300            break;
301        case FglmNoIdeal:
302            Werror( "Can't find ideal %s in ring %s", second->Name(), first->Name() );
303            destIdeal= idInit(0,0);
304            break;
305        case FglmNotZeroDim:
306            Werror( "The ideal %s has to be 0-dimensional", second->Name() );
307            destIdeal= idInit(0,0);
308            break;
309        case FglmNotReduced:
310            Werror( "The ideal %s has to be reduced", second->Name() );
311            destIdeal= idInit(0,0);
312            break;
313        default:
314            destIdeal= idInit(1,1);
315    }
316
317    result->rtyp = IDEAL_CMD;
318    result->data= (void *)destIdeal;
319    setFlag( result, FLAG_STD );
320    return (state != FglmOk);
321}
322
323// The main function for finduni().
324// Checks the input-data, and calls FindUnivariateWrapper (see fglmzero.cc).
325// Returns an ideal containing the univariate Polynomials or 0 if an error
326// has occoured.
327BOOLEAN
328findUniProc( leftv result, leftv first )
329{
330    ideal sourceIdeal;
331    ideal destIdeal = NULL;
332    FglmState state;
333
334    idhdl sourceIdealHdl = (idhdl)first->data;
335    sourceIdeal= IDIDEAL(sourceIdealHdl);
336
337    assumeStdFlag( first );
338    state= fglmIdealcheck( sourceIdeal );
339    if ( state == FglmOk ) {
340        if ( FindUnivariateWrapper( sourceIdeal, destIdeal ) == FALSE )
341            state = FglmNotReduced;
342    }
343    switch (state) {
344        case FglmOk:
345            break;
346        case FglmHasOne:
347            destIdeal= idInit(1,1);
348            (destIdeal->m)[0]= pOne();
349            state= FglmOk;
350            break;
351        case FglmNotZeroDim:
352            Werror( "The ideal %s has to be 0-dimensional", first->Name() );
353            destIdeal= idInit(0,0);
354            break;
355        case FglmNotReduced:
356            Werror( "The ideal %s has to be reduced", first->Name() );
357            destIdeal= idInit(0,0);
358            break;
359        default:
360            destIdeal= idInit(1,1);
361    }
362
363    result->rtyp = IDEAL_CMD;
364    result->data= (void *)destIdeal;
365
366    return FALSE;
367}
368#endif
369// ----------------------------------------------------------------------------
370// Local Variables: ***
371// compile-command: "make Singular" ***
372// page-delimiter: "^\\(\\|//!\\)" ***
373// fold-internal-margins: nil ***
374// End: ***
Note: See TracBrowser for help on using the repository browser.