[BACK]Return to test1-tr.rr CVS log [TXT][DIR] Up to [local] / OpenXM / src / asir-contrib / testing

File: [local] / OpenXM / src / asir-contrib / testing / test1-tr.rr (download)

Revision 1.8, Wed May 11 06:40:10 2005 UTC (19 years ago) by takayama
Branch: MAIN
CVS Tags: R_1_3_1-2, RELEASE_1_3_1_13b, RELEASE_1_2_3_12, KNOPPIX_2006, HEAD, DEB_REL_1_2_3-9
Changes since 1.7: +2 -2 lines

1. qt.input_form, qt.hop,
   qt.hc_etov(Q,V),  Q is a monomial in quote. It returns the leading
coefficient and the exponent vector of Q.

/* $Id: test1-tr.rr,v 1.19 2005/05/04 05:48:08 taka Exp $ */
/* $OpenXM: OpenXM/src/asir-contrib/testing/test1-tr.rr,v 1.8 2005/05/11 06:40:10 takayama Exp $ */

load("tr.rr")$

/*
   qt_ ==> qt.
   tr_ ==> tr.
*/

def test0() {
  A = quotetolist(quote(1+sin(x)+sin(3*@pi)*sin(0)));
  P = quotetolist(quote(sin(pn("x")*@pi)));
  Q = ["qt.sin_int","x"];
  print(A);
  print(P);
  print(Q);
  print("----------------");
  print(tr.match0(A,P));
  A2 = quotetolist(quote(sin(2*@pi)));
  print(tr.match0(A2,P));
  print("----------------");
  print("---- tr.make_binding --------");
  print(tr.make_binding(A2,P));
  print("-----tr.rp -------------");
  R=tr.rp(A,P,Q);
  print("--------------------");
  print(R);
  print("--------------------");
  return quote_input_form_quote_list(R);
}

def test1()  {
  Rule1=[quote(sin(pn("x")*@pi)),["qt.sin_int","x"]]; /* sin($B@0?t(B*@pi) --> 0 */
  Rule2=[quote(0*pn("y")),       ["qt.zero"]];       /* 0*any --> 0 */
  Rule3=[quote(pn("y")*0),       ["qt.zero"]];       /* any*0 --> 0 */
  Rule4=[quote(pn("y")+0),       ["qt.id","y"]];       /* any+0 --> any */
  Rule5=[quote(0+pn("y")),       ["qt.id","y"]];       /* 0+any --> any */
  Rule6=[quote(sin(0)),          ["qt.zero"]];       /* sin(0) --> 0 */
  R0 = quote(1+sin(sin(2*@pi)*sin(@pi/2))+sin(5*@pi));
  print(print_input_form(R0));
  R=tr.apply_rule1(R0,Rule1[0],Rule1[1]);
  print(print_input_form(R));
  R=tr.apply_rule1(R,Rule2[0],Rule2[1]);
  print(print_input_form(R));
  R=tr.apply_rule1(R,Rule4[0],Rule4[1]);
  print(print_input_form(R));
  R=tr.apply_rule1(R,Rule6[0],Rule6[1]);
  print(print_input_form(R));
  R=tr.apply_rule1(R,Rule4[0],Rule4[1]);
  print(print_input_form(R));
  return R;
}


/* $BITDj@QJ,7W;;$NNc(B  
    c x^n $B$NOB$NITDj@QJ,(B (c $B$O(B x $B$K0MB8$;$:(B)
   $B$$$m$$$m(B $BLdBjE@$"$j(B:  $B$?$H$($P(B c $B$,(B $BL5$$$H$-$N=hM}$G$-$:(B.
*/

/* $B1&JU4X?t(B.  c x^n $B$NITDj@QJ,(B (c $B$O(B x $B$K0MB8$;$:(B)
   Todo: $B1&JU4X?t$rMF0W$K=q$/J}K!(B.
*/
def r_integral0(C,N) {
  NN = eval_str(quote_input_form_quote_list(quotetolist(N)));
  CC = quote_input_form_quote_list(quotetolist(C));
  if (NN == -1) {
     R = "quote("+CC+"*log(x))";
  }else{
     R = "quote("+CC+"/"+rtostr(NN+1)+"*x^"+rtostr(NN+1)+")";
  }
  print("r_integral0:",0);print(R);
  R = eval_str(R);
  return quotetolist(R);
}
/* $B1&JU4X?t(B $B@QJ,$N@~7?@-(B */
def r_int_linear(F,G) {
  FF = quote_input_form_quote_list(quotetolist(F));
  GG = quote_input_form_quote_list(quotetolist(G));
  R = "quote(integral("+FF+")+integral("+GG+"))";
  print("r_int_linear:",0);print(R);
  R = eval_str(R);
  return quotetolist(R);
}
def test3() {
  R0 = quote(1+integral(2*x^(-1)+2*x^2));
  return test3a(R0);
}
def test3a(R0)  {
  Rules=[
     /* c*x^n --> (c/(n+1))*x^(n+1) or c*log(x) */
     [quote(integral(pn("c")*x^pn("n"))),["r_integral0","c","n"]],
     [quote(integral(pn("f")+pn("g"))),  ["r_int_linear","f","g"]]
  ];
  print("Input=",0); print(print_input_form(R0));
  N = length(Rules);
  R = R0;
  for (J=0; J<3; J++) {  /* Todo: $B%U%i%0$,$J$$$N$G(B, $B$H$j$"$($:(B 3 $B2s(B */
    for (I=0; I<N; I++) {
      print(print_input_form(R));
      R=tr.apply_rule1(R,Rules[I][0],Rules[I][1]);
    }
  }
  return R;
}


/* $B4X?t$N%^%C%A(B.  N[] $BAjEv(B.  test4(). */
/*  quote(nn(pn(f),qt.is_function(f))); $B$OITMW(B. qt.map_arg $B$,=hM}(B */
def test4() {
  Rule=[quote(nn(pn(f))),["qt.map_arg",nn,f]];
  R0 = quote(nn(sin(1/2)*cos(1/3)));
  print(print_input_form(R0));
  R=tr.apply_rule1(R0,Rule[0],Rule[1]);
  return R;
}

def test5() {
  Rule1=[quote(sin(pn(x)*@pi)),["qt.sin_int",x]]; /* sin($B@0?t(B*@pi) --> 0 */
  Rule2=[quote(0*pn(y)),       ["qt.zero"]];       /* 0*any --> 0 */
  Rule3=[quote(pn(y)*0),       ["qt.zero"]];       /* any*0 --> 0 */
  Rule4=[quote(pn(y)+0),       ["qt.id",y]];       /* any+0 --> any */
  Rule5=[quote(0+pn(y)),       ["qt.id",y]];       /* 0+any --> any */
  Rule6=[quote(sin(0)),          ["qt.zero"]];       /* sin(0) --> 0 */
  R0 = quote(1+sin(sin(2*@pi)*sin(@pi/2))+sin(5*@pi));
  print(print_input_form(R0));
  R=tr.apply_rule1_flag(R0,Rule1[0],Rule1[1]);
  print([R[0],print_input_form(R[1])]);
  R=tr.apply_or_rules(R0,[Rule1,Rule2,Rule3,Rule4,Rule5,Rule6]);
  return R;
}


/* $BHyJ,4D$N7W;;(B */
/* x $B$K0MB8$7$F$k$+(B?  u, u_0, u_1, u_2, ... $B$O(B x $B$K0MB8$7$F$k(B.*/
def to_quote(L) {
  return eval_str("quote("+listtoquote_str(L)+")");
}
def dep6(Q) {
  if (type(Q) == 4) {
    Q = to_quote(Q);
  }  
  if (qt.is_dependent(Q,x)) return 1;
  if (qt.is_dependent(Q,u)) return 1;
  /* $B$H$j$"$($:(B 10 $B<!$^$G$N(B f. --> $B$J$s$H$+$;$h(B. */
  for (I=0; I<10; I++) {  
    if (qt.is_dependent(Q,idxtov(u,I))) return 1;
  }
  return 0;
}
def diff_lin(F,G) {
  if (type(F) == 4) F=to_quote(F);
  if (type(G) == 4) G=to_quote(G);
  return qt.replace(quote(diff(f)+diff(g)),[[f,F],[g,G]]);
}
def diff_mul(F,G) {
  F1 = dep6(F); G1 = dep6(G);
  if (type(F) == 4) F=to_quote(F);
  if (type(G) == 4) G=to_quote(G);
  if (F1 && G1)
    return qt.replace(quote(diff(f)*g+f*diff(g)),[[f,F],[g,G]]);
  if ((F1 == 1) &&  (G1 == 0)) 
    return qt.replace(quote(diff(f)*g),[[f,F],[g,G]]);
  if ((F1 == 0) &&  (G1 == 1)) 
    return qt.replace(quote(f*diff(g)),[[f,F],[g,G]]);
  if ((F1 == 0) && (G1 == 0))
    return qt.zero();
}
def diff_x_n(N) {
  N = eval_quote(N);
  N1=N-1;
  if (N1 == 0)  return qt.one();
  if (N1 == 1)  return quote(2*x);
  if (N1 > 1) return eval_str("quote("+rtostr(N)+"*x^"+rtostr(N1)+")");
}
/* F $B$,(B u $B$H$+(B u_0, u_1, ... $B$J$i(B 1 $B$rLa$9(B. */
/* debug $BMQ$NF~NO(B.
  tr.check_pn(quote(u_1),quote(pn(x,is_u_variable(x))));
*/
def is_u_variable(F) {
  /* $B=R8l$NA0$N(B check point $B$b(B debugger $B$KM_$7$$(B. */
  /* print("is_u_variable: ",0); print(print_input_form(F)); */
  if (type(F) == 17) F=quotetolist(F);
  if (rtostr(F[0]) == "internal") {
    V = eval_str(rtostr(F[1]));
    if (vtoidx(V)[0] == "u") return 1;
  }
  return 0;
}
/*  u_i^n $B$NHyJ,$r$9$k(B.  n*u_{i+1}*u_i^{n-1} 
   Todo: $B$b$C$H4J7i$K(B quote $B$r=q$1$J$$$+(B?
*/
def diff_u_n(F,N) {
  F = eval_quote(F);
  I = vtoidx(F); 
  if (length(I) == 1) I = 0; else I=I[1];
  N = eval_quote(N);
  N1=N-1;
  NextU = "u_"+rtostr(I+1);
  if (I == 0) U = "u"; else U = "u_"+rtostr(I);

  NN = objtoquote(N);
  NN1 = objtoquote(N1);
  NextU = objtoquote(eval_str(NextU));
  U = objtoquote(eval_str(U));

  if (N1 == 0)  return NextU;
  if (N1 == 1)  return qt.replace(quote(2*up*uu),[[up,NextU],[uu,U]]);
  if (N1 > 1) return qt.replace(quote(n*up*uu^m),[[up,NextU],[uu,U],
     [n,NN],[m,NN1]]);
}

def test6b() {
  T1=[quote(diff(x)),["qt.one"]];
  T2=[quote(diff(x^pn(n))),[diff_x_n,n]];  /* is_poly? $B$,M_$7$$(B. */
  R1=[quote(diff(pn(f)+pn(g))),[diff_lin,f,g]];
  R2=[quote(diff(pn(f)*pn(g))),[diff_mul,f,g]];

  A = quote(diff(2*4*x^3+x));
  print(print_input_form(A));
  R=tr.apply_or_rules(A,[R1,R2,T1,T2]);
  return R;
}

/* Use Debug2=1; $B$O(B debug $B$K$H$F$bM-1W(B. */
def test6() {
  T1=[quote(diff(x)),["qt.one"]];
  T2=[quote(diff(x^pn(n))),[diff_x_n,n]];  /* is_poly? $B$,M_$7$$(B. */
  T3=[quote(diff(pn(f,is_u_variable(f))^pn(n))),[diff_u_n,f,n]]; 
  R1=[quote(diff(pn(f)+pn(g))),[diff_lin,f,g]];
  R2=[quote(diff(pn(f)*pn(g))),[diff_mul,f,g]];

  /* A = quote(diff(2*x^3+x));*/
  A = quote(diff(2*u^3+x)); 
  print(print_input_form(A));
  R=tr.apply_or_rules(A,[R1,R2,T1,T2,T3]);
  return R;
}
end$