ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 22 Jun 2015 15:41:13 -0500Assigning variables in a listhttp://ask.sagemath.org/question/27056/assigning-variables-in-a-list/I have a variables list which I use to construct a system of linear equations then I use sage to solve this system. Before I use the solve command I like to equate some of the variables in the list. For example if V is the following list `var('x,y,z,w,a,b,c,d')
V2=[x,y,z,w,a,b,c,d]`
I would like to assign`V[i]=V[7-i] for i in [0..3]`. When I do this I get invalid syntax error. I know that I could easily set x=d, y=c and so on but this is not feasible when the list has too many items in it. Thank you for your help!Mon, 08 Jun 2015 14:52:51 -0500http://ask.sagemath.org/question/27056/assigning-variables-in-a-list/Comment by cihan for <p>I have a variables list which I use to construct a system of linear equations then I use sage to solve this system. Before I use the solve command I like to equate some of the variables in the list. For example if V is the following list <code>var('x,y,z,w,a,b,c,d')
V2=[x,y,z,w,a,b,c,d]</code></p>
<p>I would like to assign<code>V[i]=V[7-i] for i in [0..3]</code>. When I do this I get invalid syntax error. I know that I could easily set x=d, y=c and so on but this is not feasible when the list has too many items in it. Thank you for your help!</p>
http://ask.sagemath.org/question/27056/assigning-variables-in-a-list/?comment=27078#post-id-27078That is probably true since I am very new to Python and to programming in general. The code I used is a bit lengthy so I will try to explain in words what I need. I basically want Sage to assume V[i]=V[7-i] for i in range(j) when solving the system. I tried to use the assume command but wasnt successful.Tue, 09 Jun 2015 13:45:03 -0500http://ask.sagemath.org/question/27056/assigning-variables-in-a-list/?comment=27078#post-id-27078Comment by tmonteil for <p>I have a variables list which I use to construct a system of linear equations then I use sage to solve this system. Before I use the solve command I like to equate some of the variables in the list. For example if V is the following list <code>var('x,y,z,w,a,b,c,d')
V2=[x,y,z,w,a,b,c,d]</code></p>
<p>I would like to assign<code>V[i]=V[7-i] for i in [0..3]</code>. When I do this I get invalid syntax error. I know that I could easily set x=d, y=c and so on but this is not feasible when the list has too many items in it. Thank you for your help!</p>
http://ask.sagemath.org/question/27056/assigning-variables-in-a-list/?comment=27057#post-id-27057It seems you are confusing Python variables and symbolic variables, but i am not sure. In order to understand your question better (and provide an adequate solution), could you please provide an example of system of equations you would like to solve, and how you built it in Sage ?Mon, 08 Jun 2015 16:16:13 -0500http://ask.sagemath.org/question/27056/assigning-variables-in-a-list/?comment=27057#post-id-27057Answer by nbruin for <p>I have a variables list which I use to construct a system of linear equations then I use sage to solve this system. Before I use the solve command I like to equate some of the variables in the list. For example if V is the following list <code>var('x,y,z,w,a,b,c,d')
V2=[x,y,z,w,a,b,c,d]</code></p>
<p>I would like to assign<code>V[i]=V[7-i] for i in [0..3]</code>. When I do this I get invalid syntax error. I know that I could easily set x=d, y=c and so on but this is not feasible when the list has too many items in it. Thank you for your help!</p>
http://ask.sagemath.org/question/27056/assigning-variables-in-a-list/?answer=27185#post-id-27185It sounds like you have an expression `s` in `x,y,z,w,a,b,c,d` from which you want to obtain a different expression, let's say `t` in which you substitute `x=d,y=c,z=b,w=a`. You can make multiple substitutions at once using `s.subs(...)` but you need to pass it a python dictionary:
sage: s=x^2+y^2+z^2+w^2+a+b+c+d
sage: s.subs({V2[i]:V2[7-i] for i in [0..3]})
a^2 + b^2 + c^2 + d^2 + a + b + c + d
If your system of linear equations is represented by a python list L then you can make your substitutions using
sage: D = {V2[i]:V2[7-i] for i in [0..3]}
sage: [s.subs(D) for s in L]
...Mon, 22 Jun 2015 15:41:13 -0500http://ask.sagemath.org/question/27056/assigning-variables-in-a-list/?answer=27185#post-id-27185Answer by castor for <p>I have a variables list which I use to construct a system of linear equations then I use sage to solve this system. Before I use the solve command I like to equate some of the variables in the list. For example if V is the following list <code>var('x,y,z,w,a,b,c,d')
V2=[x,y,z,w,a,b,c,d]</code></p>
<p>I would like to assign<code>V[i]=V[7-i] for i in [0..3]</code>. When I do this I get invalid syntax error. I know that I could easily set x=d, y=c and so on but this is not feasible when the list has too many items in it. Thank you for your help!</p>
http://ask.sagemath.org/question/27056/assigning-variables-in-a-list/?answer=27058#post-id-27058As far as I see you look for something like:
<pre><code>
var('x,y,z,w,a,b,c,d')
V2=[x,y,z,w,a,b,c,d]
s=[V2[i]==V2[7-i] for i in [0..3]]
solve(s,x,y,z,w,a,b,c,d)
</code></pre>
Here one obtains that
<pre><code>
[[x == r1, y == r2, z == r3, w == r4, a ==r4, b == r3, c == r2, d == r1]]
</code></pre>,
where r1,r2,r3,r4 are parameters.Mon, 08 Jun 2015 16:24:59 -0500http://ask.sagemath.org/question/27056/assigning-variables-in-a-list/?answer=27058#post-id-27058Comment by cihan for <p>As far as I see you look for something like:</p>
<pre><code>
var('x,y,z,w,a,b,c,d')
V2=[x,y,z,w,a,b,c,d]
s=[V2[i]==V2[7-i] for i in [0..3]]
solve(s,x,y,z,w,a,b,c,d)
</code></pre>
<p>Here one obtains that
</p><pre><code>
[[x == r1, y == r2, z == r3, w == r4, a ==r4, b == r3, c == r2, d == r1]]
</code></pre>,
where r1,r2,r3,r4 are parameters.<p></p>
http://ask.sagemath.org/question/27056/assigning-variables-in-a-list/?comment=27077#post-id-27077Thanks, but this is not what I was looking for. I would like sage to assume x=d, y=c and so on when solving the system.Tue, 09 Jun 2015 13:39:57 -0500http://ask.sagemath.org/question/27056/assigning-variables-in-a-list/?comment=27077#post-id-27077