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.Tue, 12 Jan 2021 16:04:27 +0100Combining 'for'https://ask.sagemath.org/question/55244/combining-for/I have the matrices:
x = lambda i: SR.var(f"x_{i}", latex_name=f"x_{{{i}}}")
a = matrix(2,2,[x(i) for i in [0 .. 3]]); A = matrix(4,4,[x(i) for i in [4 .. 19]])
And I would like to genarate four expressions with: j=0, j=1, j=2 and j=3
for j in (0..3):
e=[ x(j) - sum(A[j,i] for i in (0..1)) ]
e = -x_16 - x_17 + x_3
But the command above give me just the last one, i.e., -x_16 - x_17 + x_3. Any suggestions?Mon, 11 Jan 2021 20:57:19 +0100https://ask.sagemath.org/question/55244/combining-for/Answer by John Palmieri for <p>I have the matrices: </p>
<pre><code>x = lambda i: SR.var(f"x_{i}", latex_name=f"x_{{{i}}}")
a = matrix(2,2,[x(i) for i in [0 .. 3]]); A = matrix(4,4,[x(i) for i in [4 .. 19]])
</code></pre>
<p>And I would like to genarate four expressions with: j=0, j=1, j=2 and j=3</p>
<pre><code>for j in (0..3):
e=[ x(j) - sum(A[j,i] for i in (0..1)) ]
e = -x_16 - x_17 + x_3
</code></pre>
<p>But the command above give me just the last one, i.e., -x_16 - x_17 + x_3. Any suggestions?</p>
https://ask.sagemath.org/question/55244/combining-for/?answer=55247#post-id-55247Do you mean something like this?
e = []
for j in (0..3):
e.append(x(j) - sum(A[j,i] for i in (0..1)))
or
e=[ x(j) - sum(A[j,i] for i in (0..1)) for j in (0..3)]
Tue, 12 Jan 2021 05:11:55 +0100https://ask.sagemath.org/question/55244/combining-for/?answer=55247#post-id-55247Comment by phcosta for <p>Do you mean something like this?</p>
<pre><code>e = []
for j in (0..3):
e.append(x(j) - sum(A[j,i] for i in (0..1)))
</code></pre>
<p>or</p>
<pre><code>e=[ x(j) - sum(A[j,i] for i in (0..1)) for j in (0..3)]
</code></pre>
https://ask.sagemath.org/question/55244/combining-for/?comment=55250#post-id-55250yes exactly. Thank you.Tue, 12 Jan 2021 16:04:27 +0100https://ask.sagemath.org/question/55244/combining-for/?comment=55250#post-id-55250