how to get element of solution of solveset

2,047 views
Skip to first unread message

chaowen guo

unread,
May 17, 2016, 10:24:47 PM5/17/16
to sympy
Hi:

In sympy1.0, it is recommended to use solveset to solve equation, but how to get the element of the solution?

For example:

import sympy
x=sympy.symbols('x',real=True)
e,m=sympy.symbols('e,m',positive=True)
sympy.solveset(sympy.sqrt(2*e*m-(x*m)**2),x)

I want to get the first and second solution individually to do further calculation, but I can not directly use [0] and [1], so how to get individual element?

Denis Akhiyarov

unread,
May 18, 2016, 5:26:23 PM5/18/16
to sympy
Solveset is not indexable, so try converting to list. What surprised me is that it is not like regular python set:

ptipython
Python 3.5.1 |Anaconda custom (64-bit)| (default, Dec  7 2015, 11:16:01)
Type "copyright", "credits" or "license" for more information.


IPython 4.2.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python'
s own help system.
object?   -> Details about 'object', use 'object??' for extra details.


In [1]: import sympy


In [2]: sympy.__version__
Out[2]: '1.0'


In [3]: x=sympy.symbols('x',real=True)
   
...: e,m=sympy.symbols('e,m',positive=True)
   
...: sympy.solveset(sympy.sqrt(2*e*m-(x*m)**2),x)
Out[3]: {-sqrt(2)*sqrt(e)/sqrt(m), sqrt(2)*sqrt(e)/sqrt(m)}


In [4]: _
Out[4]: {-sqrt(2)*sqrt(e)/sqrt(m), sqrt(2)*sqrt(e)/sqrt(m)}


In [5]: sols=_


In [6]: type(sols)
Out[6]: sympy.sets.sets.FiniteSet


In [7]: sols.pop()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-188fe3e26bd2> in <module>()
----> 1 sols.pop()


AttributeError: 'FiniteSet' object has no attribute 'pop'


In [8]: list(sols)
Out[8]: [sqrt(2)*sqrt(e)/sqrt(m), -sqrt(2)*sqrt(e)/sqrt(m)]

Aaron Meurer

unread,
May 18, 2016, 7:20:28 PM5/18/16
to sy...@googlegroups.com
SymPy set objects are immutable, like every other SymPy object, so pop
could not work. However, iterating over the set does work. For finite
sets, the easiest way is to convert to a list. For countable infinite
sets, converting to a list won't work obviously, but you can use
iter(s) and call next() on it. Iteration is generally implemented so
that it hits every element (for instance, iterating over S.Integers
gives 0, 1, -1, 2, -2, ...). For uncountable sets like intervals, all
you can't iterate, but you can check containment using "in", or
intersect with other sets.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/733f8e55-e2ad-438e-bd2a-792524801685%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Aaron Meurer

unread,
May 18, 2016, 7:21:34 PM5/18/16
to sy...@googlegroups.com
Also be aware that FiniteSets are unordered so the order of the result
from list() may differ between sessions.

Aaron Meurer

Chris Smith

unread,
May 23, 2016, 12:45:55 AM5/23/16
to sympy
To make it canonical, you could use `list(ordered(finite_set))`.
Reply all
Reply to author
Forward
0 new messages