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¶
UncertainArray
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 bymatmul()
(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 incore
can be applied directly to anUncertainArray
. AnUncertainArray
is returned, containing the result of the function applied to each element.
Functions¶
The functions
inv()
,transpose()
,solve()
anddet()
implement the usual linear algebra operations.The functions
identity()
,empty()
,zeros()
full()
andones()
create simple arrays.
Reporting functions¶
Reporting functions
u_component()
andsensitivity()
can be applied directly to a pair of arrays. AnUncertainArray
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¶
-
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: - array – An array-like object containing
int
,float
,complex
UncertainReal
orUncertainComplex
elements. - label (str) – A label to assign to the array. This label does not change labels previously assigned to array elements.
- names (list[str]) – The field names to use to create a structured array.
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)])
- array – An array-like object containing
-
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
-
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
-
solve
(a, b)¶ Return \(x\), the solution of \(a \cdot x = b\)
New in version 1.1.
Parameters: - a – 2D
UncertainArray
- b –
UncertainArray
Return type: Example:
>>> a = la.uarray([[-2,3],[-4,1]]) >>> b = la.uarray([4,-2]) >>> la.solve(a,b) uarray([1.0, 2.0])
- a – 2D
-
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]])
-
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
-
identity
(n)¶ Return an identity array with
n
dimensionsNew in version 1.1.
Example:
>>> la.identity(3) uarray([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
-
zeros
(shape)¶ Return an array of shape
shape
containing0
elementsNew in version 1.1.
Example:
>>> la.zeros( (2,3) ) uarray([[0, 0, 0], [0, 0, 0]])
-
ones
(shape)¶ Return an array of shape
shape
containing1
elementsNew in version 1.1.
Example:
>>> la.ones( (2,3) ) uarray([[1, 1, 1], [1, 1, 1]])
-
empty
(shape)¶ Return an array of shape
shape
containingNone
elementsNew 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
containingfill_value
elementsNew 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)]])
-
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
-
UncertainArray
¶