Problem with assumptions

38 views
Skip to first unread message

Juan Luis Cano Rodríguez

unread,
Mar 25, 2012, 4:42:33 AM3/25/12
to sy...@googlegroups.com
Hi everyone. I'm a fairly new SymPy user and I encountered some problems with how the assumptions work and how they are documented.

I read in some issues and threads about the "new assumptions system" in SymPy, but I'm still not sure which is the old one and which is the new one. On the one hand, I see this Assumptions module [1], which seems to work with the refine function [2] and so on, but not with the is_* functions:

  >>> p = Symbol('p')
  >>> global_assumptions.add(Q.positive(p))
  >>> p.is_positive is None
  True
  >>> global_assumptions
  AssumptionsContext(Q.positive(p))
  >>> q = sqrt(p ** 2)
  >>> print q
  (p**2)**(1/2)
  >>> refine(q)
  p

On the other hand, I can set some assumptions on the variable when using the constructor and then use is_* functions, but

a) I cannot find is_positive, is_integer, ... documented anywhere, nor a list of all possible assumptions, and
b) Setting assumptions after creating the variable seems not to work:

  >>> pp = Symbol('pp')
  >>> qq = sqrt(pp ** 2)
  >>> print qq
  (pp**2)**(1/2)
  >>> pp.is_positive = True
  [...]
  AttributeError: can't set attribute
  >>> pp = Symbol('pp', positive=True)
  >>> qq = sqrt(pp ** 2)
  >>> print qq
  pp

So my questions are, what is the state of the assumtions system? Which one is recommended? Why seems the documentation incomplete at this point? Will you mark one of these as deprecated in the near future?

Thank you in advance,

Juan Luis Cano



Aaron Meurer

unread,
Mar 25, 2012, 5:58:01 PM3/25/12
to sy...@googlegroups.com
On Sun, Mar 25, 2012 at 2:42 AM, Juan Luis Cano Rodríguez
<juan...@gmail.com> wrote:
> Hi everyone. I'm a fairly new SymPy user and I encountered some problems
> with how the assumptions work and how they are documented.
>
> I read in some issues and threads about the "new assumptions system" in
> SymPy, but I'm still not sure which is the old one and which is the new one.
> On the one hand, I see this Assumptions module [1], which seems to work with
> the refine function [2] and so on, but not with the is_* functions:
>
>   >>> p = Symbol('p')
>   >>> global_assumptions.add(Q.positive(p))
>   >>> p.is_positive is None
>   True
>   >>> global_assumptions
>   AssumptionsContext(Q.positive(p))
>   >>> q = sqrt(p ** 2)
>   >>> print q
>   (p**2)**(1/2)
>   >>> refine(q)
>   p

These are the new assumptions.

>
> On the other hand, I can set some assumptions on the variable when using the
> constructor and then use is_* functions, but

Those are the old assumptions.

>
> a) I cannot find is_positive, is_integer, ... documented anywhere, nor a
> list of all possible assumptions, and

Yeah, see issue http://code.google.com/p/sympy/issues/detail?id=2196.

> b) Setting assumptions after creating the variable seems not to work:

No, you can't do this, because the is_stuff is set using magic
getattr() stuff that is hooked into logical deductions (so e.g., you
can set positive=True and is_real will be True).

>
>   >>> pp = Symbol('pp')
>   >>> qq = sqrt(pp ** 2)
>   >>> print qq
>   (pp**2)**(1/2)
>   >>> pp.is_positive = True
>   [...]
>   AttributeError: can't set attribute
>   >>> pp = Symbol('pp', positive=True)
>   >>> qq = sqrt(pp ** 2)
>   >>> print qq
>   pp
>
> So my questions are, what is the state of the assumtions system? Which one
> is recommended? Why seems the documentation incomplete at this point? Will
> you mark one of these as deprecated in the near future?

Currently, the code uses the old assumptions. So you will probably
find yourself needing to use that, just because that is what currently
works.

We hope to merge the new assumptions in such a way that it will use
the same interface as the old assumptions. In other words, at some
point, Symbol('x', positive=True) and x.is_positive will be calling
the new assumptions. So you hopefully shouldn't need to worry about
deprecation.

You can play around with using the new assumptions if you want. For
now, they are only non-global, so there is no way to "set" an
assumption on a variable. You just pass around the assumption, like
ask(Q.real(x), Q.positive(x)) (this means, "is x real given that it is
positive?"). You can use global_assumptions, but this does not hook
into anything else yet (except for refine), so it's not really useful.

Aaron Meurer

> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to
> sympy+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.

Juan Luis Cano Rodríguez

unread,
Mar 27, 2012, 2:54:39 AM3/27/12
to sy...@googlegroups.com
All right, thank you very much for this in-depth information.

Regards,

Juan Luis Cano
Reply all
Reply to author
Forward
0 new messages