def test_adding_rows(self): columns = 2 rows = 3 matrix = Matrix(rows, columns) self.assertRaises(ValueError, matrix.setRow, 1, [1, 2, 3]) matrix.setRow(2, [2, 5]) self.assertEqual(matrix.getRow(1), [0, 0]) self.assertEqual(matrix.getRow(2), [2, 5])
def testLowerDecomposition(self): columns = 3 rows = 3 matrix = Matrix(rows, columns) matrix.setRow(1, [1, 2, 3]) matrix.setRow(2, [4, 5, 6]) matrix.setRow(3, [7, 8, 9]) lu_matrix_list = matrix.get_lu_decomposition() new_matrix = lu_matrix_list[0] * lu_matrix_list[1] self.assertEqual(matrix.matrix, new_matrix.matrix)
def test_multiplying_matrixes(self): matrix1 = Matrix(2, 3) matrix1.setRow(1, [3, 2, 1]) matrix1.setRow(2, [1, 0, 2]) matrix2 = Matrix(3, 2) matrix2.setColumn(1, [1, 0, 4]) matrix2.setColumn(2, [2, 1, 0]) matrixWithWrongDimension = Matrix(5, 6) with self.assertRaises(TypeError): matrix2 * matrixWithWrongDimension correctMultipliedMatrix = Matrix(2, 2) correctMultipliedMatrix.setRow(1, [7, 8]) correctMultipliedMatrix.setRow(2, [9, 2]) multipliedMatrix = matrix1 * matrix2 self.assertEqual(multipliedMatrix.matrix, correctMultipliedMatrix.matrix)
def test_multiplying_matrixes_with_floats(self): matrix1 = Matrix(2, 3) matrix1.setRow(1, [3.5, 2, 1]) matrix1.setRow(2, [1, 0, 2.5]) matrix2 = Matrix(3, 2) matrix2.setColumn(1, [0.5, 0, 4]) matrix2.setColumn(2, [2, 1.375, 0]) correctMultipliedMatrix = Matrix(2, 2) correctMultipliedMatrix.setRow(1, [5.75, 9.75]) correctMultipliedMatrix.setRow(2, [10.5, 2]) multipliedMatrix = matrix1 * matrix2 self.assertEqual(multipliedMatrix.matrix, correctMultipliedMatrix.matrix)