source: git/Singular/fglm.cc @ a21f1f

spielwiese
Last change on this file since a21f1f was a21f1f, checked in by Hans Schönemann <hannes@…>, 27 years ago
* wichmann: Changes for compilation under macintosh (iostream removed) git-svn-id: file:///usr/local/Singular/svn/trunk@627 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 11.0 KB
Line 
1// emacs edit mode for this file is -*- C++ -*-
2// $Id: fglm.cc,v 1.10 1997-08-15 08:09:15 Singular Exp $
3
4/****************************************
5*  Computer Algebra System SINGULAR     *
6****************************************/
7/*
8* ABSTRACT - The FGLM-Algorithm
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*/
16
17#include "mod2.h"
18
19#ifdef HAVE_FGLM
20#include "tok.h"
21#include "structs.h"
22#include "polys.h"
23#include "ideals.h"
24#include "ring.h"
25#include "ipid.h"
26#include "ipshell.h"
27#include "febase.h"
28#include "maps.h"
29#include "mmemory.h"
30#include "kstd1.h"   
31#include "fglm.h"
32
33// internal Version: 1.18.1.6
34//     enumeration to handle the various errors to occour.
35enum FglmState{ 
36    FglmOk, 
37    FglmHasOne, 
38    FglmNoIdeal,
39    FglmNotReduced,
40    FglmNotZeroDim, 
41    FglmIncompatibleRings
42};
43
44// Has to be called, if currQuotient != NULL. ( i.e. qring-case )
45// Then a new ideal is build, consisting of the generators of sourceIdeal
46// and the generators of currQuotient, which are completely reduced by
47// the sourceIdeal. This means: If sourceIdeal is reduced, then the new
48// ideal will be reduced as well.
49// Assumes that currRing == sourceRing
50ideal fglmUpdatesource( const ideal sourceIdeal ) 
51{
52    int k, l, offset;
53    BOOLEAN found;
54    ideal newSource= idInit( IDELEMS( sourceIdeal ) + IDELEMS( currQuotient ), 1 );
55    for ( k= IDELEMS( sourceIdeal )-1; k >=0; k-- )
56        (newSource->m)[k]= pCopy( (sourceIdeal->m)[k] );
57    offset= IDELEMS( sourceIdeal );
58    for ( l= IDELEMS( currQuotient )-1; l >= 0; l-- ) {
59        if ( (currQuotient->m)[l] != NULL ) {
60            found= FALSE;
61            for ( k= IDELEMS( sourceIdeal )-1; (k >= 0) && (found == FALSE); k-- )
62                if ( pDivisibleBy( (sourceIdeal->m)[k], (currQuotient->m)[l] ) )
63                    found= TRUE;
64            if ( ! found ) {
65                (newSource->m)[offset]= pCopy( (currQuotient->m)[l] );
66                offset++;
67            }
68        }
69    }
70    idSkipZeroes( newSource );
71    return newSource;
72}
73
74// Has to be called, if currQuotient != NULL, i.e. in qring-case.
75// Gets rid of the elements of result which are contained in
76// currQuotient and skips Zeroes.
77// Assumes that currRing == destRing
78void
79fglmUpdateresult( ideal & result ) 
80{
81    int k, l;
82    BOOLEAN found;
83    for ( k= IDELEMS( result )-1; k >=0; k-- ) {
84        if ( (result->m)[k] != NULL ) {
85            found= FALSE;
86            for ( l= IDELEMS( currQuotient )-1; (l >= 0) && ( found == FALSE ); l-- ) 
87                if ( pDivisibleBy( (currQuotient->m)[l], (result->m)[k] ) )
88                    found= TRUE;
89            if ( found ) pDelete( & ((result->m)[k]) );
90        }
91    }
92    idSkipZeroes( result );
93}
94
95// Checks if the two rings sringHdl and dringHdl are compatible enough to
96// be used for the fglm. This means:
97//  1) Same Characteristic, 2) globalOrderings in both rings,
98//  3) Same number of variables, 4) same number of parameters
99//  5) variables in one ring are permutated variables of the other one
100//  6) parameters in one ring are permutated parameters of the other one
101//  7) either both rings are rings or both rings are qrings
102//  8) if they are qrings, the quotientIdeals of both must coincide.
103// vperm must be a vector of length pVariables+1, initialized by 0.
104// If both rings are compatible, it stores the permutation of the
105// variables if mapped from sringHdl to dringHdl.
106// if the rings are compatible, it returns FglmOk.
107// Should be called with currRing= IDRING( sringHdl );
108FglmState
109fglmConsistency( idhdl sringHdl, idhdl dringHdl, int * vperm ) 
110{
111    int k;
112    FglmState state= FglmOk;
113    ring dring = IDRING( dringHdl );
114    ring sring = IDRING( sringHdl );
115   
116    if ( sring->ch != dring->ch ) {
117        WerrorS( "rings must have same characteristic" );
118        state= FglmIncompatibleRings;
119    }
120    if ( (sring->OrdSgn != 1) || (dring->OrdSgn != 1) ) {
121        WerrorS( "only works for global orderings" );
122        state= FglmIncompatibleRings;
123    }
124    if ( sring->N != dring->N ) {
125        WerrorS( "rings must have same number of variables" );
126        state= FglmIncompatibleRings;
127    }
128    if ( sring->P != dring->P ) {
129        WerrorS( "rings must have same number of parameters" );
130        state= FglmIncompatibleRings;
131    }
132    if ( state != FglmOk ) return state;
133    // now the rings have the same number of variables resp. parameters.
134    // check if the names of the variables resp. parameters do agree:
135    int nvar = sring->N;
136    int npar = sring->P;
137    int * pperm;
138    if ( npar > 0 ) 
139        pperm= (int *)Alloc0( (npar+1)*sizeof( int ) );
140    else
141        pperm= NULL;
142    maFindPerm( sring->names, nvar, sring->parameter, npar, dring->names, nvar, dring->parameter, npar, vperm, pperm );
143    for ( k= nvar; (k > 0) && (state == FglmOk); k-- )
144        if ( vperm[k] <= 0 ) {
145            WerrorS( "variable names do not agree" );
146            state= FglmIncompatibleRings;
147        }
148    for ( k= npar-1; (k >= 0) && (state == FglmOk); k-- )
149        if ( pperm[k] >= 0 ) {
150            WerrorS( "paramater names do not agree" );
151            state= FglmIncompatibleRings;
152        }
153    Free( (ADDRESS)pperm, (npar+1)*sizeof( int ) );
154    if ( state != FglmOk ) return state;
155    // check if both rings are qrings or not
156    if ( sring->qideal != NULL ) {
157        if ( dring->qideal == NULL ) {
158            Werror( "%s is a qring, current ring not", sringHdl->id );
159            return FglmIncompatibleRings;
160        }
161        // both rings are qrings, now check if both quotients define the same ideal.
162        // check if sring->qideal is contained in dring->qideal:
163        rSetHdl( dringHdl, TRUE );
164        nSetMap( sring->ch, sring->parameter, npar, sring->minpoly );
165        ideal sqind = idInit( IDELEMS( sring->qideal ), 1 );
166        for ( k= IDELEMS( sring->qideal )-1; k >= 0; k-- )
167            (sqind->m)[k]= pPermPoly( (sring->qideal->m)[k], vperm, nvar );
168        ideal sqindred = kNF( dring->qideal, NULL, sqind );
169        if ( ! idIs0( sqindred ) ) {
170            WerrorS( "the quotients do not agree" );
171            state= FglmIncompatibleRings;
172        }
173        idDelete( & sqind );
174        idDelete( & sqindred );
175        rSetHdl( sringHdl, TRUE );
176        if ( state != FglmOk ) return state;
177        // check if dring->qideal is contained in sring->qideal:
178        int * dsvperm = (int *)Alloc0( (nvar+1)*sizeof( int ) );
179        maFindPerm( dring->names, nvar, NULL, 0, sring->names, nvar, NULL, 0, dsvperm, NULL );
180        nSetMap( dring->ch, dring->parameter, npar, dring->minpoly );
181        ideal dqins = idInit( IDELEMS( dring->qideal ), 1 );
182        for ( k= IDELEMS( dring->qideal )-1; k >= 0; k-- ) 
183            (dqins->m)[k]= pPermPoly( (dring->qideal->m)[k], dsvperm, nvar );
184        ideal dqinsred = kNF( sring->qideal, NULL, dqins );
185        if ( ! idIs0( dqinsred ) ) {
186            WerrorS( "the quotients do not agree" );
187            state= FglmIncompatibleRings;
188        }
189        idDelete( & dqins );
190        idDelete( & dqinsred );
191        Free( (ADDRESS)dsvperm, (nvar+1)*sizeof( int ) );
192        if ( state != FglmOk ) return state;
193    } 
194    else {
195        if ( dring->qideal != NULL ) {
196            Werror( "current ring is a qring, %s not", sringHdl->id );
197            return FglmIncompatibleRings;
198        }
199    }
200    return FglmOk;
201}
202
203//     Checks if the ideal "theIdeal" is zero-dimensional and minimal. It does
204//      not check, if it is reduced.
205//     returns FglmOk if we can use theIdeal for CalculateFunctionals (this
206//                     function reports an error if theIdeal is not reduced,
207//                     so this need not to be tested here)
208//             FglmNotReduced if theIdeal is not minimal
209//             FglmNotZeroDim if it is not zero-dimensional
210//             FglmHasOne if 1 belongs to theIdeal
211FglmState
212fglmIdealcheck( const ideal theIdeal )
213{
214    FglmState state = FglmOk;
215    int power;
216    int k; 
217    BOOLEAN * purePowers = (BOOLEAN *)Alloc( pVariables*sizeof( BOOLEAN ) );
218    for ( k= pVariables-1; k >= 0; k-- ) 
219        purePowers[k]= FALSE;
220
221    for ( k= IDELEMS( theIdeal ) - 1; (state == FglmOk) && (k >= 0); k-- ) {
222        poly p = (theIdeal->m)[k];
223        if ( pIsConstant( p ) ) state= FglmHasOne;
224        else if ( (power= pIsPurePower( p )) > 0 ) {
225            fglmASSERT( 0 < power && power <= pVariables, "illegal power" );
226            if ( purePowers[power-1] == TRUE  ) state= FglmNotReduced;
227            else purePowers[power-1]= TRUE; 
228        }
229        for ( int l = IDELEMS( theIdeal ) - 1; state == FglmOk && l >= 0; l-- ) 
230            if ( (k != l) && pDivisibleBy( p, (theIdeal->m)[l] ) )
231                state= FglmNotReduced;
232    }
233    if ( state == FglmOk ) {
234        for ( k= pVariables-1 ; (state == FglmOk) && (k >= 0); k-- ) 
235            if ( purePowers[k] == FALSE ) state= FglmNotZeroDim;
236    }
237    Free( (ADDRESS)purePowers, pVariables*sizeof( BOOLEAN ) );
238    return state;
239}
240
241//     the main function for the fglm-Algorithm.
242//     Checks the input-data, calls CalculateFunctionals, handles change
243//     of ring-vars and finaly calls GroebnerViaFunctionals.
244//     returns the new groebnerbasis or 0 if an error occoured.
245BOOLEAN
246fglmProc( leftv result, leftv first, leftv second ) 
247{
248    FglmState state = FglmOk;
249
250    idhdl destRingHdl = currRingHdl;
251    ring destRing = currRing;
252    ideal destIdeal = NULL;
253    idhdl sourceRingHdl = (idhdl)first->data;
254    rSetHdl( sourceRingHdl, TRUE );
255    ring sourceRing = currRing;
256
257    int * vperm = (int *)Alloc0( (pVariables+1)*sizeof( int ) );
258    state= fglmConsistency( sourceRingHdl, destRingHdl, vperm );
259    Free( (ADDRESS)vperm, (pVariables+1)*sizeof(int) );
260
261    if ( state == FglmOk ) {
262        idhdl ih = currRing->idroot->get( second->Name(), myynest );
263        if ( (ih != NULL) && (IDTYP(ih)==IDEAL_CMD) ) {
264            ideal sourceIdeal;
265            if ( currQuotient != NULL ) 
266                sourceIdeal= fglmUpdatesource( IDIDEAL( ih ) );
267            else
268                sourceIdeal = IDIDEAL( ih );
269            state= fglmIdealcheck( sourceIdeal );
270            if ( state == FglmOk ) { 
271                // Now the settings are compatible with FGLM
272                assumeStdFlag( (leftv)ih );
273                if ( fglmzero( sourceRingHdl, sourceIdeal, destRingHdl, destIdeal, FALSE, (currQuotient != NULL) ) == FALSE )
274                    state= FglmNotReduced;
275            }
276        } else state= FglmNoIdeal;
277    }
278    if ( currRingHdl != destRingHdl )
279        rSetHdl( destRingHdl, TRUE );
280    switch (state) {
281        case FglmOk:
282            if ( currQuotient != NULL ) fglmUpdateresult( destIdeal );
283            break;
284        case FglmHasOne:
285            destIdeal= idInit(1,1);
286            (destIdeal->m)[0]= pOne();
287            state= FglmOk;
288            break;
289        case FglmIncompatibleRings:
290            Werror( "ring %s and current ring are incompatible", first->Name() );
291            destIdeal= idInit(0,0);
292            break;
293        case FglmNoIdeal:
294            Werror( "Can't find ideal %s in ring %s", second->Name(), first->Name() );
295            destIdeal= idInit(0,0);
296            break;
297        case FglmNotZeroDim:
298            Werror( "The ideal %s has to be 0-dimensional", second->Name() );
299            destIdeal= idInit(0,0);
300            break;
301        case FglmNotReduced:
302            Werror( "The ideal %s has to be reduced", second->Name() );
303            destIdeal= idInit(0,0);
304            break;
305        default:
306            destIdeal= idInit(1,1);
307    }
308
309    result->rtyp = IDEAL_CMD;
310    result->data= (void *)destIdeal;
311    setFlag( result, FLAG_STD );
312    if ( state == FglmOk )
313        return FALSE;
314    else
315        return TRUE;
316}
317
318#endif
319// ----------------------------------------------------------------------------
320// Local Variables: ***
321// compile-command: "make Singular" ***
322// page-delimiter: "^\\(\\|//!\\)" ***
323// fold-internal-margins: nil ***
324// End: ***
Note: See TracBrowser for help on using the repository browser.