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