[BACK]Return to array.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / builtin

Annotation of OpenXM_contrib2/asir2000/builtin/array.c, Revision 1.77

1.6       noro        1: /*
                      2:  * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
                      3:  * All rights reserved.
                      4:  *
                      5:  * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
                      6:  * non-exclusive and royalty-free license to use, copy, modify and
                      7:  * redistribute, solely for non-commercial and non-profit purposes, the
                      8:  * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
                      9:  * conditions of this Agreement. For the avoidance of doubt, you acquire
                     10:  * only a limited right to use the SOFTWARE hereunder, and FLL or any
                     11:  * third party developer retains all rights, including but not limited to
                     12:  * copyrights, in and to the SOFTWARE.
                     13:  *
                     14:  * (1) FLL does not grant you a license in any way for commercial
                     15:  * purposes. You may use the SOFTWARE only for non-commercial and
                     16:  * non-profit purposes only, such as academic, research and internal
                     17:  * business use.
                     18:  * (2) The SOFTWARE is protected by the Copyright Law of Japan and
                     19:  * international copyright treaties. If you make copies of the SOFTWARE,
                     20:  * with or without modification, as permitted hereunder, you shall affix
                     21:  * to all such copies of the SOFTWARE the above copyright notice.
                     22:  * (3) An explicit reference to this SOFTWARE and its copyright owner
                     23:  * shall be made on your publication or presentation in any form of the
                     24:  * results obtained by use of the SOFTWARE.
                     25:  * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.7       noro       26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.6       noro       27:  * for such modification or the source code of the modified part of the
                     28:  * SOFTWARE.
                     29:  *
                     30:  * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
                     31:  * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
                     32:  * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
                     33:  * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
                     34:  * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
                     35:  * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
                     36:  * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
                     37:  * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
                     38:  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
                     39:  * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
                     40:  * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
                     41:  * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
                     42:  * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
                     43:  * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
                     44:  * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
                     45:  * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
                     46:  * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
                     47:  *
1.77    ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/builtin/array.c,v 1.76 2018/03/29 01:32:50 noro Exp $
1.6       noro       49: */
1.1       noro       50: #include "ca.h"
                     51: #include "base.h"
                     52: #include "parse.h"
                     53: #include "inline.h"
1.4       noro       54:
1.51      noro       55: #include <sys/types.h>
                     56: #include <sys/stat.h>
1.58      ohara      57: #if !defined(_MSC_VER)
1.51      noro       58: #include <unistd.h>
1.58      ohara      59: #endif
1.51      noro       60:
1.38      noro       61: #define F4_INTRAT_PERIOD 8
                     62:
1.4       noro       63: #if 0
1.1       noro       64: #undef DMAR
                     65: #define DMAR(a1,a2,a3,d,r) (r)=dmar(a1,a2,a3,d);
1.4       noro       66: #endif
1.1       noro       67:
1.11      noro       68: extern int DP_Print; /* XXX */
1.1       noro       69:
1.24      noro       70:
1.71      noro       71: void Pnewvect(), Pnewmat(), Psepvect(), Psize(), Pdet(), Pleqm(), Pleqm1(), Pgeninvm(), Ptriangleq();
1.23      noro       72: void Pinvmat();
1.49      noro       73: void Pnewbytearray(),Pmemoryplot_to_coord();
1.1       noro       74:
1.25      noro       75: void Pgeneric_gauss_elim();
1.1       noro       76: void Pgeneric_gauss_elim_mod();
                     77:
1.69      noro       78: void Pindep_rows_mod();
                     79:
1.1       noro       80: void Pmat_to_gfmmat(),Plu_gfmmat(),Psolve_by_lu_gfmmat();
1.33      noro       81: void Pgeninvm_swap(), Premainder(), Psremainder(), Pvtol(), Pltov();
1.27      noro       82: void Pgeninv_sf_swap();
1.1       noro       83: void sepvect();
                     84: void Pmulmat_gf2n();
                     85: void Pbconvmat_gf2n();
                     86: void Pmul_vect_mat_gf2n();
                     87: void PNBmul_gf2n();
                     88: void Pmul_mat_vect_int();
                     89: void Psepmat_destructive();
                     90: void Px962_irredpoly_up2();
                     91: void Pirredpoly_up2();
                     92: void Pnbpoly_up2();
                     93: void Pqsort();
1.14      noro       94: void Pexponent_vector();
1.26      noro       95: void Pmat_swap_row_destructive();
                     96: void Pmat_swap_col_destructive();
1.28      saito      97: void Pvect();
                     98: void Pmat();
1.29      saito      99: void Pmatc();
1.36      noro      100: void Pnd_det();
1.53      noro      101: void Plu_mat();
1.59      ohara     102: void Pmat_col();
1.63      noro      103: void Plusolve_prep();
                    104: void Plusolve_main();
1.1       noro      105:
                    106: struct ftab array_tab[] = {
1.76      noro      107:   {"lu_mat",Plu_mat,1},
                    108:   {"solve_by_lu_gfmmat",Psolve_by_lu_gfmmat,4},
                    109:   {"lu_gfmmat",Plu_gfmmat,2},
                    110:   {"mat_to_gfmmat",Pmat_to_gfmmat,2},
                    111:   {"generic_gauss_elim",Pgeneric_gauss_elim,1},
                    112:   {"generic_gauss_elim_mod",Pgeneric_gauss_elim_mod,2},
                    113:   {"indep_rows_mod",Pindep_rows_mod,2},
                    114:   {"newvect",Pnewvect,-2},
                    115:   {"vect",Pvect,-99999999},
                    116:   {"vector",Pnewvect,-2},
                    117:   {"exponent_vector",Pexponent_vector,-99999999},
                    118:   {"newmat",Pnewmat,-3},
                    119:   {"matrix",Pnewmat,-3},
                    120:   {"mat",Pmat,-99999999},
                    121:   {"matr",Pmat,-99999999},
                    122:   {"matc",Pmatc,-99999999},
                    123:   {"newbytearray",Pnewbytearray,-2},
                    124:   {"memoryplot_to_coord",Pmemoryplot_to_coord,1},
                    125:   {"sepmat_destructive",Psepmat_destructive,2},
                    126:   {"sepvect",Psepvect,2},
                    127:   {"qsort",Pqsort,-2},
                    128:   {"vtol",Pvtol,1},
                    129:   {"ltov",Pltov,1},
                    130:   {"size",Psize,1},
                    131:   {"det",Pdet,-2},
                    132:   {"nd_det",Pnd_det,-2},
                    133:   {"invmat",Pinvmat,-2},
                    134:   {"leqm",Pleqm,2},
                    135:   {"leqm1",Pleqm1,2},
                    136:   {"geninvm",Pgeninvm,2},
                    137:   {"geninvm_swap",Pgeninvm_swap,2},
                    138:   {"geninv_sf_swap",Pgeninv_sf_swap,1},
                    139:   {"remainder",Premainder,2},
                    140:   {"sremainder",Psremainder,2},
                    141:   {"mulmat_gf2n",Pmulmat_gf2n,1},
                    142:   {"bconvmat_gf2n",Pbconvmat_gf2n,-4},
                    143:   {"mul_vect_mat_gf2n",Pmul_vect_mat_gf2n,2},
                    144:   {"mul_mat_vect_int",Pmul_mat_vect_int,2},
                    145:   {"nbmul_gf2n",PNBmul_gf2n,3},
                    146:   {"x962_irredpoly_up2",Px962_irredpoly_up2,2},
                    147:   {"irredpoly_up2",Pirredpoly_up2,2},
                    148:   {"nbpoly_up2",Pnbpoly_up2,2},
                    149:   {"mat_swap_row_destructive",Pmat_swap_row_destructive,3},
                    150:   {"mat_swap_col_destructive",Pmat_swap_col_destructive,3},
                    151:   {"mat_col",Pmat_col,2},
                    152:   {"lusolve_prep",Plusolve_prep,1},
                    153:   {"lusolve_main",Plusolve_main,1},
                    154:   {"triangleq",Ptriangleq,1},
                    155:   {0,0,0},
1.1       noro      156: };
                    157:
1.63      noro      158: typedef struct _ent { int j; unsigned int e; } ent;
                    159:
                    160: ent *get_row(FILE *,int *l);
                    161: void put_row(FILE *out,int l,ent *a);
1.72      ohara     162: void lu_elim(int *l,ent **a,int k,int i,int mul,int mod);
                    163: void lu_append(int *,ent **,int *,int,int,int);
                    164: void solve_l(int *,ent **,int,int *,int);
                    165: void solve_u(int *,ent **,int,int *,int);
                    166:
1.63      noro      167:
                    168: static int *ul,*ll;
                    169: static ent **u,**l;
                    170: static int modulus;
                    171:
                    172: void Plusolve_prep(NODE arg,Q *rp)
                    173: {
1.76      noro      174:   char *fname;
                    175:   FILE *in;
                    176:   int len,i,rank;
                    177:   int *rhs;
                    178:
                    179:   fname = BDY((STRING)ARG0(arg));
                    180:   in = fopen(fname,"r");
                    181:   modulus = getw(in);
                    182:   len = getw(in);
                    183:   ul = (int *)MALLOC_ATOMIC(len*sizeof(int));
                    184:   u = (ent **)MALLOC(len*sizeof(ent *));
                    185:   ll = (int *)MALLOC_ATOMIC(len*sizeof(int));
                    186:   l = (ent **)MALLOC(len*sizeof(ent *));
                    187:   for ( i = 0; i < len; i++ ) {
                    188:     u[i] = get_row(in,&ul[i]);
                    189:   }
                    190:   for ( i = 0; i < len; i++ ) {
                    191:     l[i] = get_row(in,&ll[i]);
                    192:   }
                    193:   fclose(in);
                    194:   *rp = ONE;
1.63      noro      195: }
                    196:
                    197: void Plusolve_main(NODE arg,VECT *rp)
                    198: {
1.76      noro      199:   Q *d,*p;
                    200:   VECT v,r;
                    201:   int len,i;
                    202:   int *rhs;
                    203:
                    204:   v = (VECT)ARG0(arg); len = v->len;
                    205:   d = (Q *)BDY(v);
                    206:   rhs = (int *)MALLOC_ATOMIC(len*sizeof(int));
                    207:   for ( i = 0; i < len; i++ ) rhs[i] = QTOS(d[i]);
                    208:   solve_l(ll,l,len,rhs,modulus);
                    209:   solve_u(ul,u,len,rhs,modulus);
                    210:   NEWVECT(r); r->len = len;
                    211:   r->body = (pointer *)MALLOC(len*sizeof(pointer));
                    212:   p = (Q *)r->body;
                    213:   for ( i = 0; i < len; i++ )
                    214:     STOQ(rhs[i],p[i]);
                    215:   *rp = r;
1.63      noro      216: }
                    217:
                    218: ent *get_row(FILE *in,int *l)
                    219: {
1.76      noro      220:   int len,i;
                    221:   ent *a;
1.63      noro      222:
1.76      noro      223:   *l = len = getw(in);
                    224:   a = (ent *)MALLOC_ATOMIC(len*sizeof(ent));
                    225:   for ( i = 0; i < len; i++ ) {
                    226:     a[i].j = getw(in);
                    227:     a[i].e = getw(in);
                    228:   }
                    229:   return a;
1.63      noro      230: }
                    231:
1.72      ohara     232: void lu_gauss(int *ul,ent **u,int *ll,ent **l,int n,int mod)
1.63      noro      233: {
1.76      noro      234:   int i,j,k,s,mul;
                    235:   unsigned int inv;
                    236:   int *ll2;
                    237:
                    238:   ll2 = (int *)MALLOC_ATOMIC(n*sizeof(int));
                    239:   for ( i = 0; i < n; i++ ) ll2[i] = 0;
                    240:   for ( i = 0; i < n; i++ ) {
                    241:     fprintf(stderr,"i=%d\n",i);
                    242:     inv = invm(u[i][0].e,mod);
                    243:     for ( k = i+1; k < n; k++ )
                    244:       if ( u[k][0].j == n-i ) {
                    245:         s = u[k][0].e;
                    246:         DMAR(s,inv,0,mod,mul);
                    247:         lu_elim(ul,u,k,i,mul,mod);
                    248:         lu_append(ll,l,ll2,k,i,mul);
                    249:       }
                    250:   }
1.63      noro      251: }
                    252:
                    253: #define INITLEN 10
                    254:
1.72      ohara     255: void lu_append(int *l,ent **a,int *l2,int k,int i,int mul)
1.63      noro      256: {
1.76      noro      257:   int len;
                    258:   ent *p;
1.63      noro      259:
1.76      noro      260:   len = l[k];
                    261:   if ( !len ) {
                    262:     a[k] = p = (ent *)MALLOC_ATOMIC(INITLEN*sizeof(ent));
                    263:     p[0].j = i; p[0].e = mul;
                    264:     l[k] = 1; l2[k] = INITLEN;
                    265:   } else {
                    266:     if ( l2[k] == l[k] ) {
                    267:       l2[k] *= 2;
                    268:       a[k] = REALLOC(a[k],l2[k]*sizeof(ent));
                    269:     }
                    270:     p =a[k];
                    271:     p[l[k]].j = i; p[l[k]].e = mul;
                    272:     l[k]++;
                    273:   }
1.63      noro      274: }
                    275:
                    276: /* a[k] = a[k]-mul*a[i] */
                    277:
1.72      ohara     278: void lu_elim(int *l,ent **a,int k,int i,int mul,int mod)
1.63      noro      279: {
1.76      noro      280:   ent *ak,*ai,*w;
                    281:   int lk,li,j,m,p,q,r,s,t,j0;
1.63      noro      282:
1.76      noro      283:   ak = a[k]; ai = a[i]; lk = l[k]; li = l[i];
                    284:   w = (ent *)alloca((lk+li)*sizeof(ent));
                    285:   p = 0; q = 0; j = 0;
                    286:   mul = mod-mul;
                    287:   while ( p < lk && q < li ) {
                    288:     if ( ak[p].j > ai[q].j ) {
                    289:       w[j] = ak[p]; j++; p++;
                    290:     } else if ( ak[p].j < ai[q].j ) {
                    291:       w[j].j = ai[q].j;
                    292:       t = ai[q].e;
                    293:       DMAR(t,mul,0,mod,r);
                    294:       w[j].e = r;
                    295:       j++; q++;
                    296:     } else {
                    297:       t = ai[q].e; s = ak[p].e;
                    298:       DMAR(t,mul,s,mod,r);
                    299:       if ( r ) {
                    300:         w[j].j = ai[q].j; w[j].e = r; j++;
                    301:       }
                    302:       p++; q++;
                    303:     }
                    304:   }
                    305:   if ( q == li )
                    306:     while ( p < lk ) {
                    307:       w[j] = ak[p]; j++; p++;
                    308:     }
                    309:   else if ( p == lk )
                    310:     while ( q < li ) {
                    311:       w[j].j = ai[q].j;
                    312:       t = ai[q].e;
                    313:       DMAR(t,mul,0,mod,r);
                    314:       w[j].e = r;
                    315:       j++; q++;
                    316:     }
                    317:   if ( j <= lk ) {
                    318:     for ( m = 0; m < j; m++ ) ak[m] = w[m];
                    319:   } else {
                    320:     a[k] = ak = (ent *)MALLOC_ATOMIC(j*sizeof(ent));
                    321:     for ( m = 0; m < j; m++ ) ak[m] = w[m];
                    322:   }
                    323:   l[k] = j;
1.63      noro      324: }
                    325:
1.72      ohara     326: void solve_l(int *ll,ent **l,int n,int *rhs,int mod)
1.63      noro      327: {
1.76      noro      328:   int j,k,s,len;
                    329:   ent *p;
1.63      noro      330:
1.76      noro      331:   for ( j = 0; j < n; j++ ) {
                    332:     len = ll[j]; p = l[j];
                    333:     for ( k = 0, s = 0; k < len; k++ )
                    334:       s = dmar(p[k].e,rhs[p[k].j],s,mod);
                    335:     rhs[j] -=  s;
                    336:     if ( rhs[j] < 0 ) rhs[j] += mod;
                    337:   }
1.63      noro      338: }
                    339:
1.72      ohara     340: void solve_u(int *ul,ent **u,int n,int *rhs,int mod)
1.63      noro      341: {
1.76      noro      342:   int j,k,s,len,inv;
                    343:   ent *p;
1.63      noro      344:
1.76      noro      345:   for ( j = n-1; j >= 0; j-- ) {
                    346:     len = ul[j]; p = u[j];
                    347:     for ( k = 1, s = 0; k < len; k++ )
                    348:       s = dmar(p[k].e,rhs[p[k].j],s,mod);
                    349:     rhs[j] -=  s;
                    350:     if ( rhs[j] < 0 ) rhs[j] += mod;
                    351:     inv = invm((unsigned int)p[0].e,mod);
                    352:     rhs[j] = dmar(rhs[j],inv,0,mod);
                    353:   }
1.63      noro      354: }
                    355:
1.24      noro      356: int comp_obj(Obj *a,Obj *b)
1.1       noro      357: {
1.76      noro      358:   return arf_comp(CO,*a,*b);
1.1       noro      359: }
                    360:
                    361: static FUNC generic_comp_obj_func;
                    362: static NODE generic_comp_obj_arg;
1.60      ohara     363: static NODE generic_comp_obj_option;
1.1       noro      364:
1.24      noro      365: int generic_comp_obj(Obj *a,Obj *b)
1.1       noro      366: {
1.76      noro      367:   Q r;
                    368:
                    369:   BDY(generic_comp_obj_arg)=(pointer)(*a);
                    370:   BDY(NEXT(generic_comp_obj_arg))=(pointer)(*b);
                    371:   r = (Q)bevalf_with_opts(generic_comp_obj_func,generic_comp_obj_arg,generic_comp_obj_option);
                    372:   if ( !r )
                    373:     return 0;
                    374:   else
                    375:     return SGN(r)>0?1:-1;
1.1       noro      376: }
                    377:
                    378:
1.46      saito     379: void Pqsort(NODE arg,LIST *rp)
1.1       noro      380: {
1.76      noro      381:   VECT vect;
                    382:   NODE n,n1;
                    383:   P p;
                    384:   V v;
                    385:   FUNC func;
                    386:   int len,i;
                    387:   pointer *a;
                    388:   Obj t;
1.35      ohara     389:
1.76      noro      390:   t = ARG0(arg);
1.35      ohara     391:     if (OID(t) == O_LIST) {
                    392:         n = (NODE)BDY((LIST)t);
                    393:         len = length(n);
                    394:         MKVECT(vect,len);
                    395:         for ( i = 0; i < len; i++, n = NEXT(n) ) {
                    396:             BDY(vect)[i] = BDY(n);
                    397:         }
                    398:
                    399:     }else if (OID(t) != O_VECT) {
                    400:         error("qsort : invalid argument");
                    401:     }else {
                    402:         vect = (VECT)t;
                    403:     }
1.76      noro      404:   if ( argc(arg) == 1 )
                    405:     qsort(BDY(vect),vect->len,sizeof(Obj),(int (*)(const void *,const void *))comp_obj);
                    406:   else {
                    407:     p = (P)ARG1(arg);
                    408:     if ( !p || OID(p)!=2 )
                    409:       error("qsort : invalid argument");
                    410:     v = VR(p);
                    411:     gen_searchf(NAME(v),&func);
                    412:     if ( !func ) {
                    413:       if ( (int)v->attr != V_SR )
                    414:         error("qsort : no such function");
                    415:       func = (FUNC)v->priv;
                    416:     }
                    417:     generic_comp_obj_func = func;
                    418:     MKNODE(n,0,0); MKNODE(generic_comp_obj_arg,0,n);
                    419:     generic_comp_obj_option = current_option;
                    420:     qsort(BDY(vect),vect->len,sizeof(Obj),(int (*)(const void *,const void *))generic_comp_obj);
                    421:   }
1.35      ohara     422:     if (OID(t) == O_LIST) {
                    423:         a = BDY(vect);
                    424:         for ( i = len - 1, n = 0; i >= 0; i-- ) {
                    425:             MKNODE(n1,a[i],n); n = n1;
                    426:         }
1.46      saito     427:         MKLIST(*rp,n);
1.35      ohara     428:     }else {
1.46      saito     429:         *rp = (LIST)vect;
1.35      ohara     430:     }
1.1       noro      431: }
                    432:
1.24      noro      433: void PNBmul_gf2n(NODE arg,GF2N *rp)
1.1       noro      434: {
1.76      noro      435:   GF2N a,b;
                    436:   GF2MAT mat;
                    437:   int n,w;
                    438:   unsigned int *ab,*bb;
                    439:   UP2 r;
                    440:
                    441:   a = (GF2N)ARG0(arg);
                    442:   b = (GF2N)ARG1(arg);
                    443:   mat = (GF2MAT)ARG2(arg);
                    444:   if ( !a || !b )
                    445:     *rp = 0;
                    446:   else {
                    447:     n = mat->row;
                    448:     w = (n+BSH-1)/BSH;
                    449:
                    450:     ab = (unsigned int *)ALLOCA(w*sizeof(unsigned int));
                    451:     bzero((char *)ab,w*sizeof(unsigned int));
                    452:     bcopy(a->body->b,ab,(a->body->w)*sizeof(unsigned int));
                    453:
                    454:     bb = (unsigned int *)ALLOCA(w*sizeof(unsigned int));
                    455:     bzero((char *)bb,w*sizeof(unsigned int));
                    456:     bcopy(b->body->b,bb,(b->body->w)*sizeof(unsigned int));
                    457:
                    458:     NEWUP2(r,w);
                    459:     bzero((char *)r->b,w*sizeof(unsigned int));
                    460:     mul_nb(mat,ab,bb,r->b);
                    461:     r->w = w;
                    462:     _adjup2(r);
                    463:     if ( !r->w )
                    464:       *rp = 0;
                    465:     else
                    466:       MKGF2N(r,*rp);
                    467:   }
1.1       noro      468: }
                    469:
1.24      noro      470: void Pmul_vect_mat_gf2n(NODE arg,GF2N *rp)
1.1       noro      471: {
1.76      noro      472:   GF2N a;
                    473:   GF2MAT mat;
                    474:   int n,w;
                    475:   unsigned int *b;
                    476:   UP2 r;
                    477:
                    478:   a = (GF2N)ARG0(arg);
                    479:   mat = (GF2MAT)ARG1(arg);
                    480:   if ( !a )
                    481:     *rp = 0;
                    482:   else {
                    483:     n = mat->row;
                    484:     w = (n+BSH-1)/BSH;
                    485:     b = (unsigned int *)ALLOCA(w*sizeof(unsigned int));
                    486:     bzero((char *)b,w*sizeof(unsigned int));
                    487:     bcopy(a->body->b,b,(a->body->w)*sizeof(unsigned int));
                    488:     NEWUP2(r,w);
                    489:     bzero((char *)r->b,w*sizeof(unsigned int));
                    490:     mulgf2vectmat(mat->row,b,mat->body,r->b);
                    491:     r->w = w;
                    492:     _adjup2(r);
                    493:     if ( !r->w )
                    494:       *rp = 0;
                    495:     else {
                    496:       MKGF2N(r,*rp);
                    497:     }
                    498:   }
1.1       noro      499: }
                    500:
1.24      noro      501: void Pbconvmat_gf2n(NODE arg,LIST *rp)
1.1       noro      502: {
1.76      noro      503:   P p0,p1;
                    504:   int to;
                    505:   GF2MAT p01,p10;
                    506:   GF2N root;
                    507:   NODE n0,n1;
                    508:
                    509:   p0 = (P)ARG0(arg);
                    510:   p1 = (P)ARG1(arg);
                    511:   to = ARG2(arg)?1:0;
                    512:   if ( argc(arg) == 4 ) {
                    513:     root = (GF2N)ARG3(arg);
                    514:     compute_change_of_basis_matrix_with_root(p0,p1,to,root,&p01,&p10);
                    515:   } else
                    516:     compute_change_of_basis_matrix(p0,p1,to,&p01,&p10);
                    517:   MKNODE(n1,p10,0); MKNODE(n0,p01,n1);
                    518:   MKLIST(*rp,n0);
1.1       noro      519: }
                    520:
1.24      noro      521: void Pmulmat_gf2n(NODE arg,GF2MAT *rp)
1.1       noro      522: {
1.76      noro      523:   GF2MAT m;
1.1       noro      524:
1.76      noro      525:   if ( !compute_multiplication_matrix((P)ARG0(arg),&m) )
                    526:     error("mulmat_gf2n : input is not a normal polynomial");
                    527:   *rp = m;
1.1       noro      528: }
                    529:
1.24      noro      530: void Psepmat_destructive(NODE arg,LIST *rp)
1.1       noro      531: {
1.76      noro      532:   MAT mat,mat1;
                    533:   int i,j,row,col;
                    534:   Q **a,**a1;
                    535:   Q ent;
                    536:   N nm,mod,rem,quo;
                    537:   int sgn;
                    538:   NODE n0,n1;
                    539:
                    540:   mat = (MAT)ARG0(arg); mod = NM((Q)ARG1(arg));
                    541:   row = mat->row; col = mat->col;
                    542:   MKMAT(mat1,row,col);
                    543:   a = (Q **)mat->body; a1 = (Q **)mat1->body;
                    544:   for ( i = 0; i < row; i++ )
                    545:     for ( j = 0; j < col; j++ ) {
                    546:       ent = a[i][j];
                    547:       if ( !ent )
                    548:         continue;
                    549:       nm = NM(ent);
                    550:       sgn = SGN(ent);
                    551:       divn(nm,mod,&quo,&rem);
                    552: /*      if ( quo != nm && rem != nm ) */
                    553: /*        GCFREE(nm); */
                    554: /*      GCFREE(ent); */
                    555:       NTOQ(rem,sgn,a[i][j]); NTOQ(quo,sgn,a1[i][j]);
                    556:     }
                    557:   MKNODE(n1,mat1,0); MKNODE(n0,mat,n1);
                    558:   MKLIST(*rp,n0);
1.1       noro      559: }
                    560:
1.24      noro      561: void Psepvect(NODE arg,VECT *rp)
1.1       noro      562: {
1.76      noro      563:   sepvect((VECT)ARG0(arg),QTOS((Q)ARG1(arg)),rp);
1.1       noro      564: }
                    565:
1.24      noro      566: void sepvect(VECT v,int d,VECT *rp)
1.1       noro      567: {
1.76      noro      568:   int i,j,k,n,q,q1,r;
                    569:   pointer *pv,*pw,*pu;
                    570:   VECT w,u;
                    571:
                    572:   n = v->len;
                    573:   if ( d > n )
                    574:     d = n;
                    575:   q = n/d; r = n%d; q1 = q+1;
                    576:   MKVECT(w,d); *rp = w;
                    577:   pv = BDY(v); pw = BDY(w); k = 0;
                    578:   for ( i = 0; i < r; i++ ) {
                    579:     MKVECT(u,q1); pw[i] = (pointer)u;
                    580:     for ( pu = BDY(u), j = 0; j < q1; j++, k++ )
                    581:       pu[j] = pv[k];
                    582:   }
                    583:   for ( ; i < d; i++ ) {
                    584:     MKVECT(u,q); pw[i] = (pointer)u;
                    585:     for ( pu = BDY(u), j = 0; j < q; j++, k++ )
                    586:       pu[j] = pv[k];
                    587:   }
1.1       noro      588: }
                    589:
1.24      noro      590: void Pnewvect(NODE arg,VECT *rp)
1.1       noro      591: {
1.76      noro      592:   int len,i,r;
                    593:   VECT vect;
                    594:   pointer *vb;
                    595:   LIST list;
                    596:   NODE tn;
                    597:
                    598:   asir_assert(ARG0(arg),O_N,"newvect");
                    599:   len = QTOS((Q)ARG0(arg));
                    600:   if ( len < 0 )
                    601:     error("newvect : invalid size");
                    602:   MKVECT(vect,len);
                    603:   if ( argc(arg) == 2 ) {
                    604:     list = (LIST)ARG1(arg);
                    605:     asir_assert(list,O_LIST,"newvect");
1.56      ohara     606: #if 0
1.76      noro      607:     for ( r = 0, tn = BDY(list); tn; r++, tn = NEXT(tn) );
                    608:     if ( r > len ) {
                    609:       *rp = vect;
                    610:       return;
                    611:     }
1.56      ohara     612: #endif
1.76      noro      613:     for ( i = 0, tn = BDY(list), vb = BDY(vect); tn; i++, tn = NEXT(tn) )
                    614:       vb[i] = (pointer)BDY(tn);
                    615:   }
                    616:   *rp = vect;
1.14      noro      617: }
                    618:
1.28      saito     619: void Pvect(NODE arg,VECT *rp) {
1.76      noro      620:   int len,i;
                    621:   VECT vect;
                    622:   pointer *vb;
                    623:   NODE tn;
                    624:
                    625:   if ( !arg ) {
                    626:     *rp =0;
                    627:     return;
                    628:   }
                    629:
                    630:   for (len = 0, tn = arg; tn; tn = NEXT(tn), len++);
                    631:   if ( len == 1 ) {
                    632:     if ( ARG0(arg) != 0 ) {
                    633:       switch ( OID(ARG0(arg)) ) {
                    634:         case O_VECT:
                    635:           *rp = ARG0(arg);
                    636:           return;
                    637:         case O_LIST:
                    638:           for ( len = 0, tn = ARG0(arg); tn; tn = NEXT(tn), len++ );
                    639:           MKVECT(vect,len-1);
                    640:           for ( i = 0, tn = BDY((LIST)ARG0(arg)), vb =BDY(vect);
                    641:               tn; i++, tn = NEXT(tn) )
                    642:             vb[i] = (pointer)BDY(tn);
                    643:           *rp=vect;
                    644:           return;
                    645:       }
                    646:     }
                    647:   }
                    648:   MKVECT(vect,len);
                    649:   for ( i = 0, tn = arg, vb = BDY(vect); tn; i++, tn = NEXT(tn) )
                    650:     vb[i] = (pointer)BDY(tn);
                    651:   *rp = vect;
1.28      saito     652: }
                    653:
1.24      noro      654: void Pexponent_vector(NODE arg,DP *rp)
1.14      noro      655: {
1.76      noro      656:   nodetod(arg,rp);
1.9       noro      657: }
                    658:
1.24      noro      659: void Pnewbytearray(NODE arg,BYTEARRAY *rp)
1.9       noro      660: {
1.76      noro      661:   int len,i,r;
                    662:   BYTEARRAY array;
                    663:   unsigned char *vb;
                    664:   char *str;
                    665:   LIST list;
                    666:   NODE tn;
                    667:   int ac;
                    668:   struct stat sbuf;
                    669:   char *fname;
                    670:   FILE *fp;
                    671:
                    672:   ac = argc(arg);
                    673:   if ( ac == 1 ) {
                    674:     if ( !OID((Obj)ARG0(arg)) ) error("newbytearray : invalid argument");
                    675:     switch ( OID((Obj)ARG0(arg)) ) {
                    676:       case O_STR:
                    677:         fname = BDY((STRING)ARG0(arg));
                    678:         fp = fopen(fname,"rb");
                    679:         if ( !fp ) error("newbytearray : fopen failed");
                    680:         if ( stat(fname,&sbuf) < 0 )
                    681:           error("newbytearray : stat failed");
                    682:         len = sbuf.st_size;
                    683:         MKBYTEARRAY(array,len);
                    684:         fread(BDY(array),len,sizeof(char),fp);
                    685:         break;
                    686:       case O_N:
                    687:         if ( !RATN(ARG0(arg)) )
                    688:           error("newbytearray : invalid argument");
                    689:         len = QTOS((Q)ARG0(arg));
                    690:         if ( len < 0 )
                    691:           error("newbytearray : invalid size");
                    692:         MKBYTEARRAY(array,len);
                    693:         break;
                    694:       default:
                    695:         error("newbytearray : invalid argument");
                    696:     }
                    697:   } else if ( ac == 2 ) {
                    698:     asir_assert(ARG0(arg),O_N,"newbytearray");
                    699:     len = QTOS((Q)ARG0(arg));
                    700:     if ( len < 0 )
                    701:       error("newbytearray : invalid size");
                    702:     MKBYTEARRAY(array,len);
                    703:     if ( !ARG1(arg) )
                    704:       error("newbytearray : invalid initialization");
                    705:     switch ( OID((Obj)ARG1(arg)) ) {
                    706:       case O_LIST:
                    707:         list = (LIST)ARG1(arg);
                    708:         asir_assert(list,O_LIST,"newbytearray");
                    709:         for ( r = 0, tn = BDY(list); tn; r++, tn = NEXT(tn) );
                    710:         if ( r <= len ) {
                    711:           for ( i = 0, tn = BDY(list), vb = BDY(array); tn;
                    712:             i++, tn = NEXT(tn) )
                    713:             vb[i] = (unsigned char)QTOS((Q)BDY(tn));
                    714:         }
                    715:         break;
                    716:       case O_STR:
                    717:         str = BDY((STRING)ARG1(arg));
                    718:         r = strlen(str);
                    719:         if ( r <= len )
                    720:           bcopy(str,BDY(array),r);
                    721:         break;
                    722:       default:
                    723:         if ( !ARG1(arg) )
                    724:           error("newbytearray : invalid initialization");
                    725:     }
                    726:   } else
                    727:     error("newbytearray : invalid argument");
                    728:   *rp = array;
1.49      noro      729: }
                    730:
                    731: #define MEMORY_GETPOINT(a,len,x,y) (((a)[(len)*(y)+((x)>>3)])&(1<<((x)&7)))
                    732:
                    733: void Pmemoryplot_to_coord(NODE arg,LIST *rp)
                    734: {
1.76      noro      735:   int len,blen,y,i,j;
                    736:   unsigned char *a;
                    737:   NODE r0,r,n;
                    738:   LIST l;
                    739:   BYTEARRAY ba;
                    740:   Q iq,jq;
                    741:
                    742:   asir_assert(ARG0(arg),O_LIST,"memoryplot_to_coord");
                    743:   arg = BDY((LIST)ARG0(arg));
                    744:   len = QTOS((Q)ARG0(arg));
                    745:   blen = (len+7)/8;
                    746:   y = QTOS((Q)ARG1(arg));
                    747:   ba = (BYTEARRAY)ARG2(arg); a = ba->body;
                    748:   r0 = 0;
                    749:   for ( j = 0; j < y; j++ )
                    750:     for ( i = 0; i < len; i++ )
                    751:       if ( MEMORY_GETPOINT(a,blen,i,j) ) {
                    752:         NEXTNODE(r0,r);
                    753:         STOQ(i,iq); STOQ(j,jq);
                    754:         n = mknode(2,iq,jq);
                    755:         MKLIST(l,n);
                    756:         BDY(r) = l;
                    757:       }
                    758:   if ( r0 ) NEXT(r) = 0;
                    759:   MKLIST(*rp,r0);
1.1       noro      760: }
                    761:
1.24      noro      762: void Pnewmat(NODE arg,MAT *rp)
1.1       noro      763: {
1.76      noro      764:   int row,col;
                    765:   int i,j,r,c;
                    766:   NODE tn,sn;
                    767:   MAT m;
                    768:   pointer **mb;
                    769:   LIST list;
                    770:
                    771:   asir_assert(ARG0(arg),O_N,"newmat");
                    772:   asir_assert(ARG1(arg),O_N,"newmat");
                    773:   row = QTOS((Q)ARG0(arg)); col = QTOS((Q)ARG1(arg));
                    774:   if ( row < 0 || col < 0 )
                    775:     error("newmat : invalid size");
                    776:   MKMAT(m,row,col);
                    777:   if ( argc(arg) == 3 ) {
                    778:     list = (LIST)ARG2(arg);
                    779:     asir_assert(list,O_LIST,"newmat");
                    780:     for ( r = 0, c = 0, tn = BDY(list); tn; r++, tn = NEXT(tn) ) {
                    781:       for ( j = 0, sn = BDY((LIST)BDY(tn)); sn; j++, sn = NEXT(sn) );
                    782:       c = MAX(c,j);
                    783:     }
                    784:     if ( (r > row) || (c > col) ) {
                    785:       *rp = m;
                    786:       return;
                    787:     }
                    788:     for ( i = 0, tn = BDY(list), mb = BDY(m); tn; i++, tn = NEXT(tn) ) {
                    789:       asir_assert(BDY(tn),O_LIST,"newmat");
                    790:       for ( j = 0, sn = BDY((LIST)BDY(tn)); sn; j++, sn = NEXT(sn) )
                    791:         mb[i][j] = (pointer)BDY(sn);
                    792:     }
                    793:   }
                    794:   *rp = m;
1.28      saito     795: }
                    796:
                    797: void Pmat(NODE arg, MAT *rp)
                    798: {
1.76      noro      799:   int row,col;
                    800:   int i;
                    801:   MAT m;
                    802:   pointer **mb;
                    803:   pointer *ent;
                    804:   NODE tn, sn;
                    805:   VECT v;
                    806:
                    807:   if ( !arg ) {
                    808:     *rp =0;
                    809:     return;
                    810:   }
                    811:
                    812:   for (row = 0, tn = arg; tn; tn = NEXT(tn), row++);
                    813:   if ( row == 1 ) {
                    814:     if ( OID(ARG0(arg)) == O_MAT ) {
                    815:       *rp=ARG0(arg);
                    816:       return;
                    817:     } else if ( !(OID(ARG0(arg)) == O_LIST || OID(ARG0(arg)) == O_VECT)) {
                    818:       error("mat : invalid argument");
                    819:     }
                    820:   }
                    821:   if ( OID(ARG0(arg)) == O_VECT ) {
                    822:     v = ARG0(arg);
                    823:     col = v->len;
                    824:   } else if ( OID(ARG0(arg)) == O_LIST ) {
                    825:     for (col = 0, tn = BDY((LIST)ARG0(arg)); tn ; tn = NEXT(tn), col++);
                    826:   } else {
                    827:     error("mat : invalid argument");
                    828:   }
                    829:
                    830:   MKMAT(m,row,col);
                    831:   for (row = 0, tn = arg, mb = BDY(m); tn; tn = NEXT(tn), row++) {
                    832:     if ( BDY(tn) == 0 ) {
                    833:       error("mat : invalid argument");
                    834:     } else if ( OID(BDY(tn)) == O_VECT ) {
                    835:       v = tn->body;
                    836:       ent = BDY(v);
                    837:       for (i = 0; i < v->len; i++ ) mb[row][i] = (Obj)ent[i];
                    838:     } else if ( OID(BDY(tn)) == O_LIST ) {
                    839:       for (col = 0, sn = BDY((LIST)BDY(tn)); sn; col++, sn = NEXT(sn) )
                    840:         mb[row][col] = (pointer)BDY(sn);
                    841:     } else {
                    842:       error("mat : invalid argument");
                    843:     }
                    844:   }
                    845:   *rp = m;
1.29      saito     846: }
                    847:
                    848: void Pmatc(NODE arg, MAT *rp)
                    849: {
1.76      noro      850:   int row,col;
                    851:   int i;
                    852:   MAT m;
                    853:   pointer **mb;
                    854:   pointer *ent;
                    855:   NODE tn, sn;
                    856:   VECT v;
                    857:
                    858:   if ( !arg ) {
                    859:     *rp =0;
                    860:     return;
                    861:   }
                    862:
                    863:   for (col = 0, tn = arg; tn; tn = NEXT(tn), col++);
                    864:   if ( col == 1 ) {
                    865:     if ( OID(ARG0(arg)) == O_MAT ) {
                    866:       *rp=ARG0(arg);
                    867:       return;
                    868:     } else if ( !(OID(ARG0(arg)) == O_LIST || OID(ARG0(arg)) == O_VECT)) {
                    869:       error("matc : invalid argument");
                    870:     }
                    871:   }
                    872:   if ( OID(ARG0(arg)) == O_VECT ) {
                    873:     v = ARG0(arg);
                    874:     row = v->len;
                    875:   } else if ( OID(ARG0(arg)) == O_LIST ) {
                    876:     for (row = 0, tn = BDY((LIST)ARG0(arg)); tn ; tn = NEXT(tn), row++);
                    877:   } else {
                    878:     error("matc : invalid argument");
                    879:   }
                    880:
                    881:   MKMAT(m,row,col);
                    882:   for (col = 0, tn = arg, mb = BDY(m); tn; tn = NEXT(tn), col++) {
                    883:     if ( BDY(tn) == 0 ) {
                    884:       error("matc : invalid argument");
                    885:     } else if ( OID(BDY(tn)) == O_VECT ) {
                    886:       v = tn->body;
                    887:       ent = BDY(v);
                    888:       for (i = 0; i < v->len; i++ ) mb[i][col] = (Obj)ent[i];
                    889:     } else if ( OID(BDY(tn)) == O_LIST ) {
                    890:       for (row = 0, sn = BDY((LIST)BDY(tn)); sn; row++, sn = NEXT(sn) )
                    891:         mb[row][col] = (pointer)BDY(sn);
                    892:     } else {
                    893:       error("matc : invalid argument");
                    894:     }
                    895:   }
                    896:   *rp = m;
1.1       noro      897: }
                    898:
1.24      noro      899: void Pvtol(NODE arg,LIST *rp)
1.1       noro      900: {
1.76      noro      901:   NODE n,n1;
                    902:   VECT v;
                    903:   pointer *a;
                    904:   int len,i;
                    905:
                    906:   if ( OID(ARG0(arg)) == O_LIST ) {
                    907:     *rp = ARG0(arg);
                    908:     return;
                    909:   }
                    910:   asir_assert(ARG0(arg),O_VECT,"vtol");
                    911:   v = (VECT)ARG0(arg); len = v->len; a = BDY(v);
                    912:   for ( i = len - 1, n = 0; i >= 0; i-- ) {
                    913:     MKNODE(n1,a[i],n); n = n1;
                    914:   }
                    915:   MKLIST(*rp,n);
1.33      noro      916: }
                    917:
                    918: void Pltov(NODE arg,VECT *rp)
                    919: {
1.76      noro      920:   NODE n;
                    921:   VECT v,v0;
                    922:   int len,i;
                    923:
                    924:   if ( OID(ARG0(arg)) == O_VECT ) {
                    925:     v0 = (VECT)ARG0(arg); len = v0->len;
                    926:     MKVECT(v,len);
                    927:     for ( i = 0; i < len; i++ ) {
                    928:       BDY(v)[i] = BDY(v0)[i];
                    929:     }
                    930:     *rp = v;
                    931:     return;
                    932:   }
                    933:   asir_assert(ARG0(arg),O_LIST,"ltov");
                    934:   n = (NODE)BDY((LIST)ARG0(arg));
                    935:   len = length(n);
                    936:   MKVECT(v,len);
                    937:   for ( i = 0; i < len; i++, n = NEXT(n) )
                    938:     BDY(v)[i] = BDY(n);
                    939:   *rp = v;
1.1       noro      940: }
                    941:
1.24      noro      942: void Premainder(NODE arg,Obj *rp)
1.1       noro      943: {
1.76      noro      944:   Obj a;
                    945:   VECT v,w;
                    946:   MAT m,l;
                    947:   pointer *vb,*wb;
                    948:   pointer **mb,**lb;
                    949:   int id,i,j,n,row,col,t,smd,sgn;
                    950:   Q md,q;
                    951:
                    952:   a = (Obj)ARG0(arg); md = (Q)ARG1(arg);
                    953:   if ( !a )
                    954:     *rp = 0;
                    955:   else {
                    956:     id = OID(a);
                    957:     switch ( id ) {
                    958:       case O_N:
                    959:       case O_P:
                    960:         cmp(md,(P)a,(P *)rp); break;
                    961:       case O_VECT:
                    962:         smd = QTOS(md);
                    963:         v = (VECT)a; n = v->len; vb = v->body;
                    964:         MKVECT(w,n); wb = w->body;
                    965:         for ( i = 0; i < n; i++ ) {
                    966:           if ( q = (Q)vb[i] ) {
                    967:             sgn = SGN(q); t = rem(NM(q),smd);
                    968:             STOQ(t,q);
                    969:             if ( q )
                    970:               SGN(q) = sgn;
                    971:           }
                    972:           wb[i] = (pointer)q;
                    973:         }
                    974:         *rp = (Obj)w;
                    975:         break;
                    976:       case O_MAT:
                    977:         m = (MAT)a; row = m->row; col = m->col; mb = m->body;
                    978:         MKMAT(l,row,col); lb = l->body;
                    979:         for ( i = 0; i < row; i++ )
                    980:           for ( j = 0, vb = mb[i], wb = lb[i]; j < col; j++ )
                    981:             cmp(md,(P)vb[j],(P *)&wb[j]);
                    982:         *rp = (Obj)l;
                    983:         break;
                    984:       default:
                    985:         error("remainder : invalid argument");
                    986:     }
                    987:   }
1.1       noro      988: }
                    989:
1.24      noro      990: void Psremainder(NODE arg,Obj *rp)
1.1       noro      991: {
1.76      noro      992:   Obj a;
                    993:   VECT v,w;
                    994:   MAT m,l;
                    995:   pointer *vb,*wb;
                    996:   pointer **mb,**lb;
                    997:   unsigned int t,smd;
                    998:   int id,i,j,n,row,col;
                    999:   Q md,q;
                   1000:
                   1001:   a = (Obj)ARG0(arg); md = (Q)ARG1(arg);
                   1002:   if ( !a )
                   1003:     *rp = 0;
                   1004:   else {
                   1005:     id = OID(a);
                   1006:     switch ( id ) {
                   1007:       case O_N:
                   1008:       case O_P:
                   1009:         cmp(md,(P)a,(P *)rp); break;
                   1010:       case O_VECT:
                   1011:         smd = QTOS(md);
                   1012:         v = (VECT)a; n = v->len; vb = v->body;
                   1013:         MKVECT(w,n); wb = w->body;
                   1014:         for ( i = 0; i < n; i++ ) {
                   1015:           if ( q = (Q)vb[i] ) {
                   1016:             t = (unsigned int)rem(NM(q),smd);
                   1017:             if ( SGN(q) < 0 )
                   1018:               t = (smd - t) % smd;
                   1019:             UTOQ(t,q);
                   1020:           }
                   1021:           wb[i] = (pointer)q;
                   1022:         }
                   1023:         *rp = (Obj)w;
                   1024:         break;
                   1025:       case O_MAT:
                   1026:         m = (MAT)a; row = m->row; col = m->col; mb = m->body;
                   1027:         MKMAT(l,row,col); lb = l->body;
                   1028:         for ( i = 0; i < row; i++ )
                   1029:           for ( j = 0, vb = mb[i], wb = lb[i]; j < col; j++ )
                   1030:             cmp(md,(P)vb[j],(P *)&wb[j]);
                   1031:         *rp = (Obj)l;
                   1032:         break;
                   1033:       default:
                   1034:         error("remainder : invalid argument");
                   1035:     }
                   1036:   }
1.1       noro     1037: }
                   1038:
1.24      noro     1039: void Psize(NODE arg,LIST *rp)
1.1       noro     1040: {
                   1041:
1.76      noro     1042:   int n,m;
                   1043:   Q q;
                   1044:   NODE t,s;
                   1045:
                   1046:   if ( !ARG0(arg) )
                   1047:      t = 0;
                   1048:   else {
                   1049:     switch (OID(ARG0(arg))) {
                   1050:       case O_VECT:
                   1051:         n = ((VECT)ARG0(arg))->len;
                   1052:         STOQ(n,q); MKNODE(t,q,0);
                   1053:         break;
                   1054:       case O_MAT:
                   1055:         n = ((MAT)ARG0(arg))->row; m = ((MAT)ARG0(arg))->col;
                   1056:         STOQ(m,q); MKNODE(s,q,0); STOQ(n,q); MKNODE(t,q,s);
                   1057:         break;
                   1058:       case O_IMAT:
                   1059:         n = ((IMAT)ARG0(arg))->row; m = ((IMAT)ARG0(arg))->col;
                   1060:         STOQ(m,q); MKNODE(s,q,0); STOQ(n,q); MKNODE(t,q,s);
                   1061:         break;
                   1062:       default:
                   1063:         error("size : invalid argument"); break;
                   1064:     }
                   1065:   }
                   1066:   MKLIST(*rp,t);
1.1       noro     1067: }
                   1068:
1.24      noro     1069: void Pdet(NODE arg,P *rp)
1.1       noro     1070: {
1.76      noro     1071:   MAT m;
                   1072:   int n,i,j,mod;
                   1073:   P d;
                   1074:   P **mat,**w;
                   1075:
                   1076:   m = (MAT)ARG0(arg);
                   1077:   asir_assert(m,O_MAT,"det");
                   1078:   if ( m->row != m->col )
                   1079:     error("det : non-square matrix");
                   1080:   else if ( argc(arg) == 1 )
                   1081:     detp(CO,(P **)BDY(m),m->row,rp);
                   1082:   else {
                   1083:     n = m->row; mod = QTOS((Q)ARG1(arg)); mat = (P **)BDY(m);
                   1084:     w = (P **)almat_pointer(n,n);
                   1085:     for ( i = 0; i < n; i++ )
                   1086:       for ( j = 0; j < n; j++ )
                   1087:         ptomp(mod,mat[i][j],&w[i][j]);
                   1088:     detmp(CO,mod,w,n,&d);
                   1089:     mptop(d,rp);
                   1090:   }
1.23      noro     1091: }
                   1092:
1.24      noro     1093: void Pinvmat(NODE arg,LIST *rp)
1.23      noro     1094: {
1.76      noro     1095:   MAT m,r;
                   1096:   int n,i,j,mod;
                   1097:   P dn;
                   1098:   P **mat,**imat,**w;
                   1099:   NODE nd;
                   1100:
                   1101:   m = (MAT)ARG0(arg);
                   1102:   asir_assert(m,O_MAT,"invmat");
                   1103:   if ( m->row != m->col )
                   1104:     error("invmat : non-square matrix");
                   1105:   else if ( argc(arg) == 1 ) {
                   1106:     n = m->row;
                   1107:     invmatp(CO,(P **)BDY(m),n,&imat,&dn);
                   1108:     NEWMAT(r); r->row = n; r->col = n; r->body = (pointer **)imat;
                   1109:     nd = mknode(2,r,dn);
                   1110:     MKLIST(*rp,nd);
                   1111:   } else {
                   1112:     n = m->row; mod = QTOS((Q)ARG1(arg)); mat = (P **)BDY(m);
                   1113:     w = (P **)almat_pointer(n,n);
                   1114:     for ( i = 0; i < n; i++ )
                   1115:       for ( j = 0; j < n; j++ )
                   1116:         ptomp(mod,mat[i][j],&w[i][j]);
1.23      noro     1117: #if 0
1.76      noro     1118:     detmp(CO,mod,w,n,&d);
                   1119:     mptop(d,rp);
1.23      noro     1120: #else
1.76      noro     1121:     error("not implemented yet");
1.23      noro     1122: #endif
1.76      noro     1123:   }
1.25      noro     1124: }
                   1125:
                   1126: /*
1.76      noro     1127:   input : a row x col matrix A
                   1128:     A[I] <-> A[I][0]*x_0+A[I][1]*x_1+...
1.25      noro     1129:
1.76      noro     1130:   output : [B,D,R,C]
                   1131:     B : a rank(A) x col-rank(A) matrix
                   1132:     D : the denominator
                   1133:     R : a vector of length rank(A)
                   1134:     C : a vector of length col-rank(A)
                   1135:     B[I] <-> D*x_{R[I]}+B[I][0]x_{C[0]}+B[I][1]x_{C[1]}+...
1.25      noro     1136: */
                   1137:
                   1138: void Pgeneric_gauss_elim(NODE arg,LIST *rp)
                   1139: {
1.76      noro     1140:   NODE n0,opt,p;
                   1141:   MAT m,nm;
                   1142:   int *ri,*ci;
                   1143:   VECT rind,cind;
                   1144:   Q dn,q;
                   1145:   int i,row,col,t,rank;
                   1146:   int is_hensel = 0;
                   1147:   char *key;
                   1148:   Obj value;
                   1149:
                   1150:   if ( current_option ) {
                   1151:     for ( opt = current_option; opt; opt = NEXT(opt) ) {
                   1152:       p = BDY((LIST)BDY(opt));
                   1153:       key = BDY((STRING)BDY(p));
                   1154:       value = (Obj)BDY(NEXT(p));
                   1155:       if ( !strcmp(key,"hensel") && value ) {
                   1156:         is_hensel = value ? 1 : 0;
                   1157:         break;
                   1158:       }
                   1159:     }
                   1160:   }
                   1161:   asir_assert(ARG0(arg),O_MAT,"generic_gauss_elim");
                   1162:   m = (MAT)ARG0(arg);
                   1163:   row = m->row; col = m->col;
                   1164:   if ( is_hensel )
                   1165:     rank = generic_gauss_elim_hensel(m,&nm,&dn,&ri,&ci);
                   1166:   else
                   1167:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   1168:   t = col-rank;
                   1169:   MKVECT(rind,rank);
                   1170:   MKVECT(cind,t);
                   1171:   for ( i = 0; i < rank; i++ ) {
                   1172:     STOQ(ri[i],q);
                   1173:     BDY(rind)[i] = (pointer)q;
                   1174:   }
                   1175:   for ( i = 0; i < t; i++ ) {
                   1176:     STOQ(ci[i],q);
                   1177:     BDY(cind)[i] = (pointer)q;
                   1178:   }
                   1179:   n0 = mknode(4,nm,dn,rind,cind);
                   1180:   MKLIST(*rp,n0);
1.1       noro     1181: }
                   1182:
1.69      noro     1183: void Pindep_rows_mod(NODE arg,VECT *rp)
                   1184: {
1.76      noro     1185:   MAT m,mat;
                   1186:   VECT rind;
                   1187:   Q **tmat;
                   1188:   int **wmat,**row0;
                   1189:   Q *rib;
                   1190:   int *rowstat,*p;
                   1191:   Q q;
                   1192:   int md,i,j,k,l,row,col,t,rank;
                   1193:
                   1194:   asir_assert(ARG0(arg),O_MAT,"indep_rows_mod");
                   1195:   asir_assert(ARG1(arg),O_N,"indep_rows_mod");
                   1196:   m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
                   1197:   row = m->row; col = m->col; tmat = (Q **)m->body;
                   1198:   wmat = (int **)almat(row,col);
                   1199:
                   1200:   row0 = (int **)ALLOCA(row*sizeof(int *));
                   1201:   for ( i = 0; i < row; i++ ) row0[i] = wmat[i];
                   1202:
                   1203:   rowstat = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   1204:   for ( i = 0; i < row; i++ )
                   1205:     for ( j = 0; j < col; j++ )
                   1206:       if ( q = (Q)tmat[i][j] ) {
                   1207:         t = rem(NM(q),md);
                   1208:         if ( t && SGN(q) < 0 )
                   1209:           t = (md - t) % md;
                   1210:         wmat[i][j] = t;
                   1211:       } else
                   1212:         wmat[i][j] = 0;
                   1213:   rank = indep_rows_mod(wmat,row,col,md,rowstat);
                   1214:
                   1215:   MKVECT(rind,rank);
                   1216:   rib = (Q *)rind->body;
                   1217:   for ( j = 0; j < rank; j++ ) {
                   1218:     STOQ(rowstat[j],rib[j]);
                   1219:   }
1.69      noro     1220:     *rp = rind;
                   1221: }
                   1222:
1.1       noro     1223: /*
1.76      noro     1224:   input : a row x col matrix A
                   1225:     A[I] <-> A[I][0]*x_0+A[I][1]*x_1+...
1.1       noro     1226:
1.76      noro     1227:   output : [B,R,C]
                   1228:     B : a rank(A) x col-rank(A) matrix
                   1229:     R : a vector of length rank(A)
                   1230:     C : a vector of length col-rank(A)
                   1231:     RN : a vector of length rank(A) indicating useful rows
1.47      noro     1232:
1.76      noro     1233:     B[I] <-> x_{R[I]}+B[I][0]x_{C[0]}+B[I][1]x_{C[1]}+...
1.1       noro     1234: */
                   1235:
1.24      noro     1236: void Pgeneric_gauss_elim_mod(NODE arg,LIST *rp)
1.1       noro     1237: {
1.76      noro     1238:   NODE n0;
                   1239:   MAT m,mat;
                   1240:   VECT rind,cind,rnum;
                   1241:   Q **tmat;
                   1242:   int **wmat,**row0;
                   1243:   Q *rib,*cib,*rnb;
                   1244:   int *colstat,*p;
                   1245:   Q q;
                   1246:   int md,i,j,k,l,row,col,t,rank;
                   1247:
                   1248:   asir_assert(ARG0(arg),O_MAT,"generic_gauss_elim_mod");
                   1249:   asir_assert(ARG1(arg),O_N,"generic_gauss_elim_mod");
                   1250:   m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
                   1251:   row = m->row; col = m->col; tmat = (Q **)m->body;
                   1252:   wmat = (int **)almat(row,col);
                   1253:
                   1254:   row0 = (int **)ALLOCA(row*sizeof(int *));
                   1255:   for ( i = 0; i < row; i++ ) row0[i] = wmat[i];
                   1256:
                   1257:   colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
                   1258:   for ( i = 0; i < row; i++ )
                   1259:     for ( j = 0; j < col; j++ )
                   1260:       if ( q = (Q)tmat[i][j] ) {
                   1261:         t = rem(NM(q),md);
                   1262:         if ( t && SGN(q) < 0 )
                   1263:           t = (md - t) % md;
                   1264:         wmat[i][j] = t;
                   1265:       } else
                   1266:         wmat[i][j] = 0;
                   1267:   rank = generic_gauss_elim_mod(wmat,row,col,md,colstat);
                   1268:
                   1269:   MKVECT(rnum,rank);
                   1270:   rnb = (Q *)rnum->body;
                   1271:   for ( i = 0; i < rank; i++ )
                   1272:     for ( j = 0, p = wmat[i]; j < row; j++ )
                   1273:       if ( p == row0[j] )
                   1274:         STOQ(j,rnb[i]);
                   1275:
                   1276:   MKMAT(mat,rank,col-rank);
                   1277:   tmat = (Q **)mat->body;
                   1278:   for ( i = 0; i < rank; i++ )
                   1279:     for ( j = k = 0; j < col; j++ )
                   1280:       if ( !colstat[j] ) {
                   1281:         UTOQ(wmat[i][j],tmat[i][k]); k++;
                   1282:       }
                   1283:
                   1284:   MKVECT(rind,rank);
                   1285:   MKVECT(cind,col-rank);
                   1286:   rib = (Q *)rind->body; cib = (Q *)cind->body;
                   1287:   for ( j = k = l = 0; j < col; j++ )
                   1288:     if ( colstat[j] ) {
                   1289:       STOQ(j,rib[k]); k++;
                   1290:     } else {
                   1291:       STOQ(j,cib[l]); l++;
                   1292:     }
                   1293:   n0 = mknode(4,mat,rind,cind,rnum);
                   1294:   MKLIST(*rp,n0);
1.1       noro     1295: }
                   1296:
1.24      noro     1297: void Pleqm(NODE arg,VECT *rp)
1.1       noro     1298: {
1.76      noro     1299:   MAT m;
                   1300:   VECT vect;
                   1301:   pointer **mat;
                   1302:   Q *v;
                   1303:   Q q;
                   1304:   int **wmat;
                   1305:   int md,i,j,row,col,t,n,status;
                   1306:
                   1307:   asir_assert(ARG0(arg),O_MAT,"leqm");
                   1308:   asir_assert(ARG1(arg),O_N,"leqm");
                   1309:   m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
                   1310:   row = m->row; col = m->col; mat = m->body;
                   1311:   wmat = (int **)almat(row,col);
                   1312:   for ( i = 0; i < row; i++ )
                   1313:     for ( j = 0; j < col; j++ )
                   1314:       if ( q = (Q)mat[i][j] ) {
                   1315:         t = rem(NM(q),md);
                   1316:         if ( SGN(q) < 0 )
                   1317:           t = (md - t) % md;
                   1318:         wmat[i][j] = t;
                   1319:       } else
                   1320:         wmat[i][j] = 0;
                   1321:   status = gauss_elim_mod(wmat,row,col,md);
                   1322:   if ( status < 0 )
                   1323:     *rp = 0;
                   1324:   else if ( status > 0 )
                   1325:     *rp = (VECT)ONE;
                   1326:   else {
                   1327:     n = col - 1;
                   1328:     MKVECT(vect,n);
                   1329:     for ( i = 0, v = (Q *)vect->body; i < n; i++ ) {
                   1330:       t = (md-wmat[i][n])%md; STOQ(t,v[i]);
                   1331:     }
                   1332:     *rp = vect;
                   1333:   }
1.1       noro     1334: }
                   1335:
1.24      noro     1336: int gauss_elim_mod(int **mat,int row,int col,int md)
1.1       noro     1337: {
1.76      noro     1338:   int i,j,k,inv,a,n;
                   1339:   int *t,*pivot;
1.1       noro     1340:
1.76      noro     1341:   n = col - 1;
                   1342:   for ( j = 0; j < n; j++ ) {
                   1343:     for ( i = j; i < row && !mat[i][j]; i++ );
                   1344:     if ( i == row )
                   1345:       return 1;
                   1346:     if ( i != j ) {
                   1347:       t = mat[i]; mat[i] = mat[j]; mat[j] = t;
                   1348:     }
                   1349:     pivot = mat[j];
                   1350:     inv = invm(pivot[j],md);
                   1351:     for ( k = j; k <= n; k++ ) {
                   1352: /*      pivot[k] = dmar(pivot[k],inv,0,md); */
                   1353:       DMAR(pivot[k],inv,0,md,pivot[k])
                   1354:     }
                   1355:     for ( i = 0; i < row; i++ ) {
                   1356:       t = mat[i];
                   1357:       if ( i != j && (a = t[j]) )
                   1358:         for ( k = j, a = md - a; k <= n; k++ ) {
                   1359:           unsigned int tk;
                   1360: /*          t[k] = dmar(pivot[k],a,t[k],md); */
                   1361:           DMAR(pivot[k],a,t[k],md,tk)
                   1362:           t[k] = tk;
                   1363:         }
                   1364:     }
                   1365:   }
                   1366:   for ( i = n; i < row && !mat[i][n]; i++ );
                   1367:   if ( i == row )
                   1368:     return 0;
                   1369:   else
                   1370:     return -1;
1.1       noro     1371: }
                   1372:
1.4       noro     1373: struct oEGT eg_mod,eg_elim,eg_elim1,eg_elim2,eg_chrem,eg_gschk,eg_intrat,eg_symb;
1.31      noro     1374: struct oEGT eg_conv;
1.1       noro     1375:
1.24      noro     1376: int generic_gauss_elim(MAT mat,MAT *nm,Q *dn,int **rindp,int **cindp)
1.1       noro     1377: {
1.76      noro     1378:   int **wmat;
                   1379:   Q **bmat;
                   1380:   N **tmat;
                   1381:   Q *bmi;
                   1382:   N *tmi;
                   1383:   Q q;
                   1384:   int *wmi;
                   1385:   int *colstat,*wcolstat,*rind,*cind;
                   1386:   int row,col,ind,md,i,j,k,l,t,t1,rank,rank0,inv;
                   1387:   N m1,m2,m3,s,u;
                   1388:   MAT r,crmat;
                   1389:   struct oEGT tmp0,tmp1;
                   1390:   struct oEGT eg_mod_split,eg_elim_split,eg_chrem_split;
                   1391:   struct oEGT eg_intrat_split,eg_gschk_split;
                   1392:   int ret;
                   1393:
                   1394:   init_eg(&eg_mod_split); init_eg(&eg_chrem_split);
                   1395:   init_eg(&eg_elim_split); init_eg(&eg_intrat_split);
                   1396:   init_eg(&eg_gschk_split);
                   1397:   bmat = (Q **)mat->body;
                   1398:   row = mat->row; col = mat->col;
                   1399:   wmat = (int **)almat(row,col);
                   1400:   colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
                   1401:   wcolstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
                   1402:   for ( ind = 0; ; ind++ ) {
                   1403:     if ( DP_Print ) {
                   1404:       fprintf(asir_out,"."); fflush(asir_out);
                   1405:     }
                   1406:     md = get_lprime(ind);
                   1407:     get_eg(&tmp0);
                   1408:     for ( i = 0; i < row; i++ )
                   1409:       for ( j = 0, bmi = bmat[i], wmi = wmat[i]; j < col; j++ )
                   1410:         if ( q = (Q)bmi[j] ) {
                   1411:           t = rem(NM(q),md);
                   1412:           if ( t && SGN(q) < 0 )
                   1413:             t = (md - t) % md;
                   1414:           wmi[j] = t;
                   1415:         } else
                   1416:           wmi[j] = 0;
                   1417:     get_eg(&tmp1);
                   1418:     add_eg(&eg_mod,&tmp0,&tmp1);
                   1419:     add_eg(&eg_mod_split,&tmp0,&tmp1);
                   1420:     get_eg(&tmp0);
                   1421:     rank = generic_gauss_elim_mod(wmat,row,col,md,wcolstat);
                   1422:     get_eg(&tmp1);
                   1423:     add_eg(&eg_elim,&tmp0,&tmp1);
                   1424:     add_eg(&eg_elim_split,&tmp0,&tmp1);
                   1425:     if ( !ind ) {
1.1       noro     1426: RESET:
1.76      noro     1427:       UTON(md,m1);
                   1428:       rank0 = rank;
                   1429:       bcopy(wcolstat,colstat,col*sizeof(int));
                   1430:       MKMAT(crmat,rank,col-rank);
                   1431:       MKMAT(r,rank,col-rank); *nm = r;
                   1432:       tmat = (N **)crmat->body;
                   1433:       for ( i = 0; i < rank; i++ )
                   1434:         for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
                   1435:           if ( !colstat[j] ) {
                   1436:             UTON(wmi[j],tmi[k]); k++;
                   1437:           }
                   1438:     } else {
                   1439:       if ( rank < rank0 ) {
                   1440:         if ( DP_Print ) {
                   1441:           fprintf(asir_out,"lower rank matrix; continuing...\n");
                   1442:           fflush(asir_out);
                   1443:         }
                   1444:         continue;
                   1445:       } else if ( rank > rank0 ) {
                   1446:         if ( DP_Print ) {
                   1447:           fprintf(asir_out,"higher rank matrix; resetting...\n");
                   1448:           fflush(asir_out);
                   1449:         }
                   1450:         goto RESET;
                   1451:       } else {
                   1452:         for ( j = 0; (j<col) && (colstat[j]==wcolstat[j]); j++ );
                   1453:         if ( j < col ) {
                   1454:           if ( DP_Print ) {
                   1455:             fprintf(asir_out,"inconsitent colstat; resetting...\n");
                   1456:             fflush(asir_out);
                   1457:           }
                   1458:           goto RESET;
                   1459:         }
                   1460:       }
                   1461:
                   1462:       get_eg(&tmp0);
                   1463:       inv = invm(rem(m1,md),md);
                   1464:       UTON(md,m2); muln(m1,m2,&m3);
                   1465:       for ( i = 0; i < rank; i++ )
                   1466:         for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
                   1467:           if ( !colstat[j] ) {
                   1468:             if ( tmi[k] ) {
                   1469:             /* f3 = f1+m1*(m1 mod m2)^(-1)*(f2 - f1 mod m2) */
                   1470:               t = rem(tmi[k],md);
                   1471:               if ( wmi[j] >= t )
                   1472:                 t = wmi[j]-t;
                   1473:               else
                   1474:                 t = md-(t-wmi[j]);
                   1475:               DMAR(t,inv,0,md,t1)
                   1476:               UTON(t1,u);
                   1477:               muln(m1,u,&s);
                   1478:               addn(tmi[k],s,&u); tmi[k] = u;
                   1479:             } else if ( wmi[j] ) {
                   1480:             /* f3 = m1*(m1 mod m2)^(-1)*f2 */
                   1481:               DMAR(wmi[j],inv,0,md,t)
                   1482:               UTON(t,u);
                   1483:               muln(m1,u,&s); tmi[k] = s;
                   1484:             }
                   1485:             k++;
                   1486:           }
                   1487:       m1 = m3;
                   1488:       get_eg(&tmp1);
                   1489:       add_eg(&eg_chrem,&tmp0,&tmp1);
                   1490:       add_eg(&eg_chrem_split,&tmp0,&tmp1);
                   1491:
                   1492:       get_eg(&tmp0);
                   1493:       if ( ind % F4_INTRAT_PERIOD )
                   1494:         ret = 0;
                   1495:       else
                   1496:         ret = intmtoratm(crmat,m1,*nm,dn);
                   1497:       get_eg(&tmp1);
                   1498:       add_eg(&eg_intrat,&tmp0,&tmp1);
                   1499:       add_eg(&eg_intrat_split,&tmp0,&tmp1);
                   1500:       if ( ret ) {
                   1501:         *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
                   1502:         *cindp = cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int));
                   1503:         for ( j = k = l = 0; j < col; j++ )
                   1504:           if ( colstat[j] )
                   1505:             rind[k++] = j;
                   1506:           else
                   1507:             cind[l++] = j;
                   1508:         get_eg(&tmp0);
                   1509:         if ( gensolve_check(mat,*nm,*dn,rind,cind) ) {
                   1510:           get_eg(&tmp1);
                   1511:           add_eg(&eg_gschk,&tmp0,&tmp1);
                   1512:           add_eg(&eg_gschk_split,&tmp0,&tmp1);
                   1513:           if ( DP_Print ) {
                   1514:             print_eg("Mod",&eg_mod_split);
                   1515:             print_eg("Elim",&eg_elim_split);
                   1516:             print_eg("ChRem",&eg_chrem_split);
                   1517:             print_eg("IntRat",&eg_intrat_split);
                   1518:             print_eg("Check",&eg_gschk_split);
                   1519:             fflush(asir_out);
                   1520:           }
                   1521:           return rank;
                   1522:         }
                   1523:       }
                   1524:     }
                   1525:   }
1.3       noro     1526: }
                   1527:
1.64      noro     1528: void lu_dec_cr(MAT mat,MAT lu,Q *dn,int **perm);
                   1529:
1.53      noro     1530: /* XXX broken */
1.64      noro     1531: void lu_dec_cr(MAT mat,MAT lu,Q *dn,int **perm)
1.53      noro     1532: {
1.76      noro     1533:   Q **a0,**b;
                   1534:   Q *aiq;
                   1535:   N **a;
                   1536:   N *ai;
                   1537:   Q q,q1,dn2,a1,q0,bik;
                   1538:   MAT m;
                   1539:   unsigned int md;
                   1540:   int n,ind,i,j,rank,t,inv,t1,ret,min,k;
                   1541:   int **w;
                   1542:   int *wi,*rinfo0,*rinfo;
                   1543:   N m1,m2,m3,u,s;
                   1544:
                   1545:   a0 = (Q **)mat->body;
                   1546:   n = mat->row;
                   1547:   if ( n != mat->col )
                   1548:     error("lu_dec_cr : non-square matrix");
                   1549:   w = (int **)almat(n,n);
                   1550:   MKMAT(m,n,n);
                   1551:   a = (N **)m->body;
                   1552:   UTON(1,m1);
                   1553:   rinfo0 = 0;
                   1554:   ind = 0;
                   1555:   while ( 1 ) {
                   1556:     md = get_lprime(ind);
                   1557:     /* mat mod md */
                   1558:     for ( i = 0; i < n; i++ )
                   1559:       for ( j = 0, aiq = a0[i], wi = w[i]; j < n; j++ )
                   1560:         if ( q = aiq[j] ) {
                   1561:           t = rem(NM(q),md);
                   1562:           if ( t && SGN(q) < 0 )
                   1563:             t = (md - t) % md;
                   1564:           wi[j] = t;
                   1565:         } else
                   1566:           wi[j] = 0;
                   1567:
                   1568:     if ( !lu_mod((unsigned int **)w,n,md,&rinfo) ) continue;
                   1569:     printf("."); fflush(stdout);
                   1570:     if ( !rinfo0 )
                   1571:       *perm = rinfo0 = rinfo;
                   1572:     else {
                   1573:       for ( i = 0; i < n; i++ )
                   1574:         if ( rinfo[i] != rinfo0[i] ) break;
                   1575:       if ( i < n ) continue;
                   1576:     }
                   1577:     if ( UNIN(m1) ) {
                   1578:       for ( i = 0; i < n; i++ )
                   1579:         for ( j = 0, ai = a[i], wi = w[i]; j < n; j++ ) {
                   1580:           UTON(wi[j],u); ai[j] = u;
                   1581:         }
                   1582:       UTON(md,m1);
                   1583:     } else {
                   1584:       inv = invm(rem(m1,md),md);
                   1585:       UTON(md,m2); muln(m1,m2,&m3);
                   1586:       for ( i = 0; i < n; i++ )
                   1587:         for ( j = 0, ai = a[i], wi = w[i]; j < n; j++ )
                   1588:           if ( ai[i] ) {
                   1589:           /* f3 = f1+m1*(m1 mod m2)^(-1)*(f2 - f1 mod m2) */
                   1590:             t = rem(ai[j],md);
                   1591:             if ( wi[j] >= t )
                   1592:               t = wi[j]-t;
                   1593:             else
                   1594:               t = md-(t-wi[j]);
                   1595:             DMAR(t,inv,0,md,t1)
                   1596:             UTON(t1,u);
                   1597:             muln(m1,u,&s);
                   1598:             addn(ai[j],s,&u); ai[j] = u;
                   1599:           } else if ( wi[j] ) {
                   1600:             /* f3 = m1*(m1 mod m2)^(-1)*f2 */
                   1601:             DMAR(wi[j],inv,0,md,t)
                   1602:             UTON(t,u);
                   1603:             muln(m1,u,&s); ai[j] = s;
                   1604:           }
                   1605:       m1 = m3;
                   1606:     }
                   1607:     if ( (++ind%8) == 0 ) {
                   1608:       ret = intmtoratm(m,m1,lu,dn);
                   1609:       if ( ret ) {
                   1610:         b = (Q **)lu->body;
                   1611:         mulq(*dn,*dn,&dn2);
                   1612:         for ( i = 0; i < n; i++ ) {
                   1613:           for ( j = 0; j < n; j++ ) {
                   1614:             q = 0;
                   1615:             min = MIN(i,j);
                   1616:             for ( k = 0; k <= min; k++ ) {
                   1617:               bik = k==i ? *dn : b[i][k];
                   1618:               mulq(bik,b[k][j],&q0);
                   1619:               addq(q,q0,&q1); q = q1;
                   1620:             }
                   1621:             mulq(a0[rinfo0[i]][j],dn2,&q1);
                   1622:             if ( cmpq(q,q1) ) break;
                   1623:           }
                   1624:           if ( j < n ) break;
                   1625:         }
                   1626:         if ( i == n )
                   1627:           return;
                   1628:       }
                   1629:     }
                   1630:   }
1.53      noro     1631: }
                   1632:
1.64      noro     1633: void nmat(N **m,int n)
1.53      noro     1634: {
1.76      noro     1635:   int i,j;
1.53      noro     1636:
1.76      noro     1637:   for ( i = 0; i < n; i++ ) {
                   1638:     for ( j = 0; j < n; j++ ) {
                   1639:       printn(m[i][j]); printf(" ");
                   1640:     }
                   1641:     printf("\n");
                   1642:   }
1.53      noro     1643: }
                   1644:
1.24      noro     1645: int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn,int **rindp,int **cindp)
1.3       noro     1646: {
1.76      noro     1647:   MAT bmat,xmat;
                   1648:   Q **a0,**a,**b,**x,**nm;
                   1649:   Q *ai,*bi,*xi;
                   1650:   int row,col;
                   1651:   int **w;
                   1652:   int *wi;
                   1653:   int **wc;
                   1654:   Q mdq,q,s,u;
                   1655:   N tn;
                   1656:   int ind,md,i,j,k,l,li,ri,rank;
                   1657:   unsigned int t;
                   1658:   int *cinfo,*rinfo;
                   1659:   int *rind,*cind;
                   1660:   int count;
                   1661:   int ret;
                   1662:   struct oEGT eg_mul,eg_inv,eg_intrat,eg_check,tmp0,tmp1;
                   1663:   int period;
                   1664:   int *wx,*ptr;
                   1665:   int wxsize,nsize;
                   1666:   N wn;
                   1667:   Q wq;
                   1668:
                   1669:   a0 = (Q **)mat->body;
                   1670:   row = mat->row; col = mat->col;
                   1671:   w = (int **)almat(row,col);
                   1672:   for ( ind = 0; ; ind++ ) {
                   1673:     md = get_lprime(ind);
                   1674:     STOQ(md,mdq);
                   1675:     for ( i = 0; i < row; i++ )
                   1676:       for ( j = 0, ai = a0[i], wi = w[i]; j < col; j++ )
                   1677:         if ( q = (Q)ai[j] ) {
                   1678:           t = rem(NM(q),md);
                   1679:           if ( t && SGN(q) < 0 )
                   1680:             t = (md - t) % md;
                   1681:           wi[j] = t;
                   1682:         } else
                   1683:           wi[j] = 0;
                   1684:
                   1685:     if ( DP_Print > 3 ) {
                   1686:       fprintf(asir_out,"LU decomposition.."); fflush(asir_out);
                   1687:     }
                   1688:     rank = find_lhs_and_lu_mod((unsigned int **)w,row,col,md,&rinfo,&cinfo);
                   1689:     if ( DP_Print > 3 ) {
                   1690:       fprintf(asir_out,"done.\n"); fflush(asir_out);
                   1691:     }
                   1692:     a = (Q **)almat_pointer(rank,rank); /* lhs mat */
                   1693:     MKMAT(bmat,rank,col-rank); b = (Q **)bmat->body; /* lhs mat */
                   1694:     for ( j = li = ri = 0; j < col; j++ )
                   1695:       if ( cinfo[j] ) {
                   1696:         /* the column is in lhs */
                   1697:         for ( i = 0; i < rank; i++ ) {
                   1698:           w[i][li] = w[i][j];
                   1699:           a[i][li] = a0[rinfo[i]][j];
                   1700:         }
                   1701:         li++;
                   1702:       } else {
                   1703:         /* the column is in rhs */
                   1704:         for ( i = 0; i < rank; i++ )
                   1705:           b[i][ri] = a0[rinfo[i]][j];
                   1706:         ri++;
                   1707:       }
                   1708:
                   1709:       /* solve Ax+B=0; A: rank x rank, B: rank x ri */
                   1710:       MKMAT(xmat,rank,ri); x = (Q **)(xmat)->body;
                   1711:       MKMAT(*nmmat,rank,ri); nm = (Q **)(*nmmat)->body;
                   1712:       /* use the right part of w as work area */
                   1713:       /* ri = col - rank */
                   1714:       wc = (int **)almat(rank,ri);
                   1715:       for ( i = 0; i < rank; i++ )
                   1716:         wc[i] = w[i]+rank;
                   1717:       *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
                   1718:       *cindp = cind = (int *)MALLOC_ATOMIC((ri)*sizeof(int));
                   1719:
                   1720:       init_eg(&eg_mul); init_eg(&eg_inv);
                   1721:       init_eg(&eg_check); init_eg(&eg_intrat);
                   1722:       period = F4_INTRAT_PERIOD;
                   1723:       nsize = period;
                   1724:       wxsize = rank*ri*nsize;
                   1725:       wx = (int *)MALLOC_ATOMIC(wxsize*sizeof(int));
                   1726:       for ( i = 0; i < wxsize; i++ ) wx[i] = 0;
                   1727:       for ( q = ONE, count = 0; ; ) {
                   1728:         if ( DP_Print > 3 )
                   1729:           fprintf(stderr,"o");
                   1730:         /* wc = -b mod md */
                   1731:         get_eg(&tmp0);
                   1732:         for ( i = 0; i < rank; i++ )
                   1733:           for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ )
                   1734:             if ( u = (Q)bi[j] ) {
                   1735:               t = rem(NM(u),md);
                   1736:               if ( t && SGN(u) > 0 )
                   1737:                 t = (md - t) % md;
                   1738:               wi[j] = t;
                   1739:             } else
                   1740:               wi[j] = 0;
                   1741:         /* wc = A^(-1)wc; wc is not normalized */
                   1742:         solve_by_lu_mod(w,rank,md,wc,ri,0);
                   1743:         /* wx += q*wc */
                   1744:         ptr = wx;
                   1745:         for ( i = 0; i < rank; i++ )
                   1746:           for ( j = 0, wi = wc[i]; j < ri; j++ ) {
                   1747:             if ( wi[j] )
                   1748:               muln_1(BD(NM(q)),PL(NM(q)),wi[j],ptr);
                   1749:             ptr += nsize;
                   1750:           }
                   1751:         count++;
                   1752:         get_eg(&tmp1);
                   1753:         add_eg(&eg_inv,&tmp0,&tmp1);
                   1754:         get_eg(&tmp0);
                   1755:         for ( i = 0; i < rank; i++ )
                   1756:           for ( j = 0; j < ri; j++ ) {
                   1757:             inner_product_mat_int_mod(a,wc,rank,i,j,&u);
                   1758:             addq(b[i][j],u,&s);
                   1759:             if ( s ) {
                   1760:               t = divin(NM(s),md,&tn);
                   1761:               if ( t )
                   1762:                 error("generic_gauss_elim_hensel:incosistent");
                   1763:               NTOQ(tn,SGN(s),b[i][j]);
                   1764:             } else
                   1765:               b[i][j] = 0;
                   1766:           }
                   1767:         get_eg(&tmp1);
                   1768:         add_eg(&eg_mul,&tmp0,&tmp1);
                   1769:         /* q = q*md */
                   1770:         mulq(q,mdq,&u); q = u;
                   1771:         if ( count == period ) {
                   1772:           get_eg(&tmp0);
                   1773:           ptr = wx;
                   1774:           for ( i = 0; i < rank; i++ )
                   1775:             for ( j = 0, xi = x[i]; j < ri;
                   1776:               j++, ptr += nsize ) {
                   1777:               for ( k = nsize-1; k >= 0 && !ptr[k]; k-- );
                   1778:               if ( k >= 0 ) {
                   1779:                 wn = NALLOC(k+1);
                   1780:                 PL(wn) = k+1;
                   1781:                 for ( l = 0; l <= k; l++ ) BD(wn)[l] = (unsigned int)ptr[l];
                   1782:                 NTOQ(wn,1,wq);
                   1783:                 subq(xi[j],wq,&u); xi[j] = u;
                   1784:               }
                   1785:             }
                   1786:           ret = intmtoratm_q(xmat,NM(q),*nmmat,dn);
                   1787:           get_eg(&tmp1); add_eg(&eg_intrat,&tmp0,&tmp1);
                   1788:           if ( ret ) {
                   1789:             rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
                   1790:             cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int));
                   1791:             for ( j = k = l = 0; j < col; j++ )
                   1792:               if ( cinfo[j] )
                   1793:                 rind[k++] = j;
                   1794:               else
                   1795:                 cind[l++] = j;
                   1796:             get_eg(&tmp0);
                   1797:             ret = gensolve_check(mat,*nmmat,*dn,rind,cind);
                   1798:             get_eg(&tmp1); add_eg(&eg_check,&tmp0,&tmp1);
                   1799:             if ( ret ) {
                   1800:               if ( DP_Print > 3 ) {
                   1801:                 fprintf(stderr,"\n");
                   1802:                 print_eg("INV",&eg_inv);
                   1803:                 print_eg("MUL",&eg_mul);
                   1804:                 print_eg("INTRAT",&eg_intrat);
                   1805:                 print_eg("CHECK",&eg_check);
                   1806:                 fflush(asir_out);
                   1807:               }
                   1808:               *rindp = rind;
                   1809:               *cindp = cind;
                   1810:               for ( j = k = 0; j < col; j++ )
                   1811:                 if ( !cinfo[j] )
                   1812:                   cind[k++] = j;
                   1813:               return rank;
                   1814:             }
                   1815:           } else {
                   1816:             period = period*3/2;
                   1817:             count = 0;
                   1818:             nsize += period;
                   1819:             wxsize += rank*ri*nsize;
                   1820:             wx = (int *)REALLOC(wx,wxsize*sizeof(int));
                   1821:             for ( i = 0; i < wxsize; i++ ) wx[i] = 0;
                   1822:           }
                   1823:         }
                   1824:       }
                   1825:   }
1.50      noro     1826: }
                   1827:
1.55      noro     1828: int generic_gauss_elim_hensel_dalg(MAT mat,DP *mb,MAT *nmmat,Q *dn,int **rindp,int **cindp)
1.50      noro     1829: {
1.76      noro     1830:   MAT bmat,xmat;
                   1831:   Q **a0,**a,**b,**x,**nm;
                   1832:   Q *ai,*bi,*xi;
                   1833:   int row,col;
                   1834:   int **w;
                   1835:   int *wi;
                   1836:   int **wc;
                   1837:   Q mdq,q,s,u;
                   1838:   N tn;
                   1839:   int ind,md,i,j,k,l,li,ri,rank;
                   1840:   unsigned int t;
                   1841:   int *cinfo,*rinfo;
                   1842:   int *rind,*cind;
                   1843:   int count;
                   1844:   int ret;
                   1845:   struct oEGT eg_mul,eg_inv,eg_intrat,eg_check,tmp0,tmp1;
                   1846:   int period;
                   1847:   int *wx,*ptr;
                   1848:   int wxsize,nsize;
                   1849:   N wn;
                   1850:   Q wq;
                   1851:   NumberField nf;
                   1852:   DP m;
                   1853:   int col1;
                   1854:
                   1855:   a0 = (Q **)mat->body;
                   1856:   row = mat->row; col = mat->col;
                   1857:   w = (int **)almat(row,col);
                   1858:   for ( ind = 0; ; ind++ ) {
                   1859:     md = get_lprime(ind);
                   1860:     STOQ(md,mdq);
                   1861:     for ( i = 0; i < row; i++ )
                   1862:       for ( j = 0, ai = a0[i], wi = w[i]; j < col; j++ )
                   1863:         if ( q = (Q)ai[j] ) {
                   1864:           t = rem(NM(q),md);
                   1865:           if ( t && SGN(q) < 0 )
                   1866:             t = (md - t) % md;
                   1867:           wi[j] = t;
                   1868:         } else
                   1869:           wi[j] = 0;
                   1870:
                   1871:     if ( DP_Print ) {
                   1872:       fprintf(asir_out,"LU decomposition.."); fflush(asir_out);
                   1873:     }
                   1874:     rank = find_lhs_and_lu_mod((unsigned int **)w,row,col,md,&rinfo,&cinfo);
1.77    ! noro     1875:     printf("\n");
        !          1876:     for ( i = 0; i < row; i++ ) {
        !          1877:       for ( j = 0; j < col; j++ )
        !          1878:         printf("%d ",w[i][j]);
        !          1879:       printf("\n");
        !          1880:     }
1.76      noro     1881:     if ( DP_Print ) {
                   1882:       fprintf(asir_out,"done.\n"); fflush(asir_out);
                   1883:     }
                   1884:     for ( i = 0; i < col-1; i++ ) {
                   1885:       if ( !cinfo[i] ) {
                   1886:         m = mb[i];
                   1887:         for ( j = i+1; j < col-1; j++ )
                   1888:           if ( dp_redble(mb[j],m) )
                   1889:             cinfo[j] = -1;
                   1890:       }
                   1891:     }
                   1892:     a = (Q **)almat_pointer(rank,rank); /* lhs mat */
                   1893:     MKMAT(bmat,rank,col-rank); b = (Q **)bmat->body; /* lhs mat */
                   1894:     for ( j = li = ri = 0; j < col; j++ )
                   1895:       if ( cinfo[j] > 0 ) {
                   1896:         /* the column is in lhs */
                   1897:         for ( i = 0; i < rank; i++ ) {
                   1898:           w[i][li] = w[i][j];
                   1899:           a[i][li] = a0[rinfo[i]][j];
                   1900:         }
                   1901:         li++;
                   1902:       } else if ( !cinfo[j] ) {
                   1903:         /* the column is in rhs */
                   1904:         for ( i = 0; i < rank; i++ )
                   1905:           b[i][ri] = a0[rinfo[i]][j];
                   1906:         ri++;
                   1907:       }
                   1908:
                   1909:       /* solve Ax+B=0; A: rank x rank, B: rank x ri */
                   1910:       MKMAT(xmat,rank,ri); x = (Q **)(xmat)->body;
                   1911:       MKMAT(*nmmat,rank,ri); nm = (Q **)(*nmmat)->body;
                   1912:       /* use the right part of w as work area */
                   1913:       wc = (int **)almat(rank,ri);
                   1914:       for ( i = 0; i < rank; i++ )
                   1915:         wc[i] = w[i]+rank;
                   1916:       *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
                   1917:       *cindp = cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int));
                   1918:       init_eg(&eg_mul); init_eg(&eg_inv);
                   1919:       init_eg(&eg_check); init_eg(&eg_intrat);
                   1920:       period = F4_INTRAT_PERIOD;
                   1921:       nsize = period;
                   1922:       wxsize = rank*ri*nsize;
                   1923:       wx = (int *)MALLOC_ATOMIC(wxsize*sizeof(int));
                   1924:       for ( i = 0; i < wxsize; i++ ) wx[i] = 0;
                   1925:       for ( q = ONE, count = 0; ; ) {
                   1926:         if ( DP_Print )
                   1927:           fprintf(stderr,"o");
                   1928:         /* wc = -b mod md */
                   1929:         get_eg(&tmp0);
                   1930:         for ( i = 0; i < rank; i++ )
                   1931:           for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ )
                   1932:             if ( u = (Q)bi[j] ) {
                   1933:               t = rem(NM(u),md);
                   1934:               if ( t && SGN(u) > 0 )
                   1935:                 t = (md - t) % md;
                   1936:               wi[j] = t;
                   1937:             } else
                   1938:               wi[j] = 0;
                   1939:         /* wc = A^(-1)wc; wc is not normalized */
                   1940:         solve_by_lu_mod(w,rank,md,wc,ri,0);
                   1941:         /* wx += q*wc */
                   1942:         ptr = wx;
                   1943:         for ( i = 0; i < rank; i++ )
                   1944:           for ( j = 0, wi = wc[i]; j < ri; j++ ) {
                   1945:             if ( wi[j] )
                   1946:               muln_1(BD(NM(q)),PL(NM(q)),wi[j],ptr);
                   1947:             ptr += nsize;
                   1948:           }
                   1949:         count++;
                   1950:         get_eg(&tmp1);
                   1951:         add_eg(&eg_inv,&tmp0,&tmp1);
                   1952:         get_eg(&tmp0);
                   1953:         for ( i = 0; i < rank; i++ )
                   1954:           for ( j = 0; j < ri; j++ ) {
                   1955:             inner_product_mat_int_mod(a,wc,rank,i,j,&u);
                   1956:             addq(b[i][j],u,&s);
                   1957:             if ( s ) {
                   1958:               t = divin(NM(s),md,&tn);
                   1959:               if ( t )
                   1960:                 error("generic_gauss_elim_hensel:incosistent");
                   1961:               NTOQ(tn,SGN(s),b[i][j]);
                   1962:             } else
                   1963:               b[i][j] = 0;
                   1964:           }
1.77    ! noro     1965:         printf("\n");
        !          1966:         for ( i = 0; i < rank; i++ ) {
        !          1967:           for ( j = 0; j < ri; j++ ) {
        !          1968:             printexpr(CO,b[i][j]); printf(" ");
        !          1969:           }
        !          1970:           printf("\n");
        !          1971:         }
1.76      noro     1972:         get_eg(&tmp1);
                   1973:         add_eg(&eg_mul,&tmp0,&tmp1);
                   1974:         /* q = q*md */
                   1975:         mulq(q,mdq,&u); q = u;
                   1976:         if ( count == period ) {
                   1977:           get_eg(&tmp0);
                   1978:           ptr = wx;
                   1979:           for ( i = 0; i < rank; i++ )
                   1980:             for ( j = 0, xi = x[i]; j < ri;
                   1981:               j++, ptr += nsize ) {
                   1982:               for ( k = nsize-1; k >= 0 && !ptr[k]; k-- );
                   1983:               if ( k >= 0 ) {
                   1984:                 wn = NALLOC(k+1);
                   1985:                 PL(wn) = k+1;
                   1986:                 for ( l = 0; l <= k; l++ ) BD(wn)[l] = (unsigned int)ptr[l];
                   1987:                 NTOQ(wn,1,wq);
                   1988:                 subq(xi[j],wq,&u); xi[j] = u;
                   1989:               }
                   1990:             }
                   1991:           ret = intmtoratm_q(xmat,NM(q),*nmmat,dn);
                   1992:           get_eg(&tmp1); add_eg(&eg_intrat,&tmp0,&tmp1);
                   1993:           if ( ret ) {
                   1994:             for ( j = k = l = 0; j < col; j++ )
                   1995:               if ( cinfo[j] > 0 )
                   1996:                 rind[k++] = j;
                   1997:               else if ( !cinfo[j] )
                   1998:                 cind[l++] = j;
                   1999:             get_eg(&tmp0);
                   2000:             ret = gensolve_check(mat,*nmmat,*dn,rind,cind);
                   2001:             get_eg(&tmp1); add_eg(&eg_check,&tmp0,&tmp1);
                   2002:             if ( ret ) {
                   2003:               if ( DP_Print > 3 ) {
                   2004:                 fprintf(stderr,"\n");
                   2005:                 print_eg("INV",&eg_inv);
                   2006:                 print_eg("MUL",&eg_mul);
                   2007:                 print_eg("INTRAT",&eg_intrat);
                   2008:                 print_eg("CHECK",&eg_check);
                   2009:                 fflush(asir_out);
                   2010:               }
                   2011:               return rank;
1.77    ! noro     2012:             } else
        !          2013:               goto reset;
1.76      noro     2014:           } else {
1.77    ! noro     2015: reset:
1.76      noro     2016:             period = period*3/2;
                   2017:             count = 0;
                   2018:             nsize += period;
                   2019:             wxsize += rank*ri*nsize;
                   2020:             wx = (int *)REALLOC(wx,wxsize*sizeof(int));
                   2021:             for ( i = 0; i < wxsize; i++ ) wx[i] = 0;
                   2022:           }
                   2023:         }
                   2024:       }
                   2025:   }
1.1       noro     2026: }
                   2027:
                   2028: int f4_nocheck;
                   2029:
1.24      noro     2030: int gensolve_check(MAT mat,MAT nm,Q dn,int *rind,int *cind)
1.1       noro     2031: {
1.76      noro     2032:   int row,col,rank,clen,i,j,k,l;
                   2033:   Q s,t;
                   2034:   Q *w;
                   2035:   Q *mati,*nmk;
                   2036:
                   2037:   if ( f4_nocheck )
                   2038:     return 1;
                   2039:   row = mat->row; col = mat->col;
                   2040:   rank = nm->row; clen = nm->col;
                   2041:   w = (Q *)MALLOC(clen*sizeof(Q));
                   2042:   for ( i = 0; i < row; i++ ) {
                   2043:     mati = (Q *)mat->body[i];
1.1       noro     2044: #if 1
1.76      noro     2045:     bzero(w,clen*sizeof(Q));
                   2046:     for ( k = 0; k < rank; k++ )
                   2047:       for ( l = 0, nmk = (Q *)nm->body[k]; l < clen; l++ ) {
                   2048:         mulq(mati[rind[k]],nmk[l],&t);
                   2049:         addq(w[l],t,&s); w[l] = s;
                   2050:       }
                   2051:     for ( j = 0; j < clen; j++ ) {
                   2052:       mulq(dn,mati[cind[j]],&t);
                   2053:       if ( cmpq(w[j],t) )
                   2054:         break;
                   2055:     }
1.1       noro     2056: #else
1.76      noro     2057:     for ( j = 0; j < clen; j++ ) {
                   2058:       for ( k = 0, s = 0; k < rank; k++ ) {
                   2059:         mulq(mati[rind[k]],nm->body[k][j],&t);
                   2060:         addq(s,t,&u); s = u;
                   2061:       }
                   2062:       mulq(dn,mati[cind[j]],&t);
                   2063:       if ( cmpq(s,t) )
                   2064:         break;
                   2065:     }
1.1       noro     2066: #endif
1.76      noro     2067:     if ( j != clen )
                   2068:       break;
                   2069:   }
                   2070:   if ( i != row )
                   2071:     return 0;
                   2072:   else
                   2073:     return 1;
1.1       noro     2074: }
                   2075:
                   2076: /* assuming 0 < c < m */
                   2077:
1.24      noro     2078: int inttorat(N c,N m,N b,int *sgnp,N *nmp,N *dnp)
1.1       noro     2079: {
1.76      noro     2080:   Q qq,t,u1,v1,r1;
                   2081:   N q,u2,v2,r2;
1.1       noro     2082:
1.76      noro     2083:   u1 = 0; v1 = ONE; u2 = m; v2 = c;
                   2084:   while ( cmpn(v2,b) >= 0 ) {
                   2085:     divn(u2,v2,&q,&r2); u2 = v2; v2 = r2;
                   2086:     NTOQ(q,1,qq); mulq(qq,v1,&t); subq(u1,t,&r1); u1 = v1; v1 = r1;
                   2087:   }
                   2088:   if ( cmpn(NM(v1),b) >= 0 )
                   2089:     return 0;
                   2090:   else {
                   2091:     *nmp = v2;
                   2092:     *dnp = NM(v1);
                   2093:     *sgnp = SGN(v1);
                   2094:     return 1;
                   2095:   }
1.1       noro     2096: }
                   2097:
                   2098: /* mat->body = N ** */
                   2099:
1.24      noro     2100: int intmtoratm(MAT mat,N md,MAT nm,Q *dn)
1.1       noro     2101: {
1.76      noro     2102:   N t,s,b;
                   2103:   Q dn0,dn1,nm1,q;
                   2104:   int i,j,k,l,row,col;
                   2105:   Q **rmat;
                   2106:   N **tmat;
                   2107:   N *tmi;
                   2108:   Q *nmk;
                   2109:   N u,unm,udn;
                   2110:   int sgn,ret;
                   2111:
                   2112:   if ( UNIN(md) )
                   2113:     return 0;
                   2114:   row = mat->row; col = mat->col;
                   2115:   bshiftn(md,1,&t);
                   2116:   isqrt(t,&s);
                   2117:   bshiftn(s,64,&b);
                   2118:   if ( !b )
                   2119:     b = ONEN;
                   2120:   dn0 = ONE;
                   2121:   tmat = (N **)mat->body;
                   2122:   rmat = (Q **)nm->body;
                   2123:   for ( i = 0; i < row; i++ )
                   2124:     for ( j = 0, tmi = tmat[i]; j < col; j++ )
                   2125:       if ( tmi[j] ) {
                   2126:         muln(tmi[j],NM(dn0),&s);
                   2127:         remn(s,md,&u);
                   2128:         ret = inttorat(u,md,b,&sgn,&unm,&udn);
                   2129:         if ( !ret )
                   2130:           return 0;
                   2131:         else {
                   2132:           NTOQ(unm,sgn,nm1);
                   2133:           NTOQ(udn,1,dn1);
                   2134:           if ( !UNIQ(dn1) ) {
                   2135:             for ( k = 0; k < i; k++ )
                   2136:               for ( l = 0, nmk = rmat[k]; l < col; l++ ) {
                   2137:                 mulq(nmk[l],dn1,&q); nmk[l] = q;
                   2138:               }
                   2139:             for ( l = 0, nmk = rmat[i]; l < j; l++ ) {
                   2140:               mulq(nmk[l],dn1,&q); nmk[l] = q;
                   2141:             }
                   2142:           }
                   2143:           rmat[i][j] = nm1;
                   2144:           mulq(dn0,dn1,&q); dn0 = q;
                   2145:         }
                   2146:       }
                   2147:   *dn = dn0;
                   2148:   return 1;
1.1       noro     2149: }
                   2150:
1.3       noro     2151: /* mat->body = Q ** */
                   2152:
1.24      noro     2153: int intmtoratm_q(MAT mat,N md,MAT nm,Q *dn)
1.3       noro     2154: {
1.76      noro     2155:   N t,s,b;
                   2156:   Q dn0,dn1,nm1,q;
                   2157:   int i,j,k,l,row,col;
                   2158:   Q **rmat;
                   2159:   Q **tmat;
                   2160:   Q *tmi;
                   2161:   Q *nmk;
                   2162:   N u,unm,udn;
                   2163:   int sgn,ret;
                   2164:
                   2165:   if ( UNIN(md) )
                   2166:     return 0;
                   2167:   row = mat->row; col = mat->col;
                   2168:   bshiftn(md,1,&t);
                   2169:   isqrt(t,&s);
                   2170:   bshiftn(s,64,&b);
                   2171:   if ( !b )
                   2172:     b = ONEN;
                   2173:   dn0 = ONE;
                   2174:   tmat = (Q **)mat->body;
                   2175:   rmat = (Q **)nm->body;
                   2176:   for ( i = 0; i < row; i++ )
                   2177:     for ( j = 0, tmi = tmat[i]; j < col; j++ )
                   2178:       if ( tmi[j] ) {
                   2179:         muln(NM(tmi[j]),NM(dn0),&s);
                   2180:         remn(s,md,&u);
                   2181:         ret = inttorat(u,md,b,&sgn,&unm,&udn);
                   2182:         if ( !ret )
                   2183:           return 0;
                   2184:         else {
                   2185:           if ( SGN(tmi[j])<0 )
                   2186:             sgn = -sgn;
                   2187:           NTOQ(unm,sgn,nm1);
                   2188:           NTOQ(udn,1,dn1);
                   2189:           if ( !UNIQ(dn1) ) {
                   2190:             for ( k = 0; k < i; k++ )
                   2191:               for ( l = 0, nmk = rmat[k]; l < col; l++ ) {
                   2192:                 mulq(nmk[l],dn1,&q); nmk[l] = q;
                   2193:               }
                   2194:             for ( l = 0, nmk = rmat[i]; l < j; l++ ) {
                   2195:               mulq(nmk[l],dn1,&q); nmk[l] = q;
                   2196:             }
                   2197:           }
                   2198:           rmat[i][j] = nm1;
                   2199:           mulq(dn0,dn1,&q); dn0 = q;
                   2200:         }
                   2201:       }
                   2202:   *dn = dn0;
                   2203:   return 1;
1.3       noro     2204: }
                   2205:
1.4       noro     2206: #define ONE_STEP1  if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
                   2207:
1.24      noro     2208: void reduce_reducers_mod(int **mat,int row,int col,int md)
1.4       noro     2209: {
1.76      noro     2210:   int i,j,k,l,hc,zzz;
                   2211:   int *t,*s,*tj,*ind;
1.4       noro     2212:
1.76      noro     2213:   /* reduce the reducers */
                   2214:   ind = (int *)ALLOCA(row*sizeof(int));
                   2215:   for ( i = 0; i < row; i++ ) {
                   2216:     t = mat[i];
                   2217:     for ( j = 0; j < col && !t[j]; j++ );
                   2218:     /* register the position of the head term */
                   2219:     ind[i] = j;
                   2220:     for ( l = i-1; l >= 0; l-- ) {
                   2221:       /* reduce mat[i] by mat[l] */
                   2222:       if ( hc = t[ind[l]] ) {
                   2223:         /* mat[i] = mat[i]-hc*mat[l] */
                   2224:         j = ind[l];
                   2225:         s = mat[l]+j;
                   2226:         tj = t+j;
                   2227:         hc = md-hc;
                   2228:         k = col-j;
                   2229:         for ( ; k >= 64; k -= 64 ) {
                   2230:           ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   2231:           ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   2232:           ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   2233:           ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   2234:           ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   2235:           ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   2236:           ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   2237:           ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   2238:           ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   2239:           ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   2240:           ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   2241:           ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   2242:           ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   2243:           ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   2244:           ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   2245:           ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   2246:         }
                   2247:         for ( ; k > 0; k-- ) {
                   2248:           if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
                   2249:         }
                   2250:       }
                   2251:     }
                   2252:   }
1.4       noro     2253: }
                   2254:
                   2255: /*
1.76      noro     2256:   mat[i] : reducers (i=0,...,nred-1)
                   2257:            spolys (i=nred,...,row-1)
                   2258:   mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
                   2259:   1. reduce the reducers
                   2260:   2. reduce spolys by the reduced reducers
1.4       noro     2261: */
                   2262:
1.24      noro     2263: void pre_reduce_mod(int **mat,int row,int col,int nred,int md)
1.4       noro     2264: {
1.76      noro     2265:   int i,j,k,l,hc,inv;
                   2266:   int *t,*s,*tk,*ind;
1.4       noro     2267:
                   2268: #if 1
1.76      noro     2269:   /* reduce the reducers */
                   2270:   ind = (int *)ALLOCA(row*sizeof(int));
                   2271:   for ( i = 0; i < nred; i++ ) {
                   2272:     /* make mat[i] monic and mat[i] by mat[0],...,mat[i-1] */
                   2273:     t = mat[i];
                   2274:     for ( j = 0; j < col && !t[j]; j++ );
                   2275:     /* register the position of the head term */
                   2276:     ind[i] = j;
                   2277:     inv = invm(t[j],md);
                   2278:     for ( k = j; k < col; k++ )
                   2279:       if ( t[k] )
                   2280:         DMAR(t[k],inv,0,md,t[k])
                   2281:     for ( l = i-1; l >= 0; l-- ) {
                   2282:       /* reduce mat[i] by mat[l] */
                   2283:       if ( hc = t[ind[l]] ) {
                   2284:         /* mat[i] = mat[i]-hc*mat[l] */
                   2285:         for ( k = ind[l], hc = md-hc, s = mat[l]+k, tk = t+k;
                   2286:           k < col; k++, tk++, s++ )
                   2287:           if ( *s )
                   2288:             DMAR(*s,hc,*tk,md,*tk)
                   2289:       }
                   2290:     }
                   2291:   }
                   2292:   /* reduce the spolys */
                   2293:   for ( i = nred; i < row; i++ ) {
                   2294:     t = mat[i];
                   2295:     for ( l = nred-1; l >= 0; l-- ) {
                   2296:       /* reduce mat[i] by mat[l] */
                   2297:       if ( hc = t[ind[l]] ) {
                   2298:         /* mat[i] = mat[i]-hc*mat[l] */
                   2299:         for ( k = ind[l], hc = md-hc, s = mat[l]+k, tk = t+k;
                   2300:           k < col; k++, tk++, s++ )
                   2301:           if ( *s )
                   2302:             DMAR(*s,hc,*tk,md,*tk)
                   2303:       }
                   2304:     }
                   2305:   }
1.4       noro     2306: #endif
                   2307: }
                   2308: /*
1.76      noro     2309:   mat[i] : reducers (i=0,...,nred-1)
                   2310:   mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
1.4       noro     2311: */
                   2312:
1.24      noro     2313: void reduce_sp_by_red_mod(int *sp,int **redmat,int *ind,int nred,int col,int md)
1.4       noro     2314: {
1.76      noro     2315:   int i,j,k,hc,zzz;
                   2316:   int *s,*tj;
1.4       noro     2317:
1.76      noro     2318:   /* reduce the spolys by redmat */
                   2319:   for ( i = nred-1; i >= 0; i-- ) {
                   2320:     /* reduce sp by redmat[i] */
                   2321:     if ( hc = sp[ind[i]] ) {
                   2322:       /* sp = sp-hc*redmat[i] */
                   2323:       j = ind[i];
                   2324:       hc = md-hc;
                   2325:       s = redmat[i]+j;
                   2326:       tj = sp+j;
                   2327:       for ( k = col-j; k > 0; k-- ) {
                   2328:         if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
                   2329:       }
                   2330:     }
                   2331:   }
1.17      noro     2332: }
                   2333:
                   2334: /*
1.76      noro     2335:   mat[i] : compressed reducers (i=0,...,nred-1)
                   2336:   mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
1.15      noro     2337: */
                   2338:
1.24      noro     2339: void red_by_compress(int m,unsigned int *p,unsigned int *r,
1.76      noro     2340:   unsigned int *ri,unsigned int hc,int len)
1.18      noro     2341: {
1.76      noro     2342:   unsigned int up,lo;
                   2343:   unsigned int dmy;
                   2344:   unsigned int *pj;
                   2345:
                   2346:   p[*ri] = 0; r++; ri++;
                   2347:   for ( len--; len; len--, r++, ri++ ) {
                   2348:     pj = p+ *ri;
                   2349:     DMA(*r,hc,*pj,up,lo);
                   2350:     if ( up ) {
                   2351:       DSAB(m,up,lo,dmy,*pj);
                   2352:     } else
                   2353:       *pj = lo;
                   2354:   }
1.18      noro     2355: }
                   2356:
                   2357: /* p -= hc*r */
                   2358:
1.24      noro     2359: void red_by_vect(int m,unsigned int *p,unsigned int *r,unsigned int hc,int len)
1.18      noro     2360: {
1.76      noro     2361:   unsigned int up,lo,dmy;
1.18      noro     2362:
1.76      noro     2363:   *p++ = 0; r++; len--;
                   2364:   for ( ; len; len--, r++, p++ )
                   2365:     if ( *r ) {
                   2366:       DMA(*r,hc,*p,up,lo);
                   2367:       if ( up ) {
                   2368:         DSAB(m,up,lo,dmy,*p);
                   2369:       } else
                   2370:         *p = lo;
                   2371:     }
1.18      noro     2372: }
                   2373:
1.75      noro     2374: #if defined(__GNUC__) && SIZEOF_LONG==8
1.74      noro     2375: /* 64bit vector += UNIT vector(normalized) */
1.73      noro     2376:
1.74      noro     2377: void red_by_vect64(int m, U64 *p,unsigned int *c,U64 *r,unsigned int hc,int len)
1.73      noro     2378: {
1.74      noro     2379:   U64 t;
                   2380:
                   2381:   /* (p[0],c[0]) is normalized */
                   2382:   *p++ = 0; *c++ = 0; r++; len--;
                   2383:   for ( ; len; len--, r++, p++, c++ )
                   2384:     if ( *r ) {
                   2385:       t = (*p)+(*r)*hc;
                   2386:       if ( t < *p ) (*c)++;
                   2387:       *p = t;
                   2388:     }
1.73      noro     2389: }
                   2390: #endif
                   2391:
1.32      noro     2392: void red_by_vect_sf(int m,unsigned int *p,unsigned int *r,unsigned int hc,int len)
                   2393: {
1.76      noro     2394:   *p++ = 0; r++; len--;
                   2395:   for ( ; len; len--, r++, p++ )
                   2396:     if ( *r )
                   2397:       *p = _addsf(_mulsf(*r,hc),*p);
1.32      noro     2398: }
                   2399:
1.71      noro     2400: extern GZ current_mod_lf;
                   2401: extern int current_mod_lf_size;
                   2402:
1.70      noro     2403: void red_by_vect_lf(mpz_t *p,mpz_t *r,mpz_t hc,int len)
                   2404: {
1.76      noro     2405:   mpz_set_ui(*p++,0); r++; len--;
                   2406:   for ( ; len; len--, r++, p++ ) {
1.70      noro     2407:        mpz_addmul(*p,*r,hc);
1.71      noro     2408: #if 0
                   2409:        if ( mpz_size(*p) > current_mod_lf_size )
                   2410:          mpz_mod(*p,*p,BDY(current_mod_lf));
                   2411: #endif
                   2412:     }
1.70      noro     2413: }
                   2414:
                   2415:
1.21      noro     2416: extern unsigned int **psca;
                   2417:
1.24      noro     2418: void reduce_sp_by_red_mod_compress (int *sp,CDP *redmat,int *ind,
1.76      noro     2419:   int nred,int col,int md)
1.15      noro     2420: {
1.76      noro     2421:   int i,len;
                   2422:   CDP ri;
                   2423:   unsigned int hc;
                   2424:   unsigned int *usp;
                   2425:
                   2426:   usp = (unsigned int *)sp;
                   2427:   /* reduce the spolys by redmat */
                   2428:   for ( i = nred-1; i >= 0; i-- ) {
                   2429:     /* reduce sp by redmat[i] */
                   2430:     usp[ind[i]] %= md;
                   2431:     if ( hc = usp[ind[i]] ) {
                   2432:       /* sp = sp-hc*redmat[i] */
                   2433:       hc = md-hc;
                   2434:       ri = redmat[i];
                   2435:       len = ri->len;
                   2436:       red_by_compress(md,usp,psca[ri->psindex],ri->body,hc,len);
                   2437:     }
                   2438:   }
                   2439:   for ( i = 0; i < col; i++ )
                   2440:     if ( usp[i] >= (unsigned int)md )
                   2441:       usp[i] %= md;
1.4       noro     2442: }
                   2443:
                   2444: #define ONE_STEP2  if ( zzz = *pk ) { DMAR(zzz,a,*tk,md,*tk) } pk++; tk++;
                   2445:
1.24      noro     2446: int generic_gauss_elim_mod(int **mat0,int row,int col,int md,int *colstat)
1.1       noro     2447: {
1.76      noro     2448:   int i,j,k,l,inv,a,rank;
                   2449:   unsigned int *t,*pivot,*pk;
                   2450:   unsigned int **mat;
                   2451:
                   2452:   mat = (unsigned int **)mat0;
                   2453:   for ( rank = 0, j = 0; j < col; j++ ) {
                   2454:     for ( i = rank; i < row; i++ )
                   2455:       mat[i][j] %= md;
                   2456:     for ( i = rank; i < row; i++ )
                   2457:       if ( mat[i][j] )
                   2458:         break;
                   2459:     if ( i == row ) {
                   2460:       colstat[j] = 0;
                   2461:       continue;
                   2462:     } else
                   2463:       colstat[j] = 1;
                   2464:     if ( i != rank ) {
                   2465:       t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   2466:     }
                   2467:     pivot = mat[rank];
                   2468:     inv = invm(pivot[j],md);
                   2469:     for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   2470:       if ( *pk ) {
                   2471:         if ( *pk >= (unsigned int)md )
                   2472:           *pk %= md;
                   2473:         DMAR(*pk,inv,0,md,*pk)
                   2474:       }
                   2475:     for ( i = rank+1; i < row; i++ ) {
                   2476:       t = mat[i];
                   2477:       if ( a = t[j] )
                   2478:         red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   2479:     }
                   2480:     rank++;
                   2481:   }
                   2482:   for ( j = col-1, l = rank-1; j >= 0; j-- )
                   2483:     if ( colstat[j] ) {
                   2484:       pivot = mat[l];
                   2485:       for ( i = 0; i < l; i++ ) {
                   2486:         t = mat[i];
                   2487:         t[j] %= md;
                   2488:         if ( a = t[j] )
                   2489:           red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   2490:       }
                   2491:       l--;
                   2492:     }
                   2493:   for ( j = 0, l = 0; l < rank; j++ )
                   2494:     if ( colstat[j] ) {
                   2495:       t = mat[l];
                   2496:       for ( k = j; k < col; k++ )
                   2497:         if ( t[k] >= (unsigned int)md )
                   2498:           t[k] %= md;
                   2499:       l++;
                   2500:     }
                   2501:   return rank;
1.32      noro     2502: }
                   2503:
1.65      noro     2504: int generic_gauss_elim_mod2(int **mat0,int row,int col,int md,int *colstat,int *rowstat)
                   2505: {
1.76      noro     2506:   int i,j,k,l,inv,a,rank;
                   2507:   unsigned int *t,*pivot,*pk;
                   2508:   unsigned int **mat;
                   2509:
                   2510:   for ( i = 0; i < row; i++ ) rowstat[i] = i;
                   2511:   mat = (unsigned int **)mat0;
                   2512:   for ( rank = 0, j = 0; j < col; j++ ) {
                   2513:     for ( i = rank; i < row; i++ )
                   2514:       mat[i][j] %= md;
                   2515:     for ( i = rank; i < row; i++ )
                   2516:       if ( mat[i][j] )
                   2517:         break;
                   2518:     if ( i == row ) {
                   2519:       colstat[j] = 0;
                   2520:       continue;
                   2521:     } else
                   2522:       colstat[j] = 1;
                   2523:     if ( i != rank ) {
                   2524:       t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   2525:       k = rowstat[i]; rowstat[i] = rowstat[rank]; rowstat[rank] = k;
                   2526:     }
                   2527:     pivot = mat[rank];
                   2528:     inv = invm(pivot[j],md);
                   2529:     for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   2530:       if ( *pk ) {
                   2531:         if ( *pk >= (unsigned int)md )
                   2532:           *pk %= md;
                   2533:         DMAR(*pk,inv,0,md,*pk)
                   2534:       }
                   2535:     for ( i = rank+1; i < row; i++ ) {
                   2536:       t = mat[i];
                   2537:       if ( a = t[j] )
                   2538:         red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   2539:     }
                   2540:     rank++;
                   2541:   }
                   2542:   for ( j = col-1, l = rank-1; j >= 0; j-- )
                   2543:     if ( colstat[j] ) {
                   2544:       pivot = mat[l];
                   2545:       for ( i = 0; i < l; i++ ) {
                   2546:         t = mat[i];
                   2547:         t[j] %= md;
                   2548:         if ( a = t[j] )
                   2549:           red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   2550:       }
                   2551:       l--;
                   2552:     }
                   2553:   for ( j = 0, l = 0; l < rank; j++ )
                   2554:     if ( colstat[j] ) {
                   2555:       t = mat[l];
                   2556:       for ( k = j; k < col; k++ )
                   2557:         if ( t[k] >= (unsigned int)md )
                   2558:           t[k] %= md;
                   2559:       l++;
                   2560:     }
                   2561:   return rank;
1.65      noro     2562: }
                   2563:
1.69      noro     2564: int indep_rows_mod(int **mat0,int row,int col,int md,int *rowstat)
                   2565: {
1.76      noro     2566:   int i,j,k,l,inv,a,rank;
                   2567:   unsigned int *t,*pivot,*pk;
                   2568:   unsigned int **mat;
                   2569:
                   2570:   for ( i = 0; i < row; i++ ) rowstat[i] = i;
                   2571:   mat = (unsigned int **)mat0;
                   2572:   for ( rank = 0, j = 0; j < col; j++ ) {
                   2573:     for ( i = rank; i < row; i++ )
                   2574:       mat[i][j] %= md;
                   2575:     for ( i = rank; i < row; i++ )
                   2576:       if ( mat[i][j] )
                   2577:         break;
                   2578:     if ( i == row ) continue;
                   2579:     if ( i != rank ) {
                   2580:       t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   2581:       k = rowstat[i]; rowstat[i] = rowstat[rank]; rowstat[rank] = k;
                   2582:     }
                   2583:     pivot = mat[rank];
                   2584:     inv = invm(pivot[j],md);
                   2585:     for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   2586:       if ( *pk ) {
                   2587:         if ( *pk >= (unsigned int)md )
                   2588:           *pk %= md;
                   2589:         DMAR(*pk,inv,0,md,*pk)
                   2590:       }
                   2591:     for ( i = rank+1; i < row; i++ ) {
                   2592:       t = mat[i];
                   2593:       if ( a = t[j] )
                   2594:         red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   2595:     }
                   2596:     rank++;
                   2597:   }
                   2598:   return rank;
1.69      noro     2599: }
                   2600:
1.32      noro     2601: int generic_gauss_elim_sf(int **mat0,int row,int col,int md,int *colstat)
                   2602: {
1.76      noro     2603:   int i,j,k,l,inv,a,rank;
                   2604:   unsigned int *t,*pivot,*pk;
                   2605:   unsigned int **mat;
                   2606:
                   2607:   mat = (unsigned int **)mat0;
                   2608:   for ( rank = 0, j = 0; j < col; j++ ) {
                   2609:     for ( i = rank; i < row; i++ )
                   2610:       if ( mat[i][j] )
                   2611:         break;
                   2612:     if ( i == row ) {
                   2613:       colstat[j] = 0;
                   2614:       continue;
                   2615:     } else
                   2616:       colstat[j] = 1;
                   2617:     if ( i != rank ) {
                   2618:       t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   2619:     }
                   2620:     pivot = mat[rank];
                   2621:     inv = _invsf(pivot[j]);
                   2622:     for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   2623:       if ( *pk )
                   2624:         *pk = _mulsf(*pk,inv);
                   2625:     for ( i = rank+1; i < row; i++ ) {
                   2626:       t = mat[i];
                   2627:       if ( a = t[j] )
                   2628:         red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   2629:     }
                   2630:     rank++;
                   2631:   }
                   2632:   for ( j = col-1, l = rank-1; j >= 0; j-- )
                   2633:     if ( colstat[j] ) {
                   2634:       pivot = mat[l];
                   2635:       for ( i = 0; i < l; i++ ) {
                   2636:         t = mat[i];
                   2637:         if ( a = t[j] )
                   2638:           red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   2639:       }
                   2640:       l--;
                   2641:     }
                   2642:   return rank;
1.1       noro     2643: }
                   2644:
                   2645: /* LU decomposition; a[i][i] = 1/U[i][i] */
                   2646:
1.24      noro     2647: int lu_gfmmat(GFMMAT mat,unsigned int md,int *perm)
1.1       noro     2648: {
1.76      noro     2649:   int row,col;
                   2650:   int i,j,k;
                   2651:   unsigned int *t,*pivot;
                   2652:   unsigned int **a;
                   2653:   unsigned int inv,m;
                   2654:
                   2655:   row = mat->row; col = mat->col;
                   2656:   a = mat->body;
                   2657:   bzero(perm,row*sizeof(int));
                   2658:
                   2659:   for ( i = 0; i < row; i++ )
                   2660:     perm[i] = i;
                   2661:   for ( k = 0; k < col; k++ ) {
                   2662:     for ( i = k; i < row && !a[i][k]; i++ );
                   2663:     if ( i == row )
                   2664:       return 0;
                   2665:     if ( i != k ) {
                   2666:       j = perm[i]; perm[i] = perm[k]; perm[k] = j;
                   2667:       t = a[i]; a[i] = a[k]; a[k] = t;
                   2668:     }
                   2669:     pivot = a[k];
                   2670:     pivot[k] = inv = invm(pivot[k],md);
                   2671:     for ( i = k+1; i < row; i++ ) {
                   2672:       t = a[i];
                   2673:       if ( m = t[k] ) {
                   2674:         DMAR(inv,m,0,md,t[k])
                   2675:         for ( j = k+1, m = md - t[k]; j < col; j++ )
                   2676:           if ( pivot[j] ) {
                   2677:             unsigned int tj;
                   2678:
                   2679:             DMAR(m,pivot[j],t[j],md,tj)
                   2680:             t[j] = tj;
                   2681:           }
                   2682:       }
                   2683:     }
                   2684:   }
                   2685:   return 1;
1.1       noro     2686: }
                   2687:
1.3       noro     2688: /*
                   2689:  Input
1.76      noro     2690:   a: a row x col matrix
                   2691:   md : a modulus
1.3       noro     2692:
                   2693:  Output:
1.76      noro     2694:   return : d = the rank of mat
                   2695:   a[0..(d-1)][0..(d-1)] : LU decomposition (a[i][i] = 1/U[i][i])
                   2696:   rinfo: array of length row
                   2697:   cinfo: array of length col
1.3       noro     2698:     i-th row in new a <-> rinfo[i]-th row in old a
1.76      noro     2699:   cinfo[j]=1 <=> j-th column is contained in the LU decomp.
1.3       noro     2700: */
                   2701:
1.24      noro     2702: int find_lhs_and_lu_mod(unsigned int **a,int row,int col,
1.76      noro     2703:   unsigned int md,int **rinfo,int **cinfo)
1.3       noro     2704: {
1.76      noro     2705:   int i,j,k,d;
                   2706:   int *rp,*cp;
                   2707:   unsigned int *t,*pivot;
                   2708:   unsigned int inv,m;
                   2709:
                   2710:   *rinfo = rp = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   2711:   *cinfo = cp = (int *)MALLOC_ATOMIC(col*sizeof(int));
                   2712:   for ( i = 0; i < row; i++ )
                   2713:     rp[i] = i;
                   2714:   for ( k = 0, d = 0; k < col; k++ ) {
                   2715:     for ( i = d; i < row && !a[i][k]; i++ );
                   2716:     if ( i == row ) {
                   2717:       cp[k] = 0;
                   2718:       continue;
                   2719:     } else
                   2720:       cp[k] = 1;
                   2721:     if ( i != d ) {
                   2722:       j = rp[i]; rp[i] = rp[d]; rp[d] = j;
                   2723:       t = a[i]; a[i] = a[d]; a[d] = t;
                   2724:     }
                   2725:     pivot = a[d];
                   2726:     pivot[k] = inv = invm(pivot[k],md);
                   2727:     for ( i = d+1; i < row; i++ ) {
                   2728:       t = a[i];
                   2729:       if ( m = t[k] ) {
                   2730:         DMAR(inv,m,0,md,t[k])
                   2731:         for ( j = k+1, m = md - t[k]; j < col; j++ )
                   2732:           if ( pivot[j] ) {
                   2733:             unsigned int tj;
                   2734:             DMAR(m,pivot[j],t[j],md,tj)
                   2735:             t[j] = tj;
                   2736:           }
                   2737:       }
                   2738:     }
                   2739:     d++;
                   2740:   }
                   2741:   return d;
1.3       noro     2742: }
                   2743:
1.53      noro     2744: int lu_mod(unsigned int **a,int n,unsigned int md,int **rinfo)
                   2745: {
1.76      noro     2746:   int i,j,k;
                   2747:   int *rp;
                   2748:   unsigned int *t,*pivot;
                   2749:   unsigned int inv,m;
                   2750:
                   2751:   *rinfo = rp = (int *)MALLOC_ATOMIC(n*sizeof(int));
                   2752:   for ( i = 0; i < n; i++ ) rp[i] = i;
                   2753:   for ( k = 0; k < n; k++ ) {
                   2754:     for ( i = k; i < n && !a[i][k]; i++ );
                   2755:     if ( i == n ) return 0;
                   2756:     if ( i != k ) {
                   2757:       j = rp[i]; rp[i] = rp[k]; rp[k] = j;
                   2758:       t = a[i]; a[i] = a[k]; a[k] = t;
                   2759:     }
                   2760:     pivot = a[k];
                   2761:     inv = invm(pivot[k],md);
                   2762:     for ( i = k+1; i < n; i++ ) {
                   2763:       t = a[i];
                   2764:       if ( m = t[k] ) {
                   2765:         DMAR(inv,m,0,md,t[k])
                   2766:         for ( j = k+1, m = md - t[k]; j < n; j++ )
                   2767:           if ( pivot[j] ) {
                   2768:             unsigned int tj;
                   2769:             DMAR(m,pivot[j],t[j],md,tj)
                   2770:             t[j] = tj;
                   2771:           }
                   2772:       }
                   2773:     }
                   2774:   }
                   2775:   return 1;
1.53      noro     2776: }
                   2777:
1.3       noro     2778: /*
                   2779:   Input
1.76      noro     2780:   a : n x n matrix; a result of LU-decomposition
                   2781:   md : modulus
                   2782:   b : n x l matrix
1.3       noro     2783:  Output
1.76      noro     2784:   b = a^(-1)b
1.3       noro     2785:  */
                   2786:
1.44      noro     2787: void solve_by_lu_mod(int **a,int n,int md,int **b,int l,int normalize)
1.3       noro     2788: {
1.76      noro     2789:   unsigned int *y,*c;
                   2790:   int i,j,k;
                   2791:   unsigned int t,m,m2;
                   2792:
                   2793:   y = (int *)MALLOC_ATOMIC(n*sizeof(int));
                   2794:   c = (int *)MALLOC_ATOMIC(n*sizeof(int));
                   2795:   m2 = md>>1;
                   2796:   for ( k = 0; k < l; k++ ) {
                   2797:     /* copy b[.][k] to c */
                   2798:     for ( i = 0; i < n; i++ )
                   2799:       c[i] = (unsigned int)b[i][k];
                   2800:     /* solve Ly=c */
                   2801:     for ( i = 0; i < n; i++ ) {
                   2802:       for ( t = c[i], j = 0; j < i; j++ )
                   2803:         if ( a[i][j] ) {
                   2804:           m = md - a[i][j];
                   2805:           DMAR(m,y[j],t,md,t)
                   2806:         }
                   2807:       y[i] = t;
                   2808:     }
                   2809:     /* solve Uc=y */
                   2810:     for ( i = n-1; i >= 0; i-- ) {
                   2811:       for ( t = y[i], j =i+1; j < n; j++ )
                   2812:         if ( a[i][j] ) {
                   2813:           m = md - a[i][j];
                   2814:           DMAR(m,c[j],t,md,t)
                   2815:         }
                   2816:       /* a[i][i] = 1/U[i][i] */
                   2817:       DMAR(t,a[i][i],0,md,c[i])
                   2818:     }
                   2819:     /* copy c to b[.][k] with normalization */
                   2820:     if ( normalize )
                   2821:       for ( i = 0; i < n; i++ )
                   2822:         b[i][k] = (int)(c[i]>m2 ? c[i]-md : c[i]);
                   2823:     else
                   2824:       for ( i = 0; i < n; i++ )
                   2825:         b[i][k] = c[i];
                   2826:   }
1.3       noro     2827: }
                   2828:
1.24      noro     2829: void Pleqm1(NODE arg,VECT *rp)
1.1       noro     2830: {
1.76      noro     2831:   MAT m;
                   2832:   VECT vect;
                   2833:   pointer **mat;
                   2834:   Q *v;
                   2835:   Q q;
                   2836:   int **wmat;
                   2837:   int md,i,j,row,col,t,n,status;
                   2838:
                   2839:   asir_assert(ARG0(arg),O_MAT,"leqm1");
                   2840:   asir_assert(ARG1(arg),O_N,"leqm1");
                   2841:   m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
                   2842:   row = m->row; col = m->col; mat = m->body;
                   2843:   wmat = (int **)almat(row,col);
                   2844:   for ( i = 0; i < row; i++ )
                   2845:     for ( j = 0; j < col; j++ )
                   2846:       if ( q = (Q)mat[i][j] ) {
                   2847:         t = rem(NM(q),md);
                   2848:         if ( SGN(q) < 0 )
                   2849:           t = (md - t) % md;
                   2850:         wmat[i][j] = t;
                   2851:       } else
                   2852:         wmat[i][j] = 0;
                   2853:   status = gauss_elim_mod1(wmat,row,col,md);
                   2854:   if ( status < 0 )
                   2855:     *rp = 0;
                   2856:   else if ( status > 0 )
                   2857:     *rp = (VECT)ONE;
                   2858:   else {
                   2859:     n = col - 1;
                   2860:     MKVECT(vect,n);
                   2861:     for ( i = 0, v = (Q *)vect->body; i < n; i++ ) {
                   2862:       t = (md-wmat[i][n])%md; STOQ(t,v[i]);
                   2863:     }
                   2864:     *rp = vect;
                   2865:   }
1.1       noro     2866: }
                   2867:
1.24      noro     2868: int gauss_elim_mod1(int **mat,int row,int col,int md)
1.1       noro     2869: {
1.76      noro     2870:   int i,j,k,inv,a,n;
                   2871:   int *t,*pivot;
1.1       noro     2872:
1.76      noro     2873:   n = col - 1;
                   2874:   for ( j = 0; j < n; j++ ) {
                   2875:     for ( i = j; i < row && !mat[i][j]; i++ );
                   2876:     if ( i == row )
                   2877:       return 1;
                   2878:     if ( i != j ) {
                   2879:       t = mat[i]; mat[i] = mat[j]; mat[j] = t;
                   2880:     }
                   2881:     pivot = mat[j];
                   2882:     inv = invm(pivot[j],md);
                   2883:     for ( k = j; k <= n; k++ )
                   2884:       pivot[k] = dmar(pivot[k],inv,0,md);
                   2885:     for ( i = j+1; i < row; i++ ) {
                   2886:       t = mat[i];
                   2887:       if ( i != j && (a = t[j]) )
                   2888:         for ( k = j, a = md - a; k <= n; k++ )
                   2889:           t[k] = dmar(pivot[k],a,t[k],md);
                   2890:     }
                   2891:   }
                   2892:   for ( i = n; i < row && !mat[i][n]; i++ );
                   2893:   if ( i == row ) {
                   2894:     for ( j = n-1; j >= 0; j-- ) {
                   2895:       for ( i = j-1, a = (md-mat[j][n])%md; i >= 0; i-- ) {
                   2896:         mat[i][n] = dmar(mat[i][j],a,mat[i][n],md);
                   2897:         mat[i][j] = 0;
                   2898:       }
                   2899:     }
                   2900:     return 0;
                   2901:   } else
                   2902:     return -1;
1.1       noro     2903: }
                   2904:
1.24      noro     2905: void Pgeninvm(NODE arg,LIST *rp)
1.1       noro     2906: {
1.76      noro     2907:   MAT m;
                   2908:   pointer **mat;
                   2909:   Q **tmat;
                   2910:   Q q;
                   2911:   unsigned int **wmat;
                   2912:   int md,i,j,row,col,t,status;
                   2913:   MAT mat1,mat2;
                   2914:   NODE node1,node2;
                   2915:
                   2916:   asir_assert(ARG0(arg),O_MAT,"leqm1");
                   2917:   asir_assert(ARG1(arg),O_N,"leqm1");
                   2918:   m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
                   2919:   row = m->row; col = m->col; mat = m->body;
                   2920:   wmat = (unsigned int **)almat(row,col+row);
                   2921:   for ( i = 0; i < row; i++ ) {
                   2922:     bzero((char *)wmat[i],(col+row)*sizeof(int));
                   2923:     for ( j = 0; j < col; j++ )
                   2924:       if ( q = (Q)mat[i][j] ) {
                   2925:         t = rem(NM(q),md);
                   2926:         if ( SGN(q) < 0 )
                   2927:           t = (md - t) % md;
                   2928:         wmat[i][j] = t;
                   2929:       }
                   2930:     wmat[i][col+i] = 1;
                   2931:   }
                   2932:   status = gauss_elim_geninv_mod(wmat,row,col,md);
                   2933:   if ( status > 0 )
                   2934:     *rp = 0;
                   2935:   else {
                   2936:     MKMAT(mat1,col,row); MKMAT(mat2,row-col,row);
                   2937:     for ( i = 0, tmat = (Q **)mat1->body; i < col; i++ )
                   2938:       for ( j = 0; j < row; j++ )
                   2939:         UTOQ(wmat[i][j+col],tmat[i][j]);
                   2940:     for ( tmat = (Q **)mat2->body; i < row; i++ )
                   2941:       for ( j = 0; j < row; j++ )
                   2942:         UTOQ(wmat[i][j+col],tmat[i-col][j]);
                   2943:      MKNODE(node2,mat2,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
                   2944:   }
1.1       noro     2945: }
                   2946:
1.24      noro     2947: int gauss_elim_geninv_mod(unsigned int **mat,int row,int col,int md)
1.1       noro     2948: {
1.76      noro     2949:   int i,j,k,inv,a,n,m;
                   2950:   unsigned int *t,*pivot;
1.1       noro     2951:
1.76      noro     2952:   n = col; m = row+col;
                   2953:   for ( j = 0; j < n; j++ ) {
                   2954:     for ( i = j; i < row && !mat[i][j]; i++ );
                   2955:     if ( i == row )
                   2956:       return 1;
                   2957:     if ( i != j ) {
                   2958:       t = mat[i]; mat[i] = mat[j]; mat[j] = t;
                   2959:     }
                   2960:     pivot = mat[j];
                   2961:     inv = invm(pivot[j],md);
                   2962:     for ( k = j; k < m; k++ )
                   2963:       pivot[k] = dmar(pivot[k],inv,0,md);
                   2964:     for ( i = j+1; i < row; i++ ) {
                   2965:       t = mat[i];
                   2966:       if ( a = t[j] )
                   2967:         for ( k = j, a = md - a; k < m; k++ )
                   2968:           t[k] = dmar(pivot[k],a,t[k],md);
                   2969:     }
                   2970:   }
                   2971:   for ( j = n-1; j >= 0; j-- ) {
                   2972:     pivot = mat[j];
                   2973:     for ( i = j-1; i >= 0; i-- ) {
                   2974:       t = mat[i];
                   2975:       if ( a = t[j] )
                   2976:         for ( k = j, a = md - a; k < m; k++ )
                   2977:           t[k] = dmar(pivot[k],a,t[k],md);
                   2978:     }
                   2979:   }
                   2980:   return 0;
1.1       noro     2981: }
                   2982:
1.24      noro     2983: void Psolve_by_lu_gfmmat(NODE arg,VECT *rp)
1.1       noro     2984: {
1.76      noro     2985:   GFMMAT lu;
                   2986:   Q *perm,*rhs,*v;
                   2987:   int n,i;
                   2988:   unsigned int md;
                   2989:   unsigned int *b,*sol;
                   2990:   VECT r;
                   2991:
                   2992:   lu = (GFMMAT)ARG0(arg);
                   2993:   perm = (Q *)BDY((VECT)ARG1(arg));
                   2994:   rhs = (Q *)BDY((VECT)ARG2(arg));
                   2995:   md = (unsigned int)QTOS((Q)ARG3(arg));
                   2996:   n = lu->col;
                   2997:   b = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
                   2998:   sol = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
                   2999:   for ( i = 0; i < n; i++ )
                   3000:     b[i] = QTOS(rhs[QTOS(perm[i])]);
                   3001:   solve_by_lu_gfmmat(lu,md,b,sol);
                   3002:   MKVECT(r,n);
                   3003:   for ( i = 0, v = (Q *)r->body; i < n; i++ )
                   3004:       UTOQ(sol[i],v[i]);
                   3005:   *rp = r;
1.1       noro     3006: }
                   3007:
1.24      noro     3008: void solve_by_lu_gfmmat(GFMMAT lu,unsigned int md,
1.76      noro     3009:   unsigned int *b,unsigned int *x)
1.1       noro     3010: {
1.76      noro     3011:   int n;
                   3012:   unsigned int **a;
                   3013:   unsigned int *y;
                   3014:   int i,j;
                   3015:   unsigned int t,m;
                   3016:
                   3017:   n = lu->col;
                   3018:   a = lu->body;
                   3019:   y = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
                   3020:   /* solve Ly=b */
                   3021:   for ( i = 0; i < n; i++ ) {
                   3022:     for ( t = b[i], j = 0; j < i; j++ )
                   3023:       if ( a[i][j] ) {
                   3024:         m = md - a[i][j];
                   3025:         DMAR(m,y[j],t,md,t)
                   3026:       }
                   3027:     y[i] = t;
                   3028:   }
                   3029:   /* solve Ux=y */
                   3030:   for ( i = n-1; i >= 0; i-- ) {
                   3031:     for ( t = y[i], j =i+1; j < n; j++ )
                   3032:       if ( a[i][j] ) {
                   3033:         m = md - a[i][j];
                   3034:         DMAR(m,x[j],t,md,t)
                   3035:       }
                   3036:     /* a[i][i] = 1/U[i][i] */
                   3037:     DMAR(t,a[i][i],0,md,x[i])
                   3038:   }
1.1       noro     3039: }
                   3040:
1.53      noro     3041: void Plu_mat(NODE arg,LIST *rp)
                   3042: {
1.76      noro     3043:   MAT m,lu;
                   3044:   Q dn;
                   3045:   Q *v;
                   3046:   int n,i;
                   3047:   int *iperm;
                   3048:   VECT perm;
                   3049:   NODE n0;
                   3050:
                   3051:   asir_assert(ARG0(arg),O_MAT,"lu_mat");
                   3052:   m = (MAT)ARG0(arg);
                   3053:   n = m->row;
                   3054:   MKMAT(lu,n,n);
                   3055:   lu_dec_cr(m,lu,&dn,&iperm);
                   3056:   MKVECT(perm,n);
                   3057:   for ( i = 0, v = (Q *)perm->body; i < n; i++ )
                   3058:     STOQ(iperm[i],v[i]);
                   3059:   n0 = mknode(3,lu,dn,perm);
                   3060:   MKLIST(*rp,n0);
1.53      noro     3061: }
                   3062:
1.24      noro     3063: void Plu_gfmmat(NODE arg,LIST *rp)
1.1       noro     3064: {
1.76      noro     3065:   MAT m;
                   3066:   GFMMAT mm;
                   3067:   unsigned int md;
                   3068:   int i,row,col,status;
                   3069:   int *iperm;
                   3070:   Q *v;
                   3071:   VECT perm;
                   3072:   NODE n0;
                   3073:
                   3074:   asir_assert(ARG0(arg),O_MAT,"lu_gfmmat");
                   3075:   asir_assert(ARG1(arg),O_N,"lu_gfmmat");
                   3076:   m = (MAT)ARG0(arg); md = (unsigned int)QTOS((Q)ARG1(arg));
                   3077:   mat_to_gfmmat(m,md,&mm);
                   3078:   row = m->row;
                   3079:   col = m->col;
                   3080:   iperm = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   3081:   status = lu_gfmmat(mm,md,iperm);
                   3082:   if ( !status )
                   3083:     n0 = 0;
                   3084:   else {
                   3085:     MKVECT(perm,row);
                   3086:     for ( i = 0, v = (Q *)perm->body; i < row; i++ )
                   3087:       STOQ(iperm[i],v[i]);
                   3088:     n0 = mknode(2,mm,perm);
                   3089:   }
                   3090:   MKLIST(*rp,n0);
1.1       noro     3091: }
                   3092:
1.24      noro     3093: void Pmat_to_gfmmat(NODE arg,GFMMAT *rp)
1.1       noro     3094: {
1.76      noro     3095:   MAT m;
                   3096:   unsigned int md;
1.1       noro     3097:
1.76      noro     3098:   asir_assert(ARG0(arg),O_MAT,"mat_to_gfmmat");
                   3099:   asir_assert(ARG1(arg),O_N,"mat_to_gfmmat");
                   3100:   m = (MAT)ARG0(arg); md = (unsigned int)QTOS((Q)ARG1(arg));
                   3101:   mat_to_gfmmat(m,md,rp);
1.1       noro     3102: }
                   3103:
1.24      noro     3104: void mat_to_gfmmat(MAT m,unsigned int md,GFMMAT *rp)
1.1       noro     3105: {
1.76      noro     3106:   unsigned int **wmat;
                   3107:   unsigned int t;
                   3108:   Q **mat;
                   3109:   Q q;
                   3110:   int i,j,row,col;
                   3111:
                   3112:   row = m->row; col = m->col; mat = (Q **)m->body;
                   3113:   wmat = (unsigned int **)almat(row,col);
                   3114:   for ( i = 0; i < row; i++ ) {
                   3115:     bzero((char *)wmat[i],col*sizeof(unsigned int));
                   3116:     for ( j = 0; j < col; j++ )
                   3117:       if ( q = mat[i][j] ) {
                   3118:         t = (unsigned int)rem(NM(q),md);
                   3119:         if ( SGN(q) < 0 )
                   3120:           t = (md - t) % md;
                   3121:         wmat[i][j] = t;
                   3122:       }
                   3123:   }
                   3124:   TOGFMMAT(row,col,wmat,*rp);
1.1       noro     3125: }
                   3126:
1.72      ohara    3127: void Pgeninvm_swap(NODE arg,LIST *rp)
1.1       noro     3128: {
1.76      noro     3129:   MAT m;
                   3130:   pointer **mat;
                   3131:   Q **tmat;
                   3132:   Q *tvect;
                   3133:   Q q;
                   3134:   unsigned int **wmat,**invmat;
                   3135:   int *index;
                   3136:   unsigned int t,md;
                   3137:   int i,j,row,col,status;
                   3138:   MAT mat1;
                   3139:   VECT vect1;
                   3140:   NODE node1,node2;
                   3141:
                   3142:   asir_assert(ARG0(arg),O_MAT,"geninvm_swap");
                   3143:   asir_assert(ARG1(arg),O_N,"geninvm_swap");
                   3144:   m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
                   3145:   row = m->row; col = m->col; mat = m->body;
                   3146:   wmat = (unsigned int **)almat(row,col+row);
                   3147:   for ( i = 0; i < row; i++ ) {
                   3148:     bzero((char *)wmat[i],(col+row)*sizeof(int));
                   3149:     for ( j = 0; j < col; j++ )
                   3150:       if ( q = (Q)mat[i][j] ) {
                   3151:         t = (unsigned int)rem(NM(q),md);
                   3152:         if ( SGN(q) < 0 )
                   3153:           t = (md - t) % md;
                   3154:         wmat[i][j] = t;
                   3155:       }
                   3156:     wmat[i][col+i] = 1;
                   3157:   }
                   3158:   status = gauss_elim_geninv_mod_swap(wmat,row,col,md,&invmat,&index);
                   3159:   if ( status > 0 )
                   3160:     *rp = 0;
                   3161:   else {
                   3162:     MKMAT(mat1,col,col);
                   3163:     for ( i = 0, tmat = (Q **)mat1->body; i < col; i++ )
                   3164:       for ( j = 0; j < col; j++ )
                   3165:         UTOQ(invmat[i][j],tmat[i][j]);
                   3166:     MKVECT(vect1,row);
                   3167:     for ( i = 0, tvect = (Q *)vect1->body; i < row; i++ )
                   3168:       STOQ(index[i],tvect[i]);
                   3169:      MKNODE(node2,vect1,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
                   3170:   }
1.1       noro     3171: }
                   3172:
1.72      ohara    3173: int gauss_elim_geninv_mod_swap(unsigned int **mat,int row,int col,unsigned int md,
                   3174:     unsigned int ***invmatp,int **indexp)
1.1       noro     3175: {
1.76      noro     3176:   int i,j,k,inv,a,n,m;
                   3177:   unsigned int *t,*pivot,*s;
                   3178:   int *index;
                   3179:   unsigned int **invmat;
                   3180:
                   3181:   n = col; m = row+col;
                   3182:   *indexp = index = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   3183:   for ( i = 0; i < row; i++ )
                   3184:     index[i] = i;
                   3185:   for ( j = 0; j < n; j++ ) {
                   3186:     for ( i = j; i < row && !mat[i][j]; i++ );
                   3187:     if ( i == row ) {
                   3188:       *indexp = 0; *invmatp = 0; return 1;
                   3189:     }
                   3190:     if ( i != j ) {
                   3191:       t = mat[i]; mat[i] = mat[j]; mat[j] = t;
                   3192:       k = index[i]; index[i] = index[j]; index[j] = k;
                   3193:     }
                   3194:     pivot = mat[j];
                   3195:     inv = (unsigned int)invm(pivot[j],md);
                   3196:     for ( k = j; k < m; k++ )
                   3197:       if ( pivot[k] )
                   3198:         pivot[k] = (unsigned int)dmar(pivot[k],inv,0,md);
                   3199:     for ( i = j+1; i < row; i++ ) {
                   3200:       t = mat[i];
                   3201:       if ( a = t[j] )
                   3202:         for ( k = j, a = md - a; k < m; k++ )
                   3203:           if ( pivot[k] )
                   3204:             t[k] = dmar(pivot[k],a,t[k],md);
                   3205:     }
                   3206:   }
                   3207:   for ( j = n-1; j >= 0; j-- ) {
                   3208:     pivot = mat[j];
                   3209:     for ( i = j-1; i >= 0; i-- ) {
                   3210:       t = mat[i];
                   3211:       if ( a = t[j] )
                   3212:         for ( k = j, a = md - a; k < m; k++ )
                   3213:           if ( pivot[k] )
                   3214:             t[k] = dmar(pivot[k],a,t[k],md);
                   3215:     }
                   3216:   }
                   3217:   *invmatp = invmat = (unsigned int **)almat(col,col);
                   3218:   for ( i = 0; i < col; i++ )
                   3219:     for ( j = 0, s = invmat[i], t = mat[i]; j < col; j++ )
                   3220:       s[j] = t[col+index[j]];
                   3221:   return 0;
1.27      noro     3222: }
                   3223:
                   3224: void Pgeninv_sf_swap(NODE arg,LIST *rp)
                   3225: {
1.76      noro     3226:   MAT m;
                   3227:   GFS **mat,**tmat;
                   3228:   Q *tvect;
                   3229:   GFS q;
                   3230:   int **wmat,**invmat;
                   3231:   int *index;
                   3232:   unsigned int t;
                   3233:   int i,j,row,col,status;
                   3234:   MAT mat1;
                   3235:   VECT vect1;
                   3236:   NODE node1,node2;
                   3237:
                   3238:   asir_assert(ARG0(arg),O_MAT,"geninv_sf_swap");
                   3239:   m = (MAT)ARG0(arg);
                   3240:   row = m->row; col = m->col; mat = (GFS **)m->body;
                   3241:   wmat = (int **)almat(row,col+row);
                   3242:   for ( i = 0; i < row; i++ ) {
                   3243:     bzero((char *)wmat[i],(col+row)*sizeof(int));
                   3244:     for ( j = 0; j < col; j++ )
                   3245:       if ( q = (GFS)mat[i][j] )
                   3246:         wmat[i][j] = FTOIF(CONT(q));
                   3247:     wmat[i][col+i] = _onesf();
                   3248:   }
                   3249:   status = gauss_elim_geninv_sf_swap(wmat,row,col,&invmat,&index);
                   3250:   if ( status > 0 )
                   3251:     *rp = 0;
                   3252:   else {
                   3253:     MKMAT(mat1,col,col);
                   3254:     for ( i = 0, tmat = (GFS **)mat1->body; i < col; i++ )
                   3255:       for ( j = 0; j < col; j++ )
                   3256:         if ( t = invmat[i][j] ) {
                   3257:           MKGFS(IFTOF(t),tmat[i][j]);
                   3258:         }
                   3259:     MKVECT(vect1,row);
                   3260:     for ( i = 0, tvect = (Q *)vect1->body; i < row; i++ )
                   3261:       STOQ(index[i],tvect[i]);
                   3262:      MKNODE(node2,vect1,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
                   3263:   }
1.27      noro     3264: }
                   3265:
                   3266: int gauss_elim_geninv_sf_swap(int **mat,int row,int col,
1.76      noro     3267:   int ***invmatp,int **indexp)
1.27      noro     3268: {
1.76      noro     3269:   int i,j,k,inv,a,n,m,u;
                   3270:   int *t,*pivot,*s;
                   3271:   int *index;
                   3272:   int **invmat;
                   3273:
                   3274:   n = col; m = row+col;
                   3275:   *indexp = index = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   3276:   for ( i = 0; i < row; i++ )
                   3277:     index[i] = i;
                   3278:   for ( j = 0; j < n; j++ ) {
                   3279:     for ( i = j; i < row && !mat[i][j]; i++ );
                   3280:     if ( i == row ) {
                   3281:       *indexp = 0; *invmatp = 0; return 1;
                   3282:     }
                   3283:     if ( i != j ) {
                   3284:       t = mat[i]; mat[i] = mat[j]; mat[j] = t;
                   3285:       k = index[i]; index[i] = index[j]; index[j] = k;
                   3286:     }
                   3287:     pivot = mat[j];
                   3288:     inv = _invsf(pivot[j]);
                   3289:     for ( k = j; k < m; k++ )
                   3290:       if ( pivot[k] )
                   3291:         pivot[k] = _mulsf(pivot[k],inv);
                   3292:     for ( i = j+1; i < row; i++ ) {
                   3293:       t = mat[i];
                   3294:       if ( a = t[j] )
                   3295:         for ( k = j, a = _chsgnsf(a); k < m; k++ )
                   3296:           if ( pivot[k] ) {
                   3297:             u = _mulsf(pivot[k],a);
                   3298:             t[k] = _addsf(u,t[k]);
                   3299:           }
                   3300:     }
                   3301:   }
                   3302:   for ( j = n-1; j >= 0; j-- ) {
                   3303:     pivot = mat[j];
                   3304:     for ( i = j-1; i >= 0; i-- ) {
                   3305:       t = mat[i];
                   3306:       if ( a = t[j] )
                   3307:         for ( k = j, a = _chsgnsf(a); k < m; k++ )
                   3308:           if ( pivot[k] ) {
                   3309:             u = _mulsf(pivot[k],a);
                   3310:             t[k] = _addsf(u,t[k]);
                   3311:           }
                   3312:     }
                   3313:   }
                   3314:   *invmatp = invmat = (int **)almat(col,col);
                   3315:   for ( i = 0; i < col; i++ )
                   3316:     for ( j = 0, s = invmat[i], t = mat[i]; j < col; j++ )
                   3317:       s[j] = t[col+index[j]];
                   3318:   return 0;
1.1       noro     3319: }
                   3320:
                   3321: void _addn(N,N,N);
                   3322: int _subn(N,N,N);
                   3323: void _muln(N,N,N);
                   3324:
1.24      noro     3325: void inner_product_int(Q *a,Q *b,int n,Q *r)
1.1       noro     3326: {
1.76      noro     3327:   int la,lb,i;
                   3328:   int sgn,sgn1;
                   3329:   N wm,wma,sum,t;
                   3330:
                   3331:   for ( la = lb = 0, i = 0; i < n; i++ ) {
                   3332:     if ( a[i] )
                   3333:       if ( DN(a[i]) )
                   3334:         error("inner_product_int : invalid argument");
                   3335:       else
                   3336:         la = MAX(PL(NM(a[i])),la);
                   3337:     if ( b[i] )
                   3338:       if ( DN(b[i]) )
                   3339:         error("inner_product_int : invalid argument");
                   3340:       else
                   3341:         lb = MAX(PL(NM(b[i])),lb);
                   3342:   }
                   3343:   sgn = 0;
                   3344:   sum= NALLOC(la+lb+2);
                   3345:   bzero((char *)sum,(la+lb+3)*sizeof(unsigned int));
                   3346:   wm = NALLOC(la+lb+2);
                   3347:   wma = NALLOC(la+lb+2);
                   3348:   for ( i = 0; i < n; i++ ) {
                   3349:     if ( !a[i] || !b[i] )
                   3350:       continue;
                   3351:     _muln(NM(a[i]),NM(b[i]),wm);
                   3352:     sgn1 = SGN(a[i])*SGN(b[i]);
                   3353:     if ( !sgn ) {
                   3354:       sgn = sgn1;
                   3355:       t = wm; wm = sum; sum = t;
                   3356:     } else if ( sgn == sgn1 ) {
                   3357:       _addn(sum,wm,wma);
                   3358:       if ( !PL(wma) )
                   3359:         sgn = 0;
                   3360:       t = wma; wma = sum; sum = t;
                   3361:     } else {
                   3362:       /* sgn*sum+sgn1*wm = sgn*(sum-wm) */
                   3363:       sgn *= _subn(sum,wm,wma);
                   3364:       t = wma; wma = sum; sum = t;
                   3365:     }
                   3366:   }
                   3367:   GCFREE(wm);
                   3368:   GCFREE(wma);
                   3369:   if ( !sgn ) {
                   3370:     GCFREE(sum);
                   3371:     *r = 0;
                   3372:   } else
                   3373:     NTOQ(sum,sgn,*r);
1.1       noro     3374: }
                   3375:
1.3       noro     3376: /* (k,l) element of a*b where a: .x n matrix, b: n x . integer matrix */
                   3377:
1.24      noro     3378: void inner_product_mat_int_mod(Q **a,int **b,int n,int k,int l,Q *r)
1.3       noro     3379: {
1.76      noro     3380:   int la,lb,i;
                   3381:   int sgn,sgn1;
                   3382:   N wm,wma,sum,t;
                   3383:   Q aki;
                   3384:   int bil,bilsgn;
                   3385:   struct oN tn;
                   3386:
                   3387:   for ( la = 0, i = 0; i < n; i++ ) {
                   3388:     if ( aki = a[k][i] )
                   3389:       if ( DN(aki) )
                   3390:         error("inner_product_int : invalid argument");
                   3391:       else
                   3392:         la = MAX(PL(NM(aki)),la);
                   3393:   }
                   3394:   lb = 1;
                   3395:   sgn = 0;
                   3396:   sum= NALLOC(la+lb+2);
                   3397:   bzero((char *)sum,(la+lb+3)*sizeof(unsigned int));
                   3398:   wm = NALLOC(la+lb+2);
                   3399:   wma = NALLOC(la+lb+2);
                   3400:   for ( i = 0; i < n; i++ ) {
                   3401:     if ( !(aki = a[k][i]) || !(bil = b[i][l]) )
                   3402:       continue;
                   3403:     tn.p = 1;
                   3404:     if ( bil > 0 ) {
                   3405:       tn.b[0] = bil; bilsgn = 1;
                   3406:     } else {
                   3407:       tn.b[0] = -bil; bilsgn = -1;
                   3408:     }
                   3409:     _muln(NM(aki),&tn,wm);
                   3410:     sgn1 = SGN(aki)*bilsgn;
                   3411:     if ( !sgn ) {
                   3412:       sgn = sgn1;
                   3413:       t = wm; wm = sum; sum = t;
                   3414:     } else if ( sgn == sgn1 ) {
                   3415:       _addn(sum,wm,wma);
                   3416:       if ( !PL(wma) )
                   3417:         sgn = 0;
                   3418:       t = wma; wma = sum; sum = t;
                   3419:     } else {
                   3420:       /* sgn*sum+sgn1*wm = sgn*(sum-wm) */
                   3421:       sgn *= _subn(sum,wm,wma);
                   3422:       t = wma; wma = sum; sum = t;
                   3423:     }
                   3424:   }
                   3425:   GCFREE(wm);
                   3426:   GCFREE(wma);
                   3427:   if ( !sgn ) {
                   3428:     GCFREE(sum);
                   3429:     *r = 0;
                   3430:   } else
                   3431:     NTOQ(sum,sgn,*r);
1.3       noro     3432: }
                   3433:
1.24      noro     3434: void Pmul_mat_vect_int(NODE arg,VECT *rp)
1.1       noro     3435: {
1.76      noro     3436:   MAT mat;
                   3437:   VECT vect,r;
                   3438:   int row,col,i;
                   3439:
                   3440:   mat = (MAT)ARG0(arg);
                   3441:   vect = (VECT)ARG1(arg);
                   3442:   row = mat->row;
                   3443:   col = mat->col;
                   3444:   MKVECT(r,row);
                   3445:   for ( i = 0; i < row; i++ ) {
                   3446:     inner_product_int((Q *)mat->body[i],(Q *)vect->body,col,(Q *)&r->body[i]);
                   3447:   }
                   3448:   *rp = r;
1.1       noro     3449: }
                   3450:
1.24      noro     3451: void Pnbpoly_up2(NODE arg,GF2N *rp)
1.1       noro     3452: {
1.76      noro     3453:   int m,type,ret;
                   3454:   UP2 r;
1.1       noro     3455:
1.76      noro     3456:   m = QTOS((Q)ARG0(arg));
                   3457:   type = QTOS((Q)ARG1(arg));
                   3458:   ret = generate_ONB_polynomial(&r,m,type);
                   3459:   if ( ret == 0 )
                   3460:     MKGF2N(r,*rp);
                   3461:   else
                   3462:     *rp = 0;
1.1       noro     3463: }
                   3464:
1.24      noro     3465: void Px962_irredpoly_up2(NODE arg,GF2N *rp)
1.1       noro     3466: {
1.76      noro     3467:   int m,ret,w;
                   3468:   GF2N prev;
                   3469:   UP2 r;
                   3470:
                   3471:   m = QTOS((Q)ARG0(arg));
                   3472:   prev = (GF2N)ARG1(arg);
                   3473:   if ( !prev ) {
                   3474:     w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
                   3475:     bzero((char *)r->b,w*sizeof(unsigned int));
                   3476:   } else {
                   3477:     r = prev->body;
                   3478:     if ( degup2(r) != m ) {
                   3479:       w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
                   3480:       bzero((char *)r->b,w*sizeof(unsigned int));
                   3481:     }
                   3482:   }
                   3483:   ret = _generate_irreducible_polynomial(r,m);
                   3484:   if ( ret == 0 )
                   3485:     MKGF2N(r,*rp);
                   3486:   else
                   3487:     *rp = 0;
1.1       noro     3488: }
                   3489:
1.24      noro     3490: void Pirredpoly_up2(NODE arg,GF2N *rp)
1.1       noro     3491: {
1.76      noro     3492:   int m,ret,w;
                   3493:   GF2N prev;
                   3494:   UP2 r;
                   3495:
                   3496:   m = QTOS((Q)ARG0(arg));
                   3497:   prev = (GF2N)ARG1(arg);
                   3498:   if ( !prev ) {
                   3499:     w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
                   3500:     bzero((char *)r->b,w*sizeof(unsigned int));
                   3501:   } else {
                   3502:     r = prev->body;
                   3503:     if ( degup2(r) != m ) {
                   3504:       w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
                   3505:       bzero((char *)r->b,w*sizeof(unsigned int));
                   3506:     }
                   3507:   }
                   3508:   ret = _generate_good_irreducible_polynomial(r,m);
                   3509:   if ( ret == 0 )
                   3510:     MKGF2N(r,*rp);
                   3511:   else
                   3512:     *rp = 0;
1.1       noro     3513: }
                   3514:
1.26      noro     3515: void Pmat_swap_row_destructive(NODE arg, MAT *m)
                   3516: {
1.76      noro     3517:   int i1,i2;
                   3518:   pointer *t;
                   3519:   MAT mat;
                   3520:
                   3521:   asir_assert(ARG0(arg),O_MAT,"mat_swap_row_destructive");
                   3522:   asir_assert(ARG1(arg),O_N,"mat_swap_row_destructive");
                   3523:   asir_assert(ARG2(arg),O_N,"mat_swap_row_destructive");
                   3524:   mat = (MAT)ARG0(arg);
                   3525:   i1 = QTOS((Q)ARG1(arg));
                   3526:   i2 = QTOS((Q)ARG2(arg));
                   3527:   if ( i1 < 0 || i2 < 0 || i1 >= mat->row || i2 >= mat->row )
                   3528:     error("mat_swap_row_destructive : Out of range");
                   3529:   t = mat->body[i1];
                   3530:   mat->body[i1] = mat->body[i2];
                   3531:   mat->body[i2] = t;
                   3532:   *m = mat;
1.26      noro     3533: }
                   3534:
                   3535: void Pmat_swap_col_destructive(NODE arg, MAT *m)
                   3536: {
1.76      noro     3537:   int j1,j2,i,n;
                   3538:   pointer *mi;
                   3539:   pointer t;
                   3540:   MAT mat;
                   3541:
                   3542:   asir_assert(ARG0(arg),O_MAT,"mat_swap_col_destructive");
                   3543:   asir_assert(ARG1(arg),O_N,"mat_swap_col_destructive");
                   3544:   asir_assert(ARG2(arg),O_N,"mat_swap_col_destructive");
                   3545:   mat = (MAT)ARG0(arg);
                   3546:   j1 = QTOS((Q)ARG1(arg));
                   3547:   j2 = QTOS((Q)ARG2(arg));
                   3548:   if ( j1 < 0 || j2 < 0 || j1 >= mat->col || j2 >= mat->col )
                   3549:     error("mat_swap_col_destructive : Out of range");
                   3550:   n = mat->row;
                   3551:   for ( i = 0; i < n; i++ ) {
                   3552:     mi = mat->body[i];
                   3553:     t = mi[j1]; mi[j1] = mi[j2]; mi[j2] = t;
                   3554:   }
                   3555:   *m = mat;
1.26      noro     3556: }
1.1       noro     3557: /*
                   3558:  * f = type 'type' normal polynomial of degree m if exists
                   3559:  * IEEE P1363 A.7.2
                   3560:  *
                   3561:  * return value : 0  --- exists
                   3562:  *                1  --- does not exist
                   3563:  *                -1 --- failure (memory allocation error)
                   3564:  */
                   3565:
                   3566: int generate_ONB_polynomial(UP2 *rp,int m,int type)
                   3567: {
1.76      noro     3568:   int i,r;
                   3569:   int w;
                   3570:   UP2 f,f0,f1,f2,t;
                   3571:
                   3572:   w = (m>>5)+1;
                   3573:   switch ( type ) {
                   3574:     case 1:
                   3575:       if ( !TypeT_NB_check(m,1) ) return 1;
                   3576:       NEWUP2(f,w); *rp = f; f->w = w;
                   3577:       /* set all the bits */
                   3578:       for ( i = 0; i < w; i++ )
                   3579:         f->b[i] = 0xffffffff;
                   3580:       /* mask the top word if necessary */
                   3581:       if ( r = (m+1)&31 )
                   3582:         f->b[w-1] &= (1<<r)-1;
                   3583:       return 0;
                   3584:       break;
                   3585:     case 2:
                   3586:       if ( !TypeT_NB_check(m,2) ) return 1;
                   3587:       NEWUP2(f,w); *rp = f;
                   3588:       W_NEWUP2(f0,w);
                   3589:       W_NEWUP2(f1,w);
                   3590:       W_NEWUP2(f2,w);
                   3591:
                   3592:       /* recursion for genrating Type II normal polynomial */
                   3593:
                   3594:       /* f0 = 1, f1 = t+1 */
                   3595:       f0->w = 1; f0->b[0] = 1;
                   3596:       f1->w = 1; f1->b[0] = 3;
                   3597:       for ( i = 2; i <= m; i++ ) {
                   3598:         /* f2 = t*f1+f0 */
                   3599:         _bshiftup2(f1,-1,f2);
                   3600:         _addup2_destructive(f2,f0);
                   3601:         /* cyclic change of the variables */
                   3602:         t = f0; f0 = f1; f1 = f2; f2 = t;
                   3603:       }
                   3604:       _copyup2(f1,f);
                   3605:       return 0;
                   3606:       break;
                   3607:     default:
                   3608:       return -1;
                   3609:       break;
                   3610:     }
1.1       noro     3611: }
                   3612:
                   3613: /*
                   3614:  * f = an irreducible trinomial or pentanomial of degree d 'after' f
                   3615:  * return value : 0  --- exists
                   3616:  *                1  --- does not exist (exhaustion)
                   3617:  */
                   3618:
                   3619: int _generate_irreducible_polynomial(UP2 f,int d)
                   3620: {
1.76      noro     3621:   int ret,i,j,k,nz,i0,j0,k0;
                   3622:   int w;
                   3623:   unsigned int *fd;
                   3624:
                   3625:   /*
                   3626:    * if f = x^d+x^i+1 then i0 <- i, j0 <- 0, k0 <-0.
                   3627:    * if f = x^d+x^k+x^j+x^i+1 (k>j>i) then i0 <- i, j0 <- j, k0 <-k.
                   3628:    * otherwise i0,j0,k0 is set to 0.
                   3629:    */
                   3630:
                   3631:   fd = f->b;
                   3632:   w = (d>>5)+1;
                   3633:   if ( f->w && (d==degup2(f)) ) {
                   3634:     for ( nz = 0, i = d; i >= 0; i-- )
                   3635:       if ( fd[i>>5]&(1<<(i&31)) ) nz++;
                   3636:     switch ( nz ) {
                   3637:       case 3:
                   3638:         for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
                   3639:         /* reset i0-th bit */
                   3640:         fd[i0>>5] &= ~(1<<(i0&31));
                   3641:         j0 = k0 = 0;
                   3642:         break;
                   3643:       case 5:
                   3644:         for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
                   3645:         /* reset i0-th bit */
                   3646:         fd[i0>>5] &= ~(1<<(i0&31));
                   3647:         for ( j0 = i0+1; !(fd[j0>>5]&(1<<(j0&31))) ; j0++ );
                   3648:         /* reset j0-th bit */
                   3649:         fd[j0>>5] &= ~(1<<(j0&31));
                   3650:         for ( k0 = j0+1; !(fd[k0>>5]&(1<<(k0&31))) ; k0++ );
                   3651:         /* reset k0-th bit */
                   3652:         fd[k0>>5] &= ~(1<<(k0&31));
                   3653:         break;
                   3654:       default:
                   3655:         f->w = 0; break;
                   3656:     }
                   3657:   } else
                   3658:     f->w = 0;
                   3659:
                   3660:   if ( !f->w ) {
                   3661:     fd = f->b;
                   3662:     f->w = w; fd[0] |= 1; fd[d>>5] |= (1<<(d&31));
                   3663:     i0 = j0 = k0 = 0;
                   3664:   }
                   3665:   /* if j0 > 0 then f is already a pentanomial */
                   3666:   if ( j0 > 0 ) goto PENTA;
1.1       noro     3667:
1.76      noro     3668:   /* searching for an irreducible trinomial */
                   3669:
                   3670:   for ( i = 1; 2*i <= d; i++ ) {
                   3671:     /* skip the polynomials 'before' f */
                   3672:     if ( i < i0 ) continue;
                   3673:     if ( i == i0 ) { i0 = 0; continue; }
                   3674:     /* set i-th bit */
                   3675:     fd[i>>5] |= (1<<(i&31));
                   3676:     ret = irredcheck_dddup2(f);
                   3677:     if ( ret == 1 ) return 0;
                   3678:     /* reset i-th bit */
                   3679:     fd[i>>5] &= ~(1<<(i&31));
                   3680:   }
                   3681:
                   3682:   /* searching for an irreducible pentanomial */
1.1       noro     3683: PENTA:
1.76      noro     3684:   for ( i = 1; i < d; i++ ) {
                   3685:     /* skip the polynomials 'before' f */
                   3686:     if ( i < i0 ) continue;
                   3687:     if ( i == i0 ) i0 = 0;
                   3688:     /* set i-th bit */
                   3689:     fd[i>>5] |= (1<<(i&31));
                   3690:     for ( j = i+1; j < d; j++ ) {
                   3691:       /* skip the polynomials 'before' f */
                   3692:       if ( j < j0 ) continue;
                   3693:       if ( j == j0 ) j0 = 0;
                   3694:       /* set j-th bit */
                   3695:       fd[j>>5] |= (1<<(j&31));
                   3696:       for ( k = j+1; k < d; k++ ) {
                   3697:         /* skip the polynomials 'before' f */
                   3698:         if ( k < k0 ) continue;
                   3699:         else if ( k == k0 ) { k0 = 0; continue; }
                   3700:         /* set k-th bit */
                   3701:         fd[k>>5] |= (1<<(k&31));
                   3702:         ret = irredcheck_dddup2(f);
                   3703:         if ( ret == 1 ) return 0;
                   3704:         /* reset k-th bit */
                   3705:         fd[k>>5] &= ~(1<<(k&31));
                   3706:       }
                   3707:       /* reset j-th bit */
                   3708:       fd[j>>5] &= ~(1<<(j&31));
                   3709:     }
                   3710:     /* reset i-th bit */
                   3711:     fd[i>>5] &= ~(1<<(i&31));
                   3712:   }
                   3713:   /* exhausted */
                   3714:   return 1;
1.1       noro     3715: }
                   3716:
                   3717: /*
                   3718:  * f = an irreducible trinomial or pentanomial of degree d 'after' f
                   3719:  *
                   3720:  * searching strategy:
                   3721:  *   trinomial x^d+x^i+1:
                   3722:  *         i is as small as possible.
                   3723:  *   trinomial x^d+x^i+x^j+x^k+1:
                   3724:  *         i is as small as possible.
                   3725:  *         For such i, j is as small as possible.
                   3726:  *         For such i and j, 'k' is as small as possible.
                   3727:  *
                   3728:  * return value : 0  --- exists
                   3729:  *                1  --- does not exist (exhaustion)
                   3730:  */
                   3731:
                   3732: int _generate_good_irreducible_polynomial(UP2 f,int d)
                   3733: {
1.76      noro     3734:   int ret,i,j,k,nz,i0,j0,k0;
                   3735:   int w;
                   3736:   unsigned int *fd;
                   3737:
                   3738:   /*
                   3739:    * if f = x^d+x^i+1 then i0 <- i, j0 <- 0, k0 <-0.
                   3740:    * if f = x^d+x^k+x^j+x^i+1 (k>j>i) then i0 <- i, j0 <- j, k0 <-k.
                   3741:    * otherwise i0,j0,k0 is set to 0.
                   3742:    */
                   3743:
                   3744:   fd = f->b;
                   3745:   w = (d>>5)+1;
                   3746:   if ( f->w && (d==degup2(f)) ) {
                   3747:     for ( nz = 0, i = d; i >= 0; i-- )
                   3748:       if ( fd[i>>5]&(1<<(i&31)) ) nz++;
                   3749:     switch ( nz ) {
                   3750:       case 3:
                   3751:         for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
                   3752:         /* reset i0-th bit */
                   3753:         fd[i0>>5] &= ~(1<<(i0&31));
                   3754:         j0 = k0 = 0;
                   3755:         break;
                   3756:       case 5:
                   3757:         for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
                   3758:         /* reset i0-th bit */
                   3759:         fd[i0>>5] &= ~(1<<(i0&31));
                   3760:         for ( j0 = i0+1; !(fd[j0>>5]&(1<<(j0&31))) ; j0++ );
                   3761:         /* reset j0-th bit */
                   3762:         fd[j0>>5] &= ~(1<<(j0&31));
                   3763:         for ( k0 = j0+1; !(fd[k0>>5]&(1<<(k0&31))) ; k0++ );
                   3764:         /* reset k0-th bit */
                   3765:         fd[k0>>5] &= ~(1<<(k0&31));
                   3766:         break;
                   3767:       default:
                   3768:         f->w = 0; break;
                   3769:     }
                   3770:   } else
                   3771:     f->w = 0;
                   3772:
                   3773:   if ( !f->w ) {
                   3774:     fd = f->b;
                   3775:     f->w = w; fd[0] |= 1; fd[d>>5] |= (1<<(d&31));
                   3776:     i0 = j0 = k0 = 0;
                   3777:   }
                   3778:   /* if j0 > 0 then f is already a pentanomial */
                   3779:   if ( j0 > 0 ) goto PENTA;
1.1       noro     3780:
1.76      noro     3781:   /* searching for an irreducible trinomial */
                   3782:
                   3783:   for ( i = 1; 2*i <= d; i++ ) {
                   3784:     /* skip the polynomials 'before' f */
                   3785:     if ( i < i0 ) continue;
                   3786:     if ( i == i0 ) { i0 = 0; continue; }
                   3787:     /* set i-th bit */
                   3788:     fd[i>>5] |= (1<<(i&31));
                   3789:     ret = irredcheck_dddup2(f);
                   3790:     if ( ret == 1 ) return 0;
                   3791:     /* reset i-th bit */
                   3792:     fd[i>>5] &= ~(1<<(i&31));
                   3793:   }
                   3794:
                   3795:   /* searching for an irreducible pentanomial */
1.1       noro     3796: PENTA:
1.76      noro     3797:   for ( i = 3; i < d; i++ ) {
                   3798:     /* skip the polynomials 'before' f */
                   3799:     if ( i < i0 ) continue;
                   3800:     if ( i == i0 ) i0 = 0;
                   3801:     /* set i-th bit */
                   3802:     fd[i>>5] |= (1<<(i&31));
                   3803:      for ( j = 2; j < i; j++ ) {
                   3804:       /* skip the polynomials 'before' f */
                   3805:       if ( j < j0 ) continue;
                   3806:       if ( j == j0 ) j0 = 0;
                   3807:       /* set j-th bit */
                   3808:       fd[j>>5] |= (1<<(j&31));
                   3809:        for ( k = 1; k < j; k++ ) {
                   3810:         /* skip the polynomials 'before' f */
                   3811:         if ( k < k0 ) continue;
                   3812:         else if ( k == k0 ) { k0 = 0; continue; }
                   3813:         /* set k-th bit */
                   3814:         fd[k>>5] |= (1<<(k&31));
                   3815:         ret = irredcheck_dddup2(f);
                   3816:         if ( ret == 1 ) return 0;
                   3817:         /* reset k-th bit */
                   3818:         fd[k>>5] &= ~(1<<(k&31));
                   3819:       }
                   3820:       /* reset j-th bit */
                   3821:       fd[j>>5] &= ~(1<<(j&31));
                   3822:     }
                   3823:     /* reset i-th bit */
                   3824:     fd[i>>5] &= ~(1<<(i&31));
                   3825:   }
                   3826:   /* exhausted */
                   3827:   return 1;
1.3       noro     3828: }
                   3829:
1.24      noro     3830: void printqmat(Q **mat,int row,int col)
1.3       noro     3831: {
1.76      noro     3832:   int i,j;
1.3       noro     3833:
1.76      noro     3834:   for ( i = 0; i < row; i++ ) {
                   3835:     for ( j = 0; j < col; j++ ) {
                   3836:       printnum((Num)mat[i][j]); printf(" ");
                   3837:     }
                   3838:     printf("\n");
                   3839:   }
1.3       noro     3840: }
                   3841:
1.24      noro     3842: void printimat(int **mat,int row,int col)
1.3       noro     3843: {
1.76      noro     3844:   int i,j;
1.3       noro     3845:
1.76      noro     3846:   for ( i = 0; i < row; i++ ) {
                   3847:     for ( j = 0; j < col; j++ ) {
                   3848:       printf("%d ",mat[i][j]);
                   3849:     }
                   3850:     printf("\n");
                   3851:   }
1.36      noro     3852: }
                   3853:
                   3854: void Pnd_det(NODE arg,P *rp)
                   3855: {
1.76      noro     3856:   if ( argc(arg) == 1 )
                   3857:     nd_det(0,ARG0(arg),rp);
                   3858:   else
                   3859:     nd_det(QTOS((Q)ARG1(arg)),ARG0(arg),rp);
1.1       noro     3860: }
1.59      ohara    3861:
1.62      ohara    3862: void Pmat_col(NODE arg,VECT *rp)
1.59      ohara    3863: {
1.76      noro     3864:   int i,j,n;
                   3865:   MAT mat;
                   3866:   VECT vect;
                   3867:
                   3868:   asir_assert(ARG0(arg),O_MAT,"mat_col");
                   3869:   asir_assert(ARG1(arg),O_N,"mat_col");
                   3870:   mat = (MAT)ARG0(arg);
                   3871:   j = QTOS((Q)ARG1(arg));
                   3872:   if ( j < 0 || j >= mat->col) {
                   3873:     error("mat_col : Out of range");
                   3874:   }
                   3875:   n = mat->row;
                   3876:   MKVECT(vect,n);
                   3877:   for(i=0; i<n; i++) {
                   3878:     BDY(vect)[i] = BDY(mat)[i][j];
                   3879:   }
                   3880:   *rp = vect;
1.59      ohara    3881: }
1.71      noro     3882:
                   3883: NODE triangleq(NODE e)
                   3884: {
                   3885:   int n,i,k;
                   3886:   V v;
                   3887:   VL vl;
                   3888:   P *p;
                   3889:   NODE r,r1;
                   3890:
                   3891:   n = length(e);
                   3892:   p = (P *)MALLOC(n*sizeof(P));
                   3893:   for ( i = 0; i < n; i++, e = NEXT(e) ) p[i] = (P)BDY(e);
                   3894:   i = 0;
                   3895:   while ( 1 ) {
                   3896:     for ( ; i < n && !p[i]; i++ );
                   3897:     if ( i == n ) break;
                   3898:     if ( OID(p[i]) == O_N ) return 0;
                   3899:     v = p[i]->v;
                   3900:     for ( k = i+1; k < n; k++ )
                   3901:       if ( p[k] ) {
                   3902:         if ( OID(p[k]) == O_N ) return 0;
                   3903:         if ( p[k]->v == v ) p[k] = 0;
                   3904:       }
                   3905:     i++;
                   3906:   }
                   3907:   for ( r = 0, i = 0; i < n; i++ ) {
                   3908:     if ( p[i] ) {
                   3909:       MKNODE(r1,p[i],r); r = r1;
                   3910:     }
                   3911:   }
                   3912:   return r;
                   3913: }
                   3914:
                   3915: void Ptriangleq(NODE arg,LIST *rp)
                   3916: {
                   3917:   NODE ret;
                   3918:
                   3919:   asir_assert(ARG0(arg),O_LIST,"sparseleq");
                   3920:   ret = triangleq(BDY((LIST)ARG0(arg)));
                   3921:   MKLIST(*rp,ret);
                   3922: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>