Annotation of OpenXM_contrib2/asir2000/parse/arith.c, Revision 1.20
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.20 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/parse/arith.c,v 1.19 2005/02/08 18:06:05 saito 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.1 noro 96: };
97:
98: void arf_init() {
99: addfs = &arf[0]; addfs->name = "+"; addfs->fp = arf_add;
100: subfs = &arf[1]; subfs->name = "-"; subfs->fp = arf_sub;
101: mulfs = &arf[2]; mulfs->name = "*"; mulfs->fp = arf_mul;
102: divfs = &arf[3]; divfs->name = "/"; divfs->fp = arf_div;
103: remfs = &arf[4]; remfs->name = "%"; remfs->fp = arf_remain;
104: pwrfs = &arf[5]; pwrfs->name = "^"; pwrfs->fp = arf_pwr;
105: }
106:
107: void arf_add(vl,a,b,r)
108: VL vl;
109: Obj a,b,*r;
110: {
111: int mid;
112:
113: if ( !a )
114: *r = b;
115: else if ( !b )
116: *r = a;
117: else if ( OID(a) == OID(b) )
118: (*afunc[OID(a)].add)(vl,a,b,r);
119: else if ( (mid = MAX(OID(a),OID(b))) <= O_R )
1.13 ohara 120: (*afunc[mid].add)(vl,a,b,r);
121: else if ( (mid = MAX(OID(a),OID(b))) == O_DP && MIN(OID(a),OID(b)) <= O_R )
1.1 noro 122: (*afunc[mid].add)(vl,a,b,r);
123: else
124: notdef(vl,a,b,r);
125: }
126:
127: void arf_sub(vl,a,b,r)
128: VL vl;
129: Obj a,b,*r;
130: {
131: int mid;
132:
133: if ( !a )
134: if ( !b )
135: *r = 0;
136: else
137: (*afunc[OID(b)].chsgn)(b,r);
138: else if ( !b )
139: *r = a;
140: else if ( OID(a) == OID(b) )
141: (*afunc[OID(a)].sub)(vl,a,b,r);
142: else if ( (mid = MAX(OID(a),OID(b))) <= O_R )
1.15 ohara 143: (*afunc[mid].sub)(vl,a,b,r);
144: else if ( (mid = MAX(OID(a),OID(b))) == O_DP && MIN(OID(a),OID(b)) <= O_R )
1.1 noro 145: (*afunc[mid].sub)(vl,a,b,r);
146: else
147: notdef(vl,a,b,r);
148: }
149:
150: void arf_mul(vl,a,b,r)
151: VL vl;
152: Obj a,b,*r;
153: {
1.10 noro 154: int mid,aid,bid;
1.1 noro 155:
1.9 noro 156: if ( !a && !b )
1.1 noro 157: *r = 0;
1.9 noro 158: else if ( !a || !b ) {
1.18 saito 159: if ( !a ) a = b;
1.9 noro 160: /* compute a*0 */
161: if ( OID(a) == O_MAT || OID(a) == O_VECT )
162: (*(afunc[O_MAT].mul))(vl,a,0,r);
1.18 saito 163: else if ( OID(a) == O_IMAT )
164: (*(afunc[O_IMAT].mul))(vl,0,a,r);
1.9 noro 165: else
166: *r = 0;
1.10 noro 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 )
1.1 noro 170: (*afunc[mid].mul)(vl,a,b,r);
1.10 noro 171: else {
172: switch ( aid ) {
173: case O_N: case O_P:
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 )
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_P )
199: (*afunc[O_DP].mul)(vl,a,b,r);
1.14 noro 200: else if ( bid == O_MAT || bid == O_VECT || bid == O_DPV )
1.10 noro 201: (*afunc[bid].mul)(vl,a,b,r);
202: else
203: notdef(vl,a,b,r);
1.18 saito 204: break;
205: case O_IMAT:
206: (*afunc[O_IMAT].mul)(vl,a,b,r);
1.10 noro 207: break;
208: default:
209: notdef(vl,a,b,r);
210: break;
211: }
212: }
1.1 noro 213: }
214:
215: void arf_div(vl,a,b,r)
216: VL vl;
217: Obj a,b,*r;
218: {
219: int mid;
220:
221: if ( !b )
222: error("div : division by 0");
223: if ( !a )
224: *r = 0;
225: else if ( (OID(a) == OID(b)) )
226: (*(afunc[OID(a)].div))(vl,a,b,r);
227: else if ( (mid = MAX(OID(a),OID(b))) <= O_R ||
228: (mid == O_MAT) || (mid == O_VECT) || (mid == O_DP) )
229: (*afunc[mid].div)(vl,a,b,r);
230: else
231: notdef(vl,a,b,r);
232: }
233:
234: void arf_remain(vl,a,b,r)
235: VL vl;
236: Obj a,b,*r;
237: {
238: if ( !b )
239: error("rem : division by 0");
240: else if ( !a )
241: *r = 0;
242: else if ( MAX(OID(a),OID(b)) <= O_P )
243: cmp((Q)b,(P)a,(P *)r);
244: else
245: notdef(vl,a,b,r);
246: }
247:
1.17 noro 248: int allow_laurent;
249:
1.1 noro 250: void arf_pwr(vl,a,e,r)
251: VL vl;
252: Obj a,e,*r;
253: {
254: R t;
255:
1.16 noro 256: if ( !a ) {
1.8 noro 257: if ( !e )
258: *r = (pointer)ONE;
1.16 noro 259: else if ( RATN(e) && SGN((Q)e)>0 )
260: *r = 0;
1.8 noro 261: else
1.16 noro 262: mkpow(vl,a,e,r);
263: } else if ( OID(a) == O_QUOTE )
1.6 noro 264: (*(afunc[O_QUOTE].pwr))(vl,a,e,r);
1.9 noro 265: else if ( !e ) {
266: if ( OID(a) == O_MAT )
267: (*(afunc[O_MAT].pwr))(vl,a,e,r);
268: else
269: *r = (pointer)ONE;
270: } else if ( (OID(e) <= O_N) && INT(e) ) {
1.1 noro 271: if ( (OID(a) == O_P) && (SGN((Q)e) < 0) ) {
1.17 noro 272: if ( allow_laurent )
273: (*(afunc[O_P].pwr))(vl,a,e,r);
274: else {
275: MKRAT((P)a,(P)ONE,1,t);
276: (*(afunc[O_R].pwr))(vl,t,e,r);
277: }
1.1 noro 278: } else
279: (*(afunc[OID(a)].pwr))(vl,a,e,r);
1.17 noro 280: } else if ( (OID(e) <= O_N) && RATN(e) ) {
281: if ( (OID(a) == O_P) && allow_laurent )
282: (*(afunc[O_P].pwr))(vl,a,e,r);
283: else if ( OID(a) <= O_R )
284: mkpow(vl,a,e,r);
285: else
286: notdef(vl,a,e,r);
1.1 noro 287: } else if ( OID(a) <= O_R )
288: mkpow(vl,a,e,r);
289: else
290: notdef(vl,a,e,r);
291: }
292:
293: void arf_chsgn(a,r)
294: Obj a,*r;
295: {
296: if ( !a )
297: *r = 0;
298: else
299: (*(afunc[OID(a)].chsgn))(a,r);
300: }
301:
302: int arf_comp(vl,a,b)
303: VL vl;
304: Obj a,b;
305: {
306: if ( !a )
307: if ( !b )
308: return 0;
309: else
310: return (*afunc[OID(b)].comp)(vl,a,b);
311: else if ( !b )
312: return (*afunc[OID(a)].comp)(vl,a,b);
313: else if ( OID(a) != OID(b) )
314: return OID(a)>OID(b) ? 1 : -1;
315: else
316: return (*afunc[OID(a)].comp)(vl,a,b);
317: }
318:
319: int complist(vl,a,b)
320: VL vl;
321: LIST a,b;
322: {
1.7 noro 323: int t;
1.1 noro 324: NODE an,bn;
325:
326: if ( !a )
327: if ( !b )
328: return 0;
329: else
330: return -1;
331: else if ( !b )
332: return 1;
1.5 noro 333: for ( an = BDY(a), bn = BDY(b); an && bn; an = NEXT(an), bn = NEXT(bn) );
334: if ( an && !bn )
335: return 1;
336: else if ( !an && bn )
337: return -1;
1.1 noro 338: for ( an = BDY(a), bn = BDY(b); an; an = NEXT(an), bn = NEXT(bn) )
339: if ( t = arf_comp(vl,BDY(an),BDY(bn)) )
340: return t;
341: return 0;
342: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>