from Matrix2D import * # The 2x2 identity matrix I = Matrix2D.identity(2,2) # Construct a 3x5 matrix M = Matrix2D.create(3,5) # Multiply A times B A = Matrix2D.init_list(2, [1,2, 3,4]) B = Matrix2D.init_list(2, [4,3, 2,1]) C = A * B # equivalent to: C = A.multiply(B) assert C.getmatrix() == [[8,5], [20, 13]] #C.output() # division automatically tries to get the inverse D = Matrix2D.init_list(2, [2,0, 4,2]) assert (D/D).getmatrix() == I.getmatrix() # Multiply by scalar 2, using apply_lambda() f = lambda x: x*2 A.apply_lambda(f) assert A.getmatrix() == [[2,4], [6,8]] # Round everything to 2 decimal places f = lambda x: round(x, 2) A.apply_lambda(f) # Blank out the diagonal with 0's # (if f returns None, the value is left unchanged) def f(row,col, value): if (row==col): return 0 A.apply_func(f) assert A.getmatrix() == [[0,4], [6,0]] #A.output() # Verify that A / Ainverse = I A = Matrix2D.init_list(4, [3,2,0,0, 4,3,0,0, 0,0,6,5, 0,0,7,6]) # A1 is the inverse of A A1 = Matrix2D.init_list(4, [3,-2,0,0, -4,3,0,0, 0,0,6,-5, 0,0,-7,6]) I = A * A1 assert I.is_identity() I = A / A assert I.is_identity() I = A * A.inverse() assert I.is_identity()