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

Robert Kern robert.kern at gmail.com
Thu Oct 1 03:06:24 CEST 2009

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

You will probably want to ask on the sympy mailing list:

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

I suspect that the latter are two possible solutions given as an (x,y) pair.
Possibly, they should be given as a list of dicts. Ask on the sympy mailing list.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma