【问题标题】:KeyError while perfoming solve of two equation求解两个方程时出现 KeyError
【发布时间】:2020-01-25 14:40:07
【问题描述】:

第一个我需要得到两个最长线长度的两个方程

  1. 我将带有 eq 的长度放入列表中,如下所示 [( length 1 , eq 1 ) ,.....]
  2. 反向排序列表
  3. 得到两条最长线的两个方程

当运行下面的代码时

from sympy import *
import math

    class shape():

        def __init__(self,T):
            self.T=T
            self.Ax = self.T[0][0]
            self.Ay = self.T[0][1]
            self.Bx = self.T[1][0]
            self.By = self.T[1][1]
            self.Cx = self.T[2][0]
            self.Cy = self.T[2][1]

            #Triangle:
            self.C_dashx = 0.5 * (self.Ax + self.Bx)
            self.C_dashy = 0.5 * (self.Ay + self.By)
            self.B_dashx = 0.5 * (self.Ax + self.Cx)
            self.B_dashy = 0.5 * (self.Ay + self.Cy)
            self.A_dashx = 0.5 * (self.Bx + self.Cx)
            self.A_dashy = 0.5 * (self.By + self.Cy)

            if (self.Ax - self.A_dashx) == 0:
                self.m_AA =0
            else:
                self.m_AA = (self.Ay - self.A_dashy) / (self.Ax - self.A_dashx)


            if (self.Bx - self.B_dashx) == 0:
                self.m_BB =0
            else:
                self.m_BB = (self.By - self.B_dashy) / (self.Bx - self.B_dashx)

            if (self.Bx - self.B_dashx) == 0:
                self.m_CC =0
            else:
                self.m_CC = (self.Cy - self.C_dashy) / (self.Bx - self.B_dashx)


        def triangle_intersection(self):
            self.x , self.y = symbols('x y')
            self.eq1=Eq(self.m_AA*(self.x-self.Ax)+self.Ay-self.y,0)
            self.eq2=Eq(self.m_BB*(self.x-self.Bx)+self.By-self.y,0)
            self.solve_equation=solve([self.eq1,self.eq2],[self.x,self.y])
            self.Zx=(self.solve_equation[self.x]).factor()
            self.Zy=(self.solve_equation[self.y]).factor()
            print(self.Zx,self.Zy)


        def square_intersection(self):

            self.Dx = self.T[3][0]
            self.Dy = self.T[3][0]

            # Line Solpe:

            if (self.Bx - self.Ax) == 0:
                self.m_AB = 0
            else:
                self.m_AB = (self.By - self.Ay) / (self.Bx - self.Ax)

            if (self.Dx - self.Ax) == 0:
                self.m_AD = 0
            else:
                self.m_AD = (self.Dy - self.Ay) / (self.Dx - self.Ax)

            if (self.Cx - self.Bx) == 0:
                self.m_BC = 0
            else:
                self.m_BC = (self.Cy - self.By) / (self.Cx - self.Bx)

            if (self.Dx - self.Bx) == 0:
                self.m_BD = 0
            else:
                self.m_BD = (self.Dy - self.By) / (self.Dx - self.Bx)

            if (self.Dx - self.Cx) == 0:
                self.m_CD = 0
            else:
                self.m_CD = (self.Dy - self.Cy) / (self.Dx - self.Cx)

            if (self.Cx - self.Ax) == 0:
                self.m_AC = 0
            else:
                self.m_AC = (self.Cy - self.Ay) / (self.Cx - self.Ax)

            # Equation:
    #### WHAT IS PROBLEM HERE###########

            self.z, self.t = symbols('z t',positive=True)

            self.eq_AB = Eq(self.m_AB * (self.z - self.Ax) + self.Ay - self.t,0)
            self.eq_AC = Eq(self.m_AC * (self.z - self.Ax) + self.Ay - self.t,0)
            self.eq_AD = Eq(self.m_AD * (self.z - self.Ax) + self.Ay - self.t,0)
            self.eq_BC = Eq(self.m_BC * (self.z - self.Bx) + self.By - self.t,0)
            self.eq_BD = Eq(self.m_BD * (self.z - self.Bx) + self.By - self.t,0)
            self.eq_CD = Eq(self.m_CD * (self.z - self.Cx) + self.Cy - self.t,0)

            self.length_AB = math.sqrt((self.Bx - self.Ax)**2 + (self.By - self.Ay)**2)
            self.length_AC = math.sqrt((self.Cx - self.Ax)**2 + (self.Cy - self.Ay)**2)
            self.length_AD = math.sqrt((self.Dx - self.Ax)**2 + (self.Dy - self.Ay)**2)
            self.length_BC = math.sqrt((self.Cx - self.Bx)**2 + (self.Cy - self.By)**2)
            self.length_BD = math.sqrt((self.Dx - self.Bx)**2 + (self.Dy - self.By)**2)
            self.length_CD = math.sqrt((self.Dx - self.Cx)**2 + (self.Dy - self.Cy)**2)

    #### WHAT IS PROBLEM HERE###########

            self.lenghts = [(self.length_AB, self.eq_AB),(self.length_AC, self.eq_AC),(self.length_AD, self.eq_AD),(self.length_BC, self.eq_BC)
                , (self.length_BD, self.eq_BD), (self.length_CD, self.eq_CD)]


            self.newlenghts = sorted(self.lenghts,reverse=True)
            self.max1=self.newlenghts[0][1]
            self.max2=self.newlenghts[1][1]


            self.solve_equation_sq = solve([self.max1, self.max2], [self.z, self.t]) 
            print(self.solve_equation_sq)
            self.Rx = (self.solve_equation_sq[self.z]).factor()
            self.Ry = (self.solve_equation_sq[self.t]).factor()

            print(self.Rx, self.Ry)


    test=[(0,0),(4,0),(4,4),(0,4)]
    a1=shape(test)
    a1.triangle_intersection()
    a1.square_intersection()

我收到以下错误

2.66666666666667 1.33333333333333 {z: t} 回溯(最近一次通话最后): 文件“C:/Users/xxxxx/.PyCharmCE2019.3/config/scratches/programme logic function.py”,第127行,在

a1.square_intersection()

文件“C:/Users/xxxxx/.PyCharmCE2019.3/config/scratches/programme logic function.py”,第119行,在square_intersection

self.Ry = (self.solve_equation_sq[self.t]).factor()

KeyError: t

【问题讨论】:

  • 你真的不应该使用import *,这是不好的做法。

标签: python sympy keyerror


【解决方案1】:

如果您打印出您要求解的方程,您会发现它们是相同的,因此返回的唯一解是 {z:t} 而不是 {z:smthng, t:smthngelse}。因此,当您请求 t 的值时,它会告诉您解决方案字典中没有 t。现在您必须返回并查看您是否正确计算了要求解的方程。

另外请注意,SymPy 具有 Polygon/Triangle 对象,并且可能已经有一种方法来回答您提出的问题。

【讨论】:

  • 如果你使用 SymPy 的点、三角形、多边形,我相信你的问题会是这样的:A, B, C, D = map(Point, test); print(Triangle(A, B, C).centroid, Polygon(A, B, C, D).centroid)。即使您喜欢在较低级别工作,您也可能希望使用 LineSegment 对象,这些对象具有 slope 之类的属性以及 equationintersect 之类的方法,例如Line(A,C).intersect(Line(B,D))
猜你喜欢
  • 2016-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-19
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
相关资源
最近更新 更多