Ask Your Question

A list of symbolic variables

asked 2011-02-05 19:19:13 +0200

David Ferrone gravatar image

updated 2011-02-05 19:31:58 +0200

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:

for i in range(2*n):

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 merge delete

1 Answer

Sort by » oldest newest most voted

answered 2011-02-05 21:30:42 +0200

DSM gravatar image

updated 2011-02-05 21:31:36 +0200

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: # 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 link more


No that's the correct equation, I changed it for simplicity. Thanks! This is much cleaner.

David Ferrone gravatar imageDavid Ferrone ( 2011-02-05 23:34:46 +0200 )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 gravatar imagekcrisman ( 2011-02-06 21:08:12 +0200 )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 gravatar imagekcrisman ( 2011-02-06 21:13:14 +0200 )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 gravatar imageDSM ( 2011-02-08 19:53:10 +0200 )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 gravatar imagekcrisman ( 2011-02-08 23:23:45 +0200 )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



Asked: 2011-02-05 19:19:13 +0200

Seen: 9,063 times

Last updated: Feb 05 '11