Linear algebra

This module supports array and matrix operations.

The prefix linear_algebra (or the alias la) is needed as to resolve the names of objects defined in this module.

Classes

Arithmetic operations

Arithmetic operations are defined for both arrays and matrices (unary + and -, and binary +, - and *). The multiplication operator * is implemented element-wise for arrays, but performs the matrix product for matrices.

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

array and matrix define attributes as short-hand for some operations:

  • the array, or matrix, attribute T transposes the object
  • the matrix attribute I evaluates the matrix inverse

Functions

The functions inverse, transpose, solve and det implement the usual linear algebra operations.

Functions asmatrix and asarray change the type of object. The function aslist turns an array or matrix into a (possibly nested) list.

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

Array broadcasting

When binary arithmetic operations are used on 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 larger dimension size is used. For example:

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

Module contents

class array(obj, copy=True)

An array object stores a regular array of elements

Parameters:
  • obj – an array, a matrix, a sequence or an iterable object
  • copy – Boolean

An array can be constructed from another array or matrix, a sequence or an iterator. Nested sequences or iterators create multidimensional arrays.

When copy is True and obj is an array or, matrix, data is shared, not copied.

Examples:

>>> x = la.array( range(4) )
>>> x
array([0, 1, 2, 3])
>>> x.shape = (2,2)
>>> x
array([[0, 1],
[2, 3]])

>>> x = la.array([[0, 1],[2, 3]])
>>> x
array([[0, 1],
[2, 3]])
T

The transpose

(see also transpose)

Example:

>>> x = la.array( range(8))
>>> x.shape = 2,4
>>> xt = x.T
>>> xt.shape
(4, 2)
>>> print x
[[0 1 2 3]
[4 5 6 7]]
>>> print xt
[[0 4]
[1 5] 
[2 6] 
[3 7]]
copy()

Return a copy

Example:

>>> a = array( [1,2] )
>>> a_copy = a.copy()
>>> a_copy
array([1, 2])
>>> a
array([1, 2])
>>> a[1] = 3
>>> a_copy
array([1, 2])
>>> a
array([1, 3])
flat

An iterator for all elements

Example:

>>> x = la.array( [[1,2],[3,4]])
>>> print x
[[1 2]
[3 4]]
>>> xf = la.array( x.flat )
>>> print xf
[1 2 3 4]
shape

a tuple of dimension sizes

class matrix(obj, copy=True)

A matrix stores a 2-by-2 array of objects

Parameters:
  • obj – an array, a matrix, a sequence or an iterable
  • copy – Boolean

A matrix may be constructed from an array or matrix, a sequence or an iterator.

When copy is True and obj is an array, or matrix, data is shared, not copied.

Examples:

>>> x = la.matrix( range(4) )
>>> x
matrix([[0, 1, 2, 3]])
>>> x.shape = (2,2)
>>> x
matrix([[0, 1],
[2, 3]])

>>> x = la.matrix([[0, 1],[2, 3]])
>>> x
matrix([[0, 1],
[2, 3]])
I

The matrix inverse

(see also inverse)

Example:

>>> x = la.matrix( [[1,2],[3,4]])
>>> x * x.I
matrix([[0.9999999999999998, 1.1102230246251565e-16],
[0.0, 1.0000000000000002]])
T

The transpose

(see also transpose)

Example:

>>> x = la.array( range(8))
>>> x.shape = 2,4
>>> xt = x.T
>>> xt.shape
(4, 2)
>>> print x
[[0 1 2 3]
[4 5 6 7]]
>>> print xt
[[0 4]
[1 5] 
[2 6] 
[3 7]]
copy()

Return a copy

Example:

>>> a = array( [1,2] )
>>> a_copy = a.copy()
>>> a_copy
array([1, 2])
>>> a
array([1, 2])
>>> a[1] = 3
>>> a_copy
array([1, 2])
>>> a
array([1, 3])
flat

An iterator for all elements

Example:

>>> x = la.array( [[1,2],[3,4]])
>>> print x
[[1 2]
[3 4]]
>>> xf = la.array( x.flat )
>>> print xf
[1 2 3 4]
shape

a tuple of dimension sizes

det(a)

Return the matrix determinant

Example:

>>> x = la.matrix( range(4) )
>>> x.shape = 2,2
>>> print x
[[0 1]
[2 3]]
>>> la.det(x)
-2.0
inverse(a)

Return the matrix inverse

Example:

>>> x = la.matrix( [[2,1],[3,4]])
>>> x * la.inverse(x)
matrix([[1.0, 0.0],
[4.440892098500626e-16, 1.0]])
transpose(a, axes=None)

Return the transpose of a.

Parameters:
  • a – an array, or matrix
  • axes – a sequence of axis indices
Returns:

an array, or matrix ( the same type as a ).

Note

  • A new object is created (ie, a is not affected).
  • By default, the function will reverse the dimensions of a.
  • axes may contain a sequence of integers indexing the dimensions of a. The axes of a will then be permuted according to the contents of axes.

Examples:

>>> x = la.array( range(4) )
>>> x.shape = 2,2
>>> la.transpose(x)
array([[0, 2],
[1, 3]])

>>> x = la.array( range(12) )
>>> x.shape = 2,3,2
>>> print x
[[[0 1]
[2 3] 
[4 5]]
[[6 7]
[8 9] 
[10 11]]]
>>> la.transpose(x,(0,2,1))
array([[[0, 2, 4],
[1, 3, 5]],
[[6, 8, 10],
[7, 9, 11]]])
identity(ndim)

Return an identity array with ndim dimensions

Example:

>>> la.identity(3)
array([[1.0, 0, 0],
[0, 1.0, 0],
[0, 0, 1.0]])
ones(shape)

Return an array of shape shape containing 1‘s

Example:

>>> la.ones( (2,3) )
array([[1, 1, 1],
[1, 1, 1]])
empty(shape)

Return an array of shape shape containing None elements

Example:

>>> la.empty( (2,3) )
array([[None, None, None],
[None, None, None]])
zeros(shape)

Return an array of shape shape containing 0‘s

Example:

>>> la.zeros( (2,3) )
array([[0, 0, 0],
[0, 0, 0]])
solve(a, b)

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

Parameters:
  • a – an array or matrix
  • b – an array or matrix
Return type:

array or matrix

The type of object returned depends on the type of arguments. A matrix is returned if a or b is a matrix, otherwise an array is returned.

Example:

>>> a = la.matrix([[-2,3],[-4,1]])
>>> b = la.array([4,-2])
>>> la.solve(a,b)
matrix([[1.0, 2.0]])
asarray(a)

Return a as an array without copying

Example:

>>> x = la.array( (1,2,3) )
>>> x
array([1, 2, 3])
>>> la.asmatrix(x)
matrix([[1, 2, 3]])
asmatrix(a)

Return a as a matrix without copying

Example:

>>> x = la.array( range(4) )
>>> la.asmatrix(x)
matrix([[0, 1, 2, 3]])
aslist(a)

Return a list of the data in a

A list is constructed by iterating over the elements of a. If the elements of a are iterable, a nested list is created.

Parameters:a – an iterable object