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

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.46    ! saito      48:  * $OpenXM: OpenXM_contrib2/asir2000/builtin/array.c,v 1.45 2005/01/23 14:03:47 noro Exp $
1.6       noro       49: */
1.1       noro       50: #include "ca.h"
                     51: #include "base.h"
                     52: #include "parse.h"
                     53: #include "inline.h"
1.4       noro       54:
1.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)
                    879:                B[I] <-> x_{R[I]}+B[I][0]x_{C[0]}+B[I][1]x_{C[1]}+...
                    880: */
                    881:
1.24      noro      882: void Pgeneric_gauss_elim_mod(NODE arg,LIST *rp)
1.1       noro      883: {
                    884:        NODE n0;
                    885:        MAT m,mat;
                    886:        VECT rind,cind;
                    887:        Q **tmat;
                    888:        int **wmat;
                    889:        Q *rib,*cib;
                    890:        int *colstat;
                    891:        Q q;
1.24      noro      892:        int md,i,j,k,l,row,col,t,rank;
1.1       noro      893:
                    894:        asir_assert(ARG0(arg),O_MAT,"generic_gauss_elim_mod");
                    895:        asir_assert(ARG1(arg),O_N,"generic_gauss_elim_mod");
                    896:        m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
                    897:        row = m->row; col = m->col; tmat = (Q **)m->body;
                    898:        wmat = (int **)almat(row,col);
                    899:        colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
                    900:        for ( i = 0; i < row; i++ )
                    901:                for ( j = 0; j < col; j++ )
                    902:                        if ( q = (Q)tmat[i][j] ) {
                    903:                                t = rem(NM(q),md);
                    904:                                if ( t && SGN(q) < 0 )
                    905:                                        t = (md - t) % md;
                    906:                                wmat[i][j] = t;
                    907:                        } else
                    908:                                wmat[i][j] = 0;
                    909:        rank = generic_gauss_elim_mod(wmat,row,col,md,colstat);
                    910:
                    911:        MKMAT(mat,rank,col-rank);
                    912:        tmat = (Q **)mat->body;
                    913:        for ( i = 0; i < rank; i++ )
                    914:                for ( j = k = 0; j < col; j++ )
                    915:                        if ( !colstat[j] ) {
                    916:                                UTOQ(wmat[i][j],tmat[i][k]); k++;
                    917:                        }
                    918:
                    919:        MKVECT(rind,rank);
                    920:        MKVECT(cind,col-rank);
                    921:        rib = (Q *)rind->body; cib = (Q *)cind->body;
                    922:        for ( j = k = l = 0; j < col; j++ )
                    923:                if ( colstat[j] ) {
                    924:                        STOQ(j,rib[k]); k++;
                    925:                } else {
                    926:                        STOQ(j,cib[l]); l++;
                    927:                }
                    928:        n0 = mknode(3,mat,rind,cind);
                    929:        MKLIST(*rp,n0);
                    930: }
                    931:
1.24      noro      932: void Pleqm(NODE arg,VECT *rp)
1.1       noro      933: {
                    934:        MAT m;
                    935:        VECT vect;
                    936:        pointer **mat;
                    937:        Q *v;
                    938:        Q q;
                    939:        int **wmat;
                    940:        int md,i,j,row,col,t,n,status;
                    941:
                    942:        asir_assert(ARG0(arg),O_MAT,"leqm");
                    943:        asir_assert(ARG1(arg),O_N,"leqm");
                    944:        m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
                    945:        row = m->row; col = m->col; mat = m->body;
                    946:        wmat = (int **)almat(row,col);
                    947:        for ( i = 0; i < row; i++ )
                    948:                for ( j = 0; j < col; j++ )
                    949:                        if ( q = (Q)mat[i][j] ) {
                    950:                                t = rem(NM(q),md);
                    951:                                if ( SGN(q) < 0 )
                    952:                                        t = (md - t) % md;
                    953:                                wmat[i][j] = t;
                    954:                        } else
                    955:                                wmat[i][j] = 0;
                    956:        status = gauss_elim_mod(wmat,row,col,md);
                    957:        if ( status < 0 )
                    958:                *rp = 0;
                    959:        else if ( status > 0 )
                    960:                *rp = (VECT)ONE;
                    961:        else {
                    962:                n = col - 1;
                    963:                MKVECT(vect,n);
                    964:                for ( i = 0, v = (Q *)vect->body; i < n; i++ ) {
                    965:                        t = (md-wmat[i][n])%md; STOQ(t,v[i]);
                    966:                }
                    967:                *rp = vect;
                    968:        }
                    969: }
                    970:
1.24      noro      971: int gauss_elim_mod(int **mat,int row,int col,int md)
1.1       noro      972: {
                    973:        int i,j,k,inv,a,n;
                    974:        int *t,*pivot;
                    975:
                    976:        n = col - 1;
                    977:        for ( j = 0; j < n; j++ ) {
                    978:                for ( i = j; i < row && !mat[i][j]; i++ );
                    979:                if ( i == row )
                    980:                        return 1;
                    981:                if ( i != j ) {
                    982:                        t = mat[i]; mat[i] = mat[j]; mat[j] = t;
                    983:                }
                    984:                pivot = mat[j];
                    985:                inv = invm(pivot[j],md);
                    986:                for ( k = j; k <= n; k++ ) {
                    987: /*                     pivot[k] = dmar(pivot[k],inv,0,md); */
                    988:                        DMAR(pivot[k],inv,0,md,pivot[k])
                    989:                }
                    990:                for ( i = 0; i < row; i++ ) {
                    991:                        t = mat[i];
                    992:                        if ( i != j && (a = t[j]) )
                    993:                                for ( k = j, a = md - a; k <= n; k++ ) {
1.8       noro      994:                                        unsigned int tk;
1.1       noro      995: /*                                     t[k] = dmar(pivot[k],a,t[k],md); */
1.8       noro      996:                                        DMAR(pivot[k],a,t[k],md,tk)
                    997:                                        t[k] = tk;
1.1       noro      998:                                }
                    999:                }
                   1000:        }
                   1001:        for ( i = n; i < row && !mat[i][n]; i++ );
                   1002:        if ( i == row )
                   1003:                return 0;
                   1004:        else
                   1005:                return -1;
                   1006: }
                   1007:
1.4       noro     1008: struct oEGT eg_mod,eg_elim,eg_elim1,eg_elim2,eg_chrem,eg_gschk,eg_intrat,eg_symb;
1.31      noro     1009: struct oEGT eg_conv;
1.1       noro     1010:
1.24      noro     1011: int generic_gauss_elim(MAT mat,MAT *nm,Q *dn,int **rindp,int **cindp)
1.1       noro     1012: {
                   1013:        int **wmat;
                   1014:        Q **bmat;
                   1015:        N **tmat;
                   1016:        Q *bmi;
                   1017:        N *tmi;
                   1018:        Q q;
                   1019:        int *wmi;
                   1020:        int *colstat,*wcolstat,*rind,*cind;
                   1021:        int row,col,ind,md,i,j,k,l,t,t1,rank,rank0,inv;
                   1022:        N m1,m2,m3,s,u;
                   1023:        MAT r,crmat;
                   1024:        struct oEGT tmp0,tmp1;
                   1025:        struct oEGT eg_mod_split,eg_elim_split,eg_chrem_split;
                   1026:        struct oEGT eg_intrat_split,eg_gschk_split;
                   1027:        int ret;
                   1028:
                   1029:        init_eg(&eg_mod_split); init_eg(&eg_chrem_split);
                   1030:        init_eg(&eg_elim_split); init_eg(&eg_intrat_split);
                   1031:        init_eg(&eg_gschk_split);
                   1032:        bmat = (Q **)mat->body;
                   1033:        row = mat->row; col = mat->col;
                   1034:        wmat = (int **)almat(row,col);
                   1035:        colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
                   1036:        wcolstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
                   1037:        for ( ind = 0; ; ind++ ) {
1.11      noro     1038:                if ( DP_Print ) {
1.2       noro     1039:                        fprintf(asir_out,"."); fflush(asir_out);
                   1040:                }
1.12      noro     1041:                md = get_lprime(ind);
1.1       noro     1042:                get_eg(&tmp0);
                   1043:                for ( i = 0; i < row; i++ )
                   1044:                        for ( j = 0, bmi = bmat[i], wmi = wmat[i]; j < col; j++ )
                   1045:                                if ( q = (Q)bmi[j] ) {
                   1046:                                        t = rem(NM(q),md);
                   1047:                                        if ( t && SGN(q) < 0 )
                   1048:                                                t = (md - t) % md;
                   1049:                                        wmi[j] = t;
                   1050:                                } else
                   1051:                                        wmi[j] = 0;
                   1052:                get_eg(&tmp1);
                   1053:                add_eg(&eg_mod,&tmp0,&tmp1);
                   1054:                add_eg(&eg_mod_split,&tmp0,&tmp1);
                   1055:                get_eg(&tmp0);
                   1056:                rank = generic_gauss_elim_mod(wmat,row,col,md,wcolstat);
                   1057:                get_eg(&tmp1);
                   1058:                add_eg(&eg_elim,&tmp0,&tmp1);
                   1059:                add_eg(&eg_elim_split,&tmp0,&tmp1);
                   1060:                if ( !ind ) {
                   1061: RESET:
                   1062:                        UTON(md,m1);
                   1063:                        rank0 = rank;
                   1064:                        bcopy(wcolstat,colstat,col*sizeof(int));
                   1065:                        MKMAT(crmat,rank,col-rank);
                   1066:                        MKMAT(r,rank,col-rank); *nm = r;
                   1067:                        tmat = (N **)crmat->body;
                   1068:                        for ( i = 0; i < rank; i++ )
                   1069:                                for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
                   1070:                                        if ( !colstat[j] ) {
                   1071:                                                UTON(wmi[j],tmi[k]); k++;
                   1072:                                        }
                   1073:                } else {
                   1074:                        if ( rank < rank0 ) {
1.11      noro     1075:                                if ( DP_Print ) {
1.1       noro     1076:                                        fprintf(asir_out,"lower rank matrix; continuing...\n");
1.2       noro     1077:                                        fflush(asir_out);
                   1078:                                }
1.1       noro     1079:                                continue;
                   1080:                        } else if ( rank > rank0 ) {
1.11      noro     1081:                                if ( DP_Print ) {
1.1       noro     1082:                                        fprintf(asir_out,"higher rank matrix; resetting...\n");
1.2       noro     1083:                                        fflush(asir_out);
                   1084:                                }
1.1       noro     1085:                                goto RESET;
                   1086:                        } else {
                   1087:                                for ( j = 0; (j<col) && (colstat[j]==wcolstat[j]); j++ );
                   1088:                                if ( j < col ) {
1.11      noro     1089:                                        if ( DP_Print ) {
1.1       noro     1090:                                                fprintf(asir_out,"inconsitent colstat; resetting...\n");
1.2       noro     1091:                                                fflush(asir_out);
                   1092:                                        }
1.1       noro     1093:                                        goto RESET;
                   1094:                                }
                   1095:                        }
                   1096:
                   1097:                        get_eg(&tmp0);
                   1098:                        inv = invm(rem(m1,md),md);
                   1099:                        UTON(md,m2); muln(m1,m2,&m3);
                   1100:                        for ( i = 0; i < rank; i++ )
                   1101:                                for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
                   1102:                                        if ( !colstat[j] ) {
                   1103:                                                if ( tmi[k] ) {
                   1104:                                                /* f3 = f1+m1*(m1 mod m2)^(-1)*(f2 - f1 mod m2) */
                   1105:                                                        t = rem(tmi[k],md);
                   1106:                                                        if ( wmi[j] >= t )
                   1107:                                                                t = wmi[j]-t;
                   1108:                                                        else
                   1109:                                                                t = md-(t-wmi[j]);
                   1110:                                                        DMAR(t,inv,0,md,t1)
                   1111:                                                        UTON(t1,u);
                   1112:                                                        muln(m1,u,&s);
                   1113:                                                        addn(tmi[k],s,&u); tmi[k] = u;
                   1114:                                                } else if ( wmi[j] ) {
                   1115:                                                /* f3 = m1*(m1 mod m2)^(-1)*f2 */
                   1116:                                                        DMAR(wmi[j],inv,0,md,t)
                   1117:                                                        UTON(t,u);
                   1118:                                                        muln(m1,u,&s); tmi[k] = s;
                   1119:                                                }
                   1120:                                                k++;
                   1121:                                        }
                   1122:                        m1 = m3;
                   1123:                        get_eg(&tmp1);
                   1124:                        add_eg(&eg_chrem,&tmp0,&tmp1);
                   1125:                        add_eg(&eg_chrem_split,&tmp0,&tmp1);
                   1126:
                   1127:                        get_eg(&tmp0);
1.38      noro     1128:                        if ( ind % F4_INTRAT_PERIOD )
1.13      noro     1129:                                ret = 0;
                   1130:                        else
                   1131:                                ret = intmtoratm(crmat,m1,*nm,dn);
1.1       noro     1132:                        get_eg(&tmp1);
                   1133:                        add_eg(&eg_intrat,&tmp0,&tmp1);
                   1134:                        add_eg(&eg_intrat_split,&tmp0,&tmp1);
                   1135:                        if ( ret ) {
                   1136:                                *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
                   1137:                                *cindp = cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int));
                   1138:                                for ( j = k = l = 0; j < col; j++ )
                   1139:                                        if ( colstat[j] )
                   1140:                                                rind[k++] = j;
                   1141:                                        else
                   1142:                                                cind[l++] = j;
                   1143:                                get_eg(&tmp0);
1.3       noro     1144:                                if ( gensolve_check(mat,*nm,*dn,rind,cind) ) {
                   1145:                                        get_eg(&tmp1);
                   1146:                                        add_eg(&eg_gschk,&tmp0,&tmp1);
                   1147:                                        add_eg(&eg_gschk_split,&tmp0,&tmp1);
1.11      noro     1148:                                        if ( DP_Print ) {
1.3       noro     1149:                                                print_eg("Mod",&eg_mod_split);
                   1150:                                                print_eg("Elim",&eg_elim_split);
                   1151:                                                print_eg("ChRem",&eg_chrem_split);
                   1152:                                                print_eg("IntRat",&eg_intrat_split);
                   1153:                                                print_eg("Check",&eg_gschk_split);
                   1154:                                                fflush(asir_out);
                   1155:                                        }
                   1156:                                        return rank;
                   1157:                                }
                   1158:                        }
                   1159:                }
                   1160:        }
                   1161: }
                   1162:
1.24      noro     1163: int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn,int **rindp,int **cindp)
1.3       noro     1164: {
                   1165:        MAT bmat,xmat;
                   1166:        Q **a0,**a,**b,**x,**nm;
                   1167:        Q *ai,*bi,*xi;
                   1168:        int row,col;
                   1169:        int **w;
                   1170:        int *wi;
                   1171:        int **wc;
                   1172:        Q mdq,q,s,u;
                   1173:        N tn;
                   1174:        int ind,md,i,j,k,l,li,ri,rank;
                   1175:        unsigned int t;
                   1176:        int *cinfo,*rinfo;
                   1177:        int *rind,*cind;
                   1178:        int count;
1.41      noro     1179:        int ret;
                   1180:        struct oEGT eg_mul,eg_inv,eg_intrat,eg_check,tmp0,tmp1;
1.39      noro     1181:        int period;
1.44      noro     1182:        int *wx,*ptr;
                   1183:        int wxsize,nsize;
                   1184:        N wn;
                   1185:        Q wq;
1.3       noro     1186:
                   1187:        a0 = (Q **)mat->body;
                   1188:        row = mat->row; col = mat->col;
                   1189:        w = (int **)almat(row,col);
                   1190:        for ( ind = 0; ; ind++ ) {
1.12      noro     1191:                md = get_lprime(ind);
1.3       noro     1192:                STOQ(md,mdq);
                   1193:                for ( i = 0; i < row; i++ )
                   1194:                        for ( j = 0, ai = a0[i], wi = w[i]; j < col; j++ )
                   1195:                                if ( q = (Q)ai[j] ) {
                   1196:                                        t = rem(NM(q),md);
                   1197:                                        if ( t && SGN(q) < 0 )
                   1198:                                                t = (md - t) % md;
                   1199:                                        wi[j] = t;
                   1200:                                } else
                   1201:                                        wi[j] = 0;
                   1202:
1.27      noro     1203:                rank = find_lhs_and_lu_mod((unsigned int **)w,row,col,md,&rinfo,&cinfo);
1.3       noro     1204:                a = (Q **)almat_pointer(rank,rank); /* lhs mat */
                   1205:                MKMAT(bmat,rank,col-rank); b = (Q **)bmat->body; /* lhs mat */
                   1206:                for ( j = li = ri = 0; j < col; j++ )
                   1207:                        if ( cinfo[j] ) {
                   1208:                                /* the column is in lhs */
                   1209:                                for ( i = 0; i < rank; i++ ) {
                   1210:                                        w[i][li] = w[i][j];
                   1211:                                        a[i][li] = a0[rinfo[i]][j];
                   1212:                                }
                   1213:                                li++;
                   1214:                        } else {
                   1215:                                /* the column is in rhs */
                   1216:                                for ( i = 0; i < rank; i++ )
                   1217:                                        b[i][ri] = a0[rinfo[i]][j];
                   1218:                                ri++;
                   1219:                        }
                   1220:
                   1221:                        /* solve Ax+B=0; A: rank x rank, B: rank x ri */
                   1222:                        MKMAT(xmat,rank,ri); x = (Q **)(xmat)->body;
                   1223:                        MKMAT(*nmmat,rank,ri); nm = (Q **)(*nmmat)->body;
                   1224:                        /* use the right part of w as work area */
                   1225:                        /* ri = col - rank */
                   1226:                        wc = (int **)almat(rank,ri);
                   1227:                        for ( i = 0; i < rank; i++ )
                   1228:                                wc[i] = w[i]+rank;
                   1229:                        *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
                   1230:                        *cindp = cind = (int *)MALLOC_ATOMIC((ri)*sizeof(int));
                   1231:
                   1232:                        init_eg(&eg_mul); init_eg(&eg_inv);
1.41      noro     1233:                        init_eg(&eg_check); init_eg(&eg_intrat);
1.39      noro     1234:                        period = F4_INTRAT_PERIOD;
1.44      noro     1235:                        nsize = period;
                   1236:                        wxsize = rank*ri*nsize;
                   1237:                        wx = (int *)MALLOC_ATOMIC(wxsize*sizeof(int));
                   1238:                        for ( i = 0; i < wxsize; i++ ) wx[i] = 0;
                   1239:                        for ( q = ONE, count = 0; ; ) {
1.42      noro     1240:                                if ( DP_Print > 3 )
1.41      noro     1241:                                        fprintf(stderr,"o");
1.3       noro     1242:                                /* wc = -b mod md */
1.44      noro     1243:                                get_eg(&tmp0);
1.3       noro     1244:                                for ( i = 0; i < rank; i++ )
                   1245:                                        for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ )
                   1246:                                                if ( u = (Q)bi[j] ) {
                   1247:                                                        t = rem(NM(u),md);
                   1248:                                                        if ( t && SGN(u) > 0 )
                   1249:                                                                t = (md - t) % md;
                   1250:                                                        wi[j] = t;
                   1251:                                                } else
                   1252:                                                        wi[j] = 0;
1.44      noro     1253:                                /* wc = A^(-1)wc; wc is not normalized */
                   1254:                                solve_by_lu_mod(w,rank,md,wc,ri,0);
                   1255:                                /* wx += q*wc */
                   1256:                                ptr = wx;
                   1257:                                for ( i = 0; i < rank; i++ )
                   1258:                                        for ( j = 0, wi = wc[i]; j < ri; j++ ) {
                   1259:                                                if ( wi[j] )
                   1260:                                                        muln_1(BD(NM(q)),PL(NM(q)),wi[j],ptr);
                   1261:                                                ptr += nsize;
                   1262:                                        }
                   1263:                                count++;
1.1       noro     1264:                                get_eg(&tmp1);
1.3       noro     1265:                                add_eg(&eg_inv,&tmp0,&tmp1);
                   1266:                                get_eg(&tmp0);
                   1267:                                for ( i = 0; i < rank; i++ )
                   1268:                                        for ( j = 0; j < ri; j++ ) {
                   1269:                                                inner_product_mat_int_mod(a,wc,rank,i,j,&u);
                   1270:                                                addq(b[i][j],u,&s);
                   1271:                                                if ( s ) {
                   1272:                                                        t = divin(NM(s),md,&tn);
                   1273:                                                        if ( t )
                   1274:                                                                error("generic_gauss_elim_hensel:incosistent");
                   1275:                                                        NTOQ(tn,SGN(s),b[i][j]);
                   1276:                                                } else
                   1277:                                                        b[i][j] = 0;
                   1278:                                        }
                   1279:                                get_eg(&tmp1);
                   1280:                                add_eg(&eg_mul,&tmp0,&tmp1);
                   1281:                                /* q = q*md */
                   1282:                                mulq(q,mdq,&u); q = u;
1.44      noro     1283:                                if ( count == period ) {
1.41      noro     1284:                                        get_eg(&tmp0);
1.44      noro     1285:                                        ptr = wx;
                   1286:                                        for ( i = 0; i < rank; i++ )
                   1287:                                                for ( j = 0, xi = x[i]; j < ri;
                   1288:                                                        j++, ptr += nsize ) {
                   1289:                                                        for ( k = nsize-1; k >= 0 && !ptr[k]; k-- );
                   1290:                                                        if ( k >= 0 ) {
                   1291:                                                                wn = NALLOC(k+1);
                   1292:                                                                PL(wn) = k+1;
                   1293:                                                                for ( l = 0; l <= k; l++ ) BD(wn)[l] = (unsigned int)ptr[l];
                   1294:                                                                NTOQ(wn,1,wq);
                   1295:                                                                subq(xi[j],wq,&u); xi[j] = u;
                   1296:                                                        }
                   1297:                                                }
1.41      noro     1298:                                        ret = intmtoratm_q(xmat,NM(q),*nmmat,dn);
                   1299:                                        get_eg(&tmp1); add_eg(&eg_intrat,&tmp0,&tmp1);
                   1300:                                        if ( ret ) {
1.39      noro     1301:                                                for ( j = k = l = 0; j < col; j++ )
                   1302:                                                        if ( cinfo[j] )
                   1303:                                                                rind[k++] = j;
                   1304:                                                        else
                   1305:                                                                cind[l++] = j;
1.41      noro     1306:                                                get_eg(&tmp0);
                   1307:                                                ret = gensolve_check(mat,*nmmat,*dn,rind,cind);
                   1308:                                                get_eg(&tmp1); add_eg(&eg_check,&tmp0,&tmp1);
                   1309:                                                if ( ret ) {
1.42      noro     1310:                                                        if ( DP_Print > 3 ) {
1.40      noro     1311:                                                                fprintf(stderr,"\n");
                   1312:                                                                print_eg("INV",&eg_inv);
                   1313:                                                                print_eg("MUL",&eg_mul);
1.41      noro     1314:                                                                print_eg("INTRAT",&eg_intrat);
                   1315:                                                                print_eg("CHECK",&eg_check);
1.40      noro     1316:                                                                fflush(asir_out);
                   1317:                                                        }
1.39      noro     1318:                                                        return rank;
                   1319:                                                }
1.44      noro     1320:                                        } else {
                   1321:                                                period = period*3/2;
                   1322:                                                count = 0;
                   1323:                                                nsize += period;
                   1324:                                                wxsize += rank*ri*nsize;
                   1325:                                                wx = (int *)REALLOC(wx,wxsize*sizeof(int));
                   1326:                                                for ( i = 0; i < wxsize; i++ ) wx[i] = 0;
                   1327:                                        }
1.41      noro     1328:                                }
1.1       noro     1329:                        }
                   1330:        }
                   1331: }
                   1332:
                   1333: int f4_nocheck;
                   1334:
1.24      noro     1335: int gensolve_check(MAT mat,MAT nm,Q dn,int *rind,int *cind)
1.1       noro     1336: {
                   1337:        int row,col,rank,clen,i,j,k,l;
1.24      noro     1338:        Q s,t;
1.1       noro     1339:        Q *w;
                   1340:        Q *mati,*nmk;
                   1341:
                   1342:        if ( f4_nocheck )
                   1343:                return 1;
                   1344:        row = mat->row; col = mat->col;
                   1345:        rank = nm->row; clen = nm->col;
                   1346:        w = (Q *)MALLOC(clen*sizeof(Q));
                   1347:        for ( i = 0; i < row; i++ ) {
                   1348:                mati = (Q *)mat->body[i];
                   1349: #if 1
                   1350:                bzero(w,clen*sizeof(Q));
                   1351:                for ( k = 0; k < rank; k++ )
                   1352:                        for ( l = 0, nmk = (Q *)nm->body[k]; l < clen; l++ ) {
                   1353:                                mulq(mati[rind[k]],nmk[l],&t);
                   1354:                                addq(w[l],t,&s); w[l] = s;
                   1355:                        }
                   1356:                for ( j = 0; j < clen; j++ ) {
                   1357:                        mulq(dn,mati[cind[j]],&t);
                   1358:                        if ( cmpq(w[j],t) )
                   1359:                                break;
                   1360:                }
                   1361: #else
                   1362:                for ( j = 0; j < clen; j++ ) {
                   1363:                        for ( k = 0, s = 0; k < rank; k++ ) {
                   1364:                                mulq(mati[rind[k]],nm->body[k][j],&t);
                   1365:                                addq(s,t,&u); s = u;
                   1366:                        }
                   1367:                        mulq(dn,mati[cind[j]],&t);
                   1368:                        if ( cmpq(s,t) )
                   1369:                                break;
                   1370:                }
                   1371: #endif
                   1372:                if ( j != clen )
                   1373:                        break;
                   1374:        }
                   1375:        if ( i != row )
                   1376:                return 0;
                   1377:        else
                   1378:                return 1;
                   1379: }
                   1380:
                   1381: /* assuming 0 < c < m */
                   1382:
1.24      noro     1383: int inttorat(N c,N m,N b,int *sgnp,N *nmp,N *dnp)
1.1       noro     1384: {
1.24      noro     1385:        Q qq,t,u1,v1,r1;
                   1386:        N q,u2,v2,r2;
1.1       noro     1387:
                   1388:        u1 = 0; v1 = ONE; u2 = m; v2 = c;
                   1389:        while ( cmpn(v2,b) >= 0 ) {
                   1390:                divn(u2,v2,&q,&r2); u2 = v2; v2 = r2;
                   1391:                NTOQ(q,1,qq); mulq(qq,v1,&t); subq(u1,t,&r1); u1 = v1; v1 = r1;
                   1392:        }
                   1393:        if ( cmpn(NM(v1),b) >= 0 )
                   1394:                return 0;
                   1395:        else {
                   1396:                *nmp = v2;
                   1397:                *dnp = NM(v1);
                   1398:                *sgnp = SGN(v1);
                   1399:                return 1;
                   1400:        }
                   1401: }
                   1402:
                   1403: /* mat->body = N ** */
                   1404:
1.24      noro     1405: int intmtoratm(MAT mat,N md,MAT nm,Q *dn)
1.1       noro     1406: {
                   1407:        N t,s,b;
1.24      noro     1408:        Q dn0,dn1,nm1,q;
1.1       noro     1409:        int i,j,k,l,row,col;
                   1410:        Q **rmat;
                   1411:        N **tmat;
                   1412:        N *tmi;
                   1413:        Q *nmk;
                   1414:        N u,unm,udn;
                   1415:        int sgn,ret;
                   1416:
1.3       noro     1417:        if ( UNIN(md) )
                   1418:                return 0;
1.1       noro     1419:        row = mat->row; col = mat->col;
                   1420:        bshiftn(md,1,&t);
                   1421:        isqrt(t,&s);
                   1422:        bshiftn(s,64,&b);
                   1423:        if ( !b )
                   1424:                b = ONEN;
                   1425:        dn0 = ONE;
                   1426:        tmat = (N **)mat->body;
                   1427:        rmat = (Q **)nm->body;
                   1428:        for ( i = 0; i < row; i++ )
                   1429:                for ( j = 0, tmi = tmat[i]; j < col; j++ )
                   1430:                        if ( tmi[j] ) {
                   1431:                                muln(tmi[j],NM(dn0),&s);
                   1432:                                remn(s,md,&u);
                   1433:                                ret = inttorat(u,md,b,&sgn,&unm,&udn);
                   1434:                                if ( !ret )
                   1435:                                        return 0;
                   1436:                                else {
                   1437:                                        NTOQ(unm,sgn,nm1);
                   1438:                                        NTOQ(udn,1,dn1);
                   1439:                                        if ( !UNIQ(dn1) ) {
                   1440:                                                for ( k = 0; k < i; k++ )
                   1441:                                                        for ( l = 0, nmk = rmat[k]; l < col; l++ ) {
                   1442:                                                                mulq(nmk[l],dn1,&q); nmk[l] = q;
                   1443:                                                        }
                   1444:                                                for ( l = 0, nmk = rmat[i]; l < j; l++ ) {
                   1445:                                                        mulq(nmk[l],dn1,&q); nmk[l] = q;
                   1446:                                                }
                   1447:                                        }
                   1448:                                        rmat[i][j] = nm1;
                   1449:                                        mulq(dn0,dn1,&q); dn0 = q;
                   1450:                                }
                   1451:                        }
                   1452:        *dn = dn0;
                   1453:        return 1;
                   1454: }
                   1455:
1.3       noro     1456: /* mat->body = Q ** */
                   1457:
1.24      noro     1458: int intmtoratm_q(MAT mat,N md,MAT nm,Q *dn)
1.3       noro     1459: {
                   1460:        N t,s,b;
1.24      noro     1461:        Q dn0,dn1,nm1,q;
1.3       noro     1462:        int i,j,k,l,row,col;
                   1463:        Q **rmat;
                   1464:        Q **tmat;
                   1465:        Q *tmi;
                   1466:        Q *nmk;
                   1467:        N u,unm,udn;
                   1468:        int sgn,ret;
                   1469:
                   1470:        if ( UNIN(md) )
                   1471:                return 0;
                   1472:        row = mat->row; col = mat->col;
                   1473:        bshiftn(md,1,&t);
                   1474:        isqrt(t,&s);
                   1475:        bshiftn(s,64,&b);
                   1476:        if ( !b )
                   1477:                b = ONEN;
                   1478:        dn0 = ONE;
                   1479:        tmat = (Q **)mat->body;
                   1480:        rmat = (Q **)nm->body;
                   1481:        for ( i = 0; i < row; i++ )
                   1482:                for ( j = 0, tmi = tmat[i]; j < col; j++ )
                   1483:                        if ( tmi[j] ) {
                   1484:                                muln(NM(tmi[j]),NM(dn0),&s);
                   1485:                                remn(s,md,&u);
                   1486:                                ret = inttorat(u,md,b,&sgn,&unm,&udn);
                   1487:                                if ( !ret )
                   1488:                                        return 0;
                   1489:                                else {
                   1490:                                        if ( SGN(tmi[j])<0 )
                   1491:                                                sgn = -sgn;
                   1492:                                        NTOQ(unm,sgn,nm1);
                   1493:                                        NTOQ(udn,1,dn1);
                   1494:                                        if ( !UNIQ(dn1) ) {
                   1495:                                                for ( k = 0; k < i; k++ )
                   1496:                                                        for ( l = 0, nmk = rmat[k]; l < col; l++ ) {
                   1497:                                                                mulq(nmk[l],dn1,&q); nmk[l] = q;
                   1498:                                                        }
                   1499:                                                for ( l = 0, nmk = rmat[i]; l < j; l++ ) {
                   1500:                                                        mulq(nmk[l],dn1,&q); nmk[l] = q;
                   1501:                                                }
                   1502:                                        }
                   1503:                                        rmat[i][j] = nm1;
                   1504:                                        mulq(dn0,dn1,&q); dn0 = q;
                   1505:                                }
                   1506:                        }
                   1507:        *dn = dn0;
                   1508:        return 1;
                   1509: }
                   1510:
1.4       noro     1511: #define ONE_STEP1  if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
                   1512:
1.24      noro     1513: void reduce_reducers_mod(int **mat,int row,int col,int md)
1.4       noro     1514: {
                   1515:        int i,j,k,l,hc,zzz;
                   1516:        int *t,*s,*tj,*ind;
                   1517:
                   1518:        /* reduce the reducers */
                   1519:        ind = (int *)ALLOCA(row*sizeof(int));
                   1520:        for ( i = 0; i < row; i++ ) {
                   1521:                t = mat[i];
                   1522:                for ( j = 0; j < col && !t[j]; j++ );
                   1523:                /* register the position of the head term */
                   1524:                ind[i] = j;
                   1525:                for ( l = i-1; l >= 0; l-- ) {
                   1526:                        /* reduce mat[i] by mat[l] */
                   1527:                        if ( hc = t[ind[l]] ) {
                   1528:                                /* mat[i] = mat[i]-hc*mat[l] */
                   1529:                                j = ind[l];
                   1530:                                s = mat[l]+j;
                   1531:                                tj = t+j;
                   1532:                                hc = md-hc;
                   1533:                                k = col-j;
                   1534:                                for ( ; k >= 64; k -= 64 ) {
                   1535:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1536:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1537:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1538:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1539:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1540:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1541:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1542:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1543:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1544:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1545:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1546:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   1547:                                        ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
                   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:                                }
1.16      noro     1552:                                for ( ; k > 0; k-- ) {
1.4       noro     1553:                                        if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
                   1554:                                }
                   1555:                        }
                   1556:                }
                   1557:        }
                   1558: }
                   1559:
                   1560: /*
                   1561:        mat[i] : reducers (i=0,...,nred-1)
                   1562:                 spolys (i=nred,...,row-1)
                   1563:        mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
                   1564:        1. reduce the reducers
                   1565:        2. reduce spolys by the reduced reducers
                   1566: */
                   1567:
1.24      noro     1568: void pre_reduce_mod(int **mat,int row,int col,int nred,int md)
1.4       noro     1569: {
                   1570:        int i,j,k,l,hc,inv;
                   1571:        int *t,*s,*tk,*ind;
                   1572:
                   1573: #if 1
                   1574:        /* reduce the reducers */
                   1575:        ind = (int *)ALLOCA(row*sizeof(int));
                   1576:        for ( i = 0; i < nred; i++ ) {
                   1577:                /* make mat[i] monic and mat[i] by mat[0],...,mat[i-1] */
                   1578:                t = mat[i];
                   1579:                for ( j = 0; j < col && !t[j]; j++ );
                   1580:                /* register the position of the head term */
                   1581:                ind[i] = j;
                   1582:                inv = invm(t[j],md);
                   1583:                for ( k = j; k < col; k++ )
                   1584:                        if ( t[k] )
                   1585:                                DMAR(t[k],inv,0,md,t[k])
                   1586:                for ( l = i-1; l >= 0; l-- ) {
                   1587:                        /* reduce mat[i] by mat[l] */
                   1588:                        if ( hc = t[ind[l]] ) {
                   1589:                                /* mat[i] = mat[i]-hc*mat[l] */
                   1590:                                for ( k = ind[l], hc = md-hc, s = mat[l]+k, tk = t+k;
                   1591:                                        k < col; k++, tk++, s++ )
                   1592:                                        if ( *s )
                   1593:                                                DMAR(*s,hc,*tk,md,*tk)
                   1594:                        }
                   1595:                }
                   1596:        }
                   1597:        /* reduce the spolys */
                   1598:        for ( i = nred; i < row; i++ ) {
                   1599:                t = mat[i];
                   1600:                for ( l = nred-1; l >= 0; l-- ) {
                   1601:                        /* reduce mat[i] by mat[l] */
                   1602:                        if ( hc = t[ind[l]] ) {
                   1603:                                /* mat[i] = mat[i]-hc*mat[l] */
                   1604:                                for ( k = ind[l], hc = md-hc, s = mat[l]+k, tk = t+k;
                   1605:                                        k < col; k++, tk++, s++ )
                   1606:                                        if ( *s )
                   1607:                                                DMAR(*s,hc,*tk,md,*tk)
                   1608:                        }
                   1609:                }
                   1610:        }
                   1611: #endif
                   1612: }
                   1613: /*
                   1614:        mat[i] : reducers (i=0,...,nred-1)
                   1615:        mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
                   1616: */
                   1617:
1.24      noro     1618: void reduce_sp_by_red_mod(int *sp,int **redmat,int *ind,int nred,int col,int md)
1.4       noro     1619: {
                   1620:        int i,j,k,hc,zzz;
1.24      noro     1621:        int *s,*tj;
1.4       noro     1622:
                   1623:        /* reduce the spolys by redmat */
                   1624:        for ( i = nred-1; i >= 0; i-- ) {
                   1625:                /* reduce sp by redmat[i] */
                   1626:                if ( hc = sp[ind[i]] ) {
                   1627:                        /* sp = sp-hc*redmat[i] */
                   1628:                        j = ind[i];
                   1629:                        hc = md-hc;
                   1630:                        s = redmat[i]+j;
                   1631:                        tj = sp+j;
1.16      noro     1632:                        for ( k = col-j; k > 0; k-- ) {
1.4       noro     1633:                                if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
1.15      noro     1634:                        }
                   1635:                }
1.17      noro     1636:        }
                   1637: }
                   1638:
                   1639: /*
1.15      noro     1640:        mat[i] : compressed reducers (i=0,...,nred-1)
                   1641:        mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
                   1642: */
                   1643:
1.24      noro     1644: void red_by_compress(int m,unsigned int *p,unsigned int *r,
                   1645:        unsigned int *ri,unsigned int hc,int len)
1.18      noro     1646: {
1.19      noro     1647:        unsigned int up,lo;
1.18      noro     1648:        unsigned int dmy;
                   1649:        unsigned int *pj;
                   1650:
1.21      noro     1651:        p[*ri] = 0; r++; ri++;
                   1652:        for ( len--; len; len--, r++, ri++ ) {
                   1653:                pj = p+ *ri;
                   1654:                DMA(*r,hc,*pj,up,lo);
1.18      noro     1655:                if ( up ) {
                   1656:                        DSAB(m,up,lo,dmy,*pj);
                   1657:                } else
                   1658:                        *pj = lo;
                   1659:        }
                   1660: }
                   1661:
                   1662: /* p -= hc*r */
                   1663:
1.24      noro     1664: void red_by_vect(int m,unsigned int *p,unsigned int *r,unsigned int hc,int len)
1.18      noro     1665: {
                   1666:        register unsigned int up,lo;
                   1667:        unsigned int dmy;
                   1668:
                   1669:        *p++ = 0; r++; len--;
                   1670:        for ( ; len; len--, r++, p++ )
                   1671:                if ( *r ) {
1.20      noro     1672:                        DMA(*r,hc,*p,up,lo);
1.18      noro     1673:                        if ( up ) {
                   1674:                                DSAB(m,up,lo,dmy,*p);
                   1675:                        } else
                   1676:                                *p = lo;
                   1677:                }
                   1678: }
                   1679:
1.32      noro     1680: void red_by_vect_sf(int m,unsigned int *p,unsigned int *r,unsigned int hc,int len)
                   1681: {
                   1682:        *p++ = 0; r++; len--;
                   1683:        for ( ; len; len--, r++, p++ )
                   1684:                if ( *r )
                   1685:                        *p = _addsf(_mulsf(*r,hc),*p);
                   1686: }
                   1687:
1.21      noro     1688: extern unsigned int **psca;
                   1689:
1.24      noro     1690: void reduce_sp_by_red_mod_compress (int *sp,CDP *redmat,int *ind,
                   1691:        int nred,int col,int md)
1.15      noro     1692: {
1.24      noro     1693:        int i,len;
1.15      noro     1694:        CDP ri;
1.24      noro     1695:        unsigned int hc;
1.18      noro     1696:        unsigned int *usp;
1.15      noro     1697:
1.18      noro     1698:        usp = (unsigned int *)sp;
1.15      noro     1699:        /* reduce the spolys by redmat */
                   1700:        for ( i = nred-1; i >= 0; i-- ) {
                   1701:                /* reduce sp by redmat[i] */
1.18      noro     1702:                usp[ind[i]] %= md;
                   1703:                if ( hc = usp[ind[i]] ) {
1.15      noro     1704:                        /* sp = sp-hc*redmat[i] */
                   1705:                        hc = md-hc;
                   1706:                        ri = redmat[i];
                   1707:                        len = ri->len;
1.21      noro     1708:                        red_by_compress(md,usp,psca[ri->psindex],ri->body,hc,len);
1.4       noro     1709:                }
                   1710:        }
1.18      noro     1711:        for ( i = 0; i < col; i++ )
1.24      noro     1712:                if ( usp[i] >= (unsigned int)md )
1.18      noro     1713:                        usp[i] %= md;
1.4       noro     1714: }
                   1715:
                   1716: #define ONE_STEP2  if ( zzz = *pk ) { DMAR(zzz,a,*tk,md,*tk) } pk++; tk++;
                   1717:
1.24      noro     1718: int generic_gauss_elim_mod(int **mat0,int row,int col,int md,int *colstat)
1.1       noro     1719: {
1.24      noro     1720:        int i,j,k,l,inv,a,rank;
                   1721:        unsigned int *t,*pivot,*pk;
1.18      noro     1722:        unsigned int **mat;
1.1       noro     1723:
1.18      noro     1724:        mat = (unsigned int **)mat0;
1.1       noro     1725:        for ( rank = 0, j = 0; j < col; j++ ) {
1.18      noro     1726:                for ( i = rank; i < row; i++ )
                   1727:                        mat[i][j] %= md;
                   1728:                for ( i = rank; i < row; i++ )
                   1729:                        if ( mat[i][j] )
                   1730:                                break;
1.1       noro     1731:                if ( i == row ) {
                   1732:                        colstat[j] = 0;
                   1733:                        continue;
                   1734:                } else
                   1735:                        colstat[j] = 1;
                   1736:                if ( i != rank ) {
                   1737:                        t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   1738:                }
                   1739:                pivot = mat[rank];
                   1740:                inv = invm(pivot[j],md);
1.4       noro     1741:                for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   1742:                        if ( *pk ) {
1.24      noro     1743:                                if ( *pk >= (unsigned int)md )
1.18      noro     1744:                                        *pk %= md;
1.4       noro     1745:                                DMAR(*pk,inv,0,md,*pk)
1.1       noro     1746:                        }
                   1747:                for ( i = rank+1; i < row; i++ ) {
                   1748:                        t = mat[i];
1.18      noro     1749:                        if ( a = t[j] )
                   1750:                                red_by_vect(md,t+j,pivot+j,md-a,col-j);
1.1       noro     1751:                }
                   1752:                rank++;
                   1753:        }
                   1754:        for ( j = col-1, l = rank-1; j >= 0; j-- )
                   1755:                if ( colstat[j] ) {
                   1756:                        pivot = mat[l];
                   1757:                        for ( i = 0; i < l; i++ ) {
                   1758:                                t = mat[i];
1.18      noro     1759:                                t[j] %= md;
                   1760:                                if ( a = t[j] )
                   1761:                                        red_by_vect(md,t+j,pivot+j,md-a,col-j);
1.1       noro     1762:                        }
                   1763:                        l--;
1.18      noro     1764:                }
                   1765:        for ( j = 0, l = 0; l < rank; j++ )
                   1766:                if ( colstat[j] ) {
                   1767:                        t = mat[l];
                   1768:                        for ( k = j; k < col; k++ )
1.24      noro     1769:                                if ( t[k] >= (unsigned int)md )
1.18      noro     1770:                                        t[k] %= md;
                   1771:                        l++;
1.32      noro     1772:                }
                   1773:        return rank;
                   1774: }
                   1775:
                   1776: int generic_gauss_elim_sf(int **mat0,int row,int col,int md,int *colstat)
                   1777: {
                   1778:        int i,j,k,l,inv,a,rank;
                   1779:        unsigned int *t,*pivot,*pk;
                   1780:        unsigned int **mat;
                   1781:
                   1782:        mat = (unsigned int **)mat0;
                   1783:        for ( rank = 0, j = 0; j < col; j++ ) {
                   1784:                for ( i = rank; i < row; i++ )
                   1785:                        if ( mat[i][j] )
                   1786:                                break;
                   1787:                if ( i == row ) {
                   1788:                        colstat[j] = 0;
                   1789:                        continue;
                   1790:                } else
                   1791:                        colstat[j] = 1;
                   1792:                if ( i != rank ) {
                   1793:                        t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   1794:                }
                   1795:                pivot = mat[rank];
                   1796:                inv = _invsf(pivot[j]);
                   1797:                for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   1798:                        if ( *pk )
                   1799:                                *pk = _mulsf(*pk,inv);
                   1800:                for ( i = rank+1; i < row; i++ ) {
                   1801:                        t = mat[i];
                   1802:                        if ( a = t[j] )
                   1803:                                red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   1804:                }
                   1805:                rank++;
                   1806:        }
                   1807:        for ( j = col-1, l = rank-1; j >= 0; j-- )
                   1808:                if ( colstat[j] ) {
                   1809:                        pivot = mat[l];
                   1810:                        for ( i = 0; i < l; i++ ) {
                   1811:                                t = mat[i];
                   1812:                                if ( a = t[j] )
                   1813:                                        red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   1814:                        }
                   1815:                        l--;
1.1       noro     1816:                }
                   1817:        return rank;
                   1818: }
                   1819:
                   1820: /* LU decomposition; a[i][i] = 1/U[i][i] */
                   1821:
1.24      noro     1822: int lu_gfmmat(GFMMAT mat,unsigned int md,int *perm)
1.1       noro     1823: {
                   1824:        int row,col;
1.24      noro     1825:        int i,j,k;
1.1       noro     1826:        unsigned int *t,*pivot;
                   1827:        unsigned int **a;
                   1828:        unsigned int inv,m;
                   1829:
                   1830:        row = mat->row; col = mat->col;
                   1831:        a = mat->body;
                   1832:        bzero(perm,row*sizeof(int));
                   1833:
                   1834:        for ( i = 0; i < row; i++ )
                   1835:                perm[i] = i;
                   1836:        for ( k = 0; k < col; k++ ) {
                   1837:                for ( i = k; i < row && !a[i][k]; i++ );
                   1838:                if ( i == row )
                   1839:                        return 0;
                   1840:                if ( i != k ) {
                   1841:                        j = perm[i]; perm[i] = perm[k]; perm[k] = j;
                   1842:                        t = a[i]; a[i] = a[k]; a[k] = t;
                   1843:                }
                   1844:                pivot = a[k];
                   1845:                pivot[k] = inv = invm(pivot[k],md);
                   1846:                for ( i = k+1; i < row; i++ ) {
                   1847:                        t = a[i];
                   1848:                        if ( m = t[k] ) {
                   1849:                                DMAR(inv,m,0,md,t[k])
                   1850:                                for ( j = k+1, m = md - t[k]; j < col; j++ )
                   1851:                                        if ( pivot[j] ) {
1.8       noro     1852:                                                unsigned int tj;
                   1853:
                   1854:                                                DMAR(m,pivot[j],t[j],md,tj)
                   1855:                                                t[j] = tj;
1.1       noro     1856:                                        }
                   1857:                        }
                   1858:                }
                   1859:        }
                   1860:        return 1;
                   1861: }
                   1862:
1.3       noro     1863: /*
                   1864:  Input
                   1865:        a: a row x col matrix
                   1866:        md : a modulus
                   1867:
                   1868:  Output:
                   1869:        return : d = the rank of mat
                   1870:        a[0..(d-1)][0..(d-1)] : LU decomposition (a[i][i] = 1/U[i][i])
                   1871:        rinfo: array of length row
                   1872:        cinfo: array of length col
                   1873:     i-th row in new a <-> rinfo[i]-th row in old a
                   1874:        cinfo[j]=1 <=> j-th column is contained in the LU decomp.
                   1875: */
                   1876:
1.24      noro     1877: int find_lhs_and_lu_mod(unsigned int **a,int row,int col,
                   1878:        unsigned int md,int **rinfo,int **cinfo)
1.3       noro     1879: {
1.24      noro     1880:        int i,j,k,d;
1.3       noro     1881:        int *rp,*cp;
                   1882:        unsigned int *t,*pivot;
                   1883:        unsigned int inv,m;
                   1884:
                   1885:        *rinfo = rp = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   1886:        *cinfo = cp = (int *)MALLOC_ATOMIC(col*sizeof(int));
                   1887:        for ( i = 0; i < row; i++ )
                   1888:                rp[i] = i;
                   1889:        for ( k = 0, d = 0; k < col; k++ ) {
                   1890:                for ( i = d; i < row && !a[i][k]; i++ );
                   1891:                if ( i == row ) {
                   1892:                        cp[k] = 0;
                   1893:                        continue;
                   1894:                } else
                   1895:                        cp[k] = 1;
                   1896:                if ( i != d ) {
                   1897:                        j = rp[i]; rp[i] = rp[d]; rp[d] = j;
                   1898:                        t = a[i]; a[i] = a[d]; a[d] = t;
                   1899:                }
                   1900:                pivot = a[d];
                   1901:                pivot[k] = inv = invm(pivot[k],md);
                   1902:                for ( i = d+1; i < row; i++ ) {
                   1903:                        t = a[i];
                   1904:                        if ( m = t[k] ) {
                   1905:                                DMAR(inv,m,0,md,t[k])
                   1906:                                for ( j = k+1, m = md - t[k]; j < col; j++ )
                   1907:                                        if ( pivot[j] ) {
1.8       noro     1908:                                                unsigned int tj;
                   1909:                                                DMAR(m,pivot[j],t[j],md,tj)
                   1910:                                                t[j] = tj;
1.3       noro     1911:                                        }
                   1912:                        }
                   1913:                }
                   1914:                d++;
                   1915:        }
                   1916:        return d;
                   1917: }
                   1918:
                   1919: /*
                   1920:   Input
                   1921:        a : n x n matrix; a result of LU-decomposition
                   1922:        md : modulus
                   1923:        b : n x l matrix
                   1924:  Output
                   1925:        b = a^(-1)b
                   1926:  */
                   1927:
1.44      noro     1928: void solve_by_lu_mod(int **a,int n,int md,int **b,int l,int normalize)
1.3       noro     1929: {
                   1930:        unsigned int *y,*c;
                   1931:        int i,j,k;
                   1932:        unsigned int t,m,m2;
                   1933:
                   1934:        y = (int *)MALLOC_ATOMIC(n*sizeof(int));
                   1935:        c = (int *)MALLOC_ATOMIC(n*sizeof(int));
                   1936:        m2 = md>>1;
                   1937:        for ( k = 0; k < l; k++ ) {
                   1938:                /* copy b[.][k] to c */
                   1939:                for ( i = 0; i < n; i++ )
                   1940:                        c[i] = (unsigned int)b[i][k];
                   1941:                /* solve Ly=c */
                   1942:                for ( i = 0; i < n; i++ ) {
                   1943:                        for ( t = c[i], j = 0; j < i; j++ )
                   1944:                                if ( a[i][j] ) {
                   1945:                                        m = md - a[i][j];
                   1946:                                        DMAR(m,y[j],t,md,t)
                   1947:                                }
                   1948:                        y[i] = t;
                   1949:                }
                   1950:                /* solve Uc=y */
                   1951:                for ( i = n-1; i >= 0; i-- ) {
                   1952:                        for ( t = y[i], j =i+1; j < n; j++ )
                   1953:                                if ( a[i][j] ) {
                   1954:                                        m = md - a[i][j];
                   1955:                                        DMAR(m,c[j],t,md,t)
                   1956:                                }
                   1957:                        /* a[i][i] = 1/U[i][i] */
                   1958:                        DMAR(t,a[i][i],0,md,c[i])
                   1959:                }
                   1960:                /* copy c to b[.][k] with normalization */
1.44      noro     1961:                if ( normalize )
                   1962:                        for ( i = 0; i < n; i++ )
                   1963:                                b[i][k] = (int)(c[i]>m2 ? c[i]-md : c[i]);
                   1964:                else
                   1965:                        for ( i = 0; i < n; i++ )
                   1966:                                b[i][k] = c[i];
1.3       noro     1967:        }
                   1968: }
                   1969:
1.24      noro     1970: void Pleqm1(NODE arg,VECT *rp)
1.1       noro     1971: {
                   1972:        MAT m;
                   1973:        VECT vect;
                   1974:        pointer **mat;
                   1975:        Q *v;
                   1976:        Q q;
                   1977:        int **wmat;
                   1978:        int md,i,j,row,col,t,n,status;
                   1979:
                   1980:        asir_assert(ARG0(arg),O_MAT,"leqm1");
                   1981:        asir_assert(ARG1(arg),O_N,"leqm1");
                   1982:        m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
                   1983:        row = m->row; col = m->col; mat = m->body;
                   1984:        wmat = (int **)almat(row,col);
                   1985:        for ( i = 0; i < row; i++ )
                   1986:                for ( j = 0; j < col; j++ )
                   1987:                        if ( q = (Q)mat[i][j] ) {
                   1988:                                t = rem(NM(q),md);
                   1989:                                if ( SGN(q) < 0 )
                   1990:                                        t = (md - t) % md;
                   1991:                                wmat[i][j] = t;
                   1992:                        } else
                   1993:                                wmat[i][j] = 0;
                   1994:        status = gauss_elim_mod1(wmat,row,col,md);
                   1995:        if ( status < 0 )
                   1996:                *rp = 0;
                   1997:        else if ( status > 0 )
                   1998:                *rp = (VECT)ONE;
                   1999:        else {
                   2000:                n = col - 1;
                   2001:                MKVECT(vect,n);
                   2002:                for ( i = 0, v = (Q *)vect->body; i < n; i++ ) {
                   2003:                        t = (md-wmat[i][n])%md; STOQ(t,v[i]);
                   2004:                }
                   2005:                *rp = vect;
                   2006:        }
                   2007: }
                   2008:
1.24      noro     2009: int gauss_elim_mod1(int **mat,int row,int col,int md)
1.1       noro     2010: {
                   2011:        int i,j,k,inv,a,n;
                   2012:        int *t,*pivot;
                   2013:
                   2014:        n = col - 1;
                   2015:        for ( j = 0; j < n; j++ ) {
                   2016:                for ( i = j; i < row && !mat[i][j]; i++ );
                   2017:                if ( i == row )
                   2018:                        return 1;
                   2019:                if ( i != j ) {
                   2020:                        t = mat[i]; mat[i] = mat[j]; mat[j] = t;
                   2021:                }
                   2022:                pivot = mat[j];
                   2023:                inv = invm(pivot[j],md);
                   2024:                for ( k = j; k <= n; k++ )
                   2025:                        pivot[k] = dmar(pivot[k],inv,0,md);
                   2026:                for ( i = j+1; i < row; i++ ) {
                   2027:                        t = mat[i];
                   2028:                        if ( i != j && (a = t[j]) )
                   2029:                                for ( k = j, a = md - a; k <= n; k++ )
                   2030:                                        t[k] = dmar(pivot[k],a,t[k],md);
                   2031:                }
                   2032:        }
                   2033:        for ( i = n; i < row && !mat[i][n]; i++ );
                   2034:        if ( i == row ) {
                   2035:                for ( j = n-1; j >= 0; j-- ) {
                   2036:                        for ( i = j-1, a = (md-mat[j][n])%md; i >= 0; i-- ) {
                   2037:                                mat[i][n] = dmar(mat[i][j],a,mat[i][n],md);
                   2038:                                mat[i][j] = 0;
                   2039:                        }
                   2040:                }
                   2041:                return 0;
                   2042:        } else
                   2043:                return -1;
                   2044: }
                   2045:
1.24      noro     2046: void Pgeninvm(NODE arg,LIST *rp)
1.1       noro     2047: {
                   2048:        MAT m;
                   2049:        pointer **mat;
                   2050:        Q **tmat;
                   2051:        Q q;
                   2052:        unsigned int **wmat;
                   2053:        int md,i,j,row,col,t,status;
                   2054:        MAT mat1,mat2;
                   2055:        NODE node1,node2;
                   2056:
                   2057:        asir_assert(ARG0(arg),O_MAT,"leqm1");
                   2058:        asir_assert(ARG1(arg),O_N,"leqm1");
                   2059:        m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
                   2060:        row = m->row; col = m->col; mat = m->body;
                   2061:        wmat = (unsigned int **)almat(row,col+row);
                   2062:        for ( i = 0; i < row; i++ ) {
                   2063:                bzero((char *)wmat[i],(col+row)*sizeof(int));
                   2064:                for ( j = 0; j < col; j++ )
                   2065:                        if ( q = (Q)mat[i][j] ) {
                   2066:                                t = rem(NM(q),md);
                   2067:                                if ( SGN(q) < 0 )
                   2068:                                        t = (md - t) % md;
                   2069:                                wmat[i][j] = t;
                   2070:                        }
                   2071:                wmat[i][col+i] = 1;
                   2072:        }
                   2073:        status = gauss_elim_geninv_mod(wmat,row,col,md);
                   2074:        if ( status > 0 )
                   2075:                *rp = 0;
                   2076:        else {
                   2077:                MKMAT(mat1,col,row); MKMAT(mat2,row-col,row);
                   2078:                for ( i = 0, tmat = (Q **)mat1->body; i < col; i++ )
                   2079:                        for ( j = 0; j < row; j++ )
1.24      noro     2080:                                UTOQ(wmat[i][j+col],tmat[i][j]);
1.1       noro     2081:                for ( tmat = (Q **)mat2->body; i < row; i++ )
                   2082:                        for ( j = 0; j < row; j++ )
1.24      noro     2083:                                UTOQ(wmat[i][j+col],tmat[i-col][j]);
1.1       noro     2084:                MKNODE(node2,mat2,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
                   2085:        }
                   2086: }
                   2087:
1.24      noro     2088: int gauss_elim_geninv_mod(unsigned int **mat,int row,int col,int md)
1.1       noro     2089: {
                   2090:        int i,j,k,inv,a,n,m;
                   2091:        unsigned int *t,*pivot;
                   2092:
                   2093:        n = col; m = row+col;
                   2094:        for ( j = 0; j < n; j++ ) {
                   2095:                for ( i = j; i < row && !mat[i][j]; i++ );
                   2096:                if ( i == row )
                   2097:                        return 1;
                   2098:                if ( i != j ) {
                   2099:                        t = mat[i]; mat[i] = mat[j]; mat[j] = t;
                   2100:                }
                   2101:                pivot = mat[j];
                   2102:                inv = invm(pivot[j],md);
                   2103:                for ( k = j; k < m; k++ )
                   2104:                        pivot[k] = dmar(pivot[k],inv,0,md);
                   2105:                for ( i = j+1; i < row; i++ ) {
                   2106:                        t = mat[i];
                   2107:                        if ( a = t[j] )
                   2108:                                for ( k = j, a = md - a; k < m; k++ )
                   2109:                                        t[k] = dmar(pivot[k],a,t[k],md);
                   2110:                }
                   2111:        }
                   2112:        for ( j = n-1; j >= 0; j-- ) {
                   2113:                pivot = mat[j];
                   2114:                for ( i = j-1; i >= 0; i-- ) {
                   2115:                        t = mat[i];
                   2116:                        if ( a = t[j] )
                   2117:                                for ( k = j, a = md - a; k < m; k++ )
                   2118:                                        t[k] = dmar(pivot[k],a,t[k],md);
                   2119:                }
                   2120:        }
                   2121:        return 0;
                   2122: }
                   2123:
1.24      noro     2124: void Psolve_by_lu_gfmmat(NODE arg,VECT *rp)
1.1       noro     2125: {
                   2126:        GFMMAT lu;
                   2127:        Q *perm,*rhs,*v;
                   2128:        int n,i;
                   2129:        unsigned int md;
                   2130:        unsigned int *b,*sol;
                   2131:        VECT r;
                   2132:
                   2133:        lu = (GFMMAT)ARG0(arg);
                   2134:        perm = (Q *)BDY((VECT)ARG1(arg));
                   2135:        rhs = (Q *)BDY((VECT)ARG2(arg));
                   2136:        md = (unsigned int)QTOS((Q)ARG3(arg));
                   2137:        n = lu->col;
                   2138:        b = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
                   2139:        sol = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
                   2140:        for ( i = 0; i < n; i++ )
                   2141:                b[i] = QTOS(rhs[QTOS(perm[i])]);
                   2142:        solve_by_lu_gfmmat(lu,md,b,sol);
                   2143:        MKVECT(r,n);
                   2144:        for ( i = 0, v = (Q *)r->body; i < n; i++ )
1.24      noro     2145:                        UTOQ(sol[i],v[i]);
1.1       noro     2146:        *rp = r;
                   2147: }
                   2148:
1.24      noro     2149: void solve_by_lu_gfmmat(GFMMAT lu,unsigned int md,
                   2150:        unsigned int *b,unsigned int *x)
1.1       noro     2151: {
                   2152:        int n;
                   2153:        unsigned int **a;
                   2154:        unsigned int *y;
                   2155:        int i,j;
                   2156:        unsigned int t,m;
                   2157:
                   2158:        n = lu->col;
                   2159:        a = lu->body;
                   2160:        y = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
                   2161:        /* solve Ly=b */
                   2162:        for ( i = 0; i < n; i++ ) {
                   2163:                for ( t = b[i], j = 0; j < i; j++ )
                   2164:                        if ( a[i][j] ) {
                   2165:                                m = md - a[i][j];
                   2166:                                DMAR(m,y[j],t,md,t)
                   2167:                        }
                   2168:                y[i] = t;
                   2169:        }
                   2170:        /* solve Ux=y */
                   2171:        for ( i = n-1; i >= 0; i-- ) {
                   2172:                for ( t = y[i], j =i+1; j < n; j++ )
                   2173:                        if ( a[i][j] ) {
                   2174:                                m = md - a[i][j];
                   2175:                                DMAR(m,x[j],t,md,t)
                   2176:                        }
                   2177:                /* a[i][i] = 1/U[i][i] */
                   2178:                DMAR(t,a[i][i],0,md,x[i])
                   2179:        }
                   2180: }
                   2181:
1.24      noro     2182: void Plu_gfmmat(NODE arg,LIST *rp)
1.1       noro     2183: {
                   2184:        MAT m;
                   2185:        GFMMAT mm;
                   2186:        unsigned int md;
                   2187:        int i,row,col,status;
                   2188:        int *iperm;
                   2189:        Q *v;
                   2190:        VECT perm;
                   2191:        NODE n0;
                   2192:
                   2193:        asir_assert(ARG0(arg),O_MAT,"mat_to_gfmmat");
                   2194:        asir_assert(ARG1(arg),O_N,"mat_to_gfmmat");
                   2195:        m = (MAT)ARG0(arg); md = (unsigned int)QTOS((Q)ARG1(arg));
                   2196:        mat_to_gfmmat(m,md,&mm);
                   2197:        row = m->row;
                   2198:        col = m->col;
                   2199:        iperm = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   2200:        status = lu_gfmmat(mm,md,iperm);
                   2201:        if ( !status )
                   2202:                n0 = 0;
                   2203:        else {
                   2204:                MKVECT(perm,row);
                   2205:                for ( i = 0, v = (Q *)perm->body; i < row; i++ )
                   2206:                        STOQ(iperm[i],v[i]);
                   2207:                n0 = mknode(2,mm,perm);
                   2208:        }
                   2209:        MKLIST(*rp,n0);
                   2210: }
                   2211:
1.24      noro     2212: void Pmat_to_gfmmat(NODE arg,GFMMAT *rp)
1.1       noro     2213: {
                   2214:        MAT m;
                   2215:        unsigned int md;
                   2216:
                   2217:        asir_assert(ARG0(arg),O_MAT,"mat_to_gfmmat");
                   2218:        asir_assert(ARG1(arg),O_N,"mat_to_gfmmat");
                   2219:        m = (MAT)ARG0(arg); md = (unsigned int)QTOS((Q)ARG1(arg));
                   2220:        mat_to_gfmmat(m,md,rp);
                   2221: }
                   2222:
1.24      noro     2223: void mat_to_gfmmat(MAT m,unsigned int md,GFMMAT *rp)
1.1       noro     2224: {
                   2225:        unsigned int **wmat;
                   2226:        unsigned int t;
                   2227:        Q **mat;
                   2228:        Q q;
                   2229:        int i,j,row,col;
                   2230:
                   2231:        row = m->row; col = m->col; mat = (Q **)m->body;
                   2232:        wmat = (unsigned int **)almat(row,col);
                   2233:        for ( i = 0; i < row; i++ ) {
                   2234:                bzero((char *)wmat[i],col*sizeof(unsigned int));
                   2235:                for ( j = 0; j < col; j++ )
                   2236:                        if ( q = mat[i][j] ) {
                   2237:                                t = (unsigned int)rem(NM(q),md);
                   2238:                                if ( SGN(q) < 0 )
                   2239:                                        t = (md - t) % md;
                   2240:                                wmat[i][j] = t;
                   2241:                        }
                   2242:        }
                   2243:        TOGFMMAT(row,col,wmat,*rp);
                   2244: }
                   2245:
1.27      noro     2246: void Pgeninvm_swap(arg,rp)
                   2247: NODE arg;
                   2248: LIST *rp;
1.1       noro     2249: {
                   2250:        MAT m;
                   2251:        pointer **mat;
                   2252:        Q **tmat;
                   2253:        Q *tvect;
                   2254:        Q q;
                   2255:        unsigned int **wmat,**invmat;
                   2256:        int *index;
                   2257:        unsigned int t,md;
                   2258:        int i,j,row,col,status;
                   2259:        MAT mat1;
                   2260:        VECT vect1;
                   2261:        NODE node1,node2;
                   2262:
                   2263:        asir_assert(ARG0(arg),O_MAT,"geninvm_swap");
                   2264:        asir_assert(ARG1(arg),O_N,"geninvm_swap");
                   2265:        m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
                   2266:        row = m->row; col = m->col; mat = m->body;
                   2267:        wmat = (unsigned int **)almat(row,col+row);
                   2268:        for ( i = 0; i < row; i++ ) {
                   2269:                bzero((char *)wmat[i],(col+row)*sizeof(int));
                   2270:                for ( j = 0; j < col; j++ )
                   2271:                        if ( q = (Q)mat[i][j] ) {
                   2272:                                t = (unsigned int)rem(NM(q),md);
                   2273:                                if ( SGN(q) < 0 )
                   2274:                                        t = (md - t) % md;
                   2275:                                wmat[i][j] = t;
                   2276:                        }
                   2277:                wmat[i][col+i] = 1;
                   2278:        }
                   2279:        status = gauss_elim_geninv_mod_swap(wmat,row,col,md,&invmat,&index);
                   2280:        if ( status > 0 )
                   2281:                *rp = 0;
                   2282:        else {
                   2283:                MKMAT(mat1,col,col);
                   2284:                for ( i = 0, tmat = (Q **)mat1->body; i < col; i++ )
                   2285:                        for ( j = 0; j < col; j++ )
                   2286:                                UTOQ(invmat[i][j],tmat[i][j]);
                   2287:                MKVECT(vect1,row);
                   2288:                for ( i = 0, tvect = (Q *)vect1->body; i < row; i++ )
                   2289:                        STOQ(index[i],tvect[i]);
                   2290:                MKNODE(node2,vect1,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
                   2291:        }
                   2292: }
                   2293:
1.27      noro     2294: gauss_elim_geninv_mod_swap(mat,row,col,md,invmatp,indexp)
                   2295: unsigned int **mat;
                   2296: int row,col;
                   2297: unsigned int md;
                   2298: unsigned int ***invmatp;
                   2299: int **indexp;
1.1       noro     2300: {
                   2301:        int i,j,k,inv,a,n,m;
                   2302:        unsigned int *t,*pivot,*s;
                   2303:        int *index;
                   2304:        unsigned int **invmat;
                   2305:
                   2306:        n = col; m = row+col;
                   2307:        *indexp = index = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   2308:        for ( i = 0; i < row; i++ )
                   2309:                index[i] = i;
                   2310:        for ( j = 0; j < n; j++ ) {
                   2311:                for ( i = j; i < row && !mat[i][j]; i++ );
                   2312:                if ( i == row ) {
                   2313:                        *indexp = 0; *invmatp = 0; return 1;
                   2314:                }
                   2315:                if ( i != j ) {
                   2316:                        t = mat[i]; mat[i] = mat[j]; mat[j] = t;
                   2317:                        k = index[i]; index[i] = index[j]; index[j] = k;
                   2318:                }
                   2319:                pivot = mat[j];
                   2320:                inv = (unsigned int)invm(pivot[j],md);
                   2321:                for ( k = j; k < m; k++ )
                   2322:                        if ( pivot[k] )
                   2323:                                pivot[k] = (unsigned int)dmar(pivot[k],inv,0,md);
                   2324:                for ( i = j+1; i < row; i++ ) {
                   2325:                        t = mat[i];
                   2326:                        if ( a = t[j] )
                   2327:                                for ( k = j, a = md - a; k < m; k++ )
                   2328:                                        if ( pivot[k] )
                   2329:                                                t[k] = dmar(pivot[k],a,t[k],md);
                   2330:                }
                   2331:        }
                   2332:        for ( j = n-1; j >= 0; j-- ) {
                   2333:                pivot = mat[j];
                   2334:                for ( i = j-1; i >= 0; i-- ) {
                   2335:                        t = mat[i];
                   2336:                        if ( a = t[j] )
                   2337:                                for ( k = j, a = md - a; k < m; k++ )
                   2338:                                        if ( pivot[k] )
                   2339:                                                t[k] = dmar(pivot[k],a,t[k],md);
                   2340:                }
                   2341:        }
                   2342:        *invmatp = invmat = (unsigned int **)almat(col,col);
1.27      noro     2343:        for ( i = 0; i < col; i++ )
                   2344:                for ( j = 0, s = invmat[i], t = mat[i]; j < col; j++ )
                   2345:                        s[j] = t[col+index[j]];
                   2346:        return 0;
                   2347: }
                   2348:
                   2349: void Pgeninv_sf_swap(NODE arg,LIST *rp)
                   2350: {
                   2351:        MAT m;
                   2352:        GFS **mat,**tmat;
                   2353:        Q *tvect;
                   2354:        GFS q;
                   2355:        int **wmat,**invmat;
                   2356:        int *index;
                   2357:        unsigned int t;
                   2358:        int i,j,row,col,status;
                   2359:        MAT mat1;
                   2360:        VECT vect1;
                   2361:        NODE node1,node2;
                   2362:
                   2363:        asir_assert(ARG0(arg),O_MAT,"geninv_sf_swap");
                   2364:        m = (MAT)ARG0(arg);
                   2365:        row = m->row; col = m->col; mat = (GFS **)m->body;
                   2366:        wmat = (int **)almat(row,col+row);
                   2367:        for ( i = 0; i < row; i++ ) {
                   2368:                bzero((char *)wmat[i],(col+row)*sizeof(int));
                   2369:                for ( j = 0; j < col; j++ )
                   2370:                        if ( q = (GFS)mat[i][j] )
                   2371:                                wmat[i][j] = FTOIF(CONT(q));
                   2372:                wmat[i][col+i] = _onesf();
                   2373:        }
                   2374:        status = gauss_elim_geninv_sf_swap(wmat,row,col,&invmat,&index);
                   2375:        if ( status > 0 )
                   2376:                *rp = 0;
                   2377:        else {
                   2378:                MKMAT(mat1,col,col);
                   2379:                for ( i = 0, tmat = (GFS **)mat1->body; i < col; i++ )
                   2380:                        for ( j = 0; j < col; j++ )
                   2381:                                if ( t = invmat[i][j] ) {
                   2382:                                        MKGFS(IFTOF(t),tmat[i][j]);
                   2383:                                }
                   2384:                MKVECT(vect1,row);
                   2385:                for ( i = 0, tvect = (Q *)vect1->body; i < row; i++ )
                   2386:                        STOQ(index[i],tvect[i]);
                   2387:                MKNODE(node2,vect1,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
                   2388:        }
                   2389: }
                   2390:
                   2391: int gauss_elim_geninv_sf_swap(int **mat,int row,int col,
                   2392:        int ***invmatp,int **indexp)
                   2393: {
                   2394:        int i,j,k,inv,a,n,m,u;
                   2395:        int *t,*pivot,*s;
                   2396:        int *index;
                   2397:        int **invmat;
                   2398:
                   2399:        n = col; m = row+col;
                   2400:        *indexp = index = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   2401:        for ( i = 0; i < row; i++ )
                   2402:                index[i] = i;
                   2403:        for ( j = 0; j < n; j++ ) {
                   2404:                for ( i = j; i < row && !mat[i][j]; i++ );
                   2405:                if ( i == row ) {
                   2406:                        *indexp = 0; *invmatp = 0; return 1;
                   2407:                }
                   2408:                if ( i != j ) {
                   2409:                        t = mat[i]; mat[i] = mat[j]; mat[j] = t;
                   2410:                        k = index[i]; index[i] = index[j]; index[j] = k;
                   2411:                }
                   2412:                pivot = mat[j];
                   2413:                inv = _invsf(pivot[j]);
                   2414:                for ( k = j; k < m; k++ )
                   2415:                        if ( pivot[k] )
                   2416:                                pivot[k] = _mulsf(pivot[k],inv);
                   2417:                for ( i = j+1; i < row; i++ ) {
                   2418:                        t = mat[i];
                   2419:                        if ( a = t[j] )
                   2420:                                for ( k = j, a = _chsgnsf(a); k < m; k++ )
                   2421:                                        if ( pivot[k] ) {
                   2422:                                                u = _mulsf(pivot[k],a);
                   2423:                                                t[k] = _addsf(u,t[k]);
                   2424:                                        }
                   2425:                }
                   2426:        }
                   2427:        for ( j = n-1; j >= 0; j-- ) {
                   2428:                pivot = mat[j];
                   2429:                for ( i = j-1; i >= 0; i-- ) {
                   2430:                        t = mat[i];
                   2431:                        if ( a = t[j] )
                   2432:                                for ( k = j, a = _chsgnsf(a); k < m; k++ )
                   2433:                                        if ( pivot[k] ) {
                   2434:                                                u = _mulsf(pivot[k],a);
                   2435:                                                t[k] = _addsf(u,t[k]);
                   2436:                                        }
                   2437:                }
                   2438:        }
                   2439:        *invmatp = invmat = (int **)almat(col,col);
1.1       noro     2440:        for ( i = 0; i < col; i++ )
                   2441:                for ( j = 0, s = invmat[i], t = mat[i]; j < col; j++ )
                   2442:                        s[j] = t[col+index[j]];
                   2443:        return 0;
                   2444: }
                   2445:
                   2446: void _addn(N,N,N);
                   2447: int _subn(N,N,N);
                   2448: void _muln(N,N,N);
                   2449:
1.24      noro     2450: void inner_product_int(Q *a,Q *b,int n,Q *r)
1.1       noro     2451: {
                   2452:        int la,lb,i;
                   2453:        int sgn,sgn1;
                   2454:        N wm,wma,sum,t;
                   2455:
                   2456:        for ( la = lb = 0, i = 0; i < n; i++ ) {
                   2457:                if ( a[i] )
                   2458:                        if ( DN(a[i]) )
                   2459:                                error("inner_product_int : invalid argument");
                   2460:                        else
                   2461:                                la = MAX(PL(NM(a[i])),la);
                   2462:                if ( b[i] )
                   2463:                        if ( DN(b[i]) )
                   2464:                                error("inner_product_int : invalid argument");
                   2465:                        else
                   2466:                                lb = MAX(PL(NM(b[i])),lb);
                   2467:        }
                   2468:        sgn = 0;
                   2469:        sum= NALLOC(la+lb+2);
                   2470:        bzero((char *)sum,(la+lb+3)*sizeof(unsigned int));
                   2471:        wm = NALLOC(la+lb+2);
                   2472:        wma = NALLOC(la+lb+2);
                   2473:        for ( i = 0; i < n; i++ ) {
                   2474:                if ( !a[i] || !b[i] )
                   2475:                        continue;
                   2476:                _muln(NM(a[i]),NM(b[i]),wm);
                   2477:                sgn1 = SGN(a[i])*SGN(b[i]);
                   2478:                if ( !sgn ) {
                   2479:                        sgn = sgn1;
                   2480:                        t = wm; wm = sum; sum = t;
                   2481:                } else if ( sgn == sgn1 ) {
                   2482:                        _addn(sum,wm,wma);
                   2483:                        if ( !PL(wma) )
                   2484:                                sgn = 0;
                   2485:                        t = wma; wma = sum; sum = t;
                   2486:                } else {
                   2487:                        /* sgn*sum+sgn1*wm = sgn*(sum-wm) */
                   2488:                        sgn *= _subn(sum,wm,wma);
                   2489:                        t = wma; wma = sum; sum = t;
                   2490:                }
                   2491:        }
                   2492:        GC_free(wm);
                   2493:        GC_free(wma);
                   2494:        if ( !sgn ) {
                   2495:                GC_free(sum);
                   2496:                *r = 0;
                   2497:        } else
                   2498:                NTOQ(sum,sgn,*r);
                   2499: }
                   2500:
1.3       noro     2501: /* (k,l) element of a*b where a: .x n matrix, b: n x . integer matrix */
                   2502:
1.24      noro     2503: void inner_product_mat_int_mod(Q **a,int **b,int n,int k,int l,Q *r)
1.3       noro     2504: {
                   2505:        int la,lb,i;
                   2506:        int sgn,sgn1;
                   2507:        N wm,wma,sum,t;
                   2508:        Q aki;
                   2509:        int bil,bilsgn;
                   2510:        struct oN tn;
                   2511:
                   2512:        for ( la = 0, i = 0; i < n; i++ ) {
                   2513:                if ( aki = a[k][i] )
                   2514:                        if ( DN(aki) )
                   2515:                                error("inner_product_int : invalid argument");
                   2516:                        else
                   2517:                                la = MAX(PL(NM(aki)),la);
                   2518:        }
                   2519:        lb = 1;
                   2520:        sgn = 0;
                   2521:        sum= NALLOC(la+lb+2);
                   2522:        bzero((char *)sum,(la+lb+3)*sizeof(unsigned int));
                   2523:        wm = NALLOC(la+lb+2);
                   2524:        wma = NALLOC(la+lb+2);
                   2525:        for ( i = 0; i < n; i++ ) {
                   2526:                if ( !(aki = a[k][i]) || !(bil = b[i][l]) )
                   2527:                        continue;
                   2528:                tn.p = 1;
                   2529:                if ( bil > 0 ) {
                   2530:                        tn.b[0] = bil; bilsgn = 1;
                   2531:                } else {
                   2532:                        tn.b[0] = -bil; bilsgn = -1;
                   2533:                }
                   2534:                _muln(NM(aki),&tn,wm);
                   2535:                sgn1 = SGN(aki)*bilsgn;
                   2536:                if ( !sgn ) {
                   2537:                        sgn = sgn1;
                   2538:                        t = wm; wm = sum; sum = t;
                   2539:                } else if ( sgn == sgn1 ) {
                   2540:                        _addn(sum,wm,wma);
                   2541:                        if ( !PL(wma) )
                   2542:                                sgn = 0;
                   2543:                        t = wma; wma = sum; sum = t;
                   2544:                } else {
                   2545:                        /* sgn*sum+sgn1*wm = sgn*(sum-wm) */
                   2546:                        sgn *= _subn(sum,wm,wma);
                   2547:                        t = wma; wma = sum; sum = t;
                   2548:                }
                   2549:        }
                   2550:        GC_free(wm);
                   2551:        GC_free(wma);
                   2552:        if ( !sgn ) {
                   2553:                GC_free(sum);
                   2554:                *r = 0;
                   2555:        } else
                   2556:                NTOQ(sum,sgn,*r);
                   2557: }
                   2558:
1.24      noro     2559: void Pmul_mat_vect_int(NODE arg,VECT *rp)
1.1       noro     2560: {
                   2561:        MAT mat;
                   2562:        VECT vect,r;
                   2563:        int row,col,i;
                   2564:
                   2565:        mat = (MAT)ARG0(arg);
                   2566:        vect = (VECT)ARG1(arg);
                   2567:        row = mat->row;
                   2568:        col = mat->col;
                   2569:        MKVECT(r,row);
1.24      noro     2570:        for ( i = 0; i < row; i++ ) {
                   2571:                inner_product_int((Q *)mat->body[i],(Q *)vect->body,col,(Q *)&r->body[i]);
                   2572:        }
1.1       noro     2573:        *rp = r;
                   2574: }
                   2575:
1.24      noro     2576: void Pnbpoly_up2(NODE arg,GF2N *rp)
1.1       noro     2577: {
                   2578:        int m,type,ret;
                   2579:        UP2 r;
                   2580:
                   2581:        m = QTOS((Q)ARG0(arg));
                   2582:        type = QTOS((Q)ARG1(arg));
                   2583:        ret = generate_ONB_polynomial(&r,m,type);
                   2584:        if ( ret == 0 )
                   2585:                MKGF2N(r,*rp);
                   2586:        else
                   2587:                *rp = 0;
                   2588: }
                   2589:
1.24      noro     2590: void Px962_irredpoly_up2(NODE arg,GF2N *rp)
1.1       noro     2591: {
1.24      noro     2592:        int m,ret,w;
1.1       noro     2593:        GF2N prev;
                   2594:        UP2 r;
                   2595:
                   2596:        m = QTOS((Q)ARG0(arg));
                   2597:        prev = (GF2N)ARG1(arg);
                   2598:        if ( !prev ) {
                   2599:                w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
                   2600:                bzero((char *)r->b,w*sizeof(unsigned int));
                   2601:        } else {
                   2602:                r = prev->body;
                   2603:                if ( degup2(r) != m ) {
                   2604:                        w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
                   2605:                        bzero((char *)r->b,w*sizeof(unsigned int));
                   2606:                }
                   2607:        }
1.24      noro     2608:        ret = _generate_irreducible_polynomial(r,m);
1.1       noro     2609:        if ( ret == 0 )
                   2610:                MKGF2N(r,*rp);
                   2611:        else
                   2612:                *rp = 0;
                   2613: }
                   2614:
1.24      noro     2615: void Pirredpoly_up2(NODE arg,GF2N *rp)
1.1       noro     2616: {
1.24      noro     2617:        int m,ret,w;
1.1       noro     2618:        GF2N prev;
                   2619:        UP2 r;
                   2620:
                   2621:        m = QTOS((Q)ARG0(arg));
                   2622:        prev = (GF2N)ARG1(arg);
                   2623:        if ( !prev ) {
                   2624:                w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
                   2625:                bzero((char *)r->b,w*sizeof(unsigned int));
                   2626:        } else {
                   2627:                r = prev->body;
                   2628:                if ( degup2(r) != m ) {
                   2629:                        w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
                   2630:                        bzero((char *)r->b,w*sizeof(unsigned int));
                   2631:                }
                   2632:        }
1.24      noro     2633:        ret = _generate_good_irreducible_polynomial(r,m);
1.1       noro     2634:        if ( ret == 0 )
                   2635:                MKGF2N(r,*rp);
                   2636:        else
                   2637:                *rp = 0;
                   2638: }
                   2639:
1.26      noro     2640: void Pmat_swap_row_destructive(NODE arg, MAT *m)
                   2641: {
                   2642:        int i1,i2;
                   2643:        pointer *t;
                   2644:        MAT mat;
                   2645:
                   2646:        asir_assert(ARG0(arg),O_MAT,"mat_swap_row_destructive");
                   2647:        asir_assert(ARG1(arg),O_N,"mat_swap_row_destructive");
                   2648:        asir_assert(ARG2(arg),O_N,"mat_swap_row_destructive");
                   2649:        mat = (MAT)ARG0(arg);
                   2650:        i1 = QTOS((Q)ARG1(arg));
                   2651:        i2 = QTOS((Q)ARG2(arg));
                   2652:        if ( i1 < 0 || i2 < 0 || i1 >= mat->row || i2 >= mat->row )
                   2653:                error("mat_swap_row_destructive : Out of range");
                   2654:        t = mat->body[i1];
                   2655:        mat->body[i1] = mat->body[i2];
                   2656:        mat->body[i2] = t;
                   2657:        *m = mat;
                   2658: }
                   2659:
                   2660: void Pmat_swap_col_destructive(NODE arg, MAT *m)
                   2661: {
                   2662:        int j1,j2,i,n;
                   2663:        pointer *mi;
                   2664:        pointer t;
                   2665:        MAT mat;
                   2666:
                   2667:        asir_assert(ARG0(arg),O_MAT,"mat_swap_col_destructive");
                   2668:        asir_assert(ARG1(arg),O_N,"mat_swap_col_destructive");
                   2669:        asir_assert(ARG2(arg),O_N,"mat_swap_col_destructive");
                   2670:        mat = (MAT)ARG0(arg);
                   2671:        j1 = QTOS((Q)ARG1(arg));
                   2672:        j2 = QTOS((Q)ARG2(arg));
                   2673:        if ( j1 < 0 || j2 < 0 || j1 >= mat->col || j2 >= mat->col )
                   2674:                error("mat_swap_col_destructive : Out of range");
                   2675:        n = mat->row;
                   2676:        for ( i = 0; i < n; i++ ) {
                   2677:                mi = mat->body[i];
                   2678:                t = mi[j1]; mi[j1] = mi[j2]; mi[j2] = t;
                   2679:        }
                   2680:        *m = mat;
                   2681: }
1.1       noro     2682: /*
                   2683:  * f = type 'type' normal polynomial of degree m if exists
                   2684:  * IEEE P1363 A.7.2
                   2685:  *
                   2686:  * return value : 0  --- exists
                   2687:  *                1  --- does not exist
                   2688:  *                -1 --- failure (memory allocation error)
                   2689:  */
                   2690:
                   2691: int generate_ONB_polynomial(UP2 *rp,int m,int type)
                   2692: {
                   2693:        int i,r;
                   2694:        int w;
                   2695:        UP2 f,f0,f1,f2,t;
                   2696:
                   2697:        w = (m>>5)+1;
                   2698:        switch ( type ) {
                   2699:                case 1:
                   2700:                        if ( !TypeT_NB_check(m,1) ) return 1;
                   2701:                        NEWUP2(f,w); *rp = f; f->w = w;
                   2702:                        /* set all the bits */
                   2703:                        for ( i = 0; i < w; i++ )
                   2704:                                f->b[i] = 0xffffffff;
                   2705:                        /* mask the top word if necessary */
                   2706:                        if ( r = (m+1)&31 )
                   2707:                                f->b[w-1] &= (1<<r)-1;
                   2708:                        return 0;
                   2709:                        break;
                   2710:                case 2:
                   2711:                        if ( !TypeT_NB_check(m,2) ) return 1;
                   2712:                        NEWUP2(f,w); *rp = f;
                   2713:                        W_NEWUP2(f0,w);
                   2714:                        W_NEWUP2(f1,w);
                   2715:                        W_NEWUP2(f2,w);
                   2716:
                   2717:                        /* recursion for genrating Type II normal polynomial */
                   2718:
                   2719:                        /* f0 = 1, f1 = t+1 */
                   2720:                        f0->w = 1; f0->b[0] = 1;
                   2721:                        f1->w = 1; f1->b[0] = 3;
                   2722:                        for ( i = 2; i <= m; i++ ) {
                   2723:                                /* f2 = t*f1+f0 */
                   2724:                                _bshiftup2(f1,-1,f2);
                   2725:                                _addup2_destructive(f2,f0);
                   2726:                                /* cyclic change of the variables */
                   2727:                                t = f0; f0 = f1; f1 = f2; f2 = t;
                   2728:                        }
                   2729:                        _copyup2(f1,f);
                   2730:                        return 0;
                   2731:                        break;
                   2732:                default:
                   2733:                        return -1;
                   2734:                        break;
                   2735:                }
                   2736: }
                   2737:
                   2738: /*
                   2739:  * f = an irreducible trinomial or pentanomial of degree d 'after' f
                   2740:  * return value : 0  --- exists
                   2741:  *                1  --- does not exist (exhaustion)
                   2742:  */
                   2743:
                   2744: int _generate_irreducible_polynomial(UP2 f,int d)
                   2745: {
                   2746:        int ret,i,j,k,nz,i0,j0,k0;
                   2747:        int w;
                   2748:        unsigned int *fd;
                   2749:
                   2750:        /*
                   2751:         * if f = x^d+x^i+1 then i0 <- i, j0 <- 0, k0 <-0.
                   2752:         * if f = x^d+x^k+x^j+x^i+1 (k>j>i) then i0 <- i, j0 <- j, k0 <-k.
                   2753:         * otherwise i0,j0,k0 is set to 0.
                   2754:         */
                   2755:
                   2756:        fd = f->b;
                   2757:        w = (d>>5)+1;
                   2758:        if ( f->w && (d==degup2(f)) ) {
                   2759:                for ( nz = 0, i = d; i >= 0; i-- )
                   2760:                        if ( fd[i>>5]&(1<<(i&31)) ) nz++;
                   2761:                switch ( nz ) {
                   2762:                        case 3:
                   2763:                                for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
                   2764:                                /* reset i0-th bit */
                   2765:                                fd[i0>>5] &= ~(1<<(i0&31));
                   2766:                                j0 = k0 = 0;
                   2767:                                break;
                   2768:                        case 5:
                   2769:                                for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
                   2770:                                /* reset i0-th bit */
                   2771:                                fd[i0>>5] &= ~(1<<(i0&31));
                   2772:                                for ( j0 = i0+1; !(fd[j0>>5]&(1<<(j0&31))) ; j0++ );
                   2773:                                /* reset j0-th bit */
                   2774:                                fd[j0>>5] &= ~(1<<(j0&31));
                   2775:                                for ( k0 = j0+1; !(fd[k0>>5]&(1<<(k0&31))) ; k0++ );
                   2776:                                /* reset k0-th bit */
                   2777:                                fd[k0>>5] &= ~(1<<(k0&31));
                   2778:                                break;
                   2779:                        default:
                   2780:                                f->w = 0; break;
                   2781:                }
                   2782:        } else
                   2783:                f->w = 0;
                   2784:
                   2785:        if ( !f->w ) {
                   2786:                fd = f->b;
                   2787:                f->w = w; fd[0] |= 1; fd[d>>5] |= (1<<(d&31));
                   2788:                i0 = j0 = k0 = 0;
                   2789:        }
                   2790:        /* if j0 > 0 then f is already a pentanomial */
                   2791:        if ( j0 > 0 ) goto PENTA;
                   2792:
                   2793:        /* searching for an irreducible trinomial */
                   2794:
                   2795:        for ( i = 1; 2*i <= d; i++ ) {
                   2796:                /* skip the polynomials 'before' f */
                   2797:                if ( i < i0 ) continue;
                   2798:                if ( i == i0 ) { i0 = 0; continue; }
                   2799:                /* set i-th bit */
                   2800:                fd[i>>5] |= (1<<(i&31));
                   2801:                ret = irredcheck_dddup2(f);
                   2802:                if ( ret == 1 ) return 0;
                   2803:                /* reset i-th bit */
                   2804:                fd[i>>5] &= ~(1<<(i&31));
                   2805:        }
                   2806:
                   2807:        /* searching for an irreducible pentanomial */
                   2808: PENTA:
                   2809:        for ( i = 1; i < d; i++ ) {
                   2810:                /* skip the polynomials 'before' f */
                   2811:                if ( i < i0 ) continue;
                   2812:                if ( i == i0 ) i0 = 0;
                   2813:                /* set i-th bit */
                   2814:                fd[i>>5] |= (1<<(i&31));
                   2815:                for ( j = i+1; j < d; j++ ) {
                   2816:                        /* skip the polynomials 'before' f */
                   2817:                        if ( j < j0 ) continue;
                   2818:                        if ( j == j0 ) j0 = 0;
                   2819:                        /* set j-th bit */
                   2820:                        fd[j>>5] |= (1<<(j&31));
                   2821:                        for ( k = j+1; k < d; k++ ) {
                   2822:                                /* skip the polynomials 'before' f */
                   2823:                                if ( k < k0 ) continue;
                   2824:                                else if ( k == k0 ) { k0 = 0; continue; }
                   2825:                                /* set k-th bit */
                   2826:                                fd[k>>5] |= (1<<(k&31));
                   2827:                                ret = irredcheck_dddup2(f);
                   2828:                                if ( ret == 1 ) return 0;
                   2829:                                /* reset k-th bit */
                   2830:                                fd[k>>5] &= ~(1<<(k&31));
                   2831:                        }
                   2832:                        /* reset j-th bit */
                   2833:                        fd[j>>5] &= ~(1<<(j&31));
                   2834:                }
                   2835:                /* reset i-th bit */
                   2836:                fd[i>>5] &= ~(1<<(i&31));
                   2837:        }
                   2838:        /* exhausted */
                   2839:        return 1;
                   2840: }
                   2841:
                   2842: /*
                   2843:  * f = an irreducible trinomial or pentanomial of degree d 'after' f
                   2844:  *
                   2845:  * searching strategy:
                   2846:  *   trinomial x^d+x^i+1:
                   2847:  *         i is as small as possible.
                   2848:  *   trinomial x^d+x^i+x^j+x^k+1:
                   2849:  *         i is as small as possible.
                   2850:  *         For such i, j is as small as possible.
                   2851:  *         For such i and j, 'k' is as small as possible.
                   2852:  *
                   2853:  * return value : 0  --- exists
                   2854:  *                1  --- does not exist (exhaustion)
                   2855:  */
                   2856:
                   2857: int _generate_good_irreducible_polynomial(UP2 f,int d)
                   2858: {
                   2859:        int ret,i,j,k,nz,i0,j0,k0;
                   2860:        int w;
                   2861:        unsigned int *fd;
                   2862:
                   2863:        /*
                   2864:         * if f = x^d+x^i+1 then i0 <- i, j0 <- 0, k0 <-0.
                   2865:         * if f = x^d+x^k+x^j+x^i+1 (k>j>i) then i0 <- i, j0 <- j, k0 <-k.
                   2866:         * otherwise i0,j0,k0 is set to 0.
                   2867:         */
                   2868:
                   2869:        fd = f->b;
                   2870:        w = (d>>5)+1;
                   2871:        if ( f->w && (d==degup2(f)) ) {
                   2872:                for ( nz = 0, i = d; i >= 0; i-- )
                   2873:                        if ( fd[i>>5]&(1<<(i&31)) ) nz++;
                   2874:                switch ( nz ) {
                   2875:                        case 3:
                   2876:                                for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
                   2877:                                /* reset i0-th bit */
                   2878:                                fd[i0>>5] &= ~(1<<(i0&31));
                   2879:                                j0 = k0 = 0;
                   2880:                                break;
                   2881:                        case 5:
                   2882:                                for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
                   2883:                                /* reset i0-th bit */
                   2884:                                fd[i0>>5] &= ~(1<<(i0&31));
                   2885:                                for ( j0 = i0+1; !(fd[j0>>5]&(1<<(j0&31))) ; j0++ );
                   2886:                                /* reset j0-th bit */
                   2887:                                fd[j0>>5] &= ~(1<<(j0&31));
                   2888:                                for ( k0 = j0+1; !(fd[k0>>5]&(1<<(k0&31))) ; k0++ );
                   2889:                                /* reset k0-th bit */
                   2890:                                fd[k0>>5] &= ~(1<<(k0&31));
                   2891:                                break;
                   2892:                        default:
                   2893:                                f->w = 0; break;
                   2894:                }
                   2895:        } else
                   2896:                f->w = 0;
                   2897:
                   2898:        if ( !f->w ) {
                   2899:                fd = f->b;
                   2900:                f->w = w; fd[0] |= 1; fd[d>>5] |= (1<<(d&31));
                   2901:                i0 = j0 = k0 = 0;
                   2902:        }
                   2903:        /* if j0 > 0 then f is already a pentanomial */
                   2904:        if ( j0 > 0 ) goto PENTA;
                   2905:
                   2906:        /* searching for an irreducible trinomial */
                   2907:
                   2908:        for ( i = 1; 2*i <= d; i++ ) {
                   2909:                /* skip the polynomials 'before' f */
                   2910:                if ( i < i0 ) continue;
                   2911:                if ( i == i0 ) { i0 = 0; continue; }
                   2912:                /* set i-th bit */
                   2913:                fd[i>>5] |= (1<<(i&31));
                   2914:                ret = irredcheck_dddup2(f);
                   2915:                if ( ret == 1 ) return 0;
                   2916:                /* reset i-th bit */
                   2917:                fd[i>>5] &= ~(1<<(i&31));
                   2918:        }
                   2919:
                   2920:        /* searching for an irreducible pentanomial */
                   2921: PENTA:
                   2922:        for ( i = 3; i < d; i++ ) {
                   2923:                /* skip the polynomials 'before' f */
                   2924:                if ( i < i0 ) continue;
                   2925:                if ( i == i0 ) i0 = 0;
                   2926:                /* set i-th bit */
                   2927:                fd[i>>5] |= (1<<(i&31));
                   2928:                for ( j = 2; j < i; j++ ) {
                   2929:                        /* skip the polynomials 'before' f */
                   2930:                        if ( j < j0 ) continue;
                   2931:                        if ( j == j0 ) j0 = 0;
                   2932:                        /* set j-th bit */
                   2933:                        fd[j>>5] |= (1<<(j&31));
                   2934:                        for ( k = 1; k < j; k++ ) {
                   2935:                                /* skip the polynomials 'before' f */
                   2936:                                if ( k < k0 ) continue;
                   2937:                                else if ( k == k0 ) { k0 = 0; continue; }
                   2938:                                /* set k-th bit */
                   2939:                                fd[k>>5] |= (1<<(k&31));
                   2940:                                ret = irredcheck_dddup2(f);
                   2941:                                if ( ret == 1 ) return 0;
                   2942:                                /* reset k-th bit */
                   2943:                                fd[k>>5] &= ~(1<<(k&31));
                   2944:                        }
                   2945:                        /* reset j-th bit */
                   2946:                        fd[j>>5] &= ~(1<<(j&31));
                   2947:                }
                   2948:                /* reset i-th bit */
                   2949:                fd[i>>5] &= ~(1<<(i&31));
                   2950:        }
                   2951:        /* exhausted */
                   2952:        return 1;
1.3       noro     2953: }
                   2954:
1.24      noro     2955: void printqmat(Q **mat,int row,int col)
1.3       noro     2956: {
                   2957:        int i,j;
                   2958:
                   2959:        for ( i = 0; i < row; i++ ) {
                   2960:                for ( j = 0; j < col; j++ ) {
1.8       noro     2961:                        printnum((Num)mat[i][j]); printf(" ");
1.3       noro     2962:                }
                   2963:                printf("\n");
                   2964:        }
                   2965: }
                   2966:
1.24      noro     2967: void printimat(int **mat,int row,int col)
1.3       noro     2968: {
                   2969:        int i,j;
                   2970:
                   2971:        for ( i = 0; i < row; i++ ) {
                   2972:                for ( j = 0; j < col; j++ ) {
                   2973:                        printf("%d ",mat[i][j]);
                   2974:                }
                   2975:                printf("\n");
                   2976:        }
1.36      noro     2977: }
                   2978:
                   2979: void Pnd_det(NODE arg,P *rp)
                   2980: {
1.37      noro     2981:        if ( argc(arg) == 1 )
                   2982:                nd_det(0,ARG0(arg),rp);
                   2983:        else
                   2984:                nd_det(QTOS((Q)ARG1(arg)),ARG0(arg),rp);
1.1       noro     2985: }

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