Linear Algebra

This module provides support for calculations using arrays containing uncertain numbers.

The shorter name la has been defined as an alias for linear_algebra, to resolve the names of objects defined in this module.

Arrays of Uncertain Numbers

UncertainArray is a convenient container of uncertain numbers. The preferred way to create arrays is the function uarray().

An array can contain a mixture of UncertainReal, UncertainComplex and Python numbers (int, float and complex).

The usual mathematical operations can be applied to an array. For instance, if A and B have the same size, they can be added A + B, subtracted A - B, etc; or a function like sqrt(A) can be applied. This vectorisation provides a succinct notation for repetitive operations but it does not offer a significant speed advantage over Python iteration.

Note

To evaluate the product of two-dimensional arrays representing matrices, the function matmul() should be used (for Python 3.5 and above the built-in binary operator @ is an alternative). For example:

>>> a = la.uarray([[1.1,.5],[ureal(3,1),.5]])
>>> b = la.uarray([[5.2,ucomplex(4,1)],[.1,.1+3j]])
>>> la.matmul(a,b)
uarray([[5.7700000000000005,
         ucomplex((4.45+1.5j), u=[1.1,1.1], r=0.0, df=inf)],
        [ureal(15.650000000000002,5.2,inf),
         ucomplex((12.05+1.5j), u=[5.0,3.0], r=0.0, df=inf)]])

Classes

Arithmetic operations

Arithmetic operations are defined for arrays (unary + and -, and binary +, - and *). The multiplication operator * is implemented element-wise. For two-dimensional arrays, matrix multiplication is performed by matmul() (since Python 3.5, the @ operator can be used). Also, dot() evaluates the array dot product, which for two-dimensional arrays is equivalent to matrix multiplication.

When one argument is a scalar, it is applied to each element of the array in turn.

Mathematical operations

The standard mathematical operations defined in core can be applied directly to an UncertainArray. An UncertainArray is returned, containing the result of the function applied to each element.

Functions

The functions inv(), transpose(), solve() and det() implement the usual linear algebra operations.

The functions identity(), empty(), zeros() full() and ones() create simple arrays.

Reporting functions

Reporting functions u_component() and sensitivity() can be applied directly to a pair of arrays. An UncertainArray containing the result of applying the function to pairs of elements will be returned.

The core GTC function result() can be used to define elements of an array as intermediate uncertain numbers.

Array broadcasting

When binary arithmetic operations are applied to arrays, the shape of the array may be changed for the purposes of the calculation. The rules are as follows:

  • If arrays do not have the same number of dimensions, then dimensions of size 1 are prepended to the smaller array’s shape

Following this, the size of array dimensions are compared and checked for compatibility. Array dimensions are compatible when

  • dimension sizes are equal, or

  • one of the dimension sizes is 1

Finally, if either of the compared dimension sizes is 1, the size of the larger dimension is used. For example:

>>> x = la.uarray([1,2])
>>> y = la.uarray([[1],[2]])
>>> print(x.shape,y.shape)  
(2,) (2, 1)
>>> x + y
uarray([[2, 3],
        [3, 4]])

Module contents

det(a)

Return the matrix determinant

New in version 1.1.

Example:

>>> x = la.uarray( range(4) )
>>> x.shape = 2,2
>>> print(x)
[[0 1]
[2 3]]
>>> la.det(x)
-2.0
dot(lhs, rhs)

Dot product of two arrays.

For more details see numpy.dot().

New in version 1.1.

Parameters:
  • lhs – The array-like object on the left-hand side.

  • rhs – The array-like object on the right-hand side.

Returns:

The dot product.

Return type:

UncertainArray

empty(shape)

Return an array of shape shape containing None elements

New in version 1.1.

Example:

>>> la.empty( (2,3) )
uarray([[None, None, None],
        [None, None, None]])
full(shape, fill_value)

Return an array of shape shape containing fill_value elements

New in version 1.1.

Example:

>>> la.full( (1,3),ureal(2,1) )
uarray([[ureal(2.0,1.0,inf), ureal(2.0,1.0,inf),
         ureal(2.0,1.0,inf)]])
identity(n)

Return an identity array with n dimensions

New in version 1.1.

Example:

>>> la.identity(3)
uarray([[1, 0, 0],
        [0, 1, 0],
        [0, 0, 1]])
inv(a)

Return the (multiplicative) matrix inverse

New in version 1.1.

Example:

>>> x = la.uarray( [[2,1],[3,4]])
>>> x_inv =la.inv(x)
>>> la.matmul(x,x_inv)
uarray([[1.0, 0.0],
        [4.440892098500626e-16, 1.0]])
matmul(lhs, rhs)

Matrix product of a pair of two-dimensional arrays.

For more details see numpy.matmul.

New in version 1.1.

Parameters:
  • lhs – 2D array-like object.

  • rhs – 2D array-like object.

Returns:

The matrix product.

Return type:

UncertainArray

ones(shape)

Return an array of shape shape containing 1 elements

New in version 1.1.

Example:

>>> la.ones( (2,3) )
uarray([[1, 1, 1], [1, 1, 1]])
solve(a, b)

Return \(x\), the solution of \(a \cdot x = b\)

New in version 1.1.

Parameters:
Return type:

UncertainArray

Example:

>>> a = la.uarray([[-2,3],[-4,1]])
>>> b = la.uarray([4,-2])
>>> la.solve(a,b)
uarray([1.0, 2.0])
transpose(a, axes=None)

Array transpose

For more details see numpy.transpose().

New in version 1.1.

Parameters:

a – The array-like object

Returns:

The transpose

Return type:

UncertainArray

uarray(array, label=None, names=None)

Create an array of uncertain numbers.

For an overview on how to use an UncertainArray see Examples using UncertainArray.

New in version 1.1.

Attention

Requires numpy \(\geq\) v1.13.0 to be installed.

Parameters:
Returns:

An UncertainArray.

Examples:

Create an amps and a volts array and then calculate the resistances

>>> amps = la.uarray([ureal(0.57, 0.18), ureal(0.45, 0.12), ureal(0.68, 0.19)])
>>> volts = la.uarray([ureal(10.3, 1.3), ureal(9.5, 0.8), ureal(12.6, 1.9)])
>>> resistances = volts / amps
>>> resistances
uarray([ureal(18.070175438596493,6.145264246839438,inf),
        ureal(21.11111111111111,5.903661880050747,inf),
        ureal(18.52941176470588,5.883187720636909,inf)])

Create a Structured array, with the names 'amps' and 'volts', and then calculate the resistances.

>>> data = la.uarray([(ureal(0.57, 0.18), ureal(10.3, 1.3)),
...                (ureal(0.45, 0.12), ureal(9.5, 0.8)),
...                (ureal(0.68, 0.19), ureal(12.6, 1.9))], names=['amps', 'volts'])
>>> resistances = data['volts'] / data['amps']
>>> resistances
uarray([ureal(18.070175438596493,6.145264246839438,inf),
        ureal(21.11111111111111,5.903661880050747,inf),
        ureal(18.52941176470588,5.883187720636909,inf)])
zeros(shape)

Return an array of shape shape containing 0 elements

New in version 1.1.

Example:

>>> la.zeros( (2,3) )
uarray([[0, 0, 0],
        [0, 0, 0]])
class UncertainArray(array, dtype=None, label=None)

An UncertainArray can contain elements of type int, float, complex, UncertainReal or UncertainComplex.

Do not instantiate this class directly. Use uarray() instead.

Base: numpy.ndarray

New in version 1.1.

conjugate()

The result of applying the attribute conjugate to each element in the array.

Example:

>>> a = la.uarray([ucomplex(1.2-0.5j, 0.6), ucomplex(3.2+1.2j, (1.4, 0.2)), ucomplex(1.5j, 0.9)])
>>> a.conjugate()
uarray([ucomplex((1.2+0.5j), u=[0.6,0.6], r=0.0, df=inf),
        ucomplex((3.2-1.2j), u=[1.4,0.2], r=0.0, df=inf),
        ucomplex((0-1.5j), u=[0.9,0.9], r=0.0, df=inf)])
Return type:

UncertainArray

dof()

The result of dof() for each element in the array.

Example:

>>> a = la.uarray([ureal(6, 2, df=3), ureal(4, 1, df=4), ureal(5, 3, df=7), ureal(1, 1)])
>>> a.dof()
uarray([3.0, 4.0, 7.0, inf])
Return type:

UncertainArray

property imag

The result of applying the attribute imag to each element in the array.

Example:

>>> a = la.uarray([ucomplex(1.2-0.5j, 0.6), ucomplex(3.2+1.2j, (1.4, 0.2)), ucomplex(1.5j, 0.9)])
>>> a.imag
uarray([ureal(-0.5,0.6,inf), ureal(1.2,0.2,inf),
        ureal(1.5,0.9,inf)])
Return type:

UncertainArray

property label

The label that was assigned to the array when it was created.

Example:

>>> current = la.uarray([ureal(0.57, 0.18), ureal(0.45, 0.12), ureal(0.68, 0.19)], label='amps')
>>> current.label
'amps'
Return type:

str

property r

The result of applying the attribute r to each element in the array.

Example:

>>> a = la.uarray([ucomplex(1.2-0.5j, (1.2, 0.7, 0.7, 2.2)),
...                ucomplex(-0.2+1.2j, (0.9, 0.4, 0.4, 1.5))])
>>> a.r
uarray([0.43082021842766455, 0.34426518632954817])
Return type:

UncertainArray

property real

The result of applying the attribute real to each element in the array.

Example:

>>> a = la.uarray([ucomplex(1.2-0.5j, 0.6), ucomplex(3.2+1.2j, (1.4, 0.2)), ucomplex(1.5j, 0.9)])
>>> a.real
uarray([ureal(1.2,0.6,inf), ureal(3.2,1.4,inf),
        ureal(0.0,0.9,inf)])
Return type:

UncertainArray

uncertainty()

The result of uncertainty() for each element in the array.

Example:

>>> r = la.uarray([ureal(0.57, 0.18), ureal(0.45, 0.12), ureal(0.68, 0.19)])
>>> r.uncertainty()
uarray([0.18, 0.12, 0.19])
>>> c = la.uarray([ucomplex(1.2-0.5j, 0.6), ucomplex(3.2+1.2j, (1.4, 0.2)), ucomplex(1.5j, 0.9)])
>>> c.uncertainty()
uarray([StandardUncertainty(real=0.6, imag=0.6),
       StandardUncertainty(real=1.4, imag=0.2),
       StandardUncertainty(real=0.9, imag=0.9)])
Return type:

UncertainArray

value()

The result of value() for each element in the array.

Example:

>>> a = la.uarray([0.57, ureal(0.45, 0.12), ucomplex(1.1+0.68j, 0.19)])
>>> a.value()
uarray([0.57, 0.45, (1.1+0.68j)])
Return type:

UncertainArray

variance()

The result of variance() for each element in the array.

Example:

>>> r = la.uarray([ureal(0.57, 0.18), ureal(0.45, 0.12), ureal(0.68, 0.19)])
>>> r.variance()
uarray([0.0324, 0.0144, 0.0361])
>>> c = la.uarray([ucomplex(1.2-0.5j, 0.6), ucomplex(3.2+1.2j, (1.5, 0.5)), ucomplex(1.5j, 0.9)])
>>> c.variance()
uarray([VarianceCovariance(rr=0.36, ri=0.0, ir=0.0, ii=0.36),
        VarianceCovariance(rr=2.25, ri=0.0, ir=0.0, ii=0.25),
        VarianceCovariance(rr=0.81, ri=0.0, ir=0.0, ii=0.81)])
Return type:

UncertainArray