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

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.15    ! ohara      48:  * $OpenXM: OpenXM_contrib2/asir2000/parse/arith.c,v 1.14 2004/05/14 06:02:54 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.11      noro       87: /* O_OPTLIST=18 */     {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
                     88: /* O_SYMBOL=19 */      {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
1.12      noro       89: /* O_RANGE=20 */       {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
                     90: /* O_TB=21 */  {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
1.14      noro       91: /* O_DPV=22 */ {adddv,subdv,muldv,notdef,notdef,chsgndv,compdv},
1.1       noro       92: };
                     93:
                     94: void arf_init() {
                     95:        addfs = &arf[0]; addfs->name = "+"; addfs->fp = arf_add;
                     96:        subfs = &arf[1]; subfs->name = "-"; subfs->fp = arf_sub;
                     97:        mulfs = &arf[2]; mulfs->name = "*"; mulfs->fp = arf_mul;
                     98:        divfs = &arf[3]; divfs->name = "/"; divfs->fp = arf_div;
                     99:        remfs = &arf[4]; remfs->name = "%"; remfs->fp = arf_remain;
                    100:        pwrfs = &arf[5]; pwrfs->name = "^"; pwrfs->fp = arf_pwr;
                    101: }
                    102:
                    103: void arf_add(vl,a,b,r)
                    104: VL vl;
                    105: Obj a,b,*r;
                    106: {
                    107:        int mid;
                    108:
                    109:        if ( !a )
                    110:                *r = b;
                    111:        else if ( !b )
                    112:                *r = a;
                    113:        else if ( OID(a) == OID(b) )
                    114:                (*afunc[OID(a)].add)(vl,a,b,r);
                    115:        else if ( (mid = MAX(OID(a),OID(b))) <= O_R )
1.13      ohara     116:                (*afunc[mid].add)(vl,a,b,r);
                    117:        else if ( (mid = MAX(OID(a),OID(b))) == O_DP && MIN(OID(a),OID(b)) <= O_R )
1.1       noro      118:                (*afunc[mid].add)(vl,a,b,r);
                    119:        else
                    120:                notdef(vl,a,b,r);
                    121: }
                    122:
                    123: void arf_sub(vl,a,b,r)
                    124: VL vl;
                    125: Obj a,b,*r;
                    126: {
                    127:        int mid;
                    128:
                    129:        if ( !a )
                    130:                if ( !b )
                    131:                        *r = 0;
                    132:                else
                    133:                        (*afunc[OID(b)].chsgn)(b,r);
                    134:        else if ( !b )
                    135:                *r = a;
                    136:        else if ( OID(a) == OID(b) )
                    137:                (*afunc[OID(a)].sub)(vl,a,b,r);
                    138:        else if ( (mid = MAX(OID(a),OID(b))) <= O_R )
1.15    ! ohara     139:                (*afunc[mid].sub)(vl,a,b,r);
        !           140:        else if ( (mid = MAX(OID(a),OID(b))) == O_DP && MIN(OID(a),OID(b)) <= O_R )
1.1       noro      141:                (*afunc[mid].sub)(vl,a,b,r);
                    142:        else
                    143:                notdef(vl,a,b,r);
                    144: }
                    145:
                    146: void arf_mul(vl,a,b,r)
                    147: VL vl;
                    148: Obj a,b,*r;
                    149: {
1.10      noro      150:        int mid,aid,bid;
1.1       noro      151:
1.9       noro      152:        if ( !a && !b )
1.1       noro      153:                *r = 0;
1.9       noro      154:        else if ( !a || !b ) {
                    155:                if ( !a )
                    156:                        a = b;
                    157:                /* compute a*0 */
                    158:                if ( OID(a) == O_MAT || OID(a) == O_VECT )
                    159:                        (*(afunc[O_MAT].mul))(vl,a,0,r);
                    160:                else
                    161:                        *r = 0;
1.10      noro      162:        } else if ( (aid = OID(a)) == (bid = OID(b)) )
                    163:                (*(afunc[aid].mul))(vl,a,b,r);
                    164:        else if ( (mid = MAX(aid,bid)) <= O_R )
1.1       noro      165:                (*afunc[mid].mul)(vl,a,b,r);
1.10      noro      166:        else {
                    167:                switch ( aid ) {
                    168:                        case O_N: case O_P:
                    169:                                (*afunc[mid].mul)(vl,a,b,r);
                    170:                                break;
                    171:                        case O_R:
                    172:                                /* rat * something; bid > O_R */
                    173:                                if ( bid == O_VECT || bid == O_MAT )
                    174:                                        (*afunc[mid].mul)(vl,a,b,r);
                    175:                                else
                    176:                                        notdef(vl,a,b,r);
                    177:                                break;
                    178:                        case O_MAT:
                    179:                                if ( bid <= O_R || bid == O_VECT || bid == O_DP )
                    180:                                        (*afunc[O_MAT].mul)(vl,a,b,r);
                    181:                                else
                    182:                                        notdef(vl,a,b,r);
                    183:                                break;
                    184:                        case O_VECT:
                    185:                                if ( bid <= O_R || bid == O_DP )
                    186:                                        (*afunc[O_VECT].mul)(vl,a,b,r);
                    187:                                else if ( bid == O_MAT )
                    188:                                        (*afunc[O_MAT].mul)(vl,a,b,r);
                    189:                                else
                    190:                                        notdef(vl,a,b,r);
                    191:                                break;
                    192:                        case O_DP:
                    193:                                if ( bid <= O_P )
                    194:                                        (*afunc[O_DP].mul)(vl,a,b,r);
1.14      noro      195:                                else if ( bid == O_MAT || bid == O_VECT || bid == O_DPV )
1.10      noro      196:                                        (*afunc[bid].mul)(vl,a,b,r);
                    197:                                else
                    198:                                        notdef(vl,a,b,r);
                    199:                                break;
                    200:                        default:
                    201:                                        notdef(vl,a,b,r);
                    202:                                break;
                    203:                }
                    204:        }
1.1       noro      205: }
                    206:
                    207: void arf_div(vl,a,b,r)
                    208: VL vl;
                    209: Obj a,b,*r;
                    210: {
                    211:        int mid;
                    212:
                    213:        if ( !b )
                    214:                error("div : division by 0");
                    215:        if ( !a )
                    216:                *r = 0;
                    217:        else if ( (OID(a) == OID(b)) )
                    218:                (*(afunc[OID(a)].div))(vl,a,b,r);
                    219:        else if ( (mid = MAX(OID(a),OID(b))) <= O_R ||
                    220:                (mid == O_MAT) || (mid == O_VECT) || (mid == O_DP) )
                    221:                (*afunc[mid].div)(vl,a,b,r);
                    222:        else
                    223:                notdef(vl,a,b,r);
                    224: }
                    225:
                    226: void arf_remain(vl,a,b,r)
                    227: VL vl;
                    228: Obj a,b,*r;
                    229: {
                    230:        if ( !b )
                    231:                error("rem : division by 0");
                    232:        else if ( !a )
                    233:                *r = 0;
                    234:        else if ( MAX(OID(a),OID(b)) <= O_P )
                    235:                cmp((Q)b,(P)a,(P *)r);
                    236:        else
                    237:                notdef(vl,a,b,r);
                    238: }
                    239:
                    240: void arf_pwr(vl,a,e,r)
                    241: VL vl;
                    242: Obj a,e,*r;
                    243: {
                    244:        R t;
                    245:
                    246:        if ( !a )
1.8       noro      247:                if ( !e )
                    248:                        *r = (pointer)ONE;
                    249:                else
                    250:                        *r = 0;
1.6       noro      251:        else if ( OID(a) == O_QUOTE )
                    252:                (*(afunc[O_QUOTE].pwr))(vl,a,e,r);
1.9       noro      253:        else if ( !e ) {
                    254:                if ( OID(a) == O_MAT )
                    255:                        (*(afunc[O_MAT].pwr))(vl,a,e,r);
                    256:                else
                    257:                        *r = (pointer)ONE;
                    258:        } else if ( (OID(e) <= O_N) && INT(e) ) {
1.1       noro      259:                if ( (OID(a) == O_P) && (SGN((Q)e) < 0) ) {
                    260:                        MKRAT((P)a,(P)ONE,1,t);
                    261:                        (*(afunc[O_R].pwr))(vl,t,e,r);
                    262:                } else
                    263:                        (*(afunc[OID(a)].pwr))(vl,a,e,r);
                    264:        } else if ( OID(a) <= O_R )
                    265:                mkpow(vl,a,e,r);
                    266:        else
                    267:                notdef(vl,a,e,r);
                    268: }
                    269:
                    270: void arf_chsgn(a,r)
                    271: Obj a,*r;
                    272: {
                    273:        if ( !a )
                    274:                *r = 0;
                    275:        else
                    276:                (*(afunc[OID(a)].chsgn))(a,r);
                    277: }
                    278:
                    279: int arf_comp(vl,a,b)
                    280: VL vl;
                    281: Obj a,b;
                    282: {
                    283:        if ( !a )
                    284:                if ( !b )
                    285:                        return 0;
                    286:                else
                    287:                        return (*afunc[OID(b)].comp)(vl,a,b);
                    288:        else if ( !b )
                    289:                        return (*afunc[OID(a)].comp)(vl,a,b);
                    290:        else if ( OID(a) != OID(b) )
                    291:                return OID(a)>OID(b) ? 1 : -1;
                    292:        else
                    293:                return (*afunc[OID(a)].comp)(vl,a,b);
                    294: }
                    295:
                    296: int complist(vl,a,b)
                    297: VL vl;
                    298: LIST a,b;
                    299: {
1.7       noro      300:        int t;
1.1       noro      301:        NODE an,bn;
                    302:
                    303:        if ( !a )
                    304:                if ( !b )
                    305:                        return 0;
                    306:                else
                    307:                        return -1;
                    308:        else if ( !b )
                    309:                return 1;
1.5       noro      310:        for ( an = BDY(a), bn = BDY(b); an && bn; an = NEXT(an), bn = NEXT(bn) );
                    311:        if ( an && !bn )
                    312:                return 1;
                    313:        else if ( !an && bn )
                    314:                return -1;
1.1       noro      315:        for ( an = BDY(a), bn = BDY(b); an; an = NEXT(an), bn = NEXT(bn) )
                    316:                if ( t = arf_comp(vl,BDY(an),BDY(bn)) )
                    317:                        return t;
                    318:        return 0;
                    319: }

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