Annotation of OpenXM_contrib2/asir2000/builtin/strobj.c, Revision 1.106
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.106 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/strobj.c,v 1.105 2005/12/09 08:10:44 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.89 noro 61: #if defined(__GNUC__)
62: #define INLINE inline
63: #elif defined(VISUAL)
64: #define INLINE __inline
65: #else
66: #define INLINE
67: #endif
68:
1.18 noro 69: struct TeXSymbol {
70: char *text;
71: char *symbol;
72: };
73:
1.71 noro 74: #define OPNAME(f) (((ARF)FA0(f))->name[0])
1.77 noro 75: #define IS_ZERO(f) (((f)->id==I_FORMULA) && FA0(f)==0 )
1.75 noro 76: #define IS_BINARYPWR(f) (((f)->id==I_BOP) &&(OPNAME(f)=='^'))
77: #define IS_NARYADD(f) (((f)->id==I_NARYOP) &&(OPNAME(f)=='+'))
78: #define IS_NARYMUL(f) (((f)->id==I_NARYOP) &&(OPNAME(f)=='*'))
1.71 noro 79:
1.1 noro 80: extern char *parse_strp;
81:
1.50 ohara 82: void Psprintf();
1.1 noro 83: void Prtostr(), Pstrtov(), Peval_str();
1.3 noro 84: void Pstrtoascii(), Pasciitostr();
1.5 noro 85: void Pstr_len(), Pstr_chr(), Psub_str();
1.14 noro 86: void Pwrite_to_tb();
87: void Ptb_to_string();
88: void Pclear_tb();
89: void Pstring_to_tb();
90: void Pquotetotex_tb();
91: void Pquotetotex();
1.24 noro 92: void Pquotetotex_env();
1.47 noro 93: void Pflatten_quote();
1.69 noro 94:
1.105 noro 95: void Pqt_is_integer(),Pqt_is_rational(),Pqt_is_number();
96: void Pqt_is_dependent(),Pqt_is_function();
97: void Pqt_set_ord();
98: void Pqt_normalize();
99: void Pnqt_comp();
100: void Pnqt_match();
1.106 ! noro 101: void Pnqt_match_rewrite();
1.69 noro 102:
1.105 noro 103: void Pqt_to_nbp();
1.101 noro 104: void Pshuffle_mul(), Pharmonic_mul();
1.102 noro 105: void Pnbp_hm(), Pnbp_ht(), Pnbp_hc(), Pnbp_rest();
106: void Pnbm_deg();
107: void Pnbm_hp_rest();
108: void Pnbm_hxky(), Pnbm_xky_rest();
109: void Pnbm_hv(), Pnbm_rest();
1.98 noro 110:
1.52 noro 111: void Pquote_to_funargs(),Pfunargs_to_quote(),Pget_function_name();
1.105 noro 112: void Pqt_match(),Pget_quote_id(),Pqt_match_rewrite();
113: void Pqt_to_nary(),Pqt_to_bin();
1.88 noro 114: void fnode_do_assign(NODE arg);
1.58 ohara 115: void do_assign(NODE arg);
1.14 noro 116: void fnodetotex_tb(FNODE f,TB tb);
117: char *symbol_name(char *name);
1.28 noro 118: char *conv_rule(char *name);
1.38 noro 119: char *conv_subs(char *name);
1.28 noro 120: char *call_convfunc(char *name);
1.14 noro 121: void tb_to_string(TB tb,STRING *rp);
122: void fnodenodetotex_tb(NODE n,TB tb);
123: void fargstotex_tb(char *opname,FNODE f,TB tb);
1.35 noro 124: int top_is_minus(FNODE f);
1.105 noro 125: int qt_match(Obj f,Obj pat,NODE *rp);
1.106 ! noro 126: FNODE partial_eval(FNODE), fnode_to_nary(FNODE), fnode_to_bin(FNODE,int);
! 127: FNODE nfnode_add(FNODE a1,FNODE a2,int expand);
! 128: FNODE nfnode_mul(FNODE a1,FNODE a2,int expand);
! 129: FNODE nfnode_pwr(FNODE a1,FNODE a2,int expand);
! 130: FNODE nfnode_mul_coef(Num c,FNODE f,int expand);
! 131: FNODE fnode_expand_pwr(FNODE f,int n,int expand);
! 132: FNODE to_narymul(FNODE f);
! 133: FNODE to_naryadd(FNODE f);
! 134: FNODE fnode_node_to_nary(ARF op,NODE n);
! 135: void fnode_base_exp(FNODE f,FNODE *bp,FNODE *ep);
! 136: void fnode_coef_body(FNODE f,Num *cp,FNODE *bp);
! 137: FNODE nfnode_match_rewrite(FNODE f,FNODE p,FNODE c,FNODE a,int mode);
! 138: FNODE fnode_apply(FNODE f,FNODE (*func)(),int expand);
! 139: FNODE fnode_normalize(FNODE f,int expand);
1.1 noro 140:
141: struct ftab str_tab[] = {
1.50 ohara 142: {"sprintf",Psprintf,-99999999},
1.1 noro 143: {"rtostr",Prtostr,1},
144: {"strtov",Pstrtov,1},
145: {"eval_str",Peval_str,1},
1.3 noro 146: {"strtoascii",Pstrtoascii,1},
147: {"asciitostr",Pasciitostr,1},
1.5 noro 148: {"str_len",Pstr_len,1},
149: {"str_chr",Pstr_chr,3},
150: {"sub_str",Psub_str,3},
1.14 noro 151: {"write_to_tb",Pwrite_to_tb,2},
152: {"clear_tb",Pclear_tb,1},
153: {"tb_to_string",Ptb_to_string,1},
154: {"string_to_tb",Pstring_to_tb,1},
1.63 noro 155: {"get_quote_id",Pget_quote_id,1},
1.69 noro 156:
1.105 noro 157: {"qt_is_number",Pqt_is_number,1},
158: {"qt_is_rational",Pqt_is_rational,1},
159: {"qt_is_integer",Pqt_is_integer,1},
160: {"qt_is_function",Pqt_is_function,1},
161: {"qt_is_dependent",Pqt_is_dependent,2},
162:
163: {"qt_set_ord",Pqt_set_ord,-1},
164: {"qt_normalize",Pqt_normalize,-2},
165: {"qt_match",Pqt_match,2},
166: {"qt_match_rewrite",Pqt_match_rewrite,-4},
1.106 ! noro 167: {"nqt_match_rewrite",Pnqt_match_rewrite,3},
1.105 noro 168:
169: {"nqt_comp",Pnqt_comp,2},
170: {"nqt_match",Pnqt_match,-3},
171: {"qt_to_nbp",Pqt_to_nbp,1},
1.101 noro 172: {"shuffle_mul",Pshuffle_mul,2},
173: {"harmonic_mul",Pharmonic_mul,2},
1.70 noro 174:
1.100 noro 175: {"nbp_hm", Pnbp_hm,1},
176: {"nbp_ht", Pnbp_ht,1},
177: {"nbp_hc", Pnbp_hc,1},
178: {"nbp_rest", Pnbp_rest,1},
1.102 noro 179: {"nbm_deg", Pnbm_deg,1},
180: {"nbm_hxky", Pnbm_hxky,1},
181: {"nbm_xky_rest", Pnbm_xky_rest,1},
182: {"nbm_hp_rest", Pnbm_hp_rest,1},
183: {"nbm_hv", Pnbm_hv,1},
1.100 noro 184: {"nbm_rest", Pnbm_rest,1},
185:
1.105 noro 186: {"qt_to_nary",Pqt_to_nary,1},
187: {"qt_to_bin",Pqt_to_bin,2},
1.95 noro 188:
1.60 noro 189: {"quotetotex_tb",Pquotetotex_tb,2},
190: {"quotetotex",Pquotetotex,1},
191: {"quotetotex_env",Pquotetotex_env,-99999999},
1.65 noro 192: {"flatten_quote",Pflatten_quote,-2},
1.60 noro 193: {"quote_to_funargs",Pquote_to_funargs,1},
1.48 noro 194: {"funargs_to_quote",Pfunargs_to_quote,1},
1.52 noro 195: {"get_function_name",Pget_function_name,1},
1.1 noro 196: {0,0,0},
197: };
1.13 noro 198:
1.14 noro 199: void write_tb(char *s,TB tb)
200: {
201: if ( tb->next == tb->size ) {
202: tb->size *= 2;
203: tb->body = (char **)REALLOC(tb->body,tb->size*sizeof(char *));
204: }
205: tb->body[tb->next] = s;
206: tb->next++;
207: }
1.13 noro 208:
1.18 noro 209: int register_symbol_table(Obj arg);
210: int register_conv_rule(Obj arg);
1.38 noro 211: int register_conv_func(Obj arg);
1.23 noro 212: int register_dp_vars(Obj arg);
1.46 noro 213: int register_dp_vars_origin(Obj arg);
214: int register_dp_dvars_origin(Obj arg);
215: int register_dp_dvars_prefix(Obj arg);
1.25 noro 216: int register_dp_vars_prefix(Obj arg);
1.31 noro 217: int register_dp_vars_hweyl(Obj arg);
1.27 noro 218: int register_show_lt(Obj arg);
1.40 noro 219: char *objtostr(Obj obj);
1.18 noro 220: static struct TeXSymbol *user_texsymbol;
1.23 noro 221: static char **dp_vars;
1.25 noro 222: static int dp_vars_len;
223: static char *dp_vars_prefix;
1.46 noro 224: static char *dp_dvars_prefix;
225: static int dp_vars_origin;
226: static int dp_dvars_origin;
1.27 noro 227: static int show_lt;
1.26 noro 228: static FUNC convfunc;
1.27 noro 229: static int is_lt;
1.28 noro 230: static int conv_flag;
1.31 noro 231: static int dp_vars_hweyl;
1.28 noro 232:
1.38 noro 233: #define CONV_TABLE (1U<<0)
234: #define CONV_SUBS (1U<<1)
235: #define CONV_DMODE (1U<<2)
1.18 noro 236:
237: static struct {
238: char *name;
239: Obj value;
240: int (*reg)();
241: } qtot_env[] = {
242: {"symbol_table",0,register_symbol_table},
243: {"conv_rule",0,register_conv_rule},
1.38 noro 244: {"conv_func",0,register_conv_func},
1.23 noro 245: {"dp_vars",0,register_dp_vars},
1.25 noro 246: {"dp_vars_prefix",0,register_dp_vars_prefix},
1.46 noro 247: {"dp_dvars_prefix",0,register_dp_dvars_prefix},
248: {"dp_vars_origin",0,register_dp_vars_origin},
249: {"dp_dvars_origin",0,register_dp_dvars_origin},
1.31 noro 250: {"dp_vars_hweyl",0,register_dp_vars_hweyl},
1.27 noro 251: {"show_lt",0,register_show_lt},
1.18 noro 252: {0,0,0},
253: };
254:
1.20 noro 255: #define PARTIAL "\\partial"
1.28 noro 256:
257: char *conv_rule(char *name)
258: {
259: char *body,*r;
1.41 noro 260: int len;
1.28 noro 261:
1.38 noro 262: if ( convfunc )
263: name = call_convfunc(name);
1.41 noro 264: if ( conv_flag & CONV_TABLE ) {
265: r = symbol_name(name);
266: if ( r ) return r;
267: }
268: if ( (conv_flag & CONV_DMODE) && *name == 'd' ) {
269: body = conv_rule(name+1);
270: r = MALLOC_ATOMIC((strlen(PARTIAL)+strlen(body)+5)*sizeof(char));
271: if ( !body || !(len=strlen(body)) )
272: strcpy(r,PARTIAL);
273: else if ( len == 1 )
274: sprintf(r,"%s_%s",PARTIAL,body);
275: else
276: sprintf(r,"%s_{%s}",PARTIAL,body);
277: return r;
278: } else
279: return conv_subs(name);
1.28 noro 280: }
281:
1.40 noro 282: int _is_delimiter(char c)
283: {
284: if ( (c == ' ' || c == '_' || c == ',') ) return 1;
285: else return 0;
286: }
287:
288: int _is_alpha(char c)
289: {
290: if ( isdigit(c) || c == '{' || _is_delimiter(c) ) return 0;
291: else return 1;
292: }
293:
1.38 noro 294: char *conv_subs(char *name)
1.19 noro 295: {
1.29 noro 296: int i,j,k,len,clen,slen,start,level;
1.41 noro 297: char *buf,*head,*r,*h,*brace,*buf_conv;
1.28 noro 298: char **subs;
299:
1.41 noro 300: if ( !name || !(len=strlen(name)) ) return "";
301: if ( !(conv_flag&CONV_SUBS) ) return name;
1.28 noro 302: subs = (char **)ALLOCA(len*sizeof(char* ));
1.32 noro 303: for ( i = 0, j = 0, start = i; ; j++ ) {
1.40 noro 304: while ( (i < len) && _is_delimiter(name[i]) ) i++;
1.32 noro 305: start = i;
1.28 noro 306: if ( i == len ) break;
1.29 noro 307: if ( name[i] == '{' ) {
308: for ( level = 1, i++; i < len && level; i++ ) {
309: if ( name[i] == '{' ) level++;
310: else if ( name[i] == '}' ) level--;
311: }
1.32 noro 312: slen = i-start;
1.37 noro 313: if ( slen >= 3 ) {
314: brace = (char *)ALLOCA((slen+1)*sizeof(char));
315: strncpy(brace,name+start+1,slen-2);
316: brace[slen-2] = 0;
1.38 noro 317: buf = conv_subs(brace);
1.37 noro 318: subs[j] = (char *)ALLOCA((strlen(buf)+3)*sizeof(char));
1.38 noro 319: if ( strlen(buf) == 1 )
320: strcpy(subs[j],buf);
321: else
322: sprintf(subs[j],"{%s}",buf);
1.37 noro 323: } else
324: subs[j] = "{}";
1.32 noro 325: } else {
326: if ( isdigit(name[i]) )
327: while ( i < len && isdigit(name[i]) ) i++;
328: else
1.40 noro 329: while ( i < len && _is_alpha(name[i]) ) i++;
1.32 noro 330: slen = i-start;
331: buf = (char *)ALLOCA((slen+1)*sizeof(char));
332: strncpy(buf,name+start,slen); buf[slen] = 0;
1.41 noro 333: buf_conv = symbol_name(buf);
334: subs[j] = buf_conv?buf_conv:buf;
1.32 noro 335: }
1.28 noro 336: }
1.32 noro 337: for ( k = 0, clen = 0; k < j; k++ ) clen += strlen(subs[k]);
338: /* {subs(0)}_{{subs(1)},...,{subs(j-1)}} => {}:j+1 _:1 ,:j-2 */
339: h = r = MALLOC_ATOMIC((clen+(j+1)*2+1+(j-2)+1)*sizeof(char));
340: if ( j == 1 )
341: sprintf(h,"{%s}",subs[0]);
1.28 noro 342: else {
1.38 noro 343: sprintf(h,"{%s}_{%s",subs[0],subs[1]);
1.28 noro 344: h += strlen(h);
1.32 noro 345: for ( k = 2; k < j; k++ ) {
1.38 noro 346: sprintf(h,",%s",subs[k]);
1.28 noro 347: h += strlen(h);
1.19 noro 348: }
1.28 noro 349: strcpy(h,"}");
1.20 noro 350: }
351: return r;
1.19 noro 352: }
353:
1.26 noro 354: char *call_convfunc(char *name)
355: {
356: STRING str,r;
357: NODE arg;
358:
359: MKSTR(str,name);
360: arg = mknode(1,str);
361: r = (STRING)bevalf(convfunc,arg);
362: if ( !r || OID(r) != O_STR )
363: error("call_convfunc : invalid result");
364: return BDY(r);
365: }
366:
1.18 noro 367: int register_symbol_table(Obj arg)
368: {
369: NODE n,t;
370: Obj b;
371: STRING a0,a1;
372: struct TeXSymbol *uts;
373: int i,len;
374:
375: /* check */
376: if ( !arg ) {
377: user_texsymbol = 0;
378: return 1;
379: }
380: if ( OID(arg) != O_LIST ) return 0;
381:
382: n = BDY((LIST)arg);
383: len = length(n);
384: uts = (struct TeXSymbol *)MALLOC((len+1)*sizeof(struct TeXSymbol));
385: for ( i = 0; n; n = NEXT(n), i++ ) {
386: b = (Obj)BDY(n);
387: if ( !b || OID(b) != O_LIST ) return 0;
388: t = BDY((LIST)b);
389: if ( !t || !NEXT(t) ) return 0;
390: a0 = (STRING)BDY(t);
391: a1 = (STRING)BDY(NEXT(t));
1.23 noro 392: if ( !a0 ) return 0;
393: if ( OID(a0) == O_STR )
394: uts[i].text = BDY(a0);
395: else if ( OID(a0) == O_P )
396: uts[i].text = NAME(VR((P)a0));
397: else
398: return 0;
399: if ( !a1 ) return 0;
400: if ( OID(a1) == O_STR )
401: uts[i].symbol = BDY(a1);
402: else if ( OID(a1) == O_P )
403: uts[i].symbol = NAME(VR((P)a1));
404: else
405: return 0;
1.18 noro 406: }
407: uts[i].text = 0;
408: uts[i].symbol = 0;
409: user_texsymbol = uts;
410: return 1;
411: }
412:
1.46 noro 413: int register_dp_vars_origin(Obj arg)
414: {
415: if ( INT(arg) ) {
416: dp_vars_origin = QTOS((Q)arg);
417: return 1;
418: } else return 0;
419: }
420:
421: int register_dp_dvars_origin(Obj arg)
422: {
423: if ( INT(arg) ) {
424: dp_dvars_origin = QTOS((Q)arg);
425: return 1;
426: } else return 0;
427: }
428:
1.31 noro 429: int register_dp_vars_hweyl(Obj arg)
430: {
431: if ( INT(arg) ) {
432: dp_vars_hweyl = QTOS((Q)arg);
433: return 1;
434: } else return 0;
435: }
436:
1.27 noro 437: int register_show_lt(Obj arg)
438: {
439: if ( INT(arg) ) {
440: show_lt = QTOS((Q)arg);
441: return 1;
442: } else return 0;
443: }
1.26 noro 444:
1.18 noro 445: int register_conv_rule(Obj arg)
446: {
1.19 noro 447: if ( INT(arg) ) {
1.28 noro 448: conv_flag = QTOS((Q)arg);
449: convfunc = 0;
450: return 1;
1.38 noro 451: } else return 0;
452: }
453:
454: int register_conv_func(Obj arg)
455: {
1.43 noro 456: if ( !arg ) {
457: convfunc = 0;
458: return 1;
459: } else if ( OID(arg) == O_P && (int)(VR((P)arg))->attr == V_SR ) {
1.26 noro 460: convfunc = (FUNC)(VR((P)arg)->priv);
461: /* f must be a function which takes single argument */
462: return 1;
1.19 noro 463: } else return 0;
1.18 noro 464: }
465:
1.23 noro 466: int register_dp_vars(Obj arg)
467: {
468: int l,i;
469: char **r;
470: NODE n;
471: STRING a;
472:
473: if ( !arg ) {
474: dp_vars = 0;
475: dp_vars_len = 0;
1.25 noro 476: return 1;
1.23 noro 477: } else if ( OID(arg) != O_LIST )
478: return 0;
479: else {
480: n = BDY((LIST)arg);
481: l = length(n);
482: r = (char **)MALLOC_ATOMIC(l*sizeof(char *));
483: for ( i = 0; i < l; i++, n = NEXT(n) ) {
484: a = (STRING)BDY(n);
485: if ( !a ) return 0;
486: if ( OID(a) == O_STR )
487: r[i] = BDY(a);
488: else if ( OID(a) == O_P )
489: r[i] = NAME(VR((P)a));
490: else
491: return 0;
492: }
493: dp_vars = r;
494: dp_vars_len = l;
495: return 1;
496: }
497: }
498:
1.25 noro 499: int register_dp_vars_prefix(Obj arg)
500: {
501: if ( !arg ) {
502: dp_vars_prefix = 0;
503: return 1;
504: } else if ( OID(arg) == O_STR ) {
505: dp_vars_prefix = BDY((STRING)arg);
506: return 1;
507: } else if ( OID(arg) == O_P ) {
508: dp_vars_prefix = NAME(VR((P)arg));
509: return 1;
510: } else return 0;
511: }
512:
1.46 noro 513: int register_dp_dvars_prefix(Obj arg)
514: {
515: if ( !arg ) {
516: dp_dvars_prefix = 0;
517: return 1;
518: } else if ( OID(arg) == O_STR ) {
519: dp_dvars_prefix = BDY((STRING)arg);
520: return 1;
521: } else if ( OID(arg) == O_P ) {
522: dp_dvars_prefix = NAME(VR((P)arg));
523: return 1;
524: } else return 0;
525: }
526:
1.24 noro 527: void Pquotetotex_env(NODE arg,Obj *rp)
1.18 noro 528: {
529: int ac,i;
530: char *name;
531: NODE n,n0;
532: STRING s;
533: LIST l;
534:
535: ac = argc(arg);
536: if ( !ac ) {
537: n0 = 0;
538: for ( i = 0; qtot_env[i].name; i++ ) {
539: NEXTNODE(n0,n); MKSTR(s,qtot_env[i].name); BDY(n) = (pointer)s;
540: NEXTNODE(n0,n); BDY(n) = (Q)qtot_env[i].value;
541: }
542: NEXT(n) = 0;
543: MKLIST(l,n0);
544: *rp = (Obj)l;
1.34 noro 545: } else if ( ac == 1 && !ARG0(arg) ) {
546: /* set to default */
1.43 noro 547: for ( i = 0; qtot_env[i].name; i++ ) {
548: (qtot_env[i].reg)(0);
1.34 noro 549: qtot_env[i].value = 0;
1.43 noro 550: }
1.34 noro 551: *rp = 0;
1.18 noro 552: } else if ( ac == 1 || ac == 2 ) {
1.24 noro 553: asir_assert(ARG0(arg),O_STR,"quotetotex_env");
1.18 noro 554: name = BDY((STRING)ARG0(arg));
555: for ( i = 0; qtot_env[i].name; i++ )
556: if ( !strcmp(qtot_env[i].name,name) ) {
557: if ( ac == 2 ) {
558: if ( (qtot_env[i].reg)((Obj)ARG1(arg)) )
559: qtot_env[i].value = (Obj)ARG1(arg);
560: else
1.24 noro 561: error("quotetotex_env : invalid argument");
1.18 noro 562: }
563: *rp = qtot_env[i].value;
564: return;
565: }
566: *rp = 0;
567: } else
568: *rp = 0;
569: }
570:
1.14 noro 571: void Pwrite_to_tb(NODE arg,Q *rp)
1.13 noro 572: {
573: int i;
1.16 noro 574: Obj obj;
575: TB tb;
1.13 noro 576:
1.14 noro 577: asir_assert(ARG1(arg),O_TB,"write_to_tb");
1.16 noro 578: obj = ARG0(arg);
579: if ( !obj )
580: write_tb("",ARG1(arg));
581: else if ( OID(obj) == O_STR )
582: write_tb(BDY((STRING)obj),ARG1(arg));
583: else if ( OID(obj) == O_TB ) {
584: tb = (TB)obj;
585: for ( i = 0; i < tb->next; i++ )
586: write_tb(tb->body[i],ARG1(arg));
587: }
1.14 noro 588: *rp = 0;
1.53 noro 589: }
590:
1.105 noro 591: void Pqt_to_nary(NODE arg,QUOTE *rp)
1.63 noro 592: {
593: FNODE f;
594:
1.70 noro 595: f = fnode_to_nary(BDY((QUOTE)ARG0(arg)));
1.63 noro 596: MKQUOTE(*rp,f);
597: }
598:
1.105 noro 599: void Pqt_to_bin(NODE arg,QUOTE *rp)
1.63 noro 600: {
601: FNODE f;
602: int direction;
603:
604: direction = QTOS((Q)ARG1(arg));
1.70 noro 605: f = fnode_to_bin(BDY((QUOTE)ARG0(arg)),direction);
1.63 noro 606:
607: MKQUOTE(*rp,f);
608: }
1.61 noro 609:
1.105 noro 610: void Pqt_is_number(NODE arg,Q *rp)
1.69 noro 611: {
612: QUOTE q;
613: int ret;
614:
615: q = (QUOTE)ARG0(arg);
1.105 noro 616: asir_assert(q,O_QUOTE,"qt_is_number");
1.69 noro 617: ret = fnode_is_number(BDY(q));
618: STOQ(ret,*rp);
619: }
620:
1.105 noro 621: void Pqt_is_rational(NODE arg,Q *rp)
1.69 noro 622: {
623: QUOTE q;
624: int ret;
625:
626: q = (QUOTE)ARG0(arg);
1.105 noro 627: asir_assert(q,O_QUOTE,"qt_is_rational");
1.69 noro 628: ret = fnode_is_rational(BDY(q));
629: STOQ(ret,*rp);
630: }
631:
1.105 noro 632: void Pqt_is_integer(NODE arg,Q *rp)
1.69 noro 633: {
634: QUOTE q;
635: int ret;
636:
637: q = (QUOTE)ARG0(arg);
1.105 noro 638: asir_assert(q,O_QUOTE,"qt_is_integer");
1.69 noro 639: ret = fnode_is_integer(BDY(q));
640: STOQ(ret,*rp);
641: }
642:
1.105 noro 643: void Pqt_is_function(NODE arg,Q *rp)
1.69 noro 644: {
645: QUOTE q;
646: int ret;
647:
648: q = (QUOTE)ARG0(arg);
1.105 noro 649: asir_assert(q,O_QUOTE,"qt_is_function");
1.69 noro 650: if ( q->id == I_FUNC || q->id == I_IFUNC )
651: ret = 1;
652: else
653: ret = 0;
654: STOQ(ret,*rp);
655: }
656:
1.105 noro 657: void Pqt_is_dependent(NODE arg,Q *rp)
1.69 noro 658: {
659: P x;
660: QUOTE q,v;
661: int ret;
662: V var;
663:
664: q = (QUOTE)ARG0(arg);
665: v = (QUOTE)ARG1(arg);
1.105 noro 666: asir_assert(q,O_QUOTE,"qt_is_dependent");
667: asir_assert(v,O_QUOTE,"qt_is_dependent");
1.69 noro 668: x = (P)eval(BDY(v));
669: if ( !x || OID(x) != O_P )
670: *rp = 0;
671: var = VR(x);
672: ret = fnode_is_dependent(BDY(q),var);
673: STOQ(ret,*rp);
674: }
675:
676:
1.105 noro 677: void Pqt_match(NODE arg,Q *rp)
1.53 noro 678: {
1.61 noro 679: FNODE f,g;
1.65 noro 680: Obj obj;
1.61 noro 681: QUOTE q;
1.53 noro 682: NODE r;
1.56 noro 683: int ret;
1.53 noro 684:
1.65 noro 685: obj = (Obj)ARG0(arg);
1.105 noro 686: ret = qt_match(obj,(Obj)ARG1(arg),&r);
1.57 noro 687: if ( ret ) {
688: do_assign(r);
689: *rp = ONE;
690: } else
1.56 noro 691: *rp = 0;
1.68 noro 692: }
693:
1.105 noro 694: void Pnqt_match(NODE arg,Q *rp)
1.88 noro 695: {
696: QUOTE fq,pq;
697: FNODE f,p;
698: int ret;
1.104 noro 699: Q mode;
1.88 noro 700: NODE r;
701:
1.104 noro 702: mode = argc(arg)==3 ? (Q)ARG2(arg) : 0;
1.105 noro 703: fq = (QUOTE)ARG0(arg); Pqt_normalize(mknode(2,fq,mode),&fq); f = (FNODE)BDY(fq);
704: pq = (QUOTE)ARG1(arg); Pqt_normalize(mknode(2,pq,mode),&pq); p = (FNODE)BDY(pq);
1.95 noro 705: ret = nfnode_match(f,p,&r);
1.88 noro 706: if ( ret ) {
707: fnode_do_assign(r);
708: *rp = ONE;
709: } else
710: *rp = 0;
711: }
712:
1.103 noro 713:
1.106 ! noro 714: FNODE rewrite_fnode(FNODE,NODE,int);
1.68 noro 715:
716: extern Obj VOIDobj;
717:
1.105 noro 718: void Pqt_match_rewrite(NODE arg,Obj *rp)
1.68 noro 719: {
720: FNODE f,g,h,c,value;
721: Obj obj;
722: QUOTE q;
723: NODE r,t,s,s0,pair;
724: int ret,ind,ac;
725:
726: obj = (Obj)ARG0(arg);
1.105 noro 727: ret = qt_match(obj,(Obj)ARG1(arg),&r);
1.68 noro 728: if ( ret ) {
729: for ( t = r, s0 = 0; t; t = NEXT(t) ) {
730: NEXTNODE(s0,s);
731: pair = BDY((LIST)BDY(t));
732: ind = (int)FA0((FNODE)BDY((QUOTE)BDY(pair)));
1.79 noro 733: value = BDY((QUOTE)(BDY(NEXT(pair))));
1.68 noro 734: BDY(s) = mknode(2,ind,value);
735: }
736: if ( s0 ) NEXT(s) = 0;
737: switch ( ac = argc(arg) ) {
738: case 3:
1.106 ! noro 739: h = rewrite_fnode(BDY((QUOTE)ARG2(arg)),s0,0);
1.79 noro 740: MKQUOTE(q,h); *rp = (Obj)q;
1.68 noro 741: break;
742: case 4:
1.106 ! noro 743: c = rewrite_fnode(BDY((QUOTE)ARG2(arg)),s0,0);
1.68 noro 744: if ( eval(c) ) {
1.106 ! noro 745: h = rewrite_fnode(BDY((QUOTE)ARG3(arg)),s0,0);
1.79 noro 746: MKQUOTE(q,h); *rp = (Obj)q;
1.68 noro 747: } else
748: *rp = VOIDobj;
749: break;
750: default:
1.105 noro 751: error("qt_match_rewrite : invalid argument");
1.68 noro 752: }
753: } else
754: *rp = VOIDobj;
1.56 noro 755: }
756:
1.106 ! noro 757: void Pnqt_match_rewrite(NODE arg,Obj *rp)
! 758: {
! 759: FNODE f,p,c,a,r;
! 760: Obj obj,pat,cond,action;
! 761: NODE rule;
! 762: QUOTE q;
! 763: Q mode;
! 764: int m;
! 765:
! 766: obj = (Obj)ARG0(arg);
! 767: rule = BDY((LIST)ARG1(arg));
! 768: mode = (Q)ARG2(arg);
! 769: if ( length(rule) == 2 ) {
! 770: pat = ARG0(rule);
! 771: cond = (Obj)ONE;
! 772: action = (Obj)ARG1(rule);
! 773: } else {
! 774: pat = ARG0(rule);
! 775: cond = ARG1(rule);
! 776: action = (Obj)ARG2(rule);
! 777: }
! 778: Pqt_normalize(mknode(2,obj,mode),&q); f = (FNODE)BDY(q);
! 779: Pqt_normalize(mknode(2,pat,mode),&q); p = (FNODE)BDY(q);
! 780: Pqt_normalize(mknode(2,action,mode),&q);
! 781: a = (FNODE)BDY(q);
! 782: if ( OID(cond) == O_QUOTE ) c = BDY((QUOTE)cond);
! 783: else c = mkfnode(1,I_FORMULA,ONE);
! 784:
! 785: m = QTOS(mode);
! 786: r = nfnode_match_rewrite(f,p,c,a,m);
! 787: if ( r ) {
! 788: MKQUOTE(q,r);
! 789: *rp = (Obj)q;
! 790: } else
! 791: *rp = obj;
! 792: }
! 793:
! 794: /* f is NARYOP => do submatch */
! 795:
! 796: #define PV_ANY 99999999
! 797:
! 798: FNODE nfnode_match_rewrite(FNODE f,FNODE p,FNODE c,FNODE a,int mode)
! 799: {
! 800: ARF op;
! 801: NODE arg,h0,t,h,valuen;
! 802: NODE r,s0,s,pair;
! 803: FNODE any,pany,head,tail,a1;
! 804: QUOTE q;
! 805: int ret;
! 806: FNODE value;
! 807: int ind;
! 808:
! 809: if ( f->id == I_NARYOP ) {
! 810: op = (ARF)FA0(f);
! 811: arg = (NODE)FA1(f);
! 812: pany = 0;
! 813: for ( h0 = 0, t = arg; t; t = NEXT(t) ) {
! 814: tail = fnode_node_to_nary(op,t);
! 815: ret = nfnode_match(tail,p,&r) && eval(rewrite_fnode(c,r,1));
! 816: if ( ret ) break;
! 817:
! 818: /* append a variable to the pattern */
! 819: if ( !pany ) {
! 820: any = mkfnode(1,I_PVAR,PV_ANY);
! 821: pany = mkfnode(3,I_BOP,op,p,any);
! 822: pany = fnode_normalize(pany,mode);
! 823: }
! 824: ret = nfnode_match(tail,pany,&r) && eval(rewrite_fnode(c,r,1));
! 825: if ( ret ) {
! 826: a = fnode_normalize(mkfnode(3,I_BOP,op,a,any),mode);
! 827: break;
! 828: }
! 829:
! 830: NEXTNODE(h0,h);
! 831: BDY(h) = BDY(t);
! 832: }
! 833: if ( t ) {
! 834: if ( h0 ) NEXT(h) = 0;
! 835: head = fnode_node_to_nary(op,h0);
! 836: a = fnode_normalize(mkfnode(3,I_BOP,op,head,a),mode);
! 837: ret = 1;
! 838: } else
! 839: ret = 0;
! 840: } else
! 841: ret = nfnode_match(f,p,&r) && eval(rewrite_fnode(c,r,1));
! 842:
! 843: if ( ret ) {
! 844: a1 = rewrite_fnode(a,r,1);
! 845: return fnode_normalize(partial_eval(a1),mode);
! 846: } else
! 847: return 0;
! 848: }
! 849:
1.56 noro 850: void do_assign(NODE arg)
851: {
852: NODE t,pair;
853: int pv;
854:
855: QUOTE value;
856:
857: for ( t = arg; t; t = NEXT(t) ) {
858: pair = BDY((LIST)BDY(t));
859: pv = (int)FA0((FNODE)BDY((QUOTE)BDY(pair)));
860: value = (QUOTE)(BDY(NEXT(pair)));
861: ASSPV(pv,value);
862: }
1.53 noro 863: }
864:
1.88 noro 865: /* [[index,fnode],...] */
866:
867: void fnode_do_assign(NODE arg)
868: {
869: NODE t,pair;
870: int pv;
871: FNODE f;
872: QUOTE value;
1.95 noro 873: QUOTEARG qa;
1.88 noro 874:
875: for ( t = arg; t; t = NEXT(t) ) {
876: pair = (NODE)BDY(t);
877: pv = (int)BDY(pair);
878: f = (FNODE)(BDY(NEXT(pair)));
1.96 noro 879: if ( f->id == I_FUNC_HEAD ) {
880: /* XXX : I_FUNC_HEAD is a dummy id to pass FUNC */
1.95 noro 881: MKQUOTEARG(qa,A_func,FA0(f));
882: value = (QUOTE)qa;
883: } else
884: MKQUOTE(value,f);
1.88 noro 885: ASSPV(pv,value);
886: }
887: }
888:
1.53 noro 889: /*
1.56 noro 890: /* consistency check and merge
891: */
1.53 noro 892:
1.56 noro 893: int merge_matching_node(NODE n,NODE a,NODE *rp)
1.53 noro 894: {
895: NODE ta,ba,tn,bn;
896: QUOTE pa,va,pn,vn;
897:
1.56 noro 898: if ( !n ) {
899: *rp = a;
900: return 1;
901: }
1.53 noro 902: for ( ta = a; ta; ta = NEXT(ta) ) {
903: ba = BDY((LIST)BDY(ta));
1.55 noro 904: if ( !ba ) continue;
1.53 noro 905: pa = (QUOTE)BDY(ba); va = (QUOTE)BDY(NEXT(ba));
906: for ( tn = n; tn; tn = NEXT(tn) ) {
907: bn = BDY((LIST)BDY(tn));
1.55 noro 908: if ( !bn ) continue;
1.53 noro 909: pn = (QUOTE)BDY(bn); vn = (QUOTE)BDY(NEXT(bn));
1.55 noro 910: if ( !compquote(CO,pa,pn) ) {
911: if ( !compquote(CO,va,vn) ) break;
912: else return 0;
913: }
1.53 noro 914: }
915: if ( !tn ) {
916: MKNODE(tn,(pointer)BDY(ta),n);
917: n = tn;
918: }
919: }
1.56 noro 920: *rp = n;
921: return 1;
1.53 noro 922: }
923:
1.105 noro 924: int qt_match_node(NODE f,NODE pat,NODE *rp) {
1.56 noro 925: NODE r,a,tf,tp,r1;
926: int ret;
1.53 noro 927:
928: if ( length(f) != length(pat) ) return 0;
929: r = 0;
930: for ( tf = f, tp = pat; tf; tf = NEXT(tf), tp = NEXT(tp) ) {
1.105 noro 931: ret = qt_match((Obj)BDY(tf),(Obj)BDY(tp),&a);
1.56 noro 932: if ( !ret ) return 0;
933: ret = merge_matching_node(r,a,&r1);
934: if ( !ret ) return 0;
935: else r = r1;
1.53 noro 936: }
1.56 noro 937: *rp = r;
938: return 1;
1.53 noro 939: }
940:
1.66 noro 941: /* f = [a,b,c,...] pat = [X,Y,...] rpat matches the rest of f */
942:
1.105 noro 943: int qt_match_cons(NODE f,NODE pat,Obj rpat,NODE *rp) {
1.66 noro 944: QUOTE q;
945: Q id;
946: FNODE fn;
947: NODE r,a,tf,tp,r1,arg;
948: int ret;
949: LIST list,alist;
950:
951: /* matching of the head part */
952: if ( length(f) < length(pat) ) return 0;
953: r = 0;
954: for ( tf = f, tp = pat; tp; tf = NEXT(tf), tp = NEXT(tp) ) {
1.105 noro 955: ret = qt_match((Obj)BDY(tf),(Obj)BDY(tp),&a);
1.66 noro 956: if ( !ret ) return 0;
957: ret = merge_matching_node(r,a,&r1);
958: if ( !ret ) return 0;
959: else r = r1;
960: }
961: /* matching of the rest */
962: MKLIST(list,tf);
963: STOQ(I_LIST,id); a = mknode(2,id,list);
964: MKLIST(alist,a);
965: arg = mknode(1,alist);
966: Pfunargs_to_quote(arg,&q);
1.105 noro 967: ret = qt_match((Obj)q,rpat,&a);
1.66 noro 968: if ( !ret ) return 0;
969: ret = merge_matching_node(r,a,&r1);
970: if ( !ret ) return 0;
971: *rp = r1;
972: return 1;
973: }
974:
1.53 noro 975: void get_quote_id_arg(QUOTE f,int *id,NODE *r)
976: {
977: LIST fa;
978: NODE arg,fab;
979:
980: arg = mknode(1,f); Pquote_to_funargs(arg,&fa); fab = BDY((LIST)fa);
981: *id = QTOS((Q)BDY(fab)); *r = NEXT(fab);
982: }
983:
1.56 noro 984: /* *rp : [[quote(A),quote(1)],...] */
1.53 noro 985:
1.105 noro 986: int qt_match(Obj f, Obj pat, NODE *rp)
1.53 noro 987: {
988: NODE tf,tp,head,body;
989: NODE parg,farg,r;
1.66 noro 990: Obj rpat;
1.53 noro 991: LIST fa,l;
992: int pid,id;
1.55 noro 993: FUNC ff,pf;
1.56 noro 994: int ret;
1.64 noro 995: QUOTE q;
996: FNODE g;
1.53 noro 997:
1.67 noro 998: if ( !f )
999: if ( !pat ) {
1000: *rp = 0; return 1;
1001: } else
1002: return 0;
1003: else if ( OID(pat) == O_LIST ) {
1.53 noro 1004: if ( OID(f) == O_LIST )
1.105 noro 1005: return qt_match_node(BDY((LIST)f),BDY((LIST)pat),rp);
1.53 noro 1006: else
1007: return 0;
1008: } else if ( OID(pat) == O_QUOTE ) {
1.67 noro 1009: pid = ((FNODE)BDY((QUOTE)pat))->id;
1.53 noro 1010: switch ( pid ) {
1.55 noro 1011: case I_FORMULA:
1012: if ( compquote(CO,f,pat) )
1013: return 0;
1014: else {
1.67 noro 1015: *rp = 0; return 1;
1.55 noro 1016: }
1017: break;
1.67 noro 1018:
1019: case I_LIST: case I_CONS:
1020: get_quote_id_arg((QUOTE)pat,&pid,&parg);
1021: if ( OID(f) == O_LIST )
1022: tf = BDY((LIST)f);
1023: else if ( OID(f) == O_QUOTE
1024: && ((FNODE)BDY((QUOTE)f))->id == pid ) {
1025: get_quote_id_arg((QUOTE)f,&id,&farg);
1026: tf = BDY((LIST)BDY(farg));
1027: } else
1028: return 0;
1029:
1.66 noro 1030: tp = BDY((LIST)BDY(parg));
1.67 noro 1031: if ( pid == I_LIST )
1.105 noro 1032: return qt_match_node(tf,tp,rp);
1.67 noro 1033: else {
1034: rpat = (Obj)BDY(NEXT(parg));
1.105 noro 1035: return qt_match_cons(tf,tp,rpat,rp);
1.67 noro 1036: }
1037:
1.53 noro 1038: case I_PVAR:
1039: /* [[pat,f]] */
1040: r = mknode(2,pat,f); MKLIST(l,r);
1.56 noro 1041: *rp = mknode(1,l);
1042: return 1;
1.67 noro 1043:
1.53 noro 1044: case I_IFUNC:
1045: /* F(X,Y,...) = ... */
1.67 noro 1046: get_quote_id_arg((QUOTE)f,&id,&farg);
1047: get_quote_id_arg((QUOTE)pat,&pid,&parg);
1.53 noro 1048: if ( id == I_FUNC ) {
1.54 noro 1049: r = mknode(2,BDY(parg),BDY(farg)); MKLIST(l,r);
1050: head = mknode(1,l);
1.105 noro 1051: ret = qt_match(BDY(NEXT(farg)),
1.56 noro 1052: BDY(NEXT(parg)),&body);
1053: if ( !ret ) return 0;
1054: else return merge_matching_node(head,body,rp);
1.53 noro 1055: } else
1056: return 0;
1.64 noro 1057:
1.67 noro 1058: case I_NARYOP: case I_BOP: case I_FUNC:
1.64 noro 1059: /* X+Y = ... */
1.67 noro 1060: /* f(...) = ... */
1061: if ( OID(f) != O_QUOTE ) return 0;
1062: id = ((FNODE)BDY((QUOTE)f))->id;
1063: if ( pid == I_FUNC )
1064: ;
1065: else {
1066: /* XXX converting to I_BOP */
1067: if ( pid == I_NARYOP ) {
1.70 noro 1068: g = fnode_to_bin(BDY((QUOTE)pat),1);
1.67 noro 1069: MKQUOTE(q,g); pat = (Obj)q;
1070: }
1071: if ( id == I_NARYOP ) {
1.70 noro 1072: g = fnode_to_bin(BDY((QUOTE)f),1);
1.67 noro 1073: MKQUOTE(q,g); f = (Obj)q;
1074: }
1075: }
1076: get_quote_id_arg((QUOTE)pat,&pid,&parg);
1077: get_quote_id_arg((QUOTE)f,&id,&farg);
1.64 noro 1078: if ( compqa(CO,BDY(farg),BDY(parg)) ) return 0;
1.105 noro 1079: return qt_match_node(NEXT(farg),NEXT(parg),rp);
1.64 noro 1080:
1.53 noro 1081: default:
1.67 noro 1082: if ( OID(f) != O_QUOTE ) return 0;
1083: id = ((FNODE)BDY((QUOTE)f))->id;
1084: if ( id != pid ) return 0;
1085: get_quote_id_arg((QUOTE)pat,&pid,&parg);
1086: get_quote_id_arg((QUOTE)f,&id,&farg);
1.105 noro 1087: return qt_match_node(farg,parg,rp);
1.53 noro 1088: }
1089: }
1.13 noro 1090: }
1091:
1.14 noro 1092: void Pquotetotex(NODE arg,STRING *rp)
1.13 noro 1093: {
1.14 noro 1094: TB tb;
1.13 noro 1095:
1.14 noro 1096: NEWTB(tb);
1.27 noro 1097: /* XXX for DP */
1098: is_lt = 1;
1.14 noro 1099: fnodetotex_tb(BDY((QUOTE)ARG0(arg)),tb);
1100: tb_to_string(tb,rp);
1.13 noro 1101: }
1102:
1.14 noro 1103: void Pquotetotex_tb(NODE arg,Q *rp)
1.13 noro 1104: {
1105: int i;
1.14 noro 1106: TB tb;
1.13 noro 1107:
1.14 noro 1108: asir_assert(ARG1(arg),O_TB,"quotetotex_tb");
1.27 noro 1109: /* XXX for DP */
1110: is_lt = 1;
1.14 noro 1111: fnodetotex_tb(BDY((QUOTE)ARG0(arg)),ARG1(arg));
1.13 noro 1112: *rp = 0;
1113: }
1114:
1.14 noro 1115: void Pstring_to_tb(NODE arg,TB *rp)
1116: {
1117: TB tb;
1118:
1119: asir_assert(ARG0(arg),O_STR,"string_to_tb");
1120: NEWTB(tb);
1121: tb->body[0] = BDY((STRING)ARG0(arg));
1122: tb->next++;
1123: *rp = tb;
1124: }
1125:
1126: void Ptb_to_string(NODE arg,STRING *rp)
1127: {
1128: TB tb;
1129:
1130: asir_assert(ARG0(arg),O_TB,"tb_to_string");
1131: tb = (TB)ARG0(arg);
1132: tb_to_string(tb,rp);
1133: }
1134:
1135: void tb_to_string(TB tb,STRING *rp)
1.13 noro 1136: {
1.14 noro 1137: int j,len;
1.13 noro 1138: char *all,*p,*q;
1139:
1.14 noro 1140: for ( j = 0, len = 0; j < tb->next; j++ )
1141: len += strlen(tb->body[j]);
1142: all = (char *)MALLOC_ATOMIC((len+1)*sizeof(char));
1143: for ( j = 0, p = all; j < tb->next; j++ )
1144: for ( q = tb->body[j]; *q; *p++ = *q++ );
1145: *p = 0;
1146: MKSTR(*rp,all);
1147: }
1148:
1149: void Pclear_tb(NODE arg,Q *rp)
1150: {
1151: TB tb;
1152: int j;
1153:
1154: asir_assert(ARG0(arg),O_TB,"clear_tb");
1155: tb = (TB)ARG0(arg);
1156: for ( j = 0; j < tb->next; j++ )
1157: tb->body[j] = 0;
1158: tb->next = 0;
1159: *rp = 0;
1.13 noro 1160: }
1.5 noro 1161:
1162: void Pstr_len(arg,rp)
1163: NODE arg;
1164: Q *rp;
1165: {
1.16 noro 1166: Obj obj;
1167: TB tb;
1168: int r,i;
1.5 noro 1169:
1.16 noro 1170: obj = (Obj)ARG0(arg);
1171: if ( !obj || (OID(obj) != O_STR && OID(obj) != O_TB) )
1172: error("str_len : invalid argument");
1173: if ( OID(obj) == O_STR)
1174: r = strlen(BDY((STRING)obj));
1175: else if ( OID(obj) == O_TB ) {
1176: tb = (TB)obj;
1177: for ( r = i = 0; i < tb->next; i++ )
1178: r += strlen(tb->body[i]);
1179: }
1.5 noro 1180: STOQ(r,*rp);
1181: }
1182:
1183: void Pstr_chr(arg,rp)
1184: NODE arg;
1185: Q *rp;
1186: {
1187: STRING str,terminator;
1188: Q start;
1189: char *p,*ind;
1190: int chr,spos,r;
1191:
1192: str = (STRING)ARG0(arg);
1193: start = (Q)ARG1(arg);
1194: terminator = (STRING)ARG2(arg);
1195: asir_assert(str,O_STR,"str_chr");
1196: asir_assert(start,O_N,"str_chr");
1197: asir_assert(terminator,O_STR,"str_chr");
1198: p = BDY(str);
1199: spos = QTOS(start);
1200: chr = BDY(terminator)[0];
1.8 noro 1201: if ( spos > (int)strlen(p) )
1.5 noro 1202: r = -1;
1203: else {
1204: ind = strchr(p+spos,chr);
1205: if ( ind )
1206: r = ind-p;
1207: else
1208: r = -1;
1209: }
1210: STOQ(r,*rp);
1211: }
1212:
1213: void Psub_str(arg,rp)
1214: NODE arg;
1215: STRING *rp;
1216: {
1217: STRING str;
1218: Q head,tail;
1219: char *p,*r;
1220: int spos,epos,len;
1221:
1222: str = (STRING)ARG0(arg);
1223: head = (Q)ARG1(arg);
1224: tail = (Q)ARG2(arg);
1225: asir_assert(str,O_STR,"sub_str");
1226: asir_assert(head,O_N,"sub_str");
1227: asir_assert(tail,O_N,"sub_str");
1228: p = BDY(str);
1229: spos = QTOS(head);
1230: epos = QTOS(tail);
1231: len = strlen(p);
1232: if ( (spos >= len) || (epos < spos) ) {
1233: *rp = 0; return;
1234: }
1235: if ( epos >= len )
1236: epos = len-1;
1237: len = epos-spos+1;
1238: r = (char *)MALLOC(len+1);
1239: strncpy(r,p+spos,len);
1240: r[len] = 0;
1241: MKSTR(*rp,r);
1242: }
1.3 noro 1243:
1244: void Pstrtoascii(arg,rp)
1245: NODE arg;
1246: LIST *rp;
1247: {
1248: STRING str;
1249: unsigned char *p;
1250: int len,i;
1251: NODE n,n1;
1252: Q q;
1253:
1254: str = (STRING)ARG0(arg);
1255: asir_assert(str,O_STR,"strtoascii");
1256: p = BDY(str);
1257: len = strlen(p);
1258: for ( i = len-1, n = 0; i >= 0; i-- ) {
1259: UTOQ((unsigned int)p[i],q);
1260: MKNODE(n1,q,n);
1261: n = n1;
1262: }
1263: MKLIST(*rp,n);
1264: }
1265:
1266: void Pasciitostr(arg,rp)
1267: NODE arg;
1268: STRING *rp;
1269: {
1270: LIST list;
1271: unsigned char *p;
1272: int len,i,j;
1273: NODE n;
1274: Q q;
1275:
1276: list = (LIST)ARG0(arg);
1277: asir_assert(list,O_LIST,"asciitostr");
1278: n = BDY(list);
1279: len = length(n);
1280: p = MALLOC_ATOMIC(len+1);
1281: for ( i = 0; i < len; i++, n = NEXT(n) ) {
1282: q = (Q)BDY(n);
1283: asir_assert(q,O_N,"asciitostr");
1284: j = QTOS(q);
1.4 noro 1285: if ( j >= 256 || j <= 0 )
1.3 noro 1286: error("asciitostr : argument out of range");
1287: p[i] = j;
1288: }
1289: p[i] = 0;
1290: MKSTR(*rp,(char *)p);
1291: }
1.1 noro 1292:
1293: void Peval_str(arg,rp)
1294: NODE arg;
1295: Obj *rp;
1296: {
1297: FNODE fnode;
1298: char *cmd;
1.10 ohara 1299: #if defined(PARI)
1.8 noro 1300: void recover(int);
1301:
1.1 noro 1302: recover(0);
1.11 saito 1303: # if !(PARI_VERSION_CODE > 131588)
1.1 noro 1304: if ( setjmp(environnement) ) {
1305: avma = top; recover(1);
1306: resetenv("");
1307: }
1.11 saito 1308: # endif
1.1 noro 1309: #endif
1310: cmd = BDY((STRING)ARG0(arg));
1.9 noro 1311: exprparse_create_var(0,cmd,&fnode);
1.1 noro 1312: *rp = eval(fnode);
1313: }
1314:
1315: void Prtostr(arg,rp)
1316: NODE arg;
1317: STRING *rp;
1318: {
1319: char *b;
1320: int len;
1321:
1.2 noro 1322: len = estimate_length(CO,ARG0(arg));
1.12 noro 1323: b = (char *)MALLOC_ATOMIC(len+1);
1.1 noro 1324: soutput_init(b);
1325: sprintexpr(CO,ARG0(arg));
1326: MKSTR(*rp,b);
1327: }
1328:
1329: void Pstrtov(arg,rp)
1330: NODE arg;
1331: P *rp;
1332: {
1.8 noro 1333: char *p;
1.1 noro 1334:
1335: p = BDY((STRING)ARG0(arg));
1336: #if 0
1337: if ( !islower(*p) )
1338: *rp = 0;
1339: else {
1340: for ( t = p+1; t && (isalnum(*t) || *t == '_'); t++ );
1341: if ( *t )
1342: *rp = 0;
1343: else
1344: makevar(p,rp);
1345: }
1346: #else
1347: makevar(p,rp);
1348: #endif
1.14 noro 1349: }
1350:
1.15 noro 1351: static struct TeXSymbol texsymbol[] = {
1352: {"sin","\\sin"},
1353: {"cos","\\cos"},
1354: {"tan","\\tan"},
1355: {"sinh","\\sinh"},
1356: {"cosh","\\cosh"},
1357: {"tanh","\\tanh"},
1358: {"exp","\\exp"},
1359: {"log","\\log"},
1360:
1361: /* Greek Letters (lower case) */
1362: {"alpha","\\alpha"},
1363: {"beta","\\beta"},
1364: {"gamma","\\gamma"},
1365: {"delta","\\delta"},
1366: {"epsilon","\\epsilon"},
1367: {"varepsilon","\\varepsilon"},
1368: {"zeta","\\zeta"},
1369: {"eta","\\eta"},
1370: {"theta","\\theta"},
1371: {"vartheta","\\vartheta"},
1372: {"iota","\\iota"},
1373: {"kappa","\\kappa"},
1374: {"lambda","\\lambda"},
1375: {"mu","\\mu"},
1376: {"nu","\\nu"},
1377: {"xi","\\xi"},
1378: {"pi","\\pi"},
1379: {"varpi","\\varpi"},
1380: {"rho","\\rho"},
1381: {"sigma","\\sigma"},
1382: {"varsigma","\\varsigma"},
1383: {"tau","\\tau"},
1384: {"upsilon","\\upsilon"},
1385: {"phi","\\phi"},
1386: {"varphi","\\varphi"},
1387: {"chi","\\chi"},
1388: {"omega","\\omega"},
1389:
1390: /* Greek Letters, (upper case) */
1391: {"ggamma","\\Gamma"},
1392: {"ddelta","\\Delta"},
1393: {"ttheta","\\Theta"},
1394: {"llambda","\\Lambda"},
1395: {"xxi","\\Xi"},
1396: {"ppi","\\Pi"},
1397: {"ssigma","\\Sigma"},
1398: {"uupsilon","\\Upsilon"},
1399: {"pphi","\\Phi"},
1400: {"ppsi","\\Psi"},
1401: {"oomega","\\Omega"},
1402:
1403: /* Our own mathematical functions */
1404: {"algebra_tensor","\\otimes"},
1405: {"base_where","{\\rm \\ where \\ }"},
1406: /* Mathematical constants */
1407: {"c_pi","\\pi"},
1408: {"c_i","\\sqrt{-1}"},
1409:
1410: /* Temporary */
1411: {0,0}
1412: };
1413:
1.14 noro 1414: char *symbol_name(char *name)
1415: {
1.15 noro 1416: int i;
1417:
1.41 noro 1418: if ( !name || strlen(name) == 0 )
1419: return "";
1.38 noro 1420: if ( !(conv_flag & CONV_TABLE) )
1421: return name;
1422:
1.18 noro 1423: if ( user_texsymbol )
1424: for ( i = 0; user_texsymbol[i].text; i++ )
1425: if ( !strcmp(user_texsymbol[i].text,name) )
1426: return user_texsymbol[i].symbol;
1.15 noro 1427: for ( i = 0; texsymbol[i].text; i++ )
1428: if ( !strcmp(texsymbol[i].text,name) )
1429: return texsymbol[i].symbol;
1.41 noro 1430: return 0;
1.52 noro 1431: }
1432:
1433: void Pget_function_name(NODE arg,STRING *rp)
1434: {
1435: QUOTEARG qa;
1436: ARF f;
1437: char *opname;
1438:
1439: qa = (QUOTEARG)BDY(arg);
1440: if ( !qa || OID(qa) != O_QUOTEARG || qa->type != A_arf )
1441: *rp = 0;
1442: else {
1443: f = (ARF)BDY(qa);
1444: opname = f->name;
1445: MKSTR(*rp,opname);
1446: }
1.14 noro 1447: }
1448:
1.51 noro 1449: FNODE strip_paren(FNODE);
1450:
1.14 noro 1451: void fnodetotex_tb(FNODE f,TB tb)
1452: {
1.91 noro 1453: NODE n,t,t0,args;
1.38 noro 1454: char vname[BUFSIZ],prefix[BUFSIZ];
1455: char *opname,*vname_conv,*prefix_conv;
1.14 noro 1456: Obj obj;
1.46 noro 1457: int i,len,allzero,elen,elen2,si;
1.40 noro 1458: C cplx;
1459: char *r;
1.91 noro 1460: FNODE fi,f2,f1;
1.14 noro 1461:
1462: write_tb(" ",tb);
1463: if ( !f ) {
1464: write_tb("0",tb);
1465: return;
1466: }
1467: switch ( f->id ) {
1468: /* unary operators */
1.23 noro 1469: case I_NOT:
1470: write_tb("\\neg (",tb);
1471: fnodetotex_tb((FNODE)FA0(f),tb);
1472: write_tb(")",tb);
1473: break;
1474: case I_PAREN:
1475: write_tb("(",tb);
1476: fnodetotex_tb((FNODE)FA0(f),tb);
1477: write_tb(")",tb);
1478: break;
1479: case I_MINUS:
1480: write_tb("-",tb);
1481: fnodetotex_tb((FNODE)FA0(f),tb);
1482: break;
1483:
1484: /* binary operators */
1485: /* arg list */
1486: /* I_AND, I_OR => FA0(f), FA1(f) */
1487: /* otherwise => FA1(f), FA2(f) */
1488: case I_BOP:
1489: opname = ((ARF)FA0(f))->name;
1.70 noro 1490: switch ( opname[0] ) {
1491: case '+':
1492: fnodetotex_tb((FNODE)FA1(f),tb);
1493: if ( !top_is_minus((FNODE)FA2(f)) ) write_tb(opname,tb);
1494: fnodetotex_tb((FNODE)FA2(f),tb);
1495: break;
1496: case '-':
1497: if ( FA1(f) ) fnodetotex_tb((FNODE)FA1(f),tb);
1498: write_tb(opname,tb);
1499: fnodetotex_tb((FNODE)FA2(f),tb);
1500: break;
1501: case '*':
1502: fnodetotex_tb((FNODE)FA1(f),tb);
1503: write_tb(" ",tb);
1504: /* XXX special care for DP */
1505: f2 = (FNODE)FA2(f);
1506: if ( f2->id == I_EV ) {
1507: n = (NODE)FA0(f2);
1508: for ( i = 0; n; n = NEXT(n), i++ ) {
1509: fi = (FNODE)BDY(n);
1510: if ( fi->id != I_FORMULA || FA0(fi) )
1511: break;
1512: }
1513: if ( n )
1514: fnodetotex_tb((FNODE)FA2(f),tb);
1515: } else
1.23 noro 1516: fnodetotex_tb((FNODE)FA2(f),tb);
1.70 noro 1517: break;
1518: case '/':
1519: write_tb("\\frac{",tb);
1520: fnodetotex_tb((FNODE)FA1(f),tb);
1521: write_tb("} {",tb);
1522: fnodetotex_tb((FNODE)FA2(f),tb);
1523: write_tb("}",tb);
1524: break;
1525: case '^':
1526: fnodetotex_tb((FNODE)FA1(f),tb);
1527: write_tb("^{",tb);
1528: fnodetotex_tb(strip_paren((FNODE)FA2(f)),tb);
1529: write_tb("} ",tb);
1530: break;
1531: case '%':
1532: fnodetotex_tb((FNODE)FA1(f),tb);
1533: write_tb(" {\\rm mod}\\, ",tb);
1.23 noro 1534: fnodetotex_tb((FNODE)FA2(f),tb);
1.70 noro 1535: break;
1536: default:
1537: error("invalid binary operator");
1538: break;
1539: }
1.23 noro 1540: break;
1.91 noro 1541: case I_NARYOP:
1542: args = (NODE)FA1(f);
1543: write_tb("(",tb);
1544: switch ( OPNAME(f) ) {
1545: case '+':
1546: fnodetotex_tb((FNODE)BDY(args),tb);
1547: for ( args = NEXT(args); args; args = NEXT(args) ) {
1548: write_tb("+",tb);
1549: fnodetotex_tb((FNODE)BDY(args),tb);
1550: }
1551: break;
1552: case '*':
1553: f1 = (FNODE)BDY(args);
1554: if ( f1->id == I_FORMULA && MUNIQ(FA0(f1)) )
1555: write_tb("-",tb);
1556: else
1557: fnodetotex_tb(f1,tb);
1558: write_tb(" ",tb);
1559: for ( args = NEXT(args); args; args = NEXT(args) ) {
1560: /* XXX special care for DP */
1561: f2 = (FNODE)BDY(args);
1562: if ( f2->id == I_EV ) {
1563: n = (NODE)FA0(f2);
1564: for ( i = 0; n; n = NEXT(n), i++ ) {
1565: fi = (FNODE)BDY(n);
1566: if ( fi->id != I_FORMULA || FA0(fi) )
1567: break;
1568: }
1569: if ( n )
1570: fnodetotex_tb(f2,tb);
1571: } else
1572: fnodetotex_tb(f2,tb);
1573: }
1574: break;
1575: default:
1576: error("invalid nary op");
1577: break;
1578: }
1579: write_tb(")",tb);
1580: break;
1.23 noro 1581:
1582: case I_COP:
1583: switch( (cid)FA0(f) ) {
1584: case C_EQ:
1585: fnodetotex_tb((FNODE)FA1(f),tb);
1586: write_tb(" = ",tb);
1587: fnodetotex_tb((FNODE)FA2(f),tb);
1588: break;
1589: case C_NE:
1590: fnodetotex_tb((FNODE)FA1(f),tb);
1591: write_tb(" \\neq ",tb);
1592: fnodetotex_tb((FNODE)FA2(f),tb);
1593: break;
1594: case C_GT:
1595: fnodetotex_tb((FNODE)FA1(f),tb);
1.45 noro 1596: write_tb(" > ",tb);
1.23 noro 1597: fnodetotex_tb((FNODE)FA2(f),tb);
1598: break;
1599: case C_LT:
1600: fnodetotex_tb((FNODE)FA1(f),tb);
1.45 noro 1601: write_tb(" < ",tb);
1.23 noro 1602: fnodetotex_tb((FNODE)FA2(f),tb);
1.14 noro 1603: break;
1.23 noro 1604: case C_GE:
1605: fnodetotex_tb((FNODE)FA1(f),tb);
1606: write_tb(" \\geq ",tb);
1607: fnodetotex_tb((FNODE)FA2(f),tb);
1.14 noro 1608: break;
1.23 noro 1609: case C_LE:
1610: fnodetotex_tb((FNODE)FA1(f),tb);
1611: write_tb(" \\leq ",tb);
1612: fnodetotex_tb((FNODE)FA2(f),tb);
1.14 noro 1613: break;
1614: }
1615: break;
1616:
1.23 noro 1617: case I_LOP:
1618: switch( (lid)FA0(f) ) {
1619: case L_EQ:
1620: fnodetotex_tb((FNODE)FA1(f),tb);
1621: write_tb(" = ",tb);
1622: fnodetotex_tb((FNODE)FA2(f),tb);
1623: break;
1624: case L_NE:
1625: fnodetotex_tb((FNODE)FA1(f),tb);
1626: write_tb(" \\neq ",tb);
1627: fnodetotex_tb((FNODE)FA2(f),tb);
1628: break;
1629: case L_GT:
1630: fnodetotex_tb((FNODE)FA1(f),tb);
1.45 noro 1631: write_tb(" > ",tb);
1.23 noro 1632: fnodetotex_tb((FNODE)FA2(f),tb);
1633: break;
1634: case L_LT:
1635: fnodetotex_tb((FNODE)FA1(f),tb);
1.45 noro 1636: write_tb(" < ",tb);
1.23 noro 1637: fnodetotex_tb((FNODE)FA2(f),tb);
1638: break;
1639: case L_GE:
1640: fnodetotex_tb((FNODE)FA1(f),tb);
1641: write_tb(" \\geq ",tb);
1642: fnodetotex_tb((FNODE)FA2(f),tb);
1.14 noro 1643: break;
1.23 noro 1644: case L_LE:
1645: fnodetotex_tb((FNODE)FA1(f),tb);
1646: write_tb(" \\leq ",tb);
1647: fnodetotex_tb((FNODE)FA2(f),tb);
1.14 noro 1648: break;
1.23 noro 1649: case L_AND:
1650: fnodetotex_tb((FNODE)FA1(f),tb);
1.14 noro 1651: write_tb(" {\\rm \\ and\\ } ",tb);
1.23 noro 1652: fnodetotex_tb((FNODE)FA2(f),tb);
1653: break;
1654: case L_OR:
1.14 noro 1655: fnodetotex_tb((FNODE)FA1(f),tb);
1.23 noro 1656: write_tb(" {\\rm \\ or\\ } ",tb);
1657: fnodetotex_tb((FNODE)FA2(f),tb);
1.14 noro 1658: break;
1.23 noro 1659: case L_NOT:
1660: /* XXX : L_NOT is a unary operator */
1661: write_tb("\\neg (",tb);
1.14 noro 1662: fnodetotex_tb((FNODE)FA1(f),tb);
1.23 noro 1663: write_tb(")",tb);
1664: return;
1.14 noro 1665: }
1666: break;
1667:
1.23 noro 1668: case I_AND:
1669: fnodetotex_tb((FNODE)FA0(f),tb);
1670: write_tb(" {\\rm \\ and\\ } ",tb);
1671: fnodetotex_tb((FNODE)FA1(f),tb);
1672: break;
1673:
1674: case I_OR:
1675: fnodetotex_tb((FNODE)FA0(f),tb);
1676: write_tb(" {\\rm \\ or\\ } ",tb);
1677: fnodetotex_tb((FNODE)FA1(f),tb);
1678: break;
1679:
1.14 noro 1680: /* ternary operators */
1681: case I_CE:
1682: error("fnodetotex_tb : not implemented yet");
1683: break;
1684:
1685: /* lists */
1686: case I_LIST:
1687: write_tb(" [ ",tb);
1688: n = (NODE)FA0(f);
1689: fnodenodetotex_tb(n,tb);
1690: write_tb("]",tb);
1691: break;
1692:
1693: /* function */
1.23 noro 1694: case I_FUNC:
1.40 noro 1695: if ( !strcmp(((FUNC)FA0(f))->name,"@pi") )
1696: write_tb("\\pi",tb);
1697: else if ( !strcmp(((FUNC)FA0(f))->name,"@e") )
1698: write_tb("e",tb);
1699: else {
1700: opname = conv_rule(((FUNC)FA0(f))->name);
1701: write_tb(opname,tb);
1702: write_tb("(",tb);
1703: fargstotex_tb(opname,FA1(f),tb);
1704: write_tb(")",tb);
1705: }
1.23 noro 1706: break;
1707:
1708: /* XXX */
1709: case I_CAR:
1.28 noro 1710: opname = conv_rule("car");
1.23 noro 1711: write_tb(opname,tb);
1712: write_tb("(",tb);
1713: fargstotex_tb(opname,FA0(f),tb);
1714: write_tb(")",tb);
1715: break;
1716:
1717: case I_CDR:
1.28 noro 1718: opname = conv_rule("cdr");
1.23 noro 1719: write_tb(opname,tb);
1720: write_tb("(",tb);
1721: fargstotex_tb(opname,FA0(f),tb);
1722: write_tb(")",tb);
1723: break;
1724:
1725: /* exponent vector */
1726: case I_EV:
1727: n = (NODE)FA0(f);
1.31 noro 1728: if ( dp_vars_hweyl ) {
1729: elen = length(n);
1730: elen2 = elen>>1;
1731: elen = elen2<<1;
1732: }
1.23 noro 1733: allzero = 1;
1.27 noro 1734: if ( show_lt && is_lt )
1735: write_tb("\\underline{",tb);
1.23 noro 1736: for ( t0 = 0, i = 0; n; n = NEXT(n), i++ ) {
1737: fi = (FNODE)BDY(n);
1738: if ( fi->id == I_FORMULA && !FA0(fi) ) continue;
1739: allzero = 0;
1.38 noro 1740: if ( dp_vars && i < dp_vars_len ) {
1741: strcpy(vname,dp_vars[i]);
1742: vname_conv = conv_rule(vname);
1743: } else {
1744: if ( dp_vars_hweyl ) {
1.44 noro 1745: if ( i < elen2 ) {
1.38 noro 1746: strcpy(prefix,dp_vars_prefix?dp_vars_prefix:"x");
1.44 noro 1747: prefix_conv = conv_rule(prefix);
1748: vname_conv = (char *)ALLOCA(strlen(prefix_conv)+50);
1.46 noro 1749: si = i+dp_vars_origin;
1750: sprintf(vname_conv,(si>=0&&si<10)?"%s_%d":"%s_{%d}",
1751: prefix_conv,si);
1.44 noro 1752: } else if ( i < elen ) {
1.46 noro 1753: strcpy(prefix,
1754: dp_dvars_prefix?dp_dvars_prefix:"\\partial");
1.44 noro 1755: prefix_conv = conv_rule(prefix);
1756: vname_conv = (char *)ALLOCA(strlen(prefix_conv)+50);
1.46 noro 1757: si = i+dp_dvars_origin-elen2;
1758: sprintf(vname_conv,(si>=0&&si<10)?"%s_%d":"%s_{%d}",
1759: prefix_conv,si);
1.44 noro 1760: } else {
1.38 noro 1761: strcpy(prefix,"h");
1.44 noro 1762: vname_conv = conv_rule(prefix);
1763: }
1764: } else {
1.38 noro 1765: strcpy(prefix,dp_vars_prefix?dp_vars_prefix:"x");
1.44 noro 1766: prefix_conv = conv_rule(prefix);
1767: vname_conv = (char *)ALLOCA(strlen(prefix_conv)+50);
1.46 noro 1768: si = i+dp_vars_origin;
1769: sprintf(vname_conv,(si>=0&&si<10)?"%s_%d":"%s_{%d}",
1770: prefix_conv,si);
1.44 noro 1771: }
1.38 noro 1772: }
1.23 noro 1773: if ( fi->id == I_FORMULA && UNIQ(FA0(fi)) ) {
1774: len = strlen(vname_conv);
1775: opname = MALLOC_ATOMIC(len+2);
1776: sprintf(opname,"%s ",vname_conv);
1.14 noro 1777: write_tb(opname,tb);
1.23 noro 1778: } else {
1779: len = strlen(vname_conv);
1780: /* 2: ^{ */
1781: opname = MALLOC_ATOMIC(len+1+2);
1782: sprintf(opname,"%s^{",vname_conv);
1.14 noro 1783: write_tb(opname,tb);
1.23 noro 1784: fnodetotex_tb((FNODE)BDY(n),tb);
1785: write_tb("} ",tb);
1786: }
1.14 noro 1787: }
1.23 noro 1788: /* XXX */
1789: if ( allzero )
1790: write_tb(" 1 ",tb);
1.27 noro 1791: if ( show_lt && is_lt ) {
1792: write_tb("}",tb);
1793: is_lt = 0;
1794: }
1.14 noro 1795: break;
1796:
1.23 noro 1797: /* string */
1.14 noro 1798: case I_STR:
1799: write_tb((char *)FA0(f),tb);
1800: break;
1801:
1.23 noro 1802: /* internal object */
1.14 noro 1803: case I_FORMULA:
1804: obj = (Obj)FA0(f);
1.40 noro 1805: if ( !obj )
1.42 noro 1806: write_tb("0",tb);
1.40 noro 1807: else if ( OID(obj) == O_N && NID(obj) == N_C ) {
1808: cplx = (C)obj;
1809: write_tb("(",tb);
1810: if ( cplx->r ) {
1811: r = objtostr((Obj)cplx->r); write_tb(r,tb);
1812: }
1813: if ( cplx->i ) {
1814: if ( cplx->r && compnum(0,cplx->i,0) > 0 ) {
1815: write_tb("+",tb);
1816: if ( !UNIQ(cplx->i) ) {
1817: r = objtostr((Obj)cplx->i); write_tb(r,tb);
1818: }
1819: } else if ( MUNIQ(cplx->i) )
1820: write_tb("-",tb);
1821: else if ( !UNIQ(cplx->i) ) {
1822: r = objtostr((Obj)cplx->i); write_tb(r,tb);
1823: }
1824: write_tb("\\sqrt{-1}",tb);
1825: }
1826: write_tb(")",tb);
1827: } else if ( OID(obj) == O_P )
1828: write_tb(conv_rule(VR((P)obj)->name),tb);
1829: else
1830: write_tb(objtostr(obj),tb);
1.14 noro 1831: break;
1832:
1.23 noro 1833: /* program variable */
1.14 noro 1834: case I_PVAR:
1835: if ( FA1(f) )
1836: error("fnodetotex_tb : not implemented yet");
1837: GETPVNAME(FA0(f),opname);
1838: write_tb(opname,tb);
1839: break;
1840:
1841: default:
1842: error("fnodetotex_tb : not implemented yet");
1843: }
1.40 noro 1844: }
1845:
1846: char *objtostr(Obj obj)
1847: {
1848: int len;
1849: char *r;
1850:
1851: len = estimate_length(CO,obj);
1852: r = (char *)MALLOC_ATOMIC(len+1);
1853: soutput_init(r);
1854: sprintexpr(CO,obj);
1855: return r;
1.50 ohara 1856: }
1857:
1858: void Psprintf(NODE arg,STRING *rp)
1859: {
1860: STRING string;
1861: char *s,*t,*r;
1862: int argc,n,len;
1863: NODE node;
1864:
1865: string = (STRING)ARG0(arg);
1866: asir_assert(string,O_STR,"sprintf");
1867: s = BDY(string);
1868: for(n = 0, t = s; *t; t++) {
1869: if (*t=='%' && *(t+1)=='a') {
1870: n++;
1871: }
1872: }
1873: for(node = NEXT(arg), argc = 0, len = strlen(s); node; node = NEXT(node), argc++) {
1874: len += estimate_length(CO,BDY(node));
1875: }
1876: if (argc < n) {
1877: error("sprintf: invalid argument");
1878: }
1879: r = (char *)MALLOC_ATOMIC(len);
1880: for(node = NEXT(arg), t = r; *s; s++) {
1881: if (*s=='%' && *(s+1)=='a') {
1882: strcpy(t,objtostr(BDY(node)));
1883: node = NEXT(node);
1884: t = strchr(t,0);
1885: s++;
1886: }else {
1887: *t++ = *s;
1888: }
1889: }
1890: *t = 0;
1891: MKSTR(*rp,r);
1.14 noro 1892: }
1893:
1894: void fnodenodetotex_tb(NODE n,TB tb)
1895: {
1896: for ( ; n; n = NEXT(n) ) {
1.27 noro 1897: is_lt = 1;
1.14 noro 1898: fnodetotex_tb((FNODE)BDY(n),tb);
1899: if ( NEXT(n) ) write_tb(", ",tb);
1900: }
1901: }
1902:
1903: void fargstotex_tb(char *name,FNODE f,TB tb)
1904: {
1905: NODE n;
1906:
1907: if ( !strcmp(name,"matrix") ) {
1908: error("fargstotex_tb : not implemented yet");
1909: } else if ( !strcmp(name,"vector") ) {
1910: error("fargstotex_tb : not implemented yet");
1911: } else {
1912: if ( f->id == I_LIST ) {
1913: n = (NODE)FA0(f);
1914: fnodenodetotex_tb(n,tb);
1915: } else
1916: fnodetotex_tb(f,tb);
1.35 noro 1917: }
1918: }
1919:
1920: int top_is_minus(FNODE f)
1921: {
1922: char *opname;
1923: int len;
1924: Obj obj;
1925:
1926: if ( !f )
1927: return 0;
1928: switch ( f->id ) {
1929: case I_MINUS:
1930: return 1;
1931: case I_BOP:
1932: opname = ((ARF)FA0(f))->name;
1933: switch ( opname[0] ) {
1934: case '+': case '*': case '/': case '^': case '%':
1935: return top_is_minus((FNODE)FA1(f));
1936: case '-':
1937: if ( FA1(f) )
1938: return top_is_minus((FNODE)FA1(f));
1939: else
1940: return 1;
1941: default:
1942: return 0;
1943: }
1944: break;
1945: case I_COP:
1946: return top_is_minus((FNODE)FA1(f));
1947: case I_LOP:
1948: if ( (lid)FA0(f) == L_NOT ) return 0;
1949: else return top_is_minus((FNODE)FA1(f));
1950: case I_AND: case I_OR:
1951: return top_is_minus((FNODE)FA0(f));
1952: case I_FORMULA:
1953: obj = (Obj)FA0(f);
1.36 noro 1954: if ( !obj )
1955: return 0;
1956: else {
1957: switch ( OID(obj) ) {
1958: case O_N:
1959: return mmono((P)obj);
1960: case O_P:
1961: /* must be a variable */
1962: opname = conv_rule(VR((P)obj)->name);
1963: return opname[0]=='-';
1964: default:
1965: /* ??? */
1966: len = estimate_length(CO,obj);
1967: opname = (char *)MALLOC_ATOMIC(len+1);
1968: soutput_init(opname);
1969: sprintexpr(CO,obj);
1970: return opname[0]=='-';
1971: }
1.35 noro 1972: }
1.91 noro 1973: case I_NARYOP:
1974: return top_is_minus((FNODE)BDY((NODE)FA1(f)));
1975:
1.35 noro 1976: default:
1977: return 0;
1.14 noro 1978: }
1.47 noro 1979: }
1980:
1981: FNODE flatten_fnode(FNODE,char *);
1982:
1.48 noro 1983: void Pflatten_quote(NODE arg,Obj *rp)
1.47 noro 1984: {
1985: FNODE f;
1986: QUOTE q;
1987:
1.48 noro 1988: if ( !ARG0(arg) || OID((Obj)ARG0(arg)) != O_QUOTE )
1989: *rp = (Obj)ARG0(arg);
1.65 noro 1990: else if ( argc(arg) == 1 ) {
1991: f = flatten_fnode(BDY((QUOTE)ARG0(arg)),"+");
1992: f = flatten_fnode(f,"*");
1993: MKQUOTE(q,f);
1994: *rp = (Obj)q;
1995: } else {
1.48 noro 1996: f = flatten_fnode(BDY((QUOTE)ARG0(arg)),BDY((STRING)ARG1(arg)));
1997: MKQUOTE(q,f);
1998: *rp = (Obj)q;
1999: }
1.63 noro 2000: }
2001:
2002: void Pget_quote_id(NODE arg,Q *rp)
2003: {
2004: FNODE f;
2005: QUOTE q;
2006:
2007: q = (QUOTE)ARG0(arg);
2008: if ( !q || OID(q) != O_QUOTE )
2009: error("get_quote_id : invalid argument");
2010: f = BDY(q);
2011: STOQ((int)f->id,*rp);
1.48 noro 2012: }
2013:
2014: void Pquote_to_funargs(NODE arg,LIST *rp)
2015: {
2016: fid_spec_p spec;
2017: QUOTE q;
2018: QUOTEARG qa;
2019: FNODE f;
2020: STRING s;
2021: QUOTE r;
2022: int i;
2023: Q id,a;
1.49 noro 2024: LIST l;
2025: NODE t0,t,w,u,u0;
1.48 noro 2026:
2027: q = (QUOTE)ARG0(arg);
2028: if ( !q || OID(q) != O_QUOTE )
2029: error("quote_to_funargs : invalid argument");
2030: f = BDY(q);
2031: if ( !f ) {
2032: MKLIST(*rp,0);
2033: return;
2034: }
2035: get_fid_spec(f->id,&spec);
2036: if ( !spec )
2037: error("quote_to_funargs : not supported yet");
2038: t0 = 0;
2039: STOQ((int)f->id,id);
2040: NEXTNODE(t0,t);
2041: BDY(t) = (pointer)id;
2042: for ( i = 0; spec->type[i] != A_end; i++ ) {
2043: NEXTNODE(t0,t);
2044: switch ( spec->type[i] ) {
2045: case A_fnode:
2046: MKQUOTE(r,(FNODE)f->arg[i]);
2047: BDY(t) = (pointer)r;
2048: break;
2049: case A_int:
2050: STOQ((int)f->arg[i],a);
2051: BDY(t) = (pointer)a;
2052: break;
2053: case A_str:
2054: MKSTR(s,(char *)f->arg[i]);
2055: BDY(t) = (pointer)s;
2056: break;
2057: case A_internal:
2058: BDY(t) = (pointer)f->arg[i];
2059: break;
1.49 noro 2060: case A_node:
2061: w = (NODE)f->arg[i];
2062: for ( u0 = 0; w; w = NEXT(w) ){
2063: NEXTNODE(u0,u);
2064: MKQUOTE(r,(FNODE)BDY(w));
2065: BDY(u) = (pointer)r;
2066: }
2067: if ( u0 ) NEXT(u) = 0;
2068: MKLIST(l,u0);
2069: BDY(t) = (pointer)l;
2070: break;
1.48 noro 2071: default:
2072: MKQUOTEARG(qa,spec->type[i],f->arg[i]);
2073: BDY(t) = (pointer)qa;
2074: break;
2075: }
2076: }
2077: if ( t0 ) NEXT(t) = 0;
2078: MKLIST(*rp,t0);
2079: }
2080:
2081: void Pfunargs_to_quote(NODE arg,QUOTE *rp)
2082: {
2083: fid_spec_p spec;
2084: QUOTE q;
2085: QUOTEARG qa;
2086: FNODE f;
2087: STRING s;
1.49 noro 2088: QUOTE r,b;
1.48 noro 2089: int i;
2090: LIST l;
2091: fid id;
2092: Obj a;
1.49 noro 2093: NODE t0,t,u0,u,w;
1.48 noro 2094:
2095: l = (LIST)ARG0(arg);
2096: if ( !l || OID(l) != O_LIST || !(t=BDY(l)) )
2097: error("funargs_to_quote : invalid argument");
2098: t = BDY(l);
2099: id = (fid)QTOS((Q)BDY(t)); t = NEXT(t);
2100: get_fid_spec(id,&spec);
2101: if ( !spec )
2102: error("funargs_to_quote : not supported yet");
2103: for ( i = 0; spec->type[i] != A_end; i++ );
2104: NEWFNODE(f,i);
2105: f->id = id;
2106: for ( i = 0; spec->type[i] != A_end; i++, t = NEXT(t) ) {
2107: if ( !t )
2108: error("funargs_to_quote : argument mismatch");
2109: a = (Obj)BDY(t);
2110: switch ( spec->type[i] ) {
2111: case A_fnode:
2112: if ( !a || OID(a) != O_QUOTE )
2113: error("funargs_to_quote : invalid argument");
2114: f->arg[i] = BDY((QUOTE)a);
2115: break;
2116: case A_int:
2117: if ( !INT(a) )
2118: error("funargs_to_quote : invalid argument");
2119: f->arg[i] = (pointer)QTOS((Q)a);
2120: break;
2121: case A_str:
2122: if ( !a || OID(a) != O_STR )
2123: error("funargs_to_quote : invalid argument");
2124: f->arg[i] = (pointer)BDY((STRING)a);
2125: break;
2126: case A_internal:
2127: f->arg[i] = (pointer)a;
1.49 noro 2128: break;
2129: case A_node:
2130: if ( !a || OID(a) != O_LIST )
2131: error("funargs_to_quote : invalid argument");
2132: u0 = 0;
2133: for ( w = BDY((LIST)a); w; w = NEXT(w) ) {
2134: NEXTNODE(u0,u);
2135: b = (QUOTE)BDY(w);
2136: if ( !b || OID(b) != O_QUOTE )
2137: error("funargs_to_quote : invalid argument");
2138: BDY(u) = BDY(b);
2139: }
2140: if ( u0 ) NEXT(u) = 0;
2141: f->arg[i] = (pointer)u0;
1.48 noro 2142: break;
2143: default:
2144: if ( !a || OID(a) != O_QUOTEARG ||
2145: ((QUOTEARG)a)->type != spec->type[i] )
2146: error("funargs_to_quote : invalid argument");
2147: f->arg[i] = BDY((QUOTEARG)a);
2148: break;
2149: }
2150: }
2151: MKQUOTE(*rp,f);
1.69 noro 2152: }
2153:
1.105 noro 2154: VL reordvars(VL vl0,NODE head)
2155: {
2156: VL vl,svl,tvl;
2157: int i,j;
2158: NODE n;
2159: P t;
2160: V *va;
2161: V v;
2162:
2163: for ( vl = 0, i = 0, n = head; n; n = NEXT(n), i++ ) {
2164: NEXTVL(vl,tvl);
2165: if ( !(t = (P)BDY(n)) || (OID(t) != O_P) )
2166: error("reordvars : invalid argument");
2167: VR(tvl) = VR(t);
2168: }
2169: va = (V *)ALLOCA(i*sizeof(V));
2170: for ( j = 0, svl = vl; j < i; j++, svl = NEXT(svl) )
2171: va[j] = VR(svl);
2172: for ( svl = vl0; svl; svl = NEXT(svl) ) {
2173: v = VR(svl);
2174: for ( j = 0; j < i; j++ )
2175: if ( v == va[j] )
2176: break;
2177: if ( j == i ) {
2178: NEXTVL(vl,tvl);
2179: VR(tvl) = v;
2180: }
2181: }
2182: if ( vl )
2183: NEXT(tvl) = 0;
2184: return vl;
2185: }
2186:
2187: VL qt_current_ord;
2188: LIST qt_current_ord_obj;
2189:
2190: void Pqt_set_ord(NODE arg,LIST *rp)
2191: {
2192: NODE r0,r;
2193: VL vl;
2194: P v;
2195:
2196: if ( !argc(arg) )
2197: *rp = qt_current_ord_obj;
2198: else {
2199: qt_current_ord = reordvars(CO,BDY((LIST)ARG0(arg)));
2200: for ( r0 = 0, vl = qt_current_ord; vl; vl = NEXT(vl) ) {
2201: NEXTNODE(r0,r); MKV(vl->v,v); BDY(r) = v;
2202: }
2203: if ( r0 ) NEXT(r) = 0;
2204: MKLIST(*rp,r0);
2205: qt_current_ord_obj = *rp;
2206: }
2207: }
2208:
2209: void Pqt_normalize(NODE arg,QUOTE *rp)
1.70 noro 2210: {
2211: QUOTE q,r;
2212: FNODE f;
1.76 noro 2213: int expand,ac;
1.70 noro 2214:
1.76 noro 2215: ac = argc(arg);
1.105 noro 2216: if ( !ac ) error("qt_normalize : invalid argument");
1.70 noro 2217: q = (QUOTE)ARG0(arg);
1.103 noro 2218: if ( ac == 2 )
2219: expand = QTOS((Q)ARG1(arg));
1.97 noro 2220: if ( !q || OID(q) != O_QUOTE )
1.87 noro 2221: *rp = q;
2222: else {
1.76 noro 2223: f = fnode_normalize(BDY(q),expand);
1.70 noro 2224: MKQUOTE(r,f);
2225: *rp = r;
1.98 noro 2226: }
2227: }
2228:
2229: NBP fnode_to_nbp(FNODE f);
2230:
1.105 noro 2231: void Pqt_to_nbp(NODE arg,NBP *rp)
1.98 noro 2232: {
2233: QUOTE q;
2234: FNODE f;
2235:
2236: q = (QUOTE)ARG0(arg); f = (FNODE)BDY(q);
2237: f = fnode_normalize(f,0);
2238: *rp = fnode_to_nbp(f);
2239: }
2240:
1.101 noro 2241: void Pshuffle_mul(NODE arg,NBP *rp)
1.98 noro 2242: {
2243: NBP p1,p2;
2244:
2245: p1 = (NBP)ARG0(arg);
2246: p2 = (NBP)ARG1(arg);
2247: shuffle_mulnbp(CO,p1,p2,rp);
2248: }
1.99 noro 2249:
1.101 noro 2250: void Pharmonic_mul(NODE arg,NBP *rp)
1.99 noro 2251: {
2252: NBP p1,p2;
2253:
2254: p1 = (NBP)ARG0(arg);
2255: p2 = (NBP)ARG1(arg);
2256: harmonic_mulnbp(CO,p1,p2,rp);
2257: }
2258:
1.100 noro 2259: void Pnbp_hm(NODE arg, NBP *rp)
2260: {
2261: NBP p;
2262: NODE n;
2263: NBM m;
2264:
2265: p = (NBP)ARG0(arg);
2266: if ( !p ) *rp = 0;
2267: else {
2268: m = (NBM)BDY(BDY(p));
2269: MKNODE(n,m,0);
2270: MKNBP(*rp,n);
2271: }
2272: }
2273:
2274: void Pnbp_ht(NODE arg, NBP *rp)
2275: {
2276: NBP p;
2277: NODE n;
2278: NBM m,m1;
2279:
2280: p = (NBP)ARG0(arg);
2281: if ( !p ) *rp = 0;
2282: else {
2283: m = (NBM)BDY(BDY(p));
2284: NEWNBM(m1);
2285: m1->d = m->d; m1->c = ONE; m1->b = m->b;
2286: MKNODE(n,m1,0);
2287: MKNBP(*rp,n);
2288: }
2289: }
2290:
2291: void Pnbp_hc(NODE arg, Q *rp)
2292: {
2293: NBP p;
2294: NBM m;
2295:
2296: p = (NBP)ARG0(arg);
2297: if ( !p ) *rp = 0;
2298: else {
2299: m = (NBM)BDY(BDY(p));
2300: *rp = m->c;
2301: }
2302: }
2303:
2304: void Pnbp_rest(NODE arg, NBP *rp)
2305: {
2306: NBP p;
2307: NODE n;
2308:
2309: p = (NBP)ARG0(arg);
2310: if ( !p ) *rp = 0;
2311: else {
2312: n = BDY(p);
2313: if ( !NEXT(n) ) *rp = 0;
2314: else
2315: MKNBP(*rp,NEXT(n));
2316: }
2317: }
2318:
1.102 noro 2319: void Pnbm_deg(NODE arg, Q *rp)
1.100 noro 2320: {
2321: NBP p;
2322: NBM m;
1.102 noro 2323:
2324: p = (NBP)ARG0(arg);
2325: if ( !p )
2326: STOQ(-1,*rp);
2327: else {
2328: m = (NBM)BDY(BDY(p));
2329: STOQ(m->d,*rp);
2330: }
2331: }
2332:
2333: void Pnbm_hp_rest(NODE arg, LIST *rp)
2334: {
2335: NBP p,h,r;
2336: NBM m,m1;
1.100 noro 2337: NODE n;
1.102 noro 2338: int *b,*b1;
2339: int d,d1,v,i,j,k;
1.100 noro 2340:
2341: p = (NBP)ARG0(arg);
1.102 noro 2342: if ( !p )
1.100 noro 2343: MKLIST(*rp,0);
1.102 noro 2344: else {
2345: m = (NBM)BDY(BDY(p));
2346: b = m->b; d = m->d;
1.100 noro 2347: if ( !d )
2348: MKLIST(*rp,0);
2349: else {
1.102 noro 2350: v = NBM_GET(b,0);
1.100 noro 2351: for ( i = 1; i < d; i++ )
1.102 noro 2352: if ( NBM_GET(b,i) != v ) break;
2353: NEWNBM(m1); NEWNBMBDY(m1,i);
2354: b1 = m1->b; m1->d = i; m1->c = ONE;
2355: if ( v ) for ( j = 0; j < i; j++ ) NBM_SET(b1,j);
2356: else for ( j = 0; j < i; j++ ) NBM_CLR(b1,j);
2357: MKNODE(n,m1,0); MKNBP(h,n);
2358:
2359: d1 = d-i;
2360: NEWNBM(m1); NEWNBMBDY(m1,d1);
2361: b1 = m1->b; m1->d = d1; m1->c = ONE;
2362: for ( j = 0, k = i; j < d1; j++, k++ )
2363: if ( NBM_GET(b,k) ) NBM_SET(b1,j);
2364: else NBM_CLR(b1,j);
2365: MKNODE(n,m1,0); MKNBP(r,n);
2366: n = mknode(2,h,r);
1.100 noro 2367: MKLIST(*rp,n);
2368: }
2369: }
2370: }
2371:
1.102 noro 2372: void Pnbm_hxky(NODE arg, LIST *rp)
2373: {
2374: NBP p;
2375:
2376: p = (NBP)ARG0(arg);
2377: if ( !p )
2378: *rp = 0;
2379: else
2380: separate_xky_nbm((NBM)BDY(BDY(p)),0,rp,0);
2381: }
2382:
2383: void Pnbm_xky_rest(NODE arg,NBP *rp)
1.100 noro 2384: {
2385: NBP p;
2386:
2387: p = (NBP)ARG0(arg);
2388: if ( !p )
2389: *rp = 0;
1.102 noro 2390: else
2391: separate_xky_nbm((NBM)BDY(BDY(p)),0,0,rp);
2392: }
2393:
2394: void Pnbm_hv(NODE arg, NBP *rp)
2395: {
2396: NBP p;
2397:
2398: p = (NBP)ARG0(arg);
2399: if ( !p )
2400: *rp = 0;
2401: else
2402: separate_nbm((NBM)BDY(BDY(p)),0,rp,0);
2403: }
2404:
2405: void Pnbm_rest(NODE arg, NBP *rp)
2406: {
2407: NBP p;
2408:
2409: p = (NBP)ARG0(arg);
2410: if ( !p )
2411: *rp = 0;
2412: else
2413: separate_nbm((NBM)BDY(BDY(p)),0,0,rp);
1.100 noro 2414: }
1.98 noro 2415:
2416: NBP fnode_to_nbp(FNODE f)
2417: {
2418: Q r;
2419: int n,i;
2420: NBM m;
2421: V v;
2422: NBP u,u1,u2;
2423: NODE t,b;
2424:
2425: if ( f->id == I_FORMULA ) {
2426: r = eval(f);
2427: NEWNBM(m);
2428: if ( OID(r) == O_N ) {
2429: m->d = 0; m->c = (Q)r; m->b = 0;
2430: } else {
2431: v = VR((P)r);
2432: m->d = 1; m->c = ONE; NEWNBMBDY(m,1);
2433: if ( !strcmp(NAME(v),"x") ) NBM_SET(m->b,0);
2434: else NBM_CLR(m->b,0);
2435: }
2436: MKNODE(b,m,0); MKNBP(u,b);
2437: return u;
2438: } else if ( IS_NARYADD(f) ) {
2439: t = (NODE)FA1(f); u = fnode_to_nbp((FNODE)BDY(t));
2440: for ( t = NEXT(t); t; t = NEXT(t) ) {
2441: u1 = fnode_to_nbp((FNODE)BDY(t));
2442: addnbp(CO,u,u1,&u2); u = u2;
2443: }
2444: return u;
2445: } else if ( IS_NARYMUL(f) ) {
2446: t = (NODE)FA1(f); u = fnode_to_nbp((FNODE)BDY(t));
2447: for ( t = NEXT(t); t; t = NEXT(t) ) {
2448: u1 = fnode_to_nbp((FNODE)BDY(t));
2449: mulnbp(CO,u,u1,&u2); u = u2;
2450: }
2451: return u;
2452: } else if ( IS_BINARYPWR(f) ) {
2453: u = fnode_to_nbp((FNODE)FA1(f));
2454: r = eval((FNODE)FA2(f));
2455: pwrnbp(CO,u,r,&u1);
2456: return u1;
1.70 noro 2457: }
2458: }
2459:
1.105 noro 2460: void Pnqt_comp(NODE arg,Q *rp)
1.80 noro 2461: {
1.87 noro 2462: QUOTE q1,q2;
1.80 noro 2463: FNODE f1,f2;
2464: int r;
2465:
1.87 noro 2466: q1 = (QUOTE)ARG0(arg); f1 = (FNODE)BDY(q1);
2467: q2 = (QUOTE)ARG1(arg); f2 = (FNODE)BDY(q2);
1.97 noro 2468: f1 = fnode_normalize(f1,0);
2469: f2 = fnode_normalize(f2,0);
1.95 noro 2470: r = nfnode_comp(f1,f2);
1.80 noro 2471: STOQ(r,*rp);
2472: }
2473:
1.89 noro 2474: INLINE int fnode_is_number(FNODE f)
1.69 noro 2475: {
2476: Obj obj;
2477:
2478: switch ( f->id ) {
2479: case I_MINUS: case I_PAREN:
2480: return fnode_is_number(FA0(f));
2481:
2482: case I_FORMULA:
2483: obj = FA0(f);
2484: if ( !obj ) return 1;
2485: else if ( OID(obj) == O_QUOTE )
2486: return fnode_is_number(BDY((QUOTE)obj));
2487: else if ( NUM(obj) ) return 1;
2488: else return 0;
2489:
2490: case I_BOP:
2491: return fnode_is_number(FA1(f)) && fnode_is_number(FA2(f));
2492:
2493: default:
2494: return 0;
2495: }
2496: }
2497:
2498: int fnode_is_rational(FNODE f)
2499: {
2500: Obj obj;
2501:
2502: switch ( f->id ) {
2503: case I_MINUS: case I_PAREN:
2504: return fnode_is_number(FA0(f));
2505:
2506: case I_FORMULA:
2507: obj = FA0(f);
2508: if ( !obj ) return 1;
2509: else if ( OID(obj) == O_QUOTE )
2510: return fnode_is_rational(BDY((QUOTE)obj));
2511: else if ( NUM(obj) && RATN(obj) ) return 1;
2512: else return 0;
2513:
2514: case I_BOP:
2515: if ( !strcmp(((ARF)FA0(f))->name,"^") )
2516: return fnode_is_rational(FA1(f)) && fnode_is_integer(FA2(f));
2517: else
2518: return fnode_is_rational(FA1(f)) && fnode_is_rational(FA2(f));
2519:
2520: default:
2521: return 0;
2522: }
2523: }
2524:
2525: int fnode_is_integer(FNODE f)
2526: {
2527: Obj obj;
2528:
2529: switch ( f->id ) {
2530: case I_MINUS: case I_PAREN:
2531: return fnode_is_integer(FA0(f));
2532:
2533: case I_FORMULA:
2534: obj = FA0(f);
2535: if ( !obj ) return 1;
2536: else if ( OID(obj) == O_QUOTE )
2537: return fnode_is_integer(BDY((QUOTE)obj));
2538: else if ( INT(obj)) return 1;
2539: else return 0;
2540:
2541: case I_BOP:
1.70 noro 2542: switch ( ((ARF)FA0(f))->name[0] ) {
2543: case '^':
2544: return fnode_is_integer(FA1(f))
2545: && fnode_is_nonnegative_integer(FA2(f));
2546: case '/':
2547: return fnode_is_integer(FA1(f)) &&
2548: ( fnode_is_one(FA2(f)) || fnode_is_minusone(FA2(f)) );
2549: default:
2550: return fnode_is_integer(FA1(f)) && fnode_is_integer(FA2(f));
2551: }
2552: break;
2553:
1.69 noro 2554: default:
2555: return 0;
2556: }
2557: }
2558:
2559: int fnode_is_nonnegative_integer(FNODE f)
2560: {
2561: Q n;
2562:
2563: n = eval(f);
2564: if ( !n || (INT(n) && SGN(n) > 0) ) return 1;
2565: else return 0;
2566: }
2567:
2568: int fnode_is_one(FNODE f)
2569: {
2570: Q n;
2571:
2572: n = eval(f);
2573: if ( UNIQ(n) ) return 1;
2574: else return 0;
2575: }
2576:
2577: int fnode_is_minusone(FNODE f)
2578: {
2579: Q n;
2580:
2581: n = eval(f);
2582: if ( MUNIQ(n) ) return 1;
2583: else return 0;
2584: }
2585:
2586: int fnode_is_dependent(FNODE f,V v)
2587: {
2588: Obj obj;
2589: FNODE arg;
2590: NODE t;
2591:
2592: switch ( f->id ) {
2593: case I_MINUS: case I_PAREN:
2594: return fnode_is_dependent(FA0(f),v);
2595:
2596: case I_FORMULA:
2597: obj = FA0(f);
2598: if ( !obj ) return 0;
2599: else if ( OID(obj) == O_QUOTE )
2600: return fnode_is_dependent(BDY((QUOTE)obj),v);
2601: else if ( obj_is_dependent(obj,v) ) return 1;
2602: else return 0;
2603:
2604: case I_BOP:
2605: return fnode_is_dependent(FA1(f),v) || fnode_is_dependent(FA2(f),v);
2606:
2607: case I_FUNC:
2608: arg = (FNODE)FA1(f);
2609: for ( t = FA0(arg); t; t = NEXT(t) )
2610: if ( fnode_is_dependent(BDY(t),v) ) return 1;
2611: return 0;
2612:
2613: default:
2614: return 0;
2615: }
1.1 noro 2616: }
1.70 noro 2617:
1.72 noro 2618:
1.76 noro 2619: FNODE fnode_normalize(FNODE f,int expand)
1.70 noro 2620: {
1.73 noro 2621: FNODE a1,a2,mone,r,b2;
1.72 noro 2622: NODE n;
1.70 noro 2623: Q q;
2624:
1.103 noro 2625: if ( f->normalized && (f->expanded == expand) ) return f;
1.72 noro 2626: STOQ(-1,q);
2627: mone = mkfnode(1,I_FORMULA,q);
1.70 noro 2628: switch ( f->id ) {
2629: case I_PAREN:
1.89 noro 2630: r = fnode_normalize(FA0(f),expand);
2631: break;
1.71 noro 2632:
2633: case I_MINUS:
1.95 noro 2634: r = nfnode_mul_coef((Num)q,
1.76 noro 2635: fnode_normalize(FA0(f),expand),expand);
1.89 noro 2636: break;
1.71 noro 2637:
1.70 noro 2638: case I_BOP:
2639: /* arf fnode fnode */
1.76 noro 2640: a1 = fnode_normalize(FA1(f),expand);
2641: a2 = fnode_normalize(FA2(f),expand);
1.72 noro 2642: switch ( OPNAME(f) ) {
2643: case '+':
1.95 noro 2644: r = nfnode_add(a1,a2,expand);
1.89 noro 2645: break;
1.70 noro 2646: case '-':
1.95 noro 2647: a2 = nfnode_mul_coef((Num)q,a2,expand);
2648: r = nfnode_add(a1,a2,expand);
1.89 noro 2649: break;
1.72 noro 2650: case '*':
1.95 noro 2651: r = nfnode_mul(a1,a2,expand);
1.89 noro 2652: break;
1.70 noro 2653: case '/':
1.95 noro 2654: a2 = nfnode_pwr(a2,mone,expand);
2655: r = nfnode_mul(a1,a2,expand);
1.89 noro 2656: break;
1.73 noro 2657: case '^':
1.95 noro 2658: r = nfnode_pwr(a1,a2,expand);
1.89 noro 2659: break;
1.70 noro 2660: default:
1.89 noro 2661: r = mkfnode(3,I_BOP,FA0(f),a1,a2);
2662: break;
1.70 noro 2663: }
2664: break;
1.71 noro 2665:
1.70 noro 2666: case I_NARYOP:
1.72 noro 2667: switch ( OPNAME(f) ) {
1.71 noro 2668: case '+':
1.73 noro 2669: n = (NODE)FA1(f);
1.76 noro 2670: r = fnode_normalize(BDY(n),expand); n = NEXT(n);
1.72 noro 2671: for ( ; n; n = NEXT(n) ) {
1.76 noro 2672: a1 = fnode_normalize(BDY(n),expand);
1.95 noro 2673: r = nfnode_add(r,a1,expand);
1.72 noro 2674: }
1.89 noro 2675: break;
1.70 noro 2676: case '*':
1.73 noro 2677: n = (NODE)FA1(f);
1.76 noro 2678: r = fnode_normalize(BDY(n),expand); n = NEXT(n);
1.72 noro 2679: for ( ; n; n = NEXT(n) ) {
1.76 noro 2680: a1 = fnode_normalize(BDY(n),expand);
1.95 noro 2681: r = nfnode_mul(r,a1,expand);
1.72 noro 2682: }
1.89 noro 2683: break;
1.70 noro 2684: default:
1.72 noro 2685: error("fnode_normallize : cannot happen");
1.70 noro 2686: }
1.89 noro 2687: break;
1.72 noro 2688:
1.70 noro 2689: default:
1.76 noro 2690: return fnode_apply(f,fnode_normalize,expand);
1.70 noro 2691: }
1.89 noro 2692: r->normalized = 1;
2693: r->expanded = expand;
2694: return r;
1.70 noro 2695: }
2696:
1.76 noro 2697: FNODE fnode_apply(FNODE f,FNODE (*func)(),int expand)
1.70 noro 2698: {
2699: fid_spec_p spec;
2700: FNODE r;
2701: int i,n;
2702: NODE t,t0,s;
2703:
2704: get_fid_spec(f->id,&spec);
2705: for ( n = 0; spec->type[n] != A_end; n++ );
2706: NEWFNODE(r,n); r->id = f->id;
2707: for ( i = 0; i < n; i++ ) {
2708: switch ( spec->type[i] ) {
2709: case A_fnode:
1.78 noro 2710: r->arg[i] = func(f->arg[i],expand);
1.70 noro 2711: break;
2712: case A_node:
2713: s = (NODE)f->arg[i];
2714: for ( t0 = 0; s; s = NEXT(s) ) {
2715: NEXTNODE(t0,t);
1.76 noro 2716: BDY(t) = (pointer)func((FNODE)BDY(s),expand);
1.70 noro 2717: }
2718: if ( t0 ) NEXT(t) = 0;
2719: r->arg[i] = t0;
2720: break;
2721: default:
2722: r->arg[i] = f->arg[i];
2723: break;
2724: }
2725: }
2726: return r;
2727: }
2728:
1.95 noro 2729: FNODE nfnode_add(FNODE f1,FNODE f2,int expand)
1.71 noro 2730: {
1.72 noro 2731: NODE n1,n2,r0,r;
2732: FNODE b1,b2;
2733: int s;
2734: Num c1,c2,c;
2735:
1.77 noro 2736: if ( IS_ZERO(f1) ) return f2;
2737: else if ( IS_ZERO(f2) ) return f1;
1.75 noro 2738: f1 = to_naryadd(f1); f2 = to_naryadd(f2);
2739: n1 = (NODE)FA1(f1); n2 = (NODE)FA1(f2);
1.72 noro 2740: r0 = 0;
2741: while ( n1 && n2 ) {
1.75 noro 2742: fnode_coef_body(BDY(n1),&c1,&b1); fnode_coef_body(BDY(n2),&c2,&b2);
1.95 noro 2743: if ( (s = nfnode_comp(b1,b2)) > 0 ) {
1.73 noro 2744: NEXTNODE(r0,r); BDY(r) = BDY(n1); n1 = NEXT(n1);
1.72 noro 2745: } else if ( s < 0 ) {
1.73 noro 2746: NEXTNODE(r0,r); BDY(r) = BDY(n2); n2 = NEXT(n2);
1.72 noro 2747: } else {
2748: addnum(0,c1,c2,&c);
2749: if ( c ) {
1.95 noro 2750: NEXTNODE(r0,r); BDY(r) = nfnode_mul_coef(c,b1,expand);
1.71 noro 2751: }
1.72 noro 2752: n1 = NEXT(n1); n2 = NEXT(n2);
1.71 noro 2753: }
2754: }
1.72 noro 2755: if ( n1 )
2756: if ( r0 ) NEXT(r) = n1;
2757: else r0 = n1;
2758: else if ( n2 )
2759: if ( r0 ) NEXT(r) = n2;
2760: else r0 = n2;
2761: else if ( r0 )
2762: NEXT(r) = 0;
2763:
1.106 ! noro 2764: return fnode_node_to_nary(addfs,r0);
1.75 noro 2765: }
2766:
1.106 ! noro 2767: FNODE fnode_node_to_nary(ARF op,NODE n)
1.75 noro 2768: {
1.106 ! noro 2769: if ( !n ) {
! 2770: if ( op->name[0] == '+' )
! 2771: return mkfnode(1,I_FORMULA,0);
! 2772: else
! 2773: return mkfnode(1,I_FORMULA,ONE);
! 2774: } else if ( !NEXT(n) ) return BDY(n);
! 2775: else return mkfnode(2,I_NARYOP,op,n);
1.71 noro 2776: }
2777:
1.95 noro 2778: FNODE nfnode_mul(FNODE f1,FNODE f2,int expand)
1.71 noro 2779: {
1.72 noro 2780: NODE n1,n2,r0,r,r1;
1.76 noro 2781: FNODE b1,b2,e1,e2,cc,t,t1;
1.72 noro 2782: FNODE *m;
2783: int s;
2784: Num c1,c2,c,e;
1.75 noro 2785: int l1,l,i,j;
1.72 noro 2786:
1.77 noro 2787: if ( IS_ZERO(f1) || IS_ZERO(f2) ) return mkfnode(1,I_FORMULA,0);
1.75 noro 2788: else if ( fnode_is_number(f1) )
1.95 noro 2789: return nfnode_mul_coef((Num)eval(f1),f2,expand);
1.75 noro 2790: else if ( fnode_is_number(f2) )
1.95 noro 2791: return nfnode_mul_coef((Num)eval(f2),f1,expand);
1.76 noro 2792:
2793: if ( expand && IS_NARYADD(f1) ) {
2794: t = mkfnode(1,I_FORMULA,0);
2795: for ( n1 = (NODE)FA1(f1); n1; n1 = NEXT(n1) ) {
1.95 noro 2796: t1 = nfnode_mul(BDY(n1),f2,expand);
2797: t = nfnode_add(t,t1,expand);
1.76 noro 2798: }
2799: return t;
2800: }
2801: if ( expand && IS_NARYADD(f2) ) {
2802: t = mkfnode(1,I_FORMULA,0);
2803: for ( n2 = (NODE)FA1(f2); n2; n2 = NEXT(n2) ) {
1.95 noro 2804: t1 = nfnode_mul(f1,BDY(n2),expand);
2805: t = nfnode_add(t,t1,expand);
1.76 noro 2806: }
2807: return t;
2808: }
1.72 noro 2809:
1.75 noro 2810: fnode_coef_body(f1,&c1,&b1); fnode_coef_body(f2,&c2,&b2);
2811: mulnum(0,c1,c2,&c);
1.72 noro 2812: if ( !c ) return mkfnode(1,I_FORMULA,0);
1.71 noro 2813:
1.76 noro 2814:
1.75 noro 2815: n1 = (NODE)FA1(to_narymul(b1)); n2 = (NODE)FA1(to_narymul(b2));
2816: l1 = length(n1); l = l1+length(n2);
1.72 noro 2817: m = (FNODE *)ALLOCA(l*sizeof(FNODE));
2818: for ( r = n1, i = 0; i < l1; r = NEXT(r), i++ ) m[i] = BDY(r);
2819: for ( r = n2; r; r = NEXT(r) ) {
1.103 noro 2820: if ( i == 0 || (expand == 2) )
1.72 noro 2821: m[i++] = BDY(r);
2822: else {
1.75 noro 2823: fnode_base_exp(m[i-1],&b1,&e1); fnode_base_exp(BDY(r),&b2,&e2);
2824: if ( compfnode(b1,b2) ) break;
2825: addnum(0,eval(e1),eval(e2),&e);
2826: if ( !e ) i--;
2827: else if ( UNIQ(e) )
2828: m[i-1] = b1;
2829: else
2830: m[i-1] = mkfnode(3,I_BOP,pwrfs,b1,mkfnode(1,I_FORMULA,e));
1.71 noro 2831: }
2832: }
1.75 noro 2833: for ( j = i-1; j >= 0; j-- ) {
2834: MKNODE(r1,m[j],r); r = r1;
2835: }
2836: if ( !UNIQ(c) ) {
2837: cc = mkfnode(1,I_FORMULA,c); MKNODE(r1,cc,r); r = r1;
1.72 noro 2838: }
1.106 ! noro 2839: return fnode_node_to_nary(mulfs,r);
1.73 noro 2840: }
2841:
1.95 noro 2842: FNODE nfnode_pwr(FNODE f1,FNODE f2,int expand)
1.73 noro 2843: {
1.89 noro 2844: FNODE b,b1,e1,e,cc,r,mf2,mone,inv;
2845: Num c,c1,nf2;
2846: int ee;
2847: NODE arg,n,t0,t1;
1.76 noro 2848: Q q;
1.73 noro 2849:
1.77 noro 2850: if ( IS_ZERO(f2) ) return mkfnode(1,I_FORMULA,ONE);
2851: else if ( IS_ZERO(f1) ) return mkfnode(1,I_FORMULA,0);
1.92 noro 2852: else if ( fnode_is_number(f1) ) {
1.73 noro 2853: if ( fnode_is_integer(f2) ) {
1.92 noro 2854: if ( fnode_is_one(f2) ) return f1;
2855: else {
2856: pwrnum(0,(Num)eval(f1),(Num)eval(f2),&c);
2857: return mkfnode(1,I_FORMULA,c);
2858: }
1.73 noro 2859: } else
2860: return mkfnode(3,I_BOP,pwrfs,f1,f2);
1.92 noro 2861: } else if ( IS_BINARYPWR(f1) ) {
1.73 noro 2862: b1 = FA1(f1); e1 = FA2(f1);
1.95 noro 2863: e = nfnode_mul(e1,f2,expand);
1.73 noro 2864: if ( fnode_is_one(e) )
2865: return b1;
2866: else
2867: return mkfnode(3,I_BOP,FA0(f1),b1,e);
1.92 noro 2868: } else if ( expand && IS_NARYMUL(f1) && fnode_is_number(f2)
2869: && fnode_is_integer(f2) ) {
1.73 noro 2870: fnode_coef_body(f1,&c1,&b1);
1.89 noro 2871: nf2 = (Num)eval(f2);
2872: pwrnum(0,(Num)c1,nf2,&c);
2873: ee = QTOS((Q)nf2);
1.75 noro 2874: cc = mkfnode(1,I_FORMULA,c);
1.89 noro 2875: if ( fnode_is_nonnegative_integer(f2) )
1.103 noro 2876: b = fnode_expand_pwr(b1,ee,expand);
1.89 noro 2877: else {
2878: STOQ(-1,q);
2879: mone = mkfnode(1,I_FORMULA,q);
1.92 noro 2880: b1 = to_narymul(b1);
1.89 noro 2881: for ( t0 = 0, n = (NODE)FA1(b1); n; n = NEXT(n) ) {
2882: inv = mkfnode(3,I_BOP,pwrfs,BDY(n),mone);
2883: MKNODE(t1,inv,t0); t0 = t1;
2884: }
1.106 ! noro 2885: b1 = fnode_node_to_nary(mulfs,t0);
1.103 noro 2886: b = fnode_expand_pwr(b1,-ee,expand);
1.89 noro 2887: }
1.75 noro 2888: if ( fnode_is_one(cc) )
2889: return b;
2890: else
1.106 ! noro 2891: return fnode_node_to_nary(mulfs,mknode(2,cc,b));
1.82 noro 2892: } else if ( expand && fnode_is_integer(f2)
2893: && fnode_is_nonnegative_integer(f2) ) {
1.76 noro 2894: q = (Q)eval(f2);
1.95 noro 2895: if ( PL(NM(q)) > 1 ) error("nfnode_pwr : exponent too large");
1.103 noro 2896: return fnode_expand_pwr(f1,QTOS(q),expand);
1.73 noro 2897: } else
2898: return mkfnode(3,I_BOP,pwrfs,f1,f2);
1.72 noro 2899: }
2900:
1.103 noro 2901: FNODE fnode_expand_pwr(FNODE f,int n,int expand)
1.76 noro 2902: {
1.103 noro 2903: int n1,i;
2904: FNODE f1,f2,fn;
2905: Q q;
1.76 noro 2906:
2907: if ( !n ) return mkfnode(1,I_FORMULA,ONE);
1.77 noro 2908: else if ( IS_ZERO(f) ) return mkfnode(1,I_FORMULA,0);
1.76 noro 2909: else if ( n == 1 ) return f;
2910: else {
1.103 noro 2911: switch ( expand ) {
2912: case 1:
2913: n1 = n/2;
2914: f1 = fnode_expand_pwr(f,n1,expand);
2915: f2 = nfnode_mul(f1,f1,expand);
2916: if ( n%2 ) f2 = nfnode_mul(f2,f,1);
2917: return f2;
2918: case 2:
2919: for ( i = 1, f1 = f; i < n; i++ )
2920: f1 = nfnode_mul(f1,f,expand);
2921: return f1;
2922: case 0: default:
2923: STOQ(n,q);
2924: fn = mkfnode(1,I_FORMULA,q);
2925: return mkfnode(3,I_BOP,pwrfs,f,fn);
2926: }
1.76 noro 2927: }
2928: }
2929:
1.72 noro 2930: /* f = b^e */
2931: void fnode_base_exp(FNODE f,FNODE *bp,FNODE *ep)
2932: {
1.75 noro 2933: if ( IS_BINARYPWR(f) ) {
1.72 noro 2934: *bp = FA1(f); *ep = FA2(f);
2935: } else {
2936: *bp = f; *ep = mkfnode(1,I_FORMULA,ONE);
2937: }
2938: }
2939:
2940: FNODE to_naryadd(FNODE f)
2941: {
2942: FNODE r;
2943: NODE n;
2944:
1.75 noro 2945: if ( IS_NARYADD(f) ) return f;
2946:
2947: NEWFNODE(r,2); r->id = I_NARYOP;
2948: FA0(r) = addfs; MKNODE(n,f,0); FA1(r) = n;
1.72 noro 2949: return r;
2950: }
2951:
2952: FNODE to_narymul(FNODE f)
2953: {
2954: FNODE r;
2955: NODE n;
2956:
1.75 noro 2957: if ( IS_NARYMUL(f) ) return f;
2958:
2959: NEWFNODE(r,2); r->id = I_NARYOP;
2960: FA0(r) = mulfs; MKNODE(n,f,0); FA1(r) = n;
1.72 noro 2961: return r;
2962: }
2963:
1.95 noro 2964: FNODE nfnode_mul_coef(Num c,FNODE f,int expand)
1.72 noro 2965: {
1.76 noro 2966: FNODE b1,cc;
1.72 noro 2967: Num c1,c2;
1.76 noro 2968: NODE n,r,r0;
1.72 noro 2969:
2970: if ( !c )
2971: return mkfnode(I_FORMULA,0);
1.75 noro 2972: else {
2973: fnode_coef_body(f,&c1,&b1);
2974: mulnum(0,c,c1,&c2);
2975: if ( UNIQ(c2) ) return b1;
2976: else {
2977: cc = mkfnode(1,I_FORMULA,c2);
2978: if ( fnode_is_number(b1) ) {
2979: if ( !fnode_is_one(b1) )
1.95 noro 2980: error("nfnode_mul_coef : cannot happen");
1.75 noro 2981: else
2982: return cc;
1.76 noro 2983: } else if ( IS_NARYMUL(b1) ) {
1.75 noro 2984: MKNODE(n,cc,FA1(b1));
1.106 ! noro 2985: return fnode_node_to_nary(mulfs,n);
1.76 noro 2986: } else if ( expand && IS_NARYADD(b1) ) {
2987: for ( r0 = 0, n = (NODE)FA1(b1); n; n = NEXT(n) ) {
2988: NEXTNODE(r0,r);
1.95 noro 2989: BDY(r) = nfnode_mul_coef(c2,BDY(n),expand);
1.76 noro 2990: }
2991: if ( r0 ) NEXT(r) = 0;
1.106 ! noro 2992: return fnode_node_to_nary(addfs,r0);
1.76 noro 2993: } else
1.106 ! noro 2994: return fnode_node_to_nary(mulfs,mknode(2,cc,b1));
1.72 noro 2995: }
1.71 noro 2996: }
2997: }
2998:
1.72 noro 2999: void fnode_coef_body(FNODE f,Num *cp,FNODE *bp)
1.70 noro 3000: {
1.72 noro 3001: FNODE c;
1.70 noro 3002:
1.72 noro 3003: if ( fnode_is_number(f) ) {
3004: *cp = eval(f); *bp = mkfnode(1,I_FORMULA,ONE);
1.75 noro 3005: } else if ( IS_NARYMUL(f) ) {
3006: c=(FNODE)BDY((NODE)FA1(f));
1.72 noro 3007: if ( fnode_is_number(c) ) {
3008: *cp = eval(c);
1.106 ! noro 3009: *bp = fnode_node_to_nary(mulfs,NEXT((NODE)FA1(f)));
1.70 noro 3010: } else {
1.72 noro 3011: *cp = (Num)ONE; *bp = f;
1.70 noro 3012: }
3013: } else {
1.72 noro 3014: *cp = (Num)ONE; *bp = f;
1.70 noro 3015: }
1.80 noro 3016: }
3017:
1.95 noro 3018: int nfnode_comp_pwr(FNODE f1,FNODE f2);
1.80 noro 3019:
1.95 noro 3020: int nfnode_comp(FNODE f1,FNODE f2)
1.80 noro 3021: {
3022: NODE n1,n2;
1.95 noro 3023: int r,i1,i2,ret;
1.82 noro 3024: char *nm1,*nm2;
1.95 noro 3025: FNODE b1,b2,e1,e2,g,a1,a2,fn1,fn2;
1.83 noro 3026: Num ee,ee1,c1,c2;
1.80 noro 3027:
3028: if ( IS_NARYADD(f1) || IS_NARYADD(f2) ) {
3029: f1 = to_naryadd(f1); f2 = to_naryadd(f2);
3030: n1 = (NODE)FA1(f1); n2 = (NODE)FA1(f2);
3031: while ( n1 && n2 )
1.95 noro 3032: if ( r = nfnode_comp(BDY(n1),BDY(n2)) ) return r;
1.80 noro 3033: else {
3034: n1 = NEXT(n1); n2 = NEXT(n2);
3035: }
1.85 noro 3036: return n1?1:(n2?-1:0);
1.80 noro 3037: }
3038: if ( IS_NARYMUL(f1) || IS_NARYMUL(f2) ) {
1.83 noro 3039: fnode_coef_body(f1,&c1,&b1);
3040: fnode_coef_body(f2,&c2,&b2);
3041: if ( !compfnode(b1,b2) ) return compnum(0,c1,c2);
3042: b1 = to_narymul(b1); b2 = to_narymul(b2);
3043: n1 = (NODE)FA1(b1); n2 = (NODE)FA1(b2);
1.80 noro 3044: while ( 1 ) {
3045: while ( n1 && n2 && !compfnode(BDY(n1),BDY(n2)) ) {
3046: n1 = NEXT(n1); n2 = NEXT(n2);
3047: }
3048: if ( !n1 || !n2 ) {
1.85 noro 3049: return n1?1:(n2?-1:0);
1.80 noro 3050: }
3051: fnode_base_exp(BDY(n1),&b1,&e1);
3052: fnode_base_exp(BDY(n2),&b2,&e2);
3053:
1.95 noro 3054: if ( r = nfnode_comp(b1,b2) ) {
1.83 noro 3055: if ( r > 0 )
1.95 noro 3056: return nfnode_comp(e1,mkfnode(1,I_FORMULA,0));
1.83 noro 3057: else if ( r < 0 )
1.95 noro 3058: return nfnode_comp(mkfnode(1,I_FORMULA,0),e2);
1.83 noro 3059: } else {
3060: n1 = NEXT(n1); n2 = NEXT(n2);
3061: if ( fnode_is_number(e1) && fnode_is_number(e2) ) {
3062: /* f1 = t b^e1 ... , f2 = t b^e2 ... */
3063: subnum(0,eval(e1),eval(e2),&ee);
1.86 noro 3064: r = compnum(0,ee,0);
3065: if ( r > 0 ) {
1.83 noro 3066: g = mkfnode(3,I_BOP,pwrfs,b1,mkfnode(1,I_FORMULA,ee));
3067: MKNODE(n1,g,n1);
1.86 noro 3068: } else if ( r < 0 ) {
3069: chsgnnum(ee,&ee1);
3070: g = mkfnode(3,I_BOP,pwrfs,b1,mkfnode(1,I_FORMULA,ee1));
3071: MKNODE(n2,g,n2);
1.83 noro 3072: }
3073: } else {
1.95 noro 3074: r = nfnode_comp(e1,e2);
1.83 noro 3075: if ( r > 0 ) return 1;
3076: else if ( r < 0 ) return -1;
1.80 noro 3077: }
3078: }
3079: }
3080: }
3081: if ( IS_BINARYPWR(f1) || IS_BINARYPWR(f2) )
1.95 noro 3082: return nfnode_comp_pwr(f1,f2);
1.80 noro 3083:
1.95 noro 3084: /* now, IDs of f1 and f2 must be I_FORMULA, I_FUNC, I_IFUNC or I_PVAR */
1.106 ! noro 3085: /* I_IFUNC > I_PVAR > I_FUNC=I_FUNC_QARG > I_FORMULA */
1.80 noro 3086: switch ( f1->id ) {
3087: case I_FORMULA:
3088: switch ( f2->id ) {
3089: case I_FORMULA:
1.105 noro 3090: return arf_comp(qt_current_ord?qt_current_ord:CO,FA0(f1),FA0(f2));
1.95 noro 3091: case I_FUNC: case I_IFUNC: case I_PVAR:
1.80 noro 3092: return -1;
3093: default:
1.95 noro 3094: error("nfnode_comp : undefined");
1.80 noro 3095: }
3096: break;
1.106 ! noro 3097: case I_FUNC: case I_FUNC_QARG:
1.80 noro 3098: switch ( f2->id ) {
3099: case I_FORMULA:
3100: return 1;
1.95 noro 3101: case I_PVAR: case I_IFUNC:
3102: return -1;
1.106 ! noro 3103: case I_FUNC: case I_FUNC_QARG:
1.82 noro 3104: nm1 = ((FUNC)FA0(f1))->name; nm2 = ((FUNC)FA0(f2))->name;
3105: r = strcmp(nm1,nm2);
3106: if ( r > 0 ) return 1;
3107: else if ( r < 0 ) return -1;
1.80 noro 3108: else {
3109: /* compare args */
3110: n1 = FA0((FNODE)FA1(f1)); n2 = FA0((FNODE)FA1(f2));
3111: while ( n1 && n2 )
1.95 noro 3112: if ( r = nfnode_comp(BDY(n1),BDY(n2)) ) return r;
1.82 noro 3113: else {
3114: n1 = NEXT(n1); n2 = NEXT(n2);
3115: }
1.85 noro 3116: return n1?1:(n2?-1:0);
1.80 noro 3117: }
3118: break;
3119: default:
1.95 noro 3120: error("nfnode_comp : undefined");
1.80 noro 3121: }
3122: case I_PVAR:
3123: switch ( f2->id ) {
1.106 ! noro 3124: case I_FORMULA: case I_FUNC: case I_FUNC_QARG:
1.80 noro 3125: return 1;
1.95 noro 3126: case I_IFUNC:
3127: return -1;
1.80 noro 3128: case I_PVAR:
3129: i1 = (int)FA0(f1); i2 = (int)FA0(f2);
3130: if ( i1 > i2 ) return 1;
3131: else if ( i1 < i2 ) return -1;
3132: else return 0;
3133: default:
1.95 noro 3134: error("nfnode_comp : undefined");
3135: }
3136: break;
3137: case I_IFUNC:
3138: switch ( f2->id ) {
1.106 ! noro 3139: case I_FORMULA: case I_FUNC: case I_FUNC_QARG: case I_PVAR:
1.95 noro 3140: return 1;
3141: case I_IFUNC:
3142: i1 = (int)FA0((FNODE)FA0(f1));
3143: i2 = (int)FA0((FNODE)FA0(f2));
3144: if ( i1 > i2 ) return 1;
3145: else if ( i1 < i2 ) return -1;
3146: else {
3147: /* compare args */
3148: n1 = FA0((FNODE)FA1(f1)); n2 = FA0((FNODE)FA1(f2));
3149: while ( n1 && n2 )
3150: if ( r = nfnode_comp(BDY(n1),BDY(n2)) ) return r;
3151: else {
3152: n1 = NEXT(n1); n2 = NEXT(n2);
3153: }
3154: return n1?1:(n2?-1:0);
3155: }
3156: break;
3157:
3158: default:
3159: error("nfnode_comp : undefined");
1.80 noro 3160: }
3161: break;
3162: default:
1.95 noro 3163: error("nfnode_comp : undefined");
1.80 noro 3164: }
3165: }
3166:
1.95 noro 3167: int nfnode_comp_pwr(FNODE f1,FNODE f2)
1.80 noro 3168: {
3169: FNODE b1,b2,e1,e2;
3170: int r;
3171:
3172: fnode_base_exp(f1,&b1,&e1);
3173: fnode_base_exp(f2,&b2,&e2);
1.95 noro 3174: if ( r = nfnode_comp(b1,b2) ) {
1.83 noro 3175: if ( r > 0 )
1.95 noro 3176: return nfnode_comp(e1,mkfnode(1,I_FORMULA,0));
1.83 noro 3177: else if ( r < 0 )
1.95 noro 3178: return nfnode_comp(mkfnode(1,I_FORMULA,0),e2);
3179: } else return nfnode_comp(e1,e2);
1.70 noro 3180: }
1.87 noro 3181:
1.88 noro 3182: NODE append_node(NODE a1,NODE a2)
3183: {
3184: NODE t,t0;
3185:
3186: if ( !a1 )
3187: return a2;
3188: else {
3189: for ( t0 = 0; a1; a1 = NEXT(a1) ) {
3190: NEXTNODE(t0,t); BDY(t) = BDY(a1);
3191: }
3192: NEXT(t) = a2;
3193: return t0;
3194: }
3195: }
3196:
1.95 noro 3197: int nfnode_match(FNODE f,FNODE pat,NODE *rp)
1.87 noro 3198: {
3199: NODE m,m1,m2,base,exp,fa,pa,n;
3200: LIST l;
3201: QUOTE qp,qf;
1.96 noro 3202: FNODE fbase,fexp,a,fh;
1.87 noro 3203: FUNC ff,pf;
3204: int r;
3205:
1.97 noro 3206: if ( !pat )
3207: if ( !f ) {
3208: *rp = 0;
3209: return 1;
3210: } else
3211: return 0;
3212: else if ( !f )
3213: return 0;
1.87 noro 3214: switch ( pat->id ) {
3215: case I_PVAR:
3216: /* [[pat,f]] */
1.103 noro 3217: *rp = mknode(1,mknode(2,(int)FA0(pat),f));
1.87 noro 3218: return 1;
3219:
3220: case I_FORMULA:
1.93 noro 3221: if ( f->id == I_FORMULA && !arf_comp(CO,(Obj)FA0(f),(Obj)FA0(pat)) ) {
1.87 noro 3222: *rp = 0; return 1;
3223: } else
3224: return 0;
3225:
3226: case I_BOP:
3227: /* OPNAME should be "^" */
3228: if ( !IS_BINARYPWR(pat) )
1.95 noro 3229: error("nfnode_match : invalid BOP");
1.87 noro 3230: if ( IS_BINARYPWR(f) ) {
3231: fbase = FA1(f); fexp = FA2(f);
3232: } else {
3233: fbase = f; fexp = mkfnode(1,I_FORMULA,ONE);
3234: }
1.95 noro 3235: if ( !nfnode_match(fbase,FA1(pat),&base) ) return 0;
1.106 ! noro 3236: a = rewrite_fnode(FA2(pat),base,0);
1.95 noro 3237: if ( !nfnode_match(fexp,a,&exp) ) return 0;
1.88 noro 3238: else {
3239: *rp = append_node(base,exp);
3240: return 1;
3241: }
1.87 noro 3242: break;
3243:
1.95 noro 3244: case I_FUNC: case I_IFUNC:
1.87 noro 3245: if ( f->id != I_FUNC ) return 0;
1.95 noro 3246: ff = (FUNC)FA0(f);
3247: if ( pat->id == I_FUNC ) {
3248: pf = (FUNC)FA0(pat);
3249: if ( strcmp(ff->fullname,pf->fullname) ) return 0;
3250: m = 0;
3251: } else {
1.96 noro 3252: /* XXX : I_FUNC_HEAD is a dummy id to pass FUNC */
3253: fh = mkfnode(1,I_FUNC_HEAD,FA0(f));
3254: m = mknode(1,mknode(2,FA0((FNODE)FA0(pat)),fh),0);
1.95 noro 3255: }
1.87 noro 3256: /* FA1(f) and FA1(pat) are I_LIST */
3257: fa = (NODE)FA0((FNODE)FA1(f));
3258: pa = (NODE)FA0((FNODE)FA1(pat));
3259: while ( fa && pa ) {
1.106 ! noro 3260: a = rewrite_fnode(BDY(pa),m,0);
1.95 noro 3261: if ( !nfnode_match(BDY(fa),a,&m1) ) return 0;
1.88 noro 3262: m = append_node(m1,m);
3263: fa = NEXT(fa); pa = NEXT(pa);
1.87 noro 3264: }
3265: if ( fa || pa ) return 0;
3266: else {
3267: *rp = m;
3268: return 1;
3269: }
3270:
3271: case I_NARYOP:
3272: if ( IS_NARYADD(pat) )
1.95 noro 3273: return nfnode_match_naryadd(f,pat,rp);
1.87 noro 3274: else if ( IS_NARYMUL(pat) )
1.95 noro 3275: return nfnode_match_narymul(f,pat,rp);
1.87 noro 3276: else
1.95 noro 3277: error("nfnode_match : invalid NARYOP");
1.87 noro 3278: break;
3279:
3280: default:
1.95 noro 3281: error("nfnode_match : invalid pattern");
1.87 noro 3282: }
3283: }
3284:
1.88 noro 3285: /* remove i-th element */
1.87 noro 3286:
1.88 noro 3287: FNODE fnode_removeith_naryadd(FNODE p,int i)
3288: {
3289: int k,l;
3290: NODE t,r0,r,a;
3291:
3292: a = (NODE)FA1(p);
3293: l = length(a);
3294: if ( i < 0 || i >= l ) error("fnode_removeith_naryadd: invalid index");
3295: else if ( i == 0 )
1.106 ! noro 3296: return fnode_node_to_nary(addfs,NEXT(a));
1.88 noro 3297: else {
3298: for ( r0 = 0, k = 0, t = a; k < i; k++, t = NEXT(t) ) {
3299: NEXTNODE(r0,r);
3300: BDY(r) = BDY(t);
3301: }
1.94 noro 3302: NEXT(r) = NEXT(t);
1.106 ! noro 3303: return fnode_node_to_nary(addfs,r0);
1.88 noro 3304: }
3305:
3306: }
1.87 noro 3307:
1.88 noro 3308: /* a0,...,a(i-1) */
3309: FNODE fnode_left_narymul(FNODE p,int i)
1.87 noro 3310: {
1.88 noro 3311: int k,l;
3312: NODE t,r0,r,a;
3313:
3314: a = (NODE)FA1(p);
3315: l = length(a);
3316: if ( i < 0 || i >= l ) error("fnode_left_narymul : invalid index");
1.97 noro 3317: if ( i == 0 ) return 0;
1.88 noro 3318: else if ( i == 1 ) return (FNODE)BDY(a);
3319: else {
3320: for ( r0 = 0, k = 0, t = a; k < i; k++, t = NEXT(t) ) {
3321: NEXTNODE(r0,r);
3322: BDY(r) = BDY(t);
3323: }
3324: NEXT(r) = 0;
1.106 ! noro 3325: return fnode_node_to_nary(mulfs,r0);
1.88 noro 3326: }
3327: }
3328:
3329: /* a(i+1),...,a(l-1) */
3330: FNODE fnode_right_narymul(FNODE p,int i)
3331: {
3332: NODE a,t;
3333: int l,k;
3334:
3335: a = (NODE)FA1(p);
3336: l = length(a);
3337: if ( i < 0 || i >= l ) error("fnode_right_narymul : invalid index");
1.97 noro 3338: if ( i == l-1 ) return 0;
1.88 noro 3339: else {
3340: for ( k = 0, t = a; k <= i; k++, t = NEXT(t) );
1.106 ! noro 3341: return fnode_node_to_nary(mulfs,t);
1.88 noro 3342: }
3343: }
3344:
1.95 noro 3345: int nfnode_match_naryadd(FNODE f,FNODE p,NODE *rp)
1.88 noro 3346: {
3347: int fl,pl,fi,pi;
3348: NODE fa,pa,t,s,m,m1;
3349: FNODE fr,pr,prr,pivot;
1.87 noro 3350:
3351: f = to_naryadd(f);
1.88 noro 3352: fa = (NODE)FA1(f); fl = length(fa);
3353: pa = (NODE)FA1(p); pl = length(pa);
3354: if ( fl < pl ) return 0;
3355: else if ( pl == 1 ) {
3356: if ( fl == 1 )
1.95 noro 3357: return nfnode_match(BDY(fa),BDY(pa),rp);
1.87 noro 3358: else
3359: return 0;
3360: } else {
1.88 noro 3361: for ( t = pa, pi = 0; t; t = NEXT(t), pi++ )
3362: if ( ((FNODE)BDY(t))->id != I_PVAR ) break;
3363: if ( !t ) {
3364: /* all are I_PVAR */
3365: m = 0;
3366: for ( t = pa, s = fa; NEXT(t); t = NEXT(t), s = NEXT(s) ) {
1.95 noro 3367: nfnode_match(BDY(s),BDY(t),&m1);
1.88 noro 3368: m = append_node(m1,m);
3369: }
3370: if ( !NEXT(s) )
3371: fr = (FNODE)BDY(s);
3372: else
3373: fr = mkfnode(2,I_NARYOP,FA0(f),s);
1.95 noro 3374: nfnode_match(fr,BDY(t),&m1);
1.88 noro 3375: *rp = append_node(m1,m);
3376: return 1;
3377: } else {
3378: pivot = (FNODE)BDY(t);
3379: pr = fnode_removeith_naryadd(p,pi);
3380: for ( s = fa, fi = 0; s; s = NEXT(s), fi++ ) {
1.95 noro 3381: if ( nfnode_match(BDY(s),pivot,&m) ) {
1.88 noro 3382: fr = fnode_removeith_naryadd(f,fi);
1.106 ! noro 3383: prr = rewrite_fnode(pr,m,0);
1.95 noro 3384: if ( nfnode_match(fr,prr,&m1) ) {
1.88 noro 3385: *rp = append_node(m,m1);
3386: return 1;
3387: }
3388: }
3389: }
3390: return 0;
3391: }
1.87 noro 3392: }
3393: }
3394:
1.95 noro 3395: int nfnode_match_narymul(FNODE f,FNODE p,NODE *rp)
1.88 noro 3396: {
3397: int fl,pl,fi,pi;
3398: NODE fa,pa,t,s,m,m1;
3399: FNODE fr,pr,pleft,pleft1,pright,pright1,fleft,fright,pivot;
3400:
3401: f = to_narymul(f);
3402: fa = (NODE)FA1(f); fl = length(fa);
3403: pa = (NODE)FA1(p); pl = length(pa);
3404: if ( fl < pl ) return 0;
3405: else if ( pl == 1 ) {
3406: if ( fl == 1 )
1.95 noro 3407: return nfnode_match(BDY(fa),BDY(pa),rp);
1.88 noro 3408: else
3409: return 0;
3410: } else {
3411: for ( t = pa, pi = 0; t; t = NEXT(t), pi++ )
3412: if ( ((FNODE)BDY(t))->id != I_PVAR ) break;
3413: if ( !t ) {
3414: /* all are I_PVAR */
3415: m = 0;
3416: for ( t = pa, s = fa; NEXT(t); t = NEXT(t), s = NEXT(s) ) {
1.106 ! noro 3417: pr = rewrite_fnode(BDY(t),m,0);
1.95 noro 3418: if ( !nfnode_match(BDY(s),pr,&m1) ) return 0;
1.88 noro 3419: m = append_node(m1,m);
3420: }
3421: if ( !NEXT(s) )
3422: fr = (FNODE)BDY(s);
3423: else
3424: fr = mkfnode(2,I_NARYOP,FA0(f),s);
1.106 ! noro 3425: pr = rewrite_fnode(BDY(t),m,0);
1.95 noro 3426: if ( !nfnode_match(fr,pr,&m1) ) return 0;
1.88 noro 3427: *rp = append_node(m1,m);
3428: return 1;
3429: } else {
3430: pivot = (FNODE)BDY(t);
3431: pleft = fnode_left_narymul(p,pi);
3432: pright = fnode_right_narymul(p,pi);
3433: /* XXX : incomplete */
3434: for ( s = fa, fi = 0; s; s = NEXT(s), fi++ ) {
1.97 noro 3435: if ( fi < pi ) continue;
1.95 noro 3436: if ( nfnode_match(BDY(s),pivot,&m) ) {
1.88 noro 3437: fleft = fnode_left_narymul(f,fi);
1.106 ! noro 3438: pleft1 = rewrite_fnode(pleft,m,0);
1.95 noro 3439: if ( nfnode_match(fleft,pleft1,&m1) ) {
1.88 noro 3440: m = append_node(m1,m);
3441: fright = fnode_right_narymul(f,fi);
1.106 ! noro 3442: pright1 = rewrite_fnode(pright,m,0);
1.95 noro 3443: if ( nfnode_match(fright,pright1,&m1) ) {
1.88 noro 3444: *rp = append_node(m1,m);
3445: return 1;
3446: }
3447: }
3448: }
3449: }
3450: return 0;
3451: }
1.95 noro 3452: }
3453: }
3454:
3455: NODE nfnode_pvars(FNODE pat,NODE found)
3456: {
3457: int ind;
3458: NODE prev,t;
3459: int *pair;
3460:
3461: switch ( pat->id ) {
3462: case I_PVAR:
3463: ind = (int)FA0(pat);
3464: for ( prev = 0, t = found; t; prev = t, t = NEXT(t) ) {
3465: pair = (int *)BDY(t);
3466: if ( pair[0] == ind ) {
3467: pair[1]++;
3468: return found;
3469: }
3470: }
3471: pair = (int *)MALLOC_ATOMIC(sizeof(int)*2);
3472: pair[0] = ind; pair[1] = 1;
3473: if ( !prev )
3474: MKNODE(found,pair,0);
3475: else
3476: MKNODE(NEXT(prev),pair,0);
3477: return found;
3478:
3479: case I_FORMULA:
3480: return found;
3481:
3482: case I_BOP:
3483: /* OPNAME should be "^" */
3484: if ( !IS_BINARYPWR(pat) )
3485: error("nfnode_pvar : invalid BOP");
3486: found = nfnode_pvars(FA1(pat),found);
3487: found = nfnode_pvars(FA2(pat),found);
3488: return found;
3489:
3490: case I_FUNC:
3491: t = (NODE)FA0((FNODE)FA1(pat));
3492: for ( ; t; t = NEXT(t) )
3493: found = nfnode_pvars(BDY(t),found);
3494: return found;
3495:
3496: case I_NARYOP:
3497: t = (NODE)FA1(pat);
3498: for ( ; t; t = NEXT(t) )
3499: found = nfnode_pvars(BDY(t),found);
3500: return found;
3501:
3502: default:
3503: error("nfnode_match : invalid pattern");
1.88 noro 3504: }
3505: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>