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