taschini / pyinterval

PyInterval — Interval arithmetic in Python
http://pyinterval.readthedocs.io
Other
83 stars 25 forks source link

abs() missing #11

Closed connellybarnes closed 7 years ago

connellybarnes commented 7 years ago

Calling abs() on an interval currently does not result in the interval's absolute value:

>>> abs(interval.interval[-1,1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad operand type for abs(): 'interval'

However, this is easy to implement! This is a function I used:

def interval_abs(i):
    """
    Get absolute value of interval i.
    """
    return (i | (-i)) & interval.interval[0, inf]
taschini commented 7 years ago

With the version currently in master it is now possible to take the absolute value of an interval:

>>> abs(interval.interval[-1,1])
interval([0.0, 1.0])
taschini commented 7 years ago

Release 1.2.0 now adds support for the abs function.