规范化向量时出现数学域错误

2024-04-19 01:05:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习一门关于向量的课程,我试着用Python实现向量操作,我正在学习这个过程。我得到一个数学域错误时,规范化的向量。知道为什么吗?你知道吗

我得到的错误是:

第67行,与 升起(e) ValueError:数学域错误

import math
from decimal import Decimal, getcontext

getcontext().prec = 30

class Vector(object):
    def __init__(self, coordinates):
        try:
            if not coordinates:
                raise ValueError
            self.coordinates = tuple([Decimal(x) for x in coordinates])
            self.dim ension = len(coordinates)

        except ValueError:
            raise ValueError('The coordinates must be nonempty')

        except TypeError:
            raise TypeError('The coordinates must be an iterable')


def __str__(self):
    return 'Vector: {}'.format(self.coordinates)

def plus(self, v):
    new_coordinates = [x+y for x,y in zip(self.coordinates, v.coordinates)]
    return Vector(new_coordinates)

def minus(self, v):
    new_coordinates = [x-y for x,y in zip(self.coordinates, v.coordinates)]
    return Vector(new_coordinates)

def times_scalar(self, c):
    new_coordinates = [Decimal(c)*x for x in self.coordinates]
    return Vector(new_coordinates)

def magnitude(self):
    coordinates_squared = [x**2 for x in self.coordinates]
    return Decimal(math.sqrt(sum(coordinates_squared)))

def normalized(self):
    try:
        magnitude = self.magnitude()
        return self.times_scalar(Decimal('1.0')/magnitude)

    except ZeroDivisionError:
        raise Exception('Cannot normalize the zero vector')

def dot_product(self, v):
    return sum([x*y for x,y in zip(self.coordinates, v.coordinates)])

def angle_with(self, v, in_degrees=False):
    try:
        u1 = self.normalized()
        u2 = v.normalized()
        angle_in_radians = math.acos(u1.dot_product(u2))

        if in_degrees:
            degrees_per_radian = 180 / math.pi
            return angle_in_radians * degrees_per_radian
        else:
            return angle_in_radians

    except Exception as e:
        if str(e) == 'Cannot normalize the zero vector':
            raise Exception('Cannot compute an angle with the zero vector')
        else:
            raise(e)

a = Vector(['-7.579', '-7.88'])
b = Vector(['22.737', '23.64'])
print a.is_parallel_to(b)
print a.is_orthogonal(b)

Tags: inselfnewforreturndefmathraise
1条回答
网友
1楼 · 发布于 2024-04-19 01:05:33

Łukasz-Rogalski指出,如果u1和u2的点积在[-1,1]之外,acos会导致这个错误。 如果你规范化向量a和b,并检查点积的精度为30像你有,你会看到问题。你知道吗

将精度更改为10左右以处理此错误。你知道吗

相关问题 更多 >