Affine Arithmetic (AA)¶
This module can create affine forms and perform operations.
Affine Arithmetic (AA) has been developed to overcome the error explosion problem of standard Interval Arithmetic (IA). This method represents a quantity \(x\) as an affine form \(\hat{x}\), which is a first degree polynomial:
The coefficients \(x_i\) are finite floating-point numbers: they are called the partial deviations.
The coefficient \(x_0\) is the central value of the affine form \(\hat{x}\).
The \(\epsilon_i\) coefficients are symbolic real values called noise symbols. Their values are unknown between -1 and 1.
This representation enables a better tracking of the different quantities inside the affine form.
For example, the quantity \([0, 10]\) can be represented as the following affine form:
where
But we could also represent it like this:
where
Both forms represent the same quantity but they are handling differently the storage of internal quantities. They will behave differently during operation:
whereas
The second example illustrates this behaviour. Even though \(A\) and \(B\) represent the same quantity, they manage their quantity differently. Therefore, they are not equal.
-
class
aa.
Affine
(interval=None, x0=None, xi=None)¶ Bases:
object
Representation of an affine form. An instance of the class Affine is composed of three fields:
interval: the interval associated to the affine form
x0: the center
xi: the dictionnary of noise symbols
-
__init__
(interval=None, x0=None, xi=None)¶ Create an affine form. There are two different ways:
x1 = Affine(interval=[inf, sup]) x2 = Affine(x0=0, xi={})
If no arguments, x0=0 and xi={}.
The first method is easier to use. To convert an interval \([a, b]\) into an affine form, there is the formula:
\[\hat{x} = x_0 + x_k\epsilon_k\]with:
\[x_0 = \frac{a + b}{2} , x_k = \frac{a - b}{2}\]To convert an affine form into an interval \(X\):
\[X = [x_0 + rad(x), x_0 - rad(x)]\]with:
\[rad(x) = \sum_{i=1}^{n} |x_i|\]- Parameters
interval (list or tuple with length 2 or Interval) – the interval
x0 (int or float or mpf) – the center
xi (dict of mpf values) – noise symbols
- Returns
affine form
- Return type
- Raises
affapyError – interval must be list, tuple or Interval
Examples
>>> from affapy.aa import Affine >>> Affine([1, 3]) Affine(2.0, {5: mpf('-1.0')}) mpf('1.0') >>> print(Affine(x0=1, xi={1:2, 2:3})) 1.0 + 2.0e1 + 3.0e2
-
property
interval
¶ Return interval associated to the affine form.
-
property
x0
¶ Return the center x0.
-
property
xi
¶ Return the dictionnary of noice symbols xi.
-
static
_getNewXi
()¶ Get a new noise symbol.
-
rad
()¶ Return the radius of an affine form:
\[rad(x) = \sum_{i=1}^{n} |x_i|\]- Parameters
self (Affine) – operand
- Returns
sum of abs(xi)
- Return type
mpf
Examples
>>> x = Affine([1, 3]) >>> x.rad() mpf('1.0')
-
__neg__
()¶ Operator - (unary)
Return the additive inverse of an affine form:
\[-\hat{x} = -x_0 + \sum_{i=1}^{n} -x_i\epsilon_i\]Examples
>>> print(-Affine([1, 2])) -1.5 + 0.5e1
-
__add__
(other)¶ Operator +
Add two affine forms:
\[\hat{x} + \hat{y} = (x_0 + y_0) + \sum_{i=1}^{n} (x_i + y_i)\epsilon_i\]Or add an affine form and an integer or float or mpf:
\[\hat{x} + y = (x_0 + y) + \sum_{i=1}^{n} x_i\epsilon_i\]- Parameters
- Returns
self + other
- Return type
- Raises
affapyError – other must be Affine, int, float, mpf
Examples
>>> print(Affine([0, 1]) + Affine([3, 4])) 4.0 + -0.5e1 + -0.5e2 >>> print(Affine([1, 2]) + 3) 4.5 + -0.5e1
-
__radd__
(other)¶ Reverse operator +
Add two affine forms or an affine form 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 Affine, int, float, mpf
Examples
>>> print(1 + Affine([1, 2])) 2.5 + -0.5e3
-
__sub__
(other)¶ Operator -
Subtract two affine forms:
\[\hat{x} - \hat{y} = (x_0 - y_0) + \sum_{i=1}^{n} (x_i - y_i)\epsilon_i\]Or subtract an affine form and an integer or float or mpf:
\[\hat{x} - y = (x_0 - y) + \sum_{i=1}^{n} x_i\epsilon_i\]- Parameters
- Returns
self - other
- Return type
- Raises
affapyError – other must be Affine, int, float, mpf
Examples
>>> print(Affine([0, 1]) - Affine([3, 4])) -3.0 + -0.5e4 + 0.5e5 >>> print(Affine([1, 2]) + 3) -1.5 + -0.5e6
-
__rsub__
(other)¶ Reverse operator -
Subtract two affine forms or an integer or float or mpf and an affine form. See the sub operator for more details.
- Parameters
- Returns
other - self
- Return type
- Raises
affapyError – other must be Affine, int, float, mpf
Examples
>>> print(3 - Affine([1, 2])) 1.5 + 0.5e1
-
__mul__
(other)¶ Operator *
Multiply two affine forms:
\[\hat{x}\hat{y} = x_0y_0 + \sum_{i=1}^{n} (x_0y_i + y_0x_i)\epsilon_i + rad(x)rad(y)\epsilon_k\]\(k\) is a new noise symbol. Or multiply an affine form and integer or float or mpf:
\[\hat{x}y = x_0y + \sum_{i=1}^{n} x_iy\epsilon_i\]- Parameters
- Returns
self * other
- Return type
- Raises
affapyError – other must be Affine, int, float, mpf
Examples
>>> print(Affine([1, 2]) * Affine([3, 4])) 5.25 + -1.75e2 + -0.75e3 + 0.25e4
-
__rmul__
(other)¶ Reverse operator *
Multiply two affine forms or an integer or float or mpf and an affine form. See the mul operator for more details.
- Parameters
- Returns
other * self
- Return type
- Raises
affapyError – other must be Affine, int, float, mpf
-
_affineConstructor
(alpha, dzeta, delta)¶ Affine constructor
Return the affine form for non-affine operations:
\[\hat{\chi} = (\alpha x_0 + \zeta) + \sum_{i=1}^{n} \alpha x_i\epsilon_i + \delta \epsilon_k\]\(k\) is a new noise symbol.
- Parameters
alpha (mpmath.mpf) –
dzeta (mpmath.mpf) –
delta (mpmath.mpf) –
- Returns
construction of an affine form
- Return type
-
inv
()¶ Inverse
Return the inverse of an affine form. It uses the affine constructor with:
\[\alpha = -\frac{1}{b^2}\]\[\zeta = abs(mid(i)),\]\[\delta = radius(i)\]with:
\[i = [\frac{1}{a} - \alpha a, \frac{2}{b}]\]\[a = min(|inf|, |sup|)\]\[b = max(|inf|, |sup|)\]where \([inf, sup]\) is the interval associated to the affine form in argument.
- Parameters
self – operand
- Returns
1 / self
- Return type
- Raises
affapyError – the interval associated to the affine form contains 0
-
__truediv__
(other)¶ Operator /
Divide two affine forms or an integer or float or mpf and an affine form. It uses the identity:
\[\frac{x}{y} = x \times \frac{1}{y}\]- Parameters
- Returns
self / other
- Return type
- Raises
affapyError – other must be Affine, int, float, mpf
Examples
>>> print(Affine([1, 2]) / Affine([3, 4])) 0.4375 + -0.145833333333333e11 + 0.046875e12 + 0.0156249999999999e13 + 0.0208333333333333e14
-
__rtruediv__
(other)¶ Reverse operator /
Divide two affine forms or an affine form and an integer or float or mpf. See the truediv operator for more details.
- Parameters
- Returns
other / self
- Return type
- Raises
affapyError – other must be Affine, int, float, mpf
Examples
>>> print(2 / Affine([1, 2])) 1.5 + 0.25e15 + 0.25e16
-
sqr
()¶ Return the square of an affine form. It uses the identity:
\[x^2 = x \times x\]
-
__pow__
(n)¶ Operator **
Return the power of an affine form with another affine form or an integer. With an affine, it uses the identity:
\[x^n = exp(n \times log(x))\]- Parameters
- Returns
self ** n
- Return type
- Raises
affapyError – type error: n must be Affine or int
Examples
>>> print(Affine([1, 2])**3) 3.375 + -3.375e17 + 0.375e18 + 0.875e19
-
__abs__
()¶ Return the absolute value of an affine form. Three possibilities:
If \(x < 0\):
\[|\hat{x}| = -\hat{x}\]If \(x > 0\):
\[|\hat{x}| = \hat{x}\]3 If \(x\) straddles \(0\):
\[|\hat{x}| = \frac{|x_0|}{2} + \sum_{i=1}^{n} \frac{x_i\epsilon_i}{2}\]Examples
>>> print(abs(Affine([1, 2]))) 1.5 + -0.5e24 >>> print(abs(Affine([-2, -1]))) 1.5 + 0.5e25
-
sqrt
()¶ Function sqrt
Return the square root of an affine form. We consider the interval \([a, b]\) associated to the affine form. It uses the affine constructor with:
\[\alpha = \frac{1}{\sqrt{b} + \sqrt{a}}\]\[\zeta = \frac{\sqrt{a} + \sqrt{b}}{8} + \frac{1}{2} \frac{\sqrt{a}\sqrt{b}}{\sqrt{a} + \sqrt{b}}\]\[\delta = \frac{1}{8}\frac{(\sqrt{b} - \sqrt{a})^2}{\sqrt{a} + \sqrt{b}}\]- Parameters
self (Affine) – operand
- Returns
sqrt(self)
- Return type
- Raises
affapyError – the interval associated to the affine form must be >=0
Examples
>>> print(Affine([1, 2]).sqrt()) 1.21599025766973 + -0.207106781186548e27 + 0.00888347648318441e28
-
exp
()¶ Function exp
Return the exponential of an affine form. We consider the interval \([a, b]\) associated to the affine form. It uses the affine constructor with:
\[\alpha = \frac{exp(b) - exp(a)}{b - a}\]\[\zeta = \alpha \times (1 - log(\alpha))\]\[\delta = \frac{\alpha \times (log(\alpha)-1-a)) + exp(a)}{2}\]Examples
>>> print(Affine([1, 2]).exp()) 4.47775520281461 + -2.3353871352358e29 + 4.95873115091173e30
-
log
()¶ Function log
Return the logarithm of an affine form. We consider the interval \([a, b]\) associated to the affine form. It uses the affine constructor with:
\[\alpha = \frac{log(b) - log(a)}{b - a}\]\[\zeta = \frac{-\alpha x_s}{\frac{log(x_s) + y_s}{2}}\]\[\delta = \frac{log(x_s) - y_s}{2}\]with:
\[x_s = \frac{1}{\alpha}\]\[y_s = \alpha (x_s - a) + log(a)\]- Parameters
self (Affine) – operand
- Returns
log(self)
- Return type
- Raises
affapyError – the interval associated to the affine form must be > 0
Examples
>>> print(Affine([1, 2]).log()) -1.93043330907435 + -0.346573590279973e31 + 0.0298300505708048e32
-
sin
(npts=8)¶ Function sin
Return the sinus of an affine form. It uses the least squares and the affine constructor. The argument npts is the number of points for the linear regression approximation.
- Parameters
self (Affine) – operand
npts (int) – number of points (default: 8)
- Returns
sin(self)
- Return type
Examples
>>> print(Affine([1, 2]).sin()) 0.944892253579443 + -0.0342679845626557e33 + 0.0698628113164167e34
-
cos
()¶ Function cos
Return the cosinus of an affine form. It uses the identity:
\[cos(x) = sin\left(x + \frac{\pi}{2}\right)\]
-
tan
()¶ Function tan
Return the tangent of an affine form. It uses the identity:
\[tan(x) = \frac{sin(x)}{cos(x)}\]
-
cotan
()¶ Function cotan
Return the cotangent of an affine form. It uses the identity:
\[cotan(x) = \frac{cos(x)}{sin(x)}\]
-
cosh
()¶ Function cosh
Return the hyperbolic cosine of an affine form. It uses the identity:
\[cosh(x) = \frac{exp(x) + exp(-x)}{2}\]
-
sinh
()¶ Function sinh
Return the hyperbolic sine of an affine form. It uses the identity:
\[sinh(x) = \frac{exp(x) - exp(-x)}{2}\]
-
tanh
()¶ Function tanh
Return the hyperbolic tangeant of an affine form. It uses the identity:
\[tanh(x) = \frac{sinh(x)}{cosh(x)}\]
-
__eq__
(other)¶ Operator ==
Compare two affine forms.
- Parameters
- Returns
self == other
- Return type
bool
- Raises
affapyError – other must be Affine
-
__ne__
(other)¶ Operator !=
Negative comparison of two affine forms.
- Parameters
- Returns
self != other
- Return type
bool
- Raises
affapyError – other must be Affine
-
__contains__
(other)¶ Operator in
Return True if the interval of self is in the interval of other.
- Parameters
- Returns
self in other
- Return type
bool
- Raises
affapyError – other must be Affine, Interval, int, float, mpf
-
straddles_zero
()¶ Return True if the affine form straddles 0, False if not.
- Parameters
self (Affine) – operand
- Returns
0 in self
- Return type
bool
-
strictly_neg
()¶ Return True if the affine is strictly negative, False if not.
- Parameters
self (Affine) – operand
- Returns
self < 0
- Return type
bool
-
__str__
()¶ String format
Make the string format.
- Parameters
self (Affine) – arg
- Returns
sum of noise symbols
- Return type
string
Examples
>>> print(Affine([1, 2])) 1.5 - 0.5*e1