about_a_matrix(): Linear algebra questions about a matrix. Matrix M: [[ 1 2 3] [ 4 5 6] [ 7 8 10]] Matrix A: [[ 2. -1. 0. 0. 0. 0.] [-1. 2. -1. 0. 0. 0.] [ 0. -1. 2. -1. 0. 0.] [ 0. 0. -1. 2. -1. 0.] [ 0. 0. 0. -1. 2. -1.] [ 0. 0. 0. 0. -1. 2.]] Python properties of A: type(A) = len(A) is not the size, but the number of rows! len(A) = 6 Numpy properties of A: A.ndim = 2 A.shape = (6, 6) A.size is the size or number of elements. A.size = 36 A.dtype = float64 Transpose is A.T or np.transpose(A) [[ 2. -1. 0. 0. 0. 0.] [-1. 2. -1. 0. 0. 0.] [ 0. -1. 2. -1. 0. 0.] [ 0. 0. -1. 2. -1. 0.] [ 0. 0. 0. -1. 2. -1.] [ 0. 0. 0. 0. -1. 2.]] Flip up and down = np.flipud(A) [[ 0. 0. 0. 0. -1. 2.] [ 0. 0. 0. -1. 2. -1.] [ 0. 0. -1. 2. -1. 0.] [ 0. -1. 2. -1. 0. 0.] [-1. 2. -1. 0. 0. 0.] [ 2. -1. 0. 0. 0. 0.]] Inverse is np.linalg.inv(A) [[0.85714286 0.71428571 0.57142857 0.42857143 0.28571429 0.14285714] [0.71428571 1.42857143 1.14285714 0.85714286 0.57142857 0.28571429] [0.57142857 1.14285714 1.71428571 1.28571429 0.85714286 0.42857143] [0.42857143 0.85714286 1.28571429 1.71428571 1.14285714 0.57142857] [0.28571429 0.57142857 0.85714286 1.14285714 1.42857143 0.71428571] [0.14285714 0.28571429 0.42857143 0.57142857 0.71428571 0.85714286]] Determinant is np.linalg.det(A) 6.999999999999998 Condition is np.linalg.cond(A) 19.195669358089216 Trace is np.trace(A) 12.0 d = np.diag(A) returns diagonal vector. [2. 2. 2. 2. 2. 2.] B = np.diag(v) creates diagonal matrix from vector. [[1 0 0] [0 2 0] [0 0 3]] C = np.diag ( np.diag ( M ) ) ) uses diagonal of M to create new diagonal matrix C [[ 1 0 0] [ 0 5 0] [ 0 0 10]] L = np.tril(M,k) creates lower triangular matrix starting at diagonal k. [[0 0 0] [4 0 0] [7 8 0]] U = np.triu(M,k) creates upper triangular matrix starting at diagonal k [[ 1 2 3] [ 0 5 6] [ 0 0 10]] np.spy(A) displays 0/nonzero entries Row sums is np.sum(M) or np.sum(M,axis=1) 46 [ 6 15 25] Column sums is np.sum(M,axis=0) [12 15 19] Matrix sum np.sum(np.sum(M)) 46 Matrix norms (2 is default, fro is frobenius: np.linalg.norm(A) = 5.830951894845301 np.linalg.norm(A,1) = 4.0 np.linalg.norm(A,2) = 3.801937735804838 np.linalg.norm(A,np.inf) = 4.0 np.linalg.norm(A,'fro') = 5.830951894845301 Row 2, Column 1: M[2,1] 8 Row 2: M[2,:] [ 7 8 10] Column 1: M[:,1] [2 5 8]