def test_dot_vectors_different_sizes(self):
     u = (1, 2)
     v = (1, 2, 3)
     with self.assertRaises(
             ValueError,
             msg=
             'should raise ValueError when passing two vectors of different sizes'
     ):
         dot(u, v)
 def test_dot_happy_path(self):
     u = (1.2, 3.4)
     v = (5.6, 7.8)
     dot_actual_result = dot(u, v)
     dot_expected_result = u[0] * v[0] + u[1] * v[1]
     self.assertEqual(dot_actual_result, dot_expected_result,
                      'dot product should match')
def multiply_matrix_vector(m, v):
    if not are_valid_matrices(m):
        raise TypeError('multiply_matrix_vector requires a valid matrix as first argument')

    if not are_valid_vectors(v):
        raise TypeError('multiply_matrix_vector requires a numeric vector as argument')

    if get_matrix_dimensions(m)['num_cols'] != len(v):
        raise TypeError('multiply_matrix_vector requires matrix num columns to match vector size')

    return tuple(dot(row, v) for row in m)
def matrix_multiply(a, b):
    if not are_valid_matrices(a, b):
        raise TypeError('multiply_matrix_vector requires valid matrices as arguments')

    if get_matrix_dimensions(a)['num_cols'] != get_matrix_dimensions(b)['num_rows']:
        raise TypeError('multiply_matrix_vector requires matrix compatible for multiplication')

    return tuple(
        tuple(dot(row_from_a, col_from_b) for col_from_b in zip(*b))
        for row_from_a in a
    )
示例#5
0
def plane_equation(p0, p1, p2):
    """Returns the coefficients a, b, c, d for a plane's equation in
    its standard form (ax + by + cz = d) given three points p0, p1, p2
    in the plane
    """
    vector_in_the_plane_1 = subtract(p1, p0)
    vector_in_the_plane_2 = subtract(p2, p0)
    vector_perpendicular_to_the_plane = cross(vector_in_the_plane_1,
                                              vector_in_the_plane_2)
    a, b, c = vector_perpendicular_to_the_plane
    d = dot(vector_perpendicular_to_the_plane, p1)

    return a, b, c, d
示例#6
0
def matrix_multiply(a, b):
    return tuple(
        tuple(dot(row_from_a, col_from_b) for col_from_b in zip(*b))
        for row_from_a in a)