求解两个方程时出现 KeyError [英] KeyError while perfoming solve of two equation

查看:43
本文介绍了求解两个方程时出现 KeyError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1st 我需要得到两个最长线长度的两个方程

1st i need to get two equation of two longest line length

  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行,

2.66666666666667 1.33333333333333 {z: t} Traceback (most recent call last): File "C:/Users/xxxxx/.PyCharmCE2019.3/config/scratches/programme logic function.py", line 127, in

a1.square_intersection()

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

File "C:/Users/xxxxx/.PyCharmCE2019.3/config/scratches/programme logic function.py", line 119, in square_intersection

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

键错误:t

推荐答案

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

If you print out the equations you are trying to solve you will see that they are the same so the only solution returned is {z:t} not {z:smthng, t:smthngelse}. So when you request the value of t it tells you that there is no t in the solution dictionary. Now you have to go back and see that you properly computed that equations that were to be solved.

还要注意,SymPy 具有 Polygon/Triangle 对象,并且可能已经有一种方法可以回答您所提出的问题.

Note, too, that SymPy has the Polygon/Triangle objects and might already have a method to answer the question that you are asking.

这篇关于求解两个方程时出现 KeyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆