First time here? Check out the FAQ!

Ask Your Question
0

Assigning variables in a list

asked 9 years ago

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!

Preview: (hide)

Comments

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 ( 9 years ago )

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 ( 9 years ago )

2 Answers

Sort by » oldest newest most voted
2

answered 9 years ago

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]
...
Preview: (hide)
link
0

answered 9 years ago

castor gravatar image

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.

Preview: (hide)
link

Comments

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 ( 9 years ago )

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

Stats

Asked: 9 years ago

Seen: 900 times

Last updated: Jun 22 '15