如何定义递归以获得wxMaxima中的恢复索引?

问题描述 投票:0回答:1

例如,我使用有限连分数的情况。

像这样定义递归:

    remarray(P)$
    P[0]:a[0];
    P[n]:=a[n]+1/P[n-1];

要求深度为3:

    P[3];

结果是:

depth is 3, numbering of indices is bottom up

可以随时要求不同的深度:

    P[4];

结果是:

depth is 4, numbering of indices is bottom up

如何编写一个类似的定义,将索引编号恢复为这样:

depth is 3, numbering of indices is top down

要求不同的深度应该是可能的,无需额外的努力。

depth is 4, numbering of indices is top down

我尝试过:

    kill(M)$
    remarray(P)$
    P[0]:a[M];
    P[n]:=a[M-n]+1/P[n-1];
    P[3]$
    %,M=3;
    P[4]$
    %,M=4;

结果还可以,但我不喜欢多余的符号M。 enter image description here

maxima wxmaxima
1个回答
0
投票

这是一种可能性。我使用

subst
将新值替换为最深嵌套的术语。

(%i2) P[0]: a[0];
(%o2)                          a
                                0
(%i3) P[n]:= subst (a[n - 1] = a[n - 1] + 1/a[n], P[n - 1]);
                                          1
(%o3)       P  := subst(a      = a      + --, P     )
             n           n - 1    n - 1   a    n - 1
                                           n
(%i4) P[1];
                             1
(%o4)                        -- + a
                             a     0
                              1
(%i5) P[2];
                             1
(%o5)                     ------- + a
                          1          0
                          -- + a
                          a     1
                           2
(%i6) P[3];
                             1
(%o6)                   ------------ + a
                           1            0
                        ------- + a
                        1          1
                        -- + a
                        a     2
                         3
(%i7) P[4];
                             1
(%o7)                ----------------- + a
                          1               0
                     ------------ + a
                        1            1
                     ------- + a
                     1          2
                     -- + a
                     a     3
                      4

打电话

subst
有点笨拙;也许有更优雅的方式。

© www.soinside.com 2019 - 2024. All rights reserved.