===================================================================
RCS file: /home/cvs/OpenXM_contrib2/asir2018/engine/nd.c,v
retrieving revision 1.30
retrieving revision 1.35
diff -u -p -r1.30 -r1.35
--- OpenXM_contrib2/asir2018/engine/nd.c	2020/07/03 03:37:59	1.30
+++ OpenXM_contrib2/asir2018/engine/nd.c	2020/08/26 06:40:36	1.35
@@ -1,4 +1,4 @@
-/* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.29 2020/07/02 09:24:17 noro Exp $ */
+/* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.34 2020/07/10 08:09:05 noro Exp $ */
 
 #include "nd.h"
 
@@ -18,7 +18,7 @@ NM _nm_free_list;
 ND _nd_free_list;
 ND_pairs _ndp_free_list;
 NODE nd_hcf;
-int Nsyz;
+int Nsyz,Nsamesig;
 
 Obj nd_top_weight;
 
@@ -786,6 +786,7 @@ int ndl_module_compare(UINT *d1,UINT *d2)
 
 extern DMMstack dmm_stack;
 void _addtodl(int n,DL d1,DL d2);
+void _adddl(int n,DL d1,DL d2,DL d3);
 int _eqdl(int n,DL d1,DL d2);
 
 int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
@@ -1222,6 +1223,8 @@ void print_sig(SIG s)
   fprintf(asir_out,">>*e%d",s->pos);
 }
 
+// assuming increasing order wrt signature 
+
 INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
 {
   RHist r;
@@ -1236,11 +1239,13 @@ INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
   }
   d = ndl_hash_value(dg);
+#if 1
   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
     if ( ndl_equal(dg,DL(r)) ) {
       return r->index;
     }
   }
+#endif
   singular = 0;
   for ( i = 0; i < nd_psn; i++ ) {
     r = nd_psh[i];
@@ -2624,7 +2629,7 @@ NODE nd_sba_buch(int m,int ishomo,int **indp)
   NODE *syzlist;
   int Nredundant;
   DL lcm,quo,mul;
-  struct oEGT eg1,eg2,eg_update,eg_remove,eg_large;
+  struct oEGT eg1,eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
 
 init_eg(&eg_remove);
   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
@@ -2646,6 +2651,8 @@ init_eg(&eg_remove);
 init_eg(&eg_create);
 init_eg(&eg_merge);
 init_eg(&eg_large);
+init_eg(&eg_nf);
+init_eg(&eg_nfzero);
   while ( d ) {
 again:
     if ( DP_Print ) {
@@ -2672,11 +2679,13 @@ again:
       d = nd_reconstruct(0,d);
       goto again;
     }
+get_eg(&eg1);
 #if USE_GEOBUCKET
     stat = m?nd_nf_pbucket_s(m,h,nd_ps,!Top,&nf):nd_nf_s(m,0,h,nd_ps,!Top,&nf);
 #else
     stat = nd_nf_s(m,0,h,nd_ps,!Top,&nf);
 #endif
+get_eg(&eg2); 
     if ( !stat ) {
       NEXT(l) = d; d = l;
       d = nd_reconstruct(0,d);
@@ -2686,6 +2695,7 @@ again:
       FREENDP(l);
     } else if ( nf ) {
       if ( DP_Print ) { printf("+"); fflush(stdout); }
+      add_eg(&eg_nf,&eg1,&eg2);
       hc = HCU(nf);
       nd_removecont(m,nf);
       nfv = ndtondv(m,nf); nd_free(nf);
@@ -2695,6 +2705,7 @@ again:
       nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
       FREENDP(l);
    } else {
+      add_eg(&eg_nfzero,&eg1,&eg2);
      // syzygy
 get_eg(&eg1);
      d = remove_spair_s(d,sig);
@@ -2706,11 +2717,13 @@ get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
  }
  g = conv_ilist_s(nd_demand,0,indp);
  if ( DP_Print ) { 
-   printf("\nnd_sba done. nd_add=%d,Nsyz=%d,Nredundant=%d\n",Nnd_add,Nsyz,Nredundant);
+   printf("\nnd_sba done. nd_add=%d,Nsyz=%d,Nsamesig=%d,Nredundant=%d\n",Nnd_add,Nsyz,Nsamesig,Nredundant);
    fflush(stdout); 
    print_eg("create",&eg_create);
    print_eg("merge",&eg_merge);
    print_eg("remove",&eg_remove);
+   print_eg("nf",&eg_nf);
+   print_eg("nfzero",&eg_nfzero);
    printf("\n");
  }
  return g;
@@ -3200,12 +3213,15 @@ int comp_sig(SIG s1,SIG s2)
   if ( nvar != nd_nvar ) {
     nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
   }
-//  _ndltodl(DL(nd_psh[s1->pos]),m1);
-//  _ndltodl(DL(nd_psh[s2->pos]),m2);
+#if 0
   _copydl(nd_nvar,nd_sba_hm[s1->pos],m1);
   _copydl(nd_nvar,nd_sba_hm[s2->pos],m2);
   _addtodl(nd_nvar,s1->dl,m1);
   _addtodl(nd_nvar,s2->dl,m2);
+#else
+  _adddl(nd_nvar,s1->dl,nd_sba_hm[s1->pos],m1);
+  _adddl(nd_nvar,s2->dl,nd_sba_hm[s2->pos],m2);
+#endif
   ret = (*cmpdl)(nd_nvar,m1,m2);
   if ( ret != 0 ) return ret;
   else if ( s1->pos > s2->pos ) return 1;
@@ -3295,6 +3311,7 @@ ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
       r->next = q2; r = q2; q2 = q2->next;
     } else {
       ret = DL_COMPARE(q1->lcm,q2->lcm);
+      Nsamesig++;
       if ( ret < 0 ) {
         r->next = q1; r = q1; q1 = q1->next;
         q2 = q2->next;
@@ -3679,14 +3696,8 @@ ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
   int msugar;
   ND_pairs t,last;
 
-#if 0
   for ( msugar = SG(d), t = d; t; t = NEXT(t) )
     if ( SG(t) == msugar ) last = t;
-#else
-  msugar = (d->sig->dl->td)+nd_sba_hm[d->sig->pos]->td;
-  for ( t = d; t; t = NEXT(t) )
-    if ( ((t->sig->dl->td)+nd_sba_hm[t->sig->pos]->td) == msugar ) last = t;
-#endif
   *prest = last->next;
   last->next = 0;
   return d;
@@ -4260,7 +4271,7 @@ void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int
   nd_module = 0;
   nd_demand = 0;
   parse_nd_option(current_option);
-
+  Nsamesig = 0;
   if ( DP_Multiple )
     nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
@@ -4331,7 +4342,7 @@ void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int
   for ( r0 = 0, t = x; t; t = NEXT(t) ) {
     NEXTNODE(r0,r); 
     if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
-    BDY(r) = ndvtop(m,CO,vv,BDY(t));
+    else BDY(r) = ndvtop(m,CO,vv,BDY(t));
   }
   if ( r0 ) NEXT(r) = 0;
   MKLIST(*rp,r0);
@@ -4852,7 +4863,7 @@ DL ndltodl(int n,UINT *ndl)
     int i,j,l,s,ord_l;
     struct order_pair *op;
 
-    NEWDL(dl,n);
+    NEWDL_NOINIT(dl,n);
     dl->td = TD(ndl);
     d = dl->d;
     if ( nd_blockmask ) {
@@ -10596,71 +10607,52 @@ int nd_gauss_elim_mod64_s(mp_limb_t **mat,int *sugar,N
   UINT *ct;
   ND_pairs pair;
   SIG sg;
+  int *used;
 
+  used = (int *)MALLOC(row*sizeof(int));
   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
   for ( i = 0; i < row; i++ ) {
     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
     bzero(cmat[i],col*sizeof(UINT));
   }
 
-  for ( rank = 0, j = 0; j < col; j++ ) {
-    for ( i = rank; i < row; i++ ) {
+  for ( j = 0; j < col; j++ ) {
+    for ( i = 0; i < row; i++ ) {
       a = mat[i][j]; c = cmat[i][j];
       MOD128(a,c,md);
       mat[i][j] = a; cmat[i][j] = 0;
     }
-    imin = -1;
-    for ( i = rank; i < row; i++ )
-      if ( mat[i][j] && (imin < 0 || comp_sig(sig[imin],sig[i]) > 0) ) imin = i;
-    if ( imin == -1 ) {
+    for ( i = 0; i < row; i++ )
+      if ( !used[i] && mat[i][j] ) break;
+    if ( i == row ) {
       colstat[j] = 0;
       continue;
-    } else
+    } else {
       colstat[j] = 1;
-    if ( imin != rank ) {
-      t = mat[imin]; mat[imin] = mat[rank]; mat[rank] = t;
-      ct = cmat[imin]; cmat[imin] = cmat[rank]; cmat[rank] = ct;
-      s = sugar[imin]; sugar[imin] = sugar[rank]; sugar[rank] = s;
-      sg = sig[imin]; sig[imin] = sig[rank]; sig[rank] = sg;
-      if ( spactive ) {
-        pair = spactive[imin]; spactive[imin] = spactive[rank]; 
-        spactive[rank] = pair;
-      }
+      used[i] = 1;
     }
     /* column j is normalized */
-    s = sugar[rank];
-    inv = invm((UINT)mat[rank][j],md);
+    s = sugar[i];
+    inv = invm((UINT)mat[i][j],md);
     /* normalize pivot row */
-    for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
+    for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
     }
-    for ( i = rank+1; i < row; i++ ) {
-      if ( (a = mat[i][j]) != 0 ) {
-        sugar[i] = MAX(sugar[i],s);
-        red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
+    for ( k = i+1; k < row; k++ ) {
+      if ( (a = mat[k][j]) != 0 ) {
+        sugar[k] = MAX(sugar[k],s);
+        red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
         Nf4_red++;
       }
     }
-    rank++;
   }
-#if 1
-  for ( j = col-1, l = rank-1; j >= 0; j-- )
-    if ( colstat[j] ) {
-      for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
-        a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
-      }
-      s = sugar[l];
-      for ( i = 0; i < l; i++ ) {
-        a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
-        if ( a && comp_sig(sig[i],sig[l]) > 0 ) {
-          sugar[i] = MAX(sugar[i],s);
-          red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
-          Nf4_red++;
-        }
-      }
-      l--;
-    }
-#endif
+  rank = 0;
+  for ( i = 0; i < row; i++ ) {
+    for ( j = 0; j < col; j++ )
+      if ( mat[i][j] ) break;
+    if ( j == col ) sugar[i] = -1;
+    else rank++;
+  }
   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
   GCFREE(cmat);
   return rank;
@@ -10722,19 +10714,17 @@ NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp
     colstat = (int *)MALLOC(col*sizeof(int));
     rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
     r0 = 0;
-    for ( i = 0; i < rank; i++ ) {
-        NEXTNODE(r0,r);
-        BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
-        SG((NDV)BDY(r)) = spsugar[i];
-        ((NDV)BDY(r))->sig = spsig[i];
+    for ( i = 0; i < sprow; i++ ) {
+        if ( spsugar[i] >= 0 ) {
+          NEXTNODE(r0,r);
+          BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
+          SG((NDV)BDY(r)) = spsugar[i];
+          ((NDV)BDY(r))->sig = spsig[i];
+        } else
+          syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
         GCFREE(spmat[i]);
     }
     if ( r0 ) NEXT(r) = 0;
-
-    for ( ; i < sprow; i++ ) {
-      GCFREE(spmat[i]);
-      syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
-    }
     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
     if ( DP_Print ) {
@@ -10890,28 +10880,20 @@ int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,
 
 NODE nd_sba_f4(int m,int **indp)
 {
-  int i,nh,stat,index,f4red;
-  NODE r,g,tn0,tn,node;
-  ND_pairs d,l,t,ll0,ll,lh;
-  LIST l0,l1;
-  ND spol,red;
-  NDV nf,redv;
-  NM s0,s;
-  NODE rp0,srp0,nflist;
-  int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
+  int i,nh,stat,index,f4red,f4step;
+  int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
+  NODE r,g,rp0,nflist;
+  ND_pairs d,l,t;
+  ND h,nf;
+  NDV nfv;
+  union oNDC hc;
+  UINT *s0vect;
   UINT c;
-  UINT **spmat;
-  UINT *s0vect,*svect,*p,*v;
-  int *colstat;
-  IndArray *imat;
-  int *rhead;
-  int spcol,sprow;
-  int sugar,sugarh;
-  int nbase;
   PGeoBucket bucket;
-  struct oEGT eg0,eg1,eg_f4;
-  Z i1,i2,sugarq;
   NODE *syzlist;
+  SIG sig;
+  struct oEGT eg0,eg1,eg_f4;
+  struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
 
   Nf4_red=0;
   d = 0;
@@ -10921,64 +10903,105 @@ NODE nd_sba_f4(int m,int **indp)
   }
   nd_nbase = nd_psn;
   f4red = 1;
+  psugar = 0;
+  f4step = 0;
   while ( d ) {
-    l = nd_minsugarp_s(d,&d);
-    if ( !l ) continue;
-    sugar = nd_sugarweight?l->sugar2:SG(l);
-    if ( MaxDeg > 0 && sugar > MaxDeg ) break;
-    bucket = create_pbucket();
-    stat = nd_sp_f4(m,0,l,bucket);
-    if ( !stat ) {
-      for ( t = l; NEXT(t); t = NEXT(t) );
-      NEXT(t) = d; d = l;
-      d = nd_reconstruct(0,d);
-      continue;
-    }
-    if ( bucket->m < 0 ) continue;
-    col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
-    if ( !col ) {
-      for ( t = l; NEXT(t); t = NEXT(t) );
-      NEXT(t) = d; d = l;
-      d = nd_reconstruct(0,d);
-      continue;
-    }
-    if ( DP_Print ) fprintf(asir_out,"sugar=%d,",sugar);
-    l = remove_large_lcm(l);
-    nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
-    /* adding new bases */
-    for ( r = nflist; r; r = NEXT(r) ) {
-      ND tmp,tmpred;
-      SIG sig;
-
-      nf = (NDV)BDY(r);
-      sig = nf->sig;
-      tmp = ndvtond(m,nf);
-      stat = nd_nf_s(m,0,tmp,nd_ps,!Top,&tmpred);
-      if ( stat < 0 ) {
-        // top reducible
-        if ( DP_Print ) { fprintf(asir_out,"S"); fflush(asir_out); }
-      } else if ( tmpred ) {
-        nf = ndtondv(m,tmpred);
-        ndv_removecont(m,nf);
-        nh = ndv_newps(m,nf,0);
+    for ( t = d, ms = SG(d); t; t = NEXT(t) )
+      if ( SG(t) < ms ) ms = SG(t);
+    if ( ms == psugar && f4step >= 2 ) {
+again:
+      l = d; d = d->next;
+      if ( small_lcm(l) ) {
+        if ( DP_Print ) fprintf(asir_out,"M");
+        continue;
+      }
+      sig = l->sig;
+      stat = nd_sp(m,0,l,&h);
+      if ( !stat ) {
+        NEXT(l) = d; d = l;
+        d = nd_reconstruct(0,d);
+        goto again;
+      }
+  get_eg(&eg1);
+  #if USE_GEOBUCKET
+      stat = m?nd_nf_pbucket_s(m,h,nd_ps,!Top,&nf):nd_nf_s(m,0,h,nd_ps,!Top,&nf);
+  #else
+      stat = nd_nf_s(m,0,h,nd_ps,!Top,&nf);
+  #endif
+  get_eg(&eg2); 
+      if ( !stat ) {
+        NEXT(l) = d; d = l;
+        d = nd_reconstruct(0,d);
+        goto again;
+      } else if ( stat == -1 ) {
+        if ( DP_Print ) { printf("S"); fflush(stdout); }
+        FREENDP(l);
+      } else if ( nf ) {
+        if ( DP_Print ) { printf("+"); fflush(stdout); }
+        add_eg(&eg_nf,&eg1,&eg2);
+        hc = HCU(nf);
+        nd_removecont(m,nf);
+        nfv = ndtondv(m,nf); nd_free(nf);
+        nh = ndv_newps(m,nfv,0);
+  
         d = update_pairs_s(d,nh,syzlist);
         nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
+        FREENDP(l);
       } else {
+        add_eg(&eg_nfzero,&eg1,&eg2);
+       // syzygy
+  get_eg(&eg1);
+        d = remove_spair_s(d,sig);
+  get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
         syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
-      } 
+        if ( DP_Print ) { printf("."); fflush(stdout); }
+        FREENDP(l);
+      }
+    } else {
+      if ( ms != psugar ) f4step = 1;
+      else f4step++;
+again2:
+      psugar = ms;
+      l = nd_minsugarp_s(d,&d);
+      sugar = nd_sugarweight?d->sugar2:SG(d);
+      bucket = create_pbucket();
+      stat = nd_sp_f4(m,0,l,bucket);
+      if ( !stat ) {
+        for ( t = l; NEXT(t); t = NEXT(t) );
+        NEXT(t) = d; d = l;
+        d = nd_reconstruct(0,d);
+        goto again2;
+      }
+      if ( bucket->m < 0 ) continue;
+      col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
+      if ( !col ) {
+        for ( t = l; NEXT(t); t = NEXT(t) );
+          NEXT(t) = d; d = l;
+        d = nd_reconstruct(0,d);
+        goto again2;
+      }
+      if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
+      nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
+      /* adding new bases */
+      for ( r = nflist; r; r = NEXT(r) ) {
+        nfv = (NDV)BDY(r);
+        ndv_removecont(m,nfv);
+        nh = ndv_newps(m,nfv,0);
+        d = update_pairs_s(d,nh,syzlist);
+        nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
+      }
+      for ( i = 0; i < nd_nbase; i++ )
+        for ( r = syzlist[i]; r; r = NEXT(r) )
+            d = remove_spair_s(d,(SIG)BDY(r));
+      d = remove_large_lcm(d);
+      if ( DP_Print ) { 
+        fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
+      }
+      f4red++;
     }
-    for ( i = 0; i < nd_nbase; i++ )
-      for ( r = syzlist[i]; r; r = NEXT(r) )
-        d = remove_spair_s(d,(SIG)BDY(r));
-    if ( DP_Print ) { 
-      fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
-    }
-    f4red++;
-    if ( nd_f4red && f4red > nd_f4red ) break;
-    if ( nd_rank0 && !nflist ) break;
   }
   if ( DP_Print ) {
-    fprintf(asir_out,"number of red=%d,",Nf4_red);
+    fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
   }
   g = conv_ilist_s(nd_demand,0,indp);
   return g;