===================================================================
RCS file: /home/cvs/OpenXM_contrib2/asir2018/engine/dist.c,v
retrieving revision 1.16
retrieving revision 1.21
diff -u -p -r1.16 -r1.21
--- OpenXM_contrib2/asir2018/engine/dist.c	2019/11/21 01:54:01	1.16
+++ OpenXM_contrib2/asir2018/engine/dist.c	2020/02/05 04:56:10	1.21
@@ -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.15 2019/11/19 10:50:31 noro Exp $
+ * $OpenXM: OpenXM_contrib2/asir2018/engine/dist.c,v 1.20 2020/02/03 05:51:52 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];
@@ -2750,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);
 
@@ -2861,7 +2862,7 @@ void dpm_schreyer_base(LIST g,LIST *s)
   get_eg(&eg0);
   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;
@@ -2876,26 +2877,187 @@ void dpm_schreyer_base(LIST g,LIST *s)
   printf("sch_count=%d, schlast_count=%d\n",sch_count,schlast_count);
 }
 
+void dpm_list_to_array(LIST g,VECT *psvect,VECT *psindvect)
+{
+  NODE nd,t;
+  int n;
+  VECT psv,psiv;
+  DPM *ps;
+  int i,max,pos;
+  LIST *psi;
+  LIST l;
+  Z iz;
 
+  nd = BDY(g);
+  n = length(nd);
+  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 = (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;
+    STOZ(i,iz); MKNODE(nd,iz,BDY(psi[pos])); BDY(psi[pos]) = nd;
+  }
+  *psvect = psv; *psindvect = psiv;
+}
+
+#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;
+  }
+  STOZ(i,iz); MKNODE(nd,iz,cur);
+  if ( prev == 0 ) BDY(l) = nd;
+  else NEXT(prev) = nd;
+}
+#else
+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;
+  DP h,t1,t2;
+  MP d;
+  DMM r0,r,r1;
+  DPM sp,nf,dpm;
+  DPM *ps;
+  VECT psv,psiv;
+  DPM quo;
+  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);
+  dpm_list_to_array(g,&psv,&psiv);
+  ps = (DPM *)BDY(psv);
+  psi = (LIST *)BDY(psiv);
+  nv = ps[1]->nv;
+  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++ ) {
+    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 = ZTOS((Q)BDY(n2));
+        mm[p2] = dpm_sp_hm(ps[p1],ps[p2]);
+      }
+      for ( n2 = NEXT(n1); n2; n2 = NEXT(n2) ) {
+        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);
+  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);
+//  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,max,pos,level;
+  int nv,n,i,j,k,max,pos,level;
   DMMstack s,s1;
   DMM m1,m0,dmm;
   MP mp;
   DP dp,h;
-  DP *m;
+  DP *m,*m2;
   DPM dpm,dpm0,dpm1;
   VECT psv,psiv;
   DPM *ps;
   DMMstack_array dmmstack_array;
 
   nd = g;
-  nv = ((DPM)BDY(nd))->nv;
+  compdp_nv = nv = ((DPM)BDY(nd))->nv;
   s = 0;
   level = 0;
   while ( 1 ) {
@@ -2917,9 +3079,11 @@ DMMstack_array dpm_schreyer_frame(NODE g)
         s1->sum[i] = BDY(dpm1);
       }
     } else {
-      for ( i = 1, in = nd; i <= n; i++, in = NEXT(in) ) 
-        s1->sum[i] = s1->in[i] = BDY((DPM)BDY(in));
-
+      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++;
@@ -2938,6 +3102,7 @@ DMMstack_array dpm_schreyer_frame(NODE g)
       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) ) {
@@ -2957,6 +3122,19 @@ DMMstack_array dpm_schreyer_frame(NODE g)
           }
           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] ) {
@@ -2965,6 +3143,7 @@ DMMstack_array dpm_schreyer_frame(NODE g)
             NEXTNODE(b0,b); BDY(b) = (pointer)dpm;
           }
         }
+#endif
       }
     }
     if ( !b0 ) {
@@ -3033,9 +3212,55 @@ int compdmm_schreyer(int n,DMM m1,DMM m2)
   }
 }
 
+int compdmm_schreyer_old(int n,DMM m1,DMM m2)
+{
+  int pos1,pos2,t,npos1,npos2;
+  DMM *in,*sum;
+  DMMstack s;
+  static DL d1=0,d2=0;
+  static int dlen=0;
+
+  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;
+  _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);
+  }
+  if ( dpm_base_ordtype == 1 ) {
+    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;
+  }
+}
+
+extern int NaiveSchreyer;
+
 int compdmm(int n,DMM m1,DMM m2)
 {
-  int t;
+  int t,t1;
+  int *base_ord;
 
   switch ( dpm_ordtype ) {
   case 0: /* TOP ord->pos */
@@ -3055,7 +3280,16 @@ int compdmm(int n,DMM m1,DMM m2)
     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);
+    if ( NaiveSchreyer )
+      t = compdmm_schreyer_old(n,m1,m2);
+    else
+      t = compdmm_schreyer(n,m1,m2);
+    return t;
+  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");
   }
@@ -3331,7 +3565,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));
@@ -3372,6 +3606,7 @@ DPM dpm_compress(DPM p,int *tab)
 }
 
 // input : s, s = syz(m) output simplified s, m
+// assuming the term order is POT
 void dpm_simplify_syz(LIST s,LIST m,LIST *s1,LIST *m1,LIST *w1)
 {
   int lm,ls,i,j,k,pos,nv;
@@ -3394,10 +3629,12 @@ void dpm_simplify_syz(LIST s,LIST m,LIST *s1,LIST *m1,
     p = as[i];
     if ( p == 0 ) continue;
     nv = NV(p);
-    for ( d = BDY(p); d; d = NEXT(d) ) {
+    for ( d = BDY(p); d; ) {
       dd = d->dl->d;
       for ( k = 0; k < nv; k++ ) if ( dd[k] ) break;
       if ( k == nv ) break;
+      pos = d->pos;
+      while ( d && d->pos == pos ) d = NEXT(d);
     }
     if ( d ) {
       c = d->c; pos = d->pos;
@@ -3426,7 +3663,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]; }