[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.47

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.47    ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/builtin/array.c,v 1.46 2005/02/08 18:06:05 saito 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.38      noro       55: #define F4_INTRAT_PERIOD 8
                     56:
1.4       noro       57: #if 0
1.1       noro       58: #undef DMAR
                     59: #define DMAR(a1,a2,a3,d,r) (r)=dmar(a1,a2,a3,d);
1.4       noro       60: #endif
1.1       noro       61:
1.11      noro       62: extern int DP_Print; /* XXX */
1.1       noro       63:
1.24      noro       64:
1.1       noro       65: void Pnewvect(), Pnewmat(), Psepvect(), Psize(), Pdet(), Pleqm(), Pleqm1(), Pgeninvm();
1.23      noro       66: void Pinvmat();
1.9       noro       67: void Pnewbytearray();
1.1       noro       68:
1.25      noro       69: void Pgeneric_gauss_elim();
1.1       noro       70: void Pgeneric_gauss_elim_mod();
                     71:
                     72: void Pmat_to_gfmmat(),Plu_gfmmat(),Psolve_by_lu_gfmmat();
1.33      noro       73: void Pgeninvm_swap(), Premainder(), Psremainder(), Pvtol(), Pltov();
1.27      noro       74: void Pgeninv_sf_swap();
1.1       noro       75: void sepvect();
                     76: void Pmulmat_gf2n();
                     77: void Pbconvmat_gf2n();
                     78: void Pmul_vect_mat_gf2n();
                     79: void PNBmul_gf2n();
                     80: void Pmul_mat_vect_int();
                     81: void Psepmat_destructive();
                     82: void Px962_irredpoly_up2();
                     83: void Pirredpoly_up2();
                     84: void Pnbpoly_up2();
                     85: void Pqsort();
1.14      noro       86: void Pexponent_vector();
1.26      noro       87: void Pmat_swap_row_destructive();
                     88: void Pmat_swap_col_destructive();
1.28      saito      89: void Pvect();
                     90: void Pmat();
1.29      saito      91: void Pmatc();
1.36      noro       92: void Pnd_det();
1.1       noro       93:
                     94: struct ftab array_tab[] = {
                     95:        {"solve_by_lu_gfmmat",Psolve_by_lu_gfmmat,4},
                     96:        {"lu_gfmmat",Plu_gfmmat,2},
                     97:        {"mat_to_gfmmat",Pmat_to_gfmmat,2},
1.25      noro       98:        {"generic_gauss_elim",Pgeneric_gauss_elim,1},
1.1       noro       99:        {"generic_gauss_elim_mod",Pgeneric_gauss_elim_mod,2},
                    100:        {"newvect",Pnewvect,-2},
1.28      saito     101:        {"vect",Pvect,-99999999},
1.14      noro      102:        {"vector",Pnewvect,-2},
                    103:        {"exponent_vector",Pexponent_vector,-99999999},
1.1       noro      104:        {"newmat",Pnewmat,-3},
1.14      noro      105:        {"matrix",Pnewmat,-3},
1.28      saito     106:        {"mat",Pmat,-99999999},
1.29      saito     107:        {"matr",Pmat,-99999999},
                    108:        {"matc",Pmatc,-99999999},
1.9       noro      109:        {"newbytearray",Pnewbytearray,-2},
1.1       noro      110:        {"sepmat_destructive",Psepmat_destructive,2},
                    111:        {"sepvect",Psepvect,2},
                    112:        {"qsort",Pqsort,-2},
                    113:        {"vtol",Pvtol,1},
1.33      noro      114:        {"ltov",Pltov,1},
1.1       noro      115:        {"size",Psize,1},
                    116:        {"det",Pdet,-2},
1.37      noro      117:        {"nd_det",Pnd_det,-2},
1.23      noro      118:        {"invmat",Pinvmat,-2},
1.1       noro      119:        {"leqm",Pleqm,2},
                    120:        {"leqm1",Pleqm1,2},
                    121:        {"geninvm",Pgeninvm,2},
                    122:        {"geninvm_swap",Pgeninvm_swap,2},
1.27      noro      123:        {"geninv_sf_swap",Pgeninv_sf_swap,1},
1.1       noro      124:        {"remainder",Premainder,2},
                    125:        {"sremainder",Psremainder,2},
                    126:        {"mulmat_gf2n",Pmulmat_gf2n,1},
                    127:        {"bconvmat_gf2n",Pbconvmat_gf2n,-4},
                    128:        {"mul_vect_mat_gf2n",Pmul_vect_mat_gf2n,2},
                    129:        {"mul_mat_vect_int",Pmul_mat_vect_int,2},
                    130:        {"nbmul_gf2n",PNBmul_gf2n,3},
                    131:        {"x962_irredpoly_up2",Px962_irredpoly_up2,2},
                    132:        {"irredpoly_up2",Pirredpoly_up2,2},
                    133:        {"nbpoly_up2",Pnbpoly_up2,2},
1.26      noro      134:        {"mat_swap_row_destructive",Pmat_swap_row_destructive,3},
                    135:        {"mat_swap_col_destructive",Pmat_swap_col_destructive,3},
1.1       noro      136:        {0,0,0},
                    137: };
                    138:
1.24      noro      139: int comp_obj(Obj *a,Obj *b)
1.1       noro      140: {
                    141:        return arf_comp(CO,*a,*b);
                    142: }
                    143:
                    144: static FUNC generic_comp_obj_func;
                    145: static NODE generic_comp_obj_arg;
                    146:
1.24      noro      147: int generic_comp_obj(Obj *a,Obj *b)
1.1       noro      148: {
                    149:        Q r;
                    150:
                    151:        BDY(generic_comp_obj_arg)=(pointer)(*a);
                    152:        BDY(NEXT(generic_comp_obj_arg))=(pointer)(*b);
                    153:        r = (Q)bevalf(generic_comp_obj_func,generic_comp_obj_arg);
                    154:        if ( !r )
                    155:                return 0;
                    156:        else
                    157:                return SGN(r)>0?1:-1;
                    158: }
                    159:
                    160:
1.46      saito     161: void Pqsort(NODE arg,LIST *rp)
1.1       noro      162: {
                    163:        VECT vect;
1.35      ohara     164:        NODE n,n1;
1.1       noro      165:        P p;
                    166:        V v;
1.34      noro      167:        FUNC func;
1.35      ohara     168:        int len,i;
                    169:        pointer *a;
                    170:        Obj t;
                    171:
                    172:        t = ARG0(arg);
                    173:     if (OID(t) == O_LIST) {
                    174:         n = (NODE)BDY((LIST)t);
                    175:         len = length(n);
                    176:         MKVECT(vect,len);
                    177:         for ( i = 0; i < len; i++, n = NEXT(n) ) {
                    178:             BDY(vect)[i] = BDY(n);
                    179:         }
                    180:
                    181:     }else if (OID(t) != O_VECT) {
                    182:         error("qsort : invalid argument");
                    183:     }else {
                    184:         vect = (VECT)t;
                    185:     }
1.1       noro      186:        if ( argc(arg) == 1 )
                    187:                qsort(BDY(vect),vect->len,sizeof(Obj),(int (*)(const void *,const void *))comp_obj);
                    188:        else {
                    189:                p = (P)ARG1(arg);
                    190:                if ( !p || OID(p)!=2 )
                    191:                        error("qsort : invalid argument");
                    192:                v = VR(p);
1.34      noro      193:                gen_searchf(NAME(v),&func);
                    194:                if ( !func ) {
                    195:                        if ( (int)v->attr != V_SR )
                    196:                                error("qsort : no such function");
                    197:                        func = (FUNC)v->priv;
                    198:                }
                    199:                generic_comp_obj_func = func;
1.1       noro      200:                MKNODE(n,0,0); MKNODE(generic_comp_obj_arg,0,n);
                    201:                qsort(BDY(vect),vect->len,sizeof(Obj),(int (*)(const void *,const void *))generic_comp_obj);
                    202:        }
1.35      ohara     203:     if (OID(t) == O_LIST) {
                    204:         a = BDY(vect);
                    205:         for ( i = len - 1, n = 0; i >= 0; i-- ) {
                    206:             MKNODE(n1,a[i],n); n = n1;
                    207:         }
1.46      saito     208:         MKLIST(*rp,n);
1.35      ohara     209:     }else {
1.46      saito     210:         *rp = (LIST)vect;
1.35      ohara     211:     }
1.1       noro      212: }
                    213:
1.24      noro      214: void PNBmul_gf2n(NODE arg,GF2N *rp)
1.1       noro      215: {
                    216:        GF2N a,b;
                    217:        GF2MAT mat;
                    218:        int n,w;
                    219:        unsigned int *ab,*bb;
                    220:        UP2 r;
                    221:
                    222:        a = (GF2N)ARG0(arg);
                    223:        b = (GF2N)ARG1(arg);
                    224:        mat = (GF2MAT)ARG2(arg);
                    225:        if ( !a || !b )
                    226:                *rp = 0;
                    227:        else {
                    228:                n = mat->row;
                    229:                w = (n+BSH-1)/BSH;
                    230:
                    231:                ab = (unsigned int *)ALLOCA(w*sizeof(unsigned int));
                    232:                bzero((char *)ab,w*sizeof(unsigned int));
                    233:                bcopy(a->body->b,ab,(a->body->w)*sizeof(unsigned int));
                    234:
                    235:                bb = (unsigned int *)ALLOCA(w*sizeof(unsigned int));
                    236:                bzero((char *)bb,w*sizeof(unsigned int));
                    237:                bcopy(b->body->b,bb,(b->body->w)*sizeof(unsigned int));
                    238:
                    239:                NEWUP2(r,w);
                    240:                bzero((char *)r->b,w*sizeof(unsigned int));
                    241:                mul_nb(mat,ab,bb,r->b);
                    242:                r->w = w;
                    243:                _adjup2(r);
                    244:                if ( !r->w )
                    245:                        *rp = 0;
                    246:                else
                    247:                        MKGF2N(r,*rp);
                    248:        }
                    249: }
                    250:
1.24      noro      251: void Pmul_vect_mat_gf2n(NODE arg,GF2N *rp)
1.1       noro      252: {
                    253:        GF2N a;
                    254:        GF2MAT mat;
                    255:        int n,w;
                    256:        unsigned int *b;
                    257:        UP2 r;
                    258:
                    259:        a = (GF2N)ARG0(arg);
                    260:        mat = (GF2MAT)ARG1(arg);
                    261:        if ( !a )
                    262:                *rp = 0;
                    263:        else {
                    264:                n = mat->row;
                    265:                w = (n+BSH-1)/BSH;
                    266:                b = (unsigned int *)ALLOCA(w*sizeof(unsigned int));
                    267:                bzero((char *)b,w*sizeof(unsigned int));
                    268:                bcopy(a->body->b,b,(a->body->w)*sizeof(unsigned int));
                    269:                NEWUP2(r,w);
                    270:                bzero((char *)r->b,w*sizeof(unsigned int));
                    271:                mulgf2vectmat(mat->row,b,mat->body,r->b);
                    272:                r->w = w;
                    273:                _adjup2(r);
                    274:                if ( !r->w )
                    275:                        *rp = 0;
                    276:                else {
                    277:                        MKGF2N(r,*rp);
                    278:                }
                    279:        }
                    280: }
                    281:
1.24      noro      282: void Pbconvmat_gf2n(NODE arg,LIST *rp)
1.1       noro      283: {
                    284:        P p0,p1;
                    285:        int to;
                    286:        GF2MAT p01,p10;
                    287:        GF2N root;
                    288:        NODE n0,n1;
                    289:
                    290:        p0 = (P)ARG0(arg);
                    291:        p1 = (P)ARG1(arg);
                    292:        to = ARG2(arg)?1:0;
                    293:        if ( argc(arg) == 4 ) {
                    294:                root = (GF2N)ARG3(arg);
                    295:                compute_change_of_basis_matrix_with_root(p0,p1,to,root,&p01,&p10);
                    296:        } else
                    297:                compute_change_of_basis_matrix(p0,p1,to,&p01,&p10);
                    298:        MKNODE(n1,p10,0); MKNODE(n0,p01,n1);
                    299:        MKLIST(*rp,n0);
                    300: }
                    301:
1.24      noro      302: void Pmulmat_gf2n(NODE arg,GF2MAT *rp)
1.1       noro      303: {
                    304:        GF2MAT m;
                    305:
                    306:        if ( !compute_multiplication_matrix((P)ARG0(arg),&m) )
                    307:                error("mulmat_gf2n : input is not a normal polynomial");
                    308:        *rp = m;
                    309: }
                    310:
1.24      noro      311: void Psepmat_destructive(NODE arg,LIST *rp)
1.1       noro      312: {
                    313:        MAT mat,mat1;
                    314:        int i,j,row,col;
                    315:        Q **a,**a1;
                    316:        Q ent;
                    317:        N nm,mod,rem,quo;
                    318:        int sgn;
                    319:        NODE n0,n1;
                    320:
                    321:        mat = (MAT)ARG0(arg); mod = NM((Q)ARG1(arg));
                    322:        row = mat->row; col = mat->col;
                    323:        MKMAT(mat1,row,col);
                    324:        a = (Q **)mat->body; a1 = (Q **)mat1->body;
                    325:        for ( i = 0; i < row; i++ )
                    326:                for ( j = 0; j < col; j++ ) {
                    327:                        ent = a[i][j];
                    328:                        if ( !ent )
                    329:                                continue;
                    330:                        nm = NM(ent);
                    331:                        sgn = SGN(ent);
                    332:                        divn(nm,mod,&quo,&rem);
                    333: /*                     if ( quo != nm && rem != nm ) */
                    334: /*                             GC_free(nm); */
                    335: /*                     GC_free(ent); */
                    336:                        NTOQ(rem,sgn,a[i][j]); NTOQ(quo,sgn,a1[i][j]);
                    337:                }
                    338:        MKNODE(n1,mat1,0); MKNODE(n0,mat,n1);
                    339:        MKLIST(*rp,n0);
                    340: }
                    341:
1.24      noro      342: void Psepvect(NODE arg,VECT *rp)
1.1       noro      343: {
                    344:        sepvect((VECT)ARG0(arg),QTOS((Q)ARG1(arg)),rp);
                    345: }
                    346:
1.24      noro      347: void sepvect(VECT v,int d,VECT *rp)
1.1       noro      348: {
                    349:        int i,j,k,n,q,q1,r;
                    350:        pointer *pv,*pw,*pu;
                    351:        VECT w,u;
                    352:
                    353:        n = v->len;
                    354:        if ( d > n )
                    355:                d = n;
                    356:        q = n/d; r = n%d; q1 = q+1;
                    357:        MKVECT(w,d); *rp = w;
                    358:        pv = BDY(v); pw = BDY(w); k = 0;
                    359:        for ( i = 0; i < r; i++ ) {
                    360:                MKVECT(u,q1); pw[i] = (pointer)u;
                    361:                for ( pu = BDY(u), j = 0; j < q1; j++, k++ )
                    362:                        pu[j] = pv[k];
                    363:        }
                    364:        for ( ; i < d; i++ ) {
                    365:                MKVECT(u,q); pw[i] = (pointer)u;
                    366:                for ( pu = BDY(u), j = 0; j < q; j++, k++ )
                    367:                        pu[j] = pv[k];
                    368:        }
                    369: }
                    370:
1.24      noro      371: void Pnewvect(NODE arg,VECT *rp)
1.1       noro      372: {
                    373:        int len,i,r;
                    374:        VECT vect;
                    375:        pointer *vb;
                    376:        LIST list;
                    377:        NODE tn;
                    378:
                    379:        asir_assert(ARG0(arg),O_N,"newvect");
                    380:        len = QTOS((Q)ARG0(arg));
1.5       noro      381:        if ( len < 0 )
1.1       noro      382:                error("newvect : invalid size");
                    383:        MKVECT(vect,len);
                    384:        if ( argc(arg) == 2 ) {
                    385:                list = (LIST)ARG1(arg);
                    386:                asir_assert(list,O_LIST,"newvect");
                    387:                for ( r = 0, tn = BDY(list); tn; r++, tn = NEXT(tn) );
                    388:                if ( r > len ) {
                    389:                        *rp = vect;
                    390:                        return;
                    391:                }
                    392:                for ( i = 0, tn = BDY(list), vb = BDY(vect); tn; i++, tn = NEXT(tn) )
                    393:                        vb[i] = (pointer)BDY(tn);
                    394:        }
                    395:        *rp = vect;
1.14      noro      396: }
                    397:
1.28      saito     398: void Pvect(NODE arg,VECT *rp) {
                    399:        int len,i,r;
                    400:        VECT vect;
                    401:        pointer *vb;
                    402:        NODE tn;
                    403:
                    404:        if ( !arg ) {
                    405:                *rp =0;
                    406:                return;
                    407:        }
                    408:
                    409:        for (len = 0, tn = arg; tn; tn = NEXT(tn), len++);
1.29      saito     410:        if ( len == 1 ) {
                    411:                if ( ARG0(arg) != 0 ) {
                    412:                        switch ( OID(ARG0(arg)) ) {
                    413:                                case O_VECT:
                    414:                                        *rp = ARG0(arg);
                    415:                                        return;
                    416:                                case O_LIST:
                    417:                                        for ( len = 0, tn = ARG0(arg); tn; tn = NEXT(tn), len++ );
                    418:                                        MKVECT(vect,len-1);
                    419:                                        for ( i = 0, tn = BDY((LIST)ARG0(arg)), vb =BDY(vect);
                    420:                                                        tn; i++, tn = NEXT(tn) )
                    421:                                                vb[i] = (pointer)BDY(tn);
                    422:                                        *rp=vect;
                    423:                                        return;
                    424:                        }
                    425:                }
                    426:        }
1.28      saito     427:        MKVECT(vect,len);
                    428:        for ( i = 0, tn = arg, vb = BDY(vect); tn; i++, tn = NEXT(tn) )
                    429:                vb[i] = (pointer)BDY(tn);
                    430:        *rp = vect;
                    431: }
                    432:
1.24      noro      433: void Pexponent_vector(NODE arg,DP *rp)
1.14      noro      434: {
                    435:        nodetod(arg,rp);
1.9       noro      436: }
                    437:
1.24      noro      438: void Pnewbytearray(NODE arg,BYTEARRAY *rp)
1.9       noro      439: {
                    440:        int len,i,r;
                    441:        BYTEARRAY array;
                    442:        unsigned char *vb;
1.10      noro      443:        char *str;
1.9       noro      444:        LIST list;
                    445:        NODE tn;
                    446:
                    447:        asir_assert(ARG0(arg),O_N,"newbytearray");
                    448:        len = QTOS((Q)ARG0(arg));
                    449:        if ( len < 0 )
                    450:                error("newbytearray : invalid size");
                    451:        MKBYTEARRAY(array,len);
                    452:        if ( argc(arg) == 2 ) {
1.10      noro      453:                if ( !ARG1(arg) )
                    454:                        error("newbytearray : invalid initialization");
                    455:                switch ( OID((Obj)ARG1(arg)) ) {
                    456:                        case O_LIST:
                    457:                                list = (LIST)ARG1(arg);
                    458:                                asir_assert(list,O_LIST,"newbytearray");
                    459:                                for ( r = 0, tn = BDY(list); tn; r++, tn = NEXT(tn) );
                    460:                                if ( r <= len ) {
                    461:                                        for ( i = 0, tn = BDY(list), vb = BDY(array); tn;
                    462:                                                i++, tn = NEXT(tn) )
                    463:                                                vb[i] = (unsigned char)QTOS((Q)BDY(tn));
                    464:                                }
                    465:                                break;
                    466:                        case O_STR:
                    467:                                str = BDY((STRING)ARG1(arg));
                    468:                                r = strlen(str);
                    469:                                if ( r <= len )
                    470:                                        bcopy(str,BDY(array),r);
                    471:                                break;
                    472:                        default:
                    473:                                if ( !ARG1(arg) )
                    474:                                        error("newbytearray : invalid initialization");
1.9       noro      475:                }
                    476:        }
                    477:        *rp = array;
1.1       noro      478: }
                    479:
1.24      noro      480: void Pnewmat(NODE arg,MAT *rp)
1.1       noro      481: {
                    482:        int row,col;
                    483:        int i,j,r,c;
                    484:        NODE tn,sn;
                    485:        MAT m;
                    486:        pointer **mb;
                    487:        LIST list;
                    488:
                    489:        asir_assert(ARG0(arg),O_N,"newmat");
                    490:        asir_assert(ARG1(arg),O_N,"newmat");
                    491:        row = QTOS((Q)ARG0(arg)); col = QTOS((Q)ARG1(arg));
1.5       noro      492:        if ( row < 0 || col < 0 )
1.1       noro      493:                error("newmat : invalid size");
                    494:        MKMAT(m,row,col);
                    495:        if ( argc(arg) == 3 ) {
                    496:                list = (LIST)ARG2(arg);
                    497:                asir_assert(list,O_LIST,"newmat");
                    498:                for ( r = 0, c = 0, tn = BDY(list); tn; r++, tn = NEXT(tn) ) {
                    499:                        for ( j = 0, sn = BDY((LIST)BDY(tn)); sn; j++, sn = NEXT(sn) );
                    500:                        c = MAX(c,j);
                    501:                }
                    502:                if ( (r > row) || (c > col) ) {
                    503:                        *rp = m;
                    504:                        return;
                    505:                }
                    506:                for ( i = 0, tn = BDY(list), mb = BDY(m); tn; i++, tn = NEXT(tn) ) {
                    507:                        asir_assert(BDY(tn),O_LIST,"newmat");
                    508:                        for ( j = 0, sn = BDY((LIST)BDY(tn)); sn; j++, sn = NEXT(sn) )
                    509:                                mb[i][j] = (pointer)BDY(sn);
                    510:                }
                    511:        }
1.28      saito     512:        *rp = m;
                    513: }
                    514:
                    515: void Pmat(NODE arg, MAT *rp)
                    516: {
                    517:        int row,col;
1.29      saito     518:        int i;
1.28      saito     519:        MAT m;
                    520:        pointer **mb;
1.29      saito     521:        pointer *ent;
1.28      saito     522:        NODE tn, sn;
1.29      saito     523:        VECT v;
1.28      saito     524:
                    525:        if ( !arg ) {
                    526:                *rp =0;
                    527:                return;
                    528:        }
                    529:
                    530:        for (row = 0, tn = arg; tn; tn = NEXT(tn), row++);
1.30      saito     531:        if ( row == 1 ) {
                    532:                if ( OID(ARG0(arg)) == O_MAT ) {
                    533:                        *rp=ARG0(arg);
                    534:                        return;
                    535:                } else if ( !(OID(ARG0(arg)) == O_LIST || OID(ARG0(arg)) == O_VECT)) {
                    536:                        error("mat : invalid argument");
                    537:                }
                    538:        }
1.29      saito     539:        if ( OID(ARG0(arg)) == O_VECT ) {
                    540:                v = ARG0(arg);
                    541:                col = v->len;
                    542:        } else if ( OID(ARG0(arg)) == O_LIST ) {
                    543:                for (col = 0, tn = BDY((LIST)ARG0(arg)); tn ; tn = NEXT(tn), col++);
1.30      saito     544:        } else {
                    545:                error("mat : invalid argument");
1.29      saito     546:        }
                    547:
1.28      saito     548:        MKMAT(m,row,col);
1.29      saito     549:        for (row = 0, tn = arg, mb = BDY(m); tn; tn = NEXT(tn), row++) {
                    550:                if ( BDY(tn) == 0 ) {
                    551:                        error("mat : invalid argument");
                    552:                } else if ( OID(BDY(tn)) == O_VECT ) {
                    553:                        v = tn->body;
                    554:                        ent = BDY(v);
                    555:                        for (i = 0; i < v->len; i++ ) mb[row][i] = (Obj)ent[i];
                    556:                } else if ( OID(BDY(tn)) == O_LIST ) {
                    557:                        for (col = 0, sn = BDY((LIST)BDY(tn)); sn; col++, sn = NEXT(sn) )
                    558:                                mb[row][col] = (pointer)BDY(sn);
                    559:                } else {
                    560:                        error("mat : invalid argument");
                    561:                }
                    562:        }
                    563:        *rp = m;
                    564: }
                    565:
                    566: void Pmatc(NODE arg, MAT *rp)
                    567: {
                    568:        int row,col;
                    569:        int i;
                    570:        MAT m;
                    571:        pointer **mb;
                    572:        pointer *ent;
                    573:        NODE tn, sn;
                    574:        VECT v;
                    575:
                    576:        if ( !arg ) {
                    577:                *rp =0;
                    578:                return;
                    579:        }
                    580:
                    581:        for (col = 0, tn = arg; tn; tn = NEXT(tn), col++);
1.30      saito     582:        if ( col == 1 ) {
                    583:                if ( OID(ARG0(arg)) == O_MAT ) {
                    584:                        *rp=ARG0(arg);
                    585:                        return;
                    586:                } else if ( !(OID(ARG0(arg)) == O_LIST || OID(ARG0(arg)) == O_VECT)) {
                    587:                        error("matc : invalid argument");
                    588:                }
                    589:        }
1.29      saito     590:        if ( OID(ARG0(arg)) == O_VECT ) {
                    591:                v = ARG0(arg);
                    592:                row = v->len;
                    593:        } else if ( OID(ARG0(arg)) == O_LIST ) {
                    594:                for (row = 0, tn = BDY((LIST)ARG0(arg)); tn ; tn = NEXT(tn), row++);
1.30      saito     595:        } else {
                    596:                error("matc : invalid argument");
1.29      saito     597:        }
                    598:
                    599:        MKMAT(m,row,col);
                    600:        for (col = 0, tn = arg, mb = BDY(m); tn; tn = NEXT(tn), col++) {
                    601:                if ( BDY(tn) == 0 ) {
                    602:                        error("matc : invalid argument");
                    603:                } else if ( OID(BDY(tn)) == O_VECT ) {
                    604:                        v = tn->body;
                    605:                        ent = BDY(v);
                    606:                        for (i = 0; i < v->len; i++ ) mb[i][col] = (Obj)ent[i];
                    607:                } else if ( OID(BDY(tn)) == O_LIST ) {
                    608:                        for (row = 0, sn = BDY((LIST)BDY(tn)); sn; row++, sn = NEXT(sn) )
                    609:                                mb[row][col] = (pointer)BDY(sn);
                    610:                } else {
                    611:                        error("matc : invalid argument");
                    612:                }
                    613:        }
1.1       noro      614:        *rp = m;
                    615: }
                    616:
1.24      noro      617: void Pvtol(NODE arg,LIST *rp)
1.1       noro      618: {
                    619:        NODE n,n1;
                    620:        VECT v;
                    621:        pointer *a;
                    622:        int len,i;
                    623:
                    624:        asir_assert(ARG0(arg),O_VECT,"vtol");
                    625:        v = (VECT)ARG0(arg); len = v->len; a = BDY(v);
                    626:        for ( i = len - 1, n = 0; i >= 0; i-- ) {
                    627:                MKNODE(n1,a[i],n); n = n1;
                    628:        }
                    629:        MKLIST(*rp,n);
1.33      noro      630: }
                    631:
                    632: void Pltov(NODE arg,VECT *rp)
                    633: {
                    634:        NODE n;
                    635:        VECT v;
                    636:        int len,i;
                    637:
                    638:        asir_assert(ARG0(arg),O_LIST,"ltov");
                    639:        n = (NODE)BDY((LIST)ARG0(arg));
                    640:        len = length(n);
                    641:        MKVECT(v,len);
                    642:        for ( i = 0; i < len; i++, n = NEXT(n) )
                    643:                BDY(v)[i] = BDY(n);
                    644:        *rp = v;
1.1       noro      645: }
                    646:
1.24      noro      647: void Premainder(NODE arg,Obj *rp)
1.1       noro      648: {
                    649:        Obj a;
                    650:        VECT v,w;
                    651:        MAT m,l;
                    652:        pointer *vb,*wb;
                    653:        pointer **mb,**lb;
                    654:        int id,i,j,n,row,col,t,smd,sgn;
                    655:        Q md,q;
                    656:
                    657:        a = (Obj)ARG0(arg); md = (Q)ARG1(arg);
                    658:        if ( !a )
                    659:                *rp = 0;
                    660:        else {
                    661:                id = OID(a);
                    662:                switch ( id ) {
                    663:                        case O_N:
                    664:                        case O_P:
                    665:                                cmp(md,(P)a,(P *)rp); break;
                    666:                        case O_VECT:
                    667:                                smd = QTOS(md);
                    668:                                v = (VECT)a; n = v->len; vb = v->body;
                    669:                                MKVECT(w,n); wb = w->body;
                    670:                                for ( i = 0; i < n; i++ ) {
                    671:                                        if ( q = (Q)vb[i] ) {
                    672:                                                sgn = SGN(q); t = rem(NM(q),smd);
                    673:                                                STOQ(t,q);
                    674:                                                if ( q )
                    675:                                                        SGN(q) = sgn;
                    676:                                        }
                    677:                                        wb[i] = (pointer)q;
                    678:                                }
                    679:                                *rp = (Obj)w;
                    680:                                break;
                    681:                        case O_MAT:
                    682:                                m = (MAT)a; row = m->row; col = m->col; mb = m->body;
                    683:                                MKMAT(l,row,col); lb = l->body;
                    684:                                for ( i = 0; i < row; i++ )
                    685:                                        for ( j = 0, vb = mb[i], wb = lb[i]; j < col; j++ )
                    686:                                                cmp(md,(P)vb[j],(P *)&wb[j]);
                    687:                                *rp = (Obj)l;
                    688:                                break;
                    689:                        default:
                    690:                                error("remainder : invalid argument");
                    691:                }
                    692:        }
                    693: }
                    694:
1.24      noro      695: void Psremainder(NODE arg,Obj *rp)
1.1       noro      696: {
                    697:        Obj a;
                    698:        VECT v,w;
                    699:        MAT m,l;
                    700:        pointer *vb,*wb;
                    701:        pointer **mb,**lb;
                    702:        unsigned int t,smd;
                    703:        int id,i,j,n,row,col;
                    704:        Q md,q;
                    705:
                    706:        a = (Obj)ARG0(arg); md = (Q)ARG1(arg);
                    707:        if ( !a )
                    708:                *rp = 0;
                    709:        else {
                    710:                id = OID(a);
                    711:                switch ( id ) {
                    712:                        case O_N:
                    713:                        case O_P:
                    714:                                cmp(md,(P)a,(P *)rp); break;
                    715:                        case O_VECT:
                    716:                                smd = QTOS(md);
                    717:                                v = (VECT)a; n = v->len; vb = v->body;
                    718:                                MKVECT(w,n); wb = w->body;
                    719:                                for ( i = 0; i < n; i++ ) {
                    720:                                        if ( q = (Q)vb[i] ) {
                    721:                                                t = (unsigned int)rem(NM(q),smd);
                    722:                                                if ( SGN(q) < 0 )
                    723:                                                        t = (smd - t) % smd;
                    724:                                                UTOQ(t,q);
                    725:                                        }
                    726:                                        wb[i] = (pointer)q;
                    727:                                }
                    728:                                *rp = (Obj)w;
                    729:                                break;
                    730:                        case O_MAT:
                    731:                                m = (MAT)a; row = m->row; col = m->col; mb = m->body;
                    732:                                MKMAT(l,row,col); lb = l->body;
                    733:                                for ( i = 0; i < row; i++ )
                    734:                                        for ( j = 0, vb = mb[i], wb = lb[i]; j < col; j++ )
                    735:                                                cmp(md,(P)vb[j],(P *)&wb[j]);
                    736:                                *rp = (Obj)l;
                    737:                                break;
                    738:                        default:
                    739:                                error("remainder : invalid argument");
                    740:                }
                    741:        }
                    742: }
                    743:
1.24      noro      744: void Psize(NODE arg,LIST *rp)
1.1       noro      745: {
                    746:
                    747:        int n,m;
                    748:        Q q;
                    749:        NODE t,s;
                    750:
                    751:        if ( !ARG0(arg) )
                    752:                 t = 0;
                    753:        else {
                    754:                switch (OID(ARG0(arg))) {
                    755:                        case O_VECT:
                    756:                                n = ((VECT)ARG0(arg))->len;
                    757:                                STOQ(n,q); MKNODE(t,q,0);
                    758:                                break;
                    759:                        case O_MAT:
                    760:                                n = ((MAT)ARG0(arg))->row; m = ((MAT)ARG0(arg))->col;
1.43      saito     761:                                STOQ(m,q); MKNODE(s,q,0); STOQ(n,q); MKNODE(t,q,s);
                    762:                                break;
                    763:                        case O_IMAT:
                    764:                                n = ((IMAT)ARG0(arg))->row; m = ((IMAT)ARG0(arg))->col;
1.1       noro      765:                                STOQ(m,q); MKNODE(s,q,0); STOQ(n,q); MKNODE(t,q,s);
                    766:                                break;
                    767:                        default:
                    768:                                error("size : invalid argument"); break;
                    769:                }
                    770:        }
                    771:        MKLIST(*rp,t);
                    772: }
                    773:
1.24      noro      774: void Pdet(NODE arg,P *rp)
1.1       noro      775: {
                    776:        MAT m;
                    777:        int n,i,j,mod;
                    778:        P d;
                    779:        P **mat,**w;
                    780:
                    781:        m = (MAT)ARG0(arg);
                    782:        asir_assert(m,O_MAT,"det");
                    783:        if ( m->row != m->col )
                    784:                error("det : non-square matrix");
                    785:        else if ( argc(arg) == 1 )
                    786:                detp(CO,(P **)BDY(m),m->row,rp);
                    787:        else {
                    788:                n = m->row; mod = QTOS((Q)ARG1(arg)); mat = (P **)BDY(m);
                    789:                w = (P **)almat_pointer(n,n);
                    790:                for ( i = 0; i < n; i++ )
                    791:                        for ( j = 0; j < n; j++ )
                    792:                                ptomp(mod,mat[i][j],&w[i][j]);
                    793:                detmp(CO,mod,w,n,&d);
                    794:                mptop(d,rp);
1.23      noro      795:        }
                    796: }
                    797:
1.24      noro      798: void Pinvmat(NODE arg,LIST *rp)
1.23      noro      799: {
                    800:        MAT m,r;
                    801:        int n,i,j,mod;
                    802:        P dn;
                    803:        P **mat,**imat,**w;
                    804:        NODE nd;
                    805:
                    806:        m = (MAT)ARG0(arg);
                    807:        asir_assert(m,O_MAT,"invmat");
                    808:        if ( m->row != m->col )
                    809:                error("invmat : non-square matrix");
                    810:        else if ( argc(arg) == 1 ) {
                    811:                n = m->row;
                    812:                invmatp(CO,(P **)BDY(m),n,&imat,&dn);
                    813:                NEWMAT(r); r->row = n; r->col = n; r->body = (pointer **)imat;
                    814:                nd = mknode(2,r,dn);
                    815:                MKLIST(*rp,nd);
                    816:        } else {
                    817:                n = m->row; mod = QTOS((Q)ARG1(arg)); mat = (P **)BDY(m);
                    818:                w = (P **)almat_pointer(n,n);
                    819:                for ( i = 0; i < n; i++ )
                    820:                        for ( j = 0; j < n; j++ )
                    821:                                ptomp(mod,mat[i][j],&w[i][j]);
                    822: #if 0
                    823:                detmp(CO,mod,w,n,&d);
                    824:                mptop(d,rp);
                    825: #else
                    826:                error("not implemented yet");
                    827: #endif
1.1       noro      828:        }
1.25      noro      829: }
                    830:
                    831: /*
                    832:        input : a row x col matrix A
                    833:                A[I] <-> A[I][0]*x_0+A[I][1]*x_1+...
                    834:
1.45      noro      835:        output : [B,D,R,C]
1.25      noro      836:                B : a rank(A) x col-rank(A) matrix
1.45      noro      837:                D : the denominator
1.25      noro      838:                R : a vector of length rank(A)
                    839:                C : a vector of length col-rank(A)
1.45      noro      840:                B[I] <-> D*x_{R[I]}+B[I][0]x_{C[0]}+B[I][1]x_{C[1]}+...
1.25      noro      841: */
                    842:
                    843: void Pgeneric_gauss_elim(NODE arg,LIST *rp)
                    844: {
                    845:        NODE n0;
                    846:        MAT m,nm;
                    847:        int *ri,*ci;
                    848:        VECT rind,cind;
                    849:        Q dn,q;
                    850:        int i,j,k,l,row,col,t,rank;
                    851:
                    852:        asir_assert(ARG0(arg),O_MAT,"generic_gauss_elim");
                    853:        m = (MAT)ARG0(arg);
                    854:        row = m->row; col = m->col;
                    855:        rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                    856:        t = col-rank;
                    857:        MKVECT(rind,rank);
                    858:        MKVECT(cind,t);
                    859:        for ( i = 0; i < rank; i++ ) {
                    860:                STOQ(ri[i],q);
                    861:                BDY(rind)[i] = (pointer)q;
                    862:        }
                    863:        for ( i = 0; i < t; i++ ) {
                    864:                STOQ(ci[i],q);
                    865:                BDY(cind)[i] = (pointer)q;
                    866:        }
                    867:        n0 = mknode(4,nm,dn,rind,cind);
                    868:        MKLIST(*rp,n0);
1.1       noro      869: }
                    870:
                    871: /*
                    872:        input : a row x col matrix A
                    873:                A[I] <-> A[I][0]*x_0+A[I][1]*x_1+...
                    874:
                    875:        output : [B,R,C]
                    876:                B : a rank(A) x col-rank(A) matrix
                    877:                R : a vector of length rank(A)
                    878:                C : a vector of length col-rank(A)
1.47    ! noro      879:                RN : a vector of length rank(A) indicating useful rows
        !           880:
1.1       noro      881:                B[I] <-> x_{R[I]}+B[I][0]x_{C[0]}+B[I][1]x_{C[1]}+...
                    882: */
                    883:
1.24      noro      884: void Pgeneric_gauss_elim_mod(NODE arg,LIST *rp)
1.1       noro      885: {
                    886:        NODE n0;
                    887:        MAT m,mat;
1.47    ! noro      888:        VECT rind,cind,rnum;
1.1       noro      889:        Q **tmat;
1.47    ! noro      890:        int **wmat,**row0;
        !           891:        Q *rib,*cib,*rnb;
        !           892:        int *colstat,*p;
1.1       noro      893:        Q q;
1.24      noro      894:        int md,i,j,k,l,row,col,t,rank;
1.1       noro      895:
                    896:        asir_assert(ARG0(arg),O_MAT,"generic_gauss_elim_mod");
                    897:        asir_assert(ARG1(arg),O_N,"generic_gauss_elim_mod");
                    898:        m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
                    899:        row = m->row; col = m->col; tmat = (Q **)m->body;
                    900:        wmat = (int **)almat(row,col);
1.47    ! noro      901:
        !           902:        row0 = (int **)ALLOCA(row*sizeof(int *));
        !           903:        for ( i = 0; i < row; i++ ) row0[i] = wmat[i];
        !           904:
1.1       noro      905:        colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
                    906:        for ( i = 0; i < row; i++ )
                    907:                for ( j = 0; j < col; j++ )
                    908:                        if ( q = (Q)tmat[i][j] ) {
                    909:                                t = rem(NM(q),md);
                    910:                                if ( t && SGN(q) < 0 )
                    911:                                        t = (md - t) % md;
                    912:                                wmat[i][j] = t;
                    913:                        } else
                    914:                                wmat[i][j] = 0;
                    915:        rank = generic_gauss_elim_mod(wmat,row,col,md,colstat);
                    916:
1.47    ! noro      917:        MKVECT(rnum,rank);
        !           918:        rnb = (Q *)rnum->body;
        !           919:        for ( i = 0; i < rank; i++ )
        !           920:                for ( j = 0, p = wmat[i]; j < row; j++ )
        !           921:                        if ( p == row0[j] )
        !           922:                                STOQ(j,rnb[i]);
        !           923:
1.1       noro      924:        MKMAT(mat,rank,col-rank);
                    925:        tmat = (Q **)mat->body;
                    926:        for ( i = 0; i < rank; i++ )
                    927:                for ( j = k = 0; j < col; j++ )
                    928:                        if ( !colstat[j] ) {
                    929:                                UTOQ(wmat[i][j],tmat[i][k]); k++;
                    930:                        }
                    931:
                    932:        MKVECT(rind,rank);
                    933:        MKVECT(cind,col-rank);
                    934:        rib = (Q *)rind->body; cib = (Q *)cind->body;
                    935:        for ( j = k = l = 0; j < col; j++ )
                    936:                if ( colstat[j] ) {
                    937:                        STOQ(j,rib[k]); k++;
                    938:                } else {
                    939:                        STOQ(j,cib[l]); l++;
                    940:                }
1.47    ! noro      941:        n0 = mknode(4,mat,rind,cind,rnum);
1.1       noro      942:        MKLIST(*rp,n0);
                    943: }
                    944:
1.24      noro      945: void Pleqm(NODE arg,VECT *rp)
1.1       noro      946: {
                    947:        MAT m;
                    948:        VECT vect;
                    949:        pointer **mat;
                    950:        Q *v;
                    951:        Q q;
                    952:        int **wmat;
                    953:        int md,i,j,row,col,t,n,status;
                    954:
                    955:        asir_assert(ARG0(arg),O_MAT,"leqm");
                    956:        asir_assert(ARG1(arg),O_N,"leqm");
                    957:        m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
                    958:        row = m->row; col = m->col; mat = m->body;
                    959:        wmat = (int **)almat(row,col);
                    960:        for ( i = 0; i < row; i++ )
                    961:                for ( j = 0; j < col; j++ )
                    962:                        if ( q = (Q)mat[i][j] ) {
                    963:                                t = rem(NM(q),md);
                    964:                                if ( SGN(q) < 0 )
                    965:                                        t = (md - t) % md;
                    966:                                wmat[i][j] = t;
                    967:                        } else
                    968:                                wmat[i][j] = 0;
                    969:        status = gauss_elim_mod(wmat,row,col,md);
                    970:        if ( status < 0 )
                    971:                *rp = 0;
                    972:        else if ( status > 0 )
                    973:                *rp = (VECT)ONE;
                    974:        else {
                    975:                n = col - 1;
                    976:                MKVECT(vect,n);
                    977:                for ( i = 0, v = (Q *)vect->body; i < n; i++ ) {
                    978:                        t = (md-wmat[i][n])%md; STOQ(t,v[i]);
                    979:                }
                    980:                *rp = vect;
                    981:        }
                    982: }
                    983:
1.24      noro      984: int gauss_elim_mod(int **mat,int row,int col,int md)
1.1       noro      985: {
                    986:        int i,j,k,inv,a,n;
                    987:        int *t,*pivot;
                    988:
                    989:        n = col - 1;
                    990:        for ( j = 0; j < n; j++ ) {
                    991:                for ( i = j; i < row && !mat[i][j]; i++ );
                    992:                if ( i == row )
                    993:                        return 1;
                    994:                if ( i != j ) {
                    995:                        t = mat[i]; mat[i] = mat[j]; mat[j] = t;
                    996:                }
                    997:                pivot = mat[j];
                    998:                inv = invm(pivot[j],md);
                    999:                for ( k = j; k <= n; k++ ) {
                   1000: /*                     pivot[k] = dmar(pivot[k],inv,0,md); */
                   1001:                        DMAR(pivot[k],inv,0,md,pivot[k])
                   1002:                }
                   1003:                for ( i = 0; i < row; i++ ) {
                   1004:                        t = mat[i];
                   1005:                        if ( i != j && (a = t[j]) )
                   1006:                                for ( k = j, a = md - a; k <= n; k++ ) {
1.8       noro     1007:                                        unsigned int tk;
1.1       noro     1008: /*                                     t[k] = dmar(pivot[k],a,t[k],md); */
1.8       noro     1009:                                        DMAR(pivot[k],a,t[k],md,tk)
                   1010:                                        t[k] = tk;
1.1       noro     1011:                                }
                   1012:                }
                   1013:        }
                   1014:        for ( i = n; i < row && !mat[i][n]; i++ );
                   1015:        if ( i == row )
                   1016:                return 0;
                   1017:        else
                   1018:                return -1;
                   1019: }
                   1020:
1.4       noro     1021: struct oEGT eg_mod,eg_elim,eg_elim1,eg_elim2,eg_chrem,eg_gschk,eg_intrat,eg_symb;
1.31      noro     1022: struct oEGT eg_conv;
1.1       noro     1023:
1.24      noro     1024: int generic_gauss_elim(MAT mat,MAT *nm,Q *dn,int **rindp,int **cindp)
1.1       noro     1025: {
                   1026:        int **wmat;
                   1027:        Q **bmat;
                   1028:        N **tmat;
                   1029:        Q *bmi;
                   1030:        N *tmi;
                   1031:        Q q;
                   1032:        int *wmi;
                   1033:        int *colstat,*wcolstat,*rind,*cind;
                   1034:        int row,col,ind,md,i,j,k,l,t,t1,rank,rank0,inv;
                   1035:        N m1,m2,m3,s,u;
                   1036:        MAT r,crmat;
                   1037:        struct oEGT tmp0,tmp1;
                   1038:        struct oEGT eg_mod_split,eg_elim_split,eg_chrem_split;
                   1039:        struct oEGT eg_intrat_split,eg_gschk_split;
                   1040:        int ret;
                   1041:
                   1042:        init_eg(&eg_mod_split); init_eg(&eg_chrem_split);
                   1043:        init_eg(&eg_elim_split); init_eg(&eg_intrat_split);
                   1044:        init_eg(&eg_gschk_split);
                   1045:        bmat = (Q **)mat->body;
                   1046:        row = mat->row; col = mat->col;
                   1047:        wmat = (int **)almat(row,col);
                   1048:        colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
                   1049:        wcolstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
                   1050:        for ( ind = 0; ; ind++ ) {
1.11      noro     1051:                if ( DP_Print ) {
1.2       noro     1052:                        fprintf(asir_out,"."); fflush(asir_out);
                   1053:                }
1.12      noro     1054:                md = get_lprime(ind);
1.1       noro     1055:                get_eg(&tmp0);
                   1056:                for ( i = 0; i < row; i++ )
                   1057:                        for ( j = 0, bmi = bmat[i], wmi = wmat[i]; j < col; j++ )
                   1058:                                if ( q = (Q)bmi[j] ) {
                   1059:                                        t = rem(NM(q),md);
                   1060:                                        if ( t && SGN(q) < 0 )
                   1061:                                                t = (md - t) % md;
                   1062:                                        wmi[j] = t;
                   1063:                                } else
                   1064:                                        wmi[j] = 0;
                   1065:                get_eg(&tmp1);
                   1066:                add_eg(&eg_mod,&tmp0,&tmp1);
                   1067:                add_eg(&eg_mod_split,&tmp0,&tmp1);
                   1068:                get_eg(&tmp0);
                   1069:                rank = generic_gauss_elim_mod(wmat,row,col,md,wcolstat);
                   1070:                get_eg(&tmp1);
                   1071:                add_eg(&eg_elim,&tmp0,&tmp1);
                   1072:                add_eg(&eg_elim_split,&tmp0,&tmp1);
                   1073:                if ( !ind ) {
                   1074: RESET:
                   1075:                        UTON(md,m1);
                   1076:                        rank0 = rank;
                   1077:                        bcopy(wcolstat,colstat,col*sizeof(int));
                   1078:                        MKMAT(crmat,rank,col-rank);
                   1079:                        MKMAT(r,rank,col-rank); *nm = r;
                   1080:                        tmat = (N **)crmat->body;
                   1081:                        for ( i = 0; i < rank; i++ )
                   1082:                                for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
                   1083:                                        if ( !colstat[j] ) {
                   1084:                                                UTON(wmi[j],tmi[k]); k++;
                   1085:                                        }
                   1086:                } else {
                   1087:                        if ( rank < rank0 ) {
1.11      noro     1088:                                if ( DP_Print ) {
1.1       noro     1089:                                        fprintf(asir_out,"lower rank matrix; continuing...\n");
1.2       noro     1090:                                        fflush(asir_out);
                   1091:                                }
1.1       noro     1092:                                continue;
                   1093:                        } else if ( rank > rank0 ) {
1.11      noro     1094:                                if ( DP_Print ) {
1.1       noro     1095:                                        fprintf(asir_out,"higher rank matrix; resetting...\n");
1.2       noro     1096:                                        fflush(asir_out);
                   1097:                                }
1.1       noro     1098:                                goto RESET;
                   1099:                        } else {
                   1100:                                for ( j = 0; (j<col) && (colstat[j]==wcolstat[j]); j++ );
                   1101:                                if ( j < col ) {
1.11      noro     1102:                                        if ( DP_Print ) {
1.1       noro     1103:                                                fprintf(asir_out,"inconsitent colstat; resetting...\n");
1.2       noro     1104:                                                fflush(asir_out);
                   1105:                                        }
1.1       noro     1106:                                        goto RESET;
                   1107:                                }
                   1108:                        }
                   1109:
                   1110:                        get_eg(&tmp0);
                   1111:                        inv = invm(rem(m1,md),md);
                   1112:                        UTON(md,m2); muln(m1,m2,&m3);
                   1113:                        for ( i = 0; i < rank; i++ )
                   1114:                                for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
                   1115:                                        if ( !colstat[j] ) {
                   1116:                                                if ( tmi[k] ) {
                   1117:                                                /* f3 = f1+m1*(m1 mod m2)^(-1)*(f2 - f1 mod m2) */
                   1118:                                                        t = rem(tmi[k],md);
                   1119:                                                        if ( wmi[j] >= t )
                   1120:                                                                t = wmi[j]-t;
                   1121:                                                        else
                   1122:                                                                t = md-(t-wmi[j]);
                   1123:                                                        DMAR(t,inv,0,md,t1)
                   1124:                                                        UTON(t1,u);
                   1125:                                                        muln(m1,u,&s);
                   1126:                                                        addn(tmi[k],s,&u); tmi[k] = u;
                   1127:                                                } else if ( wmi[j] ) {
                   1128:                                                /* f3 = m1*(m1 mod m2)^(-1)*f2 */
                   1129:                                                        DMAR(wmi[j],inv,0,md,t)
                   1130:                                                        UTON(t,u);
                   1131:                                                        muln(m1,u,&s); tmi[k] = s;
                   1132:                                                }
                   1133:                                                k++;
                   1134:                                        }
                   1135:                        m1 = m3;
                   1136:                        get_eg(&tmp1);
                   1137:                        add_eg(&eg_chrem,&tmp0,&tmp1);
                   1138:                        add_eg(&eg_chrem_split,&tmp0,&tmp1);
                   1139:
                   1140:                        get_eg(&tmp0);
1.38      noro     1141:                        if ( ind % F4_INTRAT_PERIOD )
1.13      noro     1142:                                ret = 0;
                   1143:                        else
                   1144:                                ret = intmtoratm(crmat,m1,*nm,dn);
1.1       noro     1145:                        get_eg(&tmp1);
                   1146:                        add_eg(&eg_intrat,&tmp0,&tmp1);
                   1147:                        add_eg(&eg_intrat_split,&tmp0,&tmp1);
                   1148:                        if ( ret ) {
                   1149:                                *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
                   1150:                                *cindp = cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int));
                   1151:                                for ( j = k = l = 0; j < col; j++ )
                   1152:                                        if ( colstat[j] )
                   1153:                                                rind[k++] = j;
                   1154:                                        else
                   1155:                                                cind[l++] = j;
                   1156:                                get_eg(&tmp0);
1.3       noro     1157:                                if ( gensolve_check(mat,*nm,*dn,rind,cind) ) {
                   1158:                                        get_eg(&tmp1);
                   1159:                                        add_eg(&eg_gschk,&tmp0,&tmp1);
                   1160:                                        add_eg(&eg_gschk_split,&tmp0,&tmp1);
1.11      noro     1161:                                        if ( DP_Print ) {
1.3       noro     1162:                                                print_eg("Mod",&eg_mod_split);
                   1163:                                                print_eg("Elim",&eg_elim_split);
                   1164:                                                print_eg("ChRem",&eg_chrem_split);
                   1165:                                                print_eg("IntRat",&eg_intrat_split);
                   1166:                                                print_eg("Check",&eg_gschk_split);
                   1167:                                                fflush(asir_out);
                   1168:                                        }
                   1169:                                        return rank;
                   1170:                                }
                   1171:                        }
                   1172:                }
                   1173:        }
                   1174: }
                   1175:
1.24      noro     1176: int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn,int **rindp,int **cindp)
1.3       noro     1177: {
                   1178:        MAT bmat,xmat;
                   1179:        Q **a0,**a,**b,**x,**nm;
                   1180:        Q *ai,*bi,*xi;
                   1181:        int row,col;
                   1182:        int **w;
                   1183:        int *wi;
                   1184:        int **wc;
                   1185:        Q mdq,q,s,u;
                   1186:        N tn;
                   1187:        int ind,md,i,j,k,l,li,ri,rank;
                   1188:        unsigned int t;
                   1189:        int *cinfo,*rinfo;
                   1190:        int *rind,*cind;
                   1191:        int count;
1.41      noro     1192:        int ret;
                   1193:        struct oEGT eg_mul,eg_inv,eg_intrat,eg_check,tmp0,tmp1;
1.39      noro     1194:        int period;
1.44      noro     1195:        int *wx,*ptr;
                   1196:        int wxsize,nsize;
                   1197:        N wn;
                   1198:        Q wq;
1.3       noro     1199:
                   1200:        a0 = (Q **)mat->body;
                   1201:        row = mat->row; col = mat->col;
                   1202:        w = (int **)almat(row,col);
                   1203:        for ( ind = 0; ; ind++ ) {
1.12      noro     1204:                md = get_lprime(ind);
1.3       noro     1205:                STOQ(md,mdq);
                   1206:                for ( i = 0; i < row; i++ )
                   1207:                        for ( j = 0, ai = a0[i], wi = w[i]; j < col; j++ )
                   1208:                                if ( q = (Q)ai[j] ) {
                   1209:                                        t = rem(NM(q),md);
                   1210:                                        if ( t && SGN(q) < 0 )
                   1211:                                                t = (md - t) % md;
                   1212:                                        wi[j] = t;
                   1213:                                } else
                   1214:                                        wi[j] = 0;
                   1215:
1.27      noro     1216:                rank = find_lhs_and_lu_mod((unsigned int **)w,row,col,md,&rinfo,&cinfo);
1.3       noro     1217:                a = (Q **)almat_pointer(rank,rank); /* lhs mat */
                   1218:                MKMAT(bmat,rank,col-rank); b = (Q **)bmat->body; /* lhs mat */
                   1219:                for ( j = li = ri = 0; j < col; j++ )
                   1220:                        if ( cinfo[j] ) {
                   1221:                                /* the column is in lhs */
                   1222:                                for ( i = 0; i < rank; i++ ) {
                   1223:                                        w[i][li] = w[i][j];
                   1224:                                        a[i][li] = a0[rinfo[i]][j];
                   1225:                                }
                   1226:                                li++;
                   1227:                        } else {
                   1228:                                /* the column is in rhs */
                   1229:                                for ( i = 0; i < rank; i++ )
                   1230:                                        b[i][ri] = a0[rinfo[i]][j];
                   1231:                                ri++;
                   1232:                        }
                   1233:
                   1234:                        /* solve Ax+B=0; A: rank x rank, B: rank x ri */
                   1235:                        MKMAT(xmat,rank,ri); x = (Q **)(xmat)->body;
                   1236:                        MKMAT(*nmmat,rank,ri); nm = (Q **)(*nmmat)->body;
                   1237:                        /* use the right part of w as work area */
                   1238:                        /* ri = col - rank */
                   1239:                        wc = (int **)almat(rank,ri);
                   1240:                        for ( i = 0; i < rank; i++ )
                   1241:                                wc[i] = w[i]+rank;
                   1242:                        *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
                   1243:                        *cindp = cind = (int *)MALLOC_ATOMIC((ri)*sizeof(int));
                   1244:
                   1245:                        init_eg(&eg_mul); init_eg(&eg_inv);
1.41      noro     1246:                        init_eg(&eg_check); init_eg(&eg_intrat);
1.39      noro     1247:                        period = F4_INTRAT_PERIOD;
1.44      noro     1248:                        nsize = period;
                   1249:                        wxsize = rank*ri*nsize;
                   1250:                        wx = (int *)MALLOC_ATOMIC(wxsize*sizeof(int));
                   1251:                        for ( i = 0; i < wxsize; i++ ) wx[i] = 0;
                   1252:                        for ( q = ONE, count = 0; ; ) {
1.42      noro     1253:                                if ( DP_Print > 3 )
1.41      noro     1254:                                        fprintf(stderr,"o");
1.3       noro     1255:                                /* wc = -b mod md */
1.44      noro     1256:                                get_eg(&tmp0);
1.3       noro     1257:                                for ( i = 0; i < rank; i++ )
                   1258:                                        for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ )
                   1259:                                                if ( u = (Q)bi[j] ) {
                   1260:                                                        t = rem(NM(u),md);
                   1261:                                                        if ( t && SGN(u) > 0 )
                   1262:                                                                t = (md - t) % md;
                   1263:                                                        wi[j] = t;
                   1264:                                                } else
                   1265:                                                        wi[j] = 0;
1.44      noro     1266:                                /* wc = A^(-1)wc; wc is not normalized */
                   1267:                                solve_by_lu_mod(w,rank,md,wc,ri,0);
                   1268:                                /* wx += q*wc */
                   1269:                                ptr = wx;
                   1270:                                for ( i = 0; i < rank; i++ )
                   1271:                                        for ( j = 0, wi = wc[i]; j < ri; j++ ) {
                   1272:                                                if ( wi[j] )
                   1273:                                                        muln_1(BD(NM(q)),PL(NM(q)),wi[j],ptr);
                   1274:                                                ptr += nsize;
                   1275:                                        }
                   1276:                                count++;
1.1       noro     1277:                                get_eg(&tmp1);
1.3       noro     1278:                                add_eg(&eg_inv,&tmp0,&tmp1);
                   1279:                                get_eg(&tmp0);
                   1280:                                for ( i = 0; i < rank; i++ )
                   1281:                                        for ( j = 0; j < ri; j++ ) {
                   1282:                                                inner_product_mat_int_mod(a,wc,rank,i,j,&u);
                   1283:                                                addq(b[i][j],u,&s);
                   1284:                                                if ( s ) {
                   1285:                                                        t = divin(NM(s),md,&tn);
                   1286:                                                        if ( t )
                   1287:                                                                error("generic_gauss_elim_hensel:incosistent");
                   1288:                                                        NTOQ(tn,SGN(s),b[i][j]);
                   1289:                                                } else
                   1290:                                                        b[i][j] = 0;
                   1291:                                        }
                   1292:                                get_eg(&tmp1);
                   1293:                                add_eg(&eg_mul,&tmp0,&tmp1);
                   1294:                                /* q = q*md */
                   1295:                                mulq(q,mdq,&u); q = u;
1.44      noro     1296:                                if ( count == period ) {
1.41      noro     1297:                                        get_eg(&tmp0);
1.44      noro     1298:                                        ptr = wx;
                   1299:                                        for ( i = 0; i < rank; i++ )
                   1300:                                                for ( j = 0, xi = x[i]; j < ri;
                   1301:                                                        j++, ptr += nsize ) {
                   1302:                                                        for ( k = nsize-1; k >= 0 && !ptr[k]; k-- );
                   1303:                                                        if ( k >= 0 ) {
                   1304:                                                                wn = NALLOC(k+1);
                   1305:                                                                PL(wn) = k+1;
                   1306:                                                                for ( l = 0; l <= k; l++ ) BD(wn)[l] = (unsigned int)ptr[l];
                   1307:                                                                NTOQ(wn,1,wq);
                   1308:                                                                subq(xi[j],wq,&u); xi[j] = u;
                   1309:                                                        }
                   1310:                                                }
1.41      noro     1311:                                        ret = intmtoratm_q(xmat,NM(q),*nmmat,dn);
                   1312:                                        get_eg(&tmp1); add_eg(&eg_intrat,&tmp0,&tmp1);
                   1313:                                        if ( ret ) {
1.39      noro     1314:                                                for ( j = k = l = 0; j < col; j++ )
                   1315:                                                        if ( cinfo[j] )
                   1316:                                                                rind[k++] = j;
                   1317:                                                        else
                   1318:                                                                cind[l++] = j;
1.41      noro     1319:                                                get_eg(&tmp0);
                   1320:                                                ret = gensolve_check(mat,*nmmat,*dn,rind,cind);
                   1321:                                                get_eg(&tmp1); add_eg(&eg_check,&tmp0,&tmp1);
                   1322:                                                if ( ret ) {
1.42      noro     1323:                                                        if ( DP_Print > 3 ) {
1.40      noro     1324:                                                                fprintf(stderr,"\n");
                   1325:                                                                print_eg("INV",&eg_inv);
                   1326:                                                                print_eg("MUL",&eg_mul);
1.41      noro     1327:                                                                print_eg("INTRAT",&eg_intrat);
                   1328:                                                                print_eg("CHECK",&eg_check);
1.40      noro     1329:                                                                fflush(asir_out);
                   1330:                                                        }
1.39      noro     1331:                                                        return rank;
                   1332:                                                }
1.44      noro     1333:                                        } else {
                   1334:                                                period = period*3/2;
                   1335:                                                count = 0;
                   1336:                                                nsize += period;
                   1337:                                                wxsize += rank*ri*nsize;
                   1338:                                                wx = (int *)REALLOC(wx,wxsize*sizeof(int));
                   1339:                                                for ( i = 0; i < wxsize; i++ ) wx[i] = 0;
                   1340:                                        }
1.41      noro     1341:                                }
1.1       noro     1342:                        }
                   1343:        }
                   1344: }
                   1345:
                   1346: int f4_nocheck;
                   1347:
1.24      noro     1348: int gensolve_check(MAT mat,MAT nm,Q dn,int *rind,int *cind)
1.1       noro     1349: {
                   1350:        int row,col,rank,clen,i,j,k,l;
1.24      noro     1351:        Q s,t;
1.1       noro     1352:        Q *w;
                   1353:        Q *mati,*nmk;
                   1354:
                   1355:        if ( f4_nocheck )
                   1356:                return 1;
                   1357:        row = mat->row; col = mat->col;
                   1358:        rank = nm->row; clen = nm->col;
                   1359:        w = (Q *)MALLOC(clen*sizeof(Q));
                   1360:        for ( i = 0; i < row; i++ ) {
                   1361:                mati = (Q *)mat->body[i];
                   1362: #if 1
                   1363:                bzero(w,clen*sizeof(Q));
                   1364:                for ( k = 0; k < rank; k++ )
                   1365:                        for ( l = 0, nmk = (Q *)nm->body[k]; l < clen; l++ ) {
                   1366:                                mulq(mati[rind[k]],nmk[l],&t);
                   1367:                                addq(w[l],t,&s); w[l] = s;
                   1368:                        }
                   1369:                for ( j = 0; j < clen; j++ ) {
                   1370:                        mulq(dn,mati[cind[j]],&t);
                   1371:                        if ( cmpq(w[j],t) )
                   1372:                                break;
                   1373:                }
                   1374: #else
                   1375:                for ( j = 0; j < clen; j++ ) {
                   1376:                        for ( k = 0, s = 0; k < rank; k++ ) {
                   1377:                                mulq(mati[rind[k]],nm->body[k][j],&t);
                   1378:                                addq(s,t,&u); s = u;
                   1379:                        }
                   1380:                        mulq(dn,mati[cind[j]],&t);
                   1381:                        if ( cmpq(s,t) )
                   1382:                                break;
                   1383:                }
                   1384: #endif
                   1385:                if ( j != clen )
                   1386:                        break;
                   1387:        }
                   1388:        if ( i != row )
                   1389:                return 0;
                   1390:        else
                   1391:                return 1;
                   1392: }
                   1393:
                   1394: /* assuming 0 < c < m */
                   1395:
1.24      noro     1396: int inttorat(N c,N m,N b,int *sgnp,N *nmp,N *dnp)
1.1       noro     1397: {
1.24      noro     1398:        Q qq,t,u1,v1,r1;
                   1399:        N q,u2,v2,r2;
1.1       noro     1400:
                   1401:        u1 = 0; v1 = ONE; u2 = m; v2 = c;
                   1402:        while ( cmpn(v2,b) >= 0 ) {
                   1403:                divn(u2,v2,&q,&r2); u2 = v2; v2 = r2;
                   1404:                NTOQ(q,1,qq); mulq(qq,v1,&t); subq(u1,t,&r1); u1 = v1; v1 = r1;
                   1405:        }
                   1406:        if ( cmpn(NM(v1),b) >= 0 )
                   1407:                return 0;
                   1408:        else {
                   1409:                *nmp = v2;
                   1410:                *dnp = NM(v1);
                   1411:                *sgnp = SGN(v1);
                   1412:                return 1;
                   1413:        }
                   1414: }
                   1415:
                   1416: /* mat->body = N ** */
                   1417:
1.24      noro     1418: int intmtoratm(MAT mat,N md,MAT nm,Q *dn)
1.1       noro     1419: {
                   1420:        N t,s,b;
1.24      noro     1421:        Q dn0,dn1,nm1,q;
1.1       noro     1422:        int i,j,k,l,row,col;
                   1423:        Q **rmat;
                   1424:        N **tmat;
                   1425:        N *tmi;
                   1426:        Q *nmk;
                   1427:        N u,unm,udn;
                   1428:        int sgn,ret;
                   1429:
1.3       noro     1430:        if ( UNIN(md) )
                   1431:                return 0;
1.1       noro     1432:        row = mat->row; col = mat->col;
                   1433:        bshiftn(md,1,&t);
                   1434:        isqrt(t,&s);
                   1435:        bshiftn(s,64,&b);
                   1436:        if ( !b )
                   1437:                b = ONEN;
                   1438:        dn0 = ONE;
                   1439:        tmat = (N **)mat->body;
                   1440:        rmat = (Q **)nm->body;
                   1441:        for ( i = 0; i < row; i++ )
                   1442:                for ( j = 0, tmi = tmat[i]; j < col; j++ )
                   1443:                        if ( tmi[j] ) {
                   1444:                                muln(tmi[j],NM(dn0),&s);
                   1445:                                remn(s,md,&u);
                   1446:                                ret = inttorat(u,md,b,&sgn,&unm,&udn);
                   1447:                                if ( !ret )
                   1448:                                        return 0;
                   1449:                                else {
                   1450:                                        NTOQ(unm,sgn,nm1);
                   1451:                                        NTOQ(udn,1,dn1);
                   1452:                                        if ( !UNIQ(dn1) ) {
                   1453:                                                for ( k = 0; k < i; k++ )
                   1454:                                                        for ( l = 0, nmk = rmat[k]; l < col; l++ ) {
                   1455:                                                                mulq(nmk[l],dn1,&q); nmk[l] = q;
                   1456:                                                        }
                   1457:                                                for ( l = 0, nmk = rmat[i]; l < j; l++ ) {
                   1458:                                                        mulq(nmk[l],dn1,&q); nmk[l] = q;
                   1459:                                                }
                   1460:                                        }
                   1461:                                        rmat[i][j] = nm1;
                   1462:                                        mulq(dn0,dn1,&q); dn0 = q;
                   1463:                                }
                   1464:                        }
                   1465:        *dn = dn0;
                   1466:        return 1;
                   1467: }
                   1468:
1.3       noro     1469: /* mat->body = Q ** */
                   1470:
1.24      noro     1471: int intmtoratm_q(MAT mat,N md,MAT nm,Q *dn)
1.3       noro     1472: {
                   1473:        N t,s,b;
1.24      noro     1474:        Q dn0,dn1,nm1,q;
1.3       noro     1475:        int i,j,k,l,row,col;
                   1476:        Q **rmat;
                   1477:        Q **tmat;
                   1478:        Q *tmi;
                   1479:        Q *nmk;
                   1480:        N u,unm,udn;
                   1481:        int sgn,ret;
                   1482:
                   1483:        if ( UNIN(md) )
                   1484:                return 0;
                   1485:        row = mat->row; col = mat->col;
                   1486:        bshiftn(md,1,&t);
                   1487:        isqrt(t,&s);
                   1488:        bshiftn(s,64,&b);
                   1489:        if ( !b )
                   1490:                b = ONEN;
                   1491:        dn0 = ONE;
                   1492:        tmat = (Q **)mat->body;
                   1493:        rmat = (Q **)nm->body;
                   1494:        for ( i = 0; i < row; i++ )
                   1495:                for ( j = 0, tmi = tmat[i]; j < col; j++ )
                   1496:                        if ( tmi[j] ) {
                   1497:                                muln(NM(tmi[j]),NM(dn0),&s);
                   1498:                                remn(s,md,&u);
                   1499:                                ret = inttorat(u,md,b,&sgn,&unm,&udn);
                   1500:                                if ( !ret )
                   1501:                                        return 0;
                   1502:                                else {
                   1503:                                        if ( SGN(tmi[j])<0 )
                   1504:                                                sgn = -sgn;
                   1505:                                        NTOQ(unm,sgn,nm1);
                   1506:                                        NTOQ(udn,1,dn1);
                   1507:                                        if ( !UNIQ(dn1) ) {
                   1508:                                                for ( k = 0; k < i; k++ )
                   1509:                                                        for ( l = 0, nmk = rmat[k]; l < col; l++ ) {
                   1510:                                                                mulq(nmk[l],dn1,&q); nmk[l] = q;
                   1511:                                                        }
                   1512:                                                for ( l = 0, nmk = rmat[i]; l < j; l++ ) {
                   1513:                                                        mulq(nmk[l],dn1,&q); nmk[l] = q;
                   1514:                                                }
                   1515:                                        }
                   1516:                                        rmat[i][j] = nm1;
                   1517:                                        mulq(dn0,dn1,&q); dn0 = q;
                   1518:                                }
                   1519:                        }
                   1520:        *dn = dn0;
                   1521:        return 1;
                   1522: }
                   1523:
1.4       noro     1524: #define ONE_STEP1  if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
                   1525:
1.24      noro     1526: void reduce_reducers_mod(int **mat,int row,int col,int md)
1.4       noro     1527: {
                   1528:        int i,j,k,l,hc,zzz;
                   1529:        int *t,*s,*tj,*ind;
                   1530:
                   1531:        /* reduce the reducers */
                   1532:        ind = (int *)ALLOCA(row*sizeof(int));
                   1533:        for ( i = 0; i < row; i++ ) {
                   1534:                t = mat[i];
                   1535:                for ( j = 0; j < col && !t[j]; j++ );
                   1536:                /* register the position of the head term */
                   1537:                ind[i] = j;
                   1538:                for ( l = i-1; l >= 0; l-- ) {
                   1539:                        /* reduce mat[i] by mat[l] */
                   1540:                        if ( hc = t[ind[l]] ) {
                   1541:                                /* mat[i] = mat[i]-hc*mat[l] */
                   1542:                                j = ind[l];
                   1543:                                s = mat[l]+j;
                   1544:                                tj = t+j;
                   1545:                                hc = md-hc;
                   1546:                                k = col-j;
                   1547:                                for ( ; k >= 64; k -= 64 ) {
                   1548:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1549:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1550:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1551:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1552:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1553:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1554:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1555:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1556:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1557:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1558:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1559:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1560:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1561:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1562:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1563:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1564:                                }
1.16      noro     1565:                                for ( ; k > 0; k-- ) {
1.4       noro     1566:                                        if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
                   1567:                                }
                   1568:                        }
                   1569:                }
                   1570:        }
                   1571: }
                   1572:
                   1573: /*
                   1574:        mat[i] : reducers (i=0,...,nred-1)
                   1575:                 spolys (i=nred,...,row-1)
                   1576:        mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
                   1577:        1. reduce the reducers
                   1578:        2. reduce spolys by the reduced reducers
                   1579: */
                   1580:
1.24      noro     1581: void pre_reduce_mod(int **mat,int row,int col,int nred,int md)
1.4       noro     1582: {
                   1583:        int i,j,k,l,hc,inv;
                   1584:        int *t,*s,*tk,*ind;
                   1585:
                   1586: #if 1
                   1587:        /* reduce the reducers */
                   1588:        ind = (int *)ALLOCA(row*sizeof(int));
                   1589:        for ( i = 0; i < nred; i++ ) {
                   1590:                /* make mat[i] monic and mat[i] by mat[0],...,mat[i-1] */
                   1591:                t = mat[i];
                   1592:                for ( j = 0; j < col && !t[j]; j++ );
                   1593:                /* register the position of the head term */
                   1594:                ind[i] = j;
                   1595:                inv = invm(t[j],md);
                   1596:                for ( k = j; k < col; k++ )
                   1597:                        if ( t[k] )
                   1598:                                DMAR(t[k],inv,0,md,t[k])
                   1599:                for ( l = i-1; l >= 0; l-- ) {
                   1600:                        /* reduce mat[i] by mat[l] */
                   1601:                        if ( hc = t[ind[l]] ) {
                   1602:                                /* mat[i] = mat[i]-hc*mat[l] */
                   1603:                                for ( k = ind[l], hc = md-hc, s = mat[l]+k, tk = t+k;
                   1604:                                        k < col; k++, tk++, s++ )
                   1605:                                        if ( *s )
                   1606:                                                DMAR(*s,hc,*tk,md,*tk)
                   1607:                        }
                   1608:                }
                   1609:        }
                   1610:        /* reduce the spolys */
                   1611:        for ( i = nred; i < row; i++ ) {
                   1612:                t = mat[i];
                   1613:                for ( l = nred-1; l >= 0; l-- ) {
                   1614:                        /* reduce mat[i] by mat[l] */
                   1615:                        if ( hc = t[ind[l]] ) {
                   1616:                                /* mat[i] = mat[i]-hc*mat[l] */
                   1617:                                for ( k = ind[l], hc = md-hc, s = mat[l]+k, tk = t+k;
                   1618:                                        k < col; k++, tk++, s++ )
                   1619:                                        if ( *s )
                   1620:                                                DMAR(*s,hc,*tk,md,*tk)
                   1621:                        }
                   1622:                }
                   1623:        }
                   1624: #endif
                   1625: }
                   1626: /*
                   1627:        mat[i] : reducers (i=0,...,nred-1)
                   1628:        mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
                   1629: */
                   1630:
1.24      noro     1631: void reduce_sp_by_red_mod(int *sp,int **redmat,int *ind,int nred,int col,int md)
1.4       noro     1632: {
                   1633:        int i,j,k,hc,zzz;
1.24      noro     1634:        int *s,*tj;
1.4       noro     1635:
                   1636:        /* reduce the spolys by redmat */
                   1637:        for ( i = nred-1; i >= 0; i-- ) {
                   1638:                /* reduce sp by redmat[i] */
                   1639:                if ( hc = sp[ind[i]] ) {
                   1640:                        /* sp = sp-hc*redmat[i] */
                   1641:                        j = ind[i];
                   1642:                        hc = md-hc;
                   1643:                        s = redmat[i]+j;
                   1644:                        tj = sp+j;
1.16      noro     1645:                        for ( k = col-j; k > 0; k-- ) {
1.4       noro     1646:                                if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
1.15      noro     1647:                        }
                   1648:                }
1.17      noro     1649:        }
                   1650: }
                   1651:
                   1652: /*
1.15      noro     1653:        mat[i] : compressed reducers (i=0,...,nred-1)
                   1654:        mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
                   1655: */
                   1656:
1.24      noro     1657: void red_by_compress(int m,unsigned int *p,unsigned int *r,
                   1658:        unsigned int *ri,unsigned int hc,int len)
1.18      noro     1659: {
1.19      noro     1660:        unsigned int up,lo;
1.18      noro     1661:        unsigned int dmy;
                   1662:        unsigned int *pj;
                   1663:
1.21      noro     1664:        p[*ri] = 0; r++; ri++;
                   1665:        for ( len--; len; len--, r++, ri++ ) {
                   1666:                pj = p+ *ri;
                   1667:                DMA(*r,hc,*pj,up,lo);
1.18      noro     1668:                if ( up ) {
                   1669:                        DSAB(m,up,lo,dmy,*pj);
                   1670:                } else
                   1671:                        *pj = lo;
                   1672:        }
                   1673: }
                   1674:
                   1675: /* p -= hc*r */
                   1676:
1.24      noro     1677: void red_by_vect(int m,unsigned int *p,unsigned int *r,unsigned int hc,int len)
1.18      noro     1678: {
                   1679:        register unsigned int up,lo;
                   1680:        unsigned int dmy;
                   1681:
                   1682:        *p++ = 0; r++; len--;
                   1683:        for ( ; len; len--, r++, p++ )
                   1684:                if ( *r ) {
1.20      noro     1685:                        DMA(*r,hc,*p,up,lo);
1.18      noro     1686:                        if ( up ) {
                   1687:                                DSAB(m,up,lo,dmy,*p);
                   1688:                        } else
                   1689:                                *p = lo;
                   1690:                }
                   1691: }
                   1692:
1.32      noro     1693: void red_by_vect_sf(int m,unsigned int *p,unsigned int *r,unsigned int hc,int len)
                   1694: {
                   1695:        *p++ = 0; r++; len--;
                   1696:        for ( ; len; len--, r++, p++ )
                   1697:                if ( *r )
                   1698:                        *p = _addsf(_mulsf(*r,hc),*p);
                   1699: }
                   1700:
1.21      noro     1701: extern unsigned int **psca;
                   1702:
1.24      noro     1703: void reduce_sp_by_red_mod_compress (int *sp,CDP *redmat,int *ind,
                   1704:        int nred,int col,int md)
1.15      noro     1705: {
1.24      noro     1706:        int i,len;
1.15      noro     1707:        CDP ri;
1.24      noro     1708:        unsigned int hc;
1.18      noro     1709:        unsigned int *usp;
1.15      noro     1710:
1.18      noro     1711:        usp = (unsigned int *)sp;
1.15      noro     1712:        /* reduce the spolys by redmat */
                   1713:        for ( i = nred-1; i >= 0; i-- ) {
                   1714:                /* reduce sp by redmat[i] */
1.18      noro     1715:                usp[ind[i]] %= md;
                   1716:                if ( hc = usp[ind[i]] ) {
1.15      noro     1717:                        /* sp = sp-hc*redmat[i] */
                   1718:                        hc = md-hc;
                   1719:                        ri = redmat[i];
                   1720:                        len = ri->len;
1.21      noro     1721:                        red_by_compress(md,usp,psca[ri->psindex],ri->body,hc,len);
1.4       noro     1722:                }
                   1723:        }
1.18      noro     1724:        for ( i = 0; i < col; i++ )
1.24      noro     1725:                if ( usp[i] >= (unsigned int)md )
1.18      noro     1726:                        usp[i] %= md;
1.4       noro     1727: }
                   1728:
                   1729: #define ONE_STEP2  if ( zzz = *pk ) { DMAR(zzz,a,*tk,md,*tk) } pk++; tk++;
                   1730:
1.24      noro     1731: int generic_gauss_elim_mod(int **mat0,int row,int col,int md,int *colstat)
1.1       noro     1732: {
1.24      noro     1733:        int i,j,k,l,inv,a,rank;
                   1734:        unsigned int *t,*pivot,*pk;
1.18      noro     1735:        unsigned int **mat;
1.1       noro     1736:
1.18      noro     1737:        mat = (unsigned int **)mat0;
1.1       noro     1738:        for ( rank = 0, j = 0; j < col; j++ ) {
1.18      noro     1739:                for ( i = rank; i < row; i++ )
                   1740:                        mat[i][j] %= md;
                   1741:                for ( i = rank; i < row; i++ )
                   1742:                        if ( mat[i][j] )
                   1743:                                break;
1.1       noro     1744:                if ( i == row ) {
                   1745:                        colstat[j] = 0;
                   1746:                        continue;
                   1747:                } else
                   1748:                        colstat[j] = 1;
                   1749:                if ( i != rank ) {
                   1750:                        t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   1751:                }
                   1752:                pivot = mat[rank];
                   1753:                inv = invm(pivot[j],md);
1.4       noro     1754:                for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   1755:                        if ( *pk ) {
1.24      noro     1756:                                if ( *pk >= (unsigned int)md )
1.18      noro     1757:                                        *pk %= md;
1.4       noro     1758:                                DMAR(*pk,inv,0,md,*pk)
1.1       noro     1759:                        }
                   1760:                for ( i = rank+1; i < row; i++ ) {
                   1761:                        t = mat[i];
1.18      noro     1762:                        if ( a = t[j] )
                   1763:                                red_by_vect(md,t+j,pivot+j,md-a,col-j);
1.1       noro     1764:                }
                   1765:                rank++;
                   1766:        }
                   1767:        for ( j = col-1, l = rank-1; j >= 0; j-- )
                   1768:                if ( colstat[j] ) {
                   1769:                        pivot = mat[l];
                   1770:                        for ( i = 0; i < l; i++ ) {
                   1771:                                t = mat[i];
1.18      noro     1772:                                t[j] %= md;
                   1773:                                if ( a = t[j] )
                   1774:                                        red_by_vect(md,t+j,pivot+j,md-a,col-j);
1.1       noro     1775:                        }
                   1776:                        l--;
1.18      noro     1777:                }
                   1778:        for ( j = 0, l = 0; l < rank; j++ )
                   1779:                if ( colstat[j] ) {
                   1780:                        t = mat[l];
                   1781:                        for ( k = j; k < col; k++ )
1.24      noro     1782:                                if ( t[k] >= (unsigned int)md )
1.18      noro     1783:                                        t[k] %= md;
                   1784:                        l++;
1.32      noro     1785:                }
                   1786:        return rank;
                   1787: }
                   1788:
                   1789: int generic_gauss_elim_sf(int **mat0,int row,int col,int md,int *colstat)
                   1790: {
                   1791:        int i,j,k,l,inv,a,rank;
                   1792:        unsigned int *t,*pivot,*pk;
                   1793:        unsigned int **mat;
                   1794:
                   1795:        mat = (unsigned int **)mat0;
                   1796:        for ( rank = 0, j = 0; j < col; j++ ) {
                   1797:                for ( i = rank; i < row; i++ )
                   1798:                        if ( mat[i][j] )
                   1799:                                break;
                   1800:                if ( i == row ) {
                   1801:                        colstat[j] = 0;
                   1802:                        continue;
                   1803:                } else
                   1804:                        colstat[j] = 1;
                   1805:                if ( i != rank ) {
                   1806:                        t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   1807:                }
                   1808:                pivot = mat[rank];
                   1809:                inv = _invsf(pivot[j]);
                   1810:                for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   1811:                        if ( *pk )
                   1812:                                *pk = _mulsf(*pk,inv);
                   1813:                for ( i = rank+1; i < row; i++ ) {
                   1814:                        t = mat[i];
                   1815:                        if ( a = t[j] )
                   1816:                                red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   1817:                }
                   1818:                rank++;
                   1819:        }
                   1820:        for ( j = col-1, l = rank-1; j >= 0; j-- )
                   1821:                if ( colstat[j] ) {
                   1822:                        pivot = mat[l];
                   1823:                        for ( i = 0; i < l; i++ ) {
                   1824:                                t = mat[i];
                   1825:                                if ( a = t[j] )
                   1826:                                        red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   1827:                        }
                   1828:                        l--;
1.1       noro     1829:                }
                   1830:        return rank;
                   1831: }
                   1832:
                   1833: /* LU decomposition; a[i][i] = 1/U[i][i] */
                   1834:
1.24      noro     1835: int lu_gfmmat(GFMMAT mat,unsigned int md,int *perm)
1.1       noro     1836: {
                   1837:        int row,col;
1.24      noro     1838:        int i,j,k;
1.1       noro     1839:        unsigned int *t,*pivot;
                   1840:        unsigned int **a;
                   1841:        unsigned int inv,m;
                   1842:
                   1843:        row = mat->row; col = mat->col;
                   1844:        a = mat->body;
                   1845:        bzero(perm,row*sizeof(int));
                   1846:
                   1847:        for ( i = 0; i < row; i++ )
                   1848:                perm[i] = i;
                   1849:        for ( k = 0; k < col; k++ ) {
                   1850:                for ( i = k; i < row && !a[i][k]; i++ );
                   1851:                if ( i == row )
                   1852:                        return 0;
                   1853:                if ( i != k ) {
                   1854:                        j = perm[i]; perm[i] = perm[k]; perm[k] = j;
                   1855:                        t = a[i]; a[i] = a[k]; a[k] = t;
                   1856:                }
                   1857:                pivot = a[k];
                   1858:                pivot[k] = inv = invm(pivot[k],md);
                   1859:                for ( i = k+1; i < row; i++ ) {
                   1860:                        t = a[i];
                   1861:                        if ( m = t[k] ) {
                   1862:                                DMAR(inv,m,0,md,t[k])
                   1863:                                for ( j = k+1, m = md - t[k]; j < col; j++ )
                   1864:                                        if ( pivot[j] ) {
1.8       noro     1865:                                                unsigned int tj;
                   1866:
                   1867:                                                DMAR(m,pivot[j],t[j],md,tj)
                   1868:                                                t[j] = tj;
1.1       noro     1869:                                        }
                   1870:                        }
                   1871:                }
                   1872:        }
                   1873:        return 1;
                   1874: }
                   1875:
1.3       noro     1876: /*
                   1877:  Input
                   1878:        a: a row x col matrix
                   1879:        md : a modulus
                   1880:
                   1881:  Output:
                   1882:        return : d = the rank of mat
                   1883:        a[0..(d-1)][0..(d-1)] : LU decomposition (a[i][i] = 1/U[i][i])
                   1884:        rinfo: array of length row
                   1885:        cinfo: array of length col
                   1886:     i-th row in new a <-> rinfo[i]-th row in old a
                   1887:        cinfo[j]=1 <=> j-th column is contained in the LU decomp.
                   1888: */
                   1889:
1.24      noro     1890: int find_lhs_and_lu_mod(unsigned int **a,int row,int col,
                   1891:        unsigned int md,int **rinfo,int **cinfo)
1.3       noro     1892: {
1.24      noro     1893:        int i,j,k,d;
1.3       noro     1894:        int *rp,*cp;
                   1895:        unsigned int *t,*pivot;
                   1896:        unsigned int inv,m;
                   1897:
                   1898:        *rinfo = rp = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   1899:        *cinfo = cp = (int *)MALLOC_ATOMIC(col*sizeof(int));
                   1900:        for ( i = 0; i < row; i++ )
                   1901:                rp[i] = i;
                   1902:        for ( k = 0, d = 0; k < col; k++ ) {
                   1903:                for ( i = d; i < row && !a[i][k]; i++ );
                   1904:                if ( i == row ) {
                   1905:                        cp[k] = 0;
                   1906:                        continue;
                   1907:                } else
                   1908:                        cp[k] = 1;
                   1909:                if ( i != d ) {
                   1910:                        j = rp[i]; rp[i] = rp[d]; rp[d] = j;
                   1911:                        t = a[i]; a[i] = a[d]; a[d] = t;
                   1912:                }
                   1913:                pivot = a[d];
                   1914:                pivot[k] = inv = invm(pivot[k],md);
                   1915:                for ( i = d+1; i < row; i++ ) {
                   1916:                        t = a[i];
                   1917:                        if ( m = t[k] ) {
                   1918:                                DMAR(inv,m,0,md,t[k])
                   1919:                                for ( j = k+1, m = md - t[k]; j < col; j++ )
                   1920:                                        if ( pivot[j] ) {
1.8       noro     1921:                                                unsigned int tj;
                   1922:                                                DMAR(m,pivot[j],t[j],md,tj)
                   1923:                                                t[j] = tj;
1.3       noro     1924:                                        }
                   1925:                        }
                   1926:                }
                   1927:                d++;
                   1928:        }
                   1929:        return d;
                   1930: }
                   1931:
                   1932: /*
                   1933:   Input
                   1934:        a : n x n matrix; a result of LU-decomposition
                   1935:        md : modulus
                   1936:        b : n x l matrix
                   1937:  Output
                   1938:        b = a^(-1)b
                   1939:  */
                   1940:
1.44      noro     1941: void solve_by_lu_mod(int **a,int n,int md,int **b,int l,int normalize)
1.3       noro     1942: {
                   1943:        unsigned int *y,*c;
                   1944:        int i,j,k;
                   1945:        unsigned int t,m,m2;
                   1946:
                   1947:        y = (int *)MALLOC_ATOMIC(n*sizeof(int));
                   1948:        c = (int *)MALLOC_ATOMIC(n*sizeof(int));
                   1949:        m2 = md>>1;
                   1950:        for ( k = 0; k < l; k++ ) {
                   1951:                /* copy b[.][k] to c */
                   1952:                for ( i = 0; i < n; i++ )
                   1953:                        c[i] = (unsigned int)b[i][k];
                   1954:                /* solve Ly=c */
                   1955:                for ( i = 0; i < n; i++ ) {
                   1956:                        for ( t = c[i], j = 0; j < i; j++ )
                   1957:                                if ( a[i][j] ) {
                   1958:                                        m = md - a[i][j];
                   1959:                                        DMAR(m,y[j],t,md,t)
                   1960:                                }
                   1961:                        y[i] = t;
                   1962:                }
                   1963:                /* solve Uc=y */
                   1964:                for ( i = n-1; i >= 0; i-- ) {
                   1965:                        for ( t = y[i], j =i+1; j < n; j++ )
                   1966:                                if ( a[i][j] ) {
                   1967:                                        m = md - a[i][j];
                   1968:                                        DMAR(m,c[j],t,md,t)
                   1969:                                }
                   1970:                        /* a[i][i] = 1/U[i][i] */
                   1971:                        DMAR(t,a[i][i],0,md,c[i])
                   1972:                }
                   1973:                /* copy c to b[.][k] with normalization */
1.44      noro     1974:                if ( normalize )
                   1975:                        for ( i = 0; i < n; i++ )
                   1976:                                b[i][k] = (int)(c[i]>m2 ? c[i]-md : c[i]);
                   1977:                else
                   1978:                        for ( i = 0; i < n; i++ )
                   1979:                                b[i][k] = c[i];
1.3       noro     1980:        }
                   1981: }
                   1982:
1.24      noro     1983: void Pleqm1(NODE arg,VECT *rp)
1.1       noro     1984: {
                   1985:        MAT m;
                   1986:        VECT vect;
                   1987:        pointer **mat;
                   1988:        Q *v;
                   1989:        Q q;
                   1990:        int **wmat;
                   1991:        int md,i,j,row,col,t,n,status;
                   1992:
                   1993:        asir_assert(ARG0(arg),O_MAT,"leqm1");
                   1994:        asir_assert(ARG1(arg),O_N,"leqm1");
                   1995:        m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
                   1996:        row = m->row; col = m->col; mat = m->body;
                   1997:        wmat = (int **)almat(row,col);
                   1998:        for ( i = 0; i < row; i++ )
                   1999:                for ( j = 0; j < col; j++ )
                   2000:                        if ( q = (Q)mat[i][j] ) {
                   2001:                                t = rem(NM(q),md);
                   2002:                                if ( SGN(q) < 0 )
                   2003:                                        t = (md - t) % md;
                   2004:                                wmat[i][j] = t;
                   2005:                        } else
                   2006:                                wmat[i][j] = 0;
                   2007:        status = gauss_elim_mod1(wmat,row,col,md);
                   2008:        if ( status < 0 )
                   2009:                *rp = 0;
                   2010:        else if ( status > 0 )
                   2011:                *rp = (VECT)ONE;
                   2012:        else {
                   2013:                n = col - 1;
                   2014:                MKVECT(vect,n);
                   2015:                for ( i = 0, v = (Q *)vect->body; i < n; i++ ) {
                   2016:                        t = (md-wmat[i][n])%md; STOQ(t,v[i]);
                   2017:                }
                   2018:                *rp = vect;
                   2019:        }
                   2020: }
                   2021:
1.24      noro     2022: int gauss_elim_mod1(int **mat,int row,int col,int md)
1.1       noro     2023: {
                   2024:        int i,j,k,inv,a,n;
                   2025:        int *t,*pivot;
                   2026:
                   2027:        n = col - 1;
                   2028:        for ( j = 0; j < n; j++ ) {
                   2029:                for ( i = j; i < row && !mat[i][j]; i++ );
                   2030:                if ( i == row )
                   2031:                        return 1;
                   2032:                if ( i != j ) {
                   2033:                        t = mat[i]; mat[i] = mat[j]; mat[j] = t;
                   2034:                }
                   2035:                pivot = mat[j];
                   2036:                inv = invm(pivot[j],md);
                   2037:                for ( k = j; k <= n; k++ )
                   2038:                        pivot[k] = dmar(pivot[k],inv,0,md);
                   2039:                for ( i = j+1; i < row; i++ ) {
                   2040:                        t = mat[i];
                   2041:                        if ( i != j && (a = t[j]) )
                   2042:                                for ( k = j, a = md - a; k <= n; k++ )
                   2043:                                        t[k] = dmar(pivot[k],a,t[k],md);
                   2044:                }
                   2045:        }
                   2046:        for ( i = n; i < row && !mat[i][n]; i++ );
                   2047:        if ( i == row ) {
                   2048:                for ( j = n-1; j >= 0; j-- ) {
                   2049:                        for ( i = j-1, a = (md-mat[j][n])%md; i >= 0; i-- ) {
                   2050:                                mat[i][n] = dmar(mat[i][j],a,mat[i][n],md);
                   2051:                                mat[i][j] = 0;
                   2052:                        }
                   2053:                }
                   2054:                return 0;
                   2055:        } else
                   2056:                return -1;
                   2057: }
                   2058:
1.24      noro     2059: void Pgeninvm(NODE arg,LIST *rp)
1.1       noro     2060: {
                   2061:        MAT m;
                   2062:        pointer **mat;
                   2063:        Q **tmat;
                   2064:        Q q;
                   2065:        unsigned int **wmat;
                   2066:        int md,i,j,row,col,t,status;
                   2067:        MAT mat1,mat2;
                   2068:        NODE node1,node2;
                   2069:
                   2070:        asir_assert(ARG0(arg),O_MAT,"leqm1");
                   2071:        asir_assert(ARG1(arg),O_N,"leqm1");
                   2072:        m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
                   2073:        row = m->row; col = m->col; mat = m->body;
                   2074:        wmat = (unsigned int **)almat(row,col+row);
                   2075:        for ( i = 0; i < row; i++ ) {
                   2076:                bzero((char *)wmat[i],(col+row)*sizeof(int));
                   2077:                for ( j = 0; j < col; j++ )
                   2078:                        if ( q = (Q)mat[i][j] ) {
                   2079:                                t = rem(NM(q),md);
                   2080:                                if ( SGN(q) < 0 )
                   2081:                                        t = (md - t) % md;
                   2082:                                wmat[i][j] = t;
                   2083:                        }
                   2084:                wmat[i][col+i] = 1;
                   2085:        }
                   2086:        status = gauss_elim_geninv_mod(wmat,row,col,md);
                   2087:        if ( status > 0 )
                   2088:                *rp = 0;
                   2089:        else {
                   2090:                MKMAT(mat1,col,row); MKMAT(mat2,row-col,row);
                   2091:                for ( i = 0, tmat = (Q **)mat1->body; i < col; i++ )
                   2092:                        for ( j = 0; j < row; j++ )
1.24      noro     2093:                                UTOQ(wmat[i][j+col],tmat[i][j]);
1.1       noro     2094:                for ( tmat = (Q **)mat2->body; i < row; i++ )
                   2095:                        for ( j = 0; j < row; j++ )
1.24      noro     2096:                                UTOQ(wmat[i][j+col],tmat[i-col][j]);
1.1       noro     2097:                MKNODE(node2,mat2,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
                   2098:        }
                   2099: }
                   2100:
1.24      noro     2101: int gauss_elim_geninv_mod(unsigned int **mat,int row,int col,int md)
1.1       noro     2102: {
                   2103:        int i,j,k,inv,a,n,m;
                   2104:        unsigned int *t,*pivot;
                   2105:
                   2106:        n = col; m = row+col;
                   2107:        for ( j = 0; j < n; j++ ) {
                   2108:                for ( i = j; i < row && !mat[i][j]; i++ );
                   2109:                if ( i == row )
                   2110:                        return 1;
                   2111:                if ( i != j ) {
                   2112:                        t = mat[i]; mat[i] = mat[j]; mat[j] = t;
                   2113:                }
                   2114:                pivot = mat[j];
                   2115:                inv = invm(pivot[j],md);
                   2116:                for ( k = j; k < m; k++ )
                   2117:                        pivot[k] = dmar(pivot[k],inv,0,md);
                   2118:                for ( i = j+1; i < row; i++ ) {
                   2119:                        t = mat[i];
                   2120:                        if ( a = t[j] )
                   2121:                                for ( k = j, a = md - a; k < m; k++ )
                   2122:                                        t[k] = dmar(pivot[k],a,t[k],md);
                   2123:                }
                   2124:        }
                   2125:        for ( j = n-1; j >= 0; j-- ) {
                   2126:                pivot = mat[j];
                   2127:                for ( i = j-1; i >= 0; i-- ) {
                   2128:                        t = mat[i];
                   2129:                        if ( a = t[j] )
                   2130:                                for ( k = j, a = md - a; k < m; k++ )
                   2131:                                        t[k] = dmar(pivot[k],a,t[k],md);
                   2132:                }
                   2133:        }
                   2134:        return 0;
                   2135: }
                   2136:
1.24      noro     2137: void Psolve_by_lu_gfmmat(NODE arg,VECT *rp)
1.1       noro     2138: {
                   2139:        GFMMAT lu;
                   2140:        Q *perm,*rhs,*v;
                   2141:        int n,i;
                   2142:        unsigned int md;
                   2143:        unsigned int *b,*sol;
                   2144:        VECT r;
                   2145:
                   2146:        lu = (GFMMAT)ARG0(arg);
                   2147:        perm = (Q *)BDY((VECT)ARG1(arg));
                   2148:        rhs = (Q *)BDY((VECT)ARG2(arg));
                   2149:        md = (unsigned int)QTOS((Q)ARG3(arg));
                   2150:        n = lu->col;
                   2151:        b = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
                   2152:        sol = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
                   2153:        for ( i = 0; i < n; i++ )
                   2154:                b[i] = QTOS(rhs[QTOS(perm[i])]);
                   2155:        solve_by_lu_gfmmat(lu,md,b,sol);
                   2156:        MKVECT(r,n);
                   2157:        for ( i = 0, v = (Q *)r->body; i < n; i++ )
1.24      noro     2158:                        UTOQ(sol[i],v[i]);
1.1       noro     2159:        *rp = r;
                   2160: }
                   2161:
1.24      noro     2162: void solve_by_lu_gfmmat(GFMMAT lu,unsigned int md,
                   2163:        unsigned int *b,unsigned int *x)
1.1       noro     2164: {
                   2165:        int n;
                   2166:        unsigned int **a;
                   2167:        unsigned int *y;
                   2168:        int i,j;
                   2169:        unsigned int t,m;
                   2170:
                   2171:        n = lu->col;
                   2172:        a = lu->body;
                   2173:        y = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
                   2174:        /* solve Ly=b */
                   2175:        for ( i = 0; i < n; i++ ) {
                   2176:                for ( t = b[i], j = 0; j < i; j++ )
                   2177:                        if ( a[i][j] ) {
                   2178:                                m = md - a[i][j];
                   2179:                                DMAR(m,y[j],t,md,t)
                   2180:                        }
                   2181:                y[i] = t;
                   2182:        }
                   2183:        /* solve Ux=y */
                   2184:        for ( i = n-1; i >= 0; i-- ) {
                   2185:                for ( t = y[i], j =i+1; j < n; j++ )
                   2186:                        if ( a[i][j] ) {
                   2187:                                m = md - a[i][j];
                   2188:                                DMAR(m,x[j],t,md,t)
                   2189:                        }
                   2190:                /* a[i][i] = 1/U[i][i] */
                   2191:                DMAR(t,a[i][i],0,md,x[i])
                   2192:        }
                   2193: }
                   2194:
1.24      noro     2195: void Plu_gfmmat(NODE arg,LIST *rp)
1.1       noro     2196: {
                   2197:        MAT m;
                   2198:        GFMMAT mm;
                   2199:        unsigned int md;
                   2200:        int i,row,col,status;
                   2201:        int *iperm;
                   2202:        Q *v;
                   2203:        VECT perm;
                   2204:        NODE n0;
                   2205:
                   2206:        asir_assert(ARG0(arg),O_MAT,"mat_to_gfmmat");
                   2207:        asir_assert(ARG1(arg),O_N,"mat_to_gfmmat");
                   2208:        m = (MAT)ARG0(arg); md = (unsigned int)QTOS((Q)ARG1(arg));
                   2209:        mat_to_gfmmat(m,md,&mm);
                   2210:        row = m->row;
                   2211:        col = m->col;
                   2212:        iperm = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   2213:        status = lu_gfmmat(mm,md,iperm);
                   2214:        if ( !status )
                   2215:                n0 = 0;
                   2216:        else {
                   2217:                MKVECT(perm,row);
                   2218:                for ( i = 0, v = (Q *)perm->body; i < row; i++ )
                   2219:                        STOQ(iperm[i],v[i]);
                   2220:                n0 = mknode(2,mm,perm);
                   2221:        }
                   2222:        MKLIST(*rp,n0);
                   2223: }
                   2224:
1.24      noro     2225: void Pmat_to_gfmmat(NODE arg,GFMMAT *rp)
1.1       noro     2226: {
                   2227:        MAT m;
                   2228:        unsigned int md;
                   2229:
                   2230:        asir_assert(ARG0(arg),O_MAT,"mat_to_gfmmat");
                   2231:        asir_assert(ARG1(arg),O_N,"mat_to_gfmmat");
                   2232:        m = (MAT)ARG0(arg); md = (unsigned int)QTOS((Q)ARG1(arg));
                   2233:        mat_to_gfmmat(m,md,rp);
                   2234: }
                   2235:
1.24      noro     2236: void mat_to_gfmmat(MAT m,unsigned int md,GFMMAT *rp)
1.1       noro     2237: {
                   2238:        unsigned int **wmat;
                   2239:        unsigned int t;
                   2240:        Q **mat;
                   2241:        Q q;
                   2242:        int i,j,row,col;
                   2243:
                   2244:        row = m->row; col = m->col; mat = (Q **)m->body;
                   2245:        wmat = (unsigned int **)almat(row,col);
                   2246:        for ( i = 0; i < row; i++ ) {
                   2247:                bzero((char *)wmat[i],col*sizeof(unsigned int));
                   2248:                for ( j = 0; j < col; j++ )
                   2249:                        if ( q = mat[i][j] ) {
                   2250:                                t = (unsigned int)rem(NM(q),md);
                   2251:                                if ( SGN(q) < 0 )
                   2252:                                        t = (md - t) % md;
                   2253:                                wmat[i][j] = t;
                   2254:                        }
                   2255:        }
                   2256:        TOGFMMAT(row,col,wmat,*rp);
                   2257: }
                   2258:
1.27      noro     2259: void Pgeninvm_swap(arg,rp)
                   2260: NODE arg;
                   2261: LIST *rp;
1.1       noro     2262: {
                   2263:        MAT m;
                   2264:        pointer **mat;
                   2265:        Q **tmat;
                   2266:        Q *tvect;
                   2267:        Q q;
                   2268:        unsigned int **wmat,**invmat;
                   2269:        int *index;
                   2270:        unsigned int t,md;
                   2271:        int i,j,row,col,status;
                   2272:        MAT mat1;
                   2273:        VECT vect1;
                   2274:        NODE node1,node2;
                   2275:
                   2276:        asir_assert(ARG0(arg),O_MAT,"geninvm_swap");
                   2277:        asir_assert(ARG1(arg),O_N,"geninvm_swap");
                   2278:        m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
                   2279:        row = m->row; col = m->col; mat = m->body;
                   2280:        wmat = (unsigned int **)almat(row,col+row);
                   2281:        for ( i = 0; i < row; i++ ) {
                   2282:                bzero((char *)wmat[i],(col+row)*sizeof(int));
                   2283:                for ( j = 0; j < col; j++ )
                   2284:                        if ( q = (Q)mat[i][j] ) {
                   2285:                                t = (unsigned int)rem(NM(q),md);
                   2286:                                if ( SGN(q) < 0 )
                   2287:                                        t = (md - t) % md;
                   2288:                                wmat[i][j] = t;
                   2289:                        }
                   2290:                wmat[i][col+i] = 1;
                   2291:        }
                   2292:        status = gauss_elim_geninv_mod_swap(wmat,row,col,md,&invmat,&index);
                   2293:        if ( status > 0 )
                   2294:                *rp = 0;
                   2295:        else {
                   2296:                MKMAT(mat1,col,col);
                   2297:                for ( i = 0, tmat = (Q **)mat1->body; i < col; i++ )
                   2298:                        for ( j = 0; j < col; j++ )
                   2299:                                UTOQ(invmat[i][j],tmat[i][j]);
                   2300:                MKVECT(vect1,row);
                   2301:                for ( i = 0, tvect = (Q *)vect1->body; i < row; i++ )
                   2302:                        STOQ(index[i],tvect[i]);
                   2303:                MKNODE(node2,vect1,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
                   2304:        }
                   2305: }
                   2306:
1.27      noro     2307: gauss_elim_geninv_mod_swap(mat,row,col,md,invmatp,indexp)
                   2308: unsigned int **mat;
                   2309: int row,col;
                   2310: unsigned int md;
                   2311: unsigned int ***invmatp;
                   2312: int **indexp;
1.1       noro     2313: {
                   2314:        int i,j,k,inv,a,n,m;
                   2315:        unsigned int *t,*pivot,*s;
                   2316:        int *index;
                   2317:        unsigned int **invmat;
                   2318:
                   2319:        n = col; m = row+col;
                   2320:        *indexp = index = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   2321:        for ( i = 0; i < row; i++ )
                   2322:                index[i] = i;
                   2323:        for ( j = 0; j < n; j++ ) {
                   2324:                for ( i = j; i < row && !mat[i][j]; i++ );
                   2325:                if ( i == row ) {
                   2326:                        *indexp = 0; *invmatp = 0; return 1;
                   2327:                }
                   2328:                if ( i != j ) {
                   2329:                        t = mat[i]; mat[i] = mat[j]; mat[j] = t;
                   2330:                        k = index[i]; index[i] = index[j]; index[j] = k;
                   2331:                }
                   2332:                pivot = mat[j];
                   2333:                inv = (unsigned int)invm(pivot[j],md);
                   2334:                for ( k = j; k < m; k++ )
                   2335:                        if ( pivot[k] )
                   2336:                                pivot[k] = (unsigned int)dmar(pivot[k],inv,0,md);
                   2337:                for ( i = j+1; i < row; i++ ) {
                   2338:                        t = mat[i];
                   2339:                        if ( a = t[j] )
                   2340:                                for ( k = j, a = md - a; k < m; k++ )
                   2341:                                        if ( pivot[k] )
                   2342:                                                t[k] = dmar(pivot[k],a,t[k],md);
                   2343:                }
                   2344:        }
                   2345:        for ( j = n-1; j >= 0; j-- ) {
                   2346:                pivot = mat[j];
                   2347:                for ( i = j-1; i >= 0; i-- ) {
                   2348:                        t = mat[i];
                   2349:                        if ( a = t[j] )
                   2350:                                for ( k = j, a = md - a; k < m; k++ )
                   2351:                                        if ( pivot[k] )
                   2352:                                                t[k] = dmar(pivot[k],a,t[k],md);
                   2353:                }
                   2354:        }
                   2355:        *invmatp = invmat = (unsigned int **)almat(col,col);
1.27      noro     2356:        for ( i = 0; i < col; i++ )
                   2357:                for ( j = 0, s = invmat[i], t = mat[i]; j < col; j++ )
                   2358:                        s[j] = t[col+index[j]];
                   2359:        return 0;
                   2360: }
                   2361:
                   2362: void Pgeninv_sf_swap(NODE arg,LIST *rp)
                   2363: {
                   2364:        MAT m;
                   2365:        GFS **mat,**tmat;
                   2366:        Q *tvect;
                   2367:        GFS q;
                   2368:        int **wmat,**invmat;
                   2369:        int *index;
                   2370:        unsigned int t;
                   2371:        int i,j,row,col,status;
                   2372:        MAT mat1;
                   2373:        VECT vect1;
                   2374:        NODE node1,node2;
                   2375:
                   2376:        asir_assert(ARG0(arg),O_MAT,"geninv_sf_swap");
                   2377:        m = (MAT)ARG0(arg);
                   2378:        row = m->row; col = m->col; mat = (GFS **)m->body;
                   2379:        wmat = (int **)almat(row,col+row);
                   2380:        for ( i = 0; i < row; i++ ) {
                   2381:                bzero((char *)wmat[i],(col+row)*sizeof(int));
                   2382:                for ( j = 0; j < col; j++ )
                   2383:                        if ( q = (GFS)mat[i][j] )
                   2384:                                wmat[i][j] = FTOIF(CONT(q));
                   2385:                wmat[i][col+i] = _onesf();
                   2386:        }
                   2387:        status = gauss_elim_geninv_sf_swap(wmat,row,col,&invmat,&index);
                   2388:        if ( status > 0 )
                   2389:                *rp = 0;
                   2390:        else {
                   2391:                MKMAT(mat1,col,col);
                   2392:                for ( i = 0, tmat = (GFS **)mat1->body; i < col; i++ )
                   2393:                        for ( j = 0; j < col; j++ )
                   2394:                                if ( t = invmat[i][j] ) {
                   2395:                                        MKGFS(IFTOF(t),tmat[i][j]);
                   2396:                                }
                   2397:                MKVECT(vect1,row);
                   2398:                for ( i = 0, tvect = (Q *)vect1->body; i < row; i++ )
                   2399:                        STOQ(index[i],tvect[i]);
                   2400:                MKNODE(node2,vect1,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
                   2401:        }
                   2402: }
                   2403:
                   2404: int gauss_elim_geninv_sf_swap(int **mat,int row,int col,
                   2405:        int ***invmatp,int **indexp)
                   2406: {
                   2407:        int i,j,k,inv,a,n,m,u;
                   2408:        int *t,*pivot,*s;
                   2409:        int *index;
                   2410:        int **invmat;
                   2411:
                   2412:        n = col; m = row+col;
                   2413:        *indexp = index = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   2414:        for ( i = 0; i < row; i++ )
                   2415:                index[i] = i;
                   2416:        for ( j = 0; j < n; j++ ) {
                   2417:                for ( i = j; i < row && !mat[i][j]; i++ );
                   2418:                if ( i == row ) {
                   2419:                        *indexp = 0; *invmatp = 0; return 1;
                   2420:                }
                   2421:                if ( i != j ) {
                   2422:                        t = mat[i]; mat[i] = mat[j]; mat[j] = t;
                   2423:                        k = index[i]; index[i] = index[j]; index[j] = k;
                   2424:                }
                   2425:                pivot = mat[j];
                   2426:                inv = _invsf(pivot[j]);
                   2427:                for ( k = j; k < m; k++ )
                   2428:                        if ( pivot[k] )
                   2429:                                pivot[k] = _mulsf(pivot[k],inv);
                   2430:                for ( i = j+1; i < row; i++ ) {
                   2431:                        t = mat[i];
                   2432:                        if ( a = t[j] )
                   2433:                                for ( k = j, a = _chsgnsf(a); k < m; k++ )
                   2434:                                        if ( pivot[k] ) {
                   2435:                                                u = _mulsf(pivot[k],a);
                   2436:                                                t[k] = _addsf(u,t[k]);
                   2437:                                        }
                   2438:                }
                   2439:        }
                   2440:        for ( j = n-1; j >= 0; j-- ) {
                   2441:                pivot = mat[j];
                   2442:                for ( i = j-1; i >= 0; i-- ) {
                   2443:                        t = mat[i];
                   2444:                        if ( a = t[j] )
                   2445:                                for ( k = j, a = _chsgnsf(a); k < m; k++ )
                   2446:                                        if ( pivot[k] ) {
                   2447:                                                u = _mulsf(pivot[k],a);
                   2448:                                                t[k] = _addsf(u,t[k]);
                   2449:                                        }
                   2450:                }
                   2451:        }
                   2452:        *invmatp = invmat = (int **)almat(col,col);
1.1       noro     2453:        for ( i = 0; i < col; i++ )
                   2454:                for ( j = 0, s = invmat[i], t = mat[i]; j < col; j++ )
                   2455:                        s[j] = t[col+index[j]];
                   2456:        return 0;
                   2457: }
                   2458:
                   2459: void _addn(N,N,N);
                   2460: int _subn(N,N,N);
                   2461: void _muln(N,N,N);
                   2462:
1.24      noro     2463: void inner_product_int(Q *a,Q *b,int n,Q *r)
1.1       noro     2464: {
                   2465:        int la,lb,i;
                   2466:        int sgn,sgn1;
                   2467:        N wm,wma,sum,t;
                   2468:
                   2469:        for ( la = lb = 0, i = 0; i < n; i++ ) {
                   2470:                if ( a[i] )
                   2471:                        if ( DN(a[i]) )
                   2472:                                error("inner_product_int : invalid argument");
                   2473:                        else
                   2474:                                la = MAX(PL(NM(a[i])),la);
                   2475:                if ( b[i] )
                   2476:                        if ( DN(b[i]) )
                   2477:                                error("inner_product_int : invalid argument");
                   2478:                        else
                   2479:                                lb = MAX(PL(NM(b[i])),lb);
                   2480:        }
                   2481:        sgn = 0;
                   2482:        sum= NALLOC(la+lb+2);
                   2483:        bzero((char *)sum,(la+lb+3)*sizeof(unsigned int));
                   2484:        wm = NALLOC(la+lb+2);
                   2485:        wma = NALLOC(la+lb+2);
                   2486:        for ( i = 0; i < n; i++ ) {
                   2487:                if ( !a[i] || !b[i] )
                   2488:                        continue;
                   2489:                _muln(NM(a[i]),NM(b[i]),wm);
                   2490:                sgn1 = SGN(a[i])*SGN(b[i]);
                   2491:                if ( !sgn ) {
                   2492:                        sgn = sgn1;
                   2493:                        t = wm; wm = sum; sum = t;
                   2494:                } else if ( sgn == sgn1 ) {
                   2495:                        _addn(sum,wm,wma);
                   2496:                        if ( !PL(wma) )
                   2497:                                sgn = 0;
                   2498:                        t = wma; wma = sum; sum = t;
                   2499:                } else {
                   2500:                        /* sgn*sum+sgn1*wm = sgn*(sum-wm) */
                   2501:                        sgn *= _subn(sum,wm,wma);
                   2502:                        t = wma; wma = sum; sum = t;
                   2503:                }
                   2504:        }
                   2505:        GC_free(wm);
                   2506:        GC_free(wma);
                   2507:        if ( !sgn ) {
                   2508:                GC_free(sum);
                   2509:                *r = 0;
                   2510:        } else
                   2511:                NTOQ(sum,sgn,*r);
                   2512: }
                   2513:
1.3       noro     2514: /* (k,l) element of a*b where a: .x n matrix, b: n x . integer matrix */
                   2515:
1.24      noro     2516: void inner_product_mat_int_mod(Q **a,int **b,int n,int k,int l,Q *r)
1.3       noro     2517: {
                   2518:        int la,lb,i;
                   2519:        int sgn,sgn1;
                   2520:        N wm,wma,sum,t;
                   2521:        Q aki;
                   2522:        int bil,bilsgn;
                   2523:        struct oN tn;
                   2524:
                   2525:        for ( la = 0, i = 0; i < n; i++ ) {
                   2526:                if ( aki = a[k][i] )
                   2527:                        if ( DN(aki) )
                   2528:                                error("inner_product_int : invalid argument");
                   2529:                        else
                   2530:                                la = MAX(PL(NM(aki)),la);
                   2531:        }
                   2532:        lb = 1;
                   2533:        sgn = 0;
                   2534:        sum= NALLOC(la+lb+2);
                   2535:        bzero((char *)sum,(la+lb+3)*sizeof(unsigned int));
                   2536:        wm = NALLOC(la+lb+2);
                   2537:        wma = NALLOC(la+lb+2);
                   2538:        for ( i = 0; i < n; i++ ) {
                   2539:                if ( !(aki = a[k][i]) || !(bil = b[i][l]) )
                   2540:                        continue;
                   2541:                tn.p = 1;
                   2542:                if ( bil > 0 ) {
                   2543:                        tn.b[0] = bil; bilsgn = 1;
                   2544:                } else {
                   2545:                        tn.b[0] = -bil; bilsgn = -1;
                   2546:                }
                   2547:                _muln(NM(aki),&tn,wm);
                   2548:                sgn1 = SGN(aki)*bilsgn;
                   2549:                if ( !sgn ) {
                   2550:                        sgn = sgn1;
                   2551:                        t = wm; wm = sum; sum = t;
                   2552:                } else if ( sgn == sgn1 ) {
                   2553:                        _addn(sum,wm,wma);
                   2554:                        if ( !PL(wma) )
                   2555:                                sgn = 0;
                   2556:                        t = wma; wma = sum; sum = t;
                   2557:                } else {
                   2558:                        /* sgn*sum+sgn1*wm = sgn*(sum-wm) */
                   2559:                        sgn *= _subn(sum,wm,wma);
                   2560:                        t = wma; wma = sum; sum = t;
                   2561:                }
                   2562:        }
                   2563:        GC_free(wm);
                   2564:        GC_free(wma);
                   2565:        if ( !sgn ) {
                   2566:                GC_free(sum);
                   2567:                *r = 0;
                   2568:        } else
                   2569:                NTOQ(sum,sgn,*r);
                   2570: }
                   2571:
1.24      noro     2572: void Pmul_mat_vect_int(NODE arg,VECT *rp)
1.1       noro     2573: {
                   2574:        MAT mat;
                   2575:        VECT vect,r;
                   2576:        int row,col,i;
                   2577:
                   2578:        mat = (MAT)ARG0(arg);
                   2579:        vect = (VECT)ARG1(arg);
                   2580:        row = mat->row;
                   2581:        col = mat->col;
                   2582:        MKVECT(r,row);
1.24      noro     2583:        for ( i = 0; i < row; i++ ) {
                   2584:                inner_product_int((Q *)mat->body[i],(Q *)vect->body,col,(Q *)&r->body[i]);
                   2585:        }
1.1       noro     2586:        *rp = r;
                   2587: }
                   2588:
1.24      noro     2589: void Pnbpoly_up2(NODE arg,GF2N *rp)
1.1       noro     2590: {
                   2591:        int m,type,ret;
                   2592:        UP2 r;
                   2593:
                   2594:        m = QTOS((Q)ARG0(arg));
                   2595:        type = QTOS((Q)ARG1(arg));
                   2596:        ret = generate_ONB_polynomial(&r,m,type);
                   2597:        if ( ret == 0 )
                   2598:                MKGF2N(r,*rp);
                   2599:        else
                   2600:                *rp = 0;
                   2601: }
                   2602:
1.24      noro     2603: void Px962_irredpoly_up2(NODE arg,GF2N *rp)
1.1       noro     2604: {
1.24      noro     2605:        int m,ret,w;
1.1       noro     2606:        GF2N prev;
                   2607:        UP2 r;
                   2608:
                   2609:        m = QTOS((Q)ARG0(arg));
                   2610:        prev = (GF2N)ARG1(arg);
                   2611:        if ( !prev ) {
                   2612:                w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
                   2613:                bzero((char *)r->b,w*sizeof(unsigned int));
                   2614:        } else {
                   2615:                r = prev->body;
                   2616:                if ( degup2(r) != m ) {
                   2617:                        w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
                   2618:                        bzero((char *)r->b,w*sizeof(unsigned int));
                   2619:                }
                   2620:        }
1.24      noro     2621:        ret = _generate_irreducible_polynomial(r,m);
1.1       noro     2622:        if ( ret == 0 )
                   2623:                MKGF2N(r,*rp);
                   2624:        else
                   2625:                *rp = 0;
                   2626: }
                   2627:
1.24      noro     2628: void Pirredpoly_up2(NODE arg,GF2N *rp)
1.1       noro     2629: {
1.24      noro     2630:        int m,ret,w;
1.1       noro     2631:        GF2N prev;
                   2632:        UP2 r;
                   2633:
                   2634:        m = QTOS((Q)ARG0(arg));
                   2635:        prev = (GF2N)ARG1(arg);
                   2636:        if ( !prev ) {
                   2637:                w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
                   2638:                bzero((char *)r->b,w*sizeof(unsigned int));
                   2639:        } else {
                   2640:                r = prev->body;
                   2641:                if ( degup2(r) != m ) {
                   2642:                        w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
                   2643:                        bzero((char *)r->b,w*sizeof(unsigned int));
                   2644:                }
                   2645:        }
1.24      noro     2646:        ret = _generate_good_irreducible_polynomial(r,m);
1.1       noro     2647:        if ( ret == 0 )
                   2648:                MKGF2N(r,*rp);
                   2649:        else
                   2650:                *rp = 0;
                   2651: }
                   2652:
1.26      noro     2653: void Pmat_swap_row_destructive(NODE arg, MAT *m)
                   2654: {
                   2655:        int i1,i2;
                   2656:        pointer *t;
                   2657:        MAT mat;
                   2658:
                   2659:        asir_assert(ARG0(arg),O_MAT,"mat_swap_row_destructive");
                   2660:        asir_assert(ARG1(arg),O_N,"mat_swap_row_destructive");
                   2661:        asir_assert(ARG2(arg),O_N,"mat_swap_row_destructive");
                   2662:        mat = (MAT)ARG0(arg);
                   2663:        i1 = QTOS((Q)ARG1(arg));
                   2664:        i2 = QTOS((Q)ARG2(arg));
                   2665:        if ( i1 < 0 || i2 < 0 || i1 >= mat->row || i2 >= mat->row )
                   2666:                error("mat_swap_row_destructive : Out of range");
                   2667:        t = mat->body[i1];
                   2668:        mat->body[i1] = mat->body[i2];
                   2669:        mat->body[i2] = t;
                   2670:        *m = mat;
                   2671: }
                   2672:
                   2673: void Pmat_swap_col_destructive(NODE arg, MAT *m)
                   2674: {
                   2675:        int j1,j2,i,n;
                   2676:        pointer *mi;
                   2677:        pointer t;
                   2678:        MAT mat;
                   2679:
                   2680:        asir_assert(ARG0(arg),O_MAT,"mat_swap_col_destructive");
                   2681:        asir_assert(ARG1(arg),O_N,"mat_swap_col_destructive");
                   2682:        asir_assert(ARG2(arg),O_N,"mat_swap_col_destructive");
                   2683:        mat = (MAT)ARG0(arg);
                   2684:        j1 = QTOS((Q)ARG1(arg));
                   2685:        j2 = QTOS((Q)ARG2(arg));
                   2686:        if ( j1 < 0 || j2 < 0 || j1 >= mat->col || j2 >= mat->col )
                   2687:                error("mat_swap_col_destructive : Out of range");
                   2688:        n = mat->row;
                   2689:        for ( i = 0; i < n; i++ ) {
                   2690:                mi = mat->body[i];
                   2691:                t = mi[j1]; mi[j1] = mi[j2]; mi[j2] = t;
                   2692:        }
                   2693:        *m = mat;
                   2694: }
1.1       noro     2695: /*
                   2696:  * f = type 'type' normal polynomial of degree m if exists
                   2697:  * IEEE P1363 A.7.2
                   2698:  *
                   2699:  * return value : 0  --- exists
                   2700:  *                1  --- does not exist
                   2701:  *                -1 --- failure (memory allocation error)
                   2702:  */
                   2703:
                   2704: int generate_ONB_polynomial(UP2 *rp,int m,int type)
                   2705: {
                   2706:        int i,r;
                   2707:        int w;
                   2708:        UP2 f,f0,f1,f2,t;
                   2709:
                   2710:        w = (m>>5)+1;
                   2711:        switch ( type ) {
                   2712:                case 1:
                   2713:                        if ( !TypeT_NB_check(m,1) ) return 1;
                   2714:                        NEWUP2(f,w); *rp = f; f->w = w;
                   2715:                        /* set all the bits */
                   2716:                        for ( i = 0; i < w; i++ )
                   2717:                                f->b[i] = 0xffffffff;
                   2718:                        /* mask the top word if necessary */
                   2719:                        if ( r = (m+1)&31 )
                   2720:                                f->b[w-1] &= (1<<r)-1;
                   2721:                        return 0;
                   2722:                        break;
                   2723:                case 2:
                   2724:                        if ( !TypeT_NB_check(m,2) ) return 1;
                   2725:                        NEWUP2(f,w); *rp = f;
                   2726:                        W_NEWUP2(f0,w);
                   2727:                        W_NEWUP2(f1,w);
                   2728:                        W_NEWUP2(f2,w);
                   2729:
                   2730:                        /* recursion for genrating Type II normal polynomial */
                   2731:
                   2732:                        /* f0 = 1, f1 = t+1 */
                   2733:                        f0->w = 1; f0->b[0] = 1;
                   2734:                        f1->w = 1; f1->b[0] = 3;
                   2735:                        for ( i = 2; i <= m; i++ ) {
                   2736:                                /* f2 = t*f1+f0 */
                   2737:                                _bshiftup2(f1,-1,f2);
                   2738:                                _addup2_destructive(f2,f0);
                   2739:                                /* cyclic change of the variables */
                   2740:                                t = f0; f0 = f1; f1 = f2; f2 = t;
                   2741:                        }
                   2742:                        _copyup2(f1,f);
                   2743:                        return 0;
                   2744:                        break;
                   2745:                default:
                   2746:                        return -1;
                   2747:                        break;
                   2748:                }
                   2749: }
                   2750:
                   2751: /*
                   2752:  * f = an irreducible trinomial or pentanomial of degree d 'after' f
                   2753:  * return value : 0  --- exists
                   2754:  *                1  --- does not exist (exhaustion)
                   2755:  */
                   2756:
                   2757: int _generate_irreducible_polynomial(UP2 f,int d)
                   2758: {
                   2759:        int ret,i,j,k,nz,i0,j0,k0;
                   2760:        int w;
                   2761:        unsigned int *fd;
                   2762:
                   2763:        /*
                   2764:         * if f = x^d+x^i+1 then i0 <- i, j0 <- 0, k0 <-0.
                   2765:         * if f = x^d+x^k+x^j+x^i+1 (k>j>i) then i0 <- i, j0 <- j, k0 <-k.
                   2766:         * otherwise i0,j0,k0 is set to 0.
                   2767:         */
                   2768:
                   2769:        fd = f->b;
                   2770:        w = (d>>5)+1;
                   2771:        if ( f->w && (d==degup2(f)) ) {
                   2772:                for ( nz = 0, i = d; i >= 0; i-- )
                   2773:                        if ( fd[i>>5]&(1<<(i&31)) ) nz++;
                   2774:                switch ( nz ) {
                   2775:                        case 3:
                   2776:                                for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
                   2777:                                /* reset i0-th bit */
                   2778:                                fd[i0>>5] &= ~(1<<(i0&31));
                   2779:                                j0 = k0 = 0;
                   2780:                                break;
                   2781:                        case 5:
                   2782:                                for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
                   2783:                                /* reset i0-th bit */
                   2784:                                fd[i0>>5] &= ~(1<<(i0&31));
                   2785:                                for ( j0 = i0+1; !(fd[j0>>5]&(1<<(j0&31))) ; j0++ );
                   2786:                                /* reset j0-th bit */
                   2787:                                fd[j0>>5] &= ~(1<<(j0&31));
                   2788:                                for ( k0 = j0+1; !(fd[k0>>5]&(1<<(k0&31))) ; k0++ );
                   2789:                                /* reset k0-th bit */
                   2790:                                fd[k0>>5] &= ~(1<<(k0&31));
                   2791:                                break;
                   2792:                        default:
                   2793:                                f->w = 0; break;
                   2794:                }
                   2795:        } else
                   2796:                f->w = 0;
                   2797:
                   2798:        if ( !f->w ) {
                   2799:                fd = f->b;
                   2800:                f->w = w; fd[0] |= 1; fd[d>>5] |= (1<<(d&31));
                   2801:                i0 = j0 = k0 = 0;
                   2802:        }
                   2803:        /* if j0 > 0 then f is already a pentanomial */
                   2804:        if ( j0 > 0 ) goto PENTA;
                   2805:
                   2806:        /* searching for an irreducible trinomial */
                   2807:
                   2808:        for ( i = 1; 2*i <= d; i++ ) {
                   2809:                /* skip the polynomials 'before' f */
                   2810:                if ( i < i0 ) continue;
                   2811:                if ( i == i0 ) { i0 = 0; continue; }
                   2812:                /* set i-th bit */
                   2813:                fd[i>>5] |= (1<<(i&31));
                   2814:                ret = irredcheck_dddup2(f);
                   2815:                if ( ret == 1 ) return 0;
                   2816:                /* reset i-th bit */
                   2817:                fd[i>>5] &= ~(1<<(i&31));
                   2818:        }
                   2819:
                   2820:        /* searching for an irreducible pentanomial */
                   2821: PENTA:
                   2822:        for ( i = 1; i < d; i++ ) {
                   2823:                /* skip the polynomials 'before' f */
                   2824:                if ( i < i0 ) continue;
                   2825:                if ( i == i0 ) i0 = 0;
                   2826:                /* set i-th bit */
                   2827:                fd[i>>5] |= (1<<(i&31));
                   2828:                for ( j = i+1; j < d; j++ ) {
                   2829:                        /* skip the polynomials 'before' f */
                   2830:                        if ( j < j0 ) continue;
                   2831:                        if ( j == j0 ) j0 = 0;
                   2832:                        /* set j-th bit */
                   2833:                        fd[j>>5] |= (1<<(j&31));
                   2834:                        for ( k = j+1; k < d; k++ ) {
                   2835:                                /* skip the polynomials 'before' f */
                   2836:                                if ( k < k0 ) continue;
                   2837:                                else if ( k == k0 ) { k0 = 0; continue; }
                   2838:                                /* set k-th bit */
                   2839:                                fd[k>>5] |= (1<<(k&31));
                   2840:                                ret = irredcheck_dddup2(f);
                   2841:                                if ( ret == 1 ) return 0;
                   2842:                                /* reset k-th bit */
                   2843:                                fd[k>>5] &= ~(1<<(k&31));
                   2844:                        }
                   2845:                        /* reset j-th bit */
                   2846:                        fd[j>>5] &= ~(1<<(j&31));
                   2847:                }
                   2848:                /* reset i-th bit */
                   2849:                fd[i>>5] &= ~(1<<(i&31));
                   2850:        }
                   2851:        /* exhausted */
                   2852:        return 1;
                   2853: }
                   2854:
                   2855: /*
                   2856:  * f = an irreducible trinomial or pentanomial of degree d 'after' f
                   2857:  *
                   2858:  * searching strategy:
                   2859:  *   trinomial x^d+x^i+1:
                   2860:  *         i is as small as possible.
                   2861:  *   trinomial x^d+x^i+x^j+x^k+1:
                   2862:  *         i is as small as possible.
                   2863:  *         For such i, j is as small as possible.
                   2864:  *         For such i and j, 'k' is as small as possible.
                   2865:  *
                   2866:  * return value : 0  --- exists
                   2867:  *                1  --- does not exist (exhaustion)
                   2868:  */
                   2869:
                   2870: int _generate_good_irreducible_polynomial(UP2 f,int d)
                   2871: {
                   2872:        int ret,i,j,k,nz,i0,j0,k0;
                   2873:        int w;
                   2874:        unsigned int *fd;
                   2875:
                   2876:        /*
                   2877:         * if f = x^d+x^i+1 then i0 <- i, j0 <- 0, k0 <-0.
                   2878:         * if f = x^d+x^k+x^j+x^i+1 (k>j>i) then i0 <- i, j0 <- j, k0 <-k.
                   2879:         * otherwise i0,j0,k0 is set to 0.
                   2880:         */
                   2881:
                   2882:        fd = f->b;
                   2883:        w = (d>>5)+1;
                   2884:        if ( f->w && (d==degup2(f)) ) {
                   2885:                for ( nz = 0, i = d; i >= 0; i-- )
                   2886:                        if ( fd[i>>5]&(1<<(i&31)) ) nz++;
                   2887:                switch ( nz ) {
                   2888:                        case 3:
                   2889:                                for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
                   2890:                                /* reset i0-th bit */
                   2891:                                fd[i0>>5] &= ~(1<<(i0&31));
                   2892:                                j0 = k0 = 0;
                   2893:                                break;
                   2894:                        case 5:
                   2895:                                for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
                   2896:                                /* reset i0-th bit */
                   2897:                                fd[i0>>5] &= ~(1<<(i0&31));
                   2898:                                for ( j0 = i0+1; !(fd[j0>>5]&(1<<(j0&31))) ; j0++ );
                   2899:                                /* reset j0-th bit */
                   2900:                                fd[j0>>5] &= ~(1<<(j0&31));
                   2901:                                for ( k0 = j0+1; !(fd[k0>>5]&(1<<(k0&31))) ; k0++ );
                   2902:                                /* reset k0-th bit */
                   2903:                                fd[k0>>5] &= ~(1<<(k0&31));
                   2904:                                break;
                   2905:                        default:
                   2906:                                f->w = 0; break;
                   2907:                }
                   2908:        } else
                   2909:                f->w = 0;
                   2910:
                   2911:        if ( !f->w ) {
                   2912:                fd = f->b;
                   2913:                f->w = w; fd[0] |= 1; fd[d>>5] |= (1<<(d&31));
                   2914:                i0 = j0 = k0 = 0;
                   2915:        }
                   2916:        /* if j0 > 0 then f is already a pentanomial */
                   2917:        if ( j0 > 0 ) goto PENTA;
                   2918:
                   2919:        /* searching for an irreducible trinomial */
                   2920:
                   2921:        for ( i = 1; 2*i <= d; i++ ) {
                   2922:                /* skip the polynomials 'before' f */
                   2923:                if ( i < i0 ) continue;
                   2924:                if ( i == i0 ) { i0 = 0; continue; }
                   2925:                /* set i-th bit */
                   2926:                fd[i>>5] |= (1<<(i&31));
                   2927:                ret = irredcheck_dddup2(f);
                   2928:                if ( ret == 1 ) return 0;
                   2929:                /* reset i-th bit */
                   2930:                fd[i>>5] &= ~(1<<(i&31));
                   2931:        }
                   2932:
                   2933:        /* searching for an irreducible pentanomial */
                   2934: PENTA:
                   2935:        for ( i = 3; i < d; i++ ) {
                   2936:                /* skip the polynomials 'before' f */
                   2937:                if ( i < i0 ) continue;
                   2938:                if ( i == i0 ) i0 = 0;
                   2939:                /* set i-th bit */
                   2940:                fd[i>>5] |= (1<<(i&31));
                   2941:                for ( j = 2; j < i; j++ ) {
                   2942:                        /* skip the polynomials 'before' f */
                   2943:                        if ( j < j0 ) continue;
                   2944:                        if ( j == j0 ) j0 = 0;
                   2945:                        /* set j-th bit */
                   2946:                        fd[j>>5] |= (1<<(j&31));
                   2947:                        for ( k = 1; k < j; k++ ) {
                   2948:                                /* skip the polynomials 'before' f */
                   2949:                                if ( k < k0 ) continue;
                   2950:                                else if ( k == k0 ) { k0 = 0; continue; }
                   2951:                                /* set k-th bit */
                   2952:                                fd[k>>5] |= (1<<(k&31));
                   2953:                                ret = irredcheck_dddup2(f);
                   2954:                                if ( ret == 1 ) return 0;
                   2955:                                /* reset k-th bit */
                   2956:                                fd[k>>5] &= ~(1<<(k&31));
                   2957:                        }
                   2958:                        /* reset j-th bit */
                   2959:                        fd[j>>5] &= ~(1<<(j&31));
                   2960:                }
                   2961:                /* reset i-th bit */
                   2962:                fd[i>>5] &= ~(1<<(i&31));
                   2963:        }
                   2964:        /* exhausted */
                   2965:        return 1;
1.3       noro     2966: }
                   2967:
1.24      noro     2968: void printqmat(Q **mat,int row,int col)
1.3       noro     2969: {
                   2970:        int i,j;
                   2971:
                   2972:        for ( i = 0; i < row; i++ ) {
                   2973:                for ( j = 0; j < col; j++ ) {
1.8       noro     2974:                        printnum((Num)mat[i][j]); printf(" ");
1.3       noro     2975:                }
                   2976:                printf("\n");
                   2977:        }
                   2978: }
                   2979:
1.24      noro     2980: void printimat(int **mat,int row,int col)
1.3       noro     2981: {
                   2982:        int i,j;
                   2983:
                   2984:        for ( i = 0; i < row; i++ ) {
                   2985:                for ( j = 0; j < col; j++ ) {
                   2986:                        printf("%d ",mat[i][j]);
                   2987:                }
                   2988:                printf("\n");
                   2989:        }
1.36      noro     2990: }
                   2991:
                   2992: void Pnd_det(NODE arg,P *rp)
                   2993: {
1.37      noro     2994:        if ( argc(arg) == 1 )
                   2995:                nd_det(0,ARG0(arg),rp);
                   2996:        else
                   2997:                nd_det(QTOS((Q)ARG1(arg)),ARG0(arg),rp);
1.1       noro     2998: }

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