Wednesday 21 November 2007

big list - Computer Algebra Errors

This error affects all versions of Mathematica from 6 to 8. The result of a function depends on what letter is chosen for argument when calling it. In the simplest case it can be illustrated as follows:



in:



$A[text{x_}]text{:=}sum _{k=0}^{x-1} x $



$A[k]$



$A[z]$



out:



$1/2 (-1 + k) k$



$z^2$



The correct answer is evidently, the later. This behavior affects not only sums but also integrals, so one have to check so that the letter user for the argument not to coincide with the index variable used for definition. In the case of recursion this becomes very difficult. The following example shows that moving a factor not dependent on the index variable out of the sum sign changes the result:



in:



A1[0,x_]:=1
A2[0,x_]:=1

A1[n_,x_]:=Sum[A1[-1 - j + n, x]*Sum[A1[j, k], {k, 0, -1 + x}], {j, 0, -1 + n}]
A2[n_,x_]:=Sum[Sum[A2[j, k]*A2[-1 - j + n, x], {k, 0, -1 + x}], {j, 0, -1 + n}]

A1[1,x]/.x->2
A1[2,x]/.x->2
A1[3,x]/.x->2

A2[1,x]/.x->2
A2[2,x]/.x->2
A2[3,x]/.x->2

A2[1,2]
A2[2,2]
A2[3,2]


out:



2
5
13

2
5
12

2
5
13

No comments:

Post a Comment