How to evaluate a symbol is odd or even?

54 views
Skip to first unread message

mike

unread,
May 29, 2017, 11:50:04 PM5/29/17
to sympy
Hi,

I am new to learn sympy. This is a very simple question but I do not know how to deal with it.

The question is: both c and d are positive integers, is c*d even if c*(d+1) is odd.

my code:

from sympy import Symbol
from sympy.assumptions import assuming, Q, ask
c = Symbol('c')
d = Symbol('d')
with assuming(Q.positive(c), Q.integer(c), Q.positive(d), Q.integer(d)):
    with assuming(Q.odd(c*(d +1))):
        print(ask(Q.odd(c)))
        print(ask(Q.odd((d+1))))

This code works. c is odd and (d+1) is odd, but how to evaluate d is odd or even.
I run the code
ask(Q.odd(d)),
but the result is "NONE"

Ask for help.
Thanks, 

Aaron Meurer

unread,
May 29, 2017, 11:59:11 PM5/29/17
to sy...@googlegroups.com
None in this case means that SymPy doesn't know how determine the
fact. It looks like the sathandlers has an even/odd fact for Mul but
not for Add https://github.com/sympy/sympy/blob/71eb404921a4596b9fe42a7a4a0ccfa7d63a62c0/sympy/assumptions/sathandlers.py#L339.
If I remember correctly, it's because the corresponding fact for Add
requires counting, and I wasn't sure how to do that efficiently
(without adding an exponential number of clauses for large Adds).

You can always tell SymPy the facts that it needs to know to deduce
things, in this case

>>> with assuming(Q.positive(c), Q.integer(c), Q.positive(d), Q.integer(d), Q.odd(c*(d + 1)), Q.odd(d + 1) >> Q.even(d)):
... print(ask(Q.odd(d)))
False

here >> means "implies" (you could also use Implies(Q.odd(d + 1), Q.even(d))).

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/5206e651-feeb-4cd9-a4c5-ba85f17ec6f0%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

mike

unread,
May 30, 2017, 3:43:48 PM5/30/17
to sympy
Thank you Aaron, your response is very helpful.

Q.odd(d + 1) & Q.even(d) are deduced assumptions. It seems if I use the current method, Sympy can not get the conclusion "d is even" from "if (d+1) is odd" directly.
 

在 2017年5月29日星期一 UTC-6下午9:59:11,Aaron Meurer写道:

Aaron Meurer

unread,
May 30, 2017, 4:22:04 PM5/30/17
to sy...@googlegroups.com
No, the deduction of even/odd on Add isn't implemented yet. But the
implication Implies(Q.odd(d + 1), Q.even(d)) is always true, so you
can add it to the assumed facts unconditionally.

Aaron Meurer
> https://groups.google.com/d/msgid/sympy/2d41dd13-587e-4fca-996f-e3892b9fe792%40googlegroups.com.

mike

unread,
May 31, 2017, 12:55:07 PM5/31/17
to sympy
Thank you for your clarification.

在 2017年5月30日星期二 UTC-6下午2:22:04,Aaron Meurer写道:
Reply all
Reply to author
Forward
0 new messages