A list of symbolic variables

i like this post (click again to cancel)
6
i dont like this post (click again to cancel)

Hello, I'm new to sage so I hope that I'm asking a very basic question.

I'm trying to create a list of symbolic variables. I would like to be able to set the number of variables initially and then let sage create the list.

I was (sort of) able to achieve this when I realized that the input for var is a string, so I wrote the following, which produces six symbolic variables for me:

n=3;
for i in range(2*n):
      var('s_'+str(i))

In my context, the variables are actually real, and they satisfy a system of equations that I would also like sage to produce. By playing with strings, and then using eval on them so they became expressions, I was able to produce a few of the simpler equations, which sage can solve.

But when I run for loops indexed by i I can never seem to actually refer to the variables indexed by i. For example, the following will not make sense to sage:

for i in range(2*n):
        s_i = i

The only way I can think to achieve the above result is to create a string with a for loop that states the command I want, turn it into an expression, save it as an equation, and then include it in a big list of equations. Even so, I can't index the equations by i either, so I can't create the 2*n equations that I would need...

I have to do a lot more with these variables, so I hope someone can tell me what I am doing terribly wrong. The first thing I want to do is create a second list, w, defined as:

$w_k = s_{2n-k}$

asked Feb 05 '11

David Ferrone gravatar image David Ferrone
141 4 6 12

updated Feb 05 '11

i like this answer (click again to cancel)
8
i dont like this answer (click again to cancel) David Ferrone has selected this answer as correct

You're pretty close! The problem as you've noted is that "s_i" merely "s_i"; there's no rule that says that the parts of (would-be) variable names after underscores get interpolated in this way.

Here's how I'd do it, assuming I've understood you correctly:

sage: # first make a list of the variables
sage: n = 3
sage: s = list(var('s_%d' % i) for i in range(2*n))
sage: w = list(var('w_%d' % i) for i in range(2*n))
sage: s
[s_0, s_1, s_2, s_3, s_4, s_5]
sage: w
[w_0, w_1, w_2, w_3, w_4, w_5]
sage: 
sage: # then make a list of equations
sage: eqs = list(w[k] == s[2*n-k-1] for k in range(2*n))
sage: eqs
[w_0 == s_5, w_1 == s_4, w_2 == s_3, w_3 == s_2, w_4 == s_1, w_5 == s_0]

Note that I had to put a -1 in there to get the relations I think you were aiming at. If I've misunderstood it's easy to change.

link

posted Feb 05 '11

DSM gravatar image DSM flag of Canada
4892 12 65 105

updated Feb 05 '11

No that's the correct equation, I changed it for simplicity. Thanks! This is much cleaner. David Ferrone (Feb 05 '11)
Nice! Though this doesn't quite answer how to access one of these variables if one didn't make the list s in the first place, which I struggled with for a while last night before giving up. But this is cleaner than that in any case, for sure. kcrisman (Feb 06 '11)
Incidentally, DSM, you clearly are conversant with a good range of the Sage codebase, in particular much of the same stuff I care about, and it would seem to be a crying shame that you aren't more involved in development. Do you go by another 'handle' on sage-devel or Trac - perhaps Doug S. McNeil? We could definitely use your help in review, enhancements, and fixes! kcrisman (Feb 06 '11)
Yeah, that's me; and I actually started answering questions here in the first place to work up sufficient karma to convince someone to look at a bug report of mine which is driving me crazy. :^) DSM (Feb 08 '11)
Hmm, you shouldn't need karma to get someone to look at bug reports on e.g. sage-support. Or here. Usually if no one answers, it's because no one who knows happens to have time to respond - this has happened to me more than once. kcrisman (Feb 08 '11)

Your answer

Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
Login/Signup to Post

Question tools

1 follower

Tags:

Stats:

Asked: Feb 05 '11

Seen: 1,049 times

Last updated: Feb 05 '11

powered by ASKBOT version 0.7.22
Copyright Sage, 2010. Some rights reserved under creative commons license.