How to define the recurrence to get the reverted indexing in wxMaxima?

40 Views Asked by At

For example I use the case of a finite continued fraction.

Defining the recurrence like this:

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

Asking for a depth of 3:

    P[3];

Results is:

depth is 3, numbering of indices is bottom up

On can ask for a different depth, any time:

    P[4];

Result is:

depth is 4, numbering of indices is bottom up

How can one write a similar definition that has the numbering of indices reverted like this:

depth is 3, numbering of indices is top down

Asking for a different depth should be possible, without extra efford.

depth is 4, numbering of indices is top down

I tried:

    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;

Result is ok, but I don't like the extra symbol M. enter image description here

1

There are 1 best solutions below

0
On

Here's a possibility. I use subst to substitute a new value into the most deeply nested term.

(%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

Calling subst is a little clumsy; maybe there is a more elegant way.