Subject: stack overflow with recursive definitions (crash)
From: Robert Dodier
Date: Sun, 27 Nov 2005 14:55:23 -0700
On 11/26/05, van.Nek@gmx.net wrote:
> Example: binomial coefficients.
> bc[n,n]: 1$
> bc[n,0]: 1$
> bc[n,k]:= bc[n-1,k]+bc[n-1,k-1]$
>
> Such an experiment does not work.
i believe the problem is that the hash key in bc[n,n]: 1
is assumed to be a literal "n" (i.e. maxima doesn't understand
that "n" means any number here).
two ways that i can see to get a similar effect --
(1) initialize the base cases explicitly --
bc[0,0]: bc[1,1]: bc[2,2]: bc[3,3]: .... : 1;
bc[1,0]: bc[2,0]: bc[3,0]: .... : 1;
(2) use pattern matching --
matchdeclare ([nn, kk], integerp);
tellsimpafter (bc [nn, nn], 1);
tellsimpafter (bc [nn, 0], 1);
tellsimpafter (bc [nn, kk], bc [nn - 1, kk] + bc [nn - 1, kk - 1]);
bc [3, 2] => bc[2,2] + bc[2,1]
''% => bc[1,1] + bc[1,0] + 1
''% => 3
bc [3, 2], infeval => 3
for what it's worth,
robert dodier