user124123
user124123

Reputation: 1683

sympy sequential substitution python

I'm trying to get sympy to substitute a symbol for two other symbols on the basis of iteration. At the moment I have some code that does an expansion of some brackets and stores each iteration:

for i in range(0,nMoments-1):
  middle.append(K+i)

Producing

[K]
[K, K + 1]
[K, K + 1, K + 2]

What I would like to do is for each row substitute two symbols for K, which are themselves stored in vectors of equal length m1 and m2. So for the top row, for each K I would like to substitute m1[0]/m2[0], then for each K in the second row m1[1]/m2[1], for K in the third row m1[2]/m2[2] e.t.c

So that for middle[0] the equiv indexation of the m1 and m2 vectors are put into K.

For reference, nMoments is just an int variable

From what I can tell, my closest attempt so far is

for i in range(0,nMoments):
  K.replace(K,m1[i]**2/m2[i])
  print middle

However this produces this:

[K, K + 1, K + 2]
[K, K + 1, K + 2]
[K, K + 1, K + 2]

Does anyone know how I can resolve this issue?

Many thanks!

Upvotes: 0

Views: 370

Answers (1)

asmeurer
asmeurer

Reputation: 91490

Is this what you want?

for i in range(nMoments):
    middle[i] = middle[i].subs(K, m1[i]**2/m2[i])

Upvotes: 1

Related Questions