source: git/dyn_modules/syzextra/mod_main.cc @ 92992c

jengelh-datetimespielwiese
Last change on this file since 92992c was 92992c, checked in by Oleksandr Motsak <motsak@…>, 11 years ago
moving/adding/changing bits of documentation
  • Property mode set to 100644
File size: 43.3 KB
Line 
1
2
3
4
5#include <kernel/mod2.h>
6
7#include <omalloc/omalloc.h>
8
9#include <misc/intvec.h>
10#include <misc/options.h>
11
12#include <coeffs/coeffs.h>
13
14#include <polys/PolyEnumerator.h>
15
16#include <polys/monomials/p_polys.h>
17#include <polys/monomials/ring.h>
18// #include <kernel/longrat.h>
19#include <kernel/GBEngine/kstd1.h>
20
21#include <kernel/polys.h>
22
23#include <kernel/GBEngine/syz.h>
24
25#include <Singular/tok.h>
26#include <Singular/ipid.h>
27#include <Singular/lists.h>
28#include <Singular/attrib.h>
29
30#include <Singular/ipid.h> 
31#include <Singular/ipshell.h> // For iiAddCproc
32
33// extern coeffs coeffs_BIGINT
34
35#include "singularxx_defs.h"
36
37#include "DebugPrint.h"
38#include "myNF.h"
39#include "syzextra.h"
40
41
42#include <Singular/mod_lib.h>
43
44
45#if GOOGLE_PROFILE_ENABLED
46#include <google/profiler.h>
47#endif // #if GOOGLE_PROFILE_ENABLED
48
49
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53
54
55
56
57extern void pISUpdateComponents(ideal F, const intvec *const V, const int MIN, const ring r);
58// extern ring rCurrRingAssure_SyzComp();
59extern ring rAssure_InducedSchreyerOrdering(const ring r, BOOLEAN complete, int sign);
60extern int rGetISPos(const int p, const ring r);
61
62// USING_NAMESPACE_SINGULARXX;
63
64USING_NAMESPACE( SINGULARXXNAME :: DEBUG )
65USING_NAMESPACE( SINGULARXXNAME :: NF )
66USING_NAMESPACE( SINGULARXXNAME :: SYZEXTRA )
67
68
69BEGIN_NAMESPACE_NONAME
70
71
72static inline void NoReturn(leftv& res)
73{
74  res->rtyp = NONE;
75  res->data = NULL;
76}
77
78/// wrapper around n_ClearContent
79static BOOLEAN _ClearContent(leftv res, leftv h)
80{
81  NoReturn(res);
82
83  const char *usage = "'ClearContent' needs a (non-zero!) poly or vector argument...";
84 
85  if( h == NULL )
86  {
87    WarnS(usage);
88    return TRUE;
89  }
90
91  assume( h != NULL );
92
93  if( !( h->Typ() == POLY_CMD || h->Typ() == VECTOR_CMD) )
94  {
95    WarnS(usage);
96    return TRUE;
97  }
98
99  assume (h->Next() == NULL);
100 
101  poly ph = reinterpret_cast<poly>(h->Data());
102 
103  if( ph == NULL )
104  {
105    WarnS(usage);
106    return TRUE;
107  }
108 
109  const ring r =  currRing;
110  assume( r != NULL ); assume( r->cf != NULL ); const coeffs C = r->cf;
111
112  number n;
113
114  // experimentall (recursive enumerator treatment) of alg. ext
115  CPolyCoeffsEnumerator itr(ph);
116  n_ClearContent(itr, n, C);
117
118  res->data = n;
119  res->rtyp = NUMBER_CMD;
120
121  return FALSE;
122}
123
124/// wrapper around n_ClearDenominators
125static BOOLEAN _ClearDenominators(leftv res, leftv h)
126{
127  NoReturn(res);
128
129  const char *usage = "'ClearDenominators' needs a (non-zero!) poly or vector argument...";
130
131  if( h == NULL )
132  {
133    WarnS(usage);
134    return TRUE;
135  }
136
137  assume( h != NULL );
138
139  if( !( h->Typ() == POLY_CMD || h->Typ() == VECTOR_CMD) )
140  {
141    WarnS(usage);
142    return TRUE;
143  }
144
145  assume (h->Next() == NULL);
146
147  poly ph = reinterpret_cast<poly>(h->Data());
148
149  if( ph == NULL )
150  {
151    WarnS(usage);
152    return TRUE;
153  }
154
155  const ring r =  currRing;
156  assume( r != NULL ); assume( r->cf != NULL ); const coeffs C = r->cf;
157
158  number n;
159
160  // experimentall (recursive enumerator treatment) of alg. ext.
161  CPolyCoeffsEnumerator itr(ph);
162  n_ClearDenominators(itr, n, C);
163
164  res->data = n;
165  res->rtyp = NUMBER_CMD;
166
167  return FALSE;
168}
169
170
171/// try to get an optional (simple) integer argument out of h
172/// or return the default value
173static int getOptionalInteger(const leftv& h, const int _n)
174{
175  if( h!= NULL && h->Typ() == INT_CMD )
176  {
177    int n = (int)(long)(h->Data());
178
179    if( n < 0 )
180      Warn("Negative (%d) optional integer argument", n);
181
182    return (n);
183  }
184
185  return (_n); 
186}
187
188static BOOLEAN noop(leftv __res, leftv /*__v*/)
189{
190  NoReturn(__res);
191  return FALSE;
192}
193
194static BOOLEAN _ProfilerStart(leftv __res, leftv h)
195{
196  NoReturn(__res);
197#if GOOGLE_PROFILE_ENABLED
198  if( h!= NULL && h->Typ() == STRING_CMD )
199  {
200    const char* name = (char*)(h->Data());
201    assume( name != NULL );   
202    ProfilerStart(name);
203  } else
204    WerrorS("ProfilerStart requires a string [name] argument"); 
205#else
206  WarnS("Sorry no google profiler support (GOOGLE_PROFILE_ENABLE!=1)...");
207//  return TRUE; // ?
208#endif // #if GOOGLE_PROFILE_ENABLED
209  return FALSE;
210  (void)h;
211}
212static BOOLEAN _ProfilerStop(leftv __res, leftv /*__v*/)
213{
214  NoReturn(__res);
215#if GOOGLE_PROFILE_ENABLED
216  ProfilerStop();
217#else
218  WarnS("Sorry no google profiler support (GOOGLE_PROFILE_ENABLED!=1)...");
219//  return TRUE; // ?
220#endif // #if GOOGLE_PROFILE_ENABLED
221  return FALSE;
222}
223
224static inline number jjLONG2N(long d)
225{
226  return n_Init(d, coeffs_BIGINT);
227}
228
229static inline void view(const intvec* v)
230{
231#ifndef SING_NDEBUG
232  v->view();
233#else
234  // This code duplication is only due to Hannes's #ifndef SING_NDEBUG!
235  Print ("intvec: {rows: %d, cols: %d, length: %d, Values: \n", v->rows(), v->cols(), v->length());
236
237  for (int i = 0; i < v->rows(); i++)
238  {
239    Print ("Row[%3d]:", i);
240    for (int j = 0; j < v->cols(); j++)
241      Print (" %5d", (*v)[j + i * (v->cols())] );
242    PrintLn ();
243  }
244  PrintS ("}\n");
245#endif
246
247}
248
249                   
250
251static BOOLEAN DetailedPrint(leftv __res, leftv h)
252{
253  NoReturn(__res);
254
255  if( h == NULL )
256  {
257    WarnS("DetailedPrint needs an argument...");
258    return TRUE;
259  }
260
261  if( h->Typ() == NUMBER_CMD)
262  {
263    number n = (number)h->Data(); 
264
265    const ring r = currRing;
266
267#ifdef LDEBUG
268    r->cf->cfDBTest(n,__FILE__,__LINE__,r->cf);
269#endif
270
271    StringSetS("");
272    n_Write(n, r->cf);
273    PrintS(StringEndS());
274    PrintLn();
275
276    return FALSE;
277  }
278 
279  if( h->Typ() == RING_CMD)
280  {
281    const ring r = (const ring)h->Data();
282    rWrite(r, TRUE);
283    PrintLn();
284#ifdef RDEBUG
285    rDebugPrint(r);
286#endif
287    return FALSE;
288  }
289
290  if( h->Typ() == POLY_CMD || h->Typ() == VECTOR_CMD)
291  {
292    const poly p = (const poly)h->Data(); h = h->Next();
293
294    dPrint(p, currRing, currRing, getOptionalInteger(h, 3));
295
296    return FALSE;
297  }
298
299  if( h->Typ() == IDEAL_CMD || h->Typ() == MODUL_CMD)
300  {
301    const ideal id = (const ideal)h->Data(); h = h->Next(); 
302
303    dPrint(id, currRing, currRing, getOptionalInteger(h, 3));
304   
305    return FALSE;           
306  }
307
308  if( h->Typ() == RESOLUTION_CMD )
309  {
310    const syStrategy syzstr = reinterpret_cast<const syStrategy>(h->Data());
311
312    h = h->Next();
313
314    int nTerms = getOptionalInteger(h, 1);
315
316
317    Print("RESOLUTION_CMD(%p): ", reinterpret_cast<const void*>(syzstr)); PrintLn();
318
319    const ring save = currRing;
320    const ring r = syzstr->syRing;
321    const ring rr = (r != NULL) ? r: save;
322
323
324    const int iLength = syzstr->length;
325
326    Print("int 'length': %d", iLength); PrintLn();
327    Print("int 'regularity': %d", syzstr->regularity); PrintLn();
328    Print("short 'list_length': %hd", syzstr->list_length); PrintLn();
329    Print("short 'references': %hd", syzstr->references); PrintLn();
330
331
332#define PRINT_pINTVECTOR(s, v) Print("intvec '%10s'(%p)", #v, reinterpret_cast<const void*>((s)->v)); \
333if( (s)->v != NULL ){ PrintS(": "); view((s)->v); }; \
334PrintLn();
335
336    PRINT_pINTVECTOR(syzstr, resolution);
337    PRINT_pINTVECTOR(syzstr, betti);
338    PRINT_pINTVECTOR(syzstr, Tl);
339    PRINT_pINTVECTOR(syzstr, cw);
340#undef PRINT_pINTVECTOR
341
342    if (r == NULL)
343      Print("ring '%10s': NULL", "syRing");
344    else 
345      if (r == currRing)
346        Print("ring '%10s': currRing", "syRing");
347      else
348        if (r != NULL && r != save)
349        {
350          Print("ring '%10s': ", "syRing");
351          rWrite(r);
352#ifdef RDEBUG
353          //              rDebugPrint(r);
354#endif
355          // rChangeCurrRing(r);
356        }           
357    PrintLn();
358
359    const SRes rP = syzstr->resPairs;
360    Print("SRes 'resPairs': %p", reinterpret_cast<const void*>(rP)); PrintLn();
361
362    if (rP != NULL)
363      for (int iLevel = 0; (iLevel < iLength) && (rP[iLevel] != NULL) && ((*syzstr->Tl)[iLevel] >= 0); iLevel++)
364      {
365        int n = 0;
366        const int iTl = (*syzstr->Tl)[iLevel];
367        for (int j = 0; (j < iTl) && ((rP[iLevel][j].lcm!=NULL) || (rP[iLevel][j].syz!=NULL)); j++)
368        {
369          if (rP[iLevel][j].isNotMinimal==NULL)
370            n++;
371        }
372        Print("minimal-resPairs-Size[1+%d]: %d", iLevel, n); PrintLn();
373      }
374
375
376    //  const ring rrr = (iLevel > 0) ? rr : save; ?
377#define PRINT_RESOLUTION(s, v) Print("resolution '%12s': %p", #v, reinterpret_cast<const void*>((s)->v)); PrintLn(); \
378if ((s)->v != NULL) \
379  for (int iLevel = 0; (iLevel < iLength) && ( ((s)->v)[iLevel] != NULL ); iLevel++) \
380  { \
381    /* const ring rrr = (iLevel > 0) ? save : save; */ \
382    Print("id '%10s'[%d]: (%p) ncols = %d / size: %d; nrows = %d, rank = %ld / rk: %ld", #v, iLevel, reinterpret_cast<const void*>(((s)->v)[iLevel]), ((s)->v)[iLevel]->ncols, idSize(((s)->v)[iLevel]), ((s)->v)[iLevel]->nrows, ((s)->v)[iLevel]->rank, -1L/*id_RankFreeModule(((s)->v)[iLevel], rrr)*/ ); \
383    PrintLn(); \
384  } \
385  PrintLn();
386
387    // resolvente:
388    PRINT_RESOLUTION(syzstr, minres);
389    PRINT_RESOLUTION(syzstr, fullres);
390
391    assume (id_RankFreeModule (syzstr->res[1], rr) == syzstr->res[1]->rank);
392
393    PRINT_RESOLUTION(syzstr, res);
394    PRINT_RESOLUTION(syzstr, orderedRes);
395#undef PRINT_RESOLUTION
396
397#define PRINT_POINTER(s, v) Print("pointer '%17s': %p", #v, reinterpret_cast<const void*>((s)->v)); PrintLn();
398    // 2d arrays:
399    PRINT_POINTER(syzstr, truecomponents);
400    PRINT_POINTER(syzstr, ShiftedComponents);
401    PRINT_POINTER(syzstr, backcomponents);
402    PRINT_POINTER(syzstr, Howmuch);
403    PRINT_POINTER(syzstr, Firstelem);
404    PRINT_POINTER(syzstr, elemLength);
405    PRINT_POINTER(syzstr, sev);
406
407    // arrays of intvects:
408    PRINT_POINTER(syzstr, weights);
409    PRINT_POINTER(syzstr, hilb_coeffs);
410#undef PRINT_POINTER
411
412
413    if (syzstr->fullres==NULL)
414    {
415      PrintS("resolution 'fullres': (NULL) => resolution not computed yet");
416      PrintLn();
417    } else
418    {
419      Print("resolution 'fullres': (%p) => resolution seems to be computed already", reinterpret_cast<const void*>(syzstr->fullres));
420      PrintLn();
421      dPrint(*syzstr->fullres, save, save, nTerms);
422    }
423
424
425
426
427    if (syzstr->minres==NULL)
428    {
429      PrintS("resolution 'minres': (NULL) => resolution not minimized yet");
430      PrintLn();
431    } else
432    {
433      Print("resolution 'minres': (%p) => resolution seems to be minimized already", reinterpret_cast<const void*>(syzstr->minres));
434      PrintLn();
435      dPrint(*syzstr->minres, save, save, nTerms);
436    }
437
438
439
440
441    /*
442    int ** truecomponents;
443    long** ShiftedComponents;
444    int ** backcomponents;
445    int ** Howmuch;
446    int ** Firstelem;
447    int ** elemLength;
448    unsigned long ** sev;
449
450    intvec ** weights;
451    intvec ** hilb_coeffs;
452
453    SRes resPairs;               //polynomial data for internal use only
454
455    resolvente fullres;
456    resolvente minres;
457    resolvente res;              //polynomial data for internal use only
458    resolvente orderedRes;       //polynomial data for internal use only
459*/
460
461    //            if( currRing != save ) rChangeCurrRing(save);
462  }
463
464
465  return FALSE;
466}
467
468/// wrapper around p_Tail and id_Tail
469static BOOLEAN Tail(leftv res, leftv h)
470{
471  NoReturn(res);
472
473  if( h == NULL )
474  {
475    WarnS("Tail needs a poly/vector/ideal/module argument...");
476    return TRUE;
477  }
478
479  assume( h != NULL );
480
481  const ring r =  currRing;
482
483  if( h->Typ() == POLY_CMD || h->Typ() == VECTOR_CMD)
484  {
485    res->data = p_Tail( (const poly)h->Data(), r );
486    res->rtyp = h->Typ();
487
488    h = h->Next(); assume (h == NULL);
489   
490    return FALSE;
491  }
492
493  if( h->Typ() == IDEAL_CMD || h->Typ() == MODUL_CMD)
494  {
495    res->data = id_Tail( (const ideal)h->Data(), r );     
496    res->rtyp = h->Typ();
497   
498    h = h->Next(); assume (h == NULL);
499   
500    return FALSE;
501  }
502
503  WarnS("Tail needs a single poly/vector/ideal/module argument...");
504  return TRUE;
505}
506
507
508static BOOLEAN _ComputeLeadingSyzygyTerms(leftv res, leftv h)
509{
510  const ring r = currRing;
511  NoReturn(res);
512
513  if( h == NULL )
514  {
515    WarnS("ComputeLeadingSyzygyTerms needs an argument...");
516    return TRUE;
517  }
518
519  assume( h != NULL ); 
520
521#ifndef NDEBUG
522  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)TRUE)));
523#else
524  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)FALSE)));
525#endif
526
527  if( h->Typ() == IDEAL_CMD || h->Typ() == MODUL_CMD)
528  {
529    const ideal id = (const ideal)h->Data();
530
531    assume(id != NULL);
532
533    if( __DEBUG__ )
534    {
535      PrintS("ComputeLeadingSyzygyTerms::Input: \n");
536     
537      const BOOLEAN __LEAD2SYZ__ = (BOOLEAN)((long)(atGet(currRingHdl,"LEAD2SYZ",INT_CMD, (void*)0)));
538      const BOOLEAN __TAILREDSYZ__ = (BOOLEAN)((long)(atGet(currRingHdl,"TAILREDSYZ",INT_CMD, (void*)0)));
539      const BOOLEAN __SYZCHECK__ = (BOOLEAN)((long)(atGet(currRingHdl,"SYZCHECK",INT_CMD, (void*)0)));
540
541
542      Print("\nSYZCHECK: \t%d", __SYZCHECK__);
543      Print(", DEBUG: \t%d", __DEBUG__);
544      Print(", LEAD2SYZ: \t%d", __LEAD2SYZ__);
545      Print(", TAILREDSYZ: \t%d\n", __TAILREDSYZ__);
546
547      dPrint(id, r, r, 1);
548
549      assume( !__LEAD2SYZ__ );
550    }
551
552    h = h->Next(); assume (h == NULL);
553
554    const ideal newid = ComputeLeadingSyzygyTerms(id, r);
555   
556    res->data = newid; res->rtyp = MODUL_CMD;
557    return FALSE;
558  }
559
560  WarnS("ComputeLeadingSyzygyTerms needs a single ideal/module argument...");
561  return TRUE;
562}
563
564///  sorting wrt <c,ds> & reversing...
565/// change the input inplace!!!
566// TODO: use a ring with >_{c, ds}!???
567static BOOLEAN _Sort_c_ds(leftv res, leftv h)
568{
569  NoReturn(res);
570
571  const ring r = currRing;
572  NoReturn(res);
573
574  if( h == NULL )
575  {
576    WarnS("Sort_c_ds needs an argument...");
577    return TRUE;
578  }
579
580  assume( h != NULL ); 
581
582#ifndef NDEBUG
583  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)FALSE)));
584#else
585  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)FALSE)));
586#endif
587
588  if(    (h->Typ() == IDEAL_CMD || h->Typ() == MODUL_CMD)
589      && (h->rtyp  == IDHDL) // must be a variable!
590      && (h->e == NULL) // not a list element
591      ) 
592  {
593    const ideal id = (const ideal)h->Data();
594
595    assume(id != NULL);
596
597    if( __DEBUG__ )
598    {
599      PrintS("Sort_c_ds::Input: \n");
600
601      const BOOLEAN __LEAD2SYZ__ = (BOOLEAN)((long)(atGet(currRingHdl,"LEAD2SYZ",INT_CMD, (void*)0)));
602      const BOOLEAN __TAILREDSYZ__ = (BOOLEAN)((long)(atGet(currRingHdl,"TAILREDSYZ",INT_CMD, (void*)0)));
603      const BOOLEAN __SYZCHECK__ = (BOOLEAN)((long)(atGet(currRingHdl,"SYZCHECK",INT_CMD, (void*)0)));   
604
605      Print("\nSYZCHECK: \t%d", __SYZCHECK__);
606      Print(", DEBUG: \t%d", __DEBUG__);
607      Print(", LEAD2SYZ: \t%d", __LEAD2SYZ__);
608      Print(", TAILREDSYZ: \t%d\n", __TAILREDSYZ__);
609
610      dPrint(id, r, r, 1);     
611    }
612
613    assume (h->Next() == NULL);
614
615    id_Test(id, r);
616
617    Sort_c_ds(id, r); // NOT A COPY! inplace sorting!!!
618
619//    res->data = id;
620//    res->rtyp = h->Typ();
621   
622    if( __DEBUG__ )
623    {
624      PrintS("Sort_c_ds::Output: \n");
625      dPrint(id, r, r, 1);
626    }
627
628    // NOTE: nothing is to be returned!!!
629    return FALSE;
630  }
631
632  WarnS("ComputeLeadingSyzygyTerms needs a single ideal/module argument (must be a variable!)...");
633  return TRUE; 
634}
635
636
637static BOOLEAN _Compute2LeadingSyzygyTerms(leftv res, leftv h)
638{
639  const ring r = currRing;
640  NoReturn(res);
641
642  if( h == NULL )
643  {
644    WarnS("Compute2LeadingSyzygyTerms needs an argument...");
645    return TRUE;
646  }
647
648  assume( h != NULL ); 
649
650#ifndef NDEBUG
651  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)TRUE)));
652#else
653  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)FALSE)));
654#endif
655
656  const BOOLEAN __LEAD2SYZ__ = (BOOLEAN)((long)(atGet(currRingHdl,"LEAD2SYZ",INT_CMD, (void*)0)));
657 
658  assume( __LEAD2SYZ__ );
659 
660  if( h->Typ() == IDEAL_CMD || h->Typ() == MODUL_CMD)
661  {
662    const ideal id = (const ideal)h->Data();
663
664    assume(id != NULL);
665
666    if( __DEBUG__ )
667    {
668      PrintS("Compute2LeadingSyzygyTerms::Input: \n");
669      dPrint(id, r, r, 0);
670    }
671
672    h = h->Next(); assume (h == NULL);
673
674    res->data = Compute2LeadingSyzygyTerms(id, r);
675    res->rtyp = MODUL_CMD;
676
677    return FALSE;
678  }
679
680  WarnS("Compute2LeadingSyzygyTerms needs a single ideal/module argument...");
681  return TRUE;
682}
683
684
685
686/// proc SSFindReducer(def product, def syzterm, def L, def T, list #)
687static BOOLEAN _FindReducer(leftv res, leftv h)
688{
689  const char* usage = "`FindReducer(<poly/vector>, <vector/0>, <ideal/module>[,<module>])` expected";
690  const ring r = currRing;
691
692  NoReturn(res);
693
694
695#ifndef NDEBUG
696  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)TRUE)));
697#else
698  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)FALSE)));
699#endif
700
701  const BOOLEAN __TAILREDSYZ__ = (BOOLEAN)((long)(atGet(currRingHdl,"TAILREDSYZ",INT_CMD, (void*)0)));
702
703  if ((h==NULL) || (h->Typ()!=VECTOR_CMD && h->Typ() !=POLY_CMD) || (h->Data() == NULL))
704  {
705    WerrorS(usage);
706    return TRUE;   
707  }
708   
709  const poly product = (poly) h->Data(); assume (product != NULL);
710
711
712  h = h->Next();
713  if ((h==NULL) || !((h->Typ()==VECTOR_CMD) || (h->Data() == NULL)) )
714  {
715    WerrorS(usage);
716    return TRUE;   
717  }
718
719  poly syzterm = NULL;
720
721  if(h->Typ()==VECTOR_CMD) 
722    syzterm = (poly) h->Data();
723
724
725
726  h = h->Next();
727  if ((h==NULL) || (h->Typ()!=IDEAL_CMD && h->Typ() !=MODUL_CMD) || (h->Data() == NULL))
728  {
729    WerrorS(usage);
730    return TRUE;   
731  }
732 
733  const ideal L = (ideal) h->Data(); h = h->Next();
734
735  assume( IDELEMS(L) > 0 );
736
737  ideal LS = NULL;
738
739  if ((h != NULL) && (h->Typ() ==MODUL_CMD) && (h->Data() != NULL))
740  {
741    LS = (ideal)h->Data();
742    h = h->Next();
743  }
744
745  if( __TAILREDSYZ__ )
746    assume (LS != NULL);
747
748  assume( h == NULL );
749
750  if( __DEBUG__ )
751  {
752    PrintS("FindReducer(product, syzterm, L, T, #)::Input: \n");
753
754    PrintS("product: "); dPrint(product, r, r, 2);
755    PrintS("syzterm: "); dPrint(syzterm, r, r, 2);
756    PrintS("L: "); dPrint(L, r, r, 0);
757//    PrintS("T: "); dPrint(T, r, r, 4);
758
759    if( LS == NULL )
760      PrintS("LS: NULL\n");
761    else
762    {
763      PrintS("LS: "); dPrint(LS, r, r, 0);
764    }
765  }
766
767  res->rtyp = VECTOR_CMD;
768  res->data = FindReducer(product, syzterm, L, LS, r);
769
770  if( __DEBUG__ )
771  {
772    PrintS("FindReducer::Output: \n");
773    dPrint((poly)res->data, r, r, 2);
774  }   
775 
776  return FALSE;   
777 
778}
779
780// proc SchreyerSyzygyNF(vector syz_lead, vector syz_2, def L, def T, list #)
781static BOOLEAN _SchreyerSyzygyNF(leftv res, leftv h)
782{
783  const char* usage = "`SchreyerSyzygyNF(<vector>, <vector>, <ideal/module>, <ideal/module>[,<module>])` expected";
784  const ring r = currRing;
785
786  NoReturn(res);
787
788#ifndef NDEBUG
789  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)TRUE)));
790#else
791  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)FALSE)));
792#endif
793
794  const BOOLEAN __TAILREDSYZ__ = (BOOLEAN)((long)(atGet(currRingHdl,"TAILREDSYZ",INT_CMD, (void*)0)));
795//  const BOOLEAN __LEAD2SYZ__ = (BOOLEAN)((long)(atGet(currRingHdl,"LEAD2SYZ",INT_CMD, (void*)0)));
796  const BOOLEAN __SYZCHECK__ = (BOOLEAN)((long)(atGet(currRingHdl,"SYZCHECK",INT_CMD, (void*)0)));   
797
798  const BOOLEAN __HYBRIDNF__ = (BOOLEAN)((long)(atGet(currRingHdl,"HYBRIDNF",INT_CMD, (void*)0)));
799
800  assume( __HYBRIDNF__ );
801 
802  if ((h==NULL) || (h->Typ() != VECTOR_CMD) || (h->Data() == NULL))
803  {
804    WerrorS(usage);
805    return TRUE;   
806  }
807
808  const poly syz_lead = (poly) h->Data(); assume (syz_lead != NULL);
809
810
811  h = h->Next();
812  if ((h==NULL) || (h->Typ() != VECTOR_CMD) || (h->Data() == NULL))
813  {
814    WerrorS(usage);
815    return TRUE;   
816  }
817
818  const poly syz_2 = (poly) h->Data(); assume (syz_2 != NULL);
819
820  h = h->Next();
821  if ((h==NULL) || (h->Typ()!=IDEAL_CMD && h->Typ() !=MODUL_CMD) || (h->Data() == NULL))
822  {
823    WerrorS(usage);
824    return TRUE;   
825  }
826
827  const ideal L = (ideal) h->Data(); assume( IDELEMS(L) > 0 );
828
829
830  h = h->Next();
831  if ((h==NULL) || (h->Typ()!=IDEAL_CMD && h->Typ() !=MODUL_CMD) || (h->Data() == NULL))
832  {
833    WerrorS(usage);
834    return TRUE;   
835  }
836
837  const ideal T = (ideal) h->Data();
838
839  assume( IDELEMS(L) == IDELEMS(T) );
840
841  ideal LS = NULL;
842
843  h = h->Next();
844  if ((h != NULL) && (h->Typ() ==MODUL_CMD) && (h->Data() != NULL))
845  {
846    LS = (ideal)h->Data();
847    h = h->Next();
848  }
849
850  if( __TAILREDSYZ__ )
851    assume (LS != NULL);
852
853  assume( h == NULL );
854
855  if( __DEBUG__ )
856  {
857    PrintS("SchreyerSyzygyNF(syz_lead, syz_2, L, T, #)::Input: \n");
858
859    PrintS("syz_lead: "); dPrint(syz_lead, r, r, 2);
860    PrintS("syz_2: "); dPrint(syz_2, r, r, 2);
861
862    PrintS("L: "); dPrint(L, r, r, 0);
863    PrintS("T: "); dPrint(T, r, r, 0);
864
865    if( LS == NULL )
866      PrintS("LS: NULL\n");
867    else
868    {
869      PrintS("LS: "); dPrint(LS, r, r, 0);
870    }
871  }
872 
873  res->rtyp = VECTOR_CMD;
874  res->data = SchreyerSyzygyNF(syz_lead, syz_2, L, T, LS, r);
875
876  if( __DEBUG__ )
877  {
878    PrintS("SchreyerSyzygyNF::Output: ");
879
880    dPrint((poly)res->data, r, r, 2);
881  }
882
883
884  return FALSE;
885}
886
887
888
889/// proc SSReduceTerm(poly m, def t, def syzterm, def L, def T, list #)
890static BOOLEAN _ReduceTerm(leftv res, leftv h)
891{
892  const char* usage = "`ReduceTerm(<poly>, <poly/vector>, <vector/0>, <ideal/module>, <ideal/module>[,<module>])` expected";
893  const ring r = currRing;
894
895  NoReturn(res);
896
897#ifndef NDEBUG
898  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)TRUE)));
899#else
900  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)FALSE)));
901#endif
902
903  const BOOLEAN __TAILREDSYZ__ = (BOOLEAN)((long)(atGet(currRingHdl,"TAILREDSYZ",INT_CMD, (void*)0)));
904//  const BOOLEAN __LEAD2SYZ__ = (BOOLEAN)((long)(atGet(currRingHdl,"LEAD2SYZ",INT_CMD, (void*)0)));
905  const BOOLEAN __SYZCHECK__ = (BOOLEAN)((long)(atGet(currRingHdl,"SYZCHECK",INT_CMD, (void*)0)));   
906
907  if ((h==NULL) || (h->Typ() !=POLY_CMD) || (h->Data() == NULL))
908  {
909    WerrorS(usage);
910    return TRUE;   
911  }
912
913  const poly multiplier = (poly) h->Data(); assume (multiplier != NULL);
914
915 
916  h = h->Next();
917  if ((h==NULL) || (h->Typ()!=VECTOR_CMD && h->Typ() !=POLY_CMD) || (h->Data() == NULL))
918  {
919    WerrorS(usage);
920    return TRUE;   
921  }
922
923  const poly term4reduction = (poly) h->Data(); assume( term4reduction != NULL );
924
925 
926  poly syztermCheck = NULL;
927 
928  h = h->Next();
929  if ((h==NULL) || !((h->Typ()==VECTOR_CMD) || (h->Data() == NULL)) )
930  {
931    WerrorS(usage);
932    return TRUE;   
933  }
934
935  if(h->Typ()==VECTOR_CMD) 
936    syztermCheck = (poly) h->Data();
937
938 
939  h = h->Next();
940  if ((h==NULL) || (h->Typ()!=IDEAL_CMD && h->Typ() !=MODUL_CMD) || (h->Data() == NULL))
941  {
942    WerrorS(usage);
943    return TRUE;   
944  }
945
946  const ideal L = (ideal) h->Data(); assume( IDELEMS(L) > 0 );
947
948 
949  h = h->Next();
950  if ((h==NULL) || (h->Typ()!=IDEAL_CMD && h->Typ() !=MODUL_CMD) || (h->Data() == NULL))
951  {
952    WerrorS(usage);
953    return TRUE;   
954  }
955
956  const ideal T = (ideal) h->Data();
957
958  assume( IDELEMS(L) == IDELEMS(T) );
959
960  ideal LS = NULL;
961
962  h = h->Next();
963  if ((h != NULL) && (h->Typ() ==MODUL_CMD) && (h->Data() != NULL))
964  {
965    LS = (ideal)h->Data();
966    h = h->Next();
967  }
968
969  if( __TAILREDSYZ__ )
970    assume (LS != NULL);
971
972  assume( h == NULL );
973
974  if( __DEBUG__ )
975  {
976    PrintS("ReduceTerm(m, t, syzterm, L, T, #)::Input: \n");
977
978    PrintS("m: "); dPrint(multiplier, r, r, 2);
979    PrintS("t: "); dPrint(term4reduction, r, r, 2);
980    PrintS("syzterm: "); dPrint(syztermCheck, r, r, 2);
981   
982    PrintS("L: "); dPrint(L, r, r, 0);
983    PrintS("T: "); dPrint(T, r, r, 0);
984
985    if( LS == NULL )
986      PrintS("LS: NULL\n");
987    else
988    {
989      PrintS("LS: "); dPrint(LS, r, r, 0);
990    }
991  }
992
993
994  if (__SYZCHECK__ && syztermCheck != NULL)
995  {
996    const int c = p_GetComp(syztermCheck, r) - 1;
997    assume( c >= 0 && c < IDELEMS(L) );
998   
999    const poly p = L->m[c];
1000    assume( p != NULL ); assume( pNext(p) == NULL );   
1001
1002    assume( p_EqualPolys(term4reduction, p, r) ); // assume? TODO
1003
1004
1005    poly m = leadmonom(syztermCheck, r);
1006    assume( m != NULL ); assume( pNext(m) == NULL );
1007
1008    assume( p_EqualPolys(multiplier, m, r) ); // assume? TODO
1009
1010    p_Delete(&m, r);   
1011   
1012// NOTE:   leadmonomial(syzterm) == m &&  L[leadcomp(syzterm)] == t
1013  }
1014
1015  res->rtyp = VECTOR_CMD;
1016  res->data = ReduceTerm(multiplier, term4reduction, syztermCheck, L, T, LS, r);
1017
1018
1019  if( __DEBUG__ )
1020  {
1021    PrintS("ReduceTerm::Output: ");
1022
1023    dPrint((poly)res->data, r, r, 2);
1024  }
1025 
1026 
1027  return FALSE;
1028}
1029
1030
1031
1032
1033// proc SSTraverseTail(poly m, def @tail, def L, def T, list #)
1034static BOOLEAN _TraverseTail(leftv res, leftv h)
1035{
1036  const char* usage = "`TraverseTail(<poly>, <poly/vector>, <ideal/module>, <ideal/module>[,<module>])` expected";
1037  const ring r = currRing;
1038
1039  NoReturn(res);
1040
1041#ifndef NDEBUG
1042  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)TRUE)));
1043#else
1044  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)FALSE)));
1045#endif
1046
1047  const BOOLEAN __TAILREDSYZ__ = (BOOLEAN)((long)(atGet(currRingHdl,"TAILREDSYZ",INT_CMD, (void*)1)));
1048
1049  if ((h==NULL) || (h->Typ() !=POLY_CMD) || (h->Data() == NULL))
1050  {
1051    WerrorS(usage);
1052    return TRUE;   
1053  }
1054
1055  const poly multiplier = (poly) h->Data(); assume (multiplier != NULL);
1056
1057  h = h->Next();
1058  if ((h==NULL) || (h->Typ()!=VECTOR_CMD && h->Typ() !=POLY_CMD))
1059  {
1060    WerrorS(usage);
1061    return TRUE;   
1062  }
1063
1064  const poly tail = (poly) h->Data(); 
1065
1066  h = h->Next();
1067
1068  if ((h==NULL) || (h->Typ()!=IDEAL_CMD && h->Typ() !=MODUL_CMD) || (h->Data() == NULL))
1069  {
1070    WerrorS(usage);
1071    return TRUE;   
1072  }
1073
1074  const ideal L = (ideal) h->Data();
1075
1076  assume( IDELEMS(L) > 0 );
1077
1078  h = h->Next();
1079  if ((h==NULL) || (h->Typ()!=IDEAL_CMD && h->Typ() !=MODUL_CMD) || (h->Data() == NULL))
1080  {
1081    WerrorS(usage);
1082    return TRUE;   
1083  }
1084
1085  const ideal T = (ideal) h->Data();
1086
1087  assume( IDELEMS(L) == IDELEMS(T) );
1088
1089  h = h->Next();
1090 
1091  ideal LS = NULL;
1092
1093  if ((h != NULL) && (h->Typ() ==MODUL_CMD) && (h->Data() != NULL))
1094  {
1095    LS = (ideal)h->Data();
1096    h = h->Next();
1097  }
1098
1099  if( __TAILREDSYZ__ )
1100    assume (LS != NULL);
1101
1102  assume( h == NULL );
1103
1104  if( __DEBUG__ )
1105  {
1106    PrintS("TraverseTail(m, t, L, T, #)::Input: \n");
1107
1108    PrintS("m: "); dPrint(multiplier, r, r, 2);
1109    PrintS("t: "); dPrint(tail, r, r, 10);
1110
1111    PrintS("L: "); dPrint(L, r, r, 0);
1112    PrintS("T: "); dPrint(T, r, r, 0);
1113
1114    if( LS == NULL )
1115      PrintS("LS: NULL\n");
1116    else
1117    {
1118      PrintS("LS: "); dPrint(LS, r, r, 0);
1119    }
1120  }
1121
1122  res->rtyp = VECTOR_CMD;
1123  res->data = TraverseTail(multiplier, tail, L, T, LS, r);
1124
1125
1126  if( __DEBUG__ )
1127  {
1128    PrintS("TraverseTail::Output: ");
1129    dPrint((poly)res->data, r, r, 2);
1130  }
1131
1132  return FALSE;
1133}
1134
1135
1136/// module (LL, TT) = SSComputeSyzygy(L, T);
1137/// Compute Syz(L ++ T) = N = LL ++ TT
1138// proc SSComputeSyzygy(def L, def T)
1139static BOOLEAN _ComputeSyzygy(leftv res, leftv h)
1140{
1141  const char* usage = "`ComputeSyzygy(<ideal/module>, <ideal/module>])` expected";
1142  const ring r = currRing;
1143
1144  NoReturn(res);
1145
1146#ifndef NDEBUG
1147  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)TRUE)));
1148#else
1149  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)FALSE)));
1150#endif
1151
1152  if ((h==NULL) || (h->Typ()!=IDEAL_CMD && h->Typ() !=MODUL_CMD) || (h->Data() == NULL))
1153  {
1154    WerrorS(usage);
1155    return TRUE;   
1156  }
1157
1158  const ideal L = (ideal) h->Data();
1159
1160  assume( IDELEMS(L) > 0 );
1161
1162  h = h->Next();
1163  if ((h==NULL) || (h->Typ()!=IDEAL_CMD && h->Typ() !=MODUL_CMD) || (h->Data() == NULL))
1164  {
1165    WerrorS(usage);
1166    return TRUE;   
1167  }
1168
1169  const ideal T = (ideal) h->Data();
1170  assume( IDELEMS(L) == IDELEMS(T) );
1171
1172
1173  h = h->Next(); assume( h == NULL ); 
1174
1175  if( __DEBUG__ )
1176  {
1177    PrintS("ComputeSyzygy(L, T)::Input: \n");
1178
1179    PrintS("L: "); dPrint(L, r, r, 0);
1180    PrintS("T: "); dPrint(T, r, r, 0);
1181  }
1182
1183  ideal LL, TT;
1184
1185  ComputeSyzygy(L, T, LL, TT, r);
1186
1187  lists l = (lists)omAllocBin(slists_bin); l->Init(2);
1188
1189  l->m[0].rtyp = MODUL_CMD; l->m[0].data = reinterpret_cast<void *>(LL);
1190
1191  l->m[1].rtyp = MODUL_CMD; l->m[1].data = reinterpret_cast<void *>(TT);
1192 
1193  res->data = l; res->rtyp = LIST_CMD;
1194 
1195  if( __DEBUG__ )
1196  {
1197    PrintS("ComputeSyzygy::Output: ");
1198    dPrint(LL, r, r, 0);
1199    dPrint(TT, r, r, 0);
1200  }
1201
1202  return FALSE;
1203
1204}
1205
1206/// Get leading term without a module component
1207static BOOLEAN _leadmonom(leftv res, leftv h)
1208{
1209  NoReturn(res);
1210
1211  if ((h!=NULL) && (h->Typ()==VECTOR_CMD || h->Typ()==POLY_CMD) && (h->Data() != NULL))
1212  {
1213    const ring r = currRing;
1214    const poly p = (poly)(h->Data());
1215
1216    res->data = reinterpret_cast<void *>(  leadmonom(p, r) );
1217    res->rtyp = POLY_CMD;
1218
1219    return FALSE;
1220  }
1221
1222  WerrorS("`leadmonom(<poly/vector>)` expected");
1223  return TRUE;
1224}
1225
1226/// Get leading component
1227static BOOLEAN leadcomp(leftv res, leftv h)
1228{
1229  NoReturn(res);
1230
1231  if ((h!=NULL) && (h->Typ()==VECTOR_CMD || h->Typ()==POLY_CMD))
1232  {
1233    const ring r = currRing;
1234
1235    const poly p = (poly)(h->Data());
1236
1237    if (p != NULL )
1238    {
1239      assume( p != NULL );
1240      assume( p_LmTest(p, r) );
1241
1242      const unsigned long iComp = p_GetComp(p, r);
1243
1244  //    assume( iComp > 0 ); // p is a vector
1245
1246      res->data = reinterpret_cast<void *>(jjLONG2N(iComp));
1247    } else
1248      res->data = reinterpret_cast<void *>(jjLONG2N(0));
1249     
1250
1251    res->rtyp = BIGINT_CMD;
1252    return FALSE;
1253  }
1254
1255  WerrorS("`leadcomp(<poly/vector>)` expected");
1256  return TRUE;
1257}
1258
1259
1260
1261
1262/// Get raw leading exponent vector
1263static BOOLEAN leadrawexp(leftv res, leftv h)
1264{
1265  NoReturn(res);
1266
1267  if ((h!=NULL) && (h->Typ()==VECTOR_CMD || h->Typ()==POLY_CMD) && (h->Data() != NULL))
1268  {
1269    const ring r = currRing;
1270    const poly p = (poly)(h->Data());
1271
1272    assume( p != NULL );
1273    assume( p_LmTest(p, r) );
1274
1275    const int iExpSize = r->ExpL_Size;
1276
1277//    intvec *iv = new intvec(iExpSize);
1278
1279    lists l=(lists)omAllocBin(slists_bin);
1280    l->Init(iExpSize);
1281
1282    for(int i = iExpSize-1; i >= 0; i--)
1283    {
1284      l->m[i].rtyp = BIGINT_CMD;
1285      l->m[i].data = reinterpret_cast<void *>(jjLONG2N(p->exp[i])); // longs...
1286    }
1287
1288    res->rtyp = LIST_CMD; // list of bigints
1289    res->data = reinterpret_cast<void *>(l);
1290    return FALSE;
1291  }
1292
1293  WerrorS("`leadrawexp(<poly/vector>)` expected");
1294  return TRUE;
1295}
1296
1297
1298/// Endowe the current ring with additional (leading) Syz-component ordering
1299static BOOLEAN MakeSyzCompOrdering(leftv res, leftv /*h*/)
1300{
1301
1302  NoReturn(res);
1303
1304  //    res->data = rCurrRingAssure_SyzComp(); // changes current ring! :(
1305  res->data = reinterpret_cast<void *>(rAssure_SyzComp(currRing, TRUE));
1306  res->rtyp = RING_CMD; // return new ring!
1307  // QRING_CMD?
1308
1309  return FALSE;
1310}
1311
1312
1313/// Same for Induced Schreyer ordering (ordering on components is defined by sign!)
1314static BOOLEAN MakeInducedSchreyerOrdering(leftv res, leftv h)
1315{
1316
1317  NoReturn(res);
1318
1319  int sign = 1;
1320  if ((h!=NULL) && (h->Typ()==INT_CMD))
1321  {
1322    const int s = (int)((long)(h->Data()));
1323
1324    if( s != -1 && s != 1 )
1325    {
1326      WerrorS("`MakeInducedSchreyerOrdering(<int>)` called with wrong integer argument (must be +-1)!");
1327      return TRUE;
1328    }
1329
1330    sign = s;           
1331  }
1332
1333  assume( sign == 1 || sign == -1 );
1334  res->data = reinterpret_cast<void *>(rAssure_InducedSchreyerOrdering(currRing, TRUE, sign));
1335  res->rtyp = RING_CMD; // return new ring!
1336  // QRING_CMD?
1337  return FALSE;
1338}
1339
1340
1341/// Returns old SyzCompLimit, can set new limit
1342static BOOLEAN SetSyzComp(leftv res, leftv h)
1343{
1344  NoReturn(res);
1345
1346  const ring r = currRing;
1347
1348  if( !rIsSyzIndexRing(r) )
1349  {
1350    WerrorS("`SetSyzComp(<int>)` called on incompatible ring (not created by 'MakeSyzCompOrdering'!)");
1351    return TRUE;
1352  }
1353
1354  res->rtyp = INT_CMD;
1355  res->data = reinterpret_cast<void *>(rGetCurrSyzLimit(r)); // return old syz limit
1356
1357  if ((h!=NULL) && (h->Typ()==INT_CMD))
1358  {
1359    const int iSyzComp = (int)reinterpret_cast<long>(h->Data());
1360    assume( iSyzComp > 0 );
1361    rSetSyzComp(iSyzComp, currRing);
1362  }
1363
1364  return FALSE;
1365}
1366
1367/// ?
1368static BOOLEAN GetInducedData(leftv res, leftv h)
1369{
1370  NoReturn(res);
1371
1372  const ring r = currRing;
1373
1374  int p = 0; // which IS-block? p^th!
1375
1376  if ((h!=NULL) && (h->Typ()==INT_CMD))
1377  {
1378    p = (int)((long)(h->Data())); h=h->next;
1379    assume(p >= 0);
1380  }
1381
1382  const int pos = rGetISPos(p, r);
1383
1384  if(  /*(*/ -1 == pos /*)*/  )
1385  {
1386    WerrorS("`GetInducedData([int])` called on incompatible ring (not created by 'MakeInducedSchreyerOrdering'!)");
1387    return TRUE;
1388  }
1389
1390
1391  const int iLimit = r->typ[pos].data.is.limit;
1392  const ideal F = r->typ[pos].data.is.F;
1393  ideal FF = id_Copy(F, r);
1394
1395
1396 
1397  lists l=(lists)omAllocBin(slists_bin);
1398  l->Init(2);
1399
1400  l->m[0].rtyp = INT_CMD;
1401  l->m[0].data = reinterpret_cast<void *>(iLimit);
1402
1403
1404  //        l->m[1].rtyp = MODUL_CMD;
1405
1406  if( idIsModule(FF, r) )
1407  {
1408    l->m[1].rtyp = MODUL_CMD;
1409
1410    //          Print("before: %d\n", FF->nrows);
1411    //          FF->nrows = id_RankFreeModule(FF, r); // ???
1412    //          Print("after: %d\n", FF->nrows);
1413  }
1414  else
1415    l->m[1].rtyp = IDEAL_CMD;
1416
1417  l->m[1].data = reinterpret_cast<void *>(FF);
1418
1419  res->rtyp = LIST_CMD; // list of int/module
1420  res->data = reinterpret_cast<void *>(l);
1421
1422  return FALSE;
1423
1424}
1425
1426
1427/* // the following turned out to be unnecessary...   
1428/// Finds p^th AM ordering, and returns its position in r->typ[] AND
1429/// corresponding &r->wvhdl[]
1430/// returns FALSE if something went wrong!
1431/// p - starts with 0!
1432BOOLEAN rGetAMPos(const ring r, const int p, int &typ_pos, int &wvhdl_pos, const BOOLEAN bSearchWvhdl = FALSE)
1433{
1434#if MYTEST
1435  Print("rGetAMPos(p: %d...)\nF:", p);
1436  PrintLn();
1437#endif
1438  typ_pos = -1;
1439  wvhdl_pos = -1;
1440
1441  if (r->typ==NULL)
1442    return FALSE;
1443
1444
1445  int j = p; // Which IS record to use...
1446  for( int pos = 0; pos < r->OrdSize; pos++ )
1447    if( r->typ[pos].ord_typ == ro_am)
1448      if( j-- == 0 )
1449      {
1450        typ_pos = pos;
1451
1452        if( bSearchWvhdl )
1453        {
1454          const int nblocks = rBlocks(r) - 1;
1455          const int* w = r->typ[pos].data.am.weights; // ?
1456
1457          for( pos = 0; pos <= nblocks; pos ++ )
1458            if (r->order[pos] == ringorder_am)
1459              if( r->wvhdl[pos] == w )
1460              {
1461                wvhdl_pos = pos;
1462                break;
1463              }
1464          if (wvhdl_pos < 0)
1465            return FALSE;
1466
1467          assume(wvhdl_pos >= 0);
1468        }
1469        assume(typ_pos >= 0);
1470        return TRUE;
1471      }
1472
1473  return FALSE;
1474}
1475
1476// // ?
1477// static BOOLEAN GetAMData(leftv res, leftv h)
1478// {
1479//   NoReturn(res);
1480//
1481//   const ring r = currRing;
1482//
1483//   int p = 0; // which IS-block? p^th!
1484//
1485//   if ((h!=NULL) && (h->Typ()==INT_CMD))
1486//     p = (int)((long)(h->Data())); h=h->next;
1487//
1488//   assume(p >= 0);
1489//
1490//   int d, w;
1491//   
1492//   if( !rGetAMPos(r, p, d, w, TRUE) )
1493//   {
1494//     Werror("`GetAMData([int])`: no %d^th _am block-ordering!", p);
1495//     return TRUE;
1496//   }
1497//
1498//   assume( r->typ[d].ord_typ == ro_am );
1499//   assume( r->order[w] == ringorder_am );
1500//
1501//
1502//   const short start = r->typ[d].data.am.start;  // bounds of ordering (in E)
1503//   const short end = r->typ[d].data.am.end;
1504//   const short len_gen = r->typ[d].data.am.len_gen; // i>len_gen: weight(gen(i)):=0
1505//   const int *weights = r->typ[d].data.am.weights; // pointers into wvhdl field of length (end-start+1) + len_gen
1506//   // contents w_1,... w_n, len, mod_w_1, .. mod_w_len, 0
1507//
1508//   assume( weights == r->wvhdl[w] );
1509//
1510//   
1511//   lists l=(lists)omAllocBin(slists_bin);
1512//   l->Init(2);
1513//
1514//   const short V = end-start+1;
1515//   intvec* ww_vars = new intvec(V);
1516//   intvec* ww_gens = new intvec(len_gen);
1517//
1518//   for (int i = 0; i < V; i++ )
1519//     (*ww_vars)[i] = weights[i];
1520//
1521//   assume( weights[V] == len_gen );
1522//
1523//   for (int i = 0; i < len_gen; i++ )
1524//     (*ww_gens)[i] = weights[i - V - 1];
1525//   
1526//
1527//   l->m[0].rtyp = INTVEC_CMD;
1528//   l->m[0].data = reinterpret_cast<void *>(ww_vars);
1529//
1530//   l->m[1].rtyp = INTVEC_CMD;
1531//   l->m[1].data = reinterpret_cast<void *>(ww_gens);
1532//
1533//
1534//   return FALSE;
1535//
1536// }
1537*/
1538
1539/// Returns old SyzCompLimit, can set new limit
1540static BOOLEAN SetInducedReferrence(leftv res, leftv h)
1541{
1542  NoReturn(res);
1543
1544  const ring r = currRing;
1545
1546  if( !( (h!=NULL) && ( (h->Typ()==IDEAL_CMD) || (h->Typ()==MODUL_CMD))) )
1547  {
1548    WerrorS("`SetInducedReferrence(<ideal/module>, [int[, int]])` expected");
1549    return TRUE;
1550  }
1551
1552  const ideal F = (ideal)h->Data(); ; // No copy!
1553  h=h->next;
1554
1555  int rank = 0;
1556
1557  if ((h!=NULL) && (h->Typ()==INT_CMD))
1558  {
1559    rank = (int)((long)(h->Data())); h=h->next;
1560    assume(rank >= 0);
1561  } else
1562    rank = id_RankFreeModule(F, r); // Starting syz-comp (1st: i+1)
1563
1564  int p = 0; // which IS-block? p^th!
1565
1566  if ((h!=NULL) && (h->Typ()==INT_CMD))
1567  {
1568    p = (int)((long)(h->Data())); h=h->next;
1569    assume(p >= 0);
1570  }
1571
1572  const int posIS = rGetISPos(p, r);
1573
1574  if(  /*(*/ -1 == posIS /*)*/  )
1575  {
1576    WerrorS("`SetInducedReferrence(<ideal/module>, [int[, int]])` called on incompatible ring (not created by 'MakeInducedSchreyerOrdering'!)");
1577    return TRUE;
1578  }
1579
1580
1581
1582  // F & componentWeights belong to that ordering block of currRing now:
1583  rSetISReference(r, F, rank, p); // F will be copied!
1584  return FALSE;
1585}
1586
1587
1588//    F = ISUpdateComponents( F, V, MIN );
1589//    // replace gen(i) -> gen(MIN + V[i-MIN]) for all i > MIN in all terms from F!
1590static BOOLEAN ISUpdateComponents(leftv res, leftv h)
1591{
1592  NoReturn(res);
1593
1594  PrintS("ISUpdateComponents:.... \n");
1595
1596  if ((h!=NULL) && (h->Typ()==MODUL_CMD))
1597  {
1598    ideal F = (ideal)h->Data(); ; // No copy!
1599    h=h->next;
1600
1601    if ((h!=NULL) && (h->Typ()==INTVEC_CMD))
1602    {
1603      const intvec* const V = (const intvec* const) h->Data();
1604      h=h->next;
1605
1606      if ((h!=NULL) && (h->Typ()==INT_CMD))
1607      {
1608        const int MIN = (int)((long)(h->Data()));
1609
1610        pISUpdateComponents(F, V, MIN, currRing);
1611        return FALSE;
1612      }
1613    }
1614  }
1615
1616  WerrorS("`ISUpdateComponents(<module>, intvec, int)` expected");
1617  return TRUE;
1618}
1619
1620
1621/// NF using length
1622static BOOLEAN reduce_syz(leftv res, leftv h)
1623{
1624  // const ring r = currRing;
1625
1626  if ( !( (h!=NULL) && (h->Typ()==VECTOR_CMD || h->Typ()==POLY_CMD) ) )
1627  {
1628    WerrorS("`reduce_syz(<poly/vector>!, <ideal/module>, <int>, [int])` expected");
1629    return TRUE;
1630  }
1631
1632  res->rtyp = h->Typ();
1633  const poly v = reinterpret_cast<poly>(h->Data());
1634  h=h->next;
1635
1636  if ( !( (h!=NULL) && (h->Typ()==MODUL_CMD || h->Typ()==IDEAL_CMD ) ) )
1637  {
1638    WerrorS("`reduce_syz(<poly/vector>, <ideal/module>!, <int>, [int])` expected");
1639    return TRUE;
1640  }
1641
1642  assumeStdFlag(h);
1643  const ideal M = reinterpret_cast<ideal>(h->Data()); h=h->next;
1644
1645
1646  if ( !( (h!=NULL) && (h->Typ()== INT_CMD)  ) )
1647  {
1648    WerrorS("`reduce_syz(<poly/vector>, <ideal/module>, <int>!, [int])` expected");
1649    return TRUE;
1650  }
1651
1652  const int iSyzComp = (int)((long)(h->Data())); h=h->next;
1653
1654  int iLazyReduce = 0;
1655
1656  if ( ( (h!=NULL) && (h->Typ()== INT_CMD)  ) )
1657    iLazyReduce = (int)((long)(h->Data())); 
1658
1659  res->data = (void *)kNFLength(M, currQuotient, v, iSyzComp, iLazyReduce); // NOTE: currRing :(
1660  return FALSE;
1661}
1662
1663
1664/// Get raw syzygies (idPrepare)
1665static BOOLEAN idPrepare(leftv res, leftv h)
1666{
1667  //        extern int rGetISPos(const int p, const ring r);
1668
1669  const ring r = currRing;
1670
1671  const bool isSyz = rIsSyzIndexRing(r);
1672  const int posIS = rGetISPos(0, r);
1673
1674
1675  if ( !( (h!=NULL) && (h->Typ()==MODUL_CMD) && (h->Data() != NULL) ) )
1676  {
1677    WerrorS("`idPrepare(<module>)` expected");
1678    return TRUE;
1679  }
1680
1681  const ideal I = reinterpret_cast<ideal>(h->Data());
1682
1683  assume( I != NULL );
1684  idTest(I);
1685
1686  int iComp = -1;
1687
1688  h=h->next;
1689  if ( (h!=NULL) && (h->Typ()==INT_CMD) )
1690  {
1691    iComp = (int)((long)(h->Data()));
1692  } else
1693  {
1694      if( (!isSyz) && (-1 == posIS) )
1695      {
1696        WerrorS("`idPrepare(<...>)` called on incompatible ring (not created by 'MakeSyzCompOrdering' or 'MakeInducedSchreyerOrdering'!)");
1697        return TRUE;
1698      }
1699
1700    if( isSyz )
1701      iComp = rGetCurrSyzLimit(r);
1702    else
1703      iComp = id_RankFreeModule(r->typ[posIS].data.is.F, r); // ;
1704  }
1705 
1706  assume(iComp >= 0);
1707
1708
1709  intvec* w = reinterpret_cast<intvec *>(atGet(h, "isHomog", INTVEC_CMD));
1710  tHomog hom = testHomog;
1711
1712  //           int add_row_shift = 0;
1713  //
1714  if (w!=NULL)
1715  {
1716    w = ivCopy(w);
1717  //             add_row_shift = ww->min_in();
1718  //
1719  //             (*ww) -= add_row_shift;
1720  //             
1721  //             if (idTestHomModule(I, currQuotient, ww))
1722  //             {
1723    hom = isHomog;
1724  //               w = ww;
1725  //             }
1726  //             else
1727  //             {
1728  //               //WarnS("wrong weights");
1729  //               delete ww;
1730  //               w = NULL;
1731  //               hom=testHomog;
1732  //             }
1733  }
1734
1735
1736  // computes syzygies of h1,
1737  // works always in a ring with ringorder_s
1738  // NOTE: rSetSyzComp(syzcomp) should better be called beforehand
1739  //        ideal idPrepare (ideal  h1, tHomog hom, int syzcomp, intvec **w);
1740
1741  ideal J = // idPrepare( I, hom, iComp, &w);
1742           kStd(I, currQuotient, hom, &w, NULL, iComp);
1743
1744  idTest(J);
1745
1746  if (w!=NULL)
1747    atSet(res, omStrDup("isHomog"), w, INTVEC_CMD);
1748  //             if (w!=NULL) delete w;
1749
1750  res->rtyp = MODUL_CMD;
1751  res->data = reinterpret_cast<void *>(J);
1752  return FALSE;
1753}
1754
1755/// Get raw syzygies (idPrepare)
1756static BOOLEAN _p_Content(leftv res, leftv h)
1757{
1758  if ( !( (h!=NULL) && (h->Typ()==POLY_CMD) && (h->Data() != NULL) ) )
1759  {
1760    WerrorS("`p_Content(<poly-var>)` expected");
1761    return TRUE;
1762  }
1763
1764
1765  const poly p = reinterpret_cast<poly>(h->Data());
1766
1767 
1768  pTest(p);  pWrite(p); PrintLn();
1769
1770 
1771  p_Content( p, currRing);     
1772
1773  pTest(p);
1774  pWrite(p); PrintLn();
1775 
1776  NoReturn(res);
1777  return FALSE;
1778}
1779
1780static BOOLEAN _m2_end(leftv res, leftv h)
1781{
1782  int ret = 0;
1783 
1784  if ( (h!=NULL) && (h->Typ()!=INT_CMD) )
1785  {
1786    WerrorS("`m2_end([<int>])` expected");
1787    return TRUE;
1788  }
1789  ret = (int)(long)(h->Data());
1790
1791  m2_end( ret );
1792
1793  NoReturn(res);
1794  return FALSE;
1795}
1796
1797   
1798
1799END_NAMESPACE
1800
1801
1802int SI_MOD_INIT(syzextra)(SModulFunctions* psModulFunctions) 
1803{
1804#define ADD0(A,B,C,D,E) A(B, (char*)C, D, E)
1805// #define ADD(A,B,C,D,E) ADD0(iiAddCproc, "", C, D, E)
1806  #define ADD(A,B,C,D,E) ADD0(A->iiAddCproc, B, C, D, E)
1807  ADD(psModulFunctions, currPack->libname, "ClearContent", FALSE, _ClearContent);
1808  ADD(psModulFunctions, currPack->libname, "ClearDenominators", FALSE, _ClearDenominators);
1809
1810  ADD(psModulFunctions, currPack->libname, "m2_end", FALSE, _m2_end);
1811
1812  ADD(psModulFunctions, currPack->libname, "DetailedPrint", FALSE, DetailedPrint);
1813  ADD(psModulFunctions, currPack->libname, "leadmonomial", FALSE, _leadmonom);
1814  ADD(psModulFunctions, currPack->libname, "leadcomp", FALSE, leadcomp);
1815  ADD(psModulFunctions, currPack->libname, "leadrawexp", FALSE, leadrawexp);
1816
1817  ADD(psModulFunctions, currPack->libname, "ISUpdateComponents", FALSE, ISUpdateComponents);
1818  ADD(psModulFunctions, currPack->libname, "SetInducedReferrence", FALSE, SetInducedReferrence);
1819  ADD(psModulFunctions, currPack->libname, "GetInducedData", FALSE, GetInducedData);
1820  ADD(psModulFunctions, currPack->libname, "SetSyzComp", FALSE, SetSyzComp);
1821  ADD(psModulFunctions, currPack->libname, "MakeInducedSchreyerOrdering", FALSE, MakeInducedSchreyerOrdering);
1822  ADD(psModulFunctions, currPack->libname, "MakeSyzCompOrdering", FALSE, MakeSyzCompOrdering);
1823
1824  ADD(psModulFunctions, currPack->libname, "ProfilerStart", FALSE, _ProfilerStart); ADD(psModulFunctions, currPack->libname, "ProfilerStop",  FALSE, _ProfilerStop );
1825 
1826  ADD(psModulFunctions, currPack->libname, "noop", FALSE, noop);
1827  ADD(psModulFunctions, currPack->libname, "idPrepare", FALSE, idPrepare);
1828  ADD(psModulFunctions, currPack->libname, "reduce_syz", FALSE, reduce_syz);
1829
1830  ADD(psModulFunctions, currPack->libname, "p_Content", FALSE, _p_Content);
1831
1832  ADD(psModulFunctions, currPack->libname, "Tail", FALSE, Tail);
1833 
1834  ADD(psModulFunctions, currPack->libname, "ComputeLeadingSyzygyTerms", FALSE, _ComputeLeadingSyzygyTerms);
1835  ADD(psModulFunctions, currPack->libname, "Compute2LeadingSyzygyTerms", FALSE, _Compute2LeadingSyzygyTerms);
1836 
1837  ADD(psModulFunctions, currPack->libname, "Sort_c_ds", FALSE, _Sort_c_ds);
1838  ADD(psModulFunctions, currPack->libname, "FindReducer", FALSE, _FindReducer);
1839
1840
1841  ADD(psModulFunctions, currPack->libname, "ReduceTerm", FALSE, _ReduceTerm);
1842  ADD(psModulFunctions, currPack->libname, "TraverseTail", FALSE, _TraverseTail);
1843
1844   
1845  ADD(psModulFunctions, currPack->libname, "SchreyerSyzygyNF", FALSE, _SchreyerSyzygyNF);
1846  ADD(psModulFunctions, currPack->libname, "ComputeSyzygy", FALSE, _ComputeSyzygy);
1847 
1848  //  ADD(psModulFunctions, currPack->libname, "GetAMData", FALSE, GetAMData);
1849
1850  //  ADD(psModulFunctions, currPack->libname, "", FALSE, );
1851
1852#undef ADD 
1853  return 0;
1854}
1855
1856#ifndef EMBED_PYTHON
1857extern "C" { 
1858int mod_init(SModulFunctions* psModulFunctions)
1859{ 
1860  return SI_MOD_INIT(syzextra)(psModulFunctions); 
1861}
1862}
1863#endif
Note: See TracBrowser for help on using the repository browser.