#! /usr/bin/env python3 # def about_a_matrix ( ): ## about_a_matrix() reports some facts about a matrix. # import numpy as np print ( '' ) print ( 'about_a_matrix():' ) print ( ' Linear algebra questions about a matrix.' ) # # Define M = matrix, a 2D numpy array. # M = np.array ( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 10 ] ] ) print ( '' ) print ( ' Matrix M:' ) print ( M ) # # Define D = matrix, a 2D numpy array. # from dif2 import dif2 A = dif2 ( 6 ) print ( '' ) print ( ' Matrix A:' ) print ( A ) print ( '' ) print ( ' Python properties of A:' ) print ( '' ) print ( ' type(A) = ', type ( A ) ) print ( ' len(A) is not the size, but the number of rows!' ) print ( ' len(A) = ', len ( A ) ) print ( '' ) print ( ' Numpy properties of A:' ) print ( '' ) print ( ' A.ndim = ', A.ndim ) print ( ' A.shape = ', A.shape ) print ( ' A.size is the size or number of elements.' ) print ( ' A.size = ', A.size ) print ( ' A.dtype = ', A.dtype ) # # Transpose: # print ( '' ) print ( ' Transpose is A.T or np.transpose(A)' ) print ( A.T ) # # Flipud: # print ( '' ) print ( ' Flip up and down = np.flipud(A)' ) print ( np.flipud ( A ) ) # # Inverse: # print ( '' ) print ( ' Inverse is np.linalg.inv(A)' ) print ( np.linalg.inv(A) ) # # Determinant (of a square matrix): # print ( '' ) print ( ' Determinant is np.linalg.det(A)' ) print ( np.linalg.det(A) ) # # Condition (of a square matrix): # print ( '' ) print ( ' Condition is np.linalg.cond(A)' ) print ( np.linalg.cond(A) ) # # Trace: # print ( '' ) print ( ' Trace is np.trace(A)' ) print ( np.trace(A) ) # # Get diagonal of a matrix: # print ( '' ) print ( ' d = np.diag(A) returns diagonal vector.' ) d = np.diag ( A ) print ( d ) # # Create diagonal matrix from vector. # print ( '' ) print ( ' B = np.diag(v) creates diagonal matrix from vector.' ) v = np.array ( [ 1, 2, 3 ] ) B = np.diag ( v ) print ( B ) # # Create a diagonal matrix from the diagonal of another matrix. # print ( '' ) print ( ' C = np.diag ( np.diag ( M ) ) ) uses diagonal of M' ) print ( ' to create new diagonal matrix C' ) C = np.diag ( np.diag ( M ) ) print ( C ) # # tril(M,k) creates a lower triangular matrix # print ( '' ) print ( ' L = np.tril(M,k) creates lower triangular matrix' ) print ( ' starting at diagonal k.' ) L = np.tril ( M, - 1 ) print ( L ) # # triu(M,k) creates an upper triangular matrix # print ( '' ) print ( ' U = np.triu(M,k) creates upper triangular matrix' ) print ( ' starting at diagonal k' ) U = np.triu ( M, 0 ) print ( U ) # # Spy displays a plot of 0 / nonzero entries. # print ( '' ) print ( ' np.spy(A) displays 0/nonzero entries' ) import matplotlib.pyplot as plt from dif2 import dif2 A10 = dif2 ( 10 ) plt.spy ( A10, marker = 'o' ) plt.grid ( True ) plt.savefig ( 'dif2_spy.png' ) plt.show ( ) # # Row sums: # print ( '' ) print ( ' Row sums is np.sum(M) or np.sum(M,axis=1)' ) print ( np.sum(M) ) print ( np.sum(M,axis=1) ) # # Column sums: # print ( '' ) print ( ' Column sums is np.sum(M,axis=0)' ) print ( np.sum(M,axis=0) ) # # Matrix sum # print ( '' ) print ( ' Matrix sum np.sum(np.sum(M))' ) print ( np.sum(np.sum(M)) ) # # Matrix norms. # A_norm = np.linalg.norm ( A ) A_norm_one = np.linalg.norm ( A, 1 ) # L1 norm: maximum column sum of |A| A_norm_two = np.linalg.norm ( A, 2 ) # L2 norm: maximum eigenvalue of A'A A_norm_inf = np.linalg.norm ( A, np.inf ) # Linfinity norm: maximum row sum of |A| A_norm_fro = np.linalg.norm ( A, 'fro' ) # Frobenius: sqrt of sum of squares of entries print ( '' ) print ( ' Matrix norms (2 is default, fro is frobenius:' ) print ( '' ) print ( ' np.linalg.norm(A) = ', A_norm ) print ( ' np.linalg.norm(A,1) = ', A_norm_one ) print ( ' np.linalg.norm(A,2) = ', A_norm_two ) print ( ' np.linalg.norm(A,np.inf) = ', A_norm_inf ) print ( ' np.linalg.norm(A,\'fro\') = ', A_norm_fro ) # # Row 2, Column 1 # print ( '' ) print ( ' Row 2, Column 1: M[2,1]' ) print ( M[2,1] ) # # Row 2 # print ( '' ) print ( ' Row 2: M[2,:]' ) print ( M[2,:] ) # # Column 1 # print ( '' ) print ( ' Column 1: M[:,1]' ) print ( M[:,1] ) return if ( __name__ == "__main__" ): about_a_matrix ( )