Help with Sympy Sets

89 views
Skip to first unread message

Flexico Crux

unread,
Aug 29, 2016, 11:27:35 AM8/29/16
to sympy
I'm looking for a way to solve an equation within an interval and return a list of the values. Solve doesn't guarantee to catch every solution, but Solveset seems to get me partway there:
>>> solveset(sin(x) - sqrt(2)/2, x)
⎧        3⋅π        ⎫   ⎧        π        ⎫
⎨2⋅n⋅π + ─── | n ∊ ℤ⎬ ∪ ⎨2⋅n⋅π + ─ | n ∊ ℤ⎬
⎩         4         ⎭   ⎩        4        ⎭
(that may not look right in a non-monospace font)
That's great and all, but how do I turn that into a list of values, say, between -2*pi and pi?

Aaron Meurer

unread,
Aug 29, 2016, 2:10:28 PM8/29/16
to sympy
You can iterate through the solution sets, using a for loop or iter(solution). If you want to limit it to -2*pi to pi first intersect with Interval(-2*pi, pi). 

In [7]: solveset(sin(x) - sqrt(2)/2, x).intersect(Interval(-2*pi, pi))
Out[7]:
⎧-7⋅π   -5⋅π   π  3⋅π⎫
⎨─────, ─────, ─, ───⎬
⎩  4      4    4   4 ⎭

That gives you a FiniteSet, which you can convert to a list with list().

Aaron Meurer

Shekhar Prasad Rajak

unread,
Aug 29, 2016, 3:13:35 PM8/29/16
to sympy

Hello,

You can use its 3rd parameter domain=Interval(start, end) : 

```
In [4]:  solveset(sin(x) - sqrt(2)/2, x, Interval(-2*pi, 2*pi))
Out[4]:
⎧-7⋅π   -5⋅π   π  3⋅π⎫
⎨─────, ─────, ─, ───⎬
 4      4    4   4


```

--
Shekhar
Message has been deleted

Flexico Crux

unread,
Aug 30, 2016, 10:39:01 PM8/30/16
to sympy
Those don't work for me! I'm using Sympy 1.0 with Python 2.7.12.
[code]
>>> solveset(sin(x)-sqrt(2)/2, x, Interval(-2*pi, 2*pi))
⎧        3⋅π        ⎫   ⎧        π        ⎫
⎨2⋅n⋅π + ─── | n ∊ ℤ⎬ ∪ ⎨2⋅n⋅π + ─ | n ∊ ℤ⎬
⎩         4         ⎭   ⎩        4        ⎭

>>> solveset(sin(x)-sqrt(2)/2, x).intersect(Interval(-2*pi, 2*pi))
⎛              ⎧        3⋅π        ⎫⎞   ⎛              ⎧        π   
⎜[-2⋅π, 2⋅π] ∩ ⎨2⋅n⋅π + ─── | n ∊ ℤ⎬⎟ ∪ ⎜[-2⋅π, 2⋅π] ∩ ⎨2⋅n⋅π + ─ | 
⎝              ⎩         4         ⎭⎠   ⎝              ⎩        4   

     ⎫⎞
n ∊ ℤ⎬⎟
     ⎭⎠
[/code]

Shekhar Prasad Rajak

unread,
Aug 31, 2016, 1:03:32 PM8/31/16
to sympy
You can install latest repo version from this link: https://github.com/sympy/sympy/archive/master.zip

--
Shekhar 

Flexico Crux

unread,
Aug 31, 2016, 2:30:21 PM8/31/16
to sympy
Ah, that did the trick! Thank you.
Reply all
Reply to author
Forward
0 new messages