Interval Arithmetic (IA)¶
This module can create intervals and perform operations.
In order to bound rounding errors when performing floating point arithmetic, we can use Interval Arithmetic (IA) to keep track of rounding errors.
After a series of operations using basic operators like +, -, * and / we end of with an interval instead of an approximation of the result.
The interval width represents the uncertainty of the result but we would know for sure that the correct result will be within this interval.
An interval is presented by two number representing the lower and upper range of the interval:
where \(a \leq b\).
-
class
ia.
Interval
(inf, sup)¶ Bases:
object
Representation of an interval. An instance of the class Interval is composed of two fields:
inf: the infimum
sup: the supremum
-
__init__
(inf, sup)¶ Create an interval. It is composed of two fields : the infimum and the supremum. If \(inf > sup\), then the init function reorganize the two values.
- Parameters
inf (int or float or string) – infimum
sup (int or float or string) – supremum
- Returns
interval
- Return type
Examples
>>> from affapy.ia import Interval >>> x = Interval(1, 2) >>> print(x) [1.0, 2.0]
-
property
inf
¶ Return the inf.
-
property
sup
¶ Return the sup.
-
width
()¶ Width
Return the width of an interval:
\[width([a, b]) = b - a\]- Parameters
self (Interval) – arg
- Returns
sup - inf
- Return type
mpf
Examples
>>> Interval(1, 2).width() mpf('1.0')
-
mid
()¶ Middle
Return the middle of an interval:
\[middle([a, b]) = \frac{a + b}{2}\]- Parameters
self (Interval) – arg
- Returns
(inf + sup) / 2
- Return type
mpf
Examples
>>> Interval(1, 2).mid() mpf('1.5')
-
radius
()¶ Radius
Return the radius of an interval:
\[radius([a, b]) = \frac{width([a, b])}{2}\]- Parameters
self (Interval) – arg
- Returns
width / 2
- Return type
mpf
Examples
>>> Interval(1, 2).radius() mpf('0.5')
-
__neg__
()¶ Operator - (unary)
Return the additive inverse of an interval:
\[-[a, b] = [-b, -a]\]Examples
>>> -Interval(1, 2) Interval(-2.0, -1.0)
-
__add__
(other)¶ Operator +
Add two intervals:
\[[a, b] + [c, d] = [a + c, b + d]\]Or add an interval and an integer or float or mpf:
\[[a, b] + k = [a + k, b + k]\]- Parameters
- Returns
self + other
- Return type
- Raises
affapyError – other must be Interval, int, float, mpf
Examples
>>> Interval(1, 2) + Interval(3, 4) Interval(4.0, 6.0) >>> Interval(1, 2) + 2 Interval(3.0, 4.0)
-
__radd__
(other)¶ Reverse operator +
Add two intervals or an interval and an integer or float or mpf. See the add operator for more details.
- Parameters
- Returns
other + self
- Return type
- Raises
affapyError – other must be Interval, int, float, mpf
Examples
>>> 2 + Interval(1, 2) Interval(3.0, 4.0)
-
__sub__
(other)¶ Operator -
Subtract two intervals:
\[[a, b] - [c, d] = [a - c, b - d]\]Or subtract an interval and an integer or float or mpf:
\[[a, b] - k = [a - k, b - k]\]- Parameters
- Returns
self - other
- Return type
- Raises
affapyError – other must be Interval, int, float, mpf
Examples
>>> Interval(1, 2) - Interval(3, 4) Interval(-3.0, -1.0) >>> Interval(1, 2) - 3 Interval(-2.0, -1.0)
-
__rsub__
(other)¶ Reverse operator -
Subtract two intervals or an interval and an integer or float or mpf. See the sub operator for more details.
- Parameters
- Returns
other - self
- Return type
- Raises
affapyError – other must be Interval, int, float, mpf
Examples
>>> 1 - Interval(2, 3) Interval(-2.0, -1.0)
-
__mul__
(other)¶ Operator *
Multiply two intervals:
\[[a, b] \times [c, d] = [min\{a \times c, a \times d, b \times c, b \times d\}, max\{a \times c, a \times d, b \times c, b \times d\}]\]Or multiply an interval and an integer or float or mpf:
\[[a, b] \times k = [a \times k, b \times k]\]- Parameters
- Returns
self * other
- Return type
- Raises
affapyError – other must be Interval, int, float, mpf
Examples
>>> Interval(1, 2) * Interval(3, 4) Interval(3.0, 8.0) >>> Interval(1, 2) * 3 Interval(3.0, 6.0)
-
__rmul__
(other)¶ Reverse operator *
Multiply two intervals or an interval and an integer or float or mpf. See the mul operator for more details.
- Parameters
- Returns
other * self
- Return type
- Raises
affapyError – other must be Interval, int, float, mpf
-
__truediv__
(other)¶ Operator /
Divide two intervals:
\[[a, b] / [c, d] = [a, b] \times [1/d, 1/c]\]or an interval and an integer or float or mpf:
\[[a, b] / k = \frac{1}{k} \times [a, b]\]It is possible only if other does not contains 0.
- Parameters
- Returns
self / other
- Return type
- Raises
affapyError – division by 0
affapyError – other must be Interval, int, float, mpf
Examples
>>> Interval(1, 2) / Interval(3, 4) Interval(0.25, 0.666666666666667) >>> Interval(1, 2) / 2 Interval(0.5, 1.0) >>> Interval(1, 2) / Interval(-1, 1) ... affapy.affapyError.affapyError: division by 0
-
__pow__
(n)¶ Operator **
Return the power of an interval with another interval or an integer. With an interval, it uses the identity:
\[x^n = exp(n \times log(x))\]- Parameters
- Returns
self ** n
- Return type
- Raises
affapyError – type error: n must be Interval or int
Examples
>>> Interval(1, 2) ** Interval(3, 4) Interval(1.0, 16.0) >>> Interval(1, 2) ** 3 Interval(1.0, 8.0)
-
__floor__
()¶ Function floor
Return the floor of an interval:
\[floor([a, b]) = [floor(a), floor(b)]\]You need to import floor from the math library.
Examples
>>> from math import floor >>> floor(Interval(1.4, 2.5)) Interval(1.0, 2.0)
-
__ceil__
()¶ Function ceil
Return the ceil of an interval:
\[ceil([a, b]) = [ceil(a), ceil(b)]\]You need to import ceil from the math library.
Examples
>>> from math import ceil >>> ceil(Interval(1.4, 2.5)) Interval(2.0, 3.0)
-
__abs__
()¶ Function abs
Return the absolute value of an interval. Three possibilities:
If \([a, b] < 0\):
\[abs([a, b]) = [-b, -a]\]If \([a, b] > 0\):
\[abs([a, b]) = [a, b]\]If \(0 \in [a, b]\):
\[abs([a, b]) = [0, max\{abs(a), abs(b)\}]\]Examples
>>> abs(Interval(-2, -1)) Interval(1.0, 2.0) >>> abs(Interval(1, 2)) Interval(1.0, 2.0) >>> abs(Interval(-2, 1)) Interval(0.0, 2.0)
-
sqrt
()¶ Function sqrt
Return the square root of an interval:
\[\sqrt{[a, b]} = [\sqrt{a}, \sqrt{b}]\]It is possible only if \(a \geq 0\).
- Parameters
self (Interval) – operand
- Returns
sqrt(self)
- Return type
- Raises
affapyError – inf must be >= 0
Examples
>>> Interval(1, 2).sqrt() Interval(1.0, 1.4142135623731) >>> Interval(-1, 2).sqrt() ... affapy.affapyError.affapyError: inf must be >= 0
-
exp
()¶ Function exp
Return the exponential of an interval:
\[exp([a, b]) = [exp(a), exp(b)]\]Examples
>>> Interval(1, 2).exp() Interval(2.71828182845905, 7.38905609893065)
-
log
()¶ Function log
Return the logarithm of an interval:
\[log([a, b]) = [log(a), log(b)]\]It is possible only if \(a > 0\).
- Parameters
self (Interval) – operand
- Returns
log(self)
- Return type
- Raises
affapyError – inf must be > 0
Examples
>>> Interval(1, 2).log() Interval(0.0, 0.693147180559945) >>> Interval(-1, 2).log() ... affapy.affapyError.affapyError: inf must be > 0
-
minTrigo
()¶ Return the minimal \(2\pi\) periodic interval of an interval.
-
cos
()¶ Function cos
Return the cosinus of an interval. It considers the minimal \(2\pi\) periodic interval \([a, b]\) of the interval \(x\). Then:
If \(a \leq \pi\):
if \(b \leq \pi\):
\[cos(x) = [cos(b), cos(a)]\]if \(\pi < b \leq 2\pi\):
\[cos(x) = [-1, max(cos(a), cos(b))]\]else:
\[cos(x) = [-1, 1]\]If \(\pi < a \leq 2\pi\):
if \(b \leq 2\pi\):
\[cos(x) = [cos(a), cos(b)]\]if \(2\pi < b \leq 3\pi\):
\[cos(x) = [min(cos(a), cos(b)), 1]\]else:
\[cos(x) = [-1, 1]\]Examples
>>> Interval(1, 2).cos() Interval(-0.416146836547142, 0.54030230586814)
-
sin
()¶ Function sin
Return the sinus of an interval. It uses the identity:
\[sin(x) = cos\left(\frac{\pi}{2} - x\right)\]
-
tan
()¶ Function tan
Return the tangent of an interval. It uses the identity:
\[tan(x) = \frac{sin(x)}{cos(x)}\]
-
cotan
()¶ Function cotan
Return the cotangent of an interval. It uses the identity:
\[cotan(x) = \frac{cos(x)}{sin(x)}\]
-
cosh
()¶ Function cosh
Return the hyperbolic cosine of an interval. It uses the identity:
\[cosh(x) = \frac{exp(x) + exp(-x)}{2}\]
-
sinh
()¶ Function sinh
Return the hyperbolic sine of an interval. It uses the identity:
\[sinh(x) = \frac{exp(x) - exp(-x)}{2}\]
-
tanh
()¶ Function tanh
Return the hyperbolic tangeant of an interval. It uses the identity:
\[tanh(x) = \frac{sinh(x)}{cosh(x)}\]
-
__eq__
(other)¶ Operator ==
Compare two intervals.
- Parameters
- Returns
self == other
- Return type
bool
- Raises
affapyError – other must be Interval
Examples
>>> Interval(1, 2) == Interval(1, 2) True >>> Interval(1, 2) == Interval(1, 3) False
-
__ne__
(other)¶ Operator !=
Negative comparison of two intervals.
- Parameters
- Returns
self != other
- Return type
bool
- Raises
affapyError – other must be Interval
Examples
>>> Interval(1, 2) != Interval(1, 2) False >>> Interval(1, 2) != Interval(1, 3) True
-
__ge__
(other)¶ Operator >=
Greater or equal comparison between two intervals or with int, float or mpf.
- Parameters
- Returns
self >= other
- Return type
bool
- Raises
affapyError – other must be Interval, int, float, mpf
Examples
>>> Interval(1, 2) >= Interval(0, 1) True >>> Interval(1, 2) >= Interval(0, 2) False
-
__gt__
(other)¶ Operator >
Greater comparison between two intervals or with int, float or mpf.
- Parameters
- Returns
self > other
- Return type
bool
- Raises
affapyError – other must be Interval, int, float, mpf
Examples
>>> Interval(1, 2) > Interval(0, 1) False
-
__le__
(other)¶ Operator <=
Lesser or equal comparison between two intervals or with int, float or mpf.
- Parameters
- Returns
self <= other
- Return type
bool
- Raises
affapyError – other must be Interval, int, float, mpf
Examples
>>> Interval(1, 2) <= Interval(2, 3) True >>> Interval(1, 2) <= Interval(1, 3) False
-
__lt__
(other)¶ Operator <
Lesser comparison between two Intervals or with int, float or mpf.
- Parameters
- Returns
self < other
- Return type
bool
- Raises
affapyError – other must be Interval, int, float, mpf
Examples
>>> Interval(1, 2) <= Interval(2, 3) False
-
__contains__
(other)¶ Operator in
Return True if other, who is interval, or int, or float or mpf, is in self, who is an interval:
\[x \in [a, b]\]- Parameters
- Returns
other in self
- Return type
bool
- Raises
affapyError – if other is not Interval, int, float, Affine, mpf
Examples
>>> Interval(1, 2) in Interval(1, 3) True >>> Interval(1, 4) in Interval(1, 3) False
-
straddles_zero
()¶ Return True if the interval straddles 0, False if not.
- Parameters
self (Interval) – operand
- Returns
self straddles 0
- Return type
bool
-
__str__
()¶ String format
Make the string format.
- Parameters
self (Interval) – arg
- Returns
the infimum and the supremum
- Return type
string
Examples
>>> print(Interval(1, 2) [1, 2]