KeyError while perfoming solve of two equation

2024/11/15 17:43:35

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 mathclass shape():def __init__(self,T):self.T=Tself.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 =0else:self.m_AA = (self.Ay - self.A_dashy) / (self.Ax - self.A_dashx)if (self.Bx - self.B_dashx) == 0:self.m_BB =0else:self.m_BB = (self.By - self.B_dashy) / (self.Bx - self.B_dashx)if (self.Bx - self.B_dashx) == 0:self.m_CC =0else: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 = 0else:self.m_AB = (self.By - self.Ay) / (self.Bx - self.Ax)if (self.Dx - self.Ax) == 0:self.m_AD = 0else:self.m_AD = (self.Dy - self.Ay) / (self.Dx - self.Ax)if (self.Cx - self.Bx) == 0:self.m_BC = 0else:self.m_BC = (self.Cy - self.By) / (self.Cx - self.Bx)if (self.Dx - self.Bx) == 0:self.m_BD = 0else:self.m_BD = (self.Dy - self.By) / (self.Dx - self.Bx)if (self.Dx - self.Cx) == 0:self.m_CD = 0else:self.m_CD = (self.Dy - self.Cy) / (self.Dx - self.Cx)if (self.Cx - self.Ax) == 0:self.m_AC = 0else: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

Answer

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://en.xdnf.cn/q/119292.html

Related Q&A

Most Pythonic way to merge two dictionnaries having common key/value pair

I have two lists of python dictionnaries : l1 = [{"id":1, "name":"A"}, {"id":2, "name":"B"}] l2 = [{"id":1, "full_name":&…

How to close a while True loop instantly Python

I have a problem ... How can i press P on my keyboard and close the entire program faster ( i would like instantly ) ? The script that i made runs in a loop ( Loop B ) and checks for an image on deskt…

How to replace a value in a list

the program asks user to enter 5 unique number, if the number is already in the list, ask for a new number. after 5 unique numbers have been entered, display the listnumbers = [1,2,3,4,5] count = 0 ind…

How To Reverse A Nested Python List

Given the following nested list,myList=([1,[2,3],[[4,5,[6],7],8,9]])I want to reverse it to be converted into:myList= [[[4, 5, [6], 7], 8, 9], [2, 3], 1]How do I do that? Thanks.

Extract street address from a string

Is there any way to extract a street address from a string (say, email) using python? The address does not come in a set format. It can come without state, zip code, city, but I can guess and supply t…

Convert list in String format back to list of float numbers

str = [ 3.82133931e-01 4.27354313e-02 1.94678816e-03 0.00000000e+000.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+000.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e…

Hawaiian Pronunciation [duplicate]

This question already has an answer here:Hawaiian pronouncer(1 answer)Closed 1 year ago.Hitting a snag with an assignment and thought Id ask for help. The goal is to be able to pronounce Hawaiian words…

mutiline python script in html (pyscript)

Ive tried to use pyscript in html but i can only get it to work in one line of code can somebody help me get it to work for the following code? def vpn(website):from selenium import webdriverfrom sele…

How to write an output of a command to stdout and a file in Python3?

I have a Windows command which I want to write to stdout and to a file. For now, I only have 0 string writen in my file:#!/usr/bin/env python3 #! -*- coding:utf-8 -*-import subprocesswith open(auto_cha…

Mongodb adding a new field in an existing document, with specific position

I am facing this issue where I need to insert a new field in an existing document at a specific position. Sample document: { "name": "user", "age" : "21", "…