# sympy returns a dictionary sometimes, and sometimes a list of tuples...why?

Brian Blais bblais at bryant.edu
Thu Oct 1 00:47:23 CEST 2009

```Hello,

I wrote a very simple script using sympy, and things were working
fine, except for one problem.  So I have:

from sympy import *

x, y = symbols('x','y',real=True)
alpha,beta,gamma=symbols('alpha','beta','gamma',real=True)
alpha_p,beta_p,gamma_p=symbols('alpha_p','beta_p','gamma_p',real=True)

L = symbols('L',real=True)

and then I look at solutions to some equations, like:

solution=solve([beta*y - alpha*(1+y/L) ,
-beta_p*x + alpha_p ], [x, y])

print solution

which prints (correctly):

{x: alpha_p/beta_p, y: L*alpha/(-alpha + L*beta)}

now, if I do:

solution=solve([beta*y - alpha*(1+y/L) - gamma*x*(1+y/L),
-beta_p*x + alpha_p - gamma_p*y], [x, y])

print solution

I get a very long solution, which isn't a problem, but it isn't a
dictionary: I can't tell what is the solution for x and for y.  What
I get is:

[(-(alpha*beta_p*abs(gamma)*abs(gamma_p) + alpha_p*gamma*abs(gamma)
*abs(gamma_p) - L*beta*beta_p*abs(gamma)*abs(gamma_p) -
L*gamma*gamma_p*abs(gamma)*abs(gamma_p) - gamma*gamma_p*
(2*alpha*alpha_p*beta_p*gamma - 2*L*alpha_p*beta*beta_p*gamma +
2*L*alpha*beta_p*gamma*gamma_p + alpha**2*beta_p**2 +
alpha_p**2*gamma**2 + L**2*beta**2*beta_p**2 +
L**2*gamma**2*gamma_p**2 - 2*L*alpha*beta*beta_p**2 +
2*L*alpha_p*gamma_p*gamma**2 + 2*beta*beta_p*gamma*gamma_p*L**2)**
(1/2))/(2*beta_p*gamma*abs(gamma)*abs(gamma_p)) + alpha_p/beta_p,
(alpha*beta_p*abs(gamma)*abs(gamma_p) + alpha_p*gamma*abs(gamma)*abs
(gamma_p) - L*beta*beta_p*abs(gamma)*abs(gamma_p) -
L*gamma*gamma_p*abs(gamma)*abs(gamma_p) - gamma*gamma_p*
(2*alpha*alpha_p*beta_p*gamma - 2*L*alpha_p*beta*beta_p*gamma +
2*L*alpha*beta_p*gamma*gamma_p + alpha**2*beta_p**2 +
alpha_p**2*gamma**2 + L**2*beta**2*beta_p**2 +
L**2*gamma**2*gamma_p**2 - 2*L*alpha*beta*beta_p**2 +
2*L*alpha_p*gamma_p*gamma**2 + 2*beta*beta_p*gamma*gamma_p*L**2)**
(1/2))/(2*gamma*gamma_p*abs(gamma)*abs(gamma_p))), (-(alpha*beta_p*abs
(gamma)*abs(gamma_p) + alpha_p*gamma*abs(gamma)*abs(gamma_p) -
L*beta*beta_p*abs(gamma)*abs(gamma_p) - L*gamma*gamma_p*abs(gamma)*abs
(gamma_p) + gamma*gamma_p*(2*alpha*alpha_p*beta_p*gamma -
2*L*alpha_p*beta*beta_p*gamma + 2*L*alpha*beta_p*gamma*gamma_p +
alpha**2*beta_p**2 + alpha_p**2*gamma**2 + L**2*beta**2*beta_p**2 +
L**2*gamma**2*gamma_p**2 - 2*L*alpha*beta*beta_p**2 +
2*L*alpha_p*gamma_p*gamma**2 + 2*beta*beta_p*gamma*gamma_p*L**2)**
(1/2))/(2*beta_p*gamma*abs(gamma)*abs(gamma_p)) + alpha_p/beta_p,
(alpha*beta_p*abs(gamma)*abs(gamma_p) + alpha_p*gamma*abs(gamma)*abs
(gamma_p) - L*beta*beta_p*abs(gamma)*abs(gamma_p) -
L*gamma*gamma_p*abs(gamma)*abs(gamma_p) + gamma*gamma_p*
(2*alpha*alpha_p*beta_p*gamma - 2*L*alpha_p*beta*beta_p*gamma +
2*L*alpha*beta_p*gamma*gamma_p + alpha**2*beta_p**2 +
alpha_p**2*gamma**2 + L**2*beta**2*beta_p**2 +
L**2*gamma**2*gamma_p**2 - 2*L*alpha*beta*beta_p**2 +
2*L*alpha_p*gamma_p*gamma**2 + 2*beta*beta_p*gamma*gamma_p*L**2)**
(1/2))/(2*gamma*gamma_p*abs(gamma)*abs(gamma_p)))]

which, if you ignore the complexity, is a list of two items, each
item is a tuple of length 2.  What does this mean?  I can handle the
mess of the solution, but I'd like to know what is a solution for x,
and for y.  Why isn't it in a dictionary?  Am I doing something wrong?

thanks,

Brian Blais

--
Brian Blais
bblais at bryant.edu
http://web.bryant.edu/~bblais

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090930/970cde8f/attachment.html>
```