Esempio n. 1
0
def test_rref_1():
    p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
    p2 = Plane(normal_vector=Vector(['0', '1', '1']), constant_term='2')
    s = LinearSystem([p1, p2])
    r = s.compute_rref()
    assert (r[0] == Plane(normal_vector=Vector(['1', '0', '0']),
                          constant_term='-1') and r[1] == p2)
Esempio n. 2
0
def test_triangular_1_1():
    p1 = Plane(normal_vector=Vector(['0', '0', '3']), constant_term='3')
    p2 = Plane(normal_vector=Vector(['0', '2', '2']), constant_term='2')
    p3 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
    s = LinearSystem([p1, p2, p3])
    t = s.compute_triangular_form()
    assert (t[0] == p3 and t[1] == p2 and t[2] == p1)
Esempio n. 3
0
def test_case_1():
    p0 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
    p1 = Plane(normal_vector=Vector(['0', '1', '0']), constant_term='2')
    p2 = Plane(normal_vector=Vector(['1', '1', '-1']), constant_term='3')
    p3 = Plane(normal_vector=Vector(['1', '0', '-2']), constant_term='2')
    s = LinearSystem([p0, p1, p2, p3])
    s.swap_rows(0, 1)
    assert (s[0] == p1 and s[1] == p0 and s[2] == p2 and s[3] == p3)
Esempio n. 4
0
def test_infinite_solutions():
    """Test the system has infinite solutions"""
    plane_4 = Plane(Vector([1, 1, 1]), 3)
    plane_5 = Plane(Vector([2, 4, 1]), 8)
    plane_6 = Plane(Vector([6, 10, 4]), 22)

    lin_sys_2 = LinearSystem([plane_4, plane_5, plane_6])
    solutions_2 = lin_sys_2.system_solutions()
    assert solutions_2 == 'system has infinite solutions'
Esempio n. 5
0
def test_single_solution():
    """Test the system has a single solution"""
    plane_7 = Plane(Vector([1, 1, 1]), 1)
    plane_8 = Plane(Vector([0, 1, 0]), 2)
    plane_9 = Plane(Vector([1, 1, -1]), 3)
    plane_10 = Plane(Vector([1, 0, -2]), 2)

    lin_sys_3 = LinearSystem([plane_7, plane_8, plane_9, plane_10])
    solutions_3 = lin_sys_3.system_solutions()
    assert solutions_3 == 'solution is: a = 0.000, b = 2.000, c = -1.000'
Esempio n. 6
0
def test_triangular_3():
    p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
    p2 = Plane(normal_vector=Vector(['0', '1', '0']), constant_term='2')
    p3 = Plane(normal_vector=Vector(['1', '1', '-1']), constant_term='3')
    p4 = Plane(normal_vector=Vector(['1', '0', '-2']), constant_term='2')
    s = LinearSystem([p1, p2, p3, p4])
    t = s.compute_triangular_form()
    assert (t[0] == p1 and t[1] == p2
            and t[2] == Plane(normal_vector=Vector(['0', '0', '-2']),
                              constant_term='2') and t[3] == Plane())
Esempio n. 7
0
def test_no_consistent_solutions():
    """Test the system has no solutions"""

    plane_1 = Plane(Vector([1, 1, -1]), 2)
    plane_2 = Plane(Vector([2, 3, -1]), 0)
    plane_3 = Plane(Vector([3, 4, -2]), 1)

    lin_sys_1 = LinearSystem([plane_1, plane_2, plane_3])
    solutions_1 = lin_sys_1.system_solutions()
    assert solutions_1 == 'system has no consistent solutions'
Esempio n. 8
0
def test_ge_solution():
    """Quiz 12 Coding Gaussian Elimination"""
    plane_1 = Plane(Vector([5.862, 1.178, -10.366]), -8.15)
    plane_2 = Plane(Vector([-2.931, -0.589, 5.183]), -4.075)

    lin_sys_1 = LinearSystem([plane_1, plane_2])
    solutions_1 = lin_sys_1.system_solutions()
    assert solutions_1 == 'system has no consistent solutions'

    plane_3 = Plane(Vector([8.631, 5.112, -1.816]), 5.113)
    plane_4 = Plane(Vector([4.315, 11.132, 5.27]), 6.775)
    plane_5 = Plane(Vector([-2.158, 3.01, -1.727]), -0.831)

    lin_sys_2 = LinearSystem([plane_3, plane_4, plane_5])
    solutions_2 = lin_sys_2.system_solutions()
    assert solutions_2 == 'system has infinite solutions'

    plane_6 = Plane(Vector([5.262, 2.739, -9.878]), -3.441)
    plane_7 = Plane(Vector([5.111, 6.358, 7.638]), -2.152)
    plane_8 = Plane(Vector([2.016, -9.924, -1.367]), -9.278)
    plane_9 = Plane(Vector([2.167, -13.543, -18.883]), -10.567)

    lin_sys_3 = LinearSystem([plane_6, plane_7, plane_8, plane_9])
    solutions_3 = lin_sys_3.system_solutions()
    assert solutions_3 == 'solution is: a = -1.177, b = 0.707, c = -0.083'
Esempio n. 9
0
def test_rref_3():
    p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
    p2 = Plane(normal_vector=Vector(['0', '1', '0']), constant_term='2')
    p3 = Plane(normal_vector=Vector(['1', '1', '-1']), constant_term='3')
    p4 = Plane(normal_vector=Vector(['1', '0', '-2']), constant_term='2')
    s = LinearSystem([p1, p2, p3, p4])
    r = s.compute_rref()
    assert (r[0] == Plane(normal_vector=Vector(['1', '0', '0']),
                          constant_term='0') and r[1] == p2
            and r[2] == Plane(normal_vector=Vector(['0', '0', '-2']),
                              constant_term='2') and r[3] == Plane())
Esempio n. 10
0
def main(method):
    csvm = CsvManager('input.csv')
    linear = LinearSystem(csvm.to_matrix(), 4)

    try:
        run_method = linear.method[method]
        linear.show_matrix()
        run_method()
    except KeyError:
        print 'Algoritmo inexistente. Tente essas opcoes:'
        for key in linear.method:
            print key
Esempio n. 11
0
def test_rref_4():
    p1 = Plane(normal_vector=Vector(['0', '1', '1']), constant_term='1')
    p2 = Plane(normal_vector=Vector(['1', '-1', '1']), constant_term='2')
    p3 = Plane(normal_vector=Vector(['1', '2', '-5']), constant_term='3')
    s = LinearSystem([p1, p2, p3])
    r = s.compute_rref()
    assert (r[0] == Plane(normal_vector=Vector(['1', '0', '0']),
                          constant_term=Decimal('23') / Decimal('9'))
            and r[1] == Plane(normal_vector=Vector(['0', '1', '0']),
                              constant_term=Decimal('7') / Decimal('9'))
            and r[2] == Plane(normal_vector=Vector(['0', '0', '1']),
                              constant_term=Decimal('2') / Decimal('9')))
Esempio n. 12
0
def test_rref_form():
    """Test for RREF Reduced Row Echelon Form"""

    plane_1 = Plane(Vector([0, 1, 1]), 1)
    plane_2 = Plane(Vector([1, -1, 1]), 2)
    plane_3 = Plane(Vector([1, 2, -5]), 3)

    lin_sys = LinearSystem([plane_1, plane_2, plane_3])
    rref = lin_sys.compute_rref_form()

    assert rref[0] == Plane(Vector([1, 0, 0]), Decimal(23) / Decimal(9))
    assert rref[1] == Plane(Vector([0, 1, 0]), Decimal(7) / Decimal(9))
    assert rref[2] == Plane(Vector([0, 0, 1]), Decimal(2) / Decimal(9))
Esempio n. 13
0
def test_triangular_form():
    """Test for Triangular Form"""

    plane_1 = Plane(Vector([0, 1, 1]), 1)
    plane_2 = Plane(Vector([1, -1, 1]), 2)
    plane_3 = Plane(Vector([1, 2, -5]), 3)

    lin_sys = LinearSystem([plane_1, plane_2, plane_3])
    triangular = lin_sys.compute_triangular_form()

    assert triangular[0] == Plane(Vector([1, -1, 1]), 2)
    assert triangular[1] == Plane(Vector([0, 1, 1]), 1)
    assert triangular[2] == Plane(Vector([0, 0, -9]), -2)
Esempio n. 14
0
def test_linsys_multiply_row():
    """Test Linear System Multiply Coefficient and Row"""
    plane_1 = Plane(Vector([1, 1, 1]), 1)
    plane_2 = Plane(Vector([0, 1, 0]), 2)
    plane_3 = Plane(Vector([1, 1, -1]), 3)
    plane_4 = Plane(Vector([1, 0, -2]), 2)

    # same as the end of the last test
    lin_sys = LinearSystem([plane_2, plane_1, plane_3, plane_4])

    lin_sys.multiply_coefficient_and_row(1, 0)

    assert lin_sys[0] == plane_2
    assert lin_sys[1] == plane_1
    assert lin_sys[2] == plane_3
    assert lin_sys[3] == plane_4

    lin_sys.multiply_coefficient_and_row(-1, 2)
    new_plane_3 = Plane(Vector([-1, -1, 1]), -3)

    assert lin_sys[0] == plane_2
    assert lin_sys[1] == plane_1
    assert lin_sys[2] == new_plane_3
    assert lin_sys[3] == plane_4

    lin_sys.multiply_coefficient_and_row(10, 1)

    new_plane_1 = Plane(Vector([10, 10, 10]), 10)

    assert lin_sys[0] == plane_2
    assert lin_sys[1] == new_plane_1
    assert lin_sys[2] == new_plane_3
    assert lin_sys[3] == plane_4
Esempio n. 15
0
def test_linsys_swap_row():
    """Test Linear System Swap Row"""
    plane_1 = Plane(Vector([1, 1, 1]), 1)
    plane_2 = Plane(Vector([0, 1, 0]), 2)
    plane_3 = Plane(Vector([1, 1, -1]), 3)
    plane_4 = Plane(Vector([1, 0, -2]), 2)

    lin_sys = LinearSystem([plane_1, plane_2, plane_3, plane_4])
    lin_sys.swap_rows(0, 1)
    assert lin_sys[0] == plane_2  # swapped
    assert lin_sys[1] == plane_1  # swapped
    assert lin_sys[2] == plane_3
    assert lin_sys[3] == plane_4

    lin_sys.swap_rows(1, 3)
    assert lin_sys[0] == plane_2
    assert lin_sys[1] == plane_4  # swapped
    assert lin_sys[2] == plane_3
    assert lin_sys[3] == plane_1  # swapped

    lin_sys.swap_rows(3, 1)
    assert lin_sys[0] == plane_2
    assert lin_sys[1] == plane_1  # swapped
    assert lin_sys[2] == plane_3
    assert lin_sys[3] == plane_4  # swapped
Esempio n. 16
0
    def test_compute_triangular_form(self):

        p1 = Plane(normal_vector=Vector(['0','1','1']), constant_term='1')
        p2 = Plane(normal_vector=Vector(['0','0','1']), constant_term='2')
        p3 = Plane(normal_vector=Vector(['1','1','1']), constant_term='2')
        s = LinearSystem([p1, p2, p3])
        t = s.compute_triangular_form()

        self.assertEqual(t[0], p3, str(t[0]))
        self.assertEqual(t[1], p1, str(t[1]))
        self.assertEqual(t[2], p2, str(t[2]))

        p1 = Plane(normal_vector=Vector(['1','1','1']), constant_term='1')
        p2 = Plane(normal_vector=Vector(['0','1','1']), constant_term='2')
        s = LinearSystem([p1,p2])
        t = s.compute_triangular_form()

        self.assertEqual(t[0], p1)
        self.assertEqual(t[1], p2)

        p1 = Plane(normal_vector=Vector(['1','1','1']), constant_term='1')
        p2 = Plane(normal_vector=Vector(['1','1','1']), constant_term='2')
        s = LinearSystem([p1,p2])
        t = s.compute_triangular_form()

        self.assertEqual(t[0], p1, t)
        self.assertEqual(t[1], Plane(constant_term='1'), str(t[1]))

        p1 = Plane(normal_vector=Vector(['1','1','1']), constant_term='1')
        p2 = Plane(normal_vector=Vector(['0','1','0']), constant_term='2')
        p3 = Plane(normal_vector=Vector(['1','1','-1']), constant_term='3')
        p4 = Plane(normal_vector=Vector(['1','0','-2']), constant_term='2')
        s = LinearSystem([p1,p2,p3,p4])
        t = s.compute_triangular_form()

        self.assertEqual(t[0], p1)
        self.assertEqual(t[1], p2)
        self.assertEqual(t[2], Plane(normal_vector=Vector(['0','0','-2']), constant_term='2'))
        self.assertEqual(t[3], Plane())

        p1 = Plane(normal_vector=Vector(['0','1','1']), constant_term='1')
        p2 = Plane(normal_vector=Vector(['1','-1','1']), constant_term='2')
        p3 = Plane(normal_vector=Vector(['1','2','-5']), constant_term='3')
        s = LinearSystem([p1,p2,p3])
        t = s.compute_triangular_form()

        self.assertEqual(t[0], Plane(normal_vector=Vector(['1','-1','1']), constant_term='2'))
        self.assertEqual(t[1], Plane(normal_vector=Vector(['0','1','1']), constant_term='1'))
        self.assertEqual(t[2], Plane(normal_vector=Vector(['0','0','-9']), constant_term='-2'))
Esempio n. 17
0
def test_case_6():
    p0 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
    p1 = Plane(normal_vector=Vector(['0', '1', '0']), constant_term='2')
    p2 = Plane(normal_vector=Vector(['1', '1', '-1']), constant_term='3')
    p3 = Plane(normal_vector=Vector(['1', '0', '-2']), constant_term='2')
    s = LinearSystem([p0, p1, p2, p3])
    s.swap_rows(0, 1)
    s.swap_rows(1, 3)
    s.swap_rows(3, 1)
    s.multiply_coefficient_and_row(1, 0)
    s.multiply_coefficient_and_row(-2, 2)
    s.multiply_coefficient_and_row(10, 1)
    assert (s[0] == p1 and s[1] == Plane(
        normal_vector=Vector(['10', '10', '10']), constant_term='10')
            and s[2] == Plane(normal_vector=Vector(['-1', '-1', '1']),
                              constant_term='-3') and s[3] == p3)
Esempio n. 18
0
def test_ge_1():
    p1 = Plane(normal_vector=Vector(['5.862', '1.178', '-10.366']),
               constant_term='-8.15')
    p2 = Plane(normal_vector=Vector(['-2.931', '-0.589', '5.183']),
               constant_term='-4.075')
    s = LinearSystem([p1, p2])
    try:
        ge = s.compute_gaussian_elimination()
        print('failed GE test 1')
        assert False
    except Exception as e:
        if str(e) == LinearSystem.NO_SOLUTIONS_MSG:
            pass  # print('passed GE test 1')
        else:
            print('failed GE test 1')
            assert False
Esempio n. 19
0
def test_ge_3():
    p1 = Plane(Vector([5.262, 2.739, -9.878]), -3.441)
    p2 = Plane(Vector([5.111, 6.358, 7.638]), -2.152)
    p3 = Plane(Vector([2.016, -9.924, -1.367]), -9.278)
    p4 = Plane(Vector([2.167, -13.543, -18.883]), -10.567)
    s = LinearSystem([p1, p2, p3, p4])
    try:
        ge = s.compute_gaussian_elimination()
        assert ge == Vector([
            Decimal('-1.17720187578995858313947665146'),
            Decimal('0.707150558138740933006474968216'),
            Decimal('-0.0826635849022828890650647196936')
        ])
        # print('failed GE test 3')
    except Exception as e:
        print('failed GE test 3')
        assert False
Esempio n. 20
0
    def test_multiply_coefficient_and_rows(self):

        p0 = Plane(normal_vector=Vector(['0','1','0']), constant_term='2')
        p1 = Plane(normal_vector=Vector(['1','1','1']), constant_term='1')
        p2 = Plane(normal_vector=Vector(['1','1','-1']), constant_term='3')
        p3 = Plane(normal_vector=Vector(['1','0','-2']), constant_term='2')

        s = LinearSystem([p0,p1,p2,p3])

        s.multiply_coefficient_and_row(1,0)
        self.assertEqual(s[0], p0)
        self.assertEqual(s[1], p1)
        self.assertEqual(s[2], p2)
        self.assertEqual(s[3], p3)

        s.multiply_coefficient_and_row(-1,2)
        self.assertEqual(s[0], p0)
        self.assertEqual(s[1], p1)
        self.assertEqual(s[2], Plane(normal_vector=Vector(['-1','-1','1']), constant_term='-3'))
        self.assertEqual(s[3], p3)

        s.multiply_coefficient_and_row(10,1)
        self.assertEqual(s[0], p0)
        self.assertEqual(s[1], Plane(normal_vector=Vector(['10','10','10']), constant_term='10'))
        self.assertEqual(s[2], Plane(normal_vector=Vector(['-1','-1','1']), constant_term='-3'))
        self.assertEqual(s[3], p3)
Esempio n. 21
0
    def test_add_multiple_times_to_row(self):

        p0 = Plane(normal_vector=Vector(['0','1','0']), constant_term='2')
        p1 = Plane(normal_vector=Vector(['10','10','10']), constant_term='10')
        p2 = Plane(normal_vector=Vector(['-1','-1','1']), constant_term='-3')
        p3 = Plane(normal_vector=Vector(['1','0','-2']), constant_term='2')

        s = LinearSystem([p0,p1,p2,p3])

        s.add_multiple_times_row_to_row(0,0,1)
        self.assertEqual(s[0], p0)
        self.assertEqual(s[1], p1)
        self.assertEqual(s[2], p2)
        self.assertEqual(s[3], p3)

        s.add_multiple_times_row_to_row(1,0,1)
        self.assertEqual(s[0], p0)
        self.assertEqual(s[1], Plane(normal_vector=Vector(['10','11','10']), constant_term='12'), str(s[1]))
        self.assertEqual(s[2], p2)
        self.assertEqual(s[3], p3)

        s.add_multiple_times_row_to_row(-1,1,0)
        self.assertEqual(s[0], Plane(normal_vector=Vector(['-10','-10','-10']), constant_term='-10'), str(s[0]))
        self.assertEqual(s[1], Plane(normal_vector=Vector(['10','11','10']), constant_term='12'), str(s[1]))
        self.assertEqual(s[2], p2)
        self.assertEqual(s[3], p3)
Esempio n. 22
0
    def test_add_multiple_times_to_row(self):
        p0 = Plane(Vector([0,1,0]), 2)
        p1 = Plane(Vector([10,10,10]), 10)
        p2 = Plane(Vector([-1,-1,1]), -3)
        p3 = Plane(Vector([1,0,-2]), 2)

        s = LinearSystem([p0,p1,p2,p3])

        s.add_multiple_times_row_to_row(0,0,1)
        self.assertEqual(s[0], p0)
        self.assertEqual(s[1], p1)
        self.assertEqual(s[2], p2)
        self.assertEqual(s[3], p3)

        s.add_multiple_times_row_to_row(1,0,1)
        self.assertEqual(s[0], p0)
        self.assertEqual(s[1], Plane(Vector(['10','11','10']), '12'))
        self.assertEqual(s[2], p2)
        self.assertEqual(s[3], p3)
        assertComponentsHaveSameValues(self, s[0], p0)
        assertComponentsHaveSameValues(self, s[1], Plane(Vector(['10','11','10']), '12'))
        assertComponentsHaveSameValues(self, s[2], p2)
        assertComponentsHaveSameValues(self, s[3], p3)

        s.add_multiple_times_row_to_row(-1,1,0)
        self.assertEqual(s[0], Plane(Vector(['-10','-10','-10']), '-10'))
        self.assertEqual(s[1], Plane(Vector(['10','11','10']), '12'))
        self.assertEqual(s[2], p2)
        self.assertEqual(s[3], p3)
        assertComponentsHaveSameValues(self, s[0], Plane(Vector(['-10','-10','-10']), '-10'))
        assertComponentsHaveSameValues(self, s[1], Plane(Vector(['10','11','10']), '12'))
        assertComponentsHaveSameValues(self, s[2], p2)
        assertComponentsHaveSameValues(self, s[3], p3)
Esempio n. 23
0
def test_ge_2():
    p1 = Plane(normal_vector=Vector(['8.631', '5.112', '-1.816']),
               constant_term='-5.113')
    p2 = Plane(normal_vector=Vector(['4.315', '11.132', '-5.27']),
               constant_term='-6.775')
    p3 = Plane(normal_vector=Vector(['-2.158', '3.01', '-1.727']),
               constant_term='-0.831')
    s = LinearSystem([p1, p2, p3])
    try:
        ge = s.compute_gaussian_elimination()
        print('failed GE test 2')
        assert False
    except Exception as e:
        if str(e) == LinearSystem.INF_SOLUTIONS_MSG:
            pass  # print('passed GE test 2')
        else:
            print('failed GE test 2')
            assert False
Esempio n. 24
0
def test_case_5():
    p0 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
    p1 = Plane(normal_vector=Vector(['0', '1', '0']), constant_term='2')
    p2 = Plane(normal_vector=Vector(['1', '1', '-1']), constant_term='3')
    p3 = Plane(normal_vector=Vector(['1', '0', '-2']), constant_term='2')
    s = LinearSystem([p0, p1, p2, p3])
    s.swap_rows(0, 1)
    s.swap_rows(1, 3)
    s.swap_rows(3, 1)
    s.multiply_coefficient_and_row(1, 0)
    s.multiply_coefficient_and_row(-2, 2)
    # print('After test case 5:')
    # print('   s[2]: ]', s[2])
    # print('   times -1 should be: ', Plane(normal_vector=Vector(['-2','-2','2']), constant_term='-6'))
    # print('   s[2].normal_vector[0]: ', s[2].normal_vector[0])
    # print('   s[2].basepoint: ', s[2].basepoint)
    assert (s[0] == p1 and s[1] == p0 and s[2] == Plane(
        normal_vector=Vector(['-2', '-2', '2']), constant_term='-6')
            and s[2].normal_vector[0] == -2 and s[2].normal_vector[1] == -2
            and s[2].normal_vector[2] == 2 and s[2].constant_term == -6
            and s[3] == p3)
Esempio n. 25
0
def test_rref_form():
    """Quiz 11 RREF Reduced Row Echelon Form"""

    plane_1 = Plane(Vector([1, 1, 1]), 1)
    plane_2 = Plane(Vector([0, 1, 1]), 2)
    lin_sys = LinearSystem([plane_1, plane_2])
    rref = lin_sys.compute_rref_form()

    assert rref[0] == Plane(Vector([1, 0, 0]), -1)
    assert rref[1] == plane_2

    plane_1 = Plane(Vector([1, 1, 1]), 1)
    plane_2 = Plane(Vector([1, 1, 1]), 2)
    lin_sys = LinearSystem([plane_1, plane_2])
    rref = lin_sys.compute_rref_form()

    assert rref[0] == plane_1
    assert rref[1] == Plane(constant_term=1)

    plane_1 = Plane(Vector([1, 1, 1]), 1)
    plane_2 = Plane(Vector([0, 1, 0]), 2)
    plane_3 = Plane(Vector([1, 1, -1]), 3)
    plane_4 = Plane(Vector([1, 0, -2]), 2)

    lin_sys = LinearSystem([plane_1, plane_2, plane_3, plane_4])
    rref = lin_sys.compute_rref_form()

    assert rref[0] == Plane(Vector([1, 0, 0]), 0)
    assert rref[1] == plane_2
    # assert rref[2] == Plane(Vector([0, 0, -2]), 2)
    # should be
    assert rref[2] == Plane(Vector([0, 0, 1]), -1)
    assert rref[3] == Plane()

    plane_1 = Plane(Vector([0, 1, 1]), 1)
    plane_2 = Plane(Vector([1, -1, 1]), 2)
    plane_3 = Plane(Vector([1, 2, -5]), 3)

    lin_sys = LinearSystem([plane_1, plane_2, plane_3])
    rref = lin_sys.compute_rref_form()

    assert rref[0] == Plane(Vector([1, 0, 0]), Decimal(23) / Decimal(9))
    assert rref[1] == Plane(Vector([0, 1, 0]), Decimal(7) / Decimal(9))
    assert rref[2] == Plane(Vector([0, 0, 1]), Decimal(2) / Decimal(9))
Esempio n. 26
0
def test_coding_triangular_form():
    """Quiz 10 coding Triangular Form"""

    plane_1 = Plane(Vector([1, 1, 1]), 1)
    plane_2 = Plane(Vector([0, 1, 1]), 2)
    lin_sys = LinearSystem([plane_1, plane_2])
    triangular = lin_sys.compute_triangular_form()

    assert triangular[0] == plane_1
    assert triangular[1] == plane_2

    plane_1 = Plane(Vector([1, 1, 1]), 1)
    plane_2 = Plane(Vector([1, 1, 1]), 2)
    lin_sys = LinearSystem([plane_1, plane_2])
    triangular = lin_sys.compute_triangular_form()

    assert triangular[0] == plane_1
    assert triangular[1] == Plane(constant_term=1)

    plane_1 = Plane(Vector([1, 1, 1]), 1)
    plane_2 = Plane(Vector([0, 1, 0]), 2)
    plane_3 = Plane(Vector([1, 1, -1]), 3)
    plane_4 = Plane(Vector([1, 0, -2]), 2)

    lin_sys = LinearSystem([plane_1, plane_2, plane_3, plane_4])
    triangular = lin_sys.compute_triangular_form()

    assert triangular[0] == plane_1
    assert triangular[1] == plane_2
    assert triangular[2] == Plane(Vector([0, 0, -2]), 2)
    assert triangular[3] == Plane()

    plane_1 = Plane(Vector([0, 1, 1]), 1)
    plane_2 = Plane(Vector([1, -1, 1]), 2)
    plane_3 = Plane(Vector([1, 2, -5]), 3)

    lin_sys = LinearSystem([plane_1, plane_2, plane_3])
    triangular = lin_sys.compute_triangular_form()

    assert triangular[0] == Plane(Vector([1, -1, 1]), 2)
    assert triangular[1] == Plane(Vector([0, 1, 1]), 1)
    assert triangular[2] == Plane(Vector([0, 0, -9]), -2)
Esempio n. 27
0
    def test_can_multiply_coef_and_row(self):
        # like w/ the first and third tests, this one comes directly from the
        # course. note however that as this is defined it passes with a no-op
        # implementation for multiply_coefficient_and_row, because the Plane
        # __eq__ impl considers two planes that have diff coef and constant
        # terms but that reduce to the same plane/have the same points as equal.
        # to really test this then, we'd need probably to use some different -
        # perhaps poke further into the planes and validate coefficients and
        # constant term directly - and that's what I've added below w/ the
        # comparisons of normal vectors.
        # (It would also be better if we used assertEqual for each comparisons
        # instead of the assertTrue w/ the comparisons provided, at least because
        # that'd show us the actual values when we have a failure. I've done this
        # where I've added tests.)
        p0 = Plane(Vector([0,1,0]), 2)
        p1 = Plane(Vector([1,1,1]), 1)
        p2 = Plane(Vector([1,1,-1]), 3)
        p3 = Plane(Vector([1,0,-2]), 2)

        s = LinearSystem([p0,p1,p2,p3])

        s.multiply_coefficient_and_row(1,0)
        self.assertTrue(s[0] == p0 and s[1] == p1 and
                        s[2] == p2 and s[3] == p3)

        s.multiply_coefficient_and_row(-1,2)
        self.assertTrue((s[0] == p0 and
                         s[1] == p1 and
                         s[2] == Plane(Vector([-1,-1,1]), -3) and
                         s[3] == p3))
        assertComponentsHaveSameValues(self, s[0], p0)
        assertComponentsHaveSameValues(self, s[1], p1)
        assertComponentsHaveSameValues(self, s[2], Plane(Vector([-1,-1,1]), -3))
        assertComponentsHaveSameValues(self, s[3], p3)

        s.multiply_coefficient_and_row(10,1)
        self.assertTrue((s[0] == p0 and
                         s[1] == Plane(Vector([10,10,10]), 10) and
                         s[2] == Plane(Vector([-1,-1,1]), -3) and
                         s[3] == p3))
        assertComponentsHaveSameValues(self, s[0], p0)
        assertComponentsHaveSameValues(self, s[1], Plane(Vector([10,10,10]), 10))
        assertComponentsHaveSameValues(self, s[2], Plane(Vector([-1,-1,1]), -3))
        assertComponentsHaveSameValues(self, s[3], p3)
Esempio n. 28
0
def test_linsys_basepoint():
    """Test Linear System Base Point"""

    plane_1 = Plane(Vector([1, 1, 1]), 1)
    plane_2 = Plane(Vector([0, 1, 0]), 2)
    plane_3 = Plane(Vector([1, 1, -1]), 3)
    plane_4 = Plane(Vector([1, 0, -2]), 2)

    system = LinearSystem([plane_1, plane_2, plane_3, plane_4])

    system[0] = plane_1

    vector1 = Vector([1, 2])
    constant = 2
    answer = Vector([2, 0])

    line = Line(vector1, constant)
    basepoint = line.basepoint

    assert basepoint == answer
Esempio n. 29
0
def test_linsys_multiply_row_add():
    """Test Linear System Multiply Times Row and add to Row"""
    plane_2 = Plane(Vector([0, 1, 0]), 2)
    new_plane_1 = Plane(Vector([10, 10, 10]), 10)
    new_plane_3 = Plane(Vector([-1, -1, 1]), -3)
    plane_4 = Plane(Vector([1, 0, -2]), 2)

    # same as the end of the last test
    lin_sys = LinearSystem([plane_2, new_plane_1, new_plane_3, plane_4])

    # multiply the first row by 0 and add to the second row
    # this should have no affect
    lin_sys.add_multiple_times_row_to_row(0, 0, 1)

    assert lin_sys[0] == plane_2
    assert lin_sys[1] == new_plane_1
    assert lin_sys[2] == new_plane_3
    assert lin_sys[3] == plane_4

    # multiply the first row by 1 and add it to the second row
    lin_sys.add_multiple_times_row_to_row(1, 0, 1)

    plane_1_added = Plane(Vector([10, 11, 10]), 12)

    assert lin_sys[0] == plane_2
    assert lin_sys[1] == plane_1_added
    assert lin_sys[2] == new_plane_3
    assert lin_sys[3] == plane_4

    # multiply the second row by -1 and add to the first row
    lin_sys.add_multiple_times_row_to_row(-1, 1, 0)

    plane_2_subtracted = Plane(Vector([-10, -10, -10]), -10)

    assert lin_sys[0] == plane_2_subtracted
    assert lin_sys[1] == plane_1_added
    assert lin_sys[2] == new_plane_3
    assert lin_sys[3] == plane_4
Esempio n. 30
0
    def test_can_swap_rows(self):
        p0 = Plane(Vector([1,1,1]), 1)
        p1 = Plane(Vector([0,1,0]), 2)
        p2 = Plane(Vector([1,1,-1]), 3)
        p3 = Plane(Vector([1,0,-2]), 2)

        s = LinearSystem([p0,p1,p2,p3])

        s.swap_rows(0,1)
        self.assertTrue(s[0] == p1 and s[1] == p0 and
                        s[2] == p2 and s[3] == p3)

        s.swap_rows(1,3)
        self.assertTrue(s[0] == p1 and s[1] == p3 and
                        s[2] == p2 and s[3] == p0)

        s.swap_rows(3,1)
        self.assertTrue(s[0] == p1 and s[1] == p0 and
                        s[2] == p2 and s[3] == p3)
Esempio n. 31
0
from linsys import LinearSystem
from plane import Plane
from vector import Vector


p0 = Plane(normal_vector=Vector(['1','1','1']), constant_term='1')
p1 = Plane(normal_vector=Vector(['0','1','0']), constant_term='2')
p2 = Plane(normal_vector=Vector(['1','1','-1']), constant_term='3')
p3 = Plane(normal_vector=Vector(['1','0','-2']), constant_term='2')

s = LinearSystem([p0,p1,p2,p3])
s.swap_rows(0,1)
if not (s[0] == p1 and s[1] == p0 and s[2] == p2 and s[3] == p3):
    print 'test case 1 failed'

s.swap_rows(1,3)
if not (s[0] == p1 and s[1] == p3 and s[2] == p2 and s[3] == p0):
    print 'test case 2 failed'

s.swap_rows(3,1)
if not (s[0] == p1 and s[1] == p0 and s[2] == p2 and s[3] == p3):
    print 'test case 3 failed'

s.multiply_coefficient_and_row(1,0)
print s[0].normal_vector, p1.normal_vector
if not (s[0] == p1 and s[1] == p0 and s[2] == p2 and s[3] == p3):
    print 'test case 4 failed'

s.multiply_coefficient_and_row(-1,2)
print s[2].normal_vector, s[2].constant_term
print s[2].normal_vector == Vector(['-1','-1','1'])
Esempio n. 32
0
from decimal import Decimal, getcontext
from copy import deepcopy

from vector import Vector
from plane import Plane
from linsys import LinearSystem

p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
p2 = Plane(normal_vector=Vector(['0', '1', '1']), constant_term='2')
s = LinearSystem([p1, p2])
r = s.compute_rref()
if not (r[0] == Plane(normal_vector=Vector(['1', '0', '0']),
                      constant_term='-1') and r[1] == p2):
    print 'test case 1 failed'

p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
p2 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='2')
s = LinearSystem([p1, p2])
r = s.compute_rref()
if not (r[0] == p1 and r[1] == Plane(constant_term='1')):
    print 'test case 2 failed'

p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
p2 = Plane(normal_vector=Vector(['0', '1', '0']), constant_term='2')
p3 = Plane(normal_vector=Vector(['1', '1', '-1']), constant_term='3')
p4 = Plane(normal_vector=Vector(['1', '0', '-2']), constant_term='2')
s = LinearSystem([p1, p2, p3, p4])
r = s.compute_rref()
if not (r[0] == Plane(normal_vector=Vector(['1', '0', '0']), constant_term='0')
        and r[1] == p2
        and r[2] == Plane(normal_vector=Vector(['0', '0', '-2']),
Esempio n. 33
0
from plane import Plane
from vector import Vector
from linsys import LinearSystem

p1 = Plane(normal_vector=Vector([float('1'),
                                 float('1'),
                                 float('1')]),
           constant_term=float('1'))
p2 = Plane(normal_vector=Vector([float('0'),
                                 float('1'),
                                 float('1')]),
           constant_term=float('2'))
s = LinearSystem([p1, p2])
t = s.compute_triangular_form()
if not (t[0] == p1 and t[1] == p2):
    print 'test case 1 failed'

p1 = Plane(normal_vector=Vector([float('1'),
                                 float('1'),
                                 float('1')]),
           constant_term=float('1'))
p2 = Plane(normal_vector=Vector([float('1'),
                                 float('1'),
                                 float('1')]),
           constant_term=float('2'))
s = LinearSystem([p1, p2])
t = s.compute_triangular_form()
if not (t[0] == p1 and t[1] == Plane(constant_term=float('1'))):
    print 'test case 2 failed'

p1 = Plane(normal_vector=Vector([float('1'),
Esempio n. 34
0
def test_triangular_2():
    p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
    p2 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='2')
    s = LinearSystem([p1, p2])
    t = s.compute_triangular_form()
    assert (t[0] == p1 and t[1] == Plane(constant_term='1'))
Esempio n. 35
0
from vector import Vector
from plane import Plane
from linsys import LinearSystem

p0 = Plane(normal_vector=Vector(['1','1','1']), constant_term='1')
p1 = Plane(normal_vector=Vector(['0','1','0']), constant_term='2')
p2 = Plane(normal_vector=Vector(['1','1','-1']), constant_term='3')
p3 = Plane(normal_vector=Vector(['1','0','-2']), constant_term='2')

s = LinearSystem([p0,p1,p2,p3])
s.swap_rows(0,1)
if not (s[0] == p1 and s[1] == p0 and s[2] == p2 and s[3] == p3):
    print('test case 1 failed')
else:
    print('test case 1 passed')

s.swap_rows(1,3)
if not (s[0] == p1 and s[1] == p3 and s[2] == p2 and s[3] == p0):
    print('test case 2 failed')
else:
    print('test case 2 passed')

s.swap_rows(3,1)
if not (s[0] == p1 and s[1] == p0 and s[2] == p2 and s[3] == p3):
    print('test case 3 failed')
else:
    print('test case 2 passed')

s.multiply_coefficient_and_row(1,0)
if not (s[0] == p1 and s[1] == p0 and s[2] == p2 and s[3] == p3):
    print('test case 4 failed')
Esempio n. 36
0
from plane import Plane
from vector import Vector
from linsys import LinearSystem
from decimal import Decimal

#question 1
p1 = Plane(normal_vector=Vector([float('5.862'),float('1.178'),float('-10.366')]), constant_term=float('-8.15'))
p2 = Plane(normal_vector=Vector([float('-2.931'),float('-0.589'),float('5.183')]), constant_term=float('-4.075'))
s = LinearSystem([p1,p2])
r = s.solve_system()
print r

#question 2
p1 = Plane(normal_vector=Vector([float('8.631'),float('5.112'),float('-1.816')]), constant_term=float('-5.113'))
p2 = Plane(normal_vector=Vector([float('4.315'),float('11.132'),float('-5.27')]), constant_term=float('-6.775'))
p3 = Plane(normal_vector=Vector([float('-2.158'),float('3.01'),float('-1.727')]), constant_term=float('-0.831'))
s = LinearSystem([p1,p2, p3])
r = s.solve_system()
print r

#question 3
p1 = Plane(normal_vector=Vector([float('5.262'),float('2.739'),float('-9.878')]), constant_term=float('-3.441'))
p2 = Plane(normal_vector=Vector([float('5.111'),float('6.358'),float('7.638')]), constant_term=float('-2.152'))
p3 = Plane(normal_vector=Vector([float('2.016'),float('-9.924'),float('-1.367')]), constant_term=float('-9.278'))
p4 = Plane(normal_vector=Vector([float('2.167'),float('-13.543'),float('-18.883')]), constant_term=float('-10.567'))
s = LinearSystem([p1,p2,p3,p4])
r = s.solve_system()
print r
Esempio n. 37
0
from linsys import LinearSystem
from plane import Plane
from vector import Vector

p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
p2 = Plane(normal_vector=Vector(['0', '1', '1']), constant_term='2')
s = LinearSystem([p1, p2])
t = s.compute_triangular_form
if not (t[0] == p1 and t[1] == p2):
    print 'test case 1 failed'

p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
p2 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='2')
s = LinearSystem([p1, p2])
t = s.compute_triangular_form
if not (t[0] == p1 and t[1] == Plane(constant_term='1')):
    print 'test case 2 failed'

p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
p2 = Plane(normal_vector=Vector(['0', '1', '0']), constant_term='2')
p3 = Plane(normal_vector=Vector(['1', '1', '-1']), constant_term='3')
p4 = Plane(normal_vector=Vector(['1', '0', '-2']), constant_term='2')
# skip 1
# swap #2 and #3
# add #2-#1 -> 0,0,-2=2
# swap #2 and #3 1,1,1=1;0,1,0=2;0,0,-2=2;1,0,-2=2
# swap #2 and #4
# add #2-#1 -> 0,-1,-3=1
# add #2+#4 -> 0,0,-3,3

# 0,2=2;0,3=3
from linsys import LinearSystem
from plane import Plane
from vector import Vector
from decimal import Decimal, getcontext

p1 = Plane(normal_vector=Vector(['0.786', '0.786', '0.588']),
           constant_term='-0.714')
p2 = Plane(normal_vector=Vector(['-0.138', '-0.138', '0.244']),
           constant_term='0.319')
s = LinearSystem([p1, p2])
print(s.compute_solution_with_prametrize())

p1 = Plane(normal_vector=Vector(['8.631', '5.112', '-1.816']),
           constant_term='-5.113')
p2 = Plane(normal_vector=Vector(['4.315', '11.132', '-5.27']),
           constant_term='-6.775')
p3 = Plane(normal_vector=Vector(['-2.158', '3.01', '-1.727']),
           constant_term='-0.831')
s = LinearSystem([p1, p2, p3])
print(s.compute_solution_with_prametrize())

p1 = Plane(normal_vector=Vector(['0.935', '1.76', '-9.365']),
           constant_term='-9.955')
p2 = Plane(normal_vector=Vector(['0.187', '0.352', '-1.873']),
           constant_term='-1.991')
p3 = Plane(normal_vector=Vector(['0.374', '0.704', '-3.746']),
           constant_term='-3.982')
p4 = Plane(normal_vector=Vector(['-0.561', '-1.056', '5.619']),
           constant_term='5.973')
s = LinearSystem([p1, p2, p3])
print(s.compute_solution_with_prametrize())
    def test_ge_functions(self):

        p0 = Plane(normal_vector=Vector([1, 1, 1]), constant_term=1)
        p1 = Plane(normal_vector=Vector([0, 1, 0]), constant_term=2)
        p2 = Plane(normal_vector=Vector([1, 1, -1]), constant_term=3)
        p3 = Plane(normal_vector=Vector([1, 0, -2]), constant_term=2)

        s = LinearSystem([p0, p1, p2, p3])
        s.swap_rows(0, 1)
        self.assertTrue(s[0] == p1 and s[1] == p0 and s[2] == p2
                        and s[3] == p3)

        s.swap_rows(1, 3)
        self.assertTrue(s[0] == p1 and s[1] == p3 and s[2] == p2
                        and s[3] == p0)

        s.swap_rows(3, 1)
        self.assertTrue(s[0] == p1 and s[1] == p0 and s[2] == p2
                        and s[3] == p3)

        s.multiply_coefficient_and_row(1, 0)
        self.assertTrue(s[0] == p1)
        self.assertTrue(s[1] == p0)
        self.assertTrue(s[2] == p2)
        self.assertTrue(s[3] == p3)

        s.multiply_coefficient_and_row(-1, 2)
        self.assertTrue(s[0] == p1 and s[1] == p0 and s[2] == \
            Plane(normal_vector=Vector([-1,-1,1]), constant_term=-3) \
            and s[3] == p3)

        s.multiply_coefficient_and_row(10, 1)
        self.assertTrue(s[0] == p1 and \
            s[1] == Plane(normal_vector=Vector([10,10,10]),
                          constant_term=10) and
            s[2] == Plane(normal_vector=Vector([-1,-1,1]),
                          constant_term=-3) and
            s[3] == p3)

        s.add_multiple_times_row_to_row(0, 0, 1)
        self.assertTrue(s[0] == p1 and \
            s[1] == Plane(normal_vector=Vector([10,10,10]),
                          constant_term=10) and
            s[2] == Plane(normal_vector=Vector([-1,-1,1]),
                          constant_term=-3) and
            s[3] == p3)

        s.add_multiple_times_row_to_row(1, 0, 1)
        self.assertTrue(s[0] == p1 and \
            s[1] == Plane(normal_vector=Vector([10,11,10]),
                          constant_term=12) and
            s[2] == Plane(normal_vector=Vector([-1,-1,1]),
                          constant_term=-3) and
            s[3] == p3)

        # params coefficient, row_to_add, row_to_be_added_to
        s.add_multiple_times_row_to_row(-1, 1, 0)
        self.assertTrue(s[0] == Plane(normal_vector=Vector([-10, -10, -10]),
                                      constant_term=-10)
                        and s[1] == Plane(normal_vector=Vector([10, 11, 10]),
                                          constant_term=12)
                        and s[2] == Plane(normal_vector=Vector([-1, -1, 1]),
                                          constant_term=-3) and s[3] == p3)