当前位置: 首页>>代码示例>>Python>>正文


Python Vector.is_parallel方法代码示例

本文整理汇总了Python中vector.Vector.is_parallel方法的典型用法代码示例。如果您正苦于以下问题:Python Vector.is_parallel方法的具体用法?Python Vector.is_parallel怎么用?Python Vector.is_parallel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在vector.Vector的用法示例。


在下文中一共展示了Vector.is_parallel方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_parallel_or_orthogonal

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import is_parallel [as 别名]
def test_parallel_or_orthogonal():
    """Quiz 4 checking for parallelism and orthogonality"""
    vector1 = Vector([-7.579, -7.88])
    vector2 = Vector([22.737, 23.64])
    assert vector1.is_parallel(vector2) is True
    assert vector1.is_orthogonal(vector2) is False

    vector3 = Vector([-2.029, 9.97, 4.172])
    vector4 = Vector([-9.231, -6.639, -7.245])
    assert vector3.is_parallel(vector4) is False
    assert vector3.is_orthogonal(vector4) is False

    vector5 = Vector([-2.328, -7.284, -1.214])
    vector6 = Vector([-1.821, 1.072, -2.94])
    assert vector5.is_parallel(vector6) is False
    assert vector5.is_orthogonal(vector6) is True

    vector7 = Vector([2.118, 4.827])
    vector8 = Vector([0, 0])
    assert vector7.is_parallel(vector8) is True
    assert vector7.is_orthogonal(vector8) is True
开发者ID:madhavajay,项目名称:ud953,代码行数:23,代码来源:udacity_test.py

示例2: Line

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import is_parallel [as 别名]
class Line(object):

    NO_NONZERO_ELTS_FOUND_MSG = 'No nonzero elements found'
    THEY_ARE_PARALLEL_AND_DONT_INTERSECT_MSG = 'They are parallel and never intersect.'
    THEY_ARE_THE_SAME_LINE_MSG = 'Has infinite intersections because they are the same line.'

    def __init__(self, normal_vector=None, constant_term=None):
        self.dimension = 2

        if not normal_vector:
            all_zeros = ['0']*self.dimension
            normal_vector = all_zeros
        self.normal_vector = Vector(normal_vector)

        if not constant_term:
            constant_term = Decimal('0')
        self.constant_term = Decimal(constant_term)

        self.set_basepoint()


    def set_basepoint(self):
        try:
            n = self.normal_vector.coordinates
            c = self.constant_term
            basepoint_coords = ['0']*self.dimension

            initial_index = Line.first_nonzero_index(n)
            initial_coefficient = n[initial_index]

            basepoint_coords[initial_index] = c/initial_coefficient
            self.basepoint = Vector(basepoint_coords)

        except Exception as e:
            if str(e) == Line.NO_NONZERO_ELTS_FOUND_MSG:
                self.basepoint = None
            else:
                raise e

    def is_parallel(self, l):
        return self.normal_vector.is_parallel(l.normal_vector)

    def __eq__(self, l):
        if(self.is_parallel(l)):
            direction_vector = self.basepoint.minus(l.basepoint)
            return direction_vector.is_orthogonal(self.normal_vector)
        return False
            
    def intersection(self, l):
        if(self.__eq__(l)):
            return self
        if(self.is_parallel(l)):
            return None
        A, B = self.normal_vector.coordinates
        C, D = l.normal_vector.coordinates
        k1 = self.constant_term
        k2 = l.constant_term
        x = (D*k1 - B*k2)/(A*D - B*C)
        y = (A*k2 - C*k1)/(A*D - B*C)
        return Vector([x,y])


    def __str__(self):

        num_decimal_places = 3

        def write_coefficient(coefficient, is_initial_term=False):
            coefficient = round(coefficient, num_decimal_places)
            if coefficient % 1 == 0:
                coefficient = int(coefficient)

            output = ''

            if coefficient < 0:
                output += '-'
            if coefficient > 0 and not is_initial_term:
                output += '+'

            if not is_initial_term:
                output += ' '

            if abs(coefficient) != 1:
                output += '{}'.format(abs(coefficient))

            return output

        n = self.normal_vector.coordinates

        try:
            initial_index = Line.first_nonzero_index(n)
            terms = [write_coefficient(n[i], is_initial_term=(i==initial_index)) + 'x_{}'.format(i+1)
                     for i in range(self.dimension) if round(n[i], num_decimal_places) != 0]
            output = ' '.join(terms)

        except Exception as e:
            if str(e) == self.NO_NONZERO_ELTS_FOUND_MSG:
                output = '0'
            else:
                raise e

#.........这里部分代码省略.........
开发者ID:lem93,项目名称:Linear-Algebra-Basic-Functions,代码行数:103,代码来源:line.py

示例3: Plane

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import is_parallel [as 别名]
class Plane(object):
    """Plane class in the form Ax + By + Cz = K"""

    NO_NONZERO_ELTS_FOUND_MSG = 'No nonzero elements found'

    """Initialise a new Plane with a normal Vector and a Constant Term"""
    def __init__(self, normal_vector=None, constant_term=None):
        self.dimension = 3

        if not normal_vector:
            all_zeros = ['0'] * self.dimension
            normal_vector = Vector(all_zeros)
        self.normal_vector = normal_vector

        if not constant_term:
            constant_term = Decimal('0')
        self.constant_term = Decimal(constant_term)

        self.set_basepoint()

    def __eq__(self, plane):
        vector_1_rounded = self.normal_vector.round_coords(3)
        vector_2_rounded = plane.normal_vector.round_coords(3)
        vector_eq = vector_1_rounded == vector_2_rounded

        constant_eq = (self.constant_term == plane.constant_term)
        return vector_eq and constant_eq

    def __getitem__(self, index):
        return self.normal_vector[index]

    def __setitem__(self, index, value):
        new_vals = []
        for i, val in enumerate(self.normal_vector):
            if i == index:
                val = value
            new_vals.append(val)

        self.normal_vector = Vector(tuple([Decimal(x) for x in new_vals]))

    def __add__(self, plane):
        """Add one Plane with another Plane"""
        new_vector = self.normal_vector + plane.normal_vector
        new_constant = self.constant_term + plane.constant_term
        return Plane(new_vector, new_constant)

    def __sub__(self, plane):
        """Subtract one Plane from another Plane"""
        new_vector = self.normal_vector - plane.normal_vector
        new_constant = self.constant_term - plane.constant_term
        return Plane(new_vector, new_constant)

    def __mul__(self, val):
        """Performs Multiplication of Coefficients"""
        if isinstance(val, (numbers.Integral, numbers.Real, Decimal)):
            return self._multiply(val)
        else:
            raise TypeError('value must be a number')

    def _multiply(self, val):
        vector = self.normal_vector * val
        constant_term = self.constant_term * val
        mul_plane = Plane(vector, constant_term)
        return mul_plane

    def is_parallel(self, plane):
        """Determine if two Planes are parallel"""
        return self.normal_vector.is_parallel(plane.normal_vector)

    def plane_relationship(self, plane):
        """Return the relationship of the Planes"""
        if self.is_coincidence(plane) is True:
            return 'planes are coincidental'
        elif self.is_parallel(plane) is True:
            return 'planes are parallel'
        else:
            return 'planes are not parallel'

    def point_for_x_y(self, x_coord, y_coord):
        """
        Calculate a point with x, y, z passing in x and y
        Ax + By + Cz = k
        z = (k - (Ax + By)) / C
        """
        a1_coefficient = self.normal_vector[0]
        b1_coefficient = self.normal_vector[1]
        c1_coefficient = self.normal_vector[2]
        k1_constant = self.constant_term

        ax_val = a1_coefficient * x_coord
        by_val = b1_coefficient * y_coord

        z_coord = (k1_constant - (ax_val + by_val)) / c1_coefficient
        return Decimal(x_coord), Decimal(y_coord), Decimal(z_coord)

    def is_coincidence(self, plane):
        """
        Determine if two Planes are coincidence Planes
        If they are parallel and a Vector between a point from each plane
        Is orthogonal to the normal vector
#.........这里部分代码省略.........
开发者ID:madhavajay,项目名称:ud953,代码行数:103,代码来源:plane.py

示例4: test_is_parallel

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import is_parallel [as 别名]
def test_is_parallel():
    v1 = Vector(-7.579, -7.88)
    v2 = Vector(22.737, 23.64)

    assert v1.is_parallel(v2)
开发者ID:ryanleland,项目名称:Linear.py,代码行数:7,代码来源:test_vector.py

示例5: Plane

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import is_parallel [as 别名]
class Plane(object):

    NO_NONZERO_ELTS_FOUND_MSG = 'No nonzero elements found'

    def __init__(self, normal_vector=None, constant_term=None):
        self.dimension = 3

        if not normal_vector:
            all_zeros = ['0']*self.dimension
            normal_vector = all_zeros
        self.normal_vector = Vector(normal_vector)

        if not constant_term:
            constant_term = Decimal('0')
        self.constant_term = Decimal(constant_term)

        self.set_basepoint()


    def set_basepoint(self):
        try:
            n = self.normal_vector.coordinates
            c = self.constant_term
            basepoint_coords = ['0']*self.dimension

            initial_index = Plane.first_nonzero_index(n)
            initial_coefficient = n[initial_index]

            basepoint_coords[initial_index] = c/initial_coefficient
            self.basepoint = Vector(basepoint_coords)

        except Exception as e:
            if str(e) == Plane.NO_NONZERO_ELTS_FOUND_MSG:
                self.basepoint = None
            else:
                raise e

    def is_parallel(self, p):
        return self.normal_vector.is_parallel(p.normal_vector)

    def __eq__(self, p):
        if(self.is_parallel(p)):
            direction_vector = self.basepoint.minus(p.basepoint)
            return direction_vector.is_orthogonal(self.normal_vector)
        return False

    def __str__(self):

        num_decimal_places = 3

        def write_coefficient(coefficient, is_initial_term=False):
            coefficient = round(coefficient, num_decimal_places)
            if coefficient % 1 == 0:
                coefficient = int(coefficient)

            output = ''

            if coefficient < 0:
                output += '-'
            if coefficient > 0 and not is_initial_term:
                output += '+'

            if not is_initial_term:
                output += ' '

            if abs(coefficient) != 1:
                output += '{}'.format(abs(coefficient))

            return output

        n = self.normal_vector.coordinates

        try:
            initial_index = Plane.first_nonzero_index(n)
            terms = [write_coefficient(n[i], is_initial_term=(i==initial_index)) + 'x_{}'.format(i+1)
                     for i in range(self.dimension) if round(n[i], num_decimal_places) != 0]
            output = ' '.join(terms)

        except Exception as e:
            if str(e) == self.NO_NONZERO_ELTS_FOUND_MSG:
                output = '0'
            else:
                raise e

        constant = round(self.constant_term, num_decimal_places)
        if constant % 1 == 0:
            constant = int(constant)
        output += ' = {}'.format(constant)

        return output


    @staticmethod
    def first_nonzero_index(iterable):
        for k, item in enumerate(iterable):
            if not MyDecimal(item).is_near_zero():
                return k
        raise Exception(Plane.NO_NONZERO_ELTS_FOUND_MSG)
开发者ID:lem93,项目名称:Linear-Algebra-Basic-Functions,代码行数:100,代码来源:plane.py

示例6: Line

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import is_parallel [as 别名]
class Line(object):
    """Line class in the form Ax + By = C"""

    NO_NONZERO_ELTS_FOUND_MSG = 'No nonzero elements found'

    """Initialise a new Line with a normal Vector and a Constant Term"""
    def __init__(self, normal_vector=None, constant_term=None):
        self.dimension = 2

        if not normal_vector:
            all_zeros = ['0'] * self.dimension
            normal_vector = Vector(all_zeros)
        self.normal_vector = normal_vector

        if not constant_term:
            constant_term = Decimal('0')
        self.constant_term = Decimal(constant_term)

        self.set_basepoint()

    def __getitem__(self, index):
        return self.normal_vector[index]

    def __setitem__(self, index, value):
        new_vals = []
        for i, val in enumerate(self.normal_vector):
            if i == index:
                val = value
            new_vals.append(val)

        self.normal_vector = Vector(tuple([Decimal(x) for x in new_vals]))

    def direction_vector(self):
        """Return the Direction Vector of a Line where A,B becomes B,-A"""
        return Vector([self.normal_vector[1], -self.normal_vector[0]])

    def is_parallel(self, line):
        """Determine if two Lines are parallel"""
        return self.normal_vector.is_parallel(line.normal_vector)

    def point_for_x(self, x_coord):
        """
        Ax + By = C
        Get a point given a x co-ordinate where y = m x + b
        """
        b_coefficient = self.normal_vector[1]
        c_constant = self.constant_term
        m_gradient = self.gradient()
        y_coord = m_gradient * x_coord + c_constant / b_coefficient
        return Decimal(x_coord), Decimal(y_coord)

    def line_relationship(self, line):
        """Return the relationship of the Lines"""
        if self.is_coincidence(line) is True:
            return 'lines are coincidental'
        elif self.is_parallel is True:
            return 'lines are parallel'
        else:
            x_coord, y_coord = self.point_of_intersection(line)
            if x_coord is not None:
                x_coord = round(Decimal(x_coord), 3)
            if y_coord is not None:
                y_coord = round(Decimal(y_coord), 3)
            intersection = x_coord, y_coord
            return 'lines intersect at {}'.format(intersection)

    def gradient(self):
        """Get the gradient of the Line represented as m"""
        a_coefficient = self.normal_vector[0]
        b_coefficient = self.normal_vector[1]
        m_gradient = -a_coefficient / b_coefficient
        return m_gradient

    def point_of_intersection(self, line):
        """Find the point of intersection between two Lines"""
        if self.is_parallel(line) is True:
            return None, None

        a1_coefficient = self.normal_vector[0]
        b1_coefficient = self.normal_vector[1]
        c1_constant = self.constant_term

        a2_coefficient = line.normal_vector[0]
        b2_coefficient = line.normal_vector[1]
        c2_constant = line.constant_term

        determinant = (a1_coefficient * b2_coefficient) - \
                      (b1_coefficient * a2_coefficient)
        determinant_x = (c1_constant * b2_coefficient) - \
                        (b1_coefficient * c2_constant)
        determinant_y = (a1_coefficient * c2_constant) - \
                        (c1_constant * a2_coefficient)
        x_coord = determinant_x / determinant
        y_coord = determinant_y / determinant
        return Decimal(x_coord), Decimal(y_coord)

    def is_coincidence(self, line):
        """
        Determine if two Lines are coincidence Lines
        Where a new line with any point from each is also parallel
#.........这里部分代码省略.........
开发者ID:madhavajay,项目名称:ud953,代码行数:103,代码来源:line.py


注:本文中的vector.Vector.is_parallel方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。