===================================================================
RCS file: /home/cvs/OpenXM_contrib2/asir2018/engine/dist.c,v
retrieving revision 1.6
retrieving revision 1.10
diff -u -p -r1.6 -r1.10
--- OpenXM_contrib2/asir2018/engine/dist.c	2019/09/05 08:49:43	1.6
+++ OpenXM_contrib2/asir2018/engine/dist.c	2019/10/11 03:45:57	1.10
@@ -45,7 +45,7 @@
  * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
  * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
  *
- * $OpenXM: OpenXM_contrib2/asir2018/engine/dist.c,v 1.5 2019/09/04 05:32:10 noro Exp $
+ * $OpenXM: OpenXM_contrib2/asir2018/engine/dist.c,v 1.9 2019/09/19 06:29:47 noro Exp $
 */
 #include "ca.h"
 
@@ -333,7 +333,7 @@ void nodetod(NODE node,DP *dp)
 void nodetodpm(NODE node,Obj pos,DPM *dp)
 {
   NODE t;
-  int len,i,td;
+  int len,i,td,p;
   Q e;
   DL d;
   DMM m;
@@ -352,7 +352,13 @@ void nodetodpm(NODE node,Obj pos,DPM *dp)
     }
   }
   d->td = td;
-  NEWDMM(m); m->dl = d; m->pos = ZTOS((Q)pos); C(m) = (Obj)ONE; NEXT(m) = 0;
+  p = ZTOS((Q)pos);
+  if ( dp_current_spec->module_rank ) {
+    if ( p > dp_current_spec->module_rank )
+      error("nodetodpm : inconsistent order spec");
+    d->td += dp_current_spec->module_top_weight[p-1];
+  }
+  NEWDMM(m); m->dl = d; m->pos = p; C(m) = (Obj)ONE; NEXT(m) = 0;
   MKDPM(len,m,u); u->sugar = td; *dp = u;
 }
 
@@ -360,12 +366,20 @@ void dtodpm(DP d,int pos,DPM *dp)
 {
   DMM mr0,mr;
   MP m;
+  int shift;
 
   if ( !d ) *dp = 0;
   else {
+    shift = 0;
+    if ( dp_current_spec->module_rank ) {
+      if ( pos > dp_current_spec->module_rank )
+        error("nodetodpm : inconsistent order spec");
+      shift = dp_current_spec->module_top_weight[pos-1];
+    } 
     for ( m = BDY(d), mr0 = 0; m; m = NEXT(m) ) {
       NEXTDMM(mr0,mr);
       mr->dl = m->dl;
+      mr->dl->td += shift;
       mr->pos = pos;
       C(mr) = C(m);
     }
@@ -2635,29 +2649,36 @@ NBP harmonic_mul_nbm(NBM a,NBM b)
 /* DPM functions */
 
 DMMstack dmm_stack;
+int dpm_base_ordtype;;
 
-void push_schreyer_order(LIST data)
+DMMstack push_schreyer_order(LIST data,DMMstack stack)
 {
   DMMstack t;
   int len,i;
-  NODE in;
+  NODE in,t1;
+  LIST l;
 
   /* data = [DPM,...,DPM] */
+  if ( !dmm_stack && ( !dp_current_spec || dp_current_spec->id < 256 ) )
+      error("push_schreyer_order : base module order is not set");
   in = BDY(data);
   len = length(in);
   NEWDMMstack(t);
   t->rank = len;
   t->in = (DMM *)MALLOC((len+1)*sizeof(DMM));
-  t->ordtype = 0;
+  if ( stack ) {
+    MKNODE(t1,data,BDY(stack->obj)); MKLIST(l,t1); t->obj = l;
+  } else {
+    MKNODE(t1,data,0); MKLIST(l,t1); t->obj = l;
+  }
   for ( i = 1; i <= len; i++, in = NEXT(in) ) {
     t->in[i] = BDY((DPM)BDY(in));
   }
-  t->next = dmm_stack;
-  dmm_stack = t; 
-  dpm_ordtype = 2;
+  t->next = stack;;
+  dpm_ordtype = 3;
+  return t;
 }
 
-
 // data=[Ink,...,In0]
 // Ini = a list of module monomials
 
@@ -2670,18 +2691,20 @@ void set_schreyer_order(LIST data)
   if ( !data ) {
     dmm_stack = 0;
     if ( dp_current_spec && dp_current_spec->id >= 256 )
-      dpm_ordtype = dp_current_spec->ispot;
+      dpm_ordtype = dp_current_spec->module_ordtype;
     else 
       dpm_ordtype = 0;
     return;
   } else {
+    if ( !dp_current_spec || dp_current_spec->id < 256 )
+      error("set_schreyer_order : base module order is not set");
     dmm_stack = 0;
+    dpm_base_ordtype = dp_current_spec->module_ordtype;
     in = BDY(data);
     len = length(in);
     w = (LIST *)MALLOC(len*sizeof(LIST));
     for ( i = 0; i < len; i++, in = NEXT(in) ) w[i] = (LIST)BDY(in);
-    for ( i = len-1; i >= 0; i-- ) push_schreyer_order(w[i]);
-    dpm_ordtype = 2;
+    for ( i = len-1; i >= 0; i-- ) dmm_stack = push_schreyer_order(w[i],dmm_stack);
   }
 }
 
@@ -2714,8 +2737,8 @@ NODE dpm_reduceall(NODE in)
   for ( i = 0, t = in; i < n; i++, t = NEXT(t) ) ps[i] = BDY(t);
   for ( i = 0; i < n; i++ ) {
     g = ps[i]; ps[i] = 0;
-    dpm_nf_z(0,g,psv,1,DP_Multiple,&r);
-    ps[i] = r;
+//    dpm_nf_z(0,g,psv,1,DP_Multiple,&ps[i]);
+    dpm_nf_z(0,g,psv,1,0,&ps[i]);
   }
   t = 0;
   for ( i = n-1; i >= 0; i-- ) {
@@ -2725,10 +2748,15 @@ NODE dpm_reduceall(NODE in)
   return t;
 }
 
+struct oEGT egra;
+
+void dpm_ht(DPM d,DPM *r);
+
 void dpm_schreyer_base(LIST g,LIST *s)
 {
-  NODE nd,t,b0,b;
+  NODE nd,t0,t,b0,b;
   int n,i,j,k,nv;
+  LIST l;
   Z cont;
   P dn,c;
   DP h,t1,t2;
@@ -2738,11 +2766,10 @@ void dpm_schreyer_base(LIST g,LIST *s)
   DPM *ps;
   VECT psv;
   DP **m,*quo;
-  struct oEGT eg_nf,eg0,eg1;
-  extern struct oEGT egc,egcomp;
+  struct oEGT eg0,eg1;
+  extern struct oEGT egred;
 
-//  init_eg(&eg_nf);
-//  init_eg(&egcomp);
+  init_eg(&egra);
   nd = BDY(g);
   n = length(nd);
   MKVECT(psv,n);
@@ -2751,12 +2778,10 @@ void dpm_schreyer_base(LIST g,LIST *s)
   nv = ps[0]->nv;
   m = (DP **)almat_pointer(n,n);
   b0 = 0;
-//  init_eg(&egc);
   for ( i = 0; i < n; i++ ) {
     // sp(ps[i],ps[j]) = ti*ps[i]-tj*ps[j] => m[i][j] = ti
     for ( j = i+1; j < n; j++ ) m[i][j] = dpm_sp_hm(ps[i],ps[j]);
     for ( j = i+1; j < n; j++ ) {
-//    for ( j = n-1; j > i; j-- ) {
       if ( !m[i][j] ) continue;
       for ( h = m[i][j], k = i+1; k < n; k++ )
         if ( k != j && m[i][k] && dp_redble(m[i][k],h) ) m[i][k] = 0;
@@ -2764,9 +2789,7 @@ void dpm_schreyer_base(LIST g,LIST *s)
     for ( j = i+1; j < n; j++ ) {
       if ( m[i][j] ) {
         dpm_sp(ps[i],ps[j],&sp,&t1,&t2);
-//        get_eg(&eg0);
         quo = dpm_nf_and_quotient(0,sp,psv,&nf,&dn);
-//        get_eg(&eg1); add_eg(&eg_nf,&eg0,&eg1);
         if ( nf ) 
           error("dpm_schreyer_base : cannot happen");
         NEWDMM(r0); r = r0; 
@@ -2789,16 +2812,22 @@ void dpm_schreyer_base(LIST g,LIST *s)
     }
     if ( b0 ) NEXT(b) = 0;
   }
-  push_schreyer_order(g);
+  for ( t0 = t, nd = BDY(g); nd; nd = NEXT(nd) ) {
+    dpm_ht((DPM)BDY(nd),&dpm); NEXTNODE(t0,t); BDY(t) = (pointer)dpm;
+  }
+  if ( t0 ) NEXT(t) = 0;
+  MKLIST(l,t0);
+  dmm_stack = push_schreyer_order(l,dmm_stack);
   for ( t = b0; t; t = NEXT(t) ) {
     dpm_sort((DPM)BDY(t),&dpm); 
     BDY(t) = (pointer)dpm;
   }
   b0 = dpm_sort_list(b0);
-//  b0 = dpm_reduceall(b0);
+// get_eg(&eg0);
+  b0 = dpm_reduceall(b0);
+// get_eg(&eg1); add_eg(&egra,&eg0,&eg1); print_eg("RA",&egra);
   MKLIST(*s,b0);
-//  print_eg("nf",&eg_nf); printf("\n");
-//  print_eg("coef",&egc); printf("\n");
+//  print_eg("red",&egred); printf("\n");
 }
 
 int compdmm_schreyer(int n,DMM m1,DMM m2)
@@ -2831,7 +2860,7 @@ int compdmm_schreyer(int n,DMM m1,DMM m2)
    }
    // comparison by the bottom order
 LAST:
-  if ( dpm_ordtype == 1 ) {
+  if ( dpm_base_ordtype == 1 ) {
     if ( pos1 < pos2 ) return 1;
     else if ( pos1 > pos2 ) return -1;
     else return (*cmpdl)(n,d1,d2);
@@ -2844,49 +2873,34 @@ LAST:
   }
 }
 
-#if 1
 int compdmm(int n,DMM m1,DMM m2)
 {
   int t;
 
   switch ( dpm_ordtype ) {
-  case 0:
+  case 0: /* TOP ord->pos */
     t = (*cmpdl)(n,m1->dl,m2->dl);
     if ( t ) return t;
     else if ( m1->pos < m2->pos ) return 1;
     else if ( m1->pos > m2->pos ) return -1;
     else return 0;
-  case 1:
+  case 1: /* POT : pos->ord */
     if ( m1->pos < m2->pos ) return 1;
     else if ( m1->pos > m2->pos ) return -1;
     else return (*cmpdl)(n,m1->dl,m2->dl);
-  case 2:
+  case 2:  /* wPOT: weight->pos->ord */
+    if ( m1->dl->td > m2->dl->td ) return 1;
+    else if ( m1->dl->td < m2->dl->td ) return 1;
+    else if ( m1->pos < m2->pos ) return 1;
+    else if ( m1->pos > m2->pos ) return -1;
+    else return (*cmpdl)(n,m1->dl,m2->dl);
+  case 3: /* Schreyer */
     return compdmm_schreyer(n,m1,m2);
   default:
     error("compdmm : invalid dpm_ordtype");
   }
 }
-#else
-int compdmm(int n,DMM m1,DMM m2)
-{
-  int t;
 
-  if ( dpm_ordtype == 1 ) {
-    if ( m1->pos < m2->pos ) return 1;
-    else if ( m1->pos > m2->pos ) return -1;
-    else return (*cmpdl)(n,m1->dl,m2->dl);
-  } else if ( dpm_ordtype == 0 ) {
-    t = (*cmpdl)(n,m1->dl,m2->dl);
-    if ( t ) return t;
-    else if ( m1->pos < m2->pos ) return 1;
-    else if ( m1->pos > m2->pos ) return -1;
-    else return 0;
-  } else if ( dpm_ordtype == 2 ) {
-    return compdmm_schreyer(n,m1,m2);
-  }
-}
-#endif
-
 void adddpm(VL vl,DPM p1,DPM p2,DPM *pr)
 {
   int n,s;
@@ -2965,6 +2979,26 @@ void chsgndpm(DPM p,DPM *pr)
   }
 }
 
+void mulcmp(Obj c,MP m)
+{
+  MP t;
+  Obj c1;
+
+  for ( t = m; t; t = NEXT(t) ) {
+    arf_mul(CO,c,C(t),&c1); C(t) = c1;
+  }
+}
+
+void mulcdmm(Obj c,DMM m)
+{
+  DMM t;
+  Obj c1;
+
+  for ( t = m; t; t = NEXT(t) ) {
+    arf_mul(CO,c,C(t),&c1); C(t) = c1;
+  }
+}
+
 void mulcdpm(VL vl,Obj c,DPM p,DPM *pr)
 {
   DMM m,mr=0,mr0;
@@ -3123,6 +3157,7 @@ int compdpm(VL vl,DPM p1,DPM p2)
 DPM dpm_eliminate_term(DPM a,DPM p,Obj c,int pos)
 {
   MP d0,d;
+  DL dl;
   DMM m;
   DP f;
   DPM a1,p1,r;
@@ -3131,7 +3166,15 @@ DPM dpm_eliminate_term(DPM a,DPM p,Obj c,int pos)
   d0 = 0;
   for ( m = BDY(a); m; m = NEXT(m) )
     if ( m->pos == pos ) {
-      NEXTMP(d0,d); d->dl = m->dl; arf_chsgn(m->c,&d->c);
+      NEXTMP(d0,d); 
+      arf_chsgn(m->c,&d->c);
+      if ( !dp_current_spec ) d->dl = m->dl; 
+      else {
+        NEWDL(dl,NV(a));
+        _copydl(NV(a),m->dl,dl);
+        dl->td -= dp_current_spec->module_top_weight[pos-1];
+        d->dl = dl;
+      }
     }
   if ( d0 ) {
     NEXT(d) = 0; MKDP(NV(a),d0,f);
@@ -3164,14 +3207,15 @@ DPM dpm_compress(DPM p,int *tab)
 }
 
 // input : s, s = syz(m) output simplified s, m
-void dpm_simplify_syz(LIST s,LIST m,LIST *s1,LIST *m1)
+void dpm_simplify_syz(LIST s,LIST m,LIST *s1,LIST *m1,LIST *w1)
 {
-  int lm,ls,i,j,pos;
+  int lm,ls,i,j,k,pos,nv;
   DPM *am,*as;
   DPM p;
   DMM d;
   Obj c;
-  int *tab;
+  Z q;
+  int *tab,*dd,*new_w;
   NODE t,t1;
 
   lm = length(BDY(m));
@@ -3184,7 +3228,12 @@ void dpm_simplify_syz(LIST s,LIST m,LIST *s1,LIST *m1)
   for ( i = 0; i < ls; i++ ) {
     p = as[i];
     if ( p == 0 ) continue;
-    for ( d = BDY(p); d; d = NEXT(d) ) if ( d->dl->td == 0 ) break;
+    nv = NV(p);
+    for ( d = BDY(p); d; d = NEXT(d) ) {
+      dd = d->dl->d;
+      for ( k = 0; k < nv; k++ ) if ( dd[k] ) break;
+      if ( k == nv ) break;
+    }
     if ( d ) {
       c = d->c; pos = d->pos;
       for ( j = 0; j < ls; j++ )
@@ -3202,7 +3251,7 @@ void dpm_simplify_syz(LIST s,LIST m,LIST *s1,LIST *m1)
   tab = (int *)MALLOC((lm+1)*sizeof(int));
   for ( j = 0, i = 1; i <= lm; i++ ) {
     if ( am[i] ) { j++; tab[i] = j; }
-    else tab[i] = 0;
+    else { tab[i] = 0; }
   }
   t = 0;
   for ( i = ls-1; i >= 0; i-- )
@@ -3211,6 +3260,20 @@ void dpm_simplify_syz(LIST s,LIST m,LIST *s1,LIST *m1)
       MKNODE(t1,(pointer)p,t); t = t1;
     }
   MKLIST(*s1,t);
+
+  if ( dp_current_spec->module_rank ) {
+    new_w = (int *)MALLOC(j*sizeof(int));
+    for ( j = 0, i = 1; i <= lm; i++ )
+      if ( tab[i] ) { new_w[j++] = dp_current_spec->module_top_weight[i-1]; }
+    t = 0;
+    for ( i = j-1; i >= 0; i-- ) {
+      STOZ(new_w[i],q);
+      MKNODE(t1,q,t); t = t1;
+    }
+  } else
+    t = 0;
+  MKLIST(*w1,t);
+
   t = 0;
   for ( i = lm; i >= 1; i-- )
     if ( am[i] ) {