#!/usr/bin/env python # coding: utf-8 # In[16]: # D. Scott # Linear Algebra: TCI # Code Challenge 8.1 # Find null space vector and test commutativity import random import numpy as np from scipy.linalg import null_space # generate and populate random matrices with same inner value num1 = [] num2 = [] for i in range(0,12): num1.append(random.randint(-6,12)) for i in range(0,12): num2.append(random.randint(-6,12)) A1 = np.array(num1).reshape(4,3) A2 = np.array(num2).reshape(3,4) A = A1@A2 num1 = [] num2 = [] for i in range(0,12): num1.append(random.randint(-6,12)) for i in range(0,12): num2.append(random.randint(-6,12)) B1 = np.array(num1).reshape(4,3) B2 = np.array(num2).reshape(3,4) B = B1@B2 print("A:\n",A,"\nRank(A): ",np.linalg.matrix_rank(A), "\n\nB:\n",B,"\nRank(B):",np.linalg.matrix_rank(B)) n = null_space(A) print("\nBasis for N(A) [n]:\n",n) print("\nB@A@n (zeros) = \n",B@A@n) print("\nA@B@n = \n",A@B@n) print("\nMatrix multiplication not commutative.")