Ask Your Question
1

Combining 'for'

asked 4 years ago

phcosta gravatar image

updated 4 years ago

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?

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 4 years ago

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)]
Preview: (hide)
link

Comments

yes exactly. Thank you.

phcosta gravatar imagephcosta ( 4 years ago )

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: 4 years ago

Seen: 723 times

Last updated: Jan 12 '21