CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download

Tutorials

Views: 1388
Kernel: Python 3 (Ubuntu Linux)

Linear Algebra

We can perform basic linear algebra using numpy

import numpy as np # Define matrix and vector x = np.array([0,1,2]) A = np.array([[2,1,2],[3,2,0],[1,0,1]]) # matrix-vector product y = A@x # element-wise operations x[0] = 1 A[0,1] = 2 # extracing rows and columns print(A[1,:]) print(A[:,1]) # copy matrix B = A.copy() # transpose B.T
[3 2 0] [2 2 0]
array([[2, 3, 1], [2, 2, 0], [2, 0, 1]])

Exercise

What happens when you define a new matrix using C = A? Hint: try changing an element in the new matrix and inspect both C and A.

Matrix and vector norms

# vector p-norm and induced matrix norm np.linalg.norm(x,2) np.linalg.norm(x,1) np.linalg.norm(x,np.inf) np.linalg.norm(A,2) np.linalg.norm(A,1) np.linalg.norm(A,np.inf) # Frobenius norm np.linalg.norm(A,'fro')
5.196152422706632

Special arrays

# all ones or zeros y = np.zeros(4) F = np.ones((4,4)) # normally distributed random elements r = np.random.randn(4) # identity matrix I = np.eye(3) # banded matrix L = np.diag([1,1],k=-1)

Exercise

Create a 10 x 10 matrix with aii=2a_{ii} = -2, ai,i+1=ai,i1=1a_{i,i+1} = a_{i,i-1} = 1

Eigenvalues and vectors

A = np.array([[2,1,2],[3,2,0],[1,0,1]]) l,V = np.linalg.eig(A) print('eigenvalues of A:',l) print('first eigenvector of A: ',V[:,0])
eigenvalues of A: [ 4.0861302 -0.51413693 1.42800673] first eigenvector of A: [-0.5613869 -0.80731332 -0.18190642]

Exercise

Compute the eigenvalues and vectors of A=(3103) A = \left(\begin{matrix} 3 & 1 \\ 0 & 3 \end{matrix}\right) What is special about this matrix?

Sparse matrices

Large matrices that have few non-zero entries are more efficiently stored as sparse matrix:

# import scipy module for sparse matrices import scipy.sparse as sp # sparse identity matrix D = sp.eye(100) # random sparse matrix R = sp.random(100,100,density=0.01)
# plot a sparse matrix import matplotlib.pyplot as plt plt.spy(R)
<matplotlib.lines.Line2D at 0x7f512272fba8>
Image in a Jupyter notebook

Exercise

Check what the command diags([1, -2, 1], [-1, 0, 1], shape=(4, 4)) produces