Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 两条线的交点。以下哪一个等式是最节省资源的?_Python_Performance_Line_Intersection - Fatal编程技术网

Python 两条线的交点。以下哪一个等式是最节省资源的?

Python 两条线的交点。以下哪一个等式是最节省资源的?,python,performance,line,intersection,Python,Performance,Line,Intersection,在我的程序中,我需要找到两条线相交的y值 第一行表示为:y=a1*x+b1 第二行由y=a2*x+b2表示 分别求解x,将方程设置为相等,求解y,然后重新排列,我得到了四个不同但等效的方程,实际上是方程: y=b1/a1-b2/a2/1/a1-1/a2 y=b2+a2/a1*b1/1-a2/a1 y=a1*b2-b1*a2/a1-a2 y=a1*b2-b1/a1-a2+b1 现在,我运行以下Python代码来检查所有等式是否给出相同的答案: import random def linearfu

在我的程序中,我需要找到两条线相交的y值

第一行表示为:y=a1*x+b1 第二行由y=a2*x+b2表示

分别求解x,将方程设置为相等,求解y,然后重新排列,我得到了四个不同但等效的方程,实际上是方程:

y=b1/a1-b2/a2/1/a1-1/a2

y=b2+a2/a1*b1/1-a2/a1

y=a1*b2-b1*a2/a1-a2

y=a1*b2-b1/a1-a2+b1

现在,我运行以下Python代码来检查所有等式是否给出相同的答案:

import random

def linearfunction(coords1,coords2):
    x1=coords1[0]
    x2=coords2[0]
    y1=coords1[1]
    y2=coords2[1]
    a=(y2-y1)/(x2-x1)
    b=y2-a*x2
    return [a,b]

def doesitequal():
    first=[random.randint(0,100),random.randint(0,100)]
    second=[random.randint(0,100),random.randint(0,100)]
    third=[random.randint(0,100),random.randint(0,100)]
    fourth=[random.randint(0,100),random.randint(0,100)]

    a1=linearfunction(first,second)[0]
    b1=a1=linearfunction(first,second)[1]
    a2=linearfunction(third,fourth)[0]
    b2=linearfunction(third,fourth)[1]

    firstversion=(b1/a1-b2/a2)/(1/a1-1/a2)
    secondversion=(b2+(a2/a1)*b1)/(1-a2/a1)
    thirdversion=(a1*b2-b1*a2)/(a1-a2)
    fourthversion=a1*((b2-b1)/(a1-a2))+b1

    print('First equation: ', firstversion)
    print('Second equation:', secondversion)
    print('Third equation:', thirdversion)
    print('Fourth equation', fourthversion)

for i in range(100):
    doesitequal()
令我惊讶的是,第二个方程总是返回一个稍微偏离其他方程的值,这些方程返回的值彼此完全相等。请自己试试看,明白我的意思

示例输出:

First equation:  -71.13238078843175
Second equation: -67.71148218281976
Third equation: -71.13238078843175
Fourth equation -71.1323807884317
First equation:  190.62597809076678
Second equation: 186.9640062597809
Third equation: 190.6259780907668
Fourth equation 190.62597809076686
First equation:  81.93742601048538
Second equation: 80.48246237104686
Third equation: 81.93742601048537
Fourth equation 81.93742601048537
因此,我的第一个问题是:

为什么第二个等式返回的值与它应该返回的值略有不同?我的算术搞砸了吗?还是因为Python计算给定数学运算的方式

我的第二个问题是:

在这四个公式中,哪一个是内存效率最高的?也就是说,如果要在短时间内对线路交点进行大量计算,那么哪一个最不可能导致计算滞后

一个粗略的猜测是,效率与给定方程式中的运算次数成反比。例如,第一个方程式涉及7个数学运算,而最后一个方程式涉及5个,这意味着最后一个方程式比第一个方程式更有效。然而,不同的数学运算在复杂性和效率方面可能并不相等。这就是为什么我不确定这个问题的答案

非常感谢您的帮助

2. y=(b2+(a2/a1)*b1)/(1-a2/a1)

将其与我使用的等式3进行比较

a2 b1 - a1 b2
-------------
   a2 - a1

你有没有试过分析他们中的任何一个或全部?稍微不同的值是什么意思?我编辑了问题并添加了示例输出,以便查看返回值的差异。您有b1=a1=。。。这是故意的吗?第二个公式肯定是不正确的,这确实是一个错误。我修正了第二个公式,谢谢。我发布了一个答案来说明你在第二个公式中的错误,但我想说的是,这种东西物体交点有时是用齐次坐标来表示的。谢谢!另外,你能解释一下为什么你会选择等式3吗?简单,对称,memorable@user3000014编写最简单的有效代码,只优化有效且需要优化的代码在许多情况下,您可以说有效且几乎不需要优化的代码
(a2 b1 + a1 b2) / a1
--------------------
   (a1 - a2) / a1
a2 b1 + a1 b2
-------------
   a1 - a2
a2 b1 - a1 b2
-------------
   a2 - a1