ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 17 Nov 2017 23:19:36 -0600summation of matriceshttp://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 05:15:03 -0600http://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>
http://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 05:54:06 -0600http://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>
http://ask.sagemath.org/question/39638/summation-of-matrices/?comment=39642#post-id-39642Thank you.Fri, 17 Nov 2017 07:04:44 -0600http://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>
http://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))Fri, 17 Nov 2017 23:19:36 -0600http://ask.sagemath.org/question/39638/summation-of-matrices/?answer=39643#post-id-39643