Next / Previous / Contents / Shipman's homepage

8. Linear algebra functions

A number of linear algebra functions are available as sub-module linalg of numpy.

np.linalg.det(a)

Returns the determinant of a 2-d array a.

>>> m = np.array(((2,3), (-1, -2)))
>>> print m
[[ 2  3]
 [-1 -2]]
>>> print np.linalg.det(m)
-1.0
np.linalg.inv(a)

Returns the matrix inverse of a non-singular 2-d array a.

>>> good = np.array(((2.1, 3.2), (4.3, 5.4)))
>>> print np.linalg.inv(good)
[[-2.23140496  1.32231405]
 [ 1.7768595  -0.8677686 ]]
np.linalg.norm(a)

Returns the Frobenius norm of array a.

>>> print good
[[ 2.1  3.2]
 [ 4.3  5.4]]
>>> print np.linalg.norm(good)
7.89303490427
np.linalg.solve(A, b)

Solves systems of simultaneous linear equations. Given an N×N array of coefficients A, and a length-N vector of constants b, returns a length-N vector containing the solved values of the variables.

Here's an example system:

    2x +  y = 19
     x - 2y = 2

Here's a conversational example showing the solution (x=8 and y=3):

>>> coeffs = np.array([[2,1], [1,-2]])
>>> print coeffs
[[ 2  1]
 [ 1 -2]]
>>> consts = np.array((19, 2))
>>> print consts
[19  2]
>>> print np.linalg.solve(coeffs, consts)
[ 8.  3.]