Annotation of OpenXM_contrib2/asir2000/builtin/strobj.c, Revision 1.86
1.6 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.7 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.6 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.86 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/strobj.c,v 1.85 2005/10/26 11:07:50 noro Exp $
1.6 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "parse.h"
52: #include "ctype.h"
1.10 ohara 53: #if defined(PARI)
1.1 noro 54: #include "genpari.h"
1.11 saito 55: # if !(PARI_VERSION_CODE > 131588)
1.1 noro 56: extern jmp_buf environnement;
1.11 saito 57: # endif
1.1 noro 58: #endif
1.5 noro 59: #include <string.h>
60:
1.18 noro 61: struct TeXSymbol {
62: char *text;
63: char *symbol;
64: };
65:
1.71 noro 66: #define OPNAME(f) (((ARF)FA0(f))->name[0])
1.77 noro 67: #define IS_ZERO(f) (((f)->id==I_FORMULA) && FA0(f)==0 )
1.75 noro 68: #define IS_BINARYPWR(f) (((f)->id==I_BOP) &&(OPNAME(f)=='^'))
69: #define IS_NARYADD(f) (((f)->id==I_NARYOP) &&(OPNAME(f)=='+'))
70: #define IS_NARYMUL(f) (((f)->id==I_NARYOP) &&(OPNAME(f)=='*'))
1.71 noro 71:
1.1 noro 72: extern char *parse_strp;
73:
1.50 ohara 74: void Psprintf();
1.1 noro 75: void Prtostr(), Pstrtov(), Peval_str();
1.3 noro 76: void Pstrtoascii(), Pasciitostr();
1.5 noro 77: void Pstr_len(), Pstr_chr(), Psub_str();
1.14 noro 78: void Pwrite_to_tb();
79: void Ptb_to_string();
80: void Pclear_tb();
81: void Pstring_to_tb();
82: void Pquotetotex_tb();
83: void Pquotetotex();
1.24 noro 84: void Pquotetotex_env();
1.47 noro 85: void Pflatten_quote();
1.69 noro 86:
87: void Pquote_is_integer(),Pquote_is_rational(),Pquote_is_number();
88: void Pquote_is_dependent(),Pquote_is_function();
1.70 noro 89: void Pquote_normalize();
1.80 noro 90: void Pquote_normalize_comp();
1.69 noro 91:
1.52 noro 92: void Pquote_to_funargs(),Pfunargs_to_quote(),Pget_function_name();
1.68 noro 93: void Pquote_unify(),Pget_quote_id(),Pquote_match_rewrite();
1.64 noro 94: void Pquote_to_nary(),Pquote_to_bin();
1.58 ohara 95: void do_assign(NODE arg);
1.14 noro 96: void fnodetotex_tb(FNODE f,TB tb);
97: char *symbol_name(char *name);
1.28 noro 98: char *conv_rule(char *name);
1.38 noro 99: char *conv_subs(char *name);
1.28 noro 100: char *call_convfunc(char *name);
1.14 noro 101: void tb_to_string(TB tb,STRING *rp);
102: void fnodenodetotex_tb(NODE n,TB tb);
103: void fargstotex_tb(char *opname,FNODE f,TB tb);
1.35 noro 104: int top_is_minus(FNODE f);
1.56 noro 105: int quote_unify(Obj f,Obj pat,NODE *rp);
1.1 noro 106:
107: struct ftab str_tab[] = {
1.50 ohara 108: {"sprintf",Psprintf,-99999999},
1.1 noro 109: {"rtostr",Prtostr,1},
110: {"strtov",Pstrtov,1},
111: {"eval_str",Peval_str,1},
1.3 noro 112: {"strtoascii",Pstrtoascii,1},
113: {"asciitostr",Pasciitostr,1},
1.5 noro 114: {"str_len",Pstr_len,1},
115: {"str_chr",Pstr_chr,3},
116: {"sub_str",Psub_str,3},
1.14 noro 117: {"write_to_tb",Pwrite_to_tb,2},
118: {"clear_tb",Pclear_tb,1},
119: {"tb_to_string",Ptb_to_string,1},
120: {"string_to_tb",Pstring_to_tb,1},
1.63 noro 121: {"get_quote_id",Pget_quote_id,1},
1.69 noro 122:
123: {"quote_is_number",Pquote_is_number,1},
124: {"quote_is_rational",Pquote_is_rational,1},
125: {"quote_is_integer",Pquote_is_integer,1},
126: {"quote_is_function",Pquote_is_function,1},
127: {"quote_is_dependent",Pquote_is_dependent,2},
128:
1.76 noro 129: {"quote_normalize",Pquote_normalize,-2},
1.86 ! noro 130: {"quote_normalize_comp",Pquote_normalize_comp,2},
1.70 noro 131:
1.64 noro 132: {"quote_to_nary",Pquote_to_nary,1},
133: {"quote_to_bin",Pquote_to_bin,2},
1.60 noro 134: {"quotetotex_tb",Pquotetotex_tb,2},
135: {"quotetotex",Pquotetotex,1},
136: {"quotetotex_env",Pquotetotex_env,-99999999},
1.65 noro 137: {"flatten_quote",Pflatten_quote,-2},
1.60 noro 138: {"quote_to_funargs",Pquote_to_funargs,1},
1.62 noro 139: {"quote_unify",Pquote_unify,2},
1.68 noro 140: {"quote_match_rewrite",Pquote_match_rewrite,-4},
1.48 noro 141: {"funargs_to_quote",Pfunargs_to_quote,1},
1.52 noro 142: {"get_function_name",Pget_function_name,1},
1.1 noro 143: {0,0,0},
144: };
1.13 noro 145:
1.14 noro 146: void write_tb(char *s,TB tb)
147: {
148: if ( tb->next == tb->size ) {
149: tb->size *= 2;
150: tb->body = (char **)REALLOC(tb->body,tb->size*sizeof(char *));
151: }
152: tb->body[tb->next] = s;
153: tb->next++;
154: }
1.13 noro 155:
1.18 noro 156: int register_symbol_table(Obj arg);
157: int register_conv_rule(Obj arg);
1.38 noro 158: int register_conv_func(Obj arg);
1.23 noro 159: int register_dp_vars(Obj arg);
1.46 noro 160: int register_dp_vars_origin(Obj arg);
161: int register_dp_dvars_origin(Obj arg);
162: int register_dp_dvars_prefix(Obj arg);
1.25 noro 163: int register_dp_vars_prefix(Obj arg);
1.31 noro 164: int register_dp_vars_hweyl(Obj arg);
1.27 noro 165: int register_show_lt(Obj arg);
1.40 noro 166: char *objtostr(Obj obj);
1.18 noro 167: static struct TeXSymbol *user_texsymbol;
1.23 noro 168: static char **dp_vars;
1.25 noro 169: static int dp_vars_len;
170: static char *dp_vars_prefix;
1.46 noro 171: static char *dp_dvars_prefix;
172: static int dp_vars_origin;
173: static int dp_dvars_origin;
1.27 noro 174: static int show_lt;
1.26 noro 175: static FUNC convfunc;
1.27 noro 176: static int is_lt;
1.28 noro 177: static int conv_flag;
1.31 noro 178: static int dp_vars_hweyl;
1.28 noro 179:
1.38 noro 180: #define CONV_TABLE (1U<<0)
181: #define CONV_SUBS (1U<<1)
182: #define CONV_DMODE (1U<<2)
1.18 noro 183:
184: static struct {
185: char *name;
186: Obj value;
187: int (*reg)();
188: } qtot_env[] = {
189: {"symbol_table",0,register_symbol_table},
190: {"conv_rule",0,register_conv_rule},
1.38 noro 191: {"conv_func",0,register_conv_func},
1.23 noro 192: {"dp_vars",0,register_dp_vars},
1.25 noro 193: {"dp_vars_prefix",0,register_dp_vars_prefix},
1.46 noro 194: {"dp_dvars_prefix",0,register_dp_dvars_prefix},
195: {"dp_vars_origin",0,register_dp_vars_origin},
196: {"dp_dvars_origin",0,register_dp_dvars_origin},
1.31 noro 197: {"dp_vars_hweyl",0,register_dp_vars_hweyl},
1.27 noro 198: {"show_lt",0,register_show_lt},
1.18 noro 199: {0,0,0},
200: };
201:
1.20 noro 202: #define PARTIAL "\\partial"
1.28 noro 203:
204: char *conv_rule(char *name)
205: {
206: char *body,*r;
1.41 noro 207: int len;
1.28 noro 208:
1.38 noro 209: if ( convfunc )
210: name = call_convfunc(name);
1.41 noro 211: if ( conv_flag & CONV_TABLE ) {
212: r = symbol_name(name);
213: if ( r ) return r;
214: }
215: if ( (conv_flag & CONV_DMODE) && *name == 'd' ) {
216: body = conv_rule(name+1);
217: r = MALLOC_ATOMIC((strlen(PARTIAL)+strlen(body)+5)*sizeof(char));
218: if ( !body || !(len=strlen(body)) )
219: strcpy(r,PARTIAL);
220: else if ( len == 1 )
221: sprintf(r,"%s_%s",PARTIAL,body);
222: else
223: sprintf(r,"%s_{%s}",PARTIAL,body);
224: return r;
225: } else
226: return conv_subs(name);
1.28 noro 227: }
228:
1.40 noro 229: int _is_delimiter(char c)
230: {
231: if ( (c == ' ' || c == '_' || c == ',') ) return 1;
232: else return 0;
233: }
234:
235: int _is_alpha(char c)
236: {
237: if ( isdigit(c) || c == '{' || _is_delimiter(c) ) return 0;
238: else return 1;
239: }
240:
1.38 noro 241: char *conv_subs(char *name)
1.19 noro 242: {
1.29 noro 243: int i,j,k,len,clen,slen,start,level;
1.41 noro 244: char *buf,*head,*r,*h,*brace,*buf_conv;
1.28 noro 245: char **subs;
246:
1.41 noro 247: if ( !name || !(len=strlen(name)) ) return "";
248: if ( !(conv_flag&CONV_SUBS) ) return name;
1.28 noro 249: subs = (char **)ALLOCA(len*sizeof(char* ));
1.32 noro 250: for ( i = 0, j = 0, start = i; ; j++ ) {
1.40 noro 251: while ( (i < len) && _is_delimiter(name[i]) ) i++;
1.32 noro 252: start = i;
1.28 noro 253: if ( i == len ) break;
1.29 noro 254: if ( name[i] == '{' ) {
255: for ( level = 1, i++; i < len && level; i++ ) {
256: if ( name[i] == '{' ) level++;
257: else if ( name[i] == '}' ) level--;
258: }
1.32 noro 259: slen = i-start;
1.37 noro 260: if ( slen >= 3 ) {
261: brace = (char *)ALLOCA((slen+1)*sizeof(char));
262: strncpy(brace,name+start+1,slen-2);
263: brace[slen-2] = 0;
1.38 noro 264: buf = conv_subs(brace);
1.37 noro 265: subs[j] = (char *)ALLOCA((strlen(buf)+3)*sizeof(char));
1.38 noro 266: if ( strlen(buf) == 1 )
267: strcpy(subs[j],buf);
268: else
269: sprintf(subs[j],"{%s}",buf);
1.37 noro 270: } else
271: subs[j] = "{}";
1.32 noro 272: } else {
273: if ( isdigit(name[i]) )
274: while ( i < len && isdigit(name[i]) ) i++;
275: else
1.40 noro 276: while ( i < len && _is_alpha(name[i]) ) i++;
1.32 noro 277: slen = i-start;
278: buf = (char *)ALLOCA((slen+1)*sizeof(char));
279: strncpy(buf,name+start,slen); buf[slen] = 0;
1.41 noro 280: buf_conv = symbol_name(buf);
281: subs[j] = buf_conv?buf_conv:buf;
1.32 noro 282: }
1.28 noro 283: }
1.32 noro 284: for ( k = 0, clen = 0; k < j; k++ ) clen += strlen(subs[k]);
285: /* {subs(0)}_{{subs(1)},...,{subs(j-1)}} => {}:j+1 _:1 ,:j-2 */
286: h = r = MALLOC_ATOMIC((clen+(j+1)*2+1+(j-2)+1)*sizeof(char));
287: if ( j == 1 )
288: sprintf(h,"{%s}",subs[0]);
1.28 noro 289: else {
1.38 noro 290: sprintf(h,"{%s}_{%s",subs[0],subs[1]);
1.28 noro 291: h += strlen(h);
1.32 noro 292: for ( k = 2; k < j; k++ ) {
1.38 noro 293: sprintf(h,",%s",subs[k]);
1.28 noro 294: h += strlen(h);
1.19 noro 295: }
1.28 noro 296: strcpy(h,"}");
1.20 noro 297: }
298: return r;
1.19 noro 299: }
300:
1.26 noro 301: char *call_convfunc(char *name)
302: {
303: STRING str,r;
304: NODE arg;
305:
306: MKSTR(str,name);
307: arg = mknode(1,str);
308: r = (STRING)bevalf(convfunc,arg);
309: if ( !r || OID(r) != O_STR )
310: error("call_convfunc : invalid result");
311: return BDY(r);
312: }
313:
1.18 noro 314: int register_symbol_table(Obj arg)
315: {
316: NODE n,t;
317: Obj b;
318: STRING a0,a1;
319: struct TeXSymbol *uts;
320: int i,len;
321:
322: /* check */
323: if ( !arg ) {
324: user_texsymbol = 0;
325: return 1;
326: }
327: if ( OID(arg) != O_LIST ) return 0;
328:
329: n = BDY((LIST)arg);
330: len = length(n);
331: uts = (struct TeXSymbol *)MALLOC((len+1)*sizeof(struct TeXSymbol));
332: for ( i = 0; n; n = NEXT(n), i++ ) {
333: b = (Obj)BDY(n);
334: if ( !b || OID(b) != O_LIST ) return 0;
335: t = BDY((LIST)b);
336: if ( !t || !NEXT(t) ) return 0;
337: a0 = (STRING)BDY(t);
338: a1 = (STRING)BDY(NEXT(t));
1.23 noro 339: if ( !a0 ) return 0;
340: if ( OID(a0) == O_STR )
341: uts[i].text = BDY(a0);
342: else if ( OID(a0) == O_P )
343: uts[i].text = NAME(VR((P)a0));
344: else
345: return 0;
346: if ( !a1 ) return 0;
347: if ( OID(a1) == O_STR )
348: uts[i].symbol = BDY(a1);
349: else if ( OID(a1) == O_P )
350: uts[i].symbol = NAME(VR((P)a1));
351: else
352: return 0;
1.18 noro 353: }
354: uts[i].text = 0;
355: uts[i].symbol = 0;
356: user_texsymbol = uts;
357: return 1;
358: }
359:
1.46 noro 360: int register_dp_vars_origin(Obj arg)
361: {
362: if ( INT(arg) ) {
363: dp_vars_origin = QTOS((Q)arg);
364: return 1;
365: } else return 0;
366: }
367:
368: int register_dp_dvars_origin(Obj arg)
369: {
370: if ( INT(arg) ) {
371: dp_dvars_origin = QTOS((Q)arg);
372: return 1;
373: } else return 0;
374: }
375:
1.31 noro 376: int register_dp_vars_hweyl(Obj arg)
377: {
378: if ( INT(arg) ) {
379: dp_vars_hweyl = QTOS((Q)arg);
380: return 1;
381: } else return 0;
382: }
383:
1.27 noro 384: int register_show_lt(Obj arg)
385: {
386: if ( INT(arg) ) {
387: show_lt = QTOS((Q)arg);
388: return 1;
389: } else return 0;
390: }
1.26 noro 391:
1.18 noro 392: int register_conv_rule(Obj arg)
393: {
1.19 noro 394: if ( INT(arg) ) {
1.28 noro 395: conv_flag = QTOS((Q)arg);
396: convfunc = 0;
397: return 1;
1.38 noro 398: } else return 0;
399: }
400:
401: int register_conv_func(Obj arg)
402: {
1.43 noro 403: if ( !arg ) {
404: convfunc = 0;
405: return 1;
406: } else if ( OID(arg) == O_P && (int)(VR((P)arg))->attr == V_SR ) {
1.26 noro 407: convfunc = (FUNC)(VR((P)arg)->priv);
408: /* f must be a function which takes single argument */
409: return 1;
1.19 noro 410: } else return 0;
1.18 noro 411: }
412:
1.23 noro 413: int register_dp_vars(Obj arg)
414: {
415: int l,i;
416: char **r;
417: NODE n;
418: STRING a;
419:
420: if ( !arg ) {
421: dp_vars = 0;
422: dp_vars_len = 0;
1.25 noro 423: return 1;
1.23 noro 424: } else if ( OID(arg) != O_LIST )
425: return 0;
426: else {
427: n = BDY((LIST)arg);
428: l = length(n);
429: r = (char **)MALLOC_ATOMIC(l*sizeof(char *));
430: for ( i = 0; i < l; i++, n = NEXT(n) ) {
431: a = (STRING)BDY(n);
432: if ( !a ) return 0;
433: if ( OID(a) == O_STR )
434: r[i] = BDY(a);
435: else if ( OID(a) == O_P )
436: r[i] = NAME(VR((P)a));
437: else
438: return 0;
439: }
440: dp_vars = r;
441: dp_vars_len = l;
442: return 1;
443: }
444: }
445:
1.25 noro 446: int register_dp_vars_prefix(Obj arg)
447: {
448: if ( !arg ) {
449: dp_vars_prefix = 0;
450: return 1;
451: } else if ( OID(arg) == O_STR ) {
452: dp_vars_prefix = BDY((STRING)arg);
453: return 1;
454: } else if ( OID(arg) == O_P ) {
455: dp_vars_prefix = NAME(VR((P)arg));
456: return 1;
457: } else return 0;
458: }
459:
1.46 noro 460: int register_dp_dvars_prefix(Obj arg)
461: {
462: if ( !arg ) {
463: dp_dvars_prefix = 0;
464: return 1;
465: } else if ( OID(arg) == O_STR ) {
466: dp_dvars_prefix = BDY((STRING)arg);
467: return 1;
468: } else if ( OID(arg) == O_P ) {
469: dp_dvars_prefix = NAME(VR((P)arg));
470: return 1;
471: } else return 0;
472: }
473:
1.24 noro 474: void Pquotetotex_env(NODE arg,Obj *rp)
1.18 noro 475: {
476: int ac,i;
477: char *name;
478: NODE n,n0;
479: STRING s;
480: LIST l;
481:
482: ac = argc(arg);
483: if ( !ac ) {
484: n0 = 0;
485: for ( i = 0; qtot_env[i].name; i++ ) {
486: NEXTNODE(n0,n); MKSTR(s,qtot_env[i].name); BDY(n) = (pointer)s;
487: NEXTNODE(n0,n); BDY(n) = (Q)qtot_env[i].value;
488: }
489: NEXT(n) = 0;
490: MKLIST(l,n0);
491: *rp = (Obj)l;
1.34 noro 492: } else if ( ac == 1 && !ARG0(arg) ) {
493: /* set to default */
1.43 noro 494: for ( i = 0; qtot_env[i].name; i++ ) {
495: (qtot_env[i].reg)(0);
1.34 noro 496: qtot_env[i].value = 0;
1.43 noro 497: }
1.34 noro 498: *rp = 0;
1.18 noro 499: } else if ( ac == 1 || ac == 2 ) {
1.24 noro 500: asir_assert(ARG0(arg),O_STR,"quotetotex_env");
1.18 noro 501: name = BDY((STRING)ARG0(arg));
502: for ( i = 0; qtot_env[i].name; i++ )
503: if ( !strcmp(qtot_env[i].name,name) ) {
504: if ( ac == 2 ) {
505: if ( (qtot_env[i].reg)((Obj)ARG1(arg)) )
506: qtot_env[i].value = (Obj)ARG1(arg);
507: else
1.24 noro 508: error("quotetotex_env : invalid argument");
1.18 noro 509: }
510: *rp = qtot_env[i].value;
511: return;
512: }
513: *rp = 0;
514: } else
515: *rp = 0;
516: }
517:
1.14 noro 518: void Pwrite_to_tb(NODE arg,Q *rp)
1.13 noro 519: {
520: int i;
1.16 noro 521: Obj obj;
522: TB tb;
1.13 noro 523:
1.14 noro 524: asir_assert(ARG1(arg),O_TB,"write_to_tb");
1.16 noro 525: obj = ARG0(arg);
526: if ( !obj )
527: write_tb("",ARG1(arg));
528: else if ( OID(obj) == O_STR )
529: write_tb(BDY((STRING)obj),ARG1(arg));
530: else if ( OID(obj) == O_TB ) {
531: tb = (TB)obj;
532: for ( i = 0; i < tb->next; i++ )
533: write_tb(tb->body[i],ARG1(arg));
534: }
1.14 noro 535: *rp = 0;
1.53 noro 536: }
537:
1.70 noro 538: FNODE partial_eval(FNODE), fnode_to_nary(FNODE), fnode_to_bin(FNODE,int);
1.63 noro 539:
1.64 noro 540: void Pquote_to_nary(NODE arg,QUOTE *rp)
1.63 noro 541: {
542: FNODE f;
543:
1.70 noro 544: f = fnode_to_nary(BDY((QUOTE)ARG0(arg)));
1.63 noro 545: MKQUOTE(*rp,f);
546: }
547:
1.64 noro 548: void Pquote_to_bin(NODE arg,QUOTE *rp)
1.63 noro 549: {
550: FNODE f;
551: int direction;
552:
553: direction = QTOS((Q)ARG1(arg));
1.70 noro 554: f = fnode_to_bin(BDY((QUOTE)ARG0(arg)),direction);
1.63 noro 555:
556: MKQUOTE(*rp,f);
557: }
1.61 noro 558:
1.69 noro 559: void Pquote_is_number(NODE arg,Q *rp)
560: {
561: QUOTE q;
562: int ret;
563:
564: q = (QUOTE)ARG0(arg);
565: asir_assert(q,O_QUOTE,"quote_is_number");
566: ret = fnode_is_number(BDY(q));
567: STOQ(ret,*rp);
568: }
569:
570: void Pquote_is_rational(NODE arg,Q *rp)
571: {
572: QUOTE q;
573: int ret;
574:
575: q = (QUOTE)ARG0(arg);
576: asir_assert(q,O_QUOTE,"quote_is_rational");
577: ret = fnode_is_rational(BDY(q));
578: STOQ(ret,*rp);
579: }
580:
581: void Pquote_is_integer(NODE arg,Q *rp)
582: {
583: QUOTE q;
584: int ret;
585:
586: q = (QUOTE)ARG0(arg);
587: asir_assert(q,O_QUOTE,"quote_is_integer");
588: ret = fnode_is_integer(BDY(q));
589: STOQ(ret,*rp);
590: }
591:
592: void Pquote_is_function(NODE arg,Q *rp)
593: {
594: QUOTE q;
595: int ret;
596:
597: q = (QUOTE)ARG0(arg);
598: asir_assert(q,O_QUOTE,"quote_is_function");
599: if ( q->id == I_FUNC || q->id == I_IFUNC )
600: ret = 1;
601: else
602: ret = 0;
603: STOQ(ret,*rp);
604: }
605:
606: void Pquote_is_dependent(NODE arg,Q *rp)
607: {
608: P x;
609: QUOTE q,v;
610: int ret;
611: V var;
612:
613: q = (QUOTE)ARG0(arg);
614: v = (QUOTE)ARG1(arg);
615: asir_assert(q,O_QUOTE,"quote_is_dependent");
616: asir_assert(v,O_QUOTE,"quote_is_dependent");
617: x = (P)eval(BDY(v));
618: if ( !x || OID(x) != O_P )
619: *rp = 0;
620: var = VR(x);
621: ret = fnode_is_dependent(BDY(q),var);
622: STOQ(ret,*rp);
623: }
624:
625:
1.57 noro 626: void Pquote_unify(NODE arg,Q *rp)
1.53 noro 627: {
1.61 noro 628: FNODE f,g;
1.65 noro 629: Obj obj;
1.61 noro 630: QUOTE q;
1.53 noro 631: NODE r;
1.56 noro 632: int ret;
1.53 noro 633:
1.62 noro 634: #if 0
1.61 noro 635: g = partial_eval(BDY(((QUOTE)ARG0(arg))));
636: MKQUOTE(q,g);
637: ret = quote_unify((Obj)q,(Obj)ARG1(arg),&r);
1.62 noro 638: #else
1.65 noro 639: obj = (Obj)ARG0(arg);
640: ret = quote_unify(obj,(Obj)ARG1(arg),&r);
1.62 noro 641: #endif
1.57 noro 642: if ( ret ) {
643: do_assign(r);
644: *rp = ONE;
645: } else
1.56 noro 646: *rp = 0;
1.68 noro 647: }
648:
649: FNODE rewrite_fnode(FNODE,NODE);
650:
651: extern Obj VOIDobj;
652:
653: void Pquote_match_rewrite(NODE arg,Obj *rp)
654: {
655: FNODE f,g,h,c,value;
656: Obj obj;
657: QUOTE q;
658: NODE r,t,s,s0,pair;
659: int ret,ind,ac;
660:
661: obj = (Obj)ARG0(arg);
662: ret = quote_unify(obj,(Obj)ARG1(arg),&r);
663: if ( ret ) {
664: for ( t = r, s0 = 0; t; t = NEXT(t) ) {
665: NEXTNODE(s0,s);
666: pair = BDY((LIST)BDY(t));
667: ind = (int)FA0((FNODE)BDY((QUOTE)BDY(pair)));
1.79 noro 668: value = BDY((QUOTE)(BDY(NEXT(pair))));
1.68 noro 669: BDY(s) = mknode(2,ind,value);
670: }
671: if ( s0 ) NEXT(s) = 0;
672: switch ( ac = argc(arg) ) {
673: case 3:
674: h = rewrite_fnode(BDY((QUOTE)ARG2(arg)),s0);
1.79 noro 675: MKQUOTE(q,h); *rp = (Obj)q;
1.68 noro 676: break;
677: case 4:
678: c = rewrite_fnode(BDY((QUOTE)ARG2(arg)),s0);
679: if ( eval(c) ) {
680: h = rewrite_fnode(BDY((QUOTE)ARG3(arg)),s0);
1.79 noro 681: MKQUOTE(q,h); *rp = (Obj)q;
1.68 noro 682: } else
683: *rp = VOIDobj;
684: break;
685: default:
686: error("quote_match_rewrite : invalid argument");
687: }
688: } else
689: *rp = VOIDobj;
1.56 noro 690: }
691:
692: void do_assign(NODE arg)
693: {
694: NODE t,pair;
695: int pv;
696:
697: QUOTE value;
698:
699: for ( t = arg; t; t = NEXT(t) ) {
700: pair = BDY((LIST)BDY(t));
701: pv = (int)FA0((FNODE)BDY((QUOTE)BDY(pair)));
702: value = (QUOTE)(BDY(NEXT(pair)));
703: ASSPV(pv,value);
704: }
1.53 noro 705: }
706:
707: /*
1.56 noro 708: /* consistency check and merge
709: */
1.53 noro 710:
1.56 noro 711: int merge_matching_node(NODE n,NODE a,NODE *rp)
1.53 noro 712: {
713: NODE ta,ba,tn,bn;
714: QUOTE pa,va,pn,vn;
715:
1.56 noro 716: if ( !n ) {
717: *rp = a;
718: return 1;
719: }
1.53 noro 720: for ( ta = a; ta; ta = NEXT(ta) ) {
721: ba = BDY((LIST)BDY(ta));
1.55 noro 722: if ( !ba ) continue;
1.53 noro 723: pa = (QUOTE)BDY(ba); va = (QUOTE)BDY(NEXT(ba));
724: for ( tn = n; tn; tn = NEXT(tn) ) {
725: bn = BDY((LIST)BDY(tn));
1.55 noro 726: if ( !bn ) continue;
1.53 noro 727: pn = (QUOTE)BDY(bn); vn = (QUOTE)BDY(NEXT(bn));
1.55 noro 728: if ( !compquote(CO,pa,pn) ) {
729: if ( !compquote(CO,va,vn) ) break;
730: else return 0;
731: }
1.53 noro 732: }
733: if ( !tn ) {
734: MKNODE(tn,(pointer)BDY(ta),n);
735: n = tn;
736: }
737: }
1.56 noro 738: *rp = n;
739: return 1;
1.53 noro 740: }
741:
1.56 noro 742: int quote_unify_node(NODE f,NODE pat,NODE *rp) {
743: NODE r,a,tf,tp,r1;
744: int ret;
1.53 noro 745:
746: if ( length(f) != length(pat) ) return 0;
747: r = 0;
748: for ( tf = f, tp = pat; tf; tf = NEXT(tf), tp = NEXT(tp) ) {
1.56 noro 749: ret = quote_unify((Obj)BDY(tf),(Obj)BDY(tp),&a);
750: if ( !ret ) return 0;
751: ret = merge_matching_node(r,a,&r1);
752: if ( !ret ) return 0;
753: else r = r1;
1.53 noro 754: }
1.56 noro 755: *rp = r;
756: return 1;
1.53 noro 757: }
758:
1.66 noro 759: /* f = [a,b,c,...] pat = [X,Y,...] rpat matches the rest of f */
760:
761: int quote_unify_cons(NODE f,NODE pat,Obj rpat,NODE *rp) {
762: QUOTE q;
763: Q id;
764: FNODE fn;
765: NODE r,a,tf,tp,r1,arg;
766: int ret;
767: LIST list,alist;
768:
769: /* matching of the head part */
770: if ( length(f) < length(pat) ) return 0;
771: r = 0;
772: for ( tf = f, tp = pat; tp; tf = NEXT(tf), tp = NEXT(tp) ) {
773: ret = quote_unify((Obj)BDY(tf),(Obj)BDY(tp),&a);
774: if ( !ret ) return 0;
775: ret = merge_matching_node(r,a,&r1);
776: if ( !ret ) return 0;
777: else r = r1;
778: }
779: /* matching of the rest */
780: MKLIST(list,tf);
781: STOQ(I_LIST,id); a = mknode(2,id,list);
782: MKLIST(alist,a);
783: arg = mknode(1,alist);
784: Pfunargs_to_quote(arg,&q);
785: ret = quote_unify((Obj)q,rpat,&a);
786: if ( !ret ) return 0;
787: ret = merge_matching_node(r,a,&r1);
788: if ( !ret ) return 0;
789: *rp = r1;
790: return 1;
791: }
792:
1.53 noro 793: void get_quote_id_arg(QUOTE f,int *id,NODE *r)
794: {
795: LIST fa;
796: NODE arg,fab;
797:
798: arg = mknode(1,f); Pquote_to_funargs(arg,&fa); fab = BDY((LIST)fa);
799: *id = QTOS((Q)BDY(fab)); *r = NEXT(fab);
800: }
801:
1.56 noro 802: /* *rp : [[quote(A),quote(1)],...] */
1.53 noro 803:
1.56 noro 804: int quote_unify(Obj f, Obj pat, NODE *rp)
1.53 noro 805: {
806: NODE tf,tp,head,body;
807: NODE parg,farg,r;
1.66 noro 808: Obj rpat;
1.53 noro 809: LIST fa,l;
810: int pid,id;
1.55 noro 811: FUNC ff,pf;
1.56 noro 812: int ret;
1.64 noro 813: QUOTE q;
814: FNODE g;
1.53 noro 815:
1.67 noro 816: if ( !f )
817: if ( !pat ) {
818: *rp = 0; return 1;
819: } else
820: return 0;
821: else if ( OID(pat) == O_LIST ) {
1.53 noro 822: if ( OID(f) == O_LIST )
1.56 noro 823: return quote_unify_node(BDY((LIST)f),BDY((LIST)pat),rp);
1.53 noro 824: else
825: return 0;
826: } else if ( OID(pat) == O_QUOTE ) {
1.67 noro 827: pid = ((FNODE)BDY((QUOTE)pat))->id;
1.53 noro 828: switch ( pid ) {
1.55 noro 829: case I_FORMULA:
830: if ( compquote(CO,f,pat) )
831: return 0;
832: else {
1.67 noro 833: *rp = 0; return 1;
1.55 noro 834: }
835: break;
1.67 noro 836:
837: case I_LIST: case I_CONS:
838: get_quote_id_arg((QUOTE)pat,&pid,&parg);
839: if ( OID(f) == O_LIST )
840: tf = BDY((LIST)f);
841: else if ( OID(f) == O_QUOTE
842: && ((FNODE)BDY((QUOTE)f))->id == pid ) {
843: get_quote_id_arg((QUOTE)f,&id,&farg);
844: tf = BDY((LIST)BDY(farg));
845: } else
846: return 0;
847:
1.66 noro 848: tp = BDY((LIST)BDY(parg));
1.67 noro 849: if ( pid == I_LIST )
850: return quote_unify_node(tf,tp,rp);
851: else {
852: rpat = (Obj)BDY(NEXT(parg));
853: return quote_unify_cons(tf,tp,rpat,rp);
854: }
855:
1.53 noro 856: case I_PVAR:
857: /* [[pat,f]] */
858: r = mknode(2,pat,f); MKLIST(l,r);
1.56 noro 859: *rp = mknode(1,l);
860: return 1;
1.67 noro 861:
1.53 noro 862: case I_IFUNC:
863: /* F(X,Y,...) = ... */
1.67 noro 864: get_quote_id_arg((QUOTE)f,&id,&farg);
865: get_quote_id_arg((QUOTE)pat,&pid,&parg);
1.53 noro 866: if ( id == I_FUNC ) {
1.54 noro 867: r = mknode(2,BDY(parg),BDY(farg)); MKLIST(l,r);
868: head = mknode(1,l);
1.56 noro 869: ret = quote_unify(BDY(NEXT(farg)),
870: BDY(NEXT(parg)),&body);
871: if ( !ret ) return 0;
872: else return merge_matching_node(head,body,rp);
1.53 noro 873: } else
874: return 0;
1.64 noro 875:
1.67 noro 876: case I_NARYOP: case I_BOP: case I_FUNC:
1.64 noro 877: /* X+Y = ... */
1.67 noro 878: /* f(...) = ... */
879: if ( OID(f) != O_QUOTE ) return 0;
880: id = ((FNODE)BDY((QUOTE)f))->id;
881: if ( pid == I_FUNC )
882: ;
883: else {
884: /* XXX converting to I_BOP */
885: if ( pid == I_NARYOP ) {
1.70 noro 886: g = fnode_to_bin(BDY((QUOTE)pat),1);
1.67 noro 887: MKQUOTE(q,g); pat = (Obj)q;
888: }
889: if ( id == I_NARYOP ) {
1.70 noro 890: g = fnode_to_bin(BDY((QUOTE)f),1);
1.67 noro 891: MKQUOTE(q,g); f = (Obj)q;
892: }
893: }
894: get_quote_id_arg((QUOTE)pat,&pid,&parg);
895: get_quote_id_arg((QUOTE)f,&id,&farg);
1.64 noro 896: if ( compqa(CO,BDY(farg),BDY(parg)) ) return 0;
897: return quote_unify_node(NEXT(farg),NEXT(parg),rp);
898:
1.53 noro 899: default:
1.67 noro 900: if ( OID(f) != O_QUOTE ) return 0;
901: id = ((FNODE)BDY((QUOTE)f))->id;
902: if ( id != pid ) return 0;
903: get_quote_id_arg((QUOTE)pat,&pid,&parg);
904: get_quote_id_arg((QUOTE)f,&id,&farg);
905: return quote_unify_node(farg,parg,rp);
1.53 noro 906: }
907: }
1.13 noro 908: }
909:
1.14 noro 910: void Pquotetotex(NODE arg,STRING *rp)
1.13 noro 911: {
1.14 noro 912: TB tb;
1.13 noro 913:
1.14 noro 914: NEWTB(tb);
1.27 noro 915: /* XXX for DP */
916: is_lt = 1;
1.14 noro 917: fnodetotex_tb(BDY((QUOTE)ARG0(arg)),tb);
918: tb_to_string(tb,rp);
1.13 noro 919: }
920:
1.14 noro 921: void Pquotetotex_tb(NODE arg,Q *rp)
1.13 noro 922: {
923: int i;
1.14 noro 924: TB tb;
1.13 noro 925:
1.14 noro 926: asir_assert(ARG1(arg),O_TB,"quotetotex_tb");
1.27 noro 927: /* XXX for DP */
928: is_lt = 1;
1.14 noro 929: fnodetotex_tb(BDY((QUOTE)ARG0(arg)),ARG1(arg));
1.13 noro 930: *rp = 0;
931: }
932:
1.14 noro 933: void Pstring_to_tb(NODE arg,TB *rp)
934: {
935: TB tb;
936:
937: asir_assert(ARG0(arg),O_STR,"string_to_tb");
938: NEWTB(tb);
939: tb->body[0] = BDY((STRING)ARG0(arg));
940: tb->next++;
941: *rp = tb;
942: }
943:
944: void Ptb_to_string(NODE arg,STRING *rp)
945: {
946: TB tb;
947:
948: asir_assert(ARG0(arg),O_TB,"tb_to_string");
949: tb = (TB)ARG0(arg);
950: tb_to_string(tb,rp);
951: }
952:
953: void tb_to_string(TB tb,STRING *rp)
1.13 noro 954: {
1.14 noro 955: int j,len;
1.13 noro 956: char *all,*p,*q;
957:
1.14 noro 958: for ( j = 0, len = 0; j < tb->next; j++ )
959: len += strlen(tb->body[j]);
960: all = (char *)MALLOC_ATOMIC((len+1)*sizeof(char));
961: for ( j = 0, p = all; j < tb->next; j++ )
962: for ( q = tb->body[j]; *q; *p++ = *q++ );
963: *p = 0;
964: MKSTR(*rp,all);
965: }
966:
967: void Pclear_tb(NODE arg,Q *rp)
968: {
969: TB tb;
970: int j;
971:
972: asir_assert(ARG0(arg),O_TB,"clear_tb");
973: tb = (TB)ARG0(arg);
974: for ( j = 0; j < tb->next; j++ )
975: tb->body[j] = 0;
976: tb->next = 0;
977: *rp = 0;
1.13 noro 978: }
1.5 noro 979:
980: void Pstr_len(arg,rp)
981: NODE arg;
982: Q *rp;
983: {
1.16 noro 984: Obj obj;
985: TB tb;
986: int r,i;
1.5 noro 987:
1.16 noro 988: obj = (Obj)ARG0(arg);
989: if ( !obj || (OID(obj) != O_STR && OID(obj) != O_TB) )
990: error("str_len : invalid argument");
991: if ( OID(obj) == O_STR)
992: r = strlen(BDY((STRING)obj));
993: else if ( OID(obj) == O_TB ) {
994: tb = (TB)obj;
995: for ( r = i = 0; i < tb->next; i++ )
996: r += strlen(tb->body[i]);
997: }
1.5 noro 998: STOQ(r,*rp);
999: }
1000:
1001: void Pstr_chr(arg,rp)
1002: NODE arg;
1003: Q *rp;
1004: {
1005: STRING str,terminator;
1006: Q start;
1007: char *p,*ind;
1008: int chr,spos,r;
1009:
1010: str = (STRING)ARG0(arg);
1011: start = (Q)ARG1(arg);
1012: terminator = (STRING)ARG2(arg);
1013: asir_assert(str,O_STR,"str_chr");
1014: asir_assert(start,O_N,"str_chr");
1015: asir_assert(terminator,O_STR,"str_chr");
1016: p = BDY(str);
1017: spos = QTOS(start);
1018: chr = BDY(terminator)[0];
1.8 noro 1019: if ( spos > (int)strlen(p) )
1.5 noro 1020: r = -1;
1021: else {
1022: ind = strchr(p+spos,chr);
1023: if ( ind )
1024: r = ind-p;
1025: else
1026: r = -1;
1027: }
1028: STOQ(r,*rp);
1029: }
1030:
1031: void Psub_str(arg,rp)
1032: NODE arg;
1033: STRING *rp;
1034: {
1035: STRING str;
1036: Q head,tail;
1037: char *p,*r;
1038: int spos,epos,len;
1039:
1040: str = (STRING)ARG0(arg);
1041: head = (Q)ARG1(arg);
1042: tail = (Q)ARG2(arg);
1043: asir_assert(str,O_STR,"sub_str");
1044: asir_assert(head,O_N,"sub_str");
1045: asir_assert(tail,O_N,"sub_str");
1046: p = BDY(str);
1047: spos = QTOS(head);
1048: epos = QTOS(tail);
1049: len = strlen(p);
1050: if ( (spos >= len) || (epos < spos) ) {
1051: *rp = 0; return;
1052: }
1053: if ( epos >= len )
1054: epos = len-1;
1055: len = epos-spos+1;
1056: r = (char *)MALLOC(len+1);
1057: strncpy(r,p+spos,len);
1058: r[len] = 0;
1059: MKSTR(*rp,r);
1060: }
1.3 noro 1061:
1062: void Pstrtoascii(arg,rp)
1063: NODE arg;
1064: LIST *rp;
1065: {
1066: STRING str;
1067: unsigned char *p;
1068: int len,i;
1069: NODE n,n1;
1070: Q q;
1071:
1072: str = (STRING)ARG0(arg);
1073: asir_assert(str,O_STR,"strtoascii");
1074: p = BDY(str);
1075: len = strlen(p);
1076: for ( i = len-1, n = 0; i >= 0; i-- ) {
1077: UTOQ((unsigned int)p[i],q);
1078: MKNODE(n1,q,n);
1079: n = n1;
1080: }
1081: MKLIST(*rp,n);
1082: }
1083:
1084: void Pasciitostr(arg,rp)
1085: NODE arg;
1086: STRING *rp;
1087: {
1088: LIST list;
1089: unsigned char *p;
1090: int len,i,j;
1091: NODE n;
1092: Q q;
1093:
1094: list = (LIST)ARG0(arg);
1095: asir_assert(list,O_LIST,"asciitostr");
1096: n = BDY(list);
1097: len = length(n);
1098: p = MALLOC_ATOMIC(len+1);
1099: for ( i = 0; i < len; i++, n = NEXT(n) ) {
1100: q = (Q)BDY(n);
1101: asir_assert(q,O_N,"asciitostr");
1102: j = QTOS(q);
1.4 noro 1103: if ( j >= 256 || j <= 0 )
1.3 noro 1104: error("asciitostr : argument out of range");
1105: p[i] = j;
1106: }
1107: p[i] = 0;
1108: MKSTR(*rp,(char *)p);
1109: }
1.1 noro 1110:
1111: void Peval_str(arg,rp)
1112: NODE arg;
1113: Obj *rp;
1114: {
1115: FNODE fnode;
1116: char *cmd;
1.10 ohara 1117: #if defined(PARI)
1.8 noro 1118: void recover(int);
1119:
1.1 noro 1120: recover(0);
1.11 saito 1121: # if !(PARI_VERSION_CODE > 131588)
1.1 noro 1122: if ( setjmp(environnement) ) {
1123: avma = top; recover(1);
1124: resetenv("");
1125: }
1.11 saito 1126: # endif
1.1 noro 1127: #endif
1128: cmd = BDY((STRING)ARG0(arg));
1.9 noro 1129: exprparse_create_var(0,cmd,&fnode);
1.1 noro 1130: *rp = eval(fnode);
1131: }
1132:
1133: void Prtostr(arg,rp)
1134: NODE arg;
1135: STRING *rp;
1136: {
1137: char *b;
1138: int len;
1139:
1.2 noro 1140: len = estimate_length(CO,ARG0(arg));
1.12 noro 1141: b = (char *)MALLOC_ATOMIC(len+1);
1.1 noro 1142: soutput_init(b);
1143: sprintexpr(CO,ARG0(arg));
1144: MKSTR(*rp,b);
1145: }
1146:
1147: void Pstrtov(arg,rp)
1148: NODE arg;
1149: P *rp;
1150: {
1.8 noro 1151: char *p;
1.1 noro 1152:
1153: p = BDY((STRING)ARG0(arg));
1154: #if 0
1155: if ( !islower(*p) )
1156: *rp = 0;
1157: else {
1158: for ( t = p+1; t && (isalnum(*t) || *t == '_'); t++ );
1159: if ( *t )
1160: *rp = 0;
1161: else
1162: makevar(p,rp);
1163: }
1164: #else
1165: makevar(p,rp);
1166: #endif
1.14 noro 1167: }
1168:
1.15 noro 1169: static struct TeXSymbol texsymbol[] = {
1170: {"sin","\\sin"},
1171: {"cos","\\cos"},
1172: {"tan","\\tan"},
1173: {"sinh","\\sinh"},
1174: {"cosh","\\cosh"},
1175: {"tanh","\\tanh"},
1176: {"exp","\\exp"},
1177: {"log","\\log"},
1178:
1179: /* Greek Letters (lower case) */
1180: {"alpha","\\alpha"},
1181: {"beta","\\beta"},
1182: {"gamma","\\gamma"},
1183: {"delta","\\delta"},
1184: {"epsilon","\\epsilon"},
1185: {"varepsilon","\\varepsilon"},
1186: {"zeta","\\zeta"},
1187: {"eta","\\eta"},
1188: {"theta","\\theta"},
1189: {"vartheta","\\vartheta"},
1190: {"iota","\\iota"},
1191: {"kappa","\\kappa"},
1192: {"lambda","\\lambda"},
1193: {"mu","\\mu"},
1194: {"nu","\\nu"},
1195: {"xi","\\xi"},
1196: {"pi","\\pi"},
1197: {"varpi","\\varpi"},
1198: {"rho","\\rho"},
1199: {"sigma","\\sigma"},
1200: {"varsigma","\\varsigma"},
1201: {"tau","\\tau"},
1202: {"upsilon","\\upsilon"},
1203: {"phi","\\phi"},
1204: {"varphi","\\varphi"},
1205: {"chi","\\chi"},
1206: {"omega","\\omega"},
1207:
1208: /* Greek Letters, (upper case) */
1209: {"ggamma","\\Gamma"},
1210: {"ddelta","\\Delta"},
1211: {"ttheta","\\Theta"},
1212: {"llambda","\\Lambda"},
1213: {"xxi","\\Xi"},
1214: {"ppi","\\Pi"},
1215: {"ssigma","\\Sigma"},
1216: {"uupsilon","\\Upsilon"},
1217: {"pphi","\\Phi"},
1218: {"ppsi","\\Psi"},
1219: {"oomega","\\Omega"},
1220:
1221: /* Our own mathematical functions */
1222: {"algebra_tensor","\\otimes"},
1223: {"base_where","{\\rm \\ where \\ }"},
1224: /* Mathematical constants */
1225: {"c_pi","\\pi"},
1226: {"c_i","\\sqrt{-1}"},
1227:
1228: /* Temporary */
1229: {0,0}
1230: };
1231:
1.14 noro 1232: char *symbol_name(char *name)
1233: {
1.15 noro 1234: int i;
1235:
1.41 noro 1236: if ( !name || strlen(name) == 0 )
1237: return "";
1.38 noro 1238: if ( !(conv_flag & CONV_TABLE) )
1239: return name;
1240:
1.18 noro 1241: if ( user_texsymbol )
1242: for ( i = 0; user_texsymbol[i].text; i++ )
1243: if ( !strcmp(user_texsymbol[i].text,name) )
1244: return user_texsymbol[i].symbol;
1.15 noro 1245: for ( i = 0; texsymbol[i].text; i++ )
1246: if ( !strcmp(texsymbol[i].text,name) )
1247: return texsymbol[i].symbol;
1.41 noro 1248: return 0;
1.52 noro 1249: }
1250:
1251: void Pget_function_name(NODE arg,STRING *rp)
1252: {
1253: QUOTEARG qa;
1254: ARF f;
1255: char *opname;
1256:
1257: qa = (QUOTEARG)BDY(arg);
1258: if ( !qa || OID(qa) != O_QUOTEARG || qa->type != A_arf )
1259: *rp = 0;
1260: else {
1261: f = (ARF)BDY(qa);
1262: opname = f->name;
1263: MKSTR(*rp,opname);
1264: }
1.14 noro 1265: }
1266:
1.51 noro 1267: FNODE strip_paren(FNODE);
1268:
1.14 noro 1269: void fnodetotex_tb(FNODE f,TB tb)
1270: {
1271: NODE n,t,t0;
1.38 noro 1272: char vname[BUFSIZ],prefix[BUFSIZ];
1273: char *opname,*vname_conv,*prefix_conv;
1.14 noro 1274: Obj obj;
1.46 noro 1275: int i,len,allzero,elen,elen2,si;
1.40 noro 1276: C cplx;
1277: char *r;
1.17 noro 1278: FNODE fi,f2;
1.14 noro 1279:
1280: write_tb(" ",tb);
1281: if ( !f ) {
1282: write_tb("0",tb);
1283: return;
1284: }
1285: switch ( f->id ) {
1286: /* unary operators */
1.23 noro 1287: case I_NOT:
1288: write_tb("\\neg (",tb);
1289: fnodetotex_tb((FNODE)FA0(f),tb);
1290: write_tb(")",tb);
1291: break;
1292: case I_PAREN:
1293: write_tb("(",tb);
1294: fnodetotex_tb((FNODE)FA0(f),tb);
1295: write_tb(")",tb);
1296: break;
1297: case I_MINUS:
1298: write_tb("-",tb);
1299: fnodetotex_tb((FNODE)FA0(f),tb);
1300: break;
1301:
1302: /* binary operators */
1303: /* arg list */
1304: /* I_AND, I_OR => FA0(f), FA1(f) */
1305: /* otherwise => FA1(f), FA2(f) */
1306: case I_BOP:
1307: opname = ((ARF)FA0(f))->name;
1.70 noro 1308: switch ( opname[0] ) {
1309: case '+':
1310: fnodetotex_tb((FNODE)FA1(f),tb);
1311: if ( !top_is_minus((FNODE)FA2(f)) ) write_tb(opname,tb);
1312: fnodetotex_tb((FNODE)FA2(f),tb);
1313: break;
1314: case '-':
1315: if ( FA1(f) ) fnodetotex_tb((FNODE)FA1(f),tb);
1316: write_tb(opname,tb);
1317: fnodetotex_tb((FNODE)FA2(f),tb);
1318: break;
1319: case '*':
1320: fnodetotex_tb((FNODE)FA1(f),tb);
1321: write_tb(" ",tb);
1322: /* XXX special care for DP */
1323: f2 = (FNODE)FA2(f);
1324: if ( f2->id == I_EV ) {
1325: n = (NODE)FA0(f2);
1326: for ( i = 0; n; n = NEXT(n), i++ ) {
1327: fi = (FNODE)BDY(n);
1328: if ( fi->id != I_FORMULA || FA0(fi) )
1329: break;
1330: }
1331: if ( n )
1332: fnodetotex_tb((FNODE)FA2(f),tb);
1333: } else
1.23 noro 1334: fnodetotex_tb((FNODE)FA2(f),tb);
1.70 noro 1335: break;
1336: case '/':
1337: write_tb("\\frac{",tb);
1338: fnodetotex_tb((FNODE)FA1(f),tb);
1339: write_tb("} {",tb);
1340: fnodetotex_tb((FNODE)FA2(f),tb);
1341: write_tb("}",tb);
1342: break;
1343: case '^':
1344: fnodetotex_tb((FNODE)FA1(f),tb);
1345: write_tb("^{",tb);
1346: fnodetotex_tb(strip_paren((FNODE)FA2(f)),tb);
1347: write_tb("} ",tb);
1348: break;
1349: case '%':
1350: fnodetotex_tb((FNODE)FA1(f),tb);
1351: write_tb(" {\\rm mod}\\, ",tb);
1.23 noro 1352: fnodetotex_tb((FNODE)FA2(f),tb);
1.70 noro 1353: break;
1354: default:
1355: error("invalid binary operator");
1356: break;
1357: }
1.23 noro 1358: break;
1359:
1360: case I_COP:
1361: switch( (cid)FA0(f) ) {
1362: case C_EQ:
1363: fnodetotex_tb((FNODE)FA1(f),tb);
1364: write_tb(" = ",tb);
1365: fnodetotex_tb((FNODE)FA2(f),tb);
1366: break;
1367: case C_NE:
1368: fnodetotex_tb((FNODE)FA1(f),tb);
1369: write_tb(" \\neq ",tb);
1370: fnodetotex_tb((FNODE)FA2(f),tb);
1371: break;
1372: case C_GT:
1373: fnodetotex_tb((FNODE)FA1(f),tb);
1.45 noro 1374: write_tb(" > ",tb);
1.23 noro 1375: fnodetotex_tb((FNODE)FA2(f),tb);
1376: break;
1377: case C_LT:
1378: fnodetotex_tb((FNODE)FA1(f),tb);
1.45 noro 1379: write_tb(" < ",tb);
1.23 noro 1380: fnodetotex_tb((FNODE)FA2(f),tb);
1.14 noro 1381: break;
1.23 noro 1382: case C_GE:
1383: fnodetotex_tb((FNODE)FA1(f),tb);
1384: write_tb(" \\geq ",tb);
1385: fnodetotex_tb((FNODE)FA2(f),tb);
1.14 noro 1386: break;
1.23 noro 1387: case C_LE:
1388: fnodetotex_tb((FNODE)FA1(f),tb);
1389: write_tb(" \\leq ",tb);
1390: fnodetotex_tb((FNODE)FA2(f),tb);
1.14 noro 1391: break;
1392: }
1393: break;
1394:
1.23 noro 1395: case I_LOP:
1396: switch( (lid)FA0(f) ) {
1397: case L_EQ:
1398: fnodetotex_tb((FNODE)FA1(f),tb);
1399: write_tb(" = ",tb);
1400: fnodetotex_tb((FNODE)FA2(f),tb);
1401: break;
1402: case L_NE:
1403: fnodetotex_tb((FNODE)FA1(f),tb);
1404: write_tb(" \\neq ",tb);
1405: fnodetotex_tb((FNODE)FA2(f),tb);
1406: break;
1407: case L_GT:
1408: fnodetotex_tb((FNODE)FA1(f),tb);
1.45 noro 1409: write_tb(" > ",tb);
1.23 noro 1410: fnodetotex_tb((FNODE)FA2(f),tb);
1411: break;
1412: case L_LT:
1413: fnodetotex_tb((FNODE)FA1(f),tb);
1.45 noro 1414: write_tb(" < ",tb);
1.23 noro 1415: fnodetotex_tb((FNODE)FA2(f),tb);
1416: break;
1417: case L_GE:
1418: fnodetotex_tb((FNODE)FA1(f),tb);
1419: write_tb(" \\geq ",tb);
1420: fnodetotex_tb((FNODE)FA2(f),tb);
1.14 noro 1421: break;
1.23 noro 1422: case L_LE:
1423: fnodetotex_tb((FNODE)FA1(f),tb);
1424: write_tb(" \\leq ",tb);
1425: fnodetotex_tb((FNODE)FA2(f),tb);
1.14 noro 1426: break;
1.23 noro 1427: case L_AND:
1428: fnodetotex_tb((FNODE)FA1(f),tb);
1.14 noro 1429: write_tb(" {\\rm \\ and\\ } ",tb);
1.23 noro 1430: fnodetotex_tb((FNODE)FA2(f),tb);
1431: break;
1432: case L_OR:
1.14 noro 1433: fnodetotex_tb((FNODE)FA1(f),tb);
1.23 noro 1434: write_tb(" {\\rm \\ or\\ } ",tb);
1435: fnodetotex_tb((FNODE)FA2(f),tb);
1.14 noro 1436: break;
1.23 noro 1437: case L_NOT:
1438: /* XXX : L_NOT is a unary operator */
1439: write_tb("\\neg (",tb);
1.14 noro 1440: fnodetotex_tb((FNODE)FA1(f),tb);
1.23 noro 1441: write_tb(")",tb);
1442: return;
1.14 noro 1443: }
1444: break;
1445:
1.23 noro 1446: case I_AND:
1447: fnodetotex_tb((FNODE)FA0(f),tb);
1448: write_tb(" {\\rm \\ and\\ } ",tb);
1449: fnodetotex_tb((FNODE)FA1(f),tb);
1450: break;
1451:
1452: case I_OR:
1453: fnodetotex_tb((FNODE)FA0(f),tb);
1454: write_tb(" {\\rm \\ or\\ } ",tb);
1455: fnodetotex_tb((FNODE)FA1(f),tb);
1456: break;
1457:
1.14 noro 1458: /* ternary operators */
1459: case I_CE:
1460: error("fnodetotex_tb : not implemented yet");
1461: break;
1462:
1463: /* lists */
1464: case I_LIST:
1465: write_tb(" [ ",tb);
1466: n = (NODE)FA0(f);
1467: fnodenodetotex_tb(n,tb);
1468: write_tb("]",tb);
1469: break;
1470:
1471: /* function */
1.23 noro 1472: case I_FUNC:
1.40 noro 1473: if ( !strcmp(((FUNC)FA0(f))->name,"@pi") )
1474: write_tb("\\pi",tb);
1475: else if ( !strcmp(((FUNC)FA0(f))->name,"@e") )
1476: write_tb("e",tb);
1477: else {
1478: opname = conv_rule(((FUNC)FA0(f))->name);
1479: write_tb(opname,tb);
1480: write_tb("(",tb);
1481: fargstotex_tb(opname,FA1(f),tb);
1482: write_tb(")",tb);
1483: }
1.23 noro 1484: break;
1485:
1486: /* XXX */
1487: case I_CAR:
1.28 noro 1488: opname = conv_rule("car");
1.23 noro 1489: write_tb(opname,tb);
1490: write_tb("(",tb);
1491: fargstotex_tb(opname,FA0(f),tb);
1492: write_tb(")",tb);
1493: break;
1494:
1495: case I_CDR:
1.28 noro 1496: opname = conv_rule("cdr");
1.23 noro 1497: write_tb(opname,tb);
1498: write_tb("(",tb);
1499: fargstotex_tb(opname,FA0(f),tb);
1500: write_tb(")",tb);
1501: break;
1502:
1503: /* exponent vector */
1504: case I_EV:
1505: n = (NODE)FA0(f);
1.31 noro 1506: if ( dp_vars_hweyl ) {
1507: elen = length(n);
1508: elen2 = elen>>1;
1509: elen = elen2<<1;
1510: }
1.23 noro 1511: allzero = 1;
1.27 noro 1512: if ( show_lt && is_lt )
1513: write_tb("\\underline{",tb);
1.23 noro 1514: for ( t0 = 0, i = 0; n; n = NEXT(n), i++ ) {
1515: fi = (FNODE)BDY(n);
1516: if ( fi->id == I_FORMULA && !FA0(fi) ) continue;
1517: allzero = 0;
1.38 noro 1518: if ( dp_vars && i < dp_vars_len ) {
1519: strcpy(vname,dp_vars[i]);
1520: vname_conv = conv_rule(vname);
1521: } else {
1522: if ( dp_vars_hweyl ) {
1.44 noro 1523: if ( i < elen2 ) {
1.38 noro 1524: strcpy(prefix,dp_vars_prefix?dp_vars_prefix:"x");
1.44 noro 1525: prefix_conv = conv_rule(prefix);
1526: vname_conv = (char *)ALLOCA(strlen(prefix_conv)+50);
1.46 noro 1527: si = i+dp_vars_origin;
1528: sprintf(vname_conv,(si>=0&&si<10)?"%s_%d":"%s_{%d}",
1529: prefix_conv,si);
1.44 noro 1530: } else if ( i < elen ) {
1.46 noro 1531: strcpy(prefix,
1532: dp_dvars_prefix?dp_dvars_prefix:"\\partial");
1.44 noro 1533: prefix_conv = conv_rule(prefix);
1534: vname_conv = (char *)ALLOCA(strlen(prefix_conv)+50);
1.46 noro 1535: si = i+dp_dvars_origin-elen2;
1536: sprintf(vname_conv,(si>=0&&si<10)?"%s_%d":"%s_{%d}",
1537: prefix_conv,si);
1.44 noro 1538: } else {
1.38 noro 1539: strcpy(prefix,"h");
1.44 noro 1540: vname_conv = conv_rule(prefix);
1541: }
1542: } else {
1.38 noro 1543: strcpy(prefix,dp_vars_prefix?dp_vars_prefix:"x");
1.44 noro 1544: prefix_conv = conv_rule(prefix);
1545: vname_conv = (char *)ALLOCA(strlen(prefix_conv)+50);
1.46 noro 1546: si = i+dp_vars_origin;
1547: sprintf(vname_conv,(si>=0&&si<10)?"%s_%d":"%s_{%d}",
1548: prefix_conv,si);
1.44 noro 1549: }
1.38 noro 1550: }
1.23 noro 1551: if ( fi->id == I_FORMULA && UNIQ(FA0(fi)) ) {
1552: len = strlen(vname_conv);
1553: opname = MALLOC_ATOMIC(len+2);
1554: sprintf(opname,"%s ",vname_conv);
1.14 noro 1555: write_tb(opname,tb);
1.23 noro 1556: } else {
1557: len = strlen(vname_conv);
1558: /* 2: ^{ */
1559: opname = MALLOC_ATOMIC(len+1+2);
1560: sprintf(opname,"%s^{",vname_conv);
1.14 noro 1561: write_tb(opname,tb);
1.23 noro 1562: fnodetotex_tb((FNODE)BDY(n),tb);
1563: write_tb("} ",tb);
1564: }
1.14 noro 1565: }
1.23 noro 1566: /* XXX */
1567: if ( allzero )
1568: write_tb(" 1 ",tb);
1.27 noro 1569: if ( show_lt && is_lt ) {
1570: write_tb("}",tb);
1571: is_lt = 0;
1572: }
1.14 noro 1573: break;
1574:
1.23 noro 1575: /* string */
1.14 noro 1576: case I_STR:
1577: write_tb((char *)FA0(f),tb);
1578: break;
1579:
1.23 noro 1580: /* internal object */
1.14 noro 1581: case I_FORMULA:
1582: obj = (Obj)FA0(f);
1.40 noro 1583: if ( !obj )
1.42 noro 1584: write_tb("0",tb);
1.40 noro 1585: else if ( OID(obj) == O_N && NID(obj) == N_C ) {
1586: cplx = (C)obj;
1587: write_tb("(",tb);
1588: if ( cplx->r ) {
1589: r = objtostr((Obj)cplx->r); write_tb(r,tb);
1590: }
1591: if ( cplx->i ) {
1592: if ( cplx->r && compnum(0,cplx->i,0) > 0 ) {
1593: write_tb("+",tb);
1594: if ( !UNIQ(cplx->i) ) {
1595: r = objtostr((Obj)cplx->i); write_tb(r,tb);
1596: }
1597: } else if ( MUNIQ(cplx->i) )
1598: write_tb("-",tb);
1599: else if ( !UNIQ(cplx->i) ) {
1600: r = objtostr((Obj)cplx->i); write_tb(r,tb);
1601: }
1602: write_tb("\\sqrt{-1}",tb);
1603: }
1604: write_tb(")",tb);
1605: } else if ( OID(obj) == O_P )
1606: write_tb(conv_rule(VR((P)obj)->name),tb);
1607: else
1608: write_tb(objtostr(obj),tb);
1.14 noro 1609: break;
1610:
1.23 noro 1611: /* program variable */
1.14 noro 1612: case I_PVAR:
1613: if ( FA1(f) )
1614: error("fnodetotex_tb : not implemented yet");
1615: GETPVNAME(FA0(f),opname);
1616: write_tb(opname,tb);
1617: break;
1618:
1619: default:
1620: error("fnodetotex_tb : not implemented yet");
1621: }
1.40 noro 1622: }
1623:
1624: char *objtostr(Obj obj)
1625: {
1626: int len;
1627: char *r;
1628:
1629: len = estimate_length(CO,obj);
1630: r = (char *)MALLOC_ATOMIC(len+1);
1631: soutput_init(r);
1632: sprintexpr(CO,obj);
1633: return r;
1.50 ohara 1634: }
1635:
1636: void Psprintf(NODE arg,STRING *rp)
1637: {
1638: STRING string;
1639: char *s,*t,*r;
1640: int argc,n,len;
1641: NODE node;
1642:
1643: string = (STRING)ARG0(arg);
1644: asir_assert(string,O_STR,"sprintf");
1645: s = BDY(string);
1646: for(n = 0, t = s; *t; t++) {
1647: if (*t=='%' && *(t+1)=='a') {
1648: n++;
1649: }
1650: }
1651: for(node = NEXT(arg), argc = 0, len = strlen(s); node; node = NEXT(node), argc++) {
1652: len += estimate_length(CO,BDY(node));
1653: }
1654: if (argc < n) {
1655: error("sprintf: invalid argument");
1656: }
1657: r = (char *)MALLOC_ATOMIC(len);
1658: for(node = NEXT(arg), t = r; *s; s++) {
1659: if (*s=='%' && *(s+1)=='a') {
1660: strcpy(t,objtostr(BDY(node)));
1661: node = NEXT(node);
1662: t = strchr(t,0);
1663: s++;
1664: }else {
1665: *t++ = *s;
1666: }
1667: }
1668: *t = 0;
1669: MKSTR(*rp,r);
1.14 noro 1670: }
1671:
1672: void fnodenodetotex_tb(NODE n,TB tb)
1673: {
1674: for ( ; n; n = NEXT(n) ) {
1.27 noro 1675: is_lt = 1;
1.14 noro 1676: fnodetotex_tb((FNODE)BDY(n),tb);
1677: if ( NEXT(n) ) write_tb(", ",tb);
1678: }
1679: }
1680:
1681: void fargstotex_tb(char *name,FNODE f,TB tb)
1682: {
1683: NODE n;
1684:
1685: if ( !strcmp(name,"matrix") ) {
1686: error("fargstotex_tb : not implemented yet");
1687: } else if ( !strcmp(name,"vector") ) {
1688: error("fargstotex_tb : not implemented yet");
1689: } else {
1690: if ( f->id == I_LIST ) {
1691: n = (NODE)FA0(f);
1692: fnodenodetotex_tb(n,tb);
1693: } else
1694: fnodetotex_tb(f,tb);
1.35 noro 1695: }
1696: }
1697:
1698: int top_is_minus(FNODE f)
1699: {
1700: char *opname;
1701: int len;
1702: Obj obj;
1703:
1704: if ( !f )
1705: return 0;
1706: switch ( f->id ) {
1707: case I_MINUS:
1708: return 1;
1709: case I_BOP:
1710: opname = ((ARF)FA0(f))->name;
1711: switch ( opname[0] ) {
1712: case '+': case '*': case '/': case '^': case '%':
1713: return top_is_minus((FNODE)FA1(f));
1714: case '-':
1715: if ( FA1(f) )
1716: return top_is_minus((FNODE)FA1(f));
1717: else
1718: return 1;
1719: default:
1720: return 0;
1721: }
1722: break;
1723: case I_COP:
1724: return top_is_minus((FNODE)FA1(f));
1725: case I_LOP:
1726: if ( (lid)FA0(f) == L_NOT ) return 0;
1727: else return top_is_minus((FNODE)FA1(f));
1728: case I_AND: case I_OR:
1729: return top_is_minus((FNODE)FA0(f));
1730: case I_FORMULA:
1731: obj = (Obj)FA0(f);
1.36 noro 1732: if ( !obj )
1733: return 0;
1734: else {
1735: switch ( OID(obj) ) {
1736: case O_N:
1737: return mmono((P)obj);
1738: case O_P:
1739: /* must be a variable */
1740: opname = conv_rule(VR((P)obj)->name);
1741: return opname[0]=='-';
1742: default:
1743: /* ??? */
1744: len = estimate_length(CO,obj);
1745: opname = (char *)MALLOC_ATOMIC(len+1);
1746: soutput_init(opname);
1747: sprintexpr(CO,obj);
1748: return opname[0]=='-';
1749: }
1.35 noro 1750: }
1751: default:
1752: return 0;
1.14 noro 1753: }
1.47 noro 1754: }
1755:
1756: FNODE flatten_fnode(FNODE,char *);
1757:
1.48 noro 1758: void Pflatten_quote(NODE arg,Obj *rp)
1.47 noro 1759: {
1760: FNODE f;
1761: QUOTE q;
1762:
1.48 noro 1763: if ( !ARG0(arg) || OID((Obj)ARG0(arg)) != O_QUOTE )
1764: *rp = (Obj)ARG0(arg);
1.65 noro 1765: else if ( argc(arg) == 1 ) {
1766: f = flatten_fnode(BDY((QUOTE)ARG0(arg)),"+");
1767: f = flatten_fnode(f,"*");
1768: MKQUOTE(q,f);
1769: *rp = (Obj)q;
1770: } else {
1.48 noro 1771: f = flatten_fnode(BDY((QUOTE)ARG0(arg)),BDY((STRING)ARG1(arg)));
1772: MKQUOTE(q,f);
1773: *rp = (Obj)q;
1774: }
1.63 noro 1775: }
1776:
1777: void Pget_quote_id(NODE arg,Q *rp)
1778: {
1779: FNODE f;
1780: QUOTE q;
1781:
1782: q = (QUOTE)ARG0(arg);
1783: if ( !q || OID(q) != O_QUOTE )
1784: error("get_quote_id : invalid argument");
1785: f = BDY(q);
1786: STOQ((int)f->id,*rp);
1.48 noro 1787: }
1788:
1789: void Pquote_to_funargs(NODE arg,LIST *rp)
1790: {
1791: fid_spec_p spec;
1792: QUOTE q;
1793: QUOTEARG qa;
1794: FNODE f;
1795: STRING s;
1796: QUOTE r;
1797: int i;
1798: Q id,a;
1.49 noro 1799: LIST l;
1800: NODE t0,t,w,u,u0;
1.48 noro 1801:
1802: q = (QUOTE)ARG0(arg);
1803: if ( !q || OID(q) != O_QUOTE )
1804: error("quote_to_funargs : invalid argument");
1805: f = BDY(q);
1806: if ( !f ) {
1807: MKLIST(*rp,0);
1808: return;
1809: }
1810: get_fid_spec(f->id,&spec);
1811: if ( !spec )
1812: error("quote_to_funargs : not supported yet");
1813: t0 = 0;
1814: STOQ((int)f->id,id);
1815: NEXTNODE(t0,t);
1816: BDY(t) = (pointer)id;
1817: for ( i = 0; spec->type[i] != A_end; i++ ) {
1818: NEXTNODE(t0,t);
1819: switch ( spec->type[i] ) {
1820: case A_fnode:
1821: MKQUOTE(r,(FNODE)f->arg[i]);
1822: BDY(t) = (pointer)r;
1823: break;
1824: case A_int:
1825: STOQ((int)f->arg[i],a);
1826: BDY(t) = (pointer)a;
1827: break;
1828: case A_str:
1829: MKSTR(s,(char *)f->arg[i]);
1830: BDY(t) = (pointer)s;
1831: break;
1832: case A_internal:
1833: BDY(t) = (pointer)f->arg[i];
1834: break;
1.49 noro 1835: case A_node:
1836: w = (NODE)f->arg[i];
1837: for ( u0 = 0; w; w = NEXT(w) ){
1838: NEXTNODE(u0,u);
1839: MKQUOTE(r,(FNODE)BDY(w));
1840: BDY(u) = (pointer)r;
1841: }
1842: if ( u0 ) NEXT(u) = 0;
1843: MKLIST(l,u0);
1844: BDY(t) = (pointer)l;
1845: break;
1.48 noro 1846: default:
1847: MKQUOTEARG(qa,spec->type[i],f->arg[i]);
1848: BDY(t) = (pointer)qa;
1849: break;
1850: }
1851: }
1852: if ( t0 ) NEXT(t) = 0;
1853: MKLIST(*rp,t0);
1854: }
1855:
1856: void Pfunargs_to_quote(NODE arg,QUOTE *rp)
1857: {
1858: fid_spec_p spec;
1859: QUOTE q;
1860: QUOTEARG qa;
1861: FNODE f;
1862: STRING s;
1.49 noro 1863: QUOTE r,b;
1.48 noro 1864: int i;
1865: LIST l;
1866: fid id;
1867: Obj a;
1.49 noro 1868: NODE t0,t,u0,u,w;
1.48 noro 1869:
1870: l = (LIST)ARG0(arg);
1871: if ( !l || OID(l) != O_LIST || !(t=BDY(l)) )
1872: error("funargs_to_quote : invalid argument");
1873: t = BDY(l);
1874: id = (fid)QTOS((Q)BDY(t)); t = NEXT(t);
1875: get_fid_spec(id,&spec);
1876: if ( !spec )
1877: error("funargs_to_quote : not supported yet");
1878: for ( i = 0; spec->type[i] != A_end; i++ );
1879: NEWFNODE(f,i);
1880: f->id = id;
1881: for ( i = 0; spec->type[i] != A_end; i++, t = NEXT(t) ) {
1882: if ( !t )
1883: error("funargs_to_quote : argument mismatch");
1884: a = (Obj)BDY(t);
1885: switch ( spec->type[i] ) {
1886: case A_fnode:
1887: if ( !a || OID(a) != O_QUOTE )
1888: error("funargs_to_quote : invalid argument");
1889: f->arg[i] = BDY((QUOTE)a);
1890: break;
1891: case A_int:
1892: if ( !INT(a) )
1893: error("funargs_to_quote : invalid argument");
1894: f->arg[i] = (pointer)QTOS((Q)a);
1895: break;
1896: case A_str:
1897: if ( !a || OID(a) != O_STR )
1898: error("funargs_to_quote : invalid argument");
1899: f->arg[i] = (pointer)BDY((STRING)a);
1900: break;
1901: case A_internal:
1902: f->arg[i] = (pointer)a;
1.49 noro 1903: break;
1904: case A_node:
1905: if ( !a || OID(a) != O_LIST )
1906: error("funargs_to_quote : invalid argument");
1907: u0 = 0;
1908: for ( w = BDY((LIST)a); w; w = NEXT(w) ) {
1909: NEXTNODE(u0,u);
1910: b = (QUOTE)BDY(w);
1911: if ( !b || OID(b) != O_QUOTE )
1912: error("funargs_to_quote : invalid argument");
1913: BDY(u) = BDY(b);
1914: }
1915: if ( u0 ) NEXT(u) = 0;
1916: f->arg[i] = (pointer)u0;
1.48 noro 1917: break;
1918: default:
1919: if ( !a || OID(a) != O_QUOTEARG ||
1920: ((QUOTEARG)a)->type != spec->type[i] )
1921: error("funargs_to_quote : invalid argument");
1922: f->arg[i] = BDY((QUOTEARG)a);
1923: break;
1924: }
1925: }
1926: MKQUOTE(*rp,f);
1.69 noro 1927: }
1928:
1.76 noro 1929: FNODE fnode_apply(FNODE f,FNODE (*func)(),int expand);
1930: FNODE fnode_normalize(FNODE f,int expand);
1.70 noro 1931:
1932: void Pquote_normalize(NODE arg,QUOTE *rp)
1933: {
1934: QUOTE q,r;
1935: FNODE f;
1.76 noro 1936: int expand,ac;
1.70 noro 1937:
1.76 noro 1938: ac = argc(arg);
1939: if ( !ac ) error("quote_normalize : invalid argument");
1.70 noro 1940: q = (QUOTE)ARG0(arg);
1.76 noro 1941: expand = ac==2 && ARG1(arg);
1.70 noro 1942: if ( !q || OID(q) != O_QUOTE ) {
1943: *rp = q;
1944: return;
1945: } else {
1.76 noro 1946: f = fnode_normalize(BDY(q),expand);
1.70 noro 1947: MKQUOTE(r,f);
1948: *rp = r;
1949: }
1950: }
1951:
1.80 noro 1952: void Pquote_normalize_comp(NODE arg,Q *rp)
1953: {
1954: FNODE f1,f2;
1955: int r;
1956:
1957: f1 = BDY((QUOTE)ARG0(arg));
1958: f2 = BDY((QUOTE)ARG1(arg));
1959: f1 = fnode_normalize(f1,0);
1960: f2 = fnode_normalize(f2,0);
1961: r = fnode_normalize_comp(f1,f2);
1962: STOQ(r,*rp);
1963: }
1964:
1.69 noro 1965: int fnode_is_number(FNODE f)
1966: {
1967: Obj obj;
1968:
1969: switch ( f->id ) {
1970: case I_MINUS: case I_PAREN:
1971: return fnode_is_number(FA0(f));
1972:
1973: case I_FORMULA:
1974: obj = FA0(f);
1975: if ( !obj ) return 1;
1976: else if ( OID(obj) == O_QUOTE )
1977: return fnode_is_number(BDY((QUOTE)obj));
1978: else if ( NUM(obj) ) return 1;
1979: else return 0;
1980:
1981: case I_BOP:
1982: return fnode_is_number(FA1(f)) && fnode_is_number(FA2(f));
1983:
1984: default:
1985: return 0;
1986: }
1987: }
1988:
1989: int fnode_is_rational(FNODE f)
1990: {
1991: Obj obj;
1992:
1993: switch ( f->id ) {
1994: case I_MINUS: case I_PAREN:
1995: return fnode_is_number(FA0(f));
1996:
1997: case I_FORMULA:
1998: obj = FA0(f);
1999: if ( !obj ) return 1;
2000: else if ( OID(obj) == O_QUOTE )
2001: return fnode_is_rational(BDY((QUOTE)obj));
2002: else if ( NUM(obj) && RATN(obj) ) return 1;
2003: else return 0;
2004:
2005: case I_BOP:
2006: if ( !strcmp(((ARF)FA0(f))->name,"^") )
2007: return fnode_is_rational(FA1(f)) && fnode_is_integer(FA2(f));
2008: else
2009: return fnode_is_rational(FA1(f)) && fnode_is_rational(FA2(f));
2010:
2011: default:
2012: return 0;
2013: }
2014: }
2015:
2016: int fnode_is_integer(FNODE f)
2017: {
2018: Obj obj;
2019:
2020: switch ( f->id ) {
2021: case I_MINUS: case I_PAREN:
2022: return fnode_is_integer(FA0(f));
2023:
2024: case I_FORMULA:
2025: obj = FA0(f);
2026: if ( !obj ) return 1;
2027: else if ( OID(obj) == O_QUOTE )
2028: return fnode_is_integer(BDY((QUOTE)obj));
2029: else if ( INT(obj)) return 1;
2030: else return 0;
2031:
2032: case I_BOP:
1.70 noro 2033: switch ( ((ARF)FA0(f))->name[0] ) {
2034: case '^':
2035: return fnode_is_integer(FA1(f))
2036: && fnode_is_nonnegative_integer(FA2(f));
2037: case '/':
2038: return fnode_is_integer(FA1(f)) &&
2039: ( fnode_is_one(FA2(f)) || fnode_is_minusone(FA2(f)) );
2040: default:
2041: return fnode_is_integer(FA1(f)) && fnode_is_integer(FA2(f));
2042: }
2043: break;
2044:
1.69 noro 2045: default:
2046: return 0;
2047: }
2048: }
2049:
2050: int fnode_is_nonnegative_integer(FNODE f)
2051: {
2052: Q n;
2053:
2054: n = eval(f);
2055: if ( !n || (INT(n) && SGN(n) > 0) ) return 1;
2056: else return 0;
2057: }
2058:
2059: int fnode_is_one(FNODE f)
2060: {
2061: Q n;
2062:
2063: n = eval(f);
2064: if ( UNIQ(n) ) return 1;
2065: else return 0;
2066: }
2067:
2068: int fnode_is_minusone(FNODE f)
2069: {
2070: Q n;
2071:
2072: n = eval(f);
2073: if ( MUNIQ(n) ) return 1;
2074: else return 0;
2075: }
2076:
2077: int fnode_is_dependent(FNODE f,V v)
2078: {
2079: Obj obj;
2080: FNODE arg;
2081: NODE t;
2082:
2083: switch ( f->id ) {
2084: case I_MINUS: case I_PAREN:
2085: return fnode_is_dependent(FA0(f),v);
2086:
2087: case I_FORMULA:
2088: obj = FA0(f);
2089: if ( !obj ) return 0;
2090: else if ( OID(obj) == O_QUOTE )
2091: return fnode_is_dependent(BDY((QUOTE)obj),v);
2092: else if ( obj_is_dependent(obj,v) ) return 1;
2093: else return 0;
2094:
2095: case I_BOP:
2096: return fnode_is_dependent(FA1(f),v) || fnode_is_dependent(FA2(f),v);
2097:
2098: case I_FUNC:
2099: arg = (FNODE)FA1(f);
2100: for ( t = FA0(arg); t; t = NEXT(t) )
2101: if ( fnode_is_dependent(BDY(t),v) ) return 1;
2102: return 0;
2103:
2104: default:
2105: return 0;
2106: }
1.1 noro 2107: }
1.70 noro 2108:
1.76 noro 2109: FNODE fnode_normalize_add(FNODE a1,FNODE a2,int expand);
2110: FNODE fnode_normalize_mul(FNODE a1,FNODE a2,int expand);
2111: FNODE fnode_normalize_pwr(FNODE a1,FNODE a2,int expand);
2112: FNODE fnode_normalize_mul_coef(Num c,FNODE f,int expand);
2113: FNODE fnode_expand_pwr(FNODE f,int n);
1.72 noro 2114: FNODE to_narymul(FNODE f);
2115: FNODE to_naryadd(FNODE f);
1.75 noro 2116: FNODE fnode_node_to_naryadd(NODE n);
2117: FNODE fnode_node_to_narymul(NODE n);
1.72 noro 2118: void fnode_base_exp(FNODE f,FNODE *bp,FNODE *ep);
2119: void fnode_coef_body(FNODE f,Num *cp,FNODE *bp);
2120:
2121:
1.76 noro 2122: FNODE fnode_normalize(FNODE f,int expand)
1.70 noro 2123: {
1.73 noro 2124: FNODE a1,a2,mone,r,b2;
1.72 noro 2125: NODE n;
1.70 noro 2126: Q q;
2127:
1.72 noro 2128: STOQ(-1,q);
2129: mone = mkfnode(1,I_FORMULA,q);
1.70 noro 2130: switch ( f->id ) {
2131: case I_PAREN:
1.76 noro 2132: return fnode_normalize(FA0(f),expand);
1.71 noro 2133:
2134: case I_MINUS:
1.76 noro 2135: return fnode_normalize_mul_coef((Num)q,
2136: fnode_normalize(FA0(f),expand),expand);
1.71 noro 2137:
1.70 noro 2138: case I_BOP:
2139: /* arf fnode fnode */
1.76 noro 2140: a1 = fnode_normalize(FA1(f),expand);
2141: a2 = fnode_normalize(FA2(f),expand);
1.72 noro 2142: switch ( OPNAME(f) ) {
2143: case '+':
1.76 noro 2144: return fnode_normalize_add(a1,a2,expand);
1.70 noro 2145: case '-':
1.76 noro 2146: a2 = fnode_normalize_mul_coef((Num)q,a2,expand);
2147: return fnode_normalize_add(a1,a2,expand);
1.72 noro 2148: case '*':
1.76 noro 2149: return fnode_normalize_mul(a1,a2,expand);
1.70 noro 2150: case '/':
1.76 noro 2151: a2 = fnode_normalize_pwr(a2,mone,expand);
2152: return fnode_normalize_mul(a1,a2,expand);
1.73 noro 2153: case '^':
1.76 noro 2154: return fnode_normalize_pwr(a1,a2,expand);
1.70 noro 2155: default:
1.72 noro 2156: return mkfnode(3,I_BOP,FA0(f),a1,a2);
1.70 noro 2157: }
2158: break;
1.71 noro 2159:
1.70 noro 2160: case I_NARYOP:
1.72 noro 2161: switch ( OPNAME(f) ) {
1.71 noro 2162: case '+':
1.73 noro 2163: n = (NODE)FA1(f);
1.76 noro 2164: r = fnode_normalize(BDY(n),expand); n = NEXT(n);
1.72 noro 2165: for ( ; n; n = NEXT(n) ) {
1.76 noro 2166: a1 = fnode_normalize(BDY(n),expand);
2167: r = fnode_normalize_add(r,a1,expand);
1.72 noro 2168: }
2169: return r;
1.70 noro 2170: case '*':
1.73 noro 2171: n = (NODE)FA1(f);
1.76 noro 2172: r = fnode_normalize(BDY(n),expand); n = NEXT(n);
1.72 noro 2173: for ( ; n; n = NEXT(n) ) {
1.76 noro 2174: a1 = fnode_normalize(BDY(n),expand);
2175: r = fnode_normalize_mul(r,a1,expand);
1.72 noro 2176: }
2177: return r;
1.70 noro 2178: default:
1.72 noro 2179: error("fnode_normallize : cannot happen");
1.70 noro 2180: }
1.72 noro 2181:
1.70 noro 2182: default:
1.76 noro 2183: return fnode_apply(f,fnode_normalize,expand);
1.70 noro 2184: }
2185: }
2186:
1.76 noro 2187: FNODE fnode_apply(FNODE f,FNODE (*func)(),int expand)
1.70 noro 2188: {
2189: fid_spec_p spec;
2190: FNODE r;
2191: int i,n;
2192: NODE t,t0,s;
2193:
2194: get_fid_spec(f->id,&spec);
2195: for ( n = 0; spec->type[n] != A_end; n++ );
2196: NEWFNODE(r,n); r->id = f->id;
2197: for ( i = 0; i < n; i++ ) {
2198: switch ( spec->type[i] ) {
2199: case A_fnode:
1.78 noro 2200: r->arg[i] = func(f->arg[i],expand);
1.70 noro 2201: break;
2202: case A_node:
2203: s = (NODE)f->arg[i];
2204: for ( t0 = 0; s; s = NEXT(s) ) {
2205: NEXTNODE(t0,t);
1.76 noro 2206: BDY(t) = (pointer)func((FNODE)BDY(s),expand);
1.70 noro 2207: }
2208: if ( t0 ) NEXT(t) = 0;
2209: r->arg[i] = t0;
2210: break;
2211: default:
2212: r->arg[i] = f->arg[i];
2213: break;
2214: }
2215: }
2216: return r;
2217: }
2218:
1.76 noro 2219: FNODE fnode_normalize_add(FNODE f1,FNODE f2,int expand)
1.71 noro 2220: {
1.72 noro 2221: NODE n1,n2,r0,r;
2222: FNODE b1,b2;
2223: int s;
2224: Num c1,c2,c;
2225:
1.77 noro 2226: if ( IS_ZERO(f1) ) return f2;
2227: else if ( IS_ZERO(f2) ) return f1;
1.75 noro 2228: f1 = to_naryadd(f1); f2 = to_naryadd(f2);
2229: n1 = (NODE)FA1(f1); n2 = (NODE)FA1(f2);
1.72 noro 2230: r0 = 0;
2231: while ( n1 && n2 ) {
1.75 noro 2232: fnode_coef_body(BDY(n1),&c1,&b1); fnode_coef_body(BDY(n2),&c2,&b2);
1.80 noro 2233: if ( (s = fnode_normalize_comp(b1,b2)) > 0 ) {
1.73 noro 2234: NEXTNODE(r0,r); BDY(r) = BDY(n1); n1 = NEXT(n1);
1.72 noro 2235: } else if ( s < 0 ) {
1.73 noro 2236: NEXTNODE(r0,r); BDY(r) = BDY(n2); n2 = NEXT(n2);
1.72 noro 2237: } else {
2238: addnum(0,c1,c2,&c);
2239: if ( c ) {
1.76 noro 2240: NEXTNODE(r0,r); BDY(r) = fnode_normalize_mul_coef(c,b1,expand);
1.71 noro 2241: }
1.72 noro 2242: n1 = NEXT(n1); n2 = NEXT(n2);
1.71 noro 2243: }
2244: }
1.72 noro 2245: if ( n1 )
2246: if ( r0 ) NEXT(r) = n1;
2247: else r0 = n1;
2248: else if ( n2 )
2249: if ( r0 ) NEXT(r) = n2;
2250: else r0 = n2;
2251: else if ( r0 )
2252: NEXT(r) = 0;
2253:
1.75 noro 2254: return fnode_node_to_naryadd(r0);
2255: }
2256:
2257: FNODE fnode_node_to_naryadd(NODE n)
2258: {
2259: if ( !n ) return mkfnode(1,I_FORMULA,0);
2260: else if ( !NEXT(n) ) return BDY(n);
2261: else return mkfnode(2,I_NARYOP,addfs,n);
2262: }
2263:
2264: FNODE fnode_node_to_narymul(NODE n)
2265: {
2266: if ( !n ) return mkfnode(1,I_FORMULA,ONE);
2267: else if ( !NEXT(n) ) return BDY(n);
2268: else return mkfnode(2,I_NARYOP,mulfs,n);
1.71 noro 2269: }
2270:
1.76 noro 2271: FNODE fnode_normalize_mul(FNODE f1,FNODE f2,int expand)
1.71 noro 2272: {
1.72 noro 2273: NODE n1,n2,r0,r,r1;
1.76 noro 2274: FNODE b1,b2,e1,e2,cc,t,t1;
1.72 noro 2275: FNODE *m;
2276: int s;
2277: Num c1,c2,c,e;
1.75 noro 2278: int l1,l,i,j;
1.72 noro 2279:
1.77 noro 2280: if ( IS_ZERO(f1) || IS_ZERO(f2) ) return mkfnode(1,I_FORMULA,0);
1.75 noro 2281: else if ( fnode_is_number(f1) )
1.76 noro 2282: return fnode_normalize_mul_coef((Num)eval(f1),f2,expand);
1.75 noro 2283: else if ( fnode_is_number(f2) )
1.76 noro 2284: return fnode_normalize_mul_coef((Num)eval(f2),f1,expand);
2285:
2286: if ( expand && IS_NARYADD(f1) ) {
2287: t = mkfnode(1,I_FORMULA,0);
2288: for ( n1 = (NODE)FA1(f1); n1; n1 = NEXT(n1) ) {
2289: t1 = fnode_normalize_mul(BDY(n1),f2,expand);
2290: t = fnode_normalize_add(t,t1,expand);
2291: }
2292: return t;
2293: }
2294: if ( expand && IS_NARYADD(f2) ) {
2295: t = mkfnode(1,I_FORMULA,0);
2296: for ( n2 = (NODE)FA1(f2); n2; n2 = NEXT(n2) ) {
2297: t1 = fnode_normalize_mul(f1,BDY(n2),expand);
2298: t = fnode_normalize_add(t,t1,expand);
2299: }
2300: return t;
2301: }
1.72 noro 2302:
1.75 noro 2303: fnode_coef_body(f1,&c1,&b1); fnode_coef_body(f2,&c2,&b2);
2304: mulnum(0,c1,c2,&c);
1.72 noro 2305: if ( !c ) return mkfnode(1,I_FORMULA,0);
1.71 noro 2306:
1.76 noro 2307:
1.75 noro 2308: n1 = (NODE)FA1(to_narymul(b1)); n2 = (NODE)FA1(to_narymul(b2));
2309: l1 = length(n1); l = l1+length(n2);
1.72 noro 2310: m = (FNODE *)ALLOCA(l*sizeof(FNODE));
2311: for ( r = n1, i = 0; i < l1; r = NEXT(r), i++ ) m[i] = BDY(r);
2312: for ( r = n2; r; r = NEXT(r) ) {
2313: if ( i == 0 )
2314: m[i++] = BDY(r);
2315: else {
1.75 noro 2316: fnode_base_exp(m[i-1],&b1,&e1); fnode_base_exp(BDY(r),&b2,&e2);
2317: if ( compfnode(b1,b2) ) break;
2318: addnum(0,eval(e1),eval(e2),&e);
2319: if ( !e ) i--;
2320: else if ( UNIQ(e) )
2321: m[i-1] = b1;
2322: else
2323: m[i-1] = mkfnode(3,I_BOP,pwrfs,b1,mkfnode(1,I_FORMULA,e));
1.71 noro 2324: }
2325: }
1.75 noro 2326: for ( j = i-1; j >= 0; j-- ) {
2327: MKNODE(r1,m[j],r); r = r1;
2328: }
2329: if ( !UNIQ(c) ) {
2330: cc = mkfnode(1,I_FORMULA,c); MKNODE(r1,cc,r); r = r1;
1.72 noro 2331: }
1.75 noro 2332: return fnode_node_to_narymul(r);
1.73 noro 2333: }
2334:
1.76 noro 2335: FNODE fnode_normalize_pwr(FNODE f1,FNODE f2,int expand)
1.73 noro 2336: {
2337: FNODE b,b1,e1,e,cc,r;
2338: Num c,c1;
2339: NODE arg,n;
1.76 noro 2340: Q q;
1.73 noro 2341:
1.77 noro 2342: if ( IS_ZERO(f2) ) return mkfnode(1,I_FORMULA,ONE);
2343: else if ( IS_ZERO(f1) ) return mkfnode(1,I_FORMULA,0);
1.73 noro 2344: else if ( fnode_is_one(f2) ) return f1;
2345: else if ( fnode_is_number(f1) )
2346: if ( fnode_is_integer(f2) ) {
2347: pwrnum(0,(Num)eval(f1),(Num)eval(f2),&c);
2348: return mkfnode(1,I_FORMULA,c);
2349: } else
2350: return mkfnode(3,I_BOP,pwrfs,f1,f2);
1.75 noro 2351: else if ( IS_BINARYPWR(f1) ) {
1.73 noro 2352: b1 = FA1(f1); e1 = FA2(f1);
1.76 noro 2353: e = fnode_normalize_mul(e1,f2,expand);
1.73 noro 2354: if ( fnode_is_one(e) )
2355: return b1;
2356: else
2357: return mkfnode(3,I_BOP,FA0(f1),b1,e);
1.75 noro 2358: } else if ( IS_NARYMUL(f1) && fnode_is_integer(f2) ) {
1.73 noro 2359: fnode_coef_body(f1,&c1,&b1);
1.75 noro 2360: pwrnum(0,(Num)c1,(Num)eval(f2),&c);
2361: cc = mkfnode(1,I_FORMULA,c);
1.76 noro 2362: b = fnode_normalize_pwr(b1,f2,expand);
1.75 noro 2363: if ( fnode_is_one(cc) )
2364: return b;
2365: else
2366: return fnode_node_to_narymul(mknode(2,cc,b));
1.82 noro 2367: } else if ( expand && fnode_is_integer(f2)
2368: && fnode_is_nonnegative_integer(f2) ) {
1.76 noro 2369: q = (Q)eval(f2);
2370: if ( PL(NM(q)) > 1 ) error("fnode_normalize_pwr : exponent too large");
2371: return fnode_expand_pwr(f1,QTOS(q));
1.73 noro 2372: } else
2373: return mkfnode(3,I_BOP,pwrfs,f1,f2);
1.72 noro 2374: }
2375:
1.76 noro 2376: FNODE fnode_expand_pwr(FNODE f,int n)
2377: {
2378: int n1;
2379: FNODE f1,f2;
2380:
2381: if ( !n ) return mkfnode(1,I_FORMULA,ONE);
1.77 noro 2382: else if ( IS_ZERO(f) ) return mkfnode(1,I_FORMULA,0);
1.76 noro 2383: else if ( n == 1 ) return f;
2384: else {
2385: n1 = n/2;
2386: f1 = fnode_expand_pwr(f,n1);
2387: f2 = fnode_normalize_mul(f1,f1,1);
2388: if ( n%2 ) f2 = fnode_normalize_mul(f2,f,1);
2389: return f2;
2390: }
2391: }
2392:
1.72 noro 2393: /* f = b^e */
2394: void fnode_base_exp(FNODE f,FNODE *bp,FNODE *ep)
2395: {
1.75 noro 2396: if ( IS_BINARYPWR(f) ) {
1.72 noro 2397: *bp = FA1(f); *ep = FA2(f);
2398: } else {
2399: *bp = f; *ep = mkfnode(1,I_FORMULA,ONE);
2400: }
2401: }
2402:
2403: FNODE to_naryadd(FNODE f)
2404: {
2405: FNODE r;
2406: NODE n;
2407:
1.75 noro 2408: if ( IS_NARYADD(f) ) return f;
2409:
2410: NEWFNODE(r,2); r->id = I_NARYOP;
2411: FA0(r) = addfs; MKNODE(n,f,0); FA1(r) = n;
1.72 noro 2412: return r;
2413: }
2414:
2415: FNODE to_narymul(FNODE f)
2416: {
2417: FNODE r;
2418: NODE n;
2419:
1.75 noro 2420: if ( IS_NARYMUL(f) ) return f;
2421:
2422: NEWFNODE(r,2); r->id = I_NARYOP;
2423: FA0(r) = mulfs; MKNODE(n,f,0); FA1(r) = n;
1.72 noro 2424: return r;
2425: }
2426:
1.76 noro 2427: FNODE fnode_normalize_mul_coef(Num c,FNODE f,int expand)
1.72 noro 2428: {
1.76 noro 2429: FNODE b1,cc;
1.72 noro 2430: Num c1,c2;
1.76 noro 2431: NODE n,r,r0;
1.72 noro 2432:
2433: if ( !c )
2434: return mkfnode(I_FORMULA,0);
1.75 noro 2435: else {
2436: fnode_coef_body(f,&c1,&b1);
2437: mulnum(0,c,c1,&c2);
2438: if ( UNIQ(c2) ) return b1;
2439: else {
2440: cc = mkfnode(1,I_FORMULA,c2);
2441: if ( fnode_is_number(b1) ) {
2442: if ( !fnode_is_one(b1) )
2443: error("fnode_normalize_mul_coef : cannot happen");
2444: else
2445: return cc;
1.76 noro 2446: } else if ( IS_NARYMUL(b1) ) {
1.75 noro 2447: MKNODE(n,cc,FA1(b1));
1.76 noro 2448: return fnode_node_to_narymul(n);
2449: } else if ( expand && IS_NARYADD(b1) ) {
2450: for ( r0 = 0, n = (NODE)FA1(b1); n; n = NEXT(n) ) {
2451: NEXTNODE(r0,r);
2452: BDY(r) = fnode_normalize_mul_coef(c2,BDY(n),expand);
2453: }
2454: if ( r0 ) NEXT(r) = 0;
2455: return fnode_node_to_naryadd(r0);
2456: } else
2457: return fnode_node_to_narymul(mknode(2,cc,b1));
1.72 noro 2458: }
1.71 noro 2459: }
2460: }
2461:
1.72 noro 2462: void fnode_coef_body(FNODE f,Num *cp,FNODE *bp)
1.70 noro 2463: {
1.72 noro 2464: FNODE c;
1.70 noro 2465:
1.72 noro 2466: if ( fnode_is_number(f) ) {
2467: *cp = eval(f); *bp = mkfnode(1,I_FORMULA,ONE);
1.75 noro 2468: } else if ( IS_NARYMUL(f) ) {
2469: c=(FNODE)BDY((NODE)FA1(f));
1.72 noro 2470: if ( fnode_is_number(c) ) {
2471: *cp = eval(c);
1.75 noro 2472: *bp = fnode_node_to_narymul(NEXT((NODE)FA1(f)));
1.70 noro 2473: } else {
1.72 noro 2474: *cp = (Num)ONE; *bp = f;
1.70 noro 2475: }
2476: } else {
1.72 noro 2477: *cp = (Num)ONE; *bp = f;
1.70 noro 2478: }
1.80 noro 2479: }
2480:
2481: int fnode_normalize_comp_pwr(FNODE f1,FNODE f2);
2482:
2483: int fnode_normalize_comp(FNODE f1,FNODE f2)
2484: {
2485: NODE n1,n2;
2486: int r,i1,i2;
1.82 noro 2487: char *nm1,*nm2;
1.80 noro 2488: FNODE b1,b2,e1,e2,g;
1.83 noro 2489: Num ee,ee1,c1,c2;
1.80 noro 2490:
2491: if ( IS_NARYADD(f1) || IS_NARYADD(f2) ) {
2492: f1 = to_naryadd(f1); f2 = to_naryadd(f2);
2493: n1 = (NODE)FA1(f1); n2 = (NODE)FA1(f2);
2494: while ( n1 && n2 )
2495: if ( r = fnode_normalize_comp(BDY(n1),BDY(n2)) ) return r;
2496: else {
2497: n1 = NEXT(n1); n2 = NEXT(n2);
2498: }
1.85 noro 2499: return n1?1:(n2?-1:0);
1.80 noro 2500: }
2501: if ( IS_NARYMUL(f1) || IS_NARYMUL(f2) ) {
1.83 noro 2502: fnode_coef_body(f1,&c1,&b1);
2503: fnode_coef_body(f2,&c2,&b2);
2504: if ( !compfnode(b1,b2) ) return compnum(0,c1,c2);
2505: b1 = to_narymul(b1); b2 = to_narymul(b2);
2506: n1 = (NODE)FA1(b1); n2 = (NODE)FA1(b2);
1.80 noro 2507: while ( 1 ) {
2508: while ( n1 && n2 && !compfnode(BDY(n1),BDY(n2)) ) {
2509: n1 = NEXT(n1); n2 = NEXT(n2);
2510: }
2511: if ( !n1 || !n2 ) {
1.85 noro 2512: return n1?1:(n2?-1:0);
1.80 noro 2513: }
2514: fnode_base_exp(BDY(n1),&b1,&e1);
2515: fnode_base_exp(BDY(n2),&b2,&e2);
2516:
1.83 noro 2517: if ( r = fnode_normalize_comp(b1,b2) ) {
2518: if ( r > 0 )
2519: return fnode_normalize_comp(e1,mkfnode(1,I_FORMULA,0));
2520: else if ( r < 0 )
2521: return fnode_normalize_comp(mkfnode(1,I_FORMULA,0),e2);
2522: } else {
2523: n1 = NEXT(n1); n2 = NEXT(n2);
2524: if ( fnode_is_number(e1) && fnode_is_number(e2) ) {
2525: /* f1 = t b^e1 ... , f2 = t b^e2 ... */
2526: subnum(0,eval(e1),eval(e2),&ee);
1.86 ! noro 2527: r = compnum(0,ee,0);
! 2528: if ( r > 0 ) {
1.83 noro 2529: g = mkfnode(3,I_BOP,pwrfs,b1,mkfnode(1,I_FORMULA,ee));
2530: MKNODE(n1,g,n1);
1.86 ! noro 2531: } else if ( r < 0 ) {
! 2532: chsgnnum(ee,&ee1);
! 2533: g = mkfnode(3,I_BOP,pwrfs,b1,mkfnode(1,I_FORMULA,ee1));
! 2534: MKNODE(n2,g,n2);
1.83 noro 2535: }
2536: } else {
2537: r = fnode_normalize_comp(e1,e2);
2538: if ( r > 0 ) return 1;
2539: else if ( r < 0 ) return -1;
1.80 noro 2540: }
2541: }
2542: }
2543: }
2544: if ( IS_BINARYPWR(f1) || IS_BINARYPWR(f2) )
2545: return fnode_normalize_comp_pwr(f1,f2);
2546:
2547: /* now, IDs of f1 and f2 must be I_FORMULA, I_FUNC, or I_PVAR */
2548: switch ( f1->id ) {
2549: case I_FORMULA:
2550: switch ( f2->id ) {
2551: case I_FORMULA:
2552: return arf_comp(CO,FA0(f1),FA0(f2));
2553: case I_FUNC: case I_PVAR:
2554: return -1;
2555: default:
2556: error("fnode_normalize_comp : undefined");
2557: }
2558: break;
2559: case I_FUNC:
2560: switch ( f2->id ) {
2561: case I_FORMULA:
2562: return 1;
2563: case I_FUNC:
1.82 noro 2564: nm1 = ((FUNC)FA0(f1))->name; nm2 = ((FUNC)FA0(f2))->name;
2565: r = strcmp(nm1,nm2);
2566: if ( r > 0 ) return 1;
2567: else if ( r < 0 ) return -1;
1.80 noro 2568: else {
2569: /* compare args */
2570: n1 = FA0((FNODE)FA1(f1)); n2 = FA0((FNODE)FA1(f2));
2571: while ( n1 && n2 )
1.82 noro 2572: if ( r = fnode_normalize_comp(BDY(n1),BDY(n2)) ) return r;
2573: else {
2574: n1 = NEXT(n1); n2 = NEXT(n2);
2575: }
1.85 noro 2576: return n1?1:(n2?-1:0);
1.80 noro 2577: }
2578: break;
2579: case I_PVAR:
2580: return -1;
2581: default:
2582: error("fnode_normalize_comp : undefined");
2583: }
2584: case I_PVAR:
2585: switch ( f2->id ) {
2586: case I_FORMULA: case I_FUNC:
2587: return 1;
2588: case I_PVAR:
2589: i1 = (int)FA0(f1); i2 = (int)FA0(f2);
2590: if ( i1 > i2 ) return 1;
2591: else if ( i1 < i2 ) return -1;
2592: else return 0;
2593: default:
2594: error("fnode_normalize_comp : undefined");
2595: }
2596: break;
2597: default:
2598: error("fnode_normalize_comp : undefined");
2599: }
2600: }
2601:
2602: int fnode_normalize_comp_pwr(FNODE f1,FNODE f2)
2603: {
2604: FNODE b1,b2,e1,e2;
2605: int r;
2606:
2607: fnode_base_exp(f1,&b1,&e1);
2608: fnode_base_exp(f2,&b2,&e2);
1.83 noro 2609: if ( r = fnode_normalize_comp(b1,b2) ) {
2610: if ( r > 0 )
2611: return fnode_normalize_comp(e1,mkfnode(1,I_FORMULA,0));
2612: else if ( r < 0 )
2613: return fnode_normalize_comp(mkfnode(1,I_FORMULA,0),e2);
2614: } else return fnode_normalize_comp(e1,e2);
1.70 noro 2615: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>