Annotation of OpenXM_contrib2/asir2018/parse/arith.c, Revision 1.1
1.1 ! 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
! 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
! 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: *
! 48: * $OpenXM$
! 49: */
! 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: int compqa();
! 68: int compquote();
! 69: int compvoid();
! 70:
! 71: struct oAFUNC afunc0[] = {
! 72: /* O_VOID=-1 */ {notdef,notdef,notdef,notdef,notdef,notdef,compvoid},
! 73: /* ???=0 */ {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
! 74: /* O_N=1 */ {addnum,subnum,mulnum,divnum,pwrnum,chsgnnum,compnum},
! 75: /* O_P=2 */ {addp,subp,mulp,divr,pwrp,chsgnp,compp},
! 76: /* O_R=3 */ {addr,subr,mulr,divr,pwrr,chsgnr,compr},
! 77: /* O_LIST=4 */ {notdef,notdef,notdef,notdef,notdef,notdef,complist},
! 78: /* O_VECT=5 */ {addvect,subvect,mulvect,divvect,notdef,chsgnvect,compvect},
! 79: /* O_MAT=6 */ {addmat,submat,mulmat,divmat,pwrmat,chsgnmat,compmat},
! 80: /* O_STR=7 */ {addstr,notdef,notdef,notdef,notdef,notdef,compstr},
! 81: /* O_COMP=8 */ {addcomp,subcomp,mulcomp,divcomp,pwrcomp,chsgncomp,compcomp},
! 82: /* O_DP=9 */ {addd,subd,muld,divdc,notdef,chsgnd,compd},
! 83: /* O_USINT=10 */ {notdef,notdef,notdef,notdef,notdef,notdef,compui},
! 84: /* O_ERR=11 */ {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
! 85: /* O_GF2MAT=12 */ {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
! 86: /* O_MATHCAP=13 */ {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
! 87: /* O_F=14 */ {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
! 88: /* O_GFMMAT=15 */ {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
! 89: /* O_BYTEARRAY=16 */ {notdef,notdef,notdef,notdef,notdef,notdef,compbytearray},
! 90: /* O_QUOTE=17 */ {addquote,subquote,mulquote,divquote,pwrquote,chsgnquote,compquote},
! 91: /* O_OPTLIST=18 */ {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
! 92: /* O_SYMBOL=19 */ {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
! 93: /* O_RANGE=20 */ {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
! 94: /* O_TB=21 */ {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
! 95: /* O_DPV=22 */ {adddv,subdv,muldv,notdef,notdef,chsgndv,compdv},
! 96: /* O_QUOTEARG=23 */ {notdef,notdef,notdef,notdef,notdef,notdef,compqa},
! 97: /* O_MAT=24 */ {AddMatI,SubMatI,MulMatG,notdef,notdef,ChsgnI,(int(*)())notdef},
! 98: /* O_NBP=25 */ {addnbp,subnbp,mulnbp,notdef,pwrnbp,chsgnnbp,compnbp},
! 99: /* O_DPM=26 */ {adddpm,subdpm,mulobjdpm,notdef,notdef,chsgndpm,compdpm},
! 100: };
! 101:
! 102: struct oAFUNC *afunc = &afunc0[1];
! 103:
! 104: void arf_init() {
! 105: addfs = &arf[0]; addfs->name = "+"; addfs->fp = arf_add;
! 106: subfs = &arf[1]; subfs->name = "-"; subfs->fp = arf_sub;
! 107: mulfs = &arf[2]; mulfs->name = "*"; mulfs->fp = arf_mul;
! 108: divfs = &arf[3]; divfs->name = "/"; divfs->fp = arf_div;
! 109: remfs = &arf[4]; remfs->name = "%"; remfs->fp = arf_remain;
! 110: pwrfs = &arf[5]; pwrfs->name = "^"; pwrfs->fp = arf_pwr;
! 111: }
! 112:
! 113: void arf_add(VL vl,Obj a,Obj b,Obj *r)
! 114: {
! 115: int mid;
! 116:
! 117: if ( !a )
! 118: *r = b;
! 119: else if ( !b )
! 120: *r = a;
! 121: else if ( OID(a) == OID(b) )
! 122: (*afunc[OID(a)].add)(vl,a,b,r);
! 123: else if ( (mid = MAX(OID(a),OID(b))) <= O_R || mid == O_QUOTE )
! 124: (*afunc[mid].add)(vl,a,b,r);
! 125: else if ( (mid = MAX(OID(a),OID(b))) == O_DP && MIN(OID(a),OID(b)) <= O_R )
! 126: (*afunc[mid].add)(vl,a,b,r);
! 127: else
! 128: notdef(vl,a,b,r);
! 129: }
! 130:
! 131: void arf_sub(VL vl,Obj a,Obj b,Obj *r)
! 132: {
! 133: int mid;
! 134:
! 135: if ( !a )
! 136: if ( !b )
! 137: *r = 0;
! 138: else
! 139: (*afunc[OID(b)].chsgn)(b,r);
! 140: else if ( !b )
! 141: *r = a;
! 142: else if ( OID(a) == OID(b) )
! 143: (*afunc[OID(a)].sub)(vl,a,b,r);
! 144: else if ( (mid = MAX(OID(a),OID(b))) <= O_R || mid == O_QUOTE )
! 145: (*afunc[mid].sub)(vl,a,b,r);
! 146: else if ( (mid = MAX(OID(a),OID(b))) == O_DP && MIN(OID(a),OID(b)) <= O_R )
! 147: (*afunc[mid].sub)(vl,a,b,r);
! 148: else
! 149: notdef(vl,a,b,r);
! 150: }
! 151:
! 152: void arf_mul(VL vl,Obj a,Obj b,Obj *r)
! 153: {
! 154: int mid,aid,bid;
! 155:
! 156: if ( !a && !b )
! 157: *r = 0;
! 158: else if ( !a || !b ) {
! 159: if ( !a ) a = b;
! 160: /* compute a*0 */
! 161: if ( OID(a) == O_MAT || OID(a) == O_VECT )
! 162: (*(afunc[O_MAT].mul))(vl,a,0,r);
! 163: else if ( OID(a) == O_IMAT )
! 164: (*(afunc[O_IMAT].mul))(vl,0,a,r);
! 165: else
! 166: *r = 0;
! 167: } else if ( (aid = OID(a)) == (bid = OID(b)) )
! 168: (*(afunc[aid].mul))(vl,a,b,r);
! 169: else if ( (mid = MAX(aid,bid)) <= O_R || mid == O_QUOTE )
! 170: (*afunc[mid].mul)(vl,a,b,r);
! 171: else {
! 172: switch ( aid ) {
! 173: case O_N: case O_P: case O_NBP:
! 174: (*afunc[mid].mul)(vl,a,b,r);
! 175: break;
! 176: case O_R:
! 177: /* rat * something; bid > O_R */
! 178: if ( bid == O_VECT || bid == O_MAT || bid == O_DP || bid == O_DPM )
! 179: (*afunc[mid].mul)(vl,a,b,r);
! 180: else
! 181: notdef(vl,a,b,r);
! 182: break;
! 183: case O_MAT:
! 184: if ( bid <= O_R || bid == O_VECT || bid == O_DP )
! 185: (*afunc[O_MAT].mul)(vl,a,b,r);
! 186: else
! 187: notdef(vl,a,b,r);
! 188: break;
! 189: case O_VECT:
! 190: if ( bid <= O_R || bid == O_DP )
! 191: (*afunc[O_VECT].mul)(vl,a,b,r);
! 192: else if ( bid == O_MAT )
! 193: (*afunc[O_MAT].mul)(vl,a,b,r);
! 194: else
! 195: notdef(vl,a,b,r);
! 196: break;
! 197: case O_DP:
! 198: if ( bid <= O_R )
! 199: (*afunc[O_DP].mul)(vl,a,b,r);
! 200: else if ( bid == O_MAT || bid == O_VECT || bid == O_DPV || bid == O_DPM )
! 201: (*afunc[bid].mul)(vl,a,b,r);
! 202: else
! 203: notdef(vl,a,b,r);
! 204: break;
! 205: case O_IMAT:
! 206: (*afunc[O_IMAT].mul)(vl,a,b,r);
! 207: break;
! 208: default:
! 209: notdef(vl,a,b,r);
! 210: break;
! 211: }
! 212: }
! 213: }
! 214:
! 215: void arf_div(VL vl,Obj a,Obj b,Obj *r)
! 216: {
! 217: int mid;
! 218:
! 219: if ( !b )
! 220: error("div : division by 0");
! 221: if ( !a )
! 222: *r = 0;
! 223: else if ( (OID(a) == OID(b)) )
! 224: (*(afunc[OID(a)].div))(vl,a,b,r);
! 225: else if ( (mid = MAX(OID(a),OID(b))) <= O_R ||
! 226: (mid == O_MAT) || (mid == O_VECT) || (mid == O_DP) || mid == O_QUOTE )
! 227: (*afunc[mid].div)(vl,a,b,r);
! 228: else
! 229: notdef(vl,a,b,r);
! 230: }
! 231:
! 232: void arf_remain(VL vl,Obj a,Obj b,Obj *r)
! 233: {
! 234: if ( !b )
! 235: error("rem : division by 0");
! 236: else if ( !a )
! 237: *r = 0;
! 238: else if ( MAX(OID(a),OID(b)) <= O_P && INT(b) )
! 239: cmp((Z)b,(P)a,(P *)r);
! 240: else
! 241: notdef(vl,a,b,r);
! 242: }
! 243:
! 244: int allow_laurent;
! 245:
! 246: void arf_pwr(VL vl,Obj a,Obj e,Obj *r)
! 247: {
! 248: R t;
! 249:
! 250: if ( !a ) {
! 251: if ( !e )
! 252: *r = (pointer)ONE;
! 253: else if ( RATN(e) && sgnq((Q)e)>0 )
! 254: *r = 0;
! 255: else
! 256: mkpow(vl,a,e,r);
! 257: } else if ( !e ) {
! 258: if ( OID(a) == O_MAT )
! 259: (*(afunc[O_MAT].pwr))(vl,a,e,r);
! 260: else
! 261: *r = (pointer)ONE;
! 262: } else if ( OID(a) == O_QUOTE || OID(e) == O_QUOTE )
! 263: (*(afunc[O_QUOTE].pwr))(vl,a,e,r);
! 264: else if ( (OID(e) <= O_N) && INT(e) ) {
! 265: if ( (OID(a) == O_P) && (sgnq((Q)e) < 0) ) {
! 266: if ( allow_laurent )
! 267: (*(afunc[O_P].pwr))(vl,a,e,r);
! 268: else {
! 269: MKRAT((P)a,(P)ONE,1,t);
! 270: (*(afunc[O_R].pwr))(vl,t,e,r);
! 271: }
! 272: } else
! 273: (*(afunc[OID(a)].pwr))(vl,a,e,r);
! 274: } else if ( (OID(e) <= O_N) && RATN(e) ) {
! 275: if ( (OID(a) == O_P) && allow_laurent )
! 276: (*(afunc[O_P].pwr))(vl,a,e,r);
! 277: else if ( OID(a) <= O_R )
! 278: mkpow(vl,a,e,r);
! 279: else
! 280: notdef(vl,a,e,r);
! 281: } else if ( OID(a) <= O_R )
! 282: mkpow(vl,a,e,r);
! 283: else
! 284: notdef(vl,a,e,r);
! 285: }
! 286:
! 287: void arf_chsgn(Obj a,Obj *r)
! 288: {
! 289: if ( !a )
! 290: *r = 0;
! 291: else
! 292: (*(afunc[OID(a)].chsgn))(a,r);
! 293: }
! 294:
! 295: int arf_comp(VL vl,Obj a,Obj b)
! 296: {
! 297: if ( !a )
! 298: if ( !b )
! 299: return 0;
! 300: else
! 301: return (*afunc[OID(b)].comp)(vl,a,b);
! 302: else if ( !b )
! 303: return (*afunc[OID(a)].comp)(vl,a,b);
! 304: else if ( OID(a) != OID(b) )
! 305: return OID(a)>OID(b) ? 1 : -1;
! 306: else
! 307: return (*afunc[OID(a)].comp)(vl,a,b);
! 308: }
! 309:
! 310: int complist(VL vl,LIST a,LIST b)
! 311: {
! 312: int t;
! 313: NODE an,bn;
! 314:
! 315: if ( !a )
! 316: if ( !b )
! 317: return 0;
! 318: else
! 319: return -1;
! 320: else if ( !b )
! 321: return 1;
! 322: for ( an = BDY(a), bn = BDY(b); an && bn; an = NEXT(an), bn = NEXT(bn) );
! 323: if ( an && !bn )
! 324: return 1;
! 325: else if ( !an && bn )
! 326: return -1;
! 327: for ( an = BDY(a), bn = BDY(b); an; an = NEXT(an), bn = NEXT(bn) )
! 328: if ( t = arf_comp(vl,BDY(an),BDY(bn)) )
! 329: return t;
! 330: return 0;
! 331: }
! 332:
! 333: int compvoid(VL vl,Obj a,Obj b)
! 334: {
! 335: if ( !a )
! 336: if ( !b ) return 0;
! 337: else return -1;
! 338: else if ( !b )
! 339: return 1;
! 340: else if ( OID(a) > OID(b) ) return 1;
! 341: else if ( OID(b) < OID(a) ) return -1;
! 342: else return 0;
! 343: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>