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

Annotation of OpenXM_contrib2/asir2000/parse/arith.c, Revision 1.10

1.2       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.3       noro       26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2       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.10    ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/parse/arith.c,v 1.9 2003/03/27 02:59:16 noro Exp $
1.2       noro       49: */
1.1       noro       50: #include "ca.h"
                     51: #include "parse.h"
                     52:
                     53: struct oAFUNC {
                     54:        void (*add)();
                     55:        void (*sub)();
                     56:        void (*mul)();
                     57:        void (*div)();
                     58:        void (*pwr)();
                     59:        void (*chsgn)();
                     60:        int (*comp)();
                     61: };
                     62:
                     63: struct oARF arf[6];
                     64: ARF addfs, subfs, mulfs, divfs, remfs, pwrfs;
                     65:
                     66: void divsdc();
                     67:
                     68: struct oAFUNC afunc[] = {
1.4       noro       69: /* ???=0 */    {0,0,0,0,0,0,0},
                     70: /* O_N=1 */    {addnum,subnum,mulnum,divnum,pwrnum,chsgnnum,compnum},
                     71: /* O_P=2 */    {addp,subp,mulp,divr,pwrp,chsgnp,compp},
                     72: /* O_R=3 */    {addr,subr,mulr,divr,pwrr,chsgnr,compr},
                     73: /* O_LIST=4 */ {notdef,notdef,notdef,notdef,notdef,notdef,complist},
                     74: /* O_VECT=5 */ {addvect,subvect,mulvect,divvect,notdef,chsgnvect,compvect},
                     75: /* O_MAT=6 */  {addmat,submat,mulmat,divmat,pwrmat,chsgnmat,compmat},
                     76: /* O_STR=7 */  {addstr,notdef,notdef,notdef,notdef,notdef,compstr},
                     77: /* O_COMP=8 */ {addcomp,subcomp,mulcomp,divcomp,pwrcomp,chsgncomp,compcomp},
                     78: /* O_DP=9 */   {addd,subd,muld,divsdc,notdef,chsgnd,compd},
                     79: /* O_USINT=10 */       {notdef,notdef,notdef,notdef,notdef,notdef,compui},
                     80: /* O_ERR=11 */ {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
                     81: /* O_GF2MAT=12 */      {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
                     82: /* O_MATHCAP=13 */     {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
                     83: /* O_F=14 */   {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
                     84: /* O_GFMMAT=15 */      {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
                     85: /* O_BYTEARRAY=16 */   {notdef,notdef,notdef,notdef,notdef,notdef,compbytearray},
1.6       noro       86: /* O_QUOTE=17 */       {addquote,subquote,mulquote,divquote,pwrquote,chsgnquote,(int(*)())notdef},
1.1       noro       87: };
                     88:
                     89: void arf_init() {
                     90:        addfs = &arf[0]; addfs->name = "+"; addfs->fp = arf_add;
                     91:        subfs = &arf[1]; subfs->name = "-"; subfs->fp = arf_sub;
                     92:        mulfs = &arf[2]; mulfs->name = "*"; mulfs->fp = arf_mul;
                     93:        divfs = &arf[3]; divfs->name = "/"; divfs->fp = arf_div;
                     94:        remfs = &arf[4]; remfs->name = "%"; remfs->fp = arf_remain;
                     95:        pwrfs = &arf[5]; pwrfs->name = "^"; pwrfs->fp = arf_pwr;
                     96: }
                     97:
                     98: void arf_add(vl,a,b,r)
                     99: VL vl;
                    100: Obj a,b,*r;
                    101: {
                    102:        int mid;
                    103:
                    104:        if ( !a )
                    105:                *r = b;
                    106:        else if ( !b )
                    107:                *r = a;
                    108:        else if ( OID(a) == OID(b) )
                    109:                (*afunc[OID(a)].add)(vl,a,b,r);
                    110:        else if ( (mid = MAX(OID(a),OID(b))) <= O_R )
                    111:                (*afunc[mid].add)(vl,a,b,r);
                    112:        else
                    113:                notdef(vl,a,b,r);
                    114: }
                    115:
                    116: void arf_sub(vl,a,b,r)
                    117: VL vl;
                    118: Obj a,b,*r;
                    119: {
                    120:        int mid;
                    121:
                    122:        if ( !a )
                    123:                if ( !b )
                    124:                        *r = 0;
                    125:                else
                    126:                        (*afunc[OID(b)].chsgn)(b,r);
                    127:        else if ( !b )
                    128:                *r = a;
                    129:        else if ( OID(a) == OID(b) )
                    130:                (*afunc[OID(a)].sub)(vl,a,b,r);
                    131:        else if ( (mid = MAX(OID(a),OID(b))) <= O_R )
                    132:                (*afunc[mid].sub)(vl,a,b,r);
                    133:        else
                    134:                notdef(vl,a,b,r);
                    135: }
                    136:
                    137: void arf_mul(vl,a,b,r)
                    138: VL vl;
                    139: Obj a,b,*r;
                    140: {
1.10    ! noro      141:        int mid,aid,bid;
1.1       noro      142:
1.9       noro      143:        if ( !a && !b )
1.1       noro      144:                *r = 0;
1.9       noro      145:        else if ( !a || !b ) {
                    146:                if ( !a )
                    147:                        a = b;
                    148:                /* compute a*0 */
                    149:                if ( OID(a) == O_MAT || OID(a) == O_VECT )
                    150:                        (*(afunc[O_MAT].mul))(vl,a,0,r);
                    151:                else
                    152:                        *r = 0;
1.10    ! noro      153:        } else if ( (aid = OID(a)) == (bid = OID(b)) )
        !           154:                (*(afunc[aid].mul))(vl,a,b,r);
        !           155:        else if ( (mid = MAX(aid,bid)) <= O_R )
1.1       noro      156:                (*afunc[mid].mul)(vl,a,b,r);
1.10    ! noro      157:        else {
        !           158:                switch ( aid ) {
        !           159:                        case O_N: case O_P:
        !           160:                                (*afunc[mid].mul)(vl,a,b,r);
        !           161:                                break;
        !           162:                        case O_R:
        !           163:                                /* rat * something; bid > O_R */
        !           164:                                if ( bid == O_VECT || bid == O_MAT )
        !           165:                                        (*afunc[mid].mul)(vl,a,b,r);
        !           166:                                else
        !           167:                                        notdef(vl,a,b,r);
        !           168:                                break;
        !           169:                        case O_MAT:
        !           170:                                if ( bid <= O_R || bid == O_VECT || bid == O_DP )
        !           171:                                        (*afunc[O_MAT].mul)(vl,a,b,r);
        !           172:                                else
        !           173:                                        notdef(vl,a,b,r);
        !           174:                                break;
        !           175:                        case O_VECT:
        !           176:                                if ( bid <= O_R || bid == O_DP )
        !           177:                                        (*afunc[O_VECT].mul)(vl,a,b,r);
        !           178:                                else if ( bid == O_MAT )
        !           179:                                        (*afunc[O_MAT].mul)(vl,a,b,r);
        !           180:                                else
        !           181:                                        notdef(vl,a,b,r);
        !           182:                                break;
        !           183:                        case O_DP:
        !           184:                                if ( bid <= O_P )
        !           185:                                        (*afunc[O_DP].mul)(vl,a,b,r);
        !           186:                                else if ( bid == O_MAT || bid == O_VECT )
        !           187:                                        (*afunc[bid].mul)(vl,a,b,r);
        !           188:                                else
        !           189:                                        notdef(vl,a,b,r);
        !           190:                                break;
        !           191:                        default:
        !           192:                                        notdef(vl,a,b,r);
        !           193:                                break;
        !           194:                }
        !           195:        }
1.1       noro      196: }
                    197:
                    198: void arf_div(vl,a,b,r)
                    199: VL vl;
                    200: Obj a,b,*r;
                    201: {
                    202:        int mid;
                    203:
                    204:        if ( !b )
                    205:                error("div : division by 0");
                    206:        if ( !a )
                    207:                *r = 0;
                    208:        else if ( (OID(a) == OID(b)) )
                    209:                (*(afunc[OID(a)].div))(vl,a,b,r);
                    210:        else if ( (mid = MAX(OID(a),OID(b))) <= O_R ||
                    211:                (mid == O_MAT) || (mid == O_VECT) || (mid == O_DP) )
                    212:                (*afunc[mid].div)(vl,a,b,r);
                    213:        else
                    214:                notdef(vl,a,b,r);
                    215: }
                    216:
                    217: void arf_remain(vl,a,b,r)
                    218: VL vl;
                    219: Obj a,b,*r;
                    220: {
                    221:        if ( !b )
                    222:                error("rem : division by 0");
                    223:        else if ( !a )
                    224:                *r = 0;
                    225:        else if ( MAX(OID(a),OID(b)) <= O_P )
                    226:                cmp((Q)b,(P)a,(P *)r);
                    227:        else
                    228:                notdef(vl,a,b,r);
                    229: }
                    230:
                    231: void arf_pwr(vl,a,e,r)
                    232: VL vl;
                    233: Obj a,e,*r;
                    234: {
                    235:        R t;
                    236:
                    237:        if ( !a )
1.8       noro      238:                if ( !e )
                    239:                        *r = (pointer)ONE;
                    240:                else
                    241:                        *r = 0;
1.6       noro      242:        else if ( OID(a) == O_QUOTE )
                    243:                (*(afunc[O_QUOTE].pwr))(vl,a,e,r);
1.9       noro      244:        else if ( !e ) {
                    245:                if ( OID(a) == O_MAT )
                    246:                        (*(afunc[O_MAT].pwr))(vl,a,e,r);
                    247:                else
                    248:                        *r = (pointer)ONE;
                    249:        } else if ( (OID(e) <= O_N) && INT(e) ) {
1.1       noro      250:                if ( (OID(a) == O_P) && (SGN((Q)e) < 0) ) {
                    251:                        MKRAT((P)a,(P)ONE,1,t);
                    252:                        (*(afunc[O_R].pwr))(vl,t,e,r);
                    253:                } else
                    254:                        (*(afunc[OID(a)].pwr))(vl,a,e,r);
                    255:        } else if ( OID(a) <= O_R )
                    256:                mkpow(vl,a,e,r);
                    257:        else
                    258:                notdef(vl,a,e,r);
                    259: }
                    260:
                    261: void arf_chsgn(a,r)
                    262: Obj a,*r;
                    263: {
                    264:        if ( !a )
                    265:                *r = 0;
                    266:        else
                    267:                (*(afunc[OID(a)].chsgn))(a,r);
                    268: }
                    269:
                    270: int arf_comp(vl,a,b)
                    271: VL vl;
                    272: Obj a,b;
                    273: {
                    274:        if ( !a )
                    275:                if ( !b )
                    276:                        return 0;
                    277:                else
                    278:                        return (*afunc[OID(b)].comp)(vl,a,b);
                    279:        else if ( !b )
                    280:                        return (*afunc[OID(a)].comp)(vl,a,b);
                    281:        else if ( OID(a) != OID(b) )
                    282:                return OID(a)>OID(b) ? 1 : -1;
                    283:        else
                    284:                return (*afunc[OID(a)].comp)(vl,a,b);
                    285: }
                    286:
                    287: int complist(vl,a,b)
                    288: VL vl;
                    289: LIST a,b;
                    290: {
1.7       noro      291:        int t;
1.1       noro      292:        NODE an,bn;
                    293:
                    294:        if ( !a )
                    295:                if ( !b )
                    296:                        return 0;
                    297:                else
                    298:                        return -1;
                    299:        else if ( !b )
                    300:                return 1;
1.5       noro      301:        for ( an = BDY(a), bn = BDY(b); an && bn; an = NEXT(an), bn = NEXT(bn) );
                    302:        if ( an && !bn )
                    303:                return 1;
                    304:        else if ( !an && bn )
                    305:                return -1;
1.1       noro      306:        for ( an = BDY(a), bn = BDY(b); an; an = NEXT(an), bn = NEXT(bn) )
                    307:                if ( t = arf_comp(vl,BDY(an),BDY(bn)) )
                    308:                        return t;
                    309:        return 0;
                    310: }

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