[英]Given two points of 4 or more dimensions is it possible to find a line equation?
[英]To find whether a given line (equation) is able to separate the two lists of points sucessfully in python
我遇到了一個問題,明確要求我不要使用 numpy 或 pandas
問題 :
以元組列表的形式給出兩組數據點,例如
Red =[(R11,R12),(R21,R22),(R31,R32),(R41,R42),(R51,R52),..,(Rn1,Rn2)]
Blue=[(B11,B12),(B21,B22),(B31,B32),(B41,B42),(B51,B52),..,(Bm1,Bm2)]
和一組線方程(以字符串格式,即字符串列表)
Lines = [a1x+b1y+c1,a2x+b2y+c2,a3x+b3y+c3,a4x+b4y+c4,..,K lines]
注意:這里需要做字符串解析,得到x,y和intercept的系數。
您的任務是為給定的每一行打印“是”/“否”。 如果所有紅點都在直線的一側,藍點在直線的另一側,則應打印 YES,否則應打印 NO。
Ex:
Red= [(1,1),(2,1),(4,2),(2,4), (-1,4)]
Blue= [(-2,-1),(-1,-2),(-3,-2),(-3,-1),(1,-3)]
Lines=["1x+1y+0","1x-1y+0","1x+0y-3","0x+1y-0.5"]
Output:
YES
NO
NO
YES
在數學上,你取直線方程說 S
Suppose S(x)(y) = 1x+1y+0
Now take points (1,1) and (-6,-1)
S(1)(1) = 1(1)+ 1(1) = 2 >0
S(-6)(-1) = 1(-6)+(1)(-1) = -7 <0
因此,我們可以得出結論,(1,1) 和 (-6,-1) 位於直線 S 的不同側。
現在在給定的問題中,給定一個方程 S,所有紅色都應該在方程的一側,而藍色應該在另一側。
這里的問題是,我不確定如何使用 python 替換以字符串形式給出的等式中列表中點的值。
另外,我沒有為代碼提出一個邏輯(如何根據我們的要求使用循環)來解決上述問題以解決上述問題。
將不勝感激對這個問題的見解。 提前致謝。
假設你的字符串總是形式
ax+by+c
(按這個順序),你可以寫
import re
for line in Lines:
a, b, c = [float(coef.strip()) for coef in re.split('x|y', line)]
...
使用 your_string.replace() 和 eval() 函數,用它的值替換 x 和 y 字符,eval() 將字符串作為方程執行
for element in red:
equation=line.replace('x','*'+str(element[0]))
equation=equation.replace('y','*'+str(element [1]))
result=eval(equation)
if result > 0:
pass
else:
return "NO"
#code for blue
return "Yes"
def pointsAreOnSameSideOfLine(a, b, c, x1, y1, x2, y2):
fx1 = 0 # Variable to store a * x1 + b * y1 - c
fx2 = 0 # Variable to store a * x2 + b * y2 - c
fx1 = a * x1 + b * y1 - c
fx2 = a * x2 + b * y2 - c
# If fx1 and fx2 have same sign
if ((fx1 * fx2) > 0):
return True
return False
a, b, c = 1, 1, 1
x1, y1 = 1, 1
x2, y2 = 2, 1
if (pointsAreOnSameSideOfLine(a, b, c, x1, y1, x2, y2)):
print("Yes")
else:
print("No")
@hemanth ravavarapu,re.split(x|y) 將基於 x 或 yie 分割線方程,如果你給空間,那么它將根據空間分割,這里也將在找到 x 或 y 的地方分割。
e1 = []
e2 = []
def points():
for k in Lines:
a=k[0];b=k[2]+k[3];c=k[5]+k[6]
n = [];m=[]
for i in Red:
x1=i[0];y1=i[1]
eq1 = int(a) * int(x1) + int(b) * int(y1)-int(c)
n.append(eq1)
e1.append(n)
for j in Blue:
x2=j[0];y2=j[1]
eq2 = int(a) * int(x2) + int(b) * int(y2)-int(c)
m.append(eq2)
e2.append(m)
print(e1)
print('----------------------------------------------------------------------')
print(e2)
print('----------------------------------------------------------------------')
p=[]
for i,j in zip(e1,e2):
q = []
for k,l in zip(i,j):
x=k*l
if x<0:
q.append(x)
p.append(q)
print(p)
for i in p:
if len(i)==5:
print('yes')
else:
print('No')
Red= [(1,1),(2,1),(4,2),(2,4),(-1,4)]
Blue= [(-2,-1),(-1,-2),(-3,-2),(-3,-1),(1,-3)]
Lines=["1x+1y+0","1x-1y+0","1x+0y-3","0x+1y-0.5"]
points()
對於字符串解析:
def getCor(s):
first = s.split('x')
a = float(first[0])
second = first[1].split('y')
b = float(second[0])
c = float(second[1])
return (a,b,c)
getCor('24x+1y-0.5')
輸出 :
(24.0, 1.0, -0.5)
import math
def getCor(s):
first = s.split('x')
a = float(first[0])
second = first[1].split('y')
b = float(second[0])
c = float(second[1])
return (a,b,c)
def getSide(t,p):
cal = (t[0] * p[0]) + (t[1] * p[1]) + (t[2])
if cal > 0:
return 1
elif cal < 0:
return -1
elif cal == 0:
return 0
return -2
def getAns(red,blue,line):
sign_red = getSide(getCor(line),red[0])
sign_blue = getSide(getCor(line),blue[0])
for j in range(len(red)):
if sign_red != getSide(getCor(line),red[j]):
return 'no'
for j in range(len(blue)):
if sign_blue != getSide(getCor(line),blue[j]):
return 'no'
return 'Yes'
Red= [(1,1),(2,1),(4,2),(2,4), (-1,4)]
Blue= [(-2,-1),(-1,-2),(-3,-2),(-3,-1),(1,-3)]
Lines=["1x+1y+0","1x-1y+0","1x+0y-3","0x+1y-0.5"]
for i in Lines:
ans = getAns(Red, Blue, i)
print(ans)
輸出:
Yes
no
no
Yes
import math
# write your python code here
# you can take the above example as sample input for your program to test
# it should work for any general input try not to hard code for only given input strings
# you can free to change all these codes/structure
def i_am_the_one(red,blue,line):
# your code
for i in red:
eq=line.replace('x','*'+str(i[0]))
eq=eq.replace('y','*'+str(i[1]))
answer=eval(eq)
if answer>0:
pass
else:
return "NO"
# Code for Blue
for j in blue:
eq1=line.replace('x','*'+str(j[0]))
eq1=eq1.replace('y','*'+str(j[1]))
answer1=eval(eq1)
if answer1<0:
pass
else:
return "NO"
return "Yes"
Red= [(1,1),(2,1),(4,2),(2,4), (-1,4)]
Blue= [(-2,-1),(-1,-2),(-3,-2),(-3,-1),(1,-3)]
Lines=["1x+1y+0","1x-1y+0","1x+0y-3","0x+1y-0.5"]
for i in Lines:
yes_or_no = i_am_the_one(Red, Blue, i)
print(yes_or_no)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.