Ask Your Question

Assigning variables in a list

asked 2015-06-08 21:52:51 +0200

cihan gravatar image

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 flag offensive 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 ?

tmonteil gravatar imagetmonteil ( 2015-06-08 23:16:13 +0200 )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.

cihan gravatar imagecihan ( 2015-06-09 20:45:03 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2015-06-22 22:41:13 +0200

nbruin gravatar image

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]
edit flag offensive delete link more

answered 2015-06-08 23:24:59 +0200

castor gravatar image

As far as I see you look for something like:

s=[V2[i]==V2[7-i] for i in [0..3]]

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.

edit flag offensive delete link 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.

cihan gravatar imagecihan ( 2015-06-09 20:39:57 +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

1 follower


Asked: 2015-06-08 21:52:51 +0200

Seen: 589 times

Last updated: Jun 22 '15