def test_divide(self): self.assertEqual( matrix.MatrixType([[16, -4], [12, 8]]).divide(4), matrix.MatrixType([[4, -1], [3, 2]])) self.assertEqual( matrix.MatrixType([[12], [-7], [-10]]).divide( matrix.MatrixType([[4, 2, -1], [3, -2, 2], [1, -3, 1]])), matrix.MatrixType([[1], [3], [-2]]))
def test_multiply(self): self.assertEqual( matrix.MatrixType([[4, -1], [3, 2]]).multiply(4), matrix.MatrixType([[16, -4], [12, 8]])) self.assertEqual( matrix.MatrixType([[2, 1], [0, -1]]).multiply( matrix.MatrixType([[-3, 4], [2, -2]])), matrix.MatrixType([[-4, 6], [-2, 2]]))
def test_union(self): self.assertEqual( matrix.MatrixType([[1, 1, 1], [1, 2, 3], [1, 3, 6]]).union(), matrix.MatrixType([[3, -3, 1], [-3, 5, -2], [1, -2, 1]]))
def test_eq(self): self.assertTrue( matrix.MatrixType([[4, 2, -1], [3, -2, 2], [1, -3, 1]]) == matrix.MatrixType([[4, 2, -1], [3, -2, 2], [1, -3, 1]]))
def test_transposed(self): self.assertEqual( matrix.MatrixType([[-1, 3, 1, 0], [5, 7, -2, 4]]).transposed(), matrix.MatrixType([[-1, 5], [3, 7], [1, -2], [0, 4]]))
def test_determinant(self): self.assertEqual( matrix.MatrixType([[-5, 4, 1], [1, -2, 3], [0, 6, 2]]).determinant(), 108)
def test_determinant(self): with self.assertRaises(Exception) as context: matrix.MatrixType([[3, 2, -4], [2, -7, 0], [4, -1, 4], [3, 2, -4], [0, 0, 1]]).determinant() self.assertEqual(str(context.exception), 'Нельзя вычислить определитель неквадратной матрицы')
def test_subtract(self): self.assertEqual( matrix.MatrixType([[4, -1], [3, 2]]).subtract( matrix.MatrixType([[1, -5], [0, -1]])), matrix.MatrixType([[3, 4], [3, 3]]))
def test_withoutRowAndColumn(self): self.assertEqual( matrix.MatrixType([[3, 2, -4], [2, -7, 0], [4, -1, 4], [3, 2, -4], [0, 0, 1]]).withoutRowAndColumn(2, 3), matrix.MatrixType([[3, 2], [4, -1], [3, 2], [0, 0]]))
def test_width(self): self.assertEqual( matrix.MatrixType([[3, 2, -4], [2, -7, 0], [4, -1, 4], [3, 2, -4], [0, 0, 1]]).width(), 3)
def test_height(self): self.assertEqual( matrix.MatrixType([[3, 2, -4], [2, -7, 0], [4, -1, 4], [3, 2, -4], [0, 0, 1]]).height(), 5)
def test_withoutRowAndColumn(self): with self.assertRaises(Exception) as context: matrix.MatrixType([[3, 2, -4], [2, -7, 0], [4, -1, 4], [3, 2, -4], [0, 0, 1]]).withoutRowAndColumn(2, 4) self.assertEqual(str(context.exception), 'Координаты вне диапазона матрицы')
def test_divide(self): with self.assertRaises(Exception) as context: matrix.MatrixType([[16, -4], [12, 8]]).divide(0) self.assertEqual(str(context.exception), 'division by zero')
def test_multiply(self): with self.assertRaises(Exception) as context: matrix.MatrixType([[2, 1, 3], [0, -1, 8]]).multiply(matrix.MatrixType([[-3, 4], [2, -2]])) self.assertEqual(str(context.exception), 'Ширина первого множителя должна быть равна высоте второго множителя')
def test_subtract(self): with self.assertRaises(Exception) as context: matrix.MatrixType([[4, -1], [3, 2]]).subtract(matrix.MatrixType([[-1, 5], [0, 1], [6, 7]])) self.assertEqual(str(context.exception), 'Матрицы должны быть одного размера')
def test_negate(self): self.assertEqual( matrix.MatrixType([[-5, 4, 1], [1, -2, 3], [0, 6, 2]]).negate(), matrix.MatrixType([[5, -4, -1], [-1, 2, -3], [0, -6, -2]]))
def test_add(self): self.assertEqual( matrix.MatrixType([[4, -1], [3, 2]]).add(matrix.MatrixType([[-1, 5], [0, 1]])), matrix.MatrixType([[3, 4], [3, 3]]))
def test_elementAt(self): self.assertEqual( matrix.MatrixType([[3, 2, -4], [2, -7, 0], [4, -1, 4], [3, 2, -4], [0, 0, 1]]).elementAt(3, 2), -1)
def test_inverse(self): self.assertEqual( matrix.MatrixType([[1, 1, 1], [1, 2, 3], [1, 3, 6]]).inverse(), matrix.MatrixType([[3, -3, 1], [-3, 5, -2], [1, -2, 1]]))
def test_minor(self): self.assertEqual( matrix.MatrixType([[1, 3, -2], [4, 1, 0], [2, -1, 4]]).minor(3, 2), 8)
def test_algebraicComplement(self): self.assertEqual( matrix.MatrixType([[-5, 4, 1], [1, -2, 3], [0, 6, 2]]).algebraicComplement(2, 3), 30)
for i in range(n): t[i] = t[i].copy() arr = input().split(' ') for j in range(n): t[i][j] = int(arr[j]) am = n * [n * [0]] bm = n * [[0]] for i in range(n): am[i] = am[i].copy() bm[i] = bm[i].copy() for j in range(n): am[i][j] = 0 if i == 0: bm[i][0] = 1 am[i][j] = 1 elif i == j: bm[i][0] = 0 for k in range(n): if t[i][k] > 0: am[i][j] = am[i][j] + t[i][k] else: bm[i][0] = 0 if t[j][i] > 0: am[i][j] = -t[j][i] a = matrix.MatrixType(am) b = matrix.MatrixType(bm) c = b.divide(a) print("Вероятности состояний: ") for i in range(1, n + 1): print("p" + str(i) + "=" + str(c.elementAt(i, 1)))