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

Annotation of OpenXM_contrib2/asir2000/builtin/parif.c, Revision 1.17

1.3       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.4       noro       26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.3       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.17    ! saito      48:  * $OpenXM: OpenXM_contrib2/asir2000/builtin/parif.c,v 1.16 2009/03/27 14:42:29 ohara Exp $
1.3       noro       49: */
1.1       noro       50: #include "ca.h"
                     51: #include "parse.h"
                     52:
1.14      ohara      53: #if defined(PARI)
1.1       noro       54: #include "genpari.h"
1.16      ohara      55: // PARI_VERSION(2,2,8) == 131592
                     56: #if PARI_VERSION_CODE >= 131592
                     57: #define mppgcd(a,b)  (gcdii((a),(b)))
                     58: #endif
1.1       noro       59:
1.16      ohara      60: long get_pariprec();
                     61: void set_pariprec(long p);
1.1       noro       62:
                     63: void patori(GEN,Obj *);
                     64: void patori_i(GEN,N *);
                     65: void ritopa(Obj,GEN *);
                     66: void ritopa_i(N,int,GEN *);
                     67:
1.11      noro       68: void Ptodouble();
1.1       noro       69: void Peval(),Psetprec(),p_pi(),p_e(),p_mul(),p_gcd();
1.5       noro       70: void asir_cgiv(GEN);
1.1       noro       71:
1.7       saito      72: #if defined(INTERVAL) || 1
                     73: void Psetprecword();
                     74: #endif
                     75:
1.1       noro       76: struct ftab pari_tab[] = {
1.7       saito      77:        {"eval",Peval,-2},
                     78:        {"setprec",Psetprec,-1},
1.11      noro       79:        {"todouble",Ptodouble,1},
1.7       saito      80: #if defined(INTERVAL) || 1
                     81:        {"setprecword",Psetprecword,-1},
                     82: #endif
                     83:        {0,0,0},
1.1       noro       84: };
                     85:
1.16      ohara      86: #define MKPREC(a,i,b) (argc(a)==(i)?mkprec(QTOS((Q)(b))):get_pariprec())
1.1       noro       87:
                     88: #define CALLPARI1(f,a,p,r)\
1.5       noro       89: ritopa((Obj)a,&_pt1_); _pt2_ = f(_pt1_,p); patori(_pt2_,r); asir_cgiv(_pt2_); asir_cgiv(_pt1_)
1.1       noro       90: #define CALLPARI2(f,a,b,p,r)\
1.5       noro       91: ritopa((Obj)a,&_pt1_); ritopa((Obj)b,&_pt2_); _pt3_ = f(_pt1_,_pt2_,p); patori(_pt3_,r); asir_cgiv(_pt3_); asir_cgiv(_pt2_); asir_cgiv(_pt1_)
1.1       noro       92:
                     93: #define PARIF1P(f,pf)\
                     94: void f(NODE,Obj *);\
                     95: void f(ar,rp) NODE ar; Obj *rp;\
                     96: { GEN _pt1_,_pt2_; CALLPARI1(pf,ARG0(ar),MKPREC(ar,2,ARG1(ar)),rp); }
                     97: #define PARIF2P(f,pf)\
                     98: void f(NODE,Obj *);\
                     99: void f(ar,rp) NODE ar; Obj *rp;\
                    100: { GEN _pt1_,_pt2_,_pt3_; CALLPARI2(pf,ARG0(ar),ARG1(ar),MKPREC(ar,3,ARG2(ar)),rp); }
                    101:
1.7       saito     102: #if defined(INTERVAL)
1.17    ! saito     103: #define        PREC_CONV       (0.103810253/(BYTES_IN_LONG/4))  /* log(10)/(SL*log(2)) */
1.15      ohara     104: #elif SIZEOF_LONG == 4
1.1       noro      105: #define PREC_CONV              0.103810253
1.15      ohara     106: #elif SIZEOF_LONG == 8
1.1       noro      107: #define PREC_CONV              0.051905126
                    108: #endif
                    109:
1.5       noro      110: /* XXX : we should be more careful when we free PARI pointers. */
                    111:
                    112: void asir_cgiv(ptr)
                    113: GEN ptr;
                    114: {
                    115:        if ( ptr != gzero && ptr != gun
                    116:        && ptr != gdeux && ptr != ghalf
                    117:        && ptr != polvar && ptr != gi )
                    118:                cgiv(ptr);
                    119: }
                    120:
1.1       noro      121: mkprec(p)
                    122: int p;
                    123: {
1.10      noro      124:        if ( p <= 0 )
                    125:                p = 1;
                    126:        return (int)(p*PREC_CONV+3);
1.11      noro      127: }
                    128:
                    129: void Ptodouble(arg,rp)
                    130: NODE arg;
                    131: Num *rp;
                    132: {
                    133:        double r,i;
                    134:        Real real,imag;
                    135:        Num num;
                    136:
                    137:        asir_assert(ARG0(arg),O_N,"todouble");
                    138:        num = (Num)ARG0(arg);
                    139:        if ( !num ) {
                    140:                *rp = 0;
                    141:                return;
                    142:        }
                    143:        switch ( NID(num) ) {
                    144:                case N_R: case N_Q: case N_B:
                    145:                        r = ToReal(num);
                    146:                        MKReal(r,real);
                    147:                        *rp = (Num)real;
                    148:                        break;
                    149:                case N_C:
                    150:                        r = ToReal(((C)num)->r);
                    151:                        i = ToReal(((C)num)->i);
                    152:                        MKReal(r,real);
                    153:                        MKReal(i,imag);
                    154:                        reimtocplx((Num)real,(Num)imag,rp);
                    155:                        break;
                    156:                default:
                    157:                        *rp = num;
                    158:                        break;
                    159:        }
1.1       noro      160: }
                    161:
                    162: void Peval(arg,rp)
                    163: NODE arg;
                    164: Obj *rp;
                    165: {
                    166:        asir_assert(ARG0(arg),O_R,"eval");
                    167:        evalr(CO,(Obj)ARG0(arg),argc(arg)==2?QTOS((Q)ARG1(arg)):0,rp);
                    168: }
                    169:
                    170: void Psetprec(arg,rp)
                    171: NODE arg;
                    172: Obj *rp;
                    173: {
                    174:        int p;
                    175:        Q q;
1.16      ohara     176:        long prec = get_pariprec();
1.1       noro      177:
1.7       saito     178: #if defined(INTERVAL) || 1
                    179:        p = (int)((prec-2)/PREC_CONV); STOQ(p,q); *rp = (Obj)q;
                    180:        if ( arg ) {
                    181:                asir_assert(ARG0(arg),O_N,"setprec");
                    182:                p = QTOS((Q)ARG0(arg));
                    183:                if ( p > 0 )
                    184:                        prec = (long)(p*PREC_CONV+3);
                    185:        }
                    186: #else
1.1       noro      187:        p = (int)((prec-3)/PREC_CONV); STOQ(p,q); *rp = (Obj)q;
                    188:        if ( arg ) {
                    189:                asir_assert(ARG0(arg),O_N,"setprec");
                    190:                prec = mkprec(QTOS((Q)ARG0(arg)));
                    191:        }
1.7       saito     192: #endif
1.16      ohara     193:        set_pariprec(prec);
1.1       noro      194: }
1.7       saito     195:
                    196: #if defined(INTERVAL) || 1
                    197: void Psetprecword(arg,rp)
                    198: NODE arg;
                    199: Obj *rp;
                    200: {
                    201:        int p;
                    202:        Q q;
1.16      ohara     203:        long prec = get_pariprec();
1.7       saito     204:
                    205:        p = (int)((prec-2)); STOQ(p,q); *rp = (Obj)q;
                    206:        if ( arg ) {
                    207:                asir_assert(ARG0(arg),O_N,"setprecword");
                    208:                p = QTOS((Q)ARG0(arg));
                    209:                if ( p > 0 ) {
                    210:                        prec = p + 2;
                    211:                }
                    212:        }
1.16      ohara     213:        set_pariprec(prec);
1.7       saito     214: }
                    215: #endif
1.1       noro      216:
                    217: void p_pi(arg,rp)
                    218: NODE arg;
                    219: Obj *rp;
                    220: {
                    221:        GEN x;
                    222:
                    223:        x = mppi(MKPREC(arg,1,ARG0(arg)));
1.5       noro      224:        patori(x,rp); asir_cgiv(x);
1.1       noro      225: }
                    226:
                    227: void p_e(arg,rp)
                    228: NODE arg;
                    229: Obj *rp;
                    230: {
                    231:        GEN x;
                    232:
1.5       noro      233:        x = gexp(gun,MKPREC(arg,1,ARG0(arg))); patori(x,rp); asir_cgiv(x);
1.1       noro      234: }
                    235:
                    236: void p_mul(a,b,r)
                    237: Obj a,b,*r;
                    238: {
                    239:        GEN p1,p2,p3;
                    240:
                    241:        ritopa((Obj)a,&p1); ritopa((Obj)b,&p2);
                    242:        p3 = mulii(p1,p2);
1.5       noro      243:        patori(p3,r); asir_cgiv(p3); asir_cgiv(p2); asir_cgiv(p1);
1.1       noro      244: }
                    245:
                    246: void p_gcd(a,b,r)
                    247: N a,b,*r;
                    248: {
                    249:        GEN p1,p2,p3;
                    250:
                    251:        ritopa_i(a,1,&p1); ritopa_i(b,1,&p2);
                    252:        p3 = mppgcd(p1,p2);
1.5       noro      253:        patori_i(p3,r); asir_cgiv(p3); asir_cgiv(p2); asir_cgiv(p1);
1.1       noro      254: }
                    255:
                    256: PARIF1P(p_sin,gsin) PARIF1P(p_cos,gcos) PARIF1P(p_tan,gtan)
                    257: PARIF1P(p_asin,gasin) PARIF1P(p_acos,gacos) PARIF1P(p_atan,gatan)
                    258: PARIF1P(p_sinh,gsh) PARIF1P(p_cosh,gch) PARIF1P(p_tanh,gth)
                    259: PARIF1P(p_asinh,gash) PARIF1P(p_acosh,gach) PARIF1P(p_atanh,gath)
                    260: PARIF1P(p_exp,gexp) PARIF1P(p_log,glog)
                    261: PARIF1P(p_dilog,dilog) PARIF1P(p_erf,gerfc)
                    262: PARIF1P(p_eigen,eigen) PARIF1P(p_roots,roots)
                    263:
                    264: PARIF2P(p_pow,gpui)
                    265:
                    266: pointer evalparif(f,arg)
                    267: FUNC f;
                    268: NODE arg;
                    269: {
                    270:        GEN a,v;
                    271:        long ltop,lbot;
                    272:        pointer r;
1.9       noro      273:        int ac,opt,intarg,ret;
1.1       noro      274:        char buf[BUFSIZ];
1.9       noro      275:        Q q;
1.8       noro      276:        GEN (*dmy)();
1.1       noro      277:
                    278:        if ( !f->f.binf ) {
                    279:                sprintf(buf,"pari : %s undefined.",f->name);
                    280:                error(buf);
1.10      noro      281:                /* NOTREACHED */
                    282:                return 0;
1.1       noro      283:        }
                    284:        switch ( f->type ) {
1.9       noro      285:                case 0: /* in/out : integer */
                    286:                        ac = argc(arg);
                    287:                        if ( ac > 2 ) {
                    288:                                fprintf(stderr,"argument mismatch in %s()\n",NAME(f));
                    289:                                error("");
1.10      noro      290:                                /* NOTREACHED */
                    291:                                return 0;
1.9       noro      292:                        }
                    293:                        intarg = !ac ? 0 : QTOS((Q)ARG0(arg));
                    294:                        dmy = (GEN (*)())f->f.binf;
1.10      noro      295:                        ret = (int)(*dmy)(intarg);
1.9       noro      296:                        STOQ(ret,q);
                    297:                        return (pointer)q;
                    298:
1.1       noro      299:                case 1:
                    300:                        ac = argc(arg);
                    301:                        if ( !ac || ( ac > 2 ) ) {
                    302:                                fprintf(stderr,"argument mismatch in %s()\n",NAME(f));
                    303:                                error("");
1.10      noro      304:                                /* NOTREACHED */
                    305:                                return 0;
1.1       noro      306:                        }
                    307:                        ltop = avma;
                    308:                        ritopa((Obj)ARG0(arg),&a);
                    309:                        dmy = (GEN (*)())f->f.binf;
                    310:                        v = (*dmy)(a,MKPREC(arg,2,ARG1(arg)));
                    311:                        lbot = avma;
                    312:                        patori(v,(Obj *)&r); gerepile(ltop,lbot,0);
                    313:                        return r;
1.8       noro      314:
                    315:                case 2:
                    316:                        ac = argc(arg);
                    317:                        if ( !ac || ( ac > 2 ) ) {
                    318:                                fprintf(stderr,"argument mismatch in %s()\n",NAME(f));
                    319:                                error("");
1.10      noro      320:                                /* NOTREACHED */
                    321:                                return 0;
1.8       noro      322:                        }
                    323:                        if ( ac == 1 )
                    324:                                opt = 0;
                    325:                        else
                    326:                                opt = QTOS((Q)ARG1(arg));
                    327:                        ltop = avma;
                    328:                        ritopa((Obj)ARG0(arg),&a);
                    329:                        dmy = (GEN (*)())f->f.binf;
                    330:                        v = (*dmy)(a,opt);
                    331:                        lbot = avma;
                    332:                        patori(v,(Obj *)&r); gerepile(ltop,lbot,0);
                    333:                        return r;
                    334:
1.1       noro      335:                default:
                    336:                        error("evalparif : not implemented yet.");
1.10      noro      337:                        /* NOTREACHED */
                    338:                        return 0;
1.1       noro      339:        }
                    340: }
                    341:
                    342: struct pariftab {
                    343:        char *name;
                    344:        GEN (*f)();
                    345:        int type;
                    346: };
                    347:
1.8       noro      348: /*
                    349:  * type = 1 => argc = 1, second arg = precision
                    350:  * type = 2 => argc = 1, second arg = optional (long int)
                    351:  *
                    352:  */
                    353:
1.1       noro      354: struct pariftab pariftab[] = {
1.10      noro      355: {"allocatemem",(GEN(*)())allocatemoremem,0},
1.1       noro      356: {"abs",(GEN (*)())gabs,1},
                    357: {"adj",adj,1},
                    358: {"arg",garg,1},
                    359: {"bigomega",gbigomega,1},
                    360: {"binary",binaire,1},
                    361: {"ceil",gceil,1},
                    362: {"centerlift",centerlift,1},
                    363: {"cf",gcf,1},
                    364: {"classno",classno,1},
                    365: {"classno2",classno2,1},
                    366: {"conj",gconj,1},
                    367: {"content",content,1},
                    368: {"denom",denom,1},
                    369: {"det",det,1},
                    370: {"det2",det2,1},
                    371: {"dilog",dilog,1},
                    372: {"disc",discsr,1},
                    373: {"discf",discf,1},
                    374: {"divisors",divisors,1},
                    375: {"eigen",eigen,1},
                    376: {"eintg1",eint1,1},
                    377: {"erfc",gerfc,1},
                    378: {"eta",eta,1},
                    379: {"floor",gfloor,1},
                    380: {"frac",gfrac,1},
                    381: {"galois",galois,1},
                    382: {"galoisconj",galoisconj,1},
                    383: {"gamh",ggamd,1},
                    384: {"gamma",ggamma,1},
                    385: {"hclassno",classno3,1},
                    386: {"hermite",hnf,1},
                    387: {"hess",hess,1},
                    388: {"imag",gimag,1},
                    389: {"image",image,1},
                    390: {"image2",image2,1},
                    391: {"indexrank",indexrank,1},
                    392: {"indsort",indexsort,1},
                    393: {"initalg",initalg,1},
                    394: {"isfund",gisfundamental,1},
1.13      noro      395: {"isprime",gisprime,2},
1.1       noro      396: {"ispsp",gispsp,1},
                    397: {"isqrt",racine,1},
                    398: {"issqfree",gissquarefree,1},
                    399: {"issquare",gcarreparfait,1},
                    400: {"jacobi",jacobi,1},
                    401: {"jell",jell,1},
                    402: {"ker",ker,1},
                    403: {"keri",keri,1},
                    404: {"kerint",kerint,1},
                    405: {"kerintg1",kerint1,1},
                    406: {"length",(GEN(*)())glength,1},
                    407: {"lexsort",lexsort,1},
                    408: {"lift",lift,1},
                    409: {"lindep",lindep,1},
                    410: {"lll",lll,1},
                    411: {"lllgen",lllgen,1},
                    412: {"lllgram",lllgram,1},
                    413: {"lllgramgen",lllgramgen,1},
                    414: {"lllgramint",lllgramint,1},
                    415: {"lllgramkerim",lllgramkerim,1},
                    416: {"lllgramkerimgen",lllgramkerimgen,1},
                    417: {"lllint",lllint,1},
                    418: {"lllkerim",lllkerim,1},
                    419: {"lllkerimgen",lllkerimgen,1},
                    420: {"lngamma",glngamma,1},
                    421: {"logagm",glogagm,1},
                    422: {"mat",gtomat,1},
                    423: {"matrixqz2",matrixqz2,1},
                    424: {"matrixqz3",matrixqz3,1},
                    425: {"matsize",matsize,1},
                    426: {"modreverse",polymodrecip,1},
                    427: {"mu",gmu,1},
                    428: {"nextprime",nextprime,1},
                    429: {"norm",gnorm,1},
                    430: {"norml2",gnorml2,1},
                    431: {"numdiv",numbdiv,1},
                    432: {"numer",numer,1},
                    433: {"omega",gomega,1},
                    434: {"order",order,1},
                    435: {"ordred",ordred,1},
                    436: {"phi",phi,1},
                    437: {"pnqn",pnqn,1},
                    438: {"polred",polred,1},
                    439: {"polred2",polred2,1},
                    440: {"primroot",gener,1},
                    441: {"psi",gpsi,1},
                    442: {"quadgen",quadgen    ,1},
                    443: {"quadpoly",quadpoly    ,1},
                    444: {"real",greal,1},
                    445: {"recip",polrecip       ,1},
                    446: {"redreal",redreal       ,1},
                    447: {"regula",regula  ,1},
                    448: {"reorder",reorder  ,1},
                    449: {"reverse",recip  ,1},
                    450: {"rhoreal",rhoreal       ,1},
                    451: {"roots",roots,1},
                    452: {"round",ground,1},
                    453: {"sigma",sumdiv,1},
                    454: {"signat",signat,1},
                    455: {"simplify",simplify,1},
                    456: {"smalldiscf",smalldiscf,1},
                    457: {"smallfact",smallfact,1},
                    458: {"smallpolred",smallpolred,1},
                    459: {"smallpolred2",smallpolred2,1},
                    460: {"smith",smith,1},
                    461: {"smith2",smith2,1},
                    462: {"sort",sort,1},
                    463: {"sqr",gsqr,1},
                    464: {"sqred",sqred,1},
                    465: {"sqrt",gsqrt,1},
                    466: {"supplement",suppl,1},
                    467: {"trace",gtrace,1},
                    468: {"trans",gtrans,1},
                    469: {"trunc",gtrunc,1},
                    470: {"unit",fundunit,1},
                    471: {"vec",gtovec,1},
                    472: {"wf",wf,1},
                    473: {"wf2",wf2,1},
                    474: {"zeta",gzeta,1},
1.9       noro      475: {"factor",factor,1},
1.8       noro      476: {"factorint",factorint,2},
1.1       noro      477: {0,0,0},
                    478: };
                    479:
                    480: void parif_init() {
                    481:        int i;
                    482:
                    483:        for ( i = 0, parif = 0; pariftab[i].name; i++ )
                    484:                 appendparif(&parif,pariftab[i].name, (int (*)())pariftab[i].f,pariftab[i].type);
                    485: }
                    486: #else /* PARI */
                    487:
                    488: struct ftab pari_tab[] = {
                    489:        {0,0,0},
                    490: };
                    491:
                    492: void parif_init() {}
                    493:
1.2       noro      494: pointer evalparif(f,arg)
                    495: FUNC f;
                    496: NODE arg;
                    497: {
1.1       noro      498:        error("evalparif : PARI is not combined.");
                    499: }
                    500: #endif /*PARI */

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