ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 07 Jun 2019 02:33:53 +0200A list of symbolic variableshttps://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/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;<br>
for i in range(2*n):<br>
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):<br>
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}$
Sat, 05 Feb 2011 19:19:13 +0100https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/Answer by DSM for <p>Hello,
I'm new to sage so I hope that I'm asking a very basic question.</p>
<p>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.</p>
<p>I was (sort of) able to achieve this when I realized that the input for <code>var</code> is a string, so I wrote the following, which produces six symbolic variables for me:</p>
<blockquote>
<p>n=3;<br/>
for i in range(2*n):<br/>
var('s_'+str(i))</p>
</blockquote>
<p>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 <code>eval</code> on them so they became expressions, I was able to produce a few of the simpler equations, which sage can <code>solve</code>.</p>
<p>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:</p>
<blockquote>
<p>for i in range(2*n):<br/>
s_i = i</p>
</blockquote>
<p>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...</p>
<p>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:</p>
<p>$w_k = s_{2n-k}$</p>
https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/?answer=12065#post-id-12065You'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.Sat, 05 Feb 2011 21:30:42 +0100https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/?answer=12065#post-id-12065Comment by David Ferrone for <p>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.</p>
<p>Here's how I'd do it, assuming I've understood you correctly:</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/?comment=22193#post-id-22193No that's the correct equation, I changed it for simplicity. Thanks! This is much cleaner.Sat, 05 Feb 2011 23:34:46 +0100https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/?comment=22193#post-id-22193Comment by sum8tion for <p>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.</p>
<p>Here's how I'd do it, assuming I've understood you correctly:</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/?comment=46843#post-id-46843Any idea on how I could do this so that the characters have 2 indices? That is s_00, s_01, ... s_33?Fri, 07 Jun 2019 02:33:53 +0200https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/?comment=46843#post-id-46843Comment by DSM for <p>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.</p>
<p>Here's how I'd do it, assuming I've understood you correctly:</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/?comment=22173#post-id-22173Yeah, 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. :^)Tue, 08 Feb 2011 19:53:10 +0100https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/?comment=22173#post-id-22173Comment by kcrisman for <p>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.</p>
<p>Here's how I'd do it, assuming I've understood you correctly:</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/?comment=22169#post-id-22169Hmm, 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.Tue, 08 Feb 2011 23:23:45 +0100https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/?comment=22169#post-id-22169Comment by kcrisman for <p>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.</p>
<p>Here's how I'd do it, assuming I've understood you correctly:</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/?comment=22186#post-id-22186Nice! 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.Sun, 06 Feb 2011 21:08:12 +0100https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/?comment=22186#post-id-22186Comment by kcrisman for <p>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.</p>
<p>Here's how I'd do it, assuming I've understood you correctly:</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/?comment=22185#post-id-22185Incidentally, 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!Sun, 06 Feb 2011 21:13:14 +0100https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/?comment=22185#post-id-22185