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

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

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