source: git/Singular/fglm.cc @ 94d062

spielwiese
Last change on this file since 94d062 was 94d062, checked in by Hans Schönemann <hannes@…>, 26 years ago
* wichmann: Added error-Checks for findUniProc(). git-svn-id: file:///usr/local/Singular/svn/trunk@1376 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 12.1 KB
Line 
1// emacs edit mode for this file is -*- C++ -*-
2// $Id: fglm.cc,v 1.13 1998-04-14 15:29:46 Singular 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 ( sring->ch != dring->ch ) {
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        WerrorS( "rings must have same number of variables" );
128        state= FglmIncompatibleRings;
129    }
130    if ( sring->P != dring->P ) {
131        WerrorS( "rings must have same number of parameters" );
132        state= FglmIncompatibleRings;
133    }
134    if ( state != FglmOk ) return state;
135    // now the rings have the same number of variables resp. parameters.
136    // check if the names of the variables resp. parameters do agree:
137    int nvar = sring->N;
138    int npar = sring->P;
139    int * pperm;
140    if ( npar > 0 ) 
141        pperm= (int *)Alloc0( (npar+1)*sizeof( int ) );
142    else
143        pperm= NULL;
144    maFindPerm( sring->names, nvar, sring->parameter, npar, dring->names, nvar, dring->parameter, npar, vperm, pperm );
145    for ( k= nvar; (k > 0) && (state == FglmOk); k-- )
146        if ( vperm[k] <= 0 ) {
147            WerrorS( "variable names do not agree" );
148            state= FglmIncompatibleRings;
149        }
150    for ( k= npar-1; (k >= 0) && (state == FglmOk); k-- )
151        if ( pperm[k] >= 0 ) {
152            WerrorS( "paramater names do not agree" );
153            state= FglmIncompatibleRings;
154        }
155    Free( (ADDRESS)pperm, (npar+1)*sizeof( int ) );
156    if ( state != FglmOk ) return state;
157    // check if both rings are qrings or not
158    if ( sring->qideal != NULL ) {
159        if ( dring->qideal == NULL ) {
160            Werror( "%s is a qring, current ring not", sringHdl->id );
161            return FglmIncompatibleRings;
162        }
163        // both rings are qrings, now check if both quotients define the same ideal.
164        // check if sring->qideal is contained in dring->qideal:
165        rSetHdl( dringHdl, TRUE );
166        nSetMap( sring->ch, sring->parameter, npar, sring->minpoly );
167        ideal sqind = idInit( IDELEMS( sring->qideal ), 1 );
168        for ( k= IDELEMS( sring->qideal )-1; k >= 0; k-- )
169            (sqind->m)[k]= pPermPoly( (sring->qideal->m)[k], vperm, sring);
170        ideal sqindred = kNF( dring->qideal, NULL, sqind );
171        if ( ! idIs0( sqindred ) ) {
172            WerrorS( "the quotients do not agree" );
173            state= FglmIncompatibleRings;
174        }
175        idDelete( & sqind );
176        idDelete( & sqindred );
177        rSetHdl( sringHdl, TRUE );
178        if ( state != FglmOk ) return state;
179        // check if dring->qideal is contained in sring->qideal:
180        int * dsvperm = (int *)Alloc0( (nvar+1)*sizeof( int ) );
181        maFindPerm( dring->names, nvar, NULL, 0, sring->names, nvar, NULL, 0, dsvperm, NULL );
182        nSetMap( dring->ch, dring->parameter, npar, dring->minpoly );
183        ideal dqins = idInit( IDELEMS( dring->qideal ), 1 );
184        for ( k= IDELEMS( dring->qideal )-1; k >= 0; k-- ) 
185            (dqins->m)[k]= pPermPoly( (dring->qideal->m)[k], dsvperm, sring);
186        ideal dqinsred = kNF( sring->qideal, NULL, dqins );
187        if ( ! idIs0( dqinsred ) ) {
188            WerrorS( "the quotients do not agree" );
189            state= FglmIncompatibleRings;
190        }
191        idDelete( & dqins );
192        idDelete( & dqinsred );
193        Free( (ADDRESS)dsvperm, (nvar+1)*sizeof( int ) );
194        if ( state != FglmOk ) return state;
195    } 
196    else {
197        if ( dring->qideal != NULL ) {
198            Werror( "current ring is a qring, %s not", sringHdl->id );
199            return FglmIncompatibleRings;
200        }
201    }
202    return FglmOk;
203}
204
205// Checks if the ideal "theIdeal" is zero-dimensional and minimal. It does
206//  not check, if it is reduced.
207// returns FglmOk if we can use theIdeal for CalculateFunctionals (this
208//                 function reports an error if theIdeal is not reduced,
209//                 so this need not to be tested here)
210//         FglmNotReduced if theIdeal is not minimal
211//         FglmNotZeroDim if it is not zero-dimensional
212//         FglmHasOne if 1 belongs to theIdeal
213FglmState
214fglmIdealcheck( const ideal theIdeal )
215{
216    FglmState state = FglmOk;
217    int power;
218    int k; 
219    BOOLEAN * purePowers = (BOOLEAN *)Alloc( pVariables*sizeof( BOOLEAN ) );
220    for ( k= pVariables-1; k >= 0; k-- ) 
221        purePowers[k]= FALSE;
222
223    for ( k= IDELEMS( theIdeal ) - 1; (state == FglmOk) && (k >= 0); k-- ) {
224        poly p = (theIdeal->m)[k];
225        if ( pIsConstant( p ) ) state= FglmHasOne;
226        else if ( (power= pIsPurePower( p )) > 0 ) {
227            fglmASSERT( 0 < power && power <= pVariables, "illegal power" );
228            if ( purePowers[power-1] == TRUE  ) state= FglmNotReduced;
229            else purePowers[power-1]= TRUE; 
230        }
231        for ( int l = IDELEMS( theIdeal ) - 1; state == FglmOk && l >= 0; l-- ) 
232            if ( (k != l) && pDivisibleBy( p, (theIdeal->m)[l] ) )
233                state= FglmNotReduced;
234    }
235    if ( state == FglmOk ) {
236        for ( k= pVariables-1 ; (state == FglmOk) && (k >= 0); k-- ) 
237            if ( purePowers[k] == FALSE ) state= FglmNotZeroDim;
238    }
239    Free( (ADDRESS)purePowers, pVariables*sizeof( BOOLEAN ) );
240    return state;
241}
242
243// The main function for the fglm-Algorithm.
244// Checks the input-data, and calls fglmzero (see fglmzero.cc).
245// Returns the new groebnerbasis or 0 if an error occoured.
246BOOLEAN
247fglmProc( leftv result, leftv first, leftv second ) 
248{
249    FglmState state = FglmOk;
250
251    idhdl destRingHdl = currRingHdl;
252    ring destRing = currRing;
253    ideal destIdeal = NULL;
254    idhdl sourceRingHdl = (idhdl)first->data;
255    rSetHdl( sourceRingHdl, TRUE );
256    ring sourceRing = currRing;
257
258    int * vperm = (int *)Alloc0( (pVariables+1)*sizeof( int ) );
259    state= fglmConsistency( sourceRingHdl, destRingHdl, vperm );
260    Free( (ADDRESS)vperm, (pVariables+1)*sizeof(int) );
261
262    if ( state == FglmOk ) {
263        idhdl ih = currRing->idroot->get( second->Name(), myynest );
264        if ( (ih != NULL) && (IDTYP(ih)==IDEAL_CMD) ) {
265            ideal sourceIdeal;
266            if ( currQuotient != NULL ) 
267                sourceIdeal= fglmUpdatesource( IDIDEAL( ih ) );
268            else
269                sourceIdeal = IDIDEAL( ih );
270            state= fglmIdealcheck( sourceIdeal );
271            if ( state == FglmOk ) { 
272                // Now the settings are compatible with FGLM
273                assumeStdFlag( (leftv)ih );
274                if ( fglmzero( sourceRingHdl, sourceIdeal, destRingHdl, destIdeal, FALSE, (currQuotient != NULL) ) == FALSE )
275                    state= FglmNotReduced;
276            }
277        } else state= FglmNoIdeal;
278    }
279    if ( currRingHdl != destRingHdl )
280        rSetHdl( destRingHdl, TRUE );
281    switch (state) {
282        case FglmOk:
283            if ( currQuotient != NULL ) fglmUpdateresult( destIdeal );
284            break;
285        case FglmHasOne:
286            destIdeal= idInit(1,1);
287            (destIdeal->m)[0]= pOne();
288            state= FglmOk;
289            break;
290        case FglmIncompatibleRings:
291            Werror( "ring %s and current ring are incompatible", first->Name() );
292            destIdeal= idInit(0,0);
293            break;
294        case FglmNoIdeal:
295            Werror( "Can't find ideal %s in ring %s", second->Name(), first->Name() );
296            destIdeal= idInit(0,0);
297            break;
298        case FglmNotZeroDim:
299            Werror( "The ideal %s has to be 0-dimensional", second->Name() );
300            destIdeal= idInit(0,0);
301            break;
302        case FglmNotReduced:
303            Werror( "The ideal %s has to be reduced", second->Name() );
304            destIdeal= idInit(0,0);
305            break;
306        default:
307            destIdeal= idInit(1,1);
308    }
309
310    result->rtyp = IDEAL_CMD;
311    result->data= (void *)destIdeal;
312    setFlag( result, FLAG_STD );
313    if ( state == FglmOk )
314        return FALSE;
315    else
316        return TRUE;
317}
318
319// The main function for finduni().
320// Checks the input-data, and calls FindUnivariateWrapper (see fglmzero.cc).
321// Returns an ideal containing the univariate Polynomials or 0 if an error
322// has occoured.
323BOOLEAN
324findUniProc( leftv result, leftv first ) 
325{
326    ideal sourceIdeal;
327    ideal destIdeal = NULL;
328    FglmState state;
329   
330    idhdl sourceIdealHdl = (idhdl)first->data;
331    sourceIdeal= IDIDEAL(sourceIdealHdl);
332
333    assumeStdFlag( first );
334    state= fglmIdealcheck( sourceIdeal );
335    if ( state == FglmOk ) {
336        if ( FindUnivariateWrapper( sourceIdeal, destIdeal ) == FALSE )
337            state = FglmNotReduced;
338    }
339    switch (state) {
340        case FglmOk:
341            break;
342        case FglmHasOne:
343            destIdeal= idInit(1,1);
344            (destIdeal->m)[0]= pOne();
345            state= FglmOk;
346            break;
347        case FglmNotZeroDim:
348            Werror( "The ideal %s has to be 0-dimensional", first->Name() );
349            destIdeal= idInit(0,0);
350            break;
351        case FglmNotReduced:
352            Werror( "The ideal %s has to be reduced", first->Name() );
353            destIdeal= idInit(0,0);
354            break;
355        default:
356            destIdeal= idInit(1,1);
357    }
358   
359    result->rtyp = IDEAL_CMD;
360    result->data= (void *)destIdeal;
361   
362    return FALSE;
363}
364#endif
365// ----------------------------------------------------------------------------
366// Local Variables: ***
367// compile-command: "make Singular" ***
368// page-delimiter: "^\\(\\|//!\\)" ***
369// fold-internal-margins: nil ***
370// End: ***
Note: See TracBrowser for help on using the repository browser.