Ask Your Question
6

A list of symbolic variables

asked 2011-02-05 12:19:13 -0500

David Ferrone gravatar image

updated 2011-02-05 12:31:58 -0500

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}$

edit retag flag offensive close delete

1 answer

Sort by » oldest newest most voted
8

answered 2011-02-05 14:30:42 -0500

DSM gravatar image

updated 2011-02-05 14:31:36 -0500

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.

edit flag offensive delete publish link more

Comments

No that's the correct equation, I changed it for simplicity. Thanks! This is much cleaner. David Ferrone ( 2011-02-05 16:34:46 -0500 )edit
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 ( 2011-02-06 14:08:12 -0500 )edit
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 ( 2011-02-06 14:13:14 -0500 )edit
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 ( 2011-02-08 12:53:10 -0500 )edit
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 ( 2011-02-08 16:23:45 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Question Tools

Follow
1 follower

Stats

Asked: 2011-02-05 12:19:13 -0500

Seen: 1,279 times

Last updated: Feb 05 '11