# How to control counters generating a matrix with lambda?

I am trying to generate this famous matrix without the expected diagonal of 1's. So the expected output is as follows:

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
[1,]    1    0    0    0    0    0    0    0    0     0     0
[2,]    1    2    0    0    0    0    0    0    0     0     0
[3,]    1    3    3    0    0    0    0    0    0     0     0
[4,]    1    4    6    4    0    0    0    0    0     0     0
[5,]    1    5   10   10    5    0    0    0    0     0     0
[6,]    1    6   15   20   15    6    0    0    0     0     0
[7,]    1    7   21   35   35   21    7    0    0     0     0
[8,]    1    8   28   56   70   56   28    8    0     0     0
[9,]    1    9   36   84  126  126   84   36    9     0     0
[10,]    1   10   45  120  210  252  210  120   45    10     0
[11,]    1   11   55  165  330  462  462  330  165    55    11


I generated it with the following loop in R:

n = 11
asymmetric.lower.triang.Pascal = matrix(0,n,n)
for(i in 1:n){
for(k in 0:(i-1))
asymmetric.lower.triang.Pascal[i,k+1] = choose(i,k)
}


I can generate the Pascal matrix in SageMath with the loop:

r = 10
matrix(ZZ, r, lambda i, j: binomial(i, j))

[  1   0   0   0   0   0   0   0   0   0]
[  1   1   0   0   0   0   0   0   0   0]
[  1   2   1   0   0   0   0   0   0   0]
[  1   3   3   1   0   0   0   0   0   0]
[  1   4   6   4   1   0   0   0   0   0]
[  1   5  10  10   5   1   0   0   0   0]
[  1   6  15  20  15   6   1   0   0   0]
[  1   7  21  35  35  21   7   1   0   0]
[  1   8  28  56  70  56  28   8   1   0]
[  1   9  36  84 126 126  84  36   9   1]


but I want to get rid of the diagonal of 1's with the same manipulation of the counters or indices I used above. I have no idea what lambda does, having found the basic code for matrix construction in SageMath online. So the question amounts to whether I could make i run from 1 to n while j runs from 0 to n - 1 or viceversa.

edit retag close merge delete

"I have no idea what lambda does" - it means you need to learn some Ptthon. E.g. "Think Python" may be a good starting point: https://greenteapress.com/thinkpython...

( 2023-05-27 15:01:37 +0200 )edit

Sort by ยป oldest newest most voted

Is this what you want?

r = 10
matrix(ZZ, r, lambda i, j: binomial(i+1, j) if j<=i else 0)

more

This is great! However, I was hoping for some insight into the workings of the counters.

( 2023-05-27 15:24:54 +0200 )edit

You need to learn Python, in particular about lambda functions. See link to Think Python above.

( 2023-05-27 15:27:03 +0200 )edit