def test_multplication(self): # Test dot products # Row vectors v1 = Matrix([1, 2]) v2 = Matrix([5], [6]) self.assertEqual(v1 * v2, 17) self.assertEqual(v1 * [5, 6], 17) # Column vectors v1 = Matrix([10], [7]) v2 = Matrix([5 ], [8]) self.assertEqual(v1 * v2.transposed(), Matrix([50, 80], [35, 56])) # Test matrix scalar multiplication m = Matrix([1, 4, 5, 6], [3, 8, 9, 2], [9, 12, 4, 13]) self.assertEqual((m * 4).data, [[(m[j][i] * 4) for i in range(m.cols)] for j in range(m.rows)]) # Test matrix multiplication m1 = Matrix([2, 3], [4, 5], [6, 7]) m2 = Matrix([1, 2, 3], [4, 5, 6]) r = Matrix([14, 19, 24], [24, 33, 42], [34, 47, 60]) self.assertEqual(m1 * m2, r) self.assertEqual(m1 * [[1, 2, 3], [4, 5, 6]], r) # Test swapping rows of a matrix with a permutation matrix m = Matrix([11, 9 , 24, 2], [1 , 5 , 2 , 6], [3 , 17, 18, 1], [2 , 5 , 7 , 1]) p = Matrix([1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 1, 0, 0]) self.assertEqual(p * m, Matrix( [11, 9 , 24, 2], [3 , 17, 18, 1], [2 , 5 , 7 , 1], [1 , 5 , 2 , 6])) # Test swapping of cols of a matrix with the same permutation matrix self.assertEqual(m * p, Matrix( [11, 2, 9 , 24 ], [1 , 6, 5 , 2 ], [3 , 1, 17, 18 ], [2 , 1, 5 , 7 ])) # Multiplication of matrix with a matrix of 3 columns and list of 3 components self.assertEqual( Matrix([24 , 1, 8 ], [6 , 0, 2 ], [-12, 1, -3]) * Matrix([1], [9], [-2]), Matrix([17], [2], [3])) self.assertEqual( Matrix([24 , 1, 8 ], [6 , 0, 2 ], [-12, 1, -3]) * [1, 9, -2], Matrix([17], [2], [3]))
def test_transpose(self): m = Matrix([1, 2, 3], [4, 5, 6]) r = Matrix([1, 4], [2, 5], [3, 6]) self.assertEqual(m.transposed(), r)