A linear algebra session

from numpy import *   # includes import of linalg

# fill matrix A and vectors x and b
b = dot(A, x)             # matrix-vector product
y = linalg.solve(A, b)    # solve A*y = b

if allclose(x, y, atol=1.0E-12, rtol=1.0E-12):
    print 'correct solution!'

d = linalg.det(A)
B = linalg.inv(A)

# check result:
R = dot(A, B) - eye(n)   # residual
R_norm = linalg.norm(R)  # Frobenius norm of matrix R
print 'Residual R = A*A-inverse - I:', R_norm

A_eigenvalues = linalg.eigvals(A)  # eigenvalues only
A_eigenvalues, A_eigenvectors = linalg.eig(A)  

for e, v in zip(A_eigenvalues, A_eigenvectors):
    print 'eigenvalue %g has corresponding vector\n%s' % (e, v)

previousnexttable of contents