ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 18 Nov 2017 06:19:36 +0100summation of matriceshttps://ask.sagemath.org/question/39638/summation-of-matrices/ 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.
Fri, 17 Nov 2017 12:15:03 +0100https://ask.sagemath.org/question/39638/summation-of-matrices/Answer by tmonteil for <p>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.</p>
<p>A_{i}=random_matrix(ZZ,5,5) for i in range(1,10);
sum(A_{i}, i, 1, 10)</p>
<p>Note: I even could not generate 10 random matrices with the above command.</p>
https://ask.sagemath.org/question/39638/summation-of-matrices/?answer=39639#post-id-39639The 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))
Fri, 17 Nov 2017 12:54:06 +0100https://ask.sagemath.org/question/39638/summation-of-matrices/?answer=39639#post-id-39639Comment by Deepak Sarma for <p>The following works:</p>
<pre><code>sage: L = [random_matrix(ZZ,5,5) for i in range(1,10)] ; L
sage: sum(L)
</code></pre>
<p>or directly without creating a list first:</p>
<pre><code>sage: sum(random_matrix(ZZ,5,5) for i in range(1,10))
</code></pre>
https://ask.sagemath.org/question/39638/summation-of-matrices/?comment=39642#post-id-39642Thank you.Fri, 17 Nov 2017 14:04:44 +0100https://ask.sagemath.org/question/39638/summation-of-matrices/?comment=39642#post-id-39642Answer by Deepak Sarma for <p>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.</p>
<p>A_{i}=random_matrix(ZZ,5,5) for i in range(1,10);
sum(A_{i}, i, 1, 10)</p>
<p>Note: I even could not generate 10 random matrices with the above command.</p>
https://ask.sagemath.org/question/39638/summation-of-matrices/?answer=39643#post-id-39643L=[random_matrix(ZZ,5,5) for _ in range(1,10)];
sum(L[i] for i in range (0,9))Sat, 18 Nov 2017 06:19:36 +0100https://ask.sagemath.org/question/39638/summation-of-matrices/?answer=39643#post-id-39643