Represent "belongs to"

44 views
Skip to first unread message

Paul Royik

unread,
Nov 4, 2024, 5:45:17 AM11/4/24
to sympy
Is there any way to represent in Sympy the following: `x is Real`, `x is not odd`.

In other words, is there a possibility to represent the fact that expression belongs or doesn't belong to some set?

Oscar Benjamin

unread,
Nov 4, 2024, 6:53:54 AM11/4/24
to sy...@googlegroups.com
SymPy can represent various sets and you can use Contains e.g.:

In [44]: Contains(x, Reals)
Out[44]: x ∈ ℝ

In [45]: Not(Contains(x, Reals))
Out[45]: ¬x ∈ ℝ

In [46]: Contains(x, ImageSet(Lambda(n, 2*n), Integers))
Out[46]: x ∈ {2⋅n │ n ∊ ℤ}

Usually it is better though to express the same statements using
ordinary Booleans rather than Sets e.g.:

In [47]: Eq(x % 2, 0)
Out[47]: x mod 2 = 0

In [51]: Ne(x % 2, 1)
Out[51]: x mod 2 ≠ 1

--
Oscar

On Mon, 4 Nov 2024 at 10:45, Paul Royik <distan...@gmail.com> wrote:
>
> Is there any way to represent in Sympy the following: `x is Real`, `x is not odd`.
>
> In other words, is there a possibility to represent the fact that expression belongs or doesn't belong to some set?
>
> --
> 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 view this discussion visit https://groups.google.com/d/msgid/sympy/6803ee83-25e7-4733-a33e-36fd90fa641fn%40googlegroups.com.

Paul Royik

unread,
Nov 4, 2024, 7:45:07 AM11/4/24
to sympy
Thank you!

That's exactly what I was looking for.

Aaron Meurer

unread,
Nov 7, 2024, 4:24:07 PM11/7/24
to sy...@googlegroups.com
It is also worth noting that in many cases, you might just want to use
the assumptions system, like x.is_real (this will evaluate to None if
it is unknown).

Aaron Meurer
> To view this discussion visit https://groups.google.com/d/msgid/sympy/CAHVvXxRw-MWT53vVxjFoc6m3FVSqMN369DrDeZdnuP-qU5gocA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages