=================================================================== RCS file: /home/cvs/OpenXM/src/asir-contrib/testing/tr-ja.oxt,v retrieving revision 1.8 retrieving revision 1.9 diff -u -p -r1.8 -r1.9 --- OpenXM/src/asir-contrib/testing/tr-ja.oxt 2005/05/04 05:47:03 1.8 +++ OpenXM/src/asir-contrib/testing/tr-ja.oxt 2005/05/11 06:40:10 1.9 @@ -1,5 +1,5 @@ -$Id: tr-ja.oxt,v 1.8 2005/05/04 05:47:03 takayama Exp $ -$OpenXM: OpenXM/src/asir-contrib/testing/tr-ja.oxt,v 1.7 2005/04/21 10:54:50 takayama Exp $ +$Id: tr-ja.oxt,v 1.9 2005/05/11 06:40:10 takayama Exp $ +$OpenXM: OpenXM/src/asir-contrib/testing/tr-ja.oxt,v 1.8 2005/05/04 05:47:03 takayama Exp $ 注意: testing/tr.rr では quote を quotetolist で list に変換して扱うため, 下の仕様とはことなり, list 型でデータを戻す場合も多い. @@ -13,25 +13,26 @@ nkf -e tr.oxt | oxgentexi --noSorting --title 'Term re begin: AAA01| +現在のサンプル実装には bug が沢山でかつ, +サンプル実装は効率も悪い. +ご注意! + @c --------------------------------------------------------- @section 変数パターンと関数パターン 変数パターン +@itemize @bullet +@item pn(x), 任意のものにマッチ. マッチしたものを x に bind. +@item pn(x,qt.is_integer(x)), x が @code{qt.is_integer(x)} をみたせばマッチ. -pn(x) 任意のものにマッチ. マッチしたものを x に bind. -pn(x,qt_is_integer(x)) +@item Todo; 関数にマッチする fn は多分いらない. qt.is_function(x) で OK. +@end itemize -Todo; fn は多分いらない. qt_is_function(x) で OK. -fn(f) 任意の関数にマッチ. マッチした関数名を f に bind. -fn(f,pn(x),pn(y)) 任意の関数にマッチ. マッチした関数名を f に bind. - f の引数を x, y に bind - - パターンは quote で与える. 予約語 tr.and, tr.or, tr.not はパターンのマッチに関して論理演算をおこなう. たとえば -quote(tr.and(pn(x,"qt.is_integer"),pn(x,"qt.is_non_negative",x))) +quote(tr.and(pn(x,qt.is_integer(x)),pn(x,qt.is_non_negative(x))) は x が 整数で - が先頭についていない場合マッチする. end: @@ -42,16 +43,16 @@ begin: AAA011| @section 実装のための補助関数 @itemize @bullet -@item @code{quotetolist}, @code{listtoquote}, @code{listtoquote_str} +@item @code{quotetolist}, @code{listtoquote}, @code{listtoquote_str} ; quote と list の間の変換. -@item @code{quote_to_funargs}, @code{funargs_to_quote}, @code{get_function_name} +@item @code{quote_to_funargs}, @code{funargs_to_quote}, @code{get_function_name} ; quote と funarg 形式の変換. 実験的関数のマニュアルを参照. -@item @code{quotetoobj}, @code{eval_quote} +@item @code{quotetoobj}, @code{eval_quote} ; quote と object の間の変換. -@item @code{quote_input_form}, @code{ctrl("print_quote",1)}, +@item @code{quote_input_form}, @code{ctrl("print_quote",1)} ; quote と文字列の間の変換. -@item @code{quote_flatten} - 実験的関数のマニュアルを参照. +@item @code{quote_flatten} ; + 左結合的に作り変える. 実験的関数のマニュアルを参照. @end itemize end: @@ -111,6 +112,9 @@ end: begin: qt.is_rational(Q) quote データ {Q} が形式的に有理数の時 1. example: qt.is_rational(quote(1/2)); + example: + [1321] qt.is_rational(quote(1/2+4)); + 1 ref: qt.cancel_number end: @@ -147,7 +151,7 @@ begin: qt.parenthesis(Qobj) description: 参考; @itemize - @item まだ書いてない. + @item まだ実装してない. @item noro_simplify.rr の @code{noro_simplify.remove_paren()} がすでに実現づみ? (加える方はだめみたい) @@ -174,7 +178,14 @@ end: begin: qt.cancel_number(Q) Quote {Q} の中で有理数があれば通分する. - ref: qt.is_rational + example: + [1320] qt.cancel_number(quote(2/4)); + quote(1/2) + changelog: + @itemize @bullet + @item 2005.05.04, the initial version. Taylor 展開の係数の簡約のために作製. + @end itemize + ref: qt.is_rational end: begin: qt.add_paren(Q) @@ -183,15 +194,109 @@ begin: qt.add_paren(Q) end: begin: qt.add_paren0(Q) - 無条件に {Q} を ( ) する. + 無条件に {Q} を ({Q}) にする. end: +begin: qt.map_arg(F,Q) + 関数 F を quote データ {Q} の すべてのノードに再帰的に + apply した quote データを戻す. + example: qt.map_arg(nn,quote(sin(@pi)+2/3)) + nn(nn(sin(nn(@pi)))+nn(nn(2)/nn(3))) +end: +begin: qt.sin_int(Q) + {Q} が整数なら quote(0) を戻す. + {Q} が整数でないなら quote(sin(Q*@@pi)) を戻す. +description: + @itemize @bullet + @item 書き換え規則の右辺用の関数. + @item tr.simp_sin (sinを含む式の簡単化のサンプル実装)や + 例のプログラムで利用されている. + @end itemize +changelog: + @itemize @bullet + @item 2005.04.02 の初期版から存在する関数なので仕様が古い. list を戻す. + @item 2005.05.08 quote を戻すように書き換えた. 名前もよくないかも, しかし + とりあえずこのまま. + @end itemize +ref: tr.simp_sin +end: + +begin: qt.sin_int2(Q) +ref: qt.sin_int +end: + +begin: qt0021| +@c -------------------------------------------------------------------- +@section qt, quote を別のものに変換する. +end: + +begin: qt.input_form(Q) + {Q} を文字列に変換する. + {Q} がリスト, ベクトル, 行列の場合にはその要素を文字列に変換する. +changelog: + @itemize @bullet + @item 2005.05.08, the initial version. + qt.hc_etov の結果表示に. quote_input_form は Q がリストの場合不便. + @end itemize +ref: quote_input_form, + qt.hc_etov +end: + + begin: qt003| @c -------------------------------------------------------------------- -@section tr その他 +@section qt, quote で分散多項式, 冪級数を実現するための補助関数. end: +begin: qt.gtlex(f,g) +{f} は {g} より quote tree の lex order で大きい. +description: + quote tree の lex order は次のように決める. + @itemize @bullet + @item Todo; まだ実装してない. +@c 4/15 夜. 実装は明日講義の準備の終了後か? + @item 不定元は不定元の順序. + @item 不定元より +, - , *, /, ^ 等の node は大きい. + たとえば x < power(x,2) (power(x,2) は x^2 の意味) + @item あとは再帰的. times(x,y) < power(x,y) だが, + times(x,y) と times(p,q) は x と p の比較, これできまらないなら, + y, q の比較. + @end itemize + +end: + +begin: qt.dtoq(F,V) +分散表現多項式 {F} を quote に変換する. +{V} は変数リスト. +description: + @itemize @bullet + @item 変数リストが空のときは x_1, x_2, ... を用いる. + @end itemize +example: + F=dp_ptod((x-y-z)^3,[x,y]); + qt.dtoq(F,[]); + quote(x_1^3+(-3)*x_1^2*x_2+3*x_1*x_2^2+(-1)*x_2^3+(-3*z)*x_1^2 + +6*z*x_1*x_2+(-3*z)*x_2^2+3*z^2*x_1+(-3*z^2)*x_2+(-z^3)) +changelog: + @itemize @bullet + @item 2005/4/21, the initial version. + @end itemize +ref: qt.qtod +end: + +begin: qt.qtod(F,V) +quote {F} を 分散表現多項式に変換する. +{V} は変数リスト. +description: + @itemize @bullet + @item 変数リストが空のときは x_1, x_2, ... を用いる. + @item まだ実装してない. + @end itemize +ref: qt.qtod +end: + + begin: qt.vars(Q) {Q} に現れる変数を戻す. description: @code{vars(Obj)} の qt 版. @@ -204,15 +309,99 @@ begin: qt.etov_pair(Q) {Q} に p^q の形の元を探して [p,q] をリストにして戻す. description: @itemize @bullet - @item @code{dp_etov} の類似を作るための準備(2005/05/04). - @end itemize + @item この関数は木構造の中の p^q の形の元をすべて探索する. + @end itemize example: [2410] ctrl("print_quote",1); 1 [2411] qt.etov_pair(quote(3*x^4*y^(-3))); [[[internal,y],[u_op,(),[u_op,-,[internal,3]]]],[[internal,x],[internal,4]]] + changelog: + @itemize @bullet + @item 2005.05.04, dp_etov に似た関数を qt で作るための準備. + @end itemize + ref: qt.vars end: +begin: qt.hm(Q) + {Q} の頭項を戻す. + example: + [1314] Q=qt.dtoq(-4/3*<<1,4,5>>,[x,y,z]); + quote((-4/3)*x*y^4*z^5) + [1315] qt.hm(Q); + quote((-4/3)) + [1316] qt.hop(Q); + * + [1317] QQ=qt.rest(Q); + quote(x*y^4*z^5) + [1318] qt.hm(QQ); + quote(x) + [1319] qt.rest(QQ); + quote(y^4*z^5) + [1320] qt.hm(qt.rest(QQ)); + quote(y^4) + ref: qt.rest, qt.hop +end: + +begin: qt.rest(Q) + {Q}が a+b+c+... の時 b+c+...を戻す. dp_rest のまね. + description: + @itemize @bullet + @item I_BOP の時に第2引数を flatten_quote してから戻す. + @end itemize + example: + [1667] load("taka_series.rr")$ + [1668] Q=tkseries.expand1(cos(x),x,0,5); + quote(1+(-1/2)*x^2+1/24*x^4+oO(x, 6)) + [1769] qt.hm(qt.rest(Q)); + quote((-1/2)*x^2) + [1770] QQ=qt.rest(Q); + quote((-1/2)*x^2+1/24*x^4+oO(x, 6)) + changelog: + @itemize @bullet + @item 2005.05.04, tkseries.expand の結果に対して, + 毎回 flatten_quote をやらないと左結合的にならないのは何故か? + BUG 安定的に動作するかどうか不安. + @item 2005.05.04, いよいよ 係数と exponet ベクトルの取り出しを書く準備が完了. + @item 2005.05.05, Q=a*b なら qt.rest(Q) = b となるように変更. + 係数の取り出し, exponent ベクトルの取り出しにはこれが便利. + @end itemize + ref: qt.hm, qt.hop, qt.dtoq, dp_hm, dp_rest +end: + +begin: qt.hop(Q) + {Q} の頭節が binary operator (I_BOP) の時 binary operator の名前を戻す. + changelog: + @itemize @bullet + @item 2005.05.05, 始めての版. + @end itemize +end: + +begin: hc_etov(Q,V) + quote {Q} を変数リスト {V} のモノミアルとみて, hc (leading coefficient) + と exponet ベクトルを quote 型で戻す. + description: + Q = hc*p1*p2* ... なる形を仮定. これ以外の形では動作保証なし. + example: + [1967] R=qt.hc_etov(quote(-3/2*x*z^3),[x,y,z]); + [<...quoted...>,[ <...quoted...> 0 <...quoted...> ]] + [1968] qt.input_form(R); + [quote(1*(-1)*3/2),[ quote(1) 0 quote(3) ]] + changelog: + @itemize @bullet + @item 2005.05.05, + qt.hc_etov(quote(-3/2*x*z^3),[x,y,z]); でテストするもまだ動作へん. + @item if (A[0] == I_MINUS) の部分をつけて解決. 05 夜. + まだ bug が残るかもしれず. + @item Todo; qtodl (distributed poly in quote を list 形式で). + @item Todo; quote の distributed poly の比較. + @item Todo; 多変数のテイラー展開. テイラー展開の積. + @item Todo; Asir ドリルの 10 行 Buchberger アルゴリズムの実装をここで + 定義した quote 関数で書く. + @end itemize +end: + + begin: tr| @c -------------------------------------------------------------------- @@ -225,29 +414,27 @@ begin: tr.match0(Qobj,P) を戻す. example: tr.match0(quote(1+2*3),quote(pn(x)+pn(y))) x に quote(1), y に quote(2*3) - tr.match0(quote(1+2*3),quote(pn(x)+pn(y,"qt.is_integer",y))) - qt.is_integer(2*3) は 0 なので y にはマッチしない. + tr.match0(quote(1+2*3),quote(pn(x)+pn(y,qt.is_integer(y)))); + qt.is_integer(quote(2*3)) は 0 なので y にはマッチしない. + example: + [2991] tr.match0(quote(1),quote(pn(c,qt.is_dependent(c,x)))); + 0 + [2992] tr.match0(quote(x^3+1),quote(pn(c,qt.is_dependent(c,x)))); + 1 end: begin: pn(X) pn(x) は任意の quote object にマッチし, 名前 x をつける. + quote の中で使う特別な関数記号. + pn は variable PatterN (変数パターン)の略. description: tr.match0(quote(1+2*3),quote(pn(x)+pn(y))) は 1 を戻すが, - tr.match0(quote(1+2*3),quote(pn(x)+pn(y,tr.is_integer,x))) は 0 をもどす. + tr.match0(quote(1+2*3),quote(pn(x)+pn(y,tr.is_integer(y)))) は 0 をもどす. 2*3 は integer から作られた fnode ではあるが integer ではないので qt.is_integer が 0 を戻すため. end: - -begin: tr.match0_act(Qobj,P,Act) - quote データ {Qobj} が パターン {P} に適合すれば {Act} を呼び出しその値を戻す. - パターン {P} にマッチしないときは 0. - example: tr.match0_act(quote(1+2*3),quote(pn(x)+pn(y)),[myadd,x,y]) -end: -begin: tr.or_match0_act(Qobj,Rules) -end: - begin: tr.apply_rule1(Qobj,P,Act) quote データ {Qobj} の木を幅優先探索し, パターン {P} に適合するものがあるときは {Act} を呼び出しその値を戻す. @@ -256,12 +443,16 @@ begin: tr.apply_rule1(Qobj,P,Act) マッチしない場合は Qobj をそのまま戻す (これが再帰的に適用される). description: - ここで sin_int(X) は X が integer の時は quote(0) を戻し, + ここで qt.sin_int(X) は X が integer の時は quote(0) を戻し, そうでないときは quote(sin(X*@@pi)) を戻す. - 深さ優先で書き換えをするには 関数 sin_int の中でまた tr.apply_rule1 を呼び出せば - よい. + 深さ優先で書き換えをするには 関数 qt.sin_int の中でまた + tr.apply_rule1 を呼び出せばよい. - example: tr.apply_rule1(quote(1+sin(2*@pi)),quote(sin(pn(x)*@pi)),[sin_int,x]) + example: + [2215] tr.apply_rule1(quote(1+sin(2*@pi)),quote(sin(pn(x)*@pi)), + ["qt.sin_int",x]); + + quote(1+0) end: @@ -286,7 +477,47 @@ end: begin: tr.make_binding(Qobj,P) end: +begin: hoge41| +@section tr 簡単化関数のサンプル実装 +@itemize @bullet +@item 2005.05.08 簡単化関数は注意深く実装しないと, 無限ループにおちいったり, +簡単化のやりのこしがでたりする. +とりあえずまとめておくが, これらの実装はまだまだである. +@end itemize + +end: + +begin: tr.simp_zero(Q) + 式 {Q} の中から 0 や 1 をけす. たとえば 1*x は x へ. 0*1 は 0 へ. + x+0 は 0 へ. +changelog: + @itemize @bullet + @item 2005.04 のおわりに initial version? tkseries.expand1 用. + @end itemize +end: + +begin: tr.simp_unary(Q) + "-"を消すようにする. たとえば +-x は -x へ. -(-x) は x へ. +changelog: + @itemize @bullet + @item 2005.04 のおわりに initial version? tkseries.expand1 用. + @end itemize +end: + +begin: tr.simp_sin(Q) + 式 {Q} の中の sin(整数 @@pi) を 0 にする. +example: + [2217] tr.simp_sin(quote(sin(2*@pi)+sin(@pi/2))); + quote(sin(@pi()/2)) +changelog: + @itemize @bullet + @item 2005.04 のおわりに initial version? tkseries.expand1 用. + @item 2005.05.08 tr.simp_sin(quote(sin(2*sin((1/2)*@@pi)))); は無限ループ bug. + まだ直してない. + @end itemize +end: + begin: zzz00| @section 例題 @@ -299,16 +530,19 @@ example: /* 準備 */ extern P,A; P=quote(sin(pn(x)*@pi)); /* パターン */ - A=[sin_int,x] /* action, action 関数 */ + A=["sin_int",x]; /* action, action 関数 */ def sin_int(X) { X = tr.apply_rule1(X,P,A); /* 子供に [P,A] を再帰的に適用 */ if (qt.is_integer(X)) return qt.zero(); - else qt.replace(sin(y*@pi),[[y,X]]); /* sin(x*@pi) をそのまま戻す.*/ + else qt.replace(quote(sin(y*@pi)),[[y,X]]); + /* sin(x*@pi) をそのまま戻す.*/ } /* 計算 */ Qobj=quote(1+sin(sin(2*@pi)*@pi)*sin((1/2)*@pi)); tr.apply_rule1(Qobj,P,A); +example_description: + ファイルにセーブして実行のこと. @code{Debug=1;} とすると変形の様子がわかる. end: @c ------------------------------------------------------ @@ -347,19 +581,24 @@ example: } /* integral(c*f) => c*integral(f) */ - def qt.independent(F,X) { return !qt.dependent(F,X); } - S2=[quote(integral(pn(c,"qt.independent",c,x))*f)), + def is_independent(F,X) { return !qt.is_dependent(F,X); } + S2=[quote(integral(pn(c,is_independent(c,x))*f)), [int_linear2,c,f]]; def int_linear2(X,Y) { return qt.replace(quote(c*integral(f)),[[c,X],[f,Y]]); } - apply_or_rules(quote(integral(a*x^2+x+2/x)),[S1,S2]); + tr.apply_or_rules(quote(integral(a*x^2+x+2/x)),[S1,S2]); これをこれ以上書き換えが起きないまで繰り返す. このルールの場合答えは a*integral(x^2)+integral(x)+integral(2/x); quote(integral(x^pn(n))) --> x^(n+1)/(n+1) or log(x) を書く. +example: + tr.match0(quote(c*x),quote(pn(c,is_independent(c,x))*f)); +example_description: + 2005.05.08, Todo; Bug. この例が正しく動いてくれない. + end: @c --------------------------------------------------------- @@ -367,10 +606,12 @@ end: begin: zzz04|sortKey: zzz04 description: +--------------------- 5/8 ここまで修正. ---------------------- 例題 簡単な構文解析 example: + todo; この例はまだチェックしてない. 式(expression) は 式+式 | 式*式 | (式) | 整数 extern R1,R2,R3,R4,S1,S2,S3,S4; @@ -426,6 +667,58 @@ begin: misc| end: +begin: hoge001| +@section Talor 展開 (taka_series.rr の解説) + +end: + +begin: tkseries.expand1(Q,X,A,N) +{Q} を 変数 {X} に関して, {X}={A} の近傍で {N} 次まで +Taylor 展開する. + +description: +@itemize @bullet +@item qt パッケージと tr パッケージの例題として書いている. +@item Bug だらけです. +@item Todo; 多変数の Taylor 展開. +@end itemize + +example: +[1258] load("tr.rr"); +1 +[1359] load("taka_series.rr"); +1 +[1373] tkseries.expand1(cos(x),x,0,6); +quote(1+(-1/2)*x^2+1/24*x^4+(-1/720)*x^6+oO(x, 7)) +example_description: + cos の値の計算に tr.simp_sin を用いている. + Bug; この関数は不完全なので, 係数の計算はうまく行かない場合も多い. + +example: +[1374] tkseries.expand1(quote(f(x)),x,0,3); +quote(f(0)+calc_diff(f(0), 0)/1*x^1 + +calc_diff(calc_diff(f(0), 0), 0)/2*x^2 + +calc_diff(calc_diff(calc_diff(f(0), 0), 0), 0)/6*x^3+oO(x, 4)) +example_description: + Todo; f の微分に 0を代入したものは f(0), f_1(0), f_2(0), ... + (それぞれ f(0), f'(0), f''(0), ... の意味) + とした方がいいだろう. + +changelog: +@itemize @bullet +@item 2005.04 はじめての版. +@end itemize + +end: + +begin: hoge002| + +@section 記号微分 (taka_qtdiff.rr の解説) + +まだ実装してない. + +end: + begin: exp| @c ------------------------------------------------ @@ -433,12 +726,6 @@ begin: exp| end: -begin: qt.map_arg(F,Q) - 関数 F を quote データ {Q} の すべてのノードに再帰的に - apply した quote データを戻す. - example: qt.map_arg(nn,quote(sin(@pi)+2/3)) - nn(nn(sin(nn(@pi)))+nn(nn(2)/nn(3))) -end: begin: todo| @@ -533,64 +820,32 @@ begin: powerSeries| 巾級数の取扱, dp の pretty print のため. qt.qttodp(Qobj | vlist, order?) quote から dp を作る. - exponent が数でないと作れず. - qt.dptoqt(Qobj | vlist) dp から quote を作る. vlist は属性で対応? + exponent が数でないと作れず. qt.expand, qt.sort, qt.ht, qt.rest, qt.mtov も基礎関数として欲しい. end: -begin: MonomialSimplifier| +begin: monomialSimplifier00| -@subsection モノミアルを標準形へ (builtinで?) +@section モノミアルを標準形へ (builtinで? Monomial の simplifier) -example: +changelog; +@quotation +Monomial simplifier を通してから, +パターンマッチをしないと場合わけが多すぎる. +これは必須だろう. +@end quotation + +Example; +@example x^1 --> x (x*y)*(z*t) --> x*y*z*t x*2*y*4 --> 8*x*y (指定した変数以外は可換とする) x*x^3 --> x^4 x*(-y)*z --> -x*y*z ((x)) --> x これは noro_simplify.rr noro_simplify.remove_paren() が対応 - +@end example end: -4/15 夜. 実装は明日講義の準備の終了後か? -begin: qt.gtlex(f,g) -{f} は {g} より quote tree の lex order で大きい. -description: - quote tree の lex order は次のように決める. - @itemize - @item 不定元は不定元の順序. - @item 不定元より +, - , *, /, ^ 等の node は大きい. - たとえば x < power(x,2) (power(x,2) は x^2 の意味) - @item あとは再帰的. times(x,y) < power(x,y) だが, - times(x,y) と times(p,q) は x と p の比較, これできまらないなら, - y, q の比較. - @end itemize - -end: - -begin: qt.dtoq(F,V) -分散表現多項式 {F} を quote に変換する. -{V} は変数リスト. -description: - @itemize @bullet - @item 変数リストが空のときは x_1, x_2, ... を用いる. - @item Changelog. 2005/4/21 - @end itemize -example: - F=dp_ptod((x-y-z)^3,[x,y]); - qt.dtoq(F,[]); - quote(x_1^3+(-3)*x_1^2*x_2+3*x_1*x_2^2+(-1)*x_2^3+(-3*z)*x_1^2 - +6*z*x_1*x_2+(-3*z)*x_2^2+3*z^2*x_1+(-3*z^2)*x_2+(-z^3)) -ref: qt.qtod -end: - -begin: qt.qtod(F,V) -quote {F} を 分散表現多項式に変換する. -{V} は変数リスト. -description: - 変数リストが空のときは x_1, x_2, ... を用いる. -ref: qt.qtod -end: