# 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 assignV[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!

edit retag close merge delete

It 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 ?

( 2015-06-08 16:16:13 -0500 )edit

That 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.

( 2015-06-09 13:45:03 -0500 )edit

Sort by ยป oldest newest most voted

It 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]
...

more

As far as I see you look for something like:


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)


Here one obtains that


[[x == r1, y == r2, z == r3, w == r4, a ==r4, b == r3, c == r2, d == r1]]

, where r1,r2,r3,r4 are parameters.

more

Thanks, 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.

( 2015-06-09 13:39:57 -0500 )edit