Skip to main content
 首页 » 编程设计

python之使用Python计算使用五个点的唯一圆锥截面

2025年02月15日12fff_TT

我正在尝试开发一个 Python 代码,它可以使用来自用户的一组五点输入告诉您中心、准线、焦点和任何唯一圆锥曲线的方程。

我目前正在 MacBook 上的 Sublime Text 上使用 Python2 运行代码,我已经安装了 Scipy、Numpy 和 Sympy。

用户输入五个点后,会出现五种不同的圆锥曲线通式,其标准形式为:

ax^2 + bxy + cy^2 + dx + ey +f = 0

然后在分别求解 a、b、c、d、e、f 之后,我们将得到一个类似于 x^2 - y = 0 的公式,这就是该唯一圆锥曲线的方程。

我正在使用 LUsolve,但是无论我输入什么,它给我的结果总是 [0,0,0,0,0,0]。请查看我的代码并帮助我解决这个问题,谢谢。

enter code here 
 
import numpy as np 
import scipy 
 
class Point: 
    def __init__(self, x, y): 
        self.x = x 
        self.y = y 
 
    def __str__(self): 
        rep = '(' + str(self.x) + ', ' + str(self.y) + ')' 
        return rep 
 
class Conic:                                                 
    def __init__(self, points):                              
        self.points = points                                 
 
    def equation(self):                                     # ...                                
        from sympy import symbols 
        from sympy import Matrix, ImmutableMatrix 
        from sympy.matrices import zeros 
        from sympy.solvers import solve, nsolve, solveset 
        x, y, z, v = symbols("x y z v") 
        # you need symbols for x and y 
        a, b, c, d , e, f = symbols('a b c d e f') 
        xs = Matrix([a, b, c, d, e, f]) 
        l = [[x ** 2, x*y, y ** 2, x, y, 1]] 
        #l = [] 
 
        for point in self.points:  
            l.append([point.x ** 2, point.x * point.y, point.y ** 2, point.x, point.y, 1]) 
 
        m = Matrix(l) 
        v = m.det() 
 
        soln = m.LUsolve(zeros(6, 1)) ###from scipy 
        print(soln) 
        return None 
 
    def __str__(self): 
    pass 
    return None 
 
 
from random import randint 
points = [Point(-2, 4), 
          Point(-1, 1), 
          Point(0, 0), 
          Point(1, 1), 
          Point(2, 4)] 
for point in points: 
    print(point) 
c = Conic(points) 
c.equation() 

结果在这里:

(-2, 4) 
(-1, 1) 
(0, 0) 
(1, 1) 
(2, 4) 
Matrix([[0], [0], [0], [0], [0], [0]]) 
 
***Repl Closed*** 

请您参考如下方法:

我认为你得到零的原因是因为你正在用 zeros(6,1) 求解线性系统,在这种情况下,全零解是有效的。尝试将右侧设置为非零值。

另一个问题是,有五个点和六个未知数,解是欠定的。但是您可以将最后一个坐标固定为某个任意值并解决这个问题。

这是我的 Numpy 纯数值解决方案:

import numpy as np 
 
 
def fivePointsToConic(points, f=1.0): 
    """Solve for the coefficients of a conic given five points in Numpy array 
 
    `points` should have at least five rows. 
 
    `f` is the constant that you can specify. With the returned solution, 
    `(a, b, c, d, e, f)`, the full conic is specified as: 
 
    $a x^2 + b x y + c y^2 + d x + e y = -f$ 
 
    If `points` has exactly five rows, the equation will be exact. If `points` 
    has *more* than five rows, the solution will be a least-squares one that 
    fits the data the best. 
    """ 
    from numpy.linalg import lstsq 
 
    x = points[:, 0] 
    y = points[:, 1] 
    if max(x.shape) < 5: 
        raise ValueError('Need >= 5 points to solve for conic section') 
 
    A = np.vstack([x**2, x * y, y**2, x, y]).T 
    fullSolution = lstsq(A, f * np.ones(x.size)) 
    (a, b, c, d, e) = fullSolution[0] 
    return (a, b, c, d, e, f) 
 
 
if __name__ == '__main__': 
    points = np.array([[-2, 4.], [-1., 1], [0., 0], [1., 1], [2., 4]]) 
    print(fivePointsToConic(points)) 

假设 f = 1(使用符号 a 到 f 在 Wikipedia 上):

(0.62499999999999989, 9.7144514654701197e-17, -0.24999999999999983, -2.9598635688993528e-16, 0.62499999999999989, 1.0) 

或大致为 [0.625, 0, -0.25, 0, 0.625, 1]。这符合您的预期吗?