which AccumBounds result is correct

199 views
Skip to first unread message

Chris Smith

unread,
Jul 20, 2019, 6:34:21 AM7/20/19
to sympy
Which result is correct? The second is a refactoring of the first:

    >>> AccumBounds(-1, 1) + AccumBounds(-1, 1)**2
    AccumBounds(-1, 2)
    >>> AccumBounds(-1, 1)*(1 + AccumBounds(-1, 1))
    AccumBounds(-2, 2)

Oscar Benjamin

unread,
Jul 20, 2019, 6:46:13 AM7/20/19
to sympy
I guess this is the dependency problem in interval arithmetic:
https://en.wikipedia.org/wiki/Interval_arithmetic#Dependency_problem
(Note that x**2+x is the example used there)

The issue is that we have to know the identity of the intervals. For
example if x and y are distinct intervals both [-1,1] then x*y is
[-1,1]. However x**2 has to be [0, 1].
> --
> 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 on the web visit https://groups.google.com/d/msgid/sympy/e9b71b7d-8239-4851-8358-23b1249dbd7a%40googlegroups.com.

Chris Smith

unread,
Jul 20, 2019, 8:01:09 AM7/20/19
to sympy
Thanks. subs should probably expand fully any expression for which a symbol is being replaced with an AccumBounds as in `expr.subs(x, AccumBound(a,b))` so something like `x*(1 + x)` would be handled as `x + x**2`.


On Saturday, July 20, 2019 at 5:46:13 AM UTC-5, Oscar wrote:
I guess this is the dependency problem in interval arithmetic:
https://en.wikipedia.org/wiki/Interval_arithmetic#Dependency_problem
(Note that x**2+x is the example used there)

The issue is that we have to know the identity of the intervals. For
example if x and y are distinct intervals both [-1,1] then x*y is
[-1,1]. However x**2 has to be [0, 1].

On Sat, 20 Jul 2019 at 11:34, Chris Smith <smi...@gmail.com> wrote:
>
> Which result is correct? The second is a refactoring of the first:
>
>     >>> AccumBounds(-1, 1) + AccumBounds(-1, 1)**2
>     AccumBounds(-1, 2)
>     >>> AccumBounds(-1, 1)*(1 + AccumBounds(-1, 1))
>     AccumBounds(-2, 2)
>
> --
> 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 sy...@googlegroups.com.

David Bailey

unread,
Jul 21, 2019, 6:36:07 AM7/21/19
to sy...@googlegroups.com

Dear group,

I think one problem with sympy, is that its limitations aren't really defined, and you only know you have exceeded them if you get an error that is internal to the sympy code, for example:

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sympy
>>> from sympy import *
>>> sin(Interval(0,1))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\pythonsystem\lib\site-packages\sympy\core\cache.py", line 94, in wrapper
    retval = cfunc(*args, **kwargs)
  File "C:\pythonsystem\lib\site-packages\sympy\core\function.py", line 458, in __new__
    result = super(Function, cls).__new__(cls, *args, **options)
  File "C:\pythonsystem\lib\site-packages\sympy\core\cache.py", line 94, in wrapper
    retval = cfunc(*args, **kwargs)
  File "C:\pythonsystem\lib\site-packages\sympy\core\function.py", line 277, in __new__
    evaluated = cls.eval(*args)
  File "C:\pythonsystem\lib\site-packages\sympy\functions\elementary\trigonometric.py", line 294, in eval
    if arg.could_extract_minus_sign():
AttributeError: 'Interval' object has no attribute 'could_extract_minus_sign'
>>>

I was trying to explore what Interval() could really manage, so if this had worked I was going to devise an interval that would straddle pi/2 to determine whether the sympy code recognised that situation and responded correctly. As it is, I am 90% sure that the above meant that you can't feed an interval to a transcendental function, but that leaves a 10% probability that I misunderstood something!

Subsequently I noticed that Interval maybe doesn't really evaluate much at all, for example:

Interval(0,2)**2

Returns a product of two Interval's.

Furthermore:

 Interval(0,2)-Interval(0,2)
EmptySet()

However, surely this expression contains two independent intervals, so the result is wrong!

Ideally an error message would be really informative, and maybe even give an internet reference to where this is explained. Has anyone given any thought to achieving this in a future version?

David


Chris Smith

unread,
Jul 21, 2019, 7:34:23 AM7/21/19
to sympy
Are you, perhaps, confusing Interval with AccumBounds? Interval is a Set notation whereas AccumBounds is more like the interval:

    >>> AccumBounds(0,2)**2
    AccumBounds(0, 4)
    >>> AccumBounds(0, 2) - AccumBound(0, 2)
    AccumBounds(-2, 2)

Aaron Meurer

unread,
Jul 22, 2019, 5:48:03 PM7/22/19
to sympy
Operations on set objects operate on the sets themselves, not the
elements of the sets. So - does set complement, not arithmetic
subtraction.

To get what you want, use sympy.sets.setexpr.SetExpr. Note that
support for it is still somewhat limited.

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 view this discussion on the web visit https://groups.google.com/d/msgid/sympy/a391993f-7b6f-3551-bced-0139facb5dbc%40dbailey.co.uk.

David Bailey

unread,
Jul 22, 2019, 6:13:15 PM7/22/19
to sy...@googlegroups.com
On 22/07/2019 22:47, Aaron Meurer wrote:
Operations on set objects operate on the sets themselves, not the
elements of the sets. So - does set complement, not arithmetic
subtraction.

To get what you want, use sympy.sets.setexpr.SetExpr. Note that
support for it is still somewhat limited.

Aaron Meurer

Aaron,

Please note that Chris pointed out that I should have been using AccumBounds - I had jumped to the conclusion that 'Interval' implemented interval arithmetic - not something set-theoretic.

I am glad you are keen to see the diagnostics cleaned up, because my feeling is that there may be large umbers of potential casual users who are put off if things don't work easily - and I do like sympy.

I hope a runtime check will not reduce runtime performance appreciably. I wonder if trapping the problem with an exception handler, and somehow figuring out what had happened might be possible - however much code had to execute to do this would not really matter.

David


Aaron Meurer

unread,
Jul 22, 2019, 6:28:14 PM7/22/19
to sympy
AccumBounds and SetExpr aren't exactly the same thing. Which one to
use depends on what you are trying to do. AccumBounds is roughly
interval arithmetic. It represents the bounds of accumulation points
of an infinite limit (for instance, limit(sin(x), x, oo) gives
AccumBounds(-1, 1)). The bounds are not guaranteed to be tight, and
there is no way to represent a disjoint union.

SetExpr is basically a way of manipulating set comprehensions {x: x in
S}. Any kind of set is allowed. The actual containment of elements in
a set expression should hold after operations (i.e., the bounds will
be tight). In practice, this means SetExpr will return unevaluated set
comprehensions more often.

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 view this discussion on the web visit https://groups.google.com/d/msgid/sympy/bac6db53-520f-3fc4-7aed-91cf3139fb45%40dbailey.co.uk.
Reply all
Reply to author
Forward
0 new messages