Ask Your Question
1

How to control counters generating a matrix with lambda?

asked 2023-05-27 04:02:57 +0200

toni gravatar image

updated 2023-05-27 04:16:07 +0200

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 flag offensive close merge delete

Comments

"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...

Max Alekseyev gravatar imageMax Alekseyev ( 2023-05-27 15:01:37 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-27 05:55:31 +0200

Max Alekseyev gravatar image

updated 2023-05-27 05:55:51 +0200

Is this what you want?

r = 10
matrix(ZZ, r, lambda i, j: binomial(i+1, j) if j<=i else 0)
edit flag offensive delete link more

Comments

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

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

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

Max Alekseyev gravatar imageMax Alekseyev ( 2023-05-27 15:27:03 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-05-27 04:02:57 +0200

Seen: 288 times

Last updated: May 27 '23