KeyError while perfoming solve of two equation

自闭症网瘾萝莉.ら 提交于 2020-02-08 10:01:07

问题


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

  1. i put lenghths with eq in list like these [( length 1 , eq 1 ) ,.....]
  2. sort list with reverse
  3. get two equation of two longest line

when run the below code

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()

i got the below error

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()

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()

KeyError: t


回答1:


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.

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



来源:https://stackoverflow.com/questions/59910262/keyerror-while-perfoming-solve-of-two-equation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!