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

Annotation of OpenXM_contrib2/asir2000/builtin/poly.c, Revision 1.27

1.4       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.5       noro       26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.4       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.27    ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/builtin/poly.c,v 1.26 2017/02/28 07:06:28 noro Exp $
1.4       noro       49: */
1.1       noro       50: #include "ca.h"
                     51: #include "parse.h"
                     52: #include "base.h"
                     53:
                     54: void Pranp();
                     55:
1.20      noro       56: void Pheadsgn();
1.21      noro       57: void Pmul_trunc(),Pquo_trunc();
1.1       noro       58: void Pumul(),Pumul_ff(),Pusquare(),Pusquare_ff(),Putmul(),Putmul_ff();
                     59: void Pkmul(),Pksquare(),Pktmul();
1.22      noro       60: void Pord(), Premove_vars(), Pcoef0(), Pcoef(), Pdeg(), Pmindeg(), Psetmod();
1.1       noro       61: void Pcoef_gf2n();
                     62: void getcoef(), getdeglist(), mergedeglist(), change_mvar(), restore_mvar();
                     63:
1.2       noro       64: void Pp_mag(),Pmaxblen();
1.1       noro       65: void Pmergelist(), Pch_mv(), Pre_mv(), Pdeglist();
                     66: void Pptomp(),Pmptop();
                     67: void Pptolmp(),Plmptop();
1.12      noro       68: void Pptosfp(),Psfptop(),Psf_galois_action(),Psf_embed(),Psf_find_root();
1.18      noro       69: void Psf_minipoly(),Psf_log(),Psfptopsfp();
1.1       noro       70: void Pptogf2n(),Pgf2ntop(),Pgf2ntovect();
                     71: void Pptogfpn(),Pgfpntop();
                     72: void Pfind_root_gf2n();
1.2       noro       73: void Pumul_specialmod(),Pusquare_specialmod(),Putmul_specialmod();
1.1       noro       74:
                     75: void Pureverse(),Putrunc(),Pudecomp(),Purembymul(),Purembymul_precomp();
                     76: void Puinvmod(),Purevinvmod();
                     77: void Ppwrmod_ff(),Ppwrtab_ff(),Pgeneric_pwrmod_ff();
                     78: void Pkpwrmod_lm(),Pkpwrtab_lm(),Pkgeneric_pwrmod_lm();
                     79:
                     80: void Pkmulum();
                     81: void Pksquareum();
                     82:
                     83: void Pfmultest();
                     84: void Phfmul_lm();
                     85: void Plazy_lm();
                     86:
                     87: void Psetmod_ff();
                     88: void Psimp_ff();
                     89: void Pextdeg_ff();
                     90: void Pcharacteristic_ff();
                     91: void Pfield_type_ff();
                     92: void Pfield_order_ff();
                     93: void Prandom_ff();
                     94:
                     95: void Ptracemod_gf2n();
                     96: void Psparsemod_gf2n();
                     97: void Pmultest_gf2n();
                     98: void Psquaretest_gf2n();
                     99: void Pinvtest_gf2n();
                    100: void Pbininv_gf2n();
                    101: void Prinvtest_gf2n();
                    102: void Pis_irred_gf2();
                    103: void Pis_irred_ddd_gf2();
1.2       noro      104: void Pget_next_fft_prime();
1.3       noro      105: void Puadj_coef();
1.9       noro      106: void Preorder();
                    107: void Phomogeneous_part();
                    108: void Phomogeneous_deg();
1.1       noro      109: void simp_ff(Obj,Obj *);
                    110: void ranp(int,UP *);
                    111: void field_order_ff(N *);
                    112:
                    113: int current_ff;
                    114:
                    115: struct ftab poly_tab[] = {
1.20      noro      116:        {"headsgn",Pheadsgn,1},
1.21      noro      117:        {"quo_trunc",Pquo_trunc,2},
1.19      noro      118:        {"mul_trunc",Pmul_trunc,3},
1.9       noro      119:        {"homogeneous_deg",Phomogeneous_deg,-2},
                    120:        {"homogeneous_part",Phomogeneous_part,-3},
                    121:        {"reorder",Preorder,3},
1.3       noro      122:        {"uadj_coef",Puadj_coef,3},
1.1       noro      123:        {"ranp",Pranp,2},
                    124:        {"p_mag",Pp_mag,1},
1.2       noro      125:        {"maxblen",Pmaxblen,1},
1.1       noro      126:        {"ord",Pord,-1},
1.22      noro      127:        {"remove_vars",Premove_vars,1},
1.25      noro      128:        {"delete_vars",Premove_vars,1},
1.1       noro      129:        {"coef0",Pcoef0,-3},
                    130:        {"coef",Pcoef,-3},
                    131:        {"coef_gf2n",Pcoef_gf2n,2},
                    132:        {"deg",Pdeg,2},
                    133:        {"mindeg",Pmindeg,2},
                    134:        {"setmod",Psetmod,-1},
                    135:
                    136:        {"sparsemod_gf2n",Psparsemod_gf2n,-1},
                    137:
1.15      noro      138:        {"setmod_ff",Psetmod_ff,-3},
1.1       noro      139:        {"simp_ff",Psimp_ff,1},
                    140:        {"extdeg_ff",Pextdeg_ff,0},
                    141:        {"characteristic_ff",Pcharacteristic_ff,0},
                    142:        {"field_type_ff",Pfield_type_ff,0},
                    143:        {"field_order_ff",Pfield_order_ff,0},
                    144:        {"random_ff",Prandom_ff,0},
                    145:
                    146:        {"deglist",Pdeglist,2},
                    147:        {"mergelist",Pmergelist,2},
                    148:        {"ch_mv",Pch_mv,2},
                    149:        {"re_mv",Pre_mv,2},
                    150:
1.27    ! noro      151:        {"ptomp",Pptomp,-2},
1.1       noro      152:        {"mptop",Pmptop,1},
                    153:
                    154:        {"ptolmp",Pptolmp,1},
                    155:        {"lmptop",Plmptop,1},
                    156:
1.11      noro      157:        {"sf_galois_action",Psf_galois_action,2},
1.12      noro      158:        {"sf_find_root",Psf_find_root,1},
                    159:        {"sf_minipoly",Psf_minipoly,2},
                    160:        {"sf_embed",Psf_embed,3},
1.13      noro      161:        {"sf_log",Psf_log,1},
                    162:
1.11      noro      163:        {"ptosfp",Pptosfp,1},
1.6       noro      164:        {"sfptop",Psfptop,1},
1.18      noro      165:        {"sfptopsfp",Psfptopsfp,2},
1.1       noro      166:        {"ptogf2n",Pptogf2n,1},
                    167:        {"gf2ntop",Pgf2ntop,-2},
                    168:        {"gf2ntovect",Pgf2ntovect,1},
                    169:
                    170:        {"ptogfpn",Pptogfpn,1},
                    171:        {"gfpntop",Pgfpntop,-2},
                    172:
                    173:        {"kmul",Pkmul,2},
                    174:        {"ksquare",Pksquare,1},
                    175:        {"ktmul",Pktmul,3},
                    176:
                    177:        {"umul",Pumul,2},
                    178:        {"usquare",Pusquare,1},
                    179:        {"ureverse_inv_as_power_series",Purevinvmod,2},
                    180:        {"uinv_as_power_series",Puinvmod,2},
                    181:
1.2       noro      182:        {"umul_specialmod",Pumul_specialmod,3},
                    183:        {"usquare_specialmod",Pusquare_specialmod,2},
                    184:        {"utmul_specialmod",Putmul_specialmod,4},
                    185:
1.1       noro      186:        {"utmul",Putmul,3},
                    187:        {"umul_ff",Pumul_ff,2},
                    188:        {"usquare_ff",Pusquare_ff,1},
                    189:        {"utmul_ff",Putmul_ff,3},
                    190:
                    191:        /* for historical reason */
                    192:        {"trunc",Putrunc,2},
                    193:        {"decomp",Pudecomp,2},
                    194:
                    195:        {"utrunc",Putrunc,2},
                    196:        {"udecomp",Pudecomp,2},
1.3       noro      197:        {"ureverse",Pureverse,-2},
1.1       noro      198:        {"urembymul",Purembymul,2},
                    199:        {"urembymul_precomp",Purembymul_precomp,3},
                    200:
                    201:        {"lazy_lm",Plazy_lm,1},
                    202:        {"lazy_ff",Plazy_lm,1},
                    203:
                    204:        {"pwrmod_ff",Ppwrmod_ff,1},
                    205:        {"generic_pwrmod_ff",Pgeneric_pwrmod_ff,3},
                    206:        {"pwrtab_ff",Ppwrtab_ff,2},
                    207:
                    208:        {"tracemod_gf2n",Ptracemod_gf2n,3},
                    209:        {"b_find_root_gf2n",Pfind_root_gf2n,1},
                    210:
                    211:        {"is_irred_gf2",Pis_irred_gf2,1},
                    212:        {"is_irred_ddd_gf2",Pis_irred_ddd_gf2,1},
                    213:
                    214:        {"kpwrmod_lm",Pkpwrmod_lm,1},
                    215:        {"kgeneric_pwrmod_lm",Pkgeneric_pwrmod_lm,3},
                    216:        {"kpwrtab_lm",Pkpwrtab_lm,2},
                    217:
                    218:        {"kmulum",Pkmulum,3},
                    219:        {"ksquareum",Pksquareum,2},
                    220:
                    221:        {"fmultest",Pfmultest,3},
                    222:        {"hfmul_lm",Phfmul_lm,2},
                    223:
                    224:        {"multest_gf2n",Pmultest_gf2n,2},
                    225:        {"squaretest_gf2n",Psquaretest_gf2n,1},
                    226:        {"bininv_gf2n",Pbininv_gf2n,2},
                    227:        {"invtest_gf2n",Pinvtest_gf2n,1},
                    228:        {"rinvtest_gf2n",Prinvtest_gf2n,0},
1.2       noro      229:        {"get_next_fft_prime",Pget_next_fft_prime,2},
1.1       noro      230:        {0,0,0},
                    231: };
1.20      noro      232:
                    233: void Pheadsgn(NODE arg,Q *rp)
                    234: {
                    235:        int s;
                    236:
                    237:        s = headsgn((P)ARG0(arg));
                    238:        STOQ(s,*rp);
                    239: }
1.19      noro      240:
                    241: void Pmul_trunc(NODE arg,P *rp)
                    242: {
                    243:        P p1,p2,p,h;
                    244:        VL vl0,vl1,vl2,tvl,vl;
                    245:        VN vn;
                    246:        int i,n;
                    247:
                    248:        p1 = (P)ARG0(arg);
                    249:        p2 = (P)ARG1(arg);
                    250:        get_vars((Obj)p1,&vl1); get_vars((Obj)p2,&vl2); mergev(CO,vl1,vl2,&tvl);
                    251:        p = (P)ARG2(arg);
                    252:        get_vars((Obj)p,&vl0); mergev(CO,tvl,vl0,&vl);
                    253:        for ( tvl = vl, n = 0; tvl; tvl = NEXT(tvl), n++ );
                    254:        vn = (VN) ALLOCA((n+1)*sizeof(struct oVN));
                    255:        for ( i = 0, tvl = vl; i < n; tvl = NEXT(tvl), i++ ) {
                    256:                vn[i].v = tvl->v;
                    257:                vn[i].n = 0;
                    258:        }
                    259:        vn[i].v = 0;
                    260:        vn[i].n = 0;
                    261:        for ( h = p, i = 0; OID(h) == O_P; h = COEF(DC(h)) ) {
                    262:                for ( ; vn[i].v != VR(h); i++ );
                    263:                vn[i].n = QTOS(DEG(DC(h)));
                    264:        }
                    265:        mulp_trunc(vl,p1,p2,vn,rp);
1.21      noro      266: }
                    267:
                    268: void Pquo_trunc(NODE arg,P *rp)
                    269: {
                    270:        P p1,p2,p,h;
                    271:        VL vl0,vl1,vl2,tvl,vl;
                    272:        VN vn;
                    273:        int i,n;
                    274:
                    275:        p1 = (P)ARG0(arg);
                    276:        p2 = (P)ARG1(arg);
                    277:        if ( !p1 )
                    278:                *rp = 0;
                    279:        else if ( NUM(p2) )
                    280:                divsp(CO,p1,p2,rp);
                    281:        else {
                    282:                get_vars((Obj)p1,&vl1); get_vars((Obj)p2,&vl2); mergev(CO,vl1,vl2,&vl);
                    283:                for ( tvl = vl, n = 0; tvl; tvl = NEXT(tvl), n++ );
                    284:                vn = (VN) ALLOCA((n+1)*sizeof(struct oVN));
                    285:                for ( i = 0, tvl = vl; i < n; tvl = NEXT(tvl), i++ ) {
                    286:                        vn[i].v = tvl->v;
                    287:                        vn[i].n = 0;
                    288:                }
                    289:                vn[i].v = 0;
                    290:                vn[i].n = 0;
                    291:                for ( h = p2, i = 0; OID(h) == O_P; h = COEF(DC(h)) ) {
                    292:                        for ( ; vn[i].v != VR(h); i++ );
                    293:                        vn[i].n = QTOS(DEG(DC(h)));
                    294:                }
                    295:                quop_trunc(vl,p1,p2,vn,rp);
                    296:        }
1.19      noro      297: }
1.1       noro      298:
1.16      noro      299: void Phomogeneous_part(NODE arg,P *rp)
1.9       noro      300: {
                    301:        if ( argc(arg) == 2 )
                    302:                exthp(CO,(P)ARG0(arg),QTOS((Q)ARG1(arg)),rp);
                    303:        else
                    304:                exthpc_generic(CO,(P)ARG0(arg),QTOS((Q)ARG2(arg)),
                    305:                        VR((P)ARG1(arg)),rp);
                    306: }
                    307:
1.16      noro      308: void Phomogeneous_deg(NODE arg,Q *rp)
1.9       noro      309: {
                    310:        int d;
                    311:
                    312:        if ( argc(arg) == 1 )
                    313:                d = homdeg((P)ARG0(arg));
                    314:        else
                    315:                d = getchomdeg(VR((P)ARG1(arg)),(P)ARG0(arg));
                    316:        STOQ(d,*rp);
                    317: }
                    318:
                    319: /*
                    320:        p1 = reorder(p,ovl,nvl) => p1 is 'sorted accoding to nvl.
                    321: */
                    322:
1.16      noro      323: void Preorder(NODE arg,P *rp)
1.9       noro      324: {
                    325:        VL ovl,nvl,tvl;
                    326:        NODE n;
                    327:
                    328:        for ( ovl = 0, n = BDY((LIST)ARG1(arg)); n; n = NEXT(n) ) {
                    329:                if ( !ovl ) {
                    330:                        NEWVL(ovl); tvl = ovl;
                    331:                } else {
                    332:                        NEWVL(NEXT(tvl)); tvl = NEXT(tvl);
                    333:                }
                    334:                        VR(tvl) = VR((P)BDY(n));
                    335:        }
                    336:        for ( nvl = 0, n = BDY((LIST)ARG2(arg)); n; n = NEXT(n) ) {
                    337:                if ( !nvl ) {
                    338:                        NEWVL(nvl); tvl = nvl;
                    339:                } else {
                    340:                        NEWVL(NEXT(tvl)); tvl = NEXT(tvl);
                    341:                }
                    342:                        VR(tvl) = VR((P)BDY(n));
                    343:        }
                    344:        reorderp(nvl,ovl,(P)ARG0(arg),rp);
                    345: }
1.3       noro      346:
                    347: /*
                    348:        uadj_coef(F,M,M2)
                    349:        if ( F is a non-negative integer )
                    350:                return F > M2 ? F-M : M2;
                    351:        else
                    352:                F = CN*V^N+...+C0
                    353:                return uadj_coef(CN,M,M2)*V^N+...+uadj_coef(C0,M,M2);
                    354: */
                    355:
1.16      noro      356: void Puadj_coef(NODE arg,P *rp)
1.3       noro      357: {
                    358:        UP f,r;
                    359:        N m,m2;
                    360:
                    361:        ptoup((P)ARG0(arg),&f);
                    362:        m = NM((Q)ARG1(arg));
                    363:        m2 = NM((Q)ARG2(arg));
                    364:        adj_coefup(f,m,m2,&r);
                    365:        uptop(r,rp);
                    366: }
                    367:
1.2       noro      368: /*
                    369:        get_next_fft_prime(StartIndex,Bits)
                    370:        tries to find smallest Index >= StartIndex s.t.
                    371:                2^(Bits-1)|FFTprime[Index]-1
                    372:        return [Index,Mod] or 0 (not exist)
                    373: */
                    374:
1.16      noro      375: void Pget_next_fft_prime(NODE arg,LIST *rp)
1.2       noro      376: {
                    377:        unsigned int mod,d;
                    378:        int start,bits,i;
                    379:        NODE n;
                    380:        Q q,ind;
                    381:
                    382:        start = QTOS((Q)ARG0(arg));
                    383:        bits = QTOS((Q)ARG1(arg));
                    384:        for ( i = start; ; i++ ) {
                    385:                get_fft_prime(i,&mod,&d);
                    386:                if ( !mod ) {
                    387:                        *rp = 0; return;
                    388:                }
1.16      noro      389:                if ( bits <= (int)d ) {
1.2       noro      390:                        UTOQ(mod,q);
                    391:                        UTOQ(i,ind);
                    392:                        n = mknode(2,ind,q);
                    393:                        MKLIST(*rp,n);
                    394:                        return;
                    395:                }
                    396:        }
                    397: }
1.1       noro      398:
1.16      noro      399: void Pranp(NODE arg,P *rp)
1.1       noro      400: {
                    401:        int n;
                    402:        UP c;
                    403:
                    404:        n = QTOS((Q)ARG0(arg));
                    405:        ranp(n,&c);
                    406:        if ( c ) {
                    407:                up_var = VR((P)ARG1(arg));
                    408:                uptop(c,rp);
                    409:        } else
                    410:                *rp = 0;
                    411: }
                    412:
1.16      noro      413: void ranp(int n,UP *nr)
1.1       noro      414: {
                    415:        int i;
                    416:        unsigned int r;
                    417:        Q q;
                    418:        UP c;
                    419:
                    420:        *nr = c = UPALLOC(n);
                    421:        for ( i = 0; i <= n; i++ ) {
                    422:                r = random();
                    423:                UTOQ(r,q);
                    424:                c->c[i] = (Num)q;
                    425:        }
                    426:        for ( i = n; i >= 0 && !c->c[i]; i-- );
                    427:        if ( i >= 0 )
                    428:                c->d = i;
                    429:        else
                    430:                *nr = 0;
                    431: }
                    432:
1.16      noro      433: void Pmaxblen(NODE arg,Q *rp)
1.2       noro      434: {
                    435:        int l;
                    436:        l = maxblenp(ARG0(arg));
                    437:        STOQ(l,*rp);
                    438: }
                    439:
1.16      noro      440: void Pp_mag(NODE arg,Q *rp)
1.1       noro      441: {
                    442:        int l;
                    443:        l = p_mag(ARG0(arg));
                    444:        STOQ(l,*rp);
                    445: }
                    446:
1.16      noro      447: void Pord(NODE arg,LIST *listp)
1.1       noro      448: {
1.23      noro      449:        NODE n,tn,p,opt;
                    450:        char *key;
                    451:        Obj value;
                    452:        int overwrite=0;
1.1       noro      453:        LIST l;
                    454:        VL vl,tvl,svl;
                    455:        P t;
                    456:        int i,j;
                    457:        V *va;
                    458:        V v;
                    459:
1.23      noro      460:        if ( current_option ) {
                    461:                for ( opt = current_option; opt; opt = NEXT(opt) ) {
                    462:                        p = BDY((LIST)BDY(opt));
                    463:                        key = BDY((STRING)BDY(p));
                    464:                        value = (Obj)BDY(NEXT(p));
                    465:                        if ( !strcmp(key,"overwrite") && value ) {
                    466:                                overwrite = value ? 1 : 0;
                    467:                                break;
                    468:                        }
                    469:                }
                    470:        }
                    471:
1.1       noro      472:        if ( argc(arg) ) {
                    473:                asir_assert(ARG0(arg),O_LIST,"ord");
                    474:                for ( vl = 0, i = 0, n = BDY((LIST)ARG0(arg));
                    475:                        n; n = NEXT(n), i++ ) {
                    476:                        if ( !vl ) {
                    477:                                NEWVL(vl); tvl = vl;
                    478:                        } else {
                    479:                                NEWVL(NEXT(tvl)); tvl = NEXT(tvl);
                    480:                        }
                    481:                        if ( !(t = (P)BDY(n)) || (OID(t) != O_P) )
                    482:                                error("ord : invalid argument");
                    483:                        VR(tvl) = VR(t);
                    484:                }
1.23      noro      485:                if ( !overwrite ) {
                    486:                        va = (V *)ALLOCA(i*sizeof(V));
                    487:                        for ( j = 0, svl = vl; j < i; j++, svl = NEXT(svl) )
                    488:                                va[j] = VR(svl);
                    489:                        for ( svl = CO; svl; svl = NEXT(svl) ) {
                    490:                                v = VR(svl);
                    491:                                for ( j = 0; j < i; j++ )
                    492:                                        if ( v == va[j] )
                    493:                                                break;
                    494:                                if ( j == i ) {
                    495:                                        if ( !vl ) {
                    496:                                                NEWVL(vl); tvl = vl;
                    497:                                        } else {
                    498:                                                NEWVL(NEXT(tvl)); tvl = NEXT(tvl);
                    499:                                        }
                    500:                                        VR(tvl) = v;
1.1       noro      501:                                }
                    502:                        }
1.24      noro      503:                } else {
                    504:                        for ( svl = vl; svl; svl = NEXT(svl) ) {
                    505:                                if ( svl->v->attr == (pointer)V_PF )
                    506:                                        ((PFINS)svl->v->priv)->pf->ins = 0;
                    507:                        }
1.1       noro      508:                }
                    509:                if ( vl )
                    510:                        NEXT(tvl) = 0;
                    511:                CO = vl;
                    512:        }
                    513:        for ( n = 0, vl = CO; vl; vl = NEXT(vl) ) {
                    514:                NEXTNODE(n,tn); MKV(VR(vl),t); BDY(tn) = (pointer)t;
                    515:        }
                    516:        NEXT(tn) = 0; MKLIST(l,n); *listp = l;
                    517: }
                    518:
1.22      noro      519: void Premove_vars(NODE arg,LIST *listp)
                    520: {
                    521:        NODE l,nd,tnd;
                    522:        V *v,*va;
                    523:        int n,na,i,j;
                    524:        VL vl,vl1;
                    525:        P t;
                    526:        LIST list;
                    527:
                    528:        asir_assert(ARG0(arg),O_LIST,"remove_vars");
                    529:        l = BDY((LIST)ARG0(arg)); n = length(l);
                    530:        v = (V *)ALLOCA(n*sizeof(V));
                    531:        for ( i = 0; i < n; i++, l = NEXT(l) )
                    532:                if ( !(t = (P)BDY(l)) || (OID(t) != O_P) )
                    533:                        error("ord : invalid argument");
                    534:                else v[i] = VR(t);
                    535:
                    536:        for ( na = 0, vl = CO; vl; vl = NEXT(vl), na++ );
                    537:        va = (V *)ALLOCA(na*sizeof(V));
                    538:        for ( i = 0, vl = CO; i < na; i++, vl = NEXT(vl) ) va[i] = VR(vl);
                    539:        for ( i = 0; i < na; i++ )
                    540:                for ( j = 0; j < n; j++ ) if ( va[i] == v[j] ) va[i] = 0;
                    541:        for ( vl = 0, i = na-1; i >= 0; i-- )
                    542:                if ( va[i] ) {
                    543:                        NEWVL(vl1); VR(vl1) = va[i]; NEXT(vl1) = vl; vl = vl1;
                    544:                }
                    545:        CO = vl;
                    546:        for ( nd = 0, vl = CO; vl; vl = NEXT(vl) ) {
                    547:                NEXTNODE(nd,tnd); MKV(VR(vl),t); BDY(tnd) = (pointer)t;
                    548:        }
                    549:        if ( nd ) NEXT(tnd) = 0;
                    550:        MKLIST(list,nd); *listp = list;
                    551: }
                    552:
1.16      noro      553: void Pcoef0(NODE arg,Obj *rp)
1.1       noro      554: {
                    555:        Obj t,n;
                    556:        P s;
                    557:        DCP dc;
                    558:        int id;
                    559:        V v;
                    560:        VL vl;
                    561:
                    562:        if ( !(t = (Obj)ARG0(arg)) || ((id = OID(ARG0(arg))) > O_P) )
                    563:                *rp = 0;
                    564:        else if ( (n = (Obj)ARG1(arg)) && (OID(n) > O_N) )
                    565:                *rp = 0;
                    566:        else if ( id == O_N )
                    567:                if ( !n )
                    568:                        *rp = t;
                    569:                else
                    570:                        *rp = 0;
                    571:        else {
                    572:                if ( argc(arg) == 3 ) {
                    573:                        if ( (v = VR((P)ARG2(arg))) != VR((P)t) ) {
                    574:                                reordvar(CO,v,&vl); reorderp(vl,CO,(P)t,&s);
                    575:                        } else
                    576:                                s = (P)t;
                    577:                        if ( VR(s) != v ) {
                    578:                                if ( n )
                    579:                                        *rp = 0;
                    580:                                else
                    581:                                        *rp = t;
                    582:                                return;
                    583:                        }
                    584:                } else
                    585:                        s = (P)t;
                    586:                for ( dc = DC(s); dc && cmpq(DEG(dc),(Q)n); dc = NEXT(dc) );
                    587:                if ( dc )
                    588:                        *rp = (Obj)COEF(dc);
                    589:                else
                    590:                        *rp = 0;
                    591:        }
                    592: }
                    593:
1.16      noro      594: void Pcoef(NODE arg,Obj *rp)
1.1       noro      595: {
                    596:        Obj t,n;
                    597:        P s;
                    598:        DCP dc;
                    599:        int id;
                    600:        V v;
                    601:
                    602:        if ( !(t = (Obj)ARG0(arg)) || ((id = OID(ARG0(arg))) > O_P) )
                    603:                *rp = 0;
                    604:        else if ( (n = (Obj)ARG1(arg)) && (OID(n) > O_N) )
                    605:                *rp = 0;
                    606:        else if ( id == O_N ) {
                    607:                if ( !n )
                    608:                        *rp = t;
                    609:                else
                    610:                        *rp = 0;
                    611:        } else {
                    612:                if ( argc(arg) == 3 ) {
                    613:                        if ( (v = VR((P)ARG2(arg))) != VR((P)t) ) {
                    614:                                getcoef(CO,(P)t,v,(Q)n,(P *)rp); return;
                    615:                        } else
                    616:                                s = (P)t;
                    617:                        if ( VR(s) != v ) {
                    618:                                if ( n )
                    619:                                        *rp = 0;
                    620:                                else
                    621:                                        *rp = t;
                    622:                                return;
                    623:                        }
                    624:                } else
                    625:                        s = (P)t;
                    626:                for ( dc = DC(s); dc && cmpq(DEG(dc),(Q)n); dc = NEXT(dc) );
                    627:                if ( dc )
                    628:                        *rp = (Obj)COEF(dc);
                    629:                else
                    630:                        *rp = 0;
                    631:        }
                    632: }
                    633:
1.16      noro      634: void Pcoef_gf2n(NODE arg,Obj *rp)
1.1       noro      635: {
                    636:        Obj t,n;
                    637:        int id,d;
                    638:        UP2 up2;
                    639:
                    640:        if ( !(t = (Obj)ARG0(arg)) || ((id = OID(ARG0(arg))) > O_P) )
                    641:                *rp = 0;
                    642:        else if ( (n = (Obj)ARG1(arg)) && (OID(n) > O_N) )
                    643:                *rp = 0;
                    644:        else if ( id == O_N && NID((Num)t) == N_GF2N ) {
                    645:                d = QTOS((Q)n);
                    646:                up2 = ((GF2N)t)->body;
                    647:                if ( d > degup2(up2) )
                    648:                        *rp = 0;
                    649:                else
                    650:                        *rp = (Obj)(up2->b[d/BSH]&(((unsigned long)1)<<(d%BSH))?ONE:0);
                    651:        } else
                    652:                *rp = 0;
                    653: }
                    654:
1.16      noro      655: void Pdeg(NODE arg,Q *rp)
1.1       noro      656: {
                    657:        Obj t,v;
                    658:        int d;
                    659:
                    660: #if 0
                    661:        if ( !(t = (Obj)ARG0(arg)) || (OID(t) != O_P) )
                    662:                *rp = 0;
                    663:        else if ( !(v = (Obj)ARG1(arg)) || (VR((P)v) != VR((P)t)) )
                    664:                *rp = 0;
                    665:        else
                    666:                *rp = (Obj)DEG(DC((P)t));
                    667: #endif
                    668:        if ( !(t = (Obj)ARG0(arg)) )
                    669:                STOQ(-1,*rp);
                    670:        else if ( OID(t) != O_P ) {
                    671:                if ( OID(t) == O_N && NID(t) == N_GF2N
                    672:                        && (v=(Obj)ARG1(arg)) && OID(v)== O_N && NID(v) == N_GF2N ) {
                    673:                        d = degup2(((GF2N)t)->body);
                    674:                        STOQ(d,*rp);
                    675:                } else
                    676:                        *rp = 0;
                    677:        } else
                    678:                degp(VR((P)ARG1(arg)),(P)ARG0(arg),rp);
                    679: }
                    680:
1.16      noro      681: void Pmindeg(NODE arg,Q *rp)
1.1       noro      682: {
                    683:        Obj t;
                    684:
                    685:        if ( !(t = (Obj)ARG0(arg)) || (OID(t) != O_P) )
                    686:                *rp = 0;
                    687:        else
                    688:                getmindeg(VR((P)ARG1(arg)),(P)ARG0(arg),rp);
                    689: }
                    690:
1.16      noro      691: void Psetmod(NODE arg,Q *rp)
1.1       noro      692: {
                    693:        if ( arg ) {
                    694:                asir_assert(ARG0(arg),O_N,"setmod");
                    695:                current_mod = QTOS((Q)ARG0(arg));
                    696:        }
                    697:        STOQ(current_mod,*rp);
                    698: }
                    699:
1.16      noro      700: void Psparsemod_gf2n(NODE arg,Q *rp)
1.1       noro      701: {
                    702:        int id;
                    703:
                    704:        if ( arg && current_mod_gf2n )
                    705:                current_mod_gf2n->id = ARG0(arg)?1:0;
                    706:        if ( !current_mod_gf2n )
                    707:                id = -1;
                    708:        else
                    709:                id = current_mod_gf2n->id;
                    710:        STOQ(id,*rp);
                    711: }
                    712:
1.16      noro      713: void Pmultest_gf2n(NODE arg,GF2N *rp)
1.1       noro      714: {
                    715:        GF2N a,b,c;
                    716:        int i;
                    717:
                    718:        a = (GF2N)ARG0(arg); b = (GF2N)ARG0(arg);
                    719:        for ( i = 0; i < 10000; i++ )
                    720:                mulgf2n(a,b,&c);
                    721:        *rp = c;
                    722: }
                    723:
1.16      noro      724: void Psquaretest_gf2n(NODE arg,GF2N *rp)
1.1       noro      725: {
                    726:        GF2N a,c;
                    727:        int i;
                    728:
                    729:        a = (GF2N)ARG0(arg);
                    730:        for ( i = 0; i < 10000; i++ )
                    731:                squaregf2n(a,&c);
                    732:        *rp = c;
                    733: }
                    734:
1.16      noro      735: void Pinvtest_gf2n(NODE arg,GF2N *rp)
1.1       noro      736: {
                    737:        GF2N a,c;
                    738:        int i;
                    739:
                    740:        a = (GF2N)ARG0(arg);
                    741:        for ( i = 0; i < 10000; i++ )
                    742:                invgf2n(a,&c);
                    743:        *rp = c;
                    744: }
                    745:
1.16      noro      746: void Pbininv_gf2n(NODE arg,GF2N *rp)
1.1       noro      747: {
                    748:        UP2 a,inv;
                    749:        int n;
                    750:
                    751:        a = ((GF2N)ARG0(arg))->body;
                    752:        n = QTOS((Q)ARG1(arg));
                    753:        type1_bin_invup2(a,n,&inv);
                    754:        MKGF2N(inv,*rp);
                    755: }
                    756:
1.16      noro      757: void Prinvtest_gf2n(Real *rp)
1.1       noro      758: {
                    759:        GF2N *a;
                    760:        GF2N c;
                    761:        double t0,t1,r;
                    762:        int i;
                    763:        double get_clock();
                    764:
                    765:        a = (GF2N *)ALLOCA(1000*sizeof(GF2N));
                    766:        for ( i = 0; i < 1000; i++ ) {
                    767:                randomgf2n(&a[i]);
                    768:        }
                    769:        t0 = get_clock();
                    770:        for ( i = 0; i < 1000; i++ )
                    771:                invgf2n(a[i],&c);
                    772:        t1 = get_clock();
                    773:        r = (t1-t0)/1000;
                    774:        MKReal(r,*rp);
                    775: }
                    776:
1.16      noro      777: void Pfind_root_gf2n(NODE arg,GF2N *rp)
1.1       noro      778: {
                    779:
                    780: #if 0
                    781:        UP p;
                    782:
                    783:        ptoup((P)ARG0(arg),&p);
                    784:        find_root_gf2n(p,rp);
                    785: #else
                    786:        UP2 p;
                    787:
                    788:        ptoup2((P)ARG0(arg),&p);
                    789:        find_root_up2(p,rp);
                    790: #endif
                    791: }
                    792:
1.16      noro      793: void Pis_irred_gf2(NODE arg,Q *rp)
1.1       noro      794: {
                    795:        UP2 t;
                    796:
                    797:        ptoup2(ARG0(arg),&t);
                    798:        *rp = irredcheckup2(t) ? ONE : 0;
                    799: }
                    800:
1.16      noro      801: void Pis_irred_ddd_gf2(NODE arg,Q *rp)
1.1       noro      802: {
                    803:        UP2 t;
                    804:        int ret;
                    805:
                    806:        ptoup2(ARG0(arg),&t);
                    807:        ret = irredcheck_dddup2(t);
                    808:        STOQ(ret,*rp);
                    809: }
                    810:
1.16      noro      811: void Psetmod_ff(NODE arg,Obj *rp)
1.1       noro      812: {
                    813:        int ac;
1.7       noro      814:        int d;
1.1       noro      815:        Obj mod,defpoly;
                    816:        N n;
                    817:        UP up;
                    818:        UP2 up2;
1.14      noro      819:        UM dp;
1.6       noro      820:        Q q,r;
1.15      noro      821:        P p,p1,y;
1.1       noro      822:        NODE n0,n1;
                    823:        LIST list;
                    824:
                    825:        ac = argc(arg);
                    826:        if ( ac == 1 ) {
                    827:                mod = (Obj)ARG0(arg);
                    828:                if ( !mod )
1.26      noro      829:             current_ff = FF_NOT_SET;
                    830:         else {
                    831:                  switch ( OID(mod) ) {
1.1       noro      832:                        case O_N:
1.7       noro      833:                                current_ff = FF_GFP;
                    834:                                setmod_lm(NM((Q)mod));
1.6       noro      835:                                break;
1.1       noro      836:                        case O_P:
                    837:                                current_ff = FF_GF2N;
                    838:                                setmod_gf2n((P)mod); break;
                    839:                        default:
                    840:                                error("setmod_ff : invalid argument");
1.26      noro      841:                  }
                    842:       }
1.1       noro      843:        } else if ( ac == 2 ) {
1.7       noro      844:                if ( OID(ARG0(arg)) == O_N ) {
                    845:                        /* small finite field; primitive root representation */
                    846:                        current_ff = FF_GFS;
                    847:                        setmod_sf(QTOS((Q)ARG0(arg)),QTOS((Q)ARG1(arg)));
1.6       noro      848:                } else {
1.7       noro      849:                        mod = (Obj)ARG1(arg);
1.6       noro      850:                        current_ff = FF_GFPN;
                    851:                        defpoly = (Obj)ARG0(arg);
                    852:                        if ( !mod || !defpoly )
                    853:                                error("setmod_ff : invalid argument");
                    854:                        setmod_lm(NM((Q)mod));
                    855:                        setmod_gfpn((P)defpoly);
                    856:                }
1.14      noro      857:        } else if ( ac == 3 ) {
                    858:                /* finite extension of a small finite field */
                    859:                current_ff = FF_GFS;
                    860:                setmod_sf(QTOS((Q)ARG0(arg)),QTOS((Q)ARG1(arg)));
                    861:                d = QTOS((Q)ARG2(arg));
                    862:                generate_defpoly_sfum(d,&dp);
1.15      noro      863:                setmod_gfsn(dp);
                    864:                current_ff = FF_GFSN;
1.1       noro      865:        }
                    866:        switch ( current_ff ) {
                    867:                case FF_GFP:
                    868:                        getmod_lm(&n); NTOQ(n,1,q); *rp = (Obj)q; break;
                    869:                case FF_GF2N:
                    870:                        getmod_gf2n(&up2); up2top(up2,&p); *rp = (Obj)p; break;
                    871:                case FF_GFPN:
                    872:                        getmod_lm(&n); NTOQ(n,1,q);
                    873:                        getmod_gfpn(&up); uptop(up,&p);
                    874:                        MKNODE(n1,q,0); MKNODE(n0,p,n1);
                    875:                        MKLIST(list,n0);
                    876:                        *rp = (Obj)list; break;
1.6       noro      877:                case FF_GFS:
1.15      noro      878:                case FF_GFSN:
1.7       noro      879:                        STOQ(current_gfs_p,q);
1.15      noro      880:                        if ( current_gfs_ext )
1.7       noro      881:                                enc_to_p(current_gfs_p,current_gfs_iton[1],
                    882:                                        VR(current_gfs_ext),&p);
1.15      noro      883:                        else {
1.11      noro      884:                                if ( current_gfs_p == 2 )
                    885:                                        r = ONE;
1.17      noro      886:                                else if ( !current_gfs_ntoi )
                    887:                                        r = 0;
1.11      noro      888:                                else
                    889:                                        STOQ(current_gfs_iton[1],r);
1.15      noro      890:                                p = (P)r;
                    891:                        }
                    892:                        switch ( current_ff ) {
                    893:                                case FF_GFS:
                    894:                                        n0 = mknode(3,q,current_gfs_ext,p);
                    895:                                        break;
                    896:                                case FF_GFSN:
                    897:                                        getmod_gfsn(&dp);
                    898:                                        makevar("y",&y);
                    899:                                        sfumtop(VR(y),dp,&p1);
                    900:                                        n0 = mknode(4,q,current_gfs_ext,p,p1);
                    901:                                        break;
1.7       noro      902:                        }
1.6       noro      903:                        MKLIST(list,n0);
                    904:                        *rp = (Obj)list; break;
1.1       noro      905:                default:
                    906:                        *rp = 0; break;
                    907:        }
                    908: }
                    909:
1.16      noro      910: void Pextdeg_ff(Q *rp)
1.1       noro      911: {
                    912:        int d;
                    913:        UP2 up2;
                    914:        UP up;
1.15      noro      915:        UM dp;
1.1       noro      916:
                    917:        switch ( current_ff ) {
                    918:                case FF_GFP:
                    919:                        *rp = ONE; break;
                    920:                case FF_GF2N:
                    921:                        getmod_gf2n(&up2); d = degup2(up2); STOQ(d,*rp); break;
                    922:                case FF_GFPN:
                    923:                        getmod_gfpn(&up); STOQ(up->d,*rp); break;
1.10      noro      924:                case FF_GFS:
                    925:                        if ( !current_gfs_ext )
                    926:                                *rp = ONE;
                    927:                        else
                    928:                                *rp = DEG(DC(current_gfs_ext));
                    929:                        break;
1.15      noro      930:                case FF_GFSN:
                    931:                        getmod_gfsn(&dp);
                    932:                        STOQ(DEG(dp),*rp);
                    933:                        break;
1.1       noro      934:                default:
                    935:                        error("extdeg_ff : current_ff is not set");
                    936:        }
                    937: }
                    938:
1.16      noro      939: void Pcharacteristic_ff(Q *rp)
1.1       noro      940: {
                    941:        N lm;
                    942:
                    943:        switch ( current_ff ) {
                    944:                case FF_GFP:
                    945:                case FF_GFPN:
                    946:                        getmod_lm(&lm); NTOQ(lm,1,*rp); break;
                    947:                case FF_GF2N:
                    948:                        STOQ(2,*rp); break;
1.10      noro      949:                case FF_GFS:
1.15      noro      950:                case FF_GFSN:
1.10      noro      951:                        STOQ(current_gfs_p,*rp); break;
1.1       noro      952:                default:
                    953:                        error("characteristic_ff : current_ff is not set");
                    954:        }
                    955: }
                    956:
1.16      noro      957: void Pfield_type_ff(Q *rp)
1.1       noro      958: {
                    959:        STOQ(current_ff,*rp);
                    960: }
                    961:
1.16      noro      962: void Pfield_order_ff(Q *rp)
1.1       noro      963: {
                    964:        N n;
                    965:
                    966:        field_order_ff(&n);
                    967:        NTOQ(n,1,*rp);
                    968: }
                    969:
1.16      noro      970: void Prandom_ff(Obj *rp)
1.1       noro      971: {
                    972:        LM l;
                    973:        GF2N g;
                    974:        GFPN p;
1.7       noro      975:        GFS s;
1.15      noro      976:        GFSN spn;
1.1       noro      977:
                    978:        switch ( current_ff ) {
                    979:                case FF_GFP:
                    980:                        random_lm(&l); *rp = (Obj)l; break;
                    981:                case FF_GF2N:
                    982:                        randomgf2n(&g); *rp = (Obj)g; break;
                    983:                case FF_GFPN:
                    984:                        randomgfpn(&p); *rp = (Obj)p; break;
1.7       noro      985:                case FF_GFS:
                    986:                        randomgfs(&s); *rp = (Obj)s; break;
1.15      noro      987:                case FF_GFSN:
                    988:                        randomgfsn(&spn); *rp = (Obj)spn; break;
1.1       noro      989:                default:
                    990:                        error("random_ff : current_ff is not set");
                    991:        }
                    992: }
                    993:
1.16      noro      994: void Psimp_ff(NODE arg,Obj *rp)
                    995: {
1.1       noro      996:        simp_ff((Obj)ARG0(arg),rp);
                    997: }
                    998:
1.16      noro      999: void getcoef(VL vl,P p,V v,Q d,P *r)
1.1       noro     1000: {
                   1001:        P s,t,u,a,b,x;
                   1002:        DCP dc;
                   1003:        V w;
                   1004:
                   1005:        if ( !p )
                   1006:                *r = 0;
                   1007:        else if ( NUM(p) )
                   1008:                *r = d ? 0 : p;
                   1009:        else if ( (w=VR(p)) == v ) {
                   1010:                for ( dc = DC(p); dc && cmpq(DEG(dc),d); dc = NEXT(dc) );
                   1011:                *r = dc ? COEF(dc) : 0;
                   1012:        } else {
                   1013:                MKV(w,x);
                   1014:                for ( dc = DC(p), s = 0; dc; dc = NEXT(dc) ) {
                   1015:                        getcoef(vl,COEF(dc),v,d,&t);
                   1016:                        if ( t ) {
                   1017:                                pwrp(vl,x,DEG(dc),&u); mulp(vl,t,u,&a);
                   1018:                                addp(vl,s,a,&b); s = b;
                   1019:                        }
                   1020:                }
                   1021:                *r = s;
                   1022:        }
                   1023: }
                   1024:
1.16      noro     1025: void Pdeglist(NODE arg,LIST *rp)
1.1       noro     1026: {
                   1027:        NODE d;
                   1028:
                   1029:        getdeglist((P)ARG0(arg),VR((P)ARG1(arg)),&d);
                   1030:        MKLIST(*rp,d);
                   1031: }
                   1032:
1.16      noro     1033: void Pch_mv(NODE arg,P *rp)
1.1       noro     1034: {
                   1035:        change_mvar(CO,(P)ARG0(arg),VR((P)ARG1(arg)),rp);
                   1036: }
                   1037:
1.16      noro     1038: void Pre_mv(NODE arg,P *rp)
1.1       noro     1039: {
                   1040:        restore_mvar(CO,(P)ARG0(arg),VR((P)ARG1(arg)),rp);
                   1041: }
                   1042:
1.16      noro     1043: void change_mvar(VL vl,P p,V v,P *r)
1.1       noro     1044: {
                   1045:        Q d;
                   1046:        DCP dc,dc0;
                   1047:        NODE dl;
                   1048:
                   1049:        if ( !p || NUM(p) || (VR(p) == v) )
                   1050:                *r = p;
                   1051:        else {
                   1052:                getdeglist(p,v,&dl);
                   1053:                for ( dc0 = 0; dl; dl = NEXT(dl) ) {
                   1054:                        NEXTDC(dc0,dc); DEG(dc) = d = (Q)BDY(dl);
                   1055:                        getcoef(vl,p,v,d,&COEF(dc));
                   1056:                }
                   1057:                NEXT(dc) = 0; MKP(v,dc0,*r);
                   1058:        }
                   1059: }
                   1060:
1.16      noro     1061: void restore_mvar(VL vl,P p,V v,P *r)
1.1       noro     1062: {
                   1063:        P s,u,a,b,x;
                   1064:        DCP dc;
                   1065:
                   1066:        if ( !p || NUM(p) || (VR(p) != v) )
                   1067:                *r = p;
                   1068:        else {
                   1069:                MKV(v,x);
                   1070:                for ( dc = DC(p), s = 0; dc; dc = NEXT(dc) ) {
                   1071:                        pwrp(vl,x,DEG(dc),&u); mulp(vl,COEF(dc),u,&a);
                   1072:                        addp(vl,s,a,&b); s = b;
                   1073:                }
                   1074:                *r = s;
                   1075:        }
                   1076: }
                   1077:
1.16      noro     1078: void getdeglist(P p,V v,NODE *d)
1.1       noro     1079: {
                   1080:        NODE n,n0,d0,d1,d2;
                   1081:        DCP dc;
                   1082:
                   1083:        if ( !p || NUM(p) ) {
                   1084:                MKNODE(n,0,0); *d = n;
                   1085:        } else if ( VR(p) == v ) {
                   1086:                for ( n0 = 0, dc = DC(p); dc; dc = NEXT(dc) ) {
                   1087:                        NEXTNODE(n0,n); BDY(n) = (pointer)DEG(dc);
                   1088:                }
                   1089:                NEXT(n) = 0; *d = n0;
                   1090:        } else {
                   1091:                for ( dc = DC(p), d0 = 0; dc; dc = NEXT(dc) ) {
                   1092:                        getdeglist(COEF(dc),v,&d1); mergedeglist(d0,d1,&d2); d0 = d2;
                   1093:                }
                   1094:                *d = d0;
                   1095:        }
                   1096: }
1.16      noro     1097:
                   1098: void Pmergelist(NODE arg,LIST *rp)
1.1       noro     1099: {
                   1100:     NODE n;
                   1101:
                   1102:        asir_assert(ARG0(arg),O_LIST,"mergelist");
                   1103:        asir_assert(ARG1(arg),O_LIST,"mergelist");
                   1104:        mergedeglist(BDY((LIST)ARG0(arg)),BDY((LIST)ARG1(arg)),&n);
                   1105:        MKLIST(*rp,n);
                   1106: }
                   1107:
1.16      noro     1108: void mergedeglist(NODE d0,NODE d1,NODE *dr)
1.1       noro     1109: {
                   1110:        NODE t0,t,dt;
                   1111:        Q d;
                   1112:        int c;
                   1113:
                   1114:        if ( !d0 )
                   1115:                *dr = d1;
                   1116:        else {
                   1117:                while ( d1 ) {
                   1118:                        dt = d1; d1 = NEXT(d1); d = (Q)BDY(dt);
                   1119:                        for ( t0 = 0, t = d0; t; t0 = t, t = NEXT(t) ) {
                   1120:                                c = cmpq(d,(Q)BDY(t));
                   1121:                                if ( !c )
                   1122:                                        break;
                   1123:                                else if ( c > 0 ) {
                   1124:                                        if ( !t0 ) {
                   1125:                                                NEXT(dt) = d0; d0 = dt;
                   1126:                                        } else {
                   1127:                                                NEXT(t0) = dt; NEXT(dt) = t;
                   1128:                                        }
                   1129:                                        break;
                   1130:                                }
                   1131:                        }
                   1132:                        if ( !t ) {
                   1133:                                NEXT(t0) = dt; *dr = d0; return;
                   1134:                        }
                   1135:                }
                   1136:                *dr = d0;
                   1137:        }
                   1138: }
                   1139:
1.16      noro     1140: void Pptomp(NODE arg,P *rp)
1.1       noro     1141: {
1.27    ! noro     1142:   int mod;
        !          1143:
        !          1144:   if ( argc(arg) == 1 ) {
        !          1145:     if ( !current_mod )
        !          1146:       error("ptomp : current_mod is not set");
        !          1147:     else
        !          1148:       mod = current_mod;
        !          1149:   } else
        !          1150:     mod = QTOS((Q)ARG1(arg));
        !          1151:        ptomp(mod,(P)ARG0(arg),rp);
1.1       noro     1152: }
                   1153:
1.16      noro     1154: void Pmptop(NODE arg,P *rp)
1.1       noro     1155: {
                   1156:        mptop((P)ARG0(arg),rp);
                   1157: }
                   1158:
1.16      noro     1159: void Pptolmp(NODE arg,P *rp)
1.1       noro     1160: {
                   1161:        ptolmp((P)ARG0(arg),rp);
                   1162: }
                   1163:
1.16      noro     1164: void Plmptop(NODE arg,P *rp)
1.1       noro     1165: {
                   1166:        lmptop((P)ARG0(arg),rp);
1.11      noro     1167: }
                   1168:
1.16      noro     1169: void Psf_galois_action(NODE arg,P *rp)
1.11      noro     1170: {
                   1171:        sf_galois_action(ARG0(arg),ARG1(arg),rp);
1.12      noro     1172: }
                   1173:
                   1174: /*
                   1175:   sf_embed(F,B,PM)
                   1176:   F : an element of GF(pn)
                   1177:   B : the image of the primitive root of GF(pn)
                   1178:   PM : order of GF(pm)
                   1179: */
                   1180:
1.16      noro     1181: void Psf_embed(NODE arg,P *rp)
1.12      noro     1182: {
                   1183:        int k,pm;
                   1184:
                   1185:        /* GF(pn)={0,1,a,a^2,...}->GF(pm)={0,1,b,b^2,...}; a->b^k */
                   1186:        k = CONT((GFS)ARG1(arg));
                   1187:        pm = QTOS((Q)ARG2(arg));
                   1188:        sf_embed((P)ARG0(arg),k,pm,rp);
1.13      noro     1189: }
                   1190:
1.16      noro     1191: void Psf_log(NODE arg,Q *rp)
1.13      noro     1192: {
                   1193:        int k;
                   1194:
                   1195:        if ( !ARG0(arg) )
                   1196:                error("sf_log : invalid armument");
                   1197:        k = CONT((GFS)ARG0(arg));
                   1198:        STOQ(k,*rp);
1.12      noro     1199: }
                   1200:
1.16      noro     1201: void Psf_find_root(NODE arg,GFS *rp)
1.12      noro     1202: {
                   1203:        P p;
                   1204:        Obj t;
                   1205:        int d;
                   1206:        UM u;
                   1207:        int *root;
                   1208:
                   1209:        p = (P)ARG0(arg);
                   1210:        simp_ff((Obj)p,&t); p = (P)t;
                   1211:        d = getdeg(VR(p),p);
                   1212:        u = W_UMALLOC(d);
                   1213:        ptosfum(p,u);
                   1214:        root = (int *)ALLOCA(d*sizeof(int));
                   1215:        find_rootsf(u,root);
                   1216:        MKGFS(IFTOF(root[0]),*rp);
                   1217: }
                   1218:
1.16      noro     1219: void Psf_minipoly(NODE arg,P *rp)
1.12      noro     1220: {
                   1221:        Obj t;
                   1222:        P p1,p2;
                   1223:        int d1,d2;
                   1224:        UM up1,up2,m;
                   1225:
                   1226:        p1 = (P)ARG0(arg); simp_ff((Obj)p1,&t); p1 = (P)t;
                   1227:        p2 = (P)ARG1(arg); simp_ff((Obj)p2,&t); p2 = (P)t;
                   1228:        d1 = getdeg(VR(p1),p1); up1 = W_UMALLOC(d1); ptosfum(p1,up1);
                   1229:        d2 = getdeg(VR(p2),p2); up2 = W_UMALLOC(d2); ptosfum(p2,up2);
                   1230:        m = W_UMALLOC(d2);
                   1231:        minipolysf(up1,up2,m);
                   1232:        sfumtop(VR(p2),m,&p1);
                   1233:        sfptop(p1,rp);
1.11      noro     1234: }
                   1235:
1.16      noro     1236: void Pptosfp(NODE arg,P *rp)
1.11      noro     1237: {
                   1238:        ptosfp(ARG0(arg),rp);
1.6       noro     1239: }
                   1240:
1.16      noro     1241: void Psfptop(NODE arg,P *rp)
1.6       noro     1242: {
                   1243:        sfptop((P)ARG0(arg),rp);
1.18      noro     1244: }
                   1245:
                   1246: void Psfptopsfp(NODE arg,P *rp)
                   1247: {
                   1248:        sfptopsfp((P)ARG0(arg),VR((P)ARG1(arg)),rp);
1.1       noro     1249: }
                   1250:
1.16      noro     1251: void Pptogf2n(NODE arg,GF2N *rp)
1.1       noro     1252: {
                   1253:        ptogf2n((Obj)ARG0(arg),rp);
                   1254: }
                   1255:
1.16      noro     1256: void Pgf2ntop(NODE arg,P *rp)
1.1       noro     1257: {
                   1258:        if ( argc(arg) == 2 )
                   1259:                up2_var = VR((P)ARG1(arg));
                   1260:        gf2ntop((GF2N)ARG0(arg),rp);
                   1261: }
                   1262:
1.16      noro     1263: void Pgf2ntovect(NODE arg,VECT *rp)
1.1       noro     1264: {
                   1265:        gf2ntovect((GF2N)ARG0(arg),rp);
                   1266: }
                   1267:
1.16      noro     1268: void Pptogfpn(NODE arg,GFPN *rp)
1.1       noro     1269: {
                   1270:        ptogfpn((Obj)ARG0(arg),rp);
                   1271: }
                   1272:
1.16      noro     1273: void Pgfpntop(NODE arg,P *rp)
1.1       noro     1274: {
                   1275:        if ( argc(arg) == 2 )
                   1276:                up_var = VR((P)ARG1(arg));
                   1277:        gfpntop((GFPN)ARG0(arg),rp);
                   1278: }
                   1279:
1.16      noro     1280: void Pureverse(NODE arg,P *rp)
1.1       noro     1281: {
                   1282:        UP p,r;
                   1283:
                   1284:        ptoup((P)ARG0(arg),&p);
1.3       noro     1285:        if ( argc(arg) == 1 )
                   1286:                reverseup(p,p->d,&r);
                   1287:        else
                   1288:                reverseup(p,QTOS((Q)ARG1(arg)),&r);
1.1       noro     1289:        uptop(r,rp);
                   1290: }
                   1291:
1.16      noro     1292: void Putrunc(NODE arg,P *rp)
1.1       noro     1293: {
                   1294:        UP p,r;
                   1295:
                   1296:        ptoup((P)ARG0(arg),&p);
                   1297:        truncup(p,QTOS((Q)ARG1(arg))+1,&r);
                   1298:        uptop(r,rp);
                   1299: }
                   1300:
1.16      noro     1301: void Pudecomp(NODE arg,LIST *rp)
1.1       noro     1302: {
                   1303:        P u,l;
                   1304:        UP p,up,low;
                   1305:        NODE n0,n1;
                   1306:
                   1307:        ptoup((P)ARG0(arg),&p);
                   1308:        decompup(p,QTOS((Q)ARG1(arg))+1,&low,&up);
                   1309:        uptop(low,&l);
                   1310:        uptop(up,&u);
                   1311:        MKNODE(n1,u,0); MKNODE(n0,l,n1);
                   1312:        MKLIST(*rp,n0);
                   1313: }
                   1314:
1.16      noro     1315: void Purembymul(NODE arg,P *rp)
1.1       noro     1316: {
                   1317:        UP p1,p2,r;
                   1318:
                   1319:        if ( !ARG0(arg) || !ARG1(arg) )
                   1320:                *rp = 0;
                   1321:        else {
                   1322:                ptoup((P)ARG0(arg),&p1);
                   1323:                ptoup((P)ARG1(arg),&p2);
                   1324:                rembymulup(p1,p2,&r);
                   1325:                uptop(r,rp);
                   1326:        }
                   1327: }
                   1328:
                   1329: /*
                   1330:  * d1 = deg(p1), d2 = deg(p2)
                   1331:  * d1 <= 2*d2-1
                   1332:  * p2*inv = 1 mod x^d2
                   1333:  */
                   1334:
1.16      noro     1335: void Purembymul_precomp(NODE arg,P *rp)
1.1       noro     1336: {
                   1337:        UP p1,p2,inv,r;
                   1338:
                   1339:        if ( !ARG0(arg) || !ARG1(arg) )
                   1340:                *rp = 0;
                   1341:        else {
                   1342:                ptoup((P)ARG0(arg),&p1);
                   1343:                ptoup((P)ARG1(arg),&p2);
                   1344:                ptoup((P)ARG2(arg),&inv);
                   1345:                if ( p1->d >= 2*p2->d ) {
                   1346:                        error("urembymul_precomp : degree of 1st arg is too large");
                   1347: /*                     fprintf(stderr,"urembymul_precomp : degree of 1st arg is too large"); */
                   1348:                        remup(p1,p2,&r);
                   1349:                } else
                   1350:                        hybrid_rembymulup_special(current_ff,p1,p2,inv,&r);
                   1351:                uptop(r,rp);
                   1352:        }
                   1353: }
                   1354:
1.16      noro     1355: void Puinvmod(NODE arg,P *rp)
1.1       noro     1356: {
                   1357:        UP p,r;
                   1358:
                   1359:        ptoup((P)ARG0(arg),&p);
                   1360:        invmodup(p,QTOS((Q)ARG1(arg)),&r);
                   1361:        uptop(r,rp);
                   1362: }
                   1363:
1.16      noro     1364: void Purevinvmod(NODE arg,P *rp)
1.1       noro     1365: {
                   1366:        UP p,pr,r;
                   1367:
                   1368:        ptoup((P)ARG0(arg),&p);
                   1369:        reverseup(p,p->d,&pr);
                   1370:        invmodup(pr,QTOS((Q)ARG1(arg)),&r);
                   1371:        uptop(r,rp);
                   1372: }
                   1373:
1.16      noro     1374: void Ppwrmod_ff(NODE arg,P *rp)
1.1       noro     1375: {
                   1376:        UP p1,p2;
                   1377:
                   1378:        ptoup((P)ARG0(arg),&p1);
                   1379:        switch ( current_ff ) {
                   1380:                case FF_GFP:
                   1381:                        hybrid_powermodup(p1,&p2); break;
                   1382:                case FF_GF2N:
                   1383:                        powermodup_gf2n(p1,&p2); break;
                   1384:                case FF_GFPN:
1.10      noro     1385:                case FF_GFS:
1.15      noro     1386:                case FF_GFSN:
1.1       noro     1387:                        powermodup(p1,&p2); break;
                   1388:                default:
                   1389:                        error("pwrmod_ff : current_ff is not set");
                   1390:        }
                   1391:        uptop(p2,rp);
                   1392: }
                   1393:
1.16      noro     1394: void Pgeneric_pwrmod_ff(NODE arg,P *rp)
1.1       noro     1395: {
                   1396:        UP g,f,r;
                   1397:
                   1398:        ptoup((P)ARG0(arg),&g);
                   1399:        ptoup((P)ARG1(arg),&f);
                   1400:        switch ( current_ff ) {
                   1401:                case FF_GFP:
                   1402:                        hybrid_generic_powermodup(g,f,(Q)ARG2(arg),&r); break;
                   1403:                case FF_GF2N:
                   1404:                        generic_powermodup_gf2n(g,f,(Q)ARG2(arg),&r); break;
                   1405:                case FF_GFPN:
1.10      noro     1406:                case FF_GFS:
1.15      noro     1407:                case FF_GFSN:
1.1       noro     1408:                        generic_powermodup(g,f,(Q)ARG2(arg),&r); break;
                   1409:                default:
                   1410:                        error("generic_pwrmod_ff : current_ff is not set");
                   1411:        }
                   1412:        uptop(r,rp);
                   1413: }
                   1414:
1.16      noro     1415: void Ppwrtab_ff(NODE arg,VECT *rp)
1.1       noro     1416: {
                   1417:        UP f,xp;
                   1418:        UP *tab;
                   1419:        VECT r;
                   1420:        int i,d;
                   1421:
                   1422:        ptoup((P)ARG0(arg),&f);
                   1423:        ptoup((P)ARG1(arg),&xp);
                   1424:        d = f->d;
                   1425:
                   1426:        tab = (UP *)ALLOCA(d*sizeof(UP));
                   1427:        switch ( current_ff ) {
                   1428:                case FF_GFP:
                   1429:                        hybrid_powertabup(f,xp,tab); break;
                   1430:                case FF_GF2N:
                   1431:                        powertabup_gf2n(f,xp,tab); break;
                   1432:                case FF_GFPN:
1.10      noro     1433:                case FF_GFS:
1.15      noro     1434:                case FF_GFSN:
1.1       noro     1435:                        powertabup(f,xp,tab); break;
                   1436:                default:
                   1437:                        error("pwrtab_ff : current_ff is not set");
                   1438:        }
                   1439:        MKVECT(r,d); *rp = r;
                   1440:        for ( i = 0; i < d; i++ )
                   1441:                uptop(tab[i],(P *)&BDY(r)[i]);
                   1442: }
                   1443:
1.16      noro     1444: void Pkpwrmod_lm(NODE arg,P *rp)
1.1       noro     1445: {
                   1446:        UP p1,p2;
                   1447:
                   1448:        ptoup((P)ARG0(arg),&p1);
                   1449:        powermodup(p1,&p2);
                   1450:        uptop(p2,rp);
                   1451: }
                   1452:
1.16      noro     1453: void Pkgeneric_pwrmod_lm(NODE arg,P *rp)
1.1       noro     1454: {
                   1455:        UP g,f,r;
                   1456:
                   1457:        ptoup((P)ARG0(arg),&g);
                   1458:        ptoup((P)ARG1(arg),&f);
                   1459:        generic_powermodup(g,f,(Q)ARG2(arg),&r);
                   1460:        uptop(r,rp);
                   1461: }
                   1462:
1.16      noro     1463: void Pkpwrtab_lm(NODE arg,VECT *rp)
1.1       noro     1464: {
                   1465:        UP f,xp;
                   1466:        UP *tab;
                   1467:        VECT r;
                   1468:        int i,d;
                   1469:
                   1470:        ptoup((P)ARG0(arg),&f);
                   1471:        ptoup((P)ARG1(arg),&xp);
                   1472:        d = f->d;
                   1473:
                   1474:        tab = (UP *)ALLOCA(d*sizeof(UP));
                   1475:        powertabup(f,xp,tab);
                   1476:        MKVECT(r,d); *rp = r;
                   1477:        for ( i = 0; i < d; i++ )
                   1478:                uptop(tab[i],(P *)&BDY(r)[i]);
                   1479: }
                   1480:
1.16      noro     1481: void Plazy_lm(NODE arg,Q *rp)
1.1       noro     1482: {
                   1483:        lm_lazy = QTOS((Q)ARG0(arg));
                   1484:        *rp = 0;
                   1485: }
                   1486:
1.16      noro     1487: void Pkmul(NODE arg,P *rp)
1.1       noro     1488: {
                   1489:        P n1,n2;
                   1490:
                   1491:        n1 = (P)ARG0(arg); n2 = (P)ARG1(arg);
                   1492:        asir_assert(n1,O_P,"kmul");
                   1493:        asir_assert(n2,O_P,"kmul");
                   1494:        kmulp(CO,n1,n2,rp);
                   1495: }
                   1496:
1.16      noro     1497: void Pksquare(NODE arg,P *rp)
1.1       noro     1498: {
                   1499:        P n1;
                   1500:
                   1501:        n1 = (P)ARG0(arg);
                   1502:        asir_assert(n1,O_P,"ksquare");
                   1503:        ksquarep(CO,n1,rp);
                   1504: }
                   1505:
1.16      noro     1506: void Pktmul(NODE arg,P *rp)
1.1       noro     1507: {
                   1508:        UP p1,p2,r;
                   1509:
                   1510:        ptoup((P)ARG0(arg),&p1);
                   1511:        ptoup((P)ARG1(arg),&p2);
                   1512:        tkmulup(p1,p2,QTOS((Q)ARG2(arg))+1,&r);
                   1513:        uptop(r,rp);
                   1514: }
                   1515:
1.16      noro     1516: void Pumul(NODE arg,P *rp)
1.1       noro     1517: {
                   1518:        P a1,a2;
                   1519:        UP p1,p2,r;
                   1520:
                   1521:        a1 = (P)ARG0(arg); a2 = (P)ARG1(arg);
                   1522:        if ( !a1 || !a2 || NUM(a1) || NUM(a2) )
                   1523:                mulp(CO,a1,a2,rp);
                   1524:        else {
1.16      noro     1525:                if ( !uzpcheck((Obj)a1) || !uzpcheck((Obj)a2) || VR(a1) != VR(a2) )
1.1       noro     1526:                        error("umul : invalid argument");
                   1527:                ptoup(a1,&p1);
                   1528:                ptoup(a2,&p2);
                   1529:                hybrid_mulup(0,p1,p2,&r);
                   1530:                uptop(r,rp);
                   1531:        }
                   1532: }
                   1533:
1.16      noro     1534: void Pusquare(NODE arg,P *rp)
1.1       noro     1535: {
1.16      noro     1536:        UP p1,r;
1.1       noro     1537:
                   1538:        ptoup((P)ARG0(arg),&p1);
                   1539:        hybrid_squareup(0,p1,&r);
                   1540:        uptop(r,rp);
                   1541: }
                   1542:
1.16      noro     1543: void Putmul(NODE arg,P *rp)
1.1       noro     1544: {
                   1545:        UP p1,p2,r;
                   1546:
                   1547:        ptoup((P)ARG0(arg),&p1);
                   1548:        ptoup((P)ARG1(arg),&p2);
                   1549:        hybrid_tmulup(0,p1,p2,QTOS((Q)ARG2(arg))+1,&r);
                   1550:        uptop(r,rp);
                   1551: }
                   1552:
1.16      noro     1553: void Pumul_ff(NODE arg,Obj *rp)
1.1       noro     1554: {
                   1555:        P a1,a2;
                   1556:        UP p1,p2,r;
                   1557:        P p;
                   1558:
                   1559:        a1 = (P)ARG0(arg); a2 = (P)ARG1(arg);
                   1560:        ptoup(a1,&p1);
                   1561:        ptoup(a2,&p2);
                   1562:        hybrid_mulup(current_ff,p1,p2,&r);
                   1563:        uptop(r,&p);
                   1564:        simp_ff((Obj)p,rp);
                   1565: }
                   1566:
1.16      noro     1567: void Pusquare_ff(NODE arg,Obj *rp)
1.1       noro     1568: {
1.16      noro     1569:        UP p1,r;
1.1       noro     1570:        P p;
                   1571:
                   1572:        ptoup((P)ARG0(arg),&p1);
                   1573:        hybrid_squareup(current_ff,p1,&r);
                   1574:        uptop(r,&p);
                   1575:        simp_ff((Obj)p,rp);
                   1576: }
                   1577:
1.16      noro     1578: void Putmul_ff(NODE arg,Obj *rp)
1.1       noro     1579: {
                   1580:        UP p1,p2,r;
                   1581:        P p;
                   1582:
                   1583:        ptoup((P)ARG0(arg),&p1);
                   1584:        ptoup((P)ARG1(arg),&p2);
                   1585:        hybrid_tmulup(current_ff,p1,p2,QTOS((Q)ARG2(arg))+1,&r);
                   1586:        uptop(r,&p);
                   1587:        simp_ff((Obj)p,rp);
                   1588: }
                   1589:
1.16      noro     1590: void Phfmul_lm(NODE arg,P *rp)
1.1       noro     1591: {
                   1592:        UP p1,p2,r;
                   1593:        UP hi,lo,mid,t,s,p10,p11,p20,p21;
                   1594:        unsigned int m,d;
                   1595:        int i;
                   1596:
                   1597:        ptoup((P)ARG0(arg),&p1);
                   1598:        ptoup((P)ARG1(arg),&p2);
                   1599:        d = MAX(p1->d,p2->d);
                   1600:        for ( m = 1; m < d; m <<= 1 );
                   1601:        if ( m > d )
                   1602:                m >>= 1;
                   1603:        if ( d-m < 10000 ) {
                   1604:                decompup(p1,m,&p10,&p11); /* p1 = p11*x^m+p10 */
                   1605:                decompup(p2,m,&p20,&p21); /* p2 = p21*x^m+p20 */
                   1606:                fft_mulup_lm(p10,p20,&lo);
                   1607:                kmulup(p11,p21,&hi);
                   1608:                kmulup(p11,p20,&t); kmulup(p10,p21,&s); addup(t,s,&mid);
                   1609:                r = UPALLOC(2*d);
                   1610:                bzero((char *)COEF(r),(2*d+1)*sizeof(Num));
                   1611:                if ( lo )
                   1612:                        bcopy((char *)COEF(lo),(char *)COEF(r),(DEG(lo)+1)*sizeof(Num));
                   1613:                if ( hi )
                   1614:                        bcopy((char *)COEF(hi),(char *)(COEF(r)+2*m),(DEG(hi)+1)*sizeof(Num));
                   1615:                for ( i = 2*d; i >= 0 && !COEF(r)[i]; i-- );
                   1616:                if ( i < 0 )
                   1617:                        r = 0;
                   1618:                else {
                   1619:                        DEG(r) = i;
                   1620:                        t = UPALLOC(DEG(mid)+m); DEG(t) = DEG(mid)+m;
                   1621:                        bzero((char *)COEF(t),(DEG(mid)+m+1)*sizeof(Num));
                   1622:                        bcopy((char *)COEF(mid),(char *)(COEF(t)+m),(DEG(mid)+1)*sizeof(Num));
                   1623:                        addup(t,r,&s);
                   1624:                        r = s;
                   1625:                }
                   1626:        } else
                   1627:                fft_mulup_lm(p1,p2,&r);
                   1628:        uptop(r,rp);
                   1629: }
                   1630:
1.16      noro     1631: void Pfmultest(NODE arg,LIST *rp)
1.1       noro     1632: {
                   1633:        P p1,p2,r;
                   1634:        int d1,d2;
                   1635:        UM w1,w2,wr;
                   1636:        unsigned int *f1,*f2,*fr,*w;
                   1637:        int index,mod,root,d,maxint,i;
                   1638:        int cond;
                   1639:        Q prime;
                   1640:        NODE n0,n1;
                   1641:
                   1642:        p1 = (P)ARG0(arg); p2 = (P)ARG1(arg); index = QTOS((Q)ARG2(arg));
                   1643:        FFT_primes(index,&mod,&root,&d);
                   1644:        maxint = 1<<d;
                   1645:        d1 = UDEG(p1); d2 = UDEG(p2);
                   1646:        if ( maxint < d1+d2+1 )
                   1647:                *rp = 0;
                   1648:        else {
                   1649:                w1 = W_UMALLOC(d1); w2 = W_UMALLOC(d2);
                   1650:                wr = W_UMALLOC(d1+d2);
                   1651:                ptoum(mod,p1,w1); ptoum(mod,p2,w2);
                   1652:                f1 = (unsigned int *)ALLOCA(maxint*sizeof(unsigned int));
                   1653:                f2 = (unsigned int *)ALLOCA(maxint*sizeof(unsigned int));
                   1654:                fr = (unsigned int *)ALLOCA(maxint*sizeof(unsigned int));
                   1655:                w = (unsigned int *)ALLOCA(12*maxint*sizeof(unsigned int));
                   1656:
                   1657:                for ( i = 0; i <= d1; i++ )
                   1658:                        f1[i] = (unsigned int)w1->c[i];
                   1659:                for ( i = 0; i <= d2; i++ )
                   1660:                        f2[i] = (unsigned int)w2->c[i];
                   1661:
                   1662:                cond = FFT_pol_product(d1,f1,d2,f2,fr,index,w);
                   1663:                if ( cond )
                   1664:                        error("fmultest : ???");
                   1665:                wr->d = d1+d2;
                   1666:                for ( i = 0; i <= d1+d2; i++ )
                   1667:                        wr->c[i] = (unsigned int)fr[i];
                   1668:                umtop(VR(p1),wr,&r);
                   1669:                STOQ(mod,prime);
                   1670:                MKNODE(n1,prime,0);
                   1671:                MKNODE(n0,r,n1);
                   1672:                MKLIST(*rp,n0);
                   1673:        }
                   1674: }
                   1675:
1.16      noro     1676: void Pkmulum(NODE arg,P *rp)
1.1       noro     1677: {
                   1678:        P p1,p2;
                   1679:        int d1,d2,mod;
                   1680:        UM w1,w2,wr;
                   1681:
                   1682:        p1 = (P)ARG0(arg); p2 = (P)ARG1(arg); mod = QTOS((Q)ARG2(arg));
                   1683:        d1 = UDEG(p1); d2 = UDEG(p2);
                   1684:        w1 = W_UMALLOC(d1); w2 = W_UMALLOC(d2);
                   1685:        wr = W_UMALLOC(d1+d2);
                   1686:        ptoum(mod,p1,w1); ptoum(mod,p2,w2);
                   1687:        kmulum(mod,w1,w2,wr);
                   1688:        umtop(VR(p1),wr,rp);
                   1689: }
                   1690:
1.16      noro     1691: void Pksquareum(NODE arg,P *rp)
1.1       noro     1692: {
                   1693:        P p1;
                   1694:        int d1,mod;
                   1695:        UM w1,wr;
                   1696:
                   1697:        p1 = (P)ARG0(arg); mod = QTOS((Q)ARG1(arg));
                   1698:        d1 = UDEG(p1);
                   1699:        w1 = W_UMALLOC(d1);
                   1700:        wr = W_UMALLOC(2*d1);
                   1701:        ptoum(mod,p1,w1);
                   1702:        kmulum(mod,w1,w1,wr);
                   1703:        umtop(VR(p1),wr,rp);
                   1704: }
                   1705:
1.16      noro     1706: void Ptracemod_gf2n(NODE arg,P *rp)
1.1       noro     1707: {
                   1708:        UP g,f,r;
                   1709:
                   1710:        ptoup((P)ARG0(arg),&g);
                   1711:        ptoup((P)ARG1(arg),&f);
                   1712:        tracemodup_gf2n(g,f,(Q)ARG2(arg),&r);
                   1713:        uptop(r,rp);
1.2       noro     1714: }
                   1715:
1.16      noro     1716: void Pumul_specialmod(NODE arg,P *rp)
1.2       noro     1717: {
                   1718:        P a1,a2;
                   1719:        UP p1,p2,r;
                   1720:        int i,nmod;
                   1721:        int *modind;
                   1722:        NODE t,n;
                   1723:
                   1724:        a1 = (P)ARG0(arg); a2 = (P)ARG1(arg);
                   1725:        if ( !a1 || !a2 || NUM(a1) || NUM(a2) )
                   1726:                mulp(CO,a1,a2,rp);
                   1727:        else {
1.16      noro     1728:                if ( !uzpcheck((Obj)a1) || !uzpcheck((Obj)a2) || VR(a1) != VR(a2) )
1.2       noro     1729:                        error("umul_specialmod : invalid argument");
                   1730:                ptoup(a1,&p1);
                   1731:                ptoup(a2,&p2);
                   1732:                n = BDY((LIST)ARG2(arg));
                   1733:                nmod = length(n);
                   1734:                modind = (int *)MALLOC_ATOMIC(nmod*sizeof(int));
                   1735:                for ( i = 0, t = n; i < nmod; i++, t = NEXT(t) )
                   1736:                        modind[i] = QTOS((Q)BDY(t));
                   1737:                fft_mulup_specialmod_main(p1,p2,0,modind,nmod,&r);
                   1738:                uptop(r,rp);
                   1739:        }
                   1740: }
                   1741:
1.16      noro     1742: void Pusquare_specialmod(NODE arg,P *rp)
1.2       noro     1743: {
                   1744:        P a1;
                   1745:        UP p1,r;
                   1746:        int i,nmod;
                   1747:        int *modind;
                   1748:        NODE t,n;
                   1749:
                   1750:        a1 = (P)ARG0(arg);
                   1751:        if ( !a1 || NUM(a1) )
                   1752:                mulp(CO,a1,a1,rp);
                   1753:        else {
1.16      noro     1754:                if ( !uzpcheck((Obj)a1) )
1.2       noro     1755:                        error("usquare_specialmod : invalid argument");
                   1756:                ptoup(a1,&p1);
                   1757:                n = BDY((LIST)ARG1(arg));
                   1758:                nmod = length(n);
                   1759:                modind = (int *)MALLOC_ATOMIC(nmod*sizeof(int));
                   1760:                for ( i = 0, t = n; i < nmod; i++, t = NEXT(t) )
                   1761:                        modind[i] = QTOS((Q)BDY(t));
                   1762:                fft_mulup_specialmod_main(p1,p1,0,modind,nmod,&r);
                   1763:                uptop(r,rp);
                   1764:        }
                   1765: }
                   1766:
1.16      noro     1767: void Putmul_specialmod(NODE arg,P *rp)
1.2       noro     1768: {
                   1769:        P a1,a2;
                   1770:        UP p1,p2,r;
                   1771:        int i,nmod;
                   1772:        int *modind;
                   1773:        NODE t,n;
                   1774:
                   1775:        a1 = (P)ARG0(arg); a2 = (P)ARG1(arg);
                   1776:        if ( !a1 || !a2 || NUM(a1) || NUM(a2) )
                   1777:                mulp(CO,a1,a2,rp);
                   1778:        else {
1.16      noro     1779:                if ( !uzpcheck((Obj)a1) || !uzpcheck((Obj)a2) || VR(a1) != VR(a2) )
1.2       noro     1780:                        error("utmul_specialmod : invalid argument");
                   1781:                ptoup(a1,&p1);
                   1782:                ptoup(a2,&p2);
                   1783:                n = BDY((LIST)ARG3(arg));
                   1784:                nmod = length(n);
                   1785:                modind = (int *)MALLOC_ATOMIC(nmod*sizeof(int));
                   1786:                for ( i = 0, t = n; i < nmod; i++, t = NEXT(t) )
                   1787:                        modind[i] = QTOS((Q)BDY(t));
                   1788:                fft_mulup_specialmod_main(p1,p2,QTOS((Q)ARG2(arg))+1,modind,nmod,&r);
                   1789:                uptop(r,rp);
                   1790:        }
1.1       noro     1791: }

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