Ejemplo n.º 1
0
def initial_code():
    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])

    print s.indices_of_first_nonzero_terms_in_each_row()
    print '{},{},{},{}'.format(s[0], s[1], s[2], s[3])
    print len(s)
    print s

    s[0] = p1
    print s

    print MyDecimal('1e-9').is_near_zero()
    print MyDecimal('1e-11').is_near_zero()
Ejemplo n.º 2
0
    def intersection_with(self, ell):
        try:
            A, B = self.normal_vector.coordinates
            C, D = ell.normal_vector.coordinates
            k1, k2 = self.constant_term, ell.constant_term

            x_numerator = D*k1 - B*k2
            y_numerator = -C*k1 + A*k2
            one_over_denom = Decimal('1')/(A*D - B*C)

            return Vector([x_numerator, y_numerator]).times_scalar(one_over_denom)
        except ZeroDivisionError:
            if self == ell:
                return self
            else:
                return None
Ejemplo n.º 3
0
def intersections_quiz():
    ell1 = Line(normal_vector=Vector(['4.046', '2.836']), constant_term='1.21')
    ell2 = Line(normal_vector=Vector(['10.115', '7.09']), constant_term='3.025')
    print 'intersection 1:', ell1.intersection_with(ell2)

    ell3 = Line(normal_vector=Vector(['7.204', '3.182']), constant_term='8.68')
    ell4 = Line(normal_vector=Vector(['8.172', '4.114']), constant_term='9.883')
    print 'intersection 2:', ell3.intersection_with(ell4)

    ell5 = Line(normal_vector=Vector(['1.182', '5.562']), constant_term='6.744')
    ell6 = Line(normal_vector=Vector(['1.773', '8.343']), constant_term='9.525')
    print 'intersection 3:', ell5.intersection_with(ell6)
Ejemplo n.º 4
0
def coding_row_operations_quiz():
    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)
    s0p1 = s[0] == p1
    s1p0 = s[1] == p0
    s2p2 = s[2] == p2
    s3p3 = s[3] == p3
    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)
    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)
    if not (s[0] == p1 and s[1] == p0
            and s[2] == Plane(normal_vector=Vector(['-1', '-1', '1']),
                              constant_term='-3') and s[3] == p3):
        print 'test case 5 failed'

    s.multiply_coefficient_and_row(10, 1)
    if not (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):
        print 'test case 6 failed'

    s.add_multiple_times_row_to_row(0, 0, 1)
    if not (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):
        print 'test case 7 failed'

    s.add_multiple_times_row_to_row(1, 0, 1)
    if not (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):
        print 'test case 8 failed'

    s.add_multiple_times_row_to_row(-1, 1, 0)
    if not (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):
        print 'test case 9 failed'