===================================================================
RCS file: /home/cvs/OpenXM_contrib2/asir2018/engine/dist.c,v
retrieving revision 1.13
retrieving revision 1.19
diff -u -p -r1.13 -r1.19
--- OpenXM_contrib2/asir2018/engine/dist.c	2019/11/12 12:50:40	1.13
+++ OpenXM_contrib2/asir2018/engine/dist.c	2019/12/27 08:13:59	1.19
@@ -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.12 2019/11/12 07:47:45 noro Exp $
+ * $OpenXM: OpenXM_contrib2/asir2018/engine/dist.c,v 1.18 2019/12/12 04:44:59 noro Exp $
 */
 #include "ca.h"
 
@@ -353,7 +353,7 @@ void nodetodpm(NODE node,Obj pos,DPM *dp)
   }
   d->td = td;
   p = ZTOS((Q)pos);
-  if ( dp_current_spec->module_rank ) {
+  if ( dp_current_spec->module_top_weight ) {
     if ( p > dp_current_spec->module_rank )
       error("nodetodpm : inconsistent order spec");
     d->td += dp_current_spec->module_top_weight[p-1];
@@ -371,7 +371,7 @@ void dtodpm(DP d,int pos,DPM *dp)
   if ( !d ) *dp = 0;
   else {
     shift = 0;
-    if ( dp_current_spec->module_rank ) {
+    if ( dp_current_spec->module_top_weight ) {
       if ( pos > dp_current_spec->module_rank )
         error("nodetodpm : inconsistent order spec");
       shift = dp_current_spec->module_top_weight[pos-1];
@@ -2654,7 +2654,11 @@ int dpm_base_ordtype;;
 DMMstack push_schreyer_order(LIST data,DMMstack stack)
 {
   DMMstack t;
-  int len,i;
+  DP dp;
+  MP mp;
+  DMM m0,m1;
+  DPM dpm0,dpm1;
+  int len,i,nv;
   NODE in,t1;
   LIST l;
 
@@ -2666,14 +2670,24 @@ DMMstack push_schreyer_order(LIST data,DMMstack stack)
   NEWDMMstack(t);
   t->rank = len;
   t->in = (DMM *)MALLOC((len+1)*sizeof(DMM));
+  t->sum = (DMM *)MALLOC((len+1)*sizeof(DMM));
   if ( stack ) {
     MKNODE(t1,data,BDY(stack->obj)); MKLIST(l,t1); t->obj = l;
+    for ( i = 1; i <= len; i++, in = NEXT(in) ) {
+       m1 = t->in[i] = BDY((DPM)BDY(in));
+       NEWMP(mp); mp->dl = m1->dl; mp->c = m1->c; NEXT(mp) = 0;
+       nv = ((DPM)BDY(in))->nv;
+       MKDP(nv,mp,dp); dp->sugar = mp->dl->td;
+       m0 = stack->sum[m1->pos]; MKDPM(nv,m0,dpm0);
+       mulobjdpm(CO,(Obj)dp,dpm0,&dpm1);
+       t->sum[i] = BDY(dpm1);
+    }
   } else {
     MKNODE(t1,data,0); MKLIST(l,t1); t->obj = l;
+    for ( i = 1; i <= len; i++, in = NEXT(in) ) {
+      t->sum[i] = t->in[i] = BDY((DPM)BDY(in));
+    }
   }
-  for ( i = 1; i <= len; i++, in = NEXT(in) ) {
-    t->in[i] = BDY((DPM)BDY(in));
-  }
   t->next = stack;;
   dpm_ordtype = 3;
   return t;
@@ -2708,6 +2722,26 @@ void set_schreyer_order(LIST data)
   }
 }
 
+void set_schreyer_level(DMMstack_array array,int level)
+{
+  if ( !level ) {
+    dmm_stack = 0;
+    if ( dp_current_spec && dp_current_spec->id >= 256 )
+      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_level : base module order is not set");
+    if ( level > array->len )
+      error("set_schreyer_level : invalid level");
+    dmm_stack = array->body[level-1];
+    dpm_base_ordtype = dp_current_spec->module_ordtype;
+    dpm_ordtype = 3;
+  }
+}
+
 // construct a base of syz(g) 
 // assuming the schrerer order is properly set
 
@@ -2716,6 +2750,7 @@ void dpm_sp(DPM p1,DPM p2,DPM *sp,DP *t1,DP *t2);
 DPM dpm_nf_and_quotient3(DPM sp,VECT psv,DPM *nf,P *dn);
 DPM dpm_nf_and_quotient4(DPM sp,DPM *ps,VECT psiv,DPM head,DPM *nf,P *dn);
 DPM dpm_sp_nf(VECT psv,VECT psiv,int i,int j,DPM *nf);
+DPM dpm_sp_nf_zlist(VECT psv,VECT psiv,int i,int j,int top,DPM *nf);
 DPM dpm_sp_nf_asir(VECT psv,int i,int j,DPM *nf);
 void dpm_sort(DPM p,DPM *r);
 
@@ -2754,192 +2789,167 @@ NODE dpm_reduceall(NODE in)
 struct oEGT egra;
 
 void dpm_ht(DPM d,DPM *r);
+NODE reverse_node(NODE b);
 
-#if 0
 void dpm_schreyer_base(LIST g,LIST *s)
 {
   NODE nd,t0,t,b0,b;
   int n,i,j,k,nv,max,pos;
   LIST l;
-  Z cont;
-  P dn,c;
   DP h,t1,t2;
   MP d;
   DMM r0,r,r1;
   DPM sp,nf,dpm;
   DPM *ps;
-  VECT psv,psv2;
+  VECT psv,psiv;
   DPM quo;
-  DP **m;
-  NODE2 *ps2;
-  NODE2 nd2;
-  struct oEGT eg0,eg1;
+  DP *mm;
+  NODE *psi;
+  NODE n1,n2,n3;
+  int p1,p2,p3;
+  struct oEGT eg0,eg1,egsp,egnf;
   extern struct oEGT egred;
+  extern int sch_count,schrec_count,schlast_count;
 
+  sch_count = schlast_count= 0;
   init_eg(&egra);
+  init_eg(&egsp);
+  init_eg(&egnf);
   nd = BDY(g);
   n = length(nd);
-  MKVECT(psv,n);
+  MKVECT(psv,n+1);
   ps = (DPM *)BDY(psv);
-  for ( i = 0, t = nd; i < n; i++, t = NEXT(t) ) ps[i] = (DPM)BDY(t);
-  for ( i = 0, max = 0; i < n; i++ )
+  for ( i = 1, t = nd; i <= n; i++, t = NEXT(t) ) ps[i] = (DPM)BDY(t);
+  for ( i = 1, max = 0; i <= n; i++ )
     if ( (pos=BDY(ps[i])->pos) > max ) max = pos;
-  MKVECT(psv2,max+1);
-  ps2 = (NODE2 *)BDY(psv2);
-  for ( i = n-1; i >= 0; i-- ) {
+  MKVECT(psiv,max+1);
+  psi = (NODE *)BDY(psiv);
+  for ( i = n; i >= 1; i-- ) {
     pos = BDY(ps[i])->pos;
-    MKNODE2(nd2,ps[i],(long)i+1,ps2[pos]); ps2[pos] = nd2;
+    MKNODE(nd,(long)i,psi[pos]); psi[pos] = nd;
   }
-  nv = ps[0]->nv;
-  m = (DP **)almat_pointer(n,n);
+  nv = ps[1]->nv;
+  mm = (DP *)MALLOC((n+1)*sizeof(DP));
   b0 = 0;
-  k = 0;
-  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++ ) {
-      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;
-    }
-    for ( j = i+1; j < n; j++ ) {
-      if ( m[i][j] ) {
-        DPM quo1,nf1;
-        P dn1;
-
-        dpm_sp(ps[i],ps[j],&sp,&t1,&t2);
-        quo = dpm_nf_and_quotient3(sp,psv2,&nf,&dn);
-        if ( nf ) 
-          error("dpm_schreyer_base : cannot happen");
-        NEWDMM(r0); r = r0; 
-        mulp(CO,(P)BDY(t1)->c,dn,(P *)&r->c); r->pos = i+1; r->dl = BDY(t1)->dl;
-        NEWDMM(NEXT(r)); r=NEXT(r);
-        mulp(CO,(P)BDY(t2)->c,dn,&c); chsgnp(c,(P *)&r->c); r->pos = j+1; r->dl = BDY(t2)->dl;
-        if ( quo ) {
-          for ( r1 = BDY(quo); r1; r1 = NEXT(r1) ) {
-            chsgnp((P)r1->c,&c); r1->c = (Obj)c;
-          }
-          NEXT(r) = BDY(quo);
-        } else
-          NEXT(r) = 0;
-        MKDPM(nv,r0,dpm); // XXX : sugar is not set
-        NEXTNODE(b0,b);
-        BDY(b) = (pointer)dpm;
-        k++;
+  get_eg(&eg0);
+  for ( i = 1; i <= max; i++ ) {
+    memset(mm,0,(n+1)*sizeof(DP));
+    for ( n1 = psi[i]; n1; n1 = NEXT(n1) ) {
+      p1 = (long)BDY(n1);
+      for ( n2 = NEXT(n1); n2; n2 = NEXT(n2) ) {
+        p2 = (long)BDY(n2);
+        mm[p2] = dpm_sp_hm(ps[p1],ps[p2]);
       }
+      for ( n2 = NEXT(n1); n2; n2 = NEXT(n2) ) {
+        p2 = (long)BDY(n2);
+        if ( !mm[p2] ) continue;
+        for ( h = mm[p2], n3 = NEXT(n1); n3; n3 = NEXT(n3) ) {
+          p3 = (long)BDY(n3);
+          if ( n3 != n2 && mm[p3] && dp_redble(mm[p3],h) ) mm[p3] = 0;
+        }
+      }
+      for ( j = p1+1; j <= n; j++ ) {
+        if ( mm[j] ) {
+          quo = dpm_sp_nf(psv,psiv,p1,j,&nf);
+          if ( nf ) 
+            error("dpm_schreyer_base : cannot happen");
+          NEXTNODE(b0,b); BDY(b) = (pointer)quo;
+        }
+      }
     }
-    if ( b0 ) NEXT(b) = 0;
   }
-  for ( t0 = t, nd = BDY(g); nd; nd = NEXT(nd) ) {
+  get_eg(&eg1); add_eg(&egsp,&eg0,&eg1); print_eg("SP",&egsp);
+  get_eg(&eg0);
+  get_eg(&eg1); add_eg(&egnf,&eg0,&eg1); print_eg("NF",&egnf); printf("\n");
+  if ( b0 ) NEXT(b) = 0;
+  for ( t0 = 0, 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);
-  get_eg(&eg0);
-  b0 = dpm_reduceall(b0);
-  get_eg(&eg1); add_eg(&egra,&eg0,&eg1); print_eg("RA",&egra);
+//  b0 = dpm_sort_list(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("red",&egred); printf("\n");
+  printf("sch_count=%d, schlast_count=%d\n",sch_count,schlast_count);
 }
-#elif 0
-void dpm_schreyer_base(LIST g,LIST *s)
+
+void dpm_list_to_array(LIST g,VECT *psvect,VECT *psindvect)
 {
-  NODE nd,t0,t,b0,b;
-  int n,i,j,k,nv,max,pos;
-  LIST l;
-  Z cont;
-  P dn,c;
-  DP h,t1,t2;
-  MP d;
-  DMM r0,r,r1;
-  DPM sp,nf,dpm;
+  NODE nd,t;
+  int n;
+  VECT psv,psiv;
   DPM *ps;
-  VECT psiv;
-  DPM quo;
-  DP **m;
-  NODE *psi;
-  struct oEGT eg0,eg1;
-  extern struct oEGT egred;
+  int i,max,pos;
+  LIST *psi;
+  LIST l;
+  Z iz;
 
-  init_eg(&egra);
   nd = BDY(g);
   n = length(nd);
-  ps = (DPM *)MALLOC(n*sizeof(DPM));
-  for ( i = 0, t = nd; i < n; i++, t = NEXT(t) ) ps[i] = (DPM)BDY(t);
-  for ( i = 0, max = 0; i < n; i++ )
+  MKVECT(psv,n+1);
+  ps = (DPM *)BDY(psv);
+  for ( i = 1, t = nd; i <= n; i++, t = NEXT(t) ) ps[i] = (DPM)BDY(t);
+  for ( i = 1, max = 0; i <= n; i++ )
     if ( (pos=BDY(ps[i])->pos) > max ) max = pos;
   MKVECT(psiv,max+1);
-  psi = (NODE *)BDY(psiv);
-  for ( i = n-1; i >= 0; i-- ) {
+  psi = (LIST *)BDY(psiv);
+  for ( i = 0; i <= max; i++ ) {
+    MKLIST(l,0); psi[i] = l;
+  }
+  for ( i = n; i >= 1; i-- ) {
     pos = BDY(ps[i])->pos;
-    MKNODE(nd,(long)(i+1),psi[pos]); psi[pos] = nd;
+    STOZ(i,iz); MKNODE(nd,iz,BDY(psi[pos])); BDY(psi[pos]) = nd;
   }
-  nv = ps[0]->nv;
-  m = (DP **)almat_pointer(n,n);
-  b0 = 0;
-  k = 0;
-  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++ ) {
-      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;
-    }
-    for ( j = i+1; j < n; j++ ) {
-      if ( m[i][j] ) {
-        DPM quo1,nf1;
-        P dn1;
+  *psvect = psv; *psindvect = psiv;
+}
 
-        dpm_sp(ps[i],ps[j],&sp,&t1,&t2);
-        quo = dpm_nf_and_quotient4(sp,ps,psiv,0,&nf,&dn);
-        if ( nf ) 
-          error("dpm_schreyer_base : cannot happen");
-        NEWDMM(r0); r = r0; 
-        mulp(CO,(P)BDY(t1)->c,dn,(P *)&r->c); r->pos = i+1; r->dl = BDY(t1)->dl;
-        NEWDMM(NEXT(r)); r=NEXT(r);
-        mulp(CO,(P)BDY(t2)->c,dn,&c); chsgnp(c,(P *)&r->c); r->pos = j+1; r->dl = BDY(t2)->dl;
-        if ( quo ) {
-          for ( r1 = BDY(quo); r1; r1 = NEXT(r1) ) {
-            chsgnp((P)r1->c,&c); r1->c = (Obj)c;
-          }
-          NEXT(r) = BDY(quo);
-        } else
-          NEXT(r) = 0;
-        MKDPM(nv,r0,dpm); // XXX : sugar is not set
-        NEXTNODE(b0,b);
-        BDY(b) = (pointer)dpm;
-        k++;
-      }
-    }
-    if ( b0 ) NEXT(b) = 0;
+#if 0
+void dpm_insert_to_zlist(VECT psiv,int pos,int i)
+{
+  LIST l;
+  NODE prev,cur,nd;
+  int curi;
+  Z iz;
+
+  l = (LIST)BDY(psiv)[pos];
+  for ( prev = 0, cur = BDY(l); cur; cur = NEXT(cur) ) {
+    curi = ZTOS((Q)BDY(cur));
+    if ( curi == i )
+      error("dpm_insert_to_list : invalid index");
+    if ( i < curi ) break;
+    prev = cur;
   }
-  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);
-//  get_eg(&eg0);
-//  b0 = dpm_reduceall(b0);
-//  get_eg(&eg1); add_eg(&egra,&eg0,&eg1); print_eg("RA",&egra);
-  MKLIST(*s,b0);
-//  print_eg("red",&egred); printf("\n");
+  STOZ(i,iz); MKNODE(nd,iz,cur);
+  if ( prev == 0 ) BDY(l) = nd;
+  else NEXT(prev) = nd;
 }
 #else
-void dpm_schreyer_base(LIST g,LIST *s)
+void dpm_insert_to_zlist(VECT psiv,int pos,int i)
 {
+  LIST l;
+  NODE prev,cur,nd;
+  int curi;
+  Z iz;
+
+  l = (LIST)BDY(psiv)[pos];
+  for ( prev = 0, cur = BDY(l); cur; cur = NEXT(cur) ) {
+    curi = ZTOS((Q)BDY(cur));
+    if ( curi == i )
+      error("dpm_insert_to_list : invalid index");
+    prev = cur;
+  }
+  STOZ(i,iz); MKNODE(nd,iz,cur);
+  if ( prev == 0 ) BDY(l) = nd;
+  else NEXT(prev) = nd;
+}
+#endif
+
+void dpm_schreyer_base_zlist(LIST g,LIST *s)
+{
   NODE nd,t0,t,b0,b;
   int n,i,j,k,nv,max,pos;
   LIST l;
@@ -2950,132 +2960,262 @@ void dpm_schreyer_base(LIST g,LIST *s)
   DPM *ps;
   VECT psv,psiv;
   DPM quo;
-  DP **m;
-  NODE *psi;
+  DP *mm;
+  LIST *psi;
   NODE n1,n2,n3;
   int p1,p2,p3;
+  Z iz;
   struct oEGT eg0,eg1,egsp,egnf;
   extern struct oEGT egred;
+  extern int sch_count,schrec_count,schlast_count;
 
+  sch_count = schlast_count= 0;
   init_eg(&egra);
   init_eg(&egsp);
   init_eg(&egnf);
-  nd = BDY(g);
-  n = length(nd);
-  MKVECT(psv,n+1);
+  dpm_list_to_array(g,&psv,&psiv);
   ps = (DPM *)BDY(psv);
-  for ( i = 1, t = nd; i <= n; i++, t = NEXT(t) ) ps[i] = (DPM)BDY(t);
-  for ( i = 1, max = 0; i <= n; i++ )
-    if ( (pos=BDY(ps[i])->pos) > max ) max = pos;
-  MKVECT(psiv,max+1);
-  psi = (NODE *)BDY(psiv);
-  for ( i = n; i >= 1; i-- ) {
-    pos = BDY(ps[i])->pos;
-    MKNODE(nd,(long)i,psi[pos]); psi[pos] = nd;
-  }
+  psi = (LIST *)BDY(psiv);
   nv = ps[1]->nv;
-  m = (DP **)almat_pointer(n+1,n+1);
+  n = psv->len-1;
+  max = psiv->len-1;
+  mm = (DP *)MALLOC((n+1)*sizeof(DP));
   b0 = 0;
   get_eg(&eg0);
   for ( i = 1; i <= max; i++ ) {
-    for ( n1 = psi[i]; n1; n1 = NEXT(n1) ) {
-      p1 = (long)BDY(n1);
+    memset(mm,0,(n+1)*sizeof(DP));
+    for ( n1 = BDY((LIST)psi[i]); n1; n1 = NEXT(n1) ) {
+      p1 = ZTOS((Q)BDY(n1));
       for ( n2 = NEXT(n1); n2; n2 = NEXT(n2) ) {
-        p2 = (long)BDY(n2);
-        m[p1][p2] = dpm_sp_hm(ps[p1],ps[p2]);
+        p2 = ZTOS((Q)BDY(n2));
+        mm[p2] = dpm_sp_hm(ps[p1],ps[p2]);
       }
-    }
-    for ( n1 = psi[i]; n1; n1 = NEXT(n1) ) {
-      p1 = (long)BDY(n1);
       for ( n2 = NEXT(n1); n2; n2 = NEXT(n2) ) {
-        p2 = (long)BDY(n2);
-        if ( !m[p1][p2] ) continue;
-        for ( h = m[p1][p2], n3 = NEXT(n1); n3; n3 = NEXT(n3) ) {
-          p3 = (long)BDY(n3);
-          if ( n3 != n2 && m[p1][p3] && dp_redble(m[p1][p3],h) ) m[p1][p3] = 0;
+        p2 = ZTOS((Q)BDY(n2));
+        if ( !mm[p2] ) continue;
+        for ( h = mm[p2], n3 = NEXT(n1); n3; n3 = NEXT(n3) ) {
+          p3 = ZTOS((Q)BDY(n3));
+          if ( n3 != n2 && mm[p3] && dp_redble(mm[p3],h) ) mm[p3] = 0;
         }
       }
+      for ( j = p1+1; j <= n; j++ ) {
+        if ( mm[j] ) {
+          quo = dpm_sp_nf_zlist(psv,psiv,p1,j,0,&nf);
+          if ( nf ) 
+            error("dpm_schreyer_base : cannot happen");
+          NEXTNODE(b0,b); BDY(b) = (pointer)quo;
+        }
+      }
     }
   }
   get_eg(&eg1); add_eg(&egsp,&eg0,&eg1); print_eg("SP",&egsp);
   get_eg(&eg0);
-  for ( i = 1; i <= n; i++ ) {
-    for ( j = i+1; j <= n; j++ ) {
-      if ( m[i][j] ) {
-        quo = dpm_sp_nf(psv,psiv,i,j,&nf);
-        if ( nf ) 
-          error("dpm_schreyer_base : cannot happen");
-        NEXTNODE(b0,b); BDY(b) = (pointer)quo;
-      }
-    }
-  }
   get_eg(&eg1); add_eg(&egnf,&eg0,&eg1); print_eg("NF",&egnf); printf("\n");
   if ( b0 ) NEXT(b) = 0;
-  for ( t0 = t, nd = BDY(g); nd; nd = NEXT(nd) ) {
+  for ( t0 = 0, 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_sort_list(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("red",&egred); printf("\n");
+  printf("sch_count=%d, schlast_count=%d\n",sch_count,schlast_count);
 }
+
+static int compdp_nv;
+
+int compdp_revgradlex(DP *a,DP *b)
+{
+  return -cmpdl_revgradlex(compdp_nv,BDY(*a)->dl,BDY(*b)->dl);
+}
+
+int compdp_lex(DP *a,DP *b)
+{
+  return -cmpdl_lex(compdp_nv,BDY(*a)->dl,BDY(*b)->dl);
+}
+
+DMMstack_array dpm_schreyer_frame(NODE g)
+{
+  LIST l;
+  NODE nd,in,b0,b,n1,n2,n3,t;
+  NODE *psi;
+  long p1,p2,p3;
+  int nv,n,i,j,k,max,pos,level;
+  DMMstack s,s1;
+  DMM m1,m0,dmm;
+  MP mp;
+  DP dp,h;
+  DP *m,*m2;
+  DPM dpm,dpm0,dpm1;
+  VECT psv,psiv;
+  DPM *ps;
+  DMMstack_array dmmstack_array;
+
+  nd = g;
+  compdp_nv = nv = ((DPM)BDY(nd))->nv;
+  s = 0;
+  level = 0;
+  while ( 1 ) {
+    /* store the current nd to a DMMstack */
+    n = length(nd);
+    NEWDMMstack(s1);
+    MKLIST(l,nd); s1->obj = l;
+    s1->rank = n;
+    s1->in = (DMM *)MALLOC((n+1)*sizeof(DMM));
+    s1->sum = (DMM *)MALLOC((n+1)*sizeof(DMM));
+    NEXT(s1) = s;
+    if ( s ) {
+      for ( i = 1, in = nd; i <= n; i++, in = NEXT(in) ) {
+        m1 = s1->in[i] = BDY((DPM)BDY(in));
+        NEWMP(mp); mp->dl = m1->dl; mp->c = m1->c; NEXT(mp) = 0;
+        MKDP(nv,mp,dp); dp->sugar = mp->dl->td;
+        m0 = s->sum[m1->pos]; MKDPM(nv,m0,dpm0);
+        mulobjdpm(CO,(Obj)dp,dpm0,&dpm1);
+        s1->sum[i] = BDY(dpm1);
+      }
+    } else {
+      for ( i = 1, in = nd; i <= n; i++, in = NEXT(in) )  {
+        m0 = BDY((DPM)BDY(in));
+        NEWDMM(m1); *m1 = *m0; m1->c = (Obj)ONE; NEXT(m1) = 0;
+        s1->sum[i] = s1->in[i] = m1;
+      }
+    }
+    s = s1;
+    level++;
+
+
+    /* create new list */
+    MKVECT(psv,n+1);
+    ps = (DPM *)BDY(psv);
+    for ( i = 1, t = nd; i <= n; i++, t = NEXT(t) ) ps[i] = (DPM)BDY(t);
+    for ( i = 1, max = 0; i <= n; i++ )
+      if ( (pos=BDY(ps[i])->pos) > max ) max = pos;
+    MKVECT(psiv,max+1);
+    psi = (NODE *)BDY(psiv);
+    for ( i = n; i >= 1; i-- ) {
+      pos = BDY(ps[i])->pos;
+      MKNODE(nd,(long)i,psi[pos]); psi[pos] = nd;
+    }
+    m = (DP *)MALLOC((n+1)*sizeof(DP));
+    m2 = (DP *)MALLOC((n+1)*sizeof(DP));
+    b0 = 0;
+    for ( i = 1; i <= max; i++ ) {
+      for ( n1 = psi[i]; n1; n1 = NEXT(n1) ) {
+        bzero(m,(n+1)*sizeof(DP));
+        p1 = (long)BDY(n1);
+        for ( n2 = NEXT(n1); n2; n2 = NEXT(n2) ) {
+          p2 = (long)BDY(n2);
+          h = dpm_sp_hm(ps[p1],ps[p2]);
+          for ( n3 = NEXT(n1); n3 != n2; n3 = NEXT(n3) ) {
+            p3 = (long)BDY(n3);
+            if ( m[p3] ) {
+              if ( dp_redble(h,m[p3]) ) {
+                h = 0; break;
+              }
+              if ( dp_redble(m[p3],h) ) m[p3] = 0;
+            }
+          }
+          if ( h ) m[p2] = h;
+        }
+#if 0
+        // compress m to m2
+        for ( j = 0, n2 = NEXT(n1); n2; n2 = NEXT(n2) ) {
+          p2 = (long)BDY(n2);
+          if ( m[p2] ) m2[j++] = m[p2];
+        }
+        qsort(m2,j,sizeof(DP),(int (*)(const void *,const void *))compdp_lex);
+        for ( k = 0; k < j; k++ ) {
+          NEWDMM(dmm); dmm->dl = BDY(m2[k])->dl; dmm->pos = p1; dmm->c = (Obj)ONE;
+          MKDPM(nv,dmm,dpm);
+          NEXTNODE(b0,b); BDY(b) = (pointer)dpm;
+        }
+#else
+        for ( n2 = NEXT(n1); n2; n2 = NEXT(n2) ) {
+          p2 = (long)BDY(n2);
+          if ( m[p2] ) {
+            NEWDMM(dmm); dmm->dl = BDY(m[p2])->dl; dmm->pos = p1; dmm->c = (Obj)ONE;
+            MKDPM(nv,dmm,dpm);
+            NEXTNODE(b0,b); BDY(b) = (pointer)dpm;
+          }
+        }
 #endif
+      }
+    }
+    if ( !b0 ) {
+      NEWDMMstack_array(dmmstack_array);
+      dmmstack_array->len = level;
+      dmmstack_array->body = (DMMstack *)MALLOC(level*sizeof(DMMstack));
+      for ( i = level-1, s1 = s; i >= 0; i--, s1 = NEXT(s1) )
+        dmmstack_array->body[i] = s1;
+      return dmmstack_array;
+    } else {
+      NEXT(b) = 0;
+      nd = b0;
+    }
+  }
+}
 
+int sch_count,schlast_count;
+
 int compdmm_schreyer(int n,DMM m1,DMM m2)
 {
-   int pos1,pos2,t;
-   DMM *in;
-   DMMstack s;
-   static DL d1=0,d2=0;
-   static int dlen=0;
+  int pos1,pos2,t,npos1,npos2;
+  DMM *in,*sum;
+  DMMstack s;
+  static DL d1=0,d2=0;
+  static int dlen=0;
 
-   pos1 = m1->pos; pos2 = m2->pos;
-   if ( pos1 == pos2 ) return (*cmpdl)(n,m1->dl,m2->dl);
-   if ( n > dlen ) {
-     NEWDL(d1,n); NEWDL(d2,n); dlen = n;
-   }
-   _copydl(n,m1->dl,d1); 
-   _copydl(n,m2->dl,d2); 
-   for ( s = dmm_stack; s; s = NEXT(s) ) {
-     in = s->in;
-     _addtodl(n,in[pos1]->dl,d1);
-     _addtodl(n,in[pos2]->dl,d2);
-     if ( in[pos1]->pos == in[pos2]->pos && _eqdl(n,d1,d2)) {
-       if ( pos1 < pos2 ) return 1;
-       else if ( pos1 > pos2 ) return -1;
-       else return 0;
-     }
-     pos1 = in[pos1]->pos;
-     pos2 = in[pos2]->pos;
-     if ( pos1 == pos2 ) return (*cmpdl)(n,d1,d2);
-   }
-   // comparison by the bottom order
-LAST:
-  if ( dpm_base_ordtype == 1 ) {
+  sch_count++;
+  pos1 = m1->pos; pos2 = m2->pos;
+  if ( pos1 == pos2 ) return (*cmpdl)(n,m1->dl,m2->dl);
+  if ( n > dlen ) {
+    NEWDL(d1,n); NEWDL(d2,n); dlen = n;
+  }
+  sum = dmm_stack->sum;
+  _adddl(n,m1->dl,sum[pos1]->dl,d1);
+  _adddl(n,m2->dl,sum[pos2]->dl,d2);
+  t = (*cmpdl)(n,d1,d2);
+  if ( sum[pos1]->pos == sum[pos2]->pos && t == 0 ) {
+    for ( s = dmm_stack; s; s = NEXT(s) ) {
+      in = s->in;
+      npos1 = in[pos1]->pos;
+      npos2 = in[pos2]->pos;
+      if ( npos1 == npos2 ) break;
+      else {
+        pos1 = npos1;
+        pos2 = npos2;
+      }
+    }
+    // (pos1,pos2) = the last pair s.t. pos1 != pos2
+    // simply compare pos1 and pos2
     if ( pos1 < pos2 ) return 1;
     else if ( pos1 > pos2 ) return -1;
-    else return (*cmpdl)(n,d1,d2);
   } else {
-    t = (*cmpdl)(n,d1,d2);
-    if ( t ) return t;
-    else if ( pos1 < pos2 ) return 1;
-    else if ( pos1 > pos2 ) return -1;
-    else return 0;
+    schlast_count++;
+    pos1 = sum[pos1]->pos;
+    pos2 = sum[pos2]->pos;
+    if ( dpm_base_ordtype == 1 ) {
+      if ( pos1 < pos2 ) return 1;
+      else if ( pos1 > pos2 ) return -1;
+      else return t;
+    } else {
+      if ( t ) return t;
+      else if ( pos1 < pos2 ) return 1;
+      else if ( pos1 > pos2 ) return -1;
+      else return 0;
+    }
   }
 }
 
 int compdmm(int n,DMM m1,DMM m2)
 {
   int t;
+  int *base_ord;
 
   switch ( dpm_ordtype ) {
   case 0: /* TOP ord->pos */
@@ -3096,6 +3236,11 @@ int compdmm(int n,DMM m1,DMM m2)
     else return (*cmpdl)(n,m1->dl,m2->dl);
   case 3: /* Schreyer */
     return compdmm_schreyer(n,m1,m2);
+  case 4:  /* POT with base_ord */
+    base_ord = dp_current_spec->module_base_ord;
+    if ( base_ord[m1->pos] < base_ord[m2->pos] ) return 1;
+    else if ( base_ord[m1->pos] > base_ord[m2->pos] ) return -1;
+    else return (*cmpdl)(n,m1->dl,m2->dl);
   default:
     error("compdmm : invalid dpm_ordtype");
   }
@@ -3371,7 +3516,7 @@ DPM dpm_eliminate_term(DPM a,DPM p,Obj c,int pos)
     if ( m->pos == pos ) {
       NEXTMP(d0,d); 
       arf_chsgn(m->c,&d->c);
-      if ( !dp_current_spec || !dp_current_spec->module_rank )
+      if ( !dp_current_spec || !dp_current_spec->module_top_weight )
         d->dl = m->dl; 
       else {
         NEWDL(dl,NV(a));
@@ -3466,7 +3611,7 @@ void dpm_simplify_syz(LIST s,LIST m,LIST *s1,LIST *m1,
     }
   MKLIST(*s1,t);
 
-  if ( dp_current_spec && dp_current_spec->module_rank ) {
+  if ( dp_current_spec && dp_current_spec->module_top_weight ) {
     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]; }