def test_matrix():
    a = Matrix([[1, 2, 0],[0, 1, 3]])
    b = Matrix([[1, 1, 1], [1, 0, 0], [2, 2, 1]])

    assert a.num_columns == 3
    assert b.num_columns == 3
    assert a.num_rows == 2
    assert b.num_rows == 3
    assert a.order == (2, 3)
    assert b.order == (3, 3)
    assert a.is_square == False
    assert b.is_square == True
    assert a.columns == [[1, 0], [2, 1], [0, 3]]
    assert b.columns == [[1, 1, 2], [1, 0, 2], [1, 0, 1]]
    assert a.identity_columns() == Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
    assert b.identity_columns() == Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
    assert a.identity_rows() == Matrix([[1, 0], [0, 1]])
    a.transpose()
    assert a == Matrix([[1, 0], [2, 1], [0, 3]])
    a.transpose()

    assert a * b == Matrix([[3, 1, 1], [7, 6, 3]])
    assert a * 2 == Matrix([[2, 4, 0], [0, 2, 6]])

    assert (a==b) == False
    assert a != b
    assert - a == Matrix([[-1, -2, 0], [0, -1, -3]])

    c = Matrix([[1, 2, 0],[0, 1, 3]])

    assert a + c == Matrix([[2, 4, 0], [0, 2, 6]])
    assert a - c == Matrix([[0, 0, 0], [0, 0, 0]])

    c.add_row([0, 1, 1])
    assert c.rows[-1] == [0, 1, 1]
    c.add_column([0, 1, 2])
    assert c.columns[-1] == [0, 1, 2]

    c.add_row([0, 0, 2, 6], 1)
    c.sort_by_zero()
    assert c == Matrix([[1, 2, 0, 0], [0, 1, 3, 1], [0, 1, 1, 2], [0, 0, 2, 6]])

    c.to_one_in_first_no_zero(3)
    assert c.rows[3] == [0, 0, 1, 3]

    log.info(b.__str__())
    b.elementary_transformation(0, 2, 0)
    log.info(b.__str__())
    assert b.rows[2] == [0, 0, -1]

    d = Matrix([[0, 0, 1, 0], [0, 0, 2, 0]], mod=5)
    d.elementary_transformation(0, 1, 2)
    assert d.rows[1] == [0, 0, 0, 0]
Esempio n. 2
0
class test_matrix(unittest.TestCase):
    def setUp(self):
        self.matrix = Matrix(1, 2, 3, 4)
        self.matrix2 = Matrix(1, 2, 3, 4, 5, 6, 7, 8, 9)
        self.matrix3 = Matrix(3, 4, 5, 6)
        self.matrix0 = Matrix()

    def test_init(self):
        self.assertEqual((self.matrix0).__str__(), 'Matrix 0x0:\n\n')

    def test_add_matrixes(self):
        self.assertRaises(WrongMatrix, Matrix.__add__, self.matrix,
                          self.matrix2)

    def test_add_matrixes2(self):
        self.assertEqual(
            Matrix(5, 5, 5, 5).__str__(),
            (self.matrix + Matrix(4, 3, 2, 1)).__str__())

    def test_radd_number(self):
        self.assertEqual(
            Matrix(2, 3, 4, 5).__str__(), (self.matrix + 1).__str__())

    def test_rsub(self):
        self.assertNotEqual(Matrix(0, 1, 2, 10), self.matrix - 1)

    def test_rmul_(self):
        self.assertEqual(
            Matrix(9, 9, 21, 21).__str__(), (self.matrix * 3).__str__())

    def test_sub(self):
        self.assertEqual(
            Matrix(0, 1, 2, 3).__str__(), (1 - self.matrix).__str__())
        self.assertRaisesRegex(WrongMatrix,
                               "Wrong matrices. Must be the same size.",
                               Matrix.__sub__, self.matrix, self.matrix2)

    def test_mul_matrixes(self):
        self.assertEqual(
            Matrix(517, 65, 1467, 153).__str__(),
            (self.matrix * Matrix(433, 23, 42, 21)).__str__())

    def test_zero_matrix(self):
        self.assertIn('0', (self.matrix * 0).__str__())

    def test_str(self):
        self.assertIsInstance(self.matrix.__str__(), str)
Esempio n. 3
0
class TestMatrix(unittest.TestCase):

    # ToDo: Implement method
    def setUp(self):
        self.matrix_a = Matrix(dims=(3, 2))
        self.fill_b_value = 5
        self.matrix_b = Matrix(dims=(2, 3), fill=self.fill_b_value)
        self.matrix_c = Matrix(dims=(3, 2), fill="random")
        self.fill_d_value = -4
        self.matrix_d = Matrix(dims=(2, 3), fill=self.fill_d_value)
        self.fill_e_value = 3
        self.matrix_e = Matrix(dims=(2, 3), fill=self.fill_e_value)
        self.matrix_f = Matrix(dims=(2, 3), fill=self.fill_e_value)
        self.scalar_a = 11
        self.scalar_b = -5
        self.fill_g_value = -3.3
        self.matrix_g = Matrix(dims=(3, 2), fill=self.fill_g_value)
        pass

    # ToDo:
    def tearDown(self):
        pass

    def test_constructor_fill_constant_number(self):
        self.assertEqual(self.matrix_a.rows, 3)
        self.assertEqual(self.matrix_a.cols, 2)

        self.assertAlmostEqual(self.matrix_a[0, 0], 0)
        self.assertAlmostEqual(self.matrix_a[0, 1], 0)

        self.assertAlmostEqual(self.matrix_b[0, 0], 5)
        self.assertAlmostEqual(self.matrix_b[1, 1], 5)

    def test_constructor_fill_random_numbers(self):
        self.assertEqual(self.matrix_c.rows, 3)
        self.assertEqual(self.matrix_c.cols, 2)

        self.assertGreaterEqual(self.matrix_c[0, 0], -0.5)
        self.assertLessEqual(self.matrix_c[0, 0], +0.5)
        self.assertNotEqual(self.matrix_c[0, 0], self.matrix_c[1, 1])

        self.assertGreaterEqual(self.matrix_c[0, 1], -0.5)
        self.assertLessEqual(self.matrix_c[0, 1], +0.5)

        self.assertGreaterEqual(self.matrix_c[2, 1], -0.5)
        self.assertLessEqual(self.matrix_c[2, 1], +0.5)

    def test_add_scalar(self):
        addition_matrix = self.matrix_b + self.scalar_a
        expected_result_value = self.fill_b_value + self.scalar_a

        self.assertEqual(addition_matrix[0, 0], expected_result_value)
        self.assertEqual(addition_matrix[0, 1], expected_result_value)
        self.assertEqual(addition_matrix[0, 2], expected_result_value)
        self.assertEqual(addition_matrix[1, 0], expected_result_value)
        self.assertEqual(addition_matrix[1, 1], expected_result_value)
        self.assertEqual(addition_matrix[1, 2], expected_result_value)

        addition_matrix = self.matrix_b + self.scalar_b
        expected_result_value = self.fill_b_value + self.scalar_b

        self.assertEqual(addition_matrix[0, 0], expected_result_value)
        self.assertEqual(addition_matrix[0, 1], expected_result_value)
        self.assertEqual(addition_matrix[0, 2], expected_result_value)
        self.assertEqual(addition_matrix[1, 0], expected_result_value)
        self.assertEqual(addition_matrix[1, 1], expected_result_value)
        self.assertEqual(addition_matrix[1, 2], expected_result_value)

    def test_add_matrix(self):
        addition_matrix = self.matrix_b + self.matrix_d
        expected_result_value = self.fill_b_value + self.fill_d_value

        self.assertEqual(addition_matrix[0, 0], expected_result_value)
        self.assertEqual(addition_matrix[1, 1], expected_result_value)

        addition_matrix = self.matrix_b + self.matrix_e
        expected_result_value = self.fill_b_value + self.fill_e_value

        self.assertEqual(addition_matrix[0, 0], expected_result_value)
        self.assertEqual(addition_matrix[1, 1], expected_result_value)

    def test_radd_scalar(self):
        addition_matrix = self.scalar_a + self.matrix_b
        expected_result_value = self.fill_b_value + self.scalar_a

        self.assertEqual(addition_matrix[0, 0], expected_result_value)
        self.assertEqual(addition_matrix[0, 1], expected_result_value)
        self.assertEqual(addition_matrix[0, 2], expected_result_value)
        self.assertEqual(addition_matrix[1, 0], expected_result_value)
        self.assertEqual(addition_matrix[1, 1], expected_result_value)
        self.assertEqual(addition_matrix[1, 2], expected_result_value)

        addition_matrix = self.scalar_b + self.matrix_b
        expected_result_value = self.fill_b_value + self.scalar_b

        self.assertEqual(addition_matrix[0, 0], expected_result_value)
        self.assertEqual(addition_matrix[0, 1], expected_result_value)
        self.assertEqual(addition_matrix[0, 2], expected_result_value)
        self.assertEqual(addition_matrix[1, 0], expected_result_value)
        self.assertEqual(addition_matrix[1, 1], expected_result_value)
        self.assertEqual(addition_matrix[1, 2], expected_result_value)

    def test_equal(self):
        # wrong size
        self.assertFalse(self.matrix_a == self.matrix_b)
        # wrong values
        self.assertFalse(self.matrix_b == self.matrix_d)
        # wrong data type
        self.assertFalse(self.matrix_a == self.scalar_b)
        # correct test
        self.assertTrue(self.matrix_e == self.matrix_f)

    def test_str(self):
        expected_string = "[   5.000   5.000   5.000 ]\n"
        expected_string += "[   5.000   5.000   5.000 ]\n"

        self.assertEqual(self.matrix_b.__str__(), expected_string)

    def test_get_item(self):
        self.assertEqual(self.matrix_b[0, 0], self.fill_b_value)
        self.assertEqual(self.matrix_d[1, 1], self.fill_d_value)
        self.assertEqual(self.matrix_e[1, 2], self.fill_e_value)

    def test_set_item(self):
        val = -14.4
        self.matrix_a[0, 0] = val
        self.matrix_a[1, 1] = val
        self.assertEqual(self.matrix_a[0, 0], val)
        self.assertEqual(self.matrix_a[1, 1], val)

    def test_sub_scalar(self):
        result_matrix = self.matrix_b - self.scalar_a
        result_value = self.fill_b_value - self.scalar_a

        # is matrix_b unchanged?
        self.assertEqual(self.matrix_b[0, 0], self.fill_b_value)
        self.assertEqual(self.matrix_b[1, 1], self.fill_b_value)

        # is result matrix correct?
        self.assertEqual(result_matrix[0, 0], result_value)
        self.assertEqual(result_matrix[1, 1], result_value)

    def test_sub_matrix(self):
        result_matrix = self.matrix_b - self.matrix_d
        result_value = self.fill_b_value - self.fill_d_value

        # are both matrices unchanged?
        self.assertEqual(self.matrix_b[0, 0], self.fill_b_value)
        self.assertEqual(self.matrix_b[1, 1], self.fill_b_value)
        self.assertEqual(self.matrix_d[0, 0], self.fill_d_value)
        self.assertEqual(self.matrix_d[1, 1], self.fill_d_value)

        # is result matrix correct?
        self.assertEqual(result_matrix[0, 0], result_value)
        self.assertEqual(result_matrix[1, 2], result_value)

    def test_rsub_scalar(self):
        result_matrix = self.scalar_a - self.matrix_b
        result_value = self.scalar_a - self.fill_b_value

        # is matrix_b unchanged?
        self.assertEqual(self.matrix_b[0, 0], self.fill_b_value)
        self.assertEqual(self.matrix_b[1, 1], self.fill_b_value)

        self.assertEqual(self.matrix_b[0, 0], self.fill_b_value)
        self.assertEqual(result_matrix[0, 0], result_value)
        self.assertEqual(result_matrix[1, 2], result_value)

    def test_mul_scalar(self):
        actual_result_matrix = self.matrix_b * self.scalar_a
        expected_result_value = self.fill_b_value * self.scalar_a

        # check values
        self.assertEqual(actual_result_matrix[0, 0], expected_result_value)
        self.assertEqual(actual_result_matrix[1, 1], expected_result_value)

    # tests matrix multiplication, too.
    def test_mul_matrix(self):
        actual_result_matrix = self.matrix_b * self.matrix_g
        expected_result_value = self.fill_b_value * self.fill_g_value * 3

        # check size of matrix
        self.assertEqual(actual_result_matrix.cols, self.matrix_g.cols)
        self.assertEqual(actual_result_matrix.rows, self.matrix_b.rows)

        # check values
        self.assertEqual(actual_result_matrix[0, 0], expected_result_value)
        self.assertAlmostEqual(actual_result_matrix[1, 1], expected_result_value)

    def test_rmul_scalar(self):
        actual_result_matrix = self.scalar_a * self.matrix_b
        expected_result_value = self.scalar_a * self.fill_b_value

        # check values
        self.assertEqual(actual_result_matrix[0, 0], expected_result_value)
        self.assertEqual(actual_result_matrix[1, 1], expected_result_value)

    def test_transpose(self):
        actual_result_matrix = self.matrix_c.transpose()
        expected_result_matrix_cols = self.matrix_c.rows
        expected_result_matrix_rows = self.matrix_c.cols

        # check right size of result matrix
        self.assertEqual(actual_result_matrix.rows, expected_result_matrix_rows)
        self.assertEqual(actual_result_matrix.cols, expected_result_matrix_cols)

        # check right values of result matrix
        for i in range(actual_result_matrix.rows):
            for j in range(actual_result_matrix.cols):
                self.assertEqual(actual_result_matrix[i, j], self.matrix_c[j, i])

    def test_static_of(self):
        input_list = [12.12, 11.1, -0.3]
        actual_result_matrix = Matrix.of(input_list)

        # check correct size
        self.assertEqual(actual_result_matrix.rows, 1)
        self.assertEqual(actual_result_matrix.cols, len(input_list))

        # check correct values
        for i in range(len(input_list)):
            self.assertEqual(actual_result_matrix[0, i], input_list[i])