Bill Allombert on Sun, 13 Nov 2005 16:21:31 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Little problems


On Sat, Nov 12, 2005 at 11:35:44PM +0100, Martin Larsen wrote:
> It's repeated 3 times make the error clear. 
> 3. time is correct. If I do the same thing with 
> 2 for-loops the correct result is there 1. time. 
> 
> >matrix(6,6,i,j,a[i+1,j+1]=a[i,j]+3*a[i,j+1]+2*a[i,j+2])
> 
> [1 0 0 0 0 0]
> 
> [3 1 0 0 0 0]
> 
> [11 6 1 0 0 0]
> 
> [33 31 9 1 0 0]
> 
> [99 126 60 12 1 0]
> 
> [297 477 306 98 15 1]

This is because we first loop on i and then on j:

Try instead:
matrix(6,6,j,i,a[i+1,j+1]=a[i,j]+3*a[i,j+1]+2*a[i,j+2])~

With matrix(6,6,i,j,a[i+1,j+1]=a[i,j]+3*a[i,j+1]+2*a[i,j+2]),
we do:

a[2,2]=a[1,1]+3*a[1,2]+2*a[1,3]
a[3,2]=a[2,1]+3*a[2,2]+2*a[2,3]
a[4,2]=a[3,1]+3*a[3,2]+2*a[3,3]
a[5,2]=a[4,1]+3*a[4,2]+2*a[4,3]
a[6,2]=a[5,1]+3*a[5,2]+2*a[5,3]
a[7,2]=a[6,1]+3*a[6,2]+2*a[6,3]
a[2,3]=a[1,2]+3*a[1,3]+2*a[1,4]
a[3,3]=a[2,2]+3*a[2,3]+2*a[2,4]

So a[3,2] depends on a[2,3] which will be computer after.

Does it explain your problem ?

Cheers,
Bill