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