Ask Your Question

summation of matrices

asked 2017-11-17 05:15:03 -0500

Deepak Sarma gravatar image

Can we use sum function to add a number of matrices? If not, then how to add a number of matrices with just a single command. Suppose I want to create 10 random matrices of order 5x5 and then want their sum as a result. I tried the following command, but it didn't work.

A_{i}=random_matrix(ZZ,5,5) for i in range(1,10); sum(A_{i}, i, 1, 10)

Note: I even could not generate 10 random matrices with the above command.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2017-11-17 05:54:06 -0500

tmonteil gravatar image

The following works:

sage: L = [random_matrix(ZZ,5,5) for i in range(1,10)] ; L
sage: sum(L)

or directly without creating a list first:

sage: sum(random_matrix(ZZ,5,5) for i in range(1,10))
edit flag offensive delete link more


Thank you.

Deepak Sarma gravatar imageDeepak Sarma ( 2017-11-17 07:04:44 -0500 )edit

answered 2017-11-17 23:19:36 -0500

Deepak Sarma gravatar image

L=[random_matrix(ZZ,5,5) for _ in range(1,10)]; sum(L[i] for i in range (0,9))

edit flag offensive delete link more

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


Asked: 2017-11-17 05:15:03 -0500

Seen: 55 times

Last updated: Nov 17 '17