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:

\[[a, b]\]

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

Interval

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]\]
Parameters

self (Interval) – operand

Returns

-self

Return type

Interval

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
  • self (Interval) – first operand

  • other (Interval or int or float or mpf) – second operand

Returns

self + other

Return type

Interval

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
  • self (Interval) – second operand

  • other (Interval or int or float or mpf) – first operand

Returns

other + self

Return type

Interval

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
  • self (Interval) – first operand

  • other (Interval or int or float or mpf) – second operand

Returns

self - other

Return type

Interval

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
  • self (Interval) – second operand

  • other (Interval or int or float or mpf) – first operand

Returns

other - self

Return type

Interval

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
  • self (Interval) – first operand

  • other (Interval or int or float or mpf) – second operand

Returns

self * other

Return type

Interval

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
  • self (Interval) – second operand

  • other (Interval or int or float or mpf) – first operand

Returns

other * self

Return type

Interval

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

Interval

Raises

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
  • self (Interval) – first operand

  • n (Interval or int) – second operand (exponent)

Returns

self ** n

Return type

Interval

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.

Parameters

self (Interval) – arg

Returns

[floor(inf), floor(sup)]

Return type

Interval

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.

Parameters

self (Interval) – arg

Returns

[ceil(inf), ceil(sup)]

Return type

Interval

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:

  1. If \([a, b] < 0\):

\[abs([a, b]) = [-b, -a]\]
  1. If \([a, b] > 0\):

\[abs([a, b]) = [a, b]\]
  1. If \(0 \in [a, b]\):

\[abs([a, b]) = [0, max\{abs(a), abs(b)\}]\]
Parameters

self (Interval) – operand

Returns

abs(self)

Return type

Interval

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

Interval

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)]\]
Parameters

self (Interval) – operand

Returns

exp(self)

Return type

Interval

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

Interval

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.

Parameters

self (Interval) – operand

Returns

minimal \(2\pi\) periodic interval

Return type

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:

  1. 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]\]
  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]\]
Parameters

self (Interval) – operand

Returns

cos(self)

Return type

Interval

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)\]
Parameters

self (Interval) – operand

Returns

sin(self)

Return type

Interval

tan()

Function tan

Return the tangent of an interval. It uses the identity:

\[tan(x) = \frac{sin(x)}{cos(x)}\]
Parameters

self (Interval) – operand

Returns

tan(self)

Return type

Interval

cotan()

Function cotan

Return the cotangent of an interval. It uses the identity:

\[cotan(x) = \frac{cos(x)}{sin(x)}\]
Parameters

self (Interval) – operand

Returns

cotan(self)

Return type

Interval

cosh()

Function cosh

Return the hyperbolic cosine of an interval. It uses the identity:

\[cosh(x) = \frac{exp(x) + exp(-x)}{2}\]
Parameters

self (Interval) – operand

Returns

cosh(self)

Return type

Interval

sinh()

Function sinh

Return the hyperbolic sine of an interval. It uses the identity:

\[sinh(x) = \frac{exp(x) - exp(-x)}{2}\]
Parameters

self (Interval) – operand

Returns

sinh(self)

Return type

Interval

tanh()

Function tanh

Return the hyperbolic tangeant of an interval. It uses the identity:

\[tanh(x) = \frac{sinh(x)}{cosh(x)}\]
Parameters

self (Interval) – operand

Returns

tanh(self)

Return type

Interval

__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
  • self (Interval) – first operand

  • other (Interval or int or float or mpf) – second operand

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
  • self (Interval) – first operand

  • other (Interval or int or float or mpf) – second operand

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
  • self (Interval) – first operand

  • other (Interval or int or float or mpf) – second operand

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
  • self (Interval) – first operand

  • other (Interval or int or float or mpf) – second operand

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]
__repr__()

Repr format

Make the repr format.

Parameters

self (Interval) – arg

Returns

format

Return type

string

copy()

Copy an interval.

Parameters

self (Interval) – arg

Returns

self copy

Return type

Interval

convert()

Convert an interval \([a, b]\) to an affine form:

\[\hat{x} = x_0 + x_k\epsilon_k\]

with:

\[x_0 = \frac{a + b}{2} , x_k = \frac{a - b}{2}\]
Parameters

self (Interval) – operand

Returns

affine form associated to the interval

Return type

Affine