Ask Your Question

Symbolic variables in loops

asked 2014-08-14 06:43:42 -0500

Torero gravatar image

Hi, I got a list of symbolic variables defined like this: P = list(var('P_%d' % i) for i in range(2*n + 1))

But when I try to manipulate these Variables in a for-loop like: for k in range(2,n + 2,2): print(P_k)

I'll get an error "NameError: name 'P_k' is not defined" I'm guessing there is no k in P_k because its one entity 'P_k' :-D But I hope it's possible to fix this by using "%" like in the definition above. Anyone knows how to?


edit retag flag offensive close merge delete


Also, probably n = something earlier in your code...

kcrisman gravatar imagekcrisman ( 2014-08-14 07:48:34 -0500 )edit

yeah. n = anything natural >= 2

Torero gravatar imageTorero ( 2014-08-18 09:41:53 -0500 )edit

3 answers

Sort by ยป oldest newest most voted

answered 2014-08-14 07:02:33 -0500

niles gravatar image

Does P[k] work?

edit flag offensive delete link more

answered 2014-08-14 08:24:11 -0500

Torero gravatar image

@niles: P[k] works, but I need P_k in my loop. In fact I'm trying to substitute P_k by P[k]

@kcrisman: print(eval...) works fine, but N[k] = N[k].subs(eval('P_%s' %k = P[k])) doesn't (SyntaxError: keyword can't be an expression). But this is what I was aiming at.

One solution would be to redefine N[k] everytime any P_k changes. But that would kill my calculation period for bigger n, so that is my last resort

edit flag offensive delete link more


Couldn't you just make, inside your loop, `temp = eval('P_%s' %k)` and use that in your loop? I'll edit my answer.

kcrisman gravatar imagekcrisman ( 2014-08-14 11:37:44 -0500 )edit

I don't understand. Why not just include the line "P_k = P[k]"?

niles gravatar imageniles ( 2014-08-14 11:58:17 -0500 )edit

1. P_k = P[k] doesn't work in loops with index k (because even if k = 2, P_k =! P_2) 2. When I got an equation using P_2 it will still have P_2 in it afterwards

Torero gravatar imageTorero ( 2014-08-18 09:16:41 -0500 )edit

answered 2014-08-14 07:52:08 -0500

kcrisman gravatar image

updated 2014-08-14 11:38:48 -0500

for k in range(2,n + 2,2):

Ugly, I admit. It's hard to just stick in these things. Niles' solution is better.

Edit: But if you insist (I note you also used print in your original example, so it was hard to anticipate your next issue)...

sage: n = 5
sage: P = list(var('P_%d' % i) for i in range(2*n + 1))
sage: for k in range(2,n + 2,2):
....:     tem = eval('P_%s' %k)
....:     x.subs(x=tem)
edit flag offensive delete link more


I'm sorry. I hoped there is a direct way to get the variables and used print as a basic example. But this one is interesting. Unfortunately it substitutes x by P_k and not the other way around

Torero gravatar imageTorero ( 2014-08-18 09:13:25 -0500 )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

1 follower


Asked: 2014-08-14 06:43:42 -0500

Seen: 365 times

Last updated: Aug 14 '14