Ask Your Question
1

Combining 'for'

asked 2021-01-11 20:57:19 +0100

phcosta gravatar image

updated 2021-01-12 02:38:07 +0100

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?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-01-12 05:11:55 +0100

Do 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)]
edit flag offensive delete link more

Comments

yes exactly. Thank you.

phcosta gravatar imagephcosta ( 2021-01-12 16:04:27 +0100 )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

Stats

Asked: 2021-01-11 20:57:19 +0100

Seen: 72 times

Last updated: Jan 12