数学外行的克莱姆法则理解,仅供大家参考
同时也是这道题的额外知识点1
我找到了计算克莱姆法则的办法
point1 将手里现有的2个线段4个坐标都x,y,进行置换
根据我现在理解的线性等式原则
(y1-y2)x1 -(x1-x2)y1
(y3-y4)x3 -(x3-x4)y3
然后根据这两个等式中阔号内的内容换成克莱姆法则的代号
即:
a = (y1-y2)
b = (x1-x2)
c = (y3-y4)
d = (x3-x4)
置换后将上一个线性等式得出的结果分别设置为e和f
然后进行求x和y的计算
x = ed - bf / ad - bc
y = af - ec / ad - bc
最终根据《python语言程序设计》2018版中4.3和4.25编程的验证,证明了我的方法有效.
3个测试的数据
# x1, y1, x2, y2 = 2, 2, 0, 0
# x3, y3, x4, y4 = 0, 2, 2, 0
# x1, y1, x2, y2 = 2, 2, 5, -1
# x3, y3, x4, y4 = 4, 2, -1, -2
#
# x1, y1, x2, y2 = 2, 2, 7, 6
# x3, y3, x4, y4 = 4, 2, -1, -2
代码如下
x1, y1, x2, y2 = 2, 2, 7, 6
x3, y3, x4, y4 = 4, 2, -1, -2
a = y1 - y2
b = x1 - x2
c = y3 - y4
d = x3 - x4
e = ((y1 - y2) * x1) - ((x1 - x2) * y1)
f = ((y3 - y4) * x3) - ((x3 - x4) * y3)
judge_m = (a * d) - (b * c)
if judge_m == 0:
print("The equation has no solution")
else:
x = ((e * d) - (b * f)) / judge_m
y = ((a * f) - (e * c)) / judge_m
print(x,y)
类的代码段
class line_point:
def __init__(self, x1, y1, x2, y2, x3, y3, x4, y4):
self.__x1 = x1
self.__y1 = y1
self.__x2 = x2
self.__y2 = y2
self.__x3 = x3
self.__y3 = y3
self.__x4 = x4
self.__y4 = y4
def get_a(self):
return self.__y1 - self.__y2
def get_b(self):
return self.__x1 - self.__x2
def get_c(self):
return self.__y3 - self.__y4
def get_d(self):
return self.__x3 - self.__x4
def set_x1(self, x1):
return self.__x1
def set_y1(self, y1):
return self.__y1
def set_x2(self, x2):
return self.__x2
def set_y2(self, y2):
return self.__y2
def set_x3(self, x3):
return self.__x3
def set_y3(self, y3):
return self.__y3
def set_x4(self, x4):
return self.__x4
def set_y4(self, y4):
return self.__y4
def get_x1(self):
return self.__x1
def get_y1(self):
return self.__y1
def get_x2(self):
return self.__x2
def get_y2(self):
return self.__y2
def get_x3(self):
return self.__x3
def get_y3(self):
return self.__y3
def get_x4(self):
return self.__x4
def get_y4(self):
return self.__y4
def get_e(self):
a = line_point.get_a(self)
b = line_point.get_b(self)
x1 = line_point.get_x1(self)
y1 = line_point.get_y1(self)
return (a * x1) - (b * y1)
def get_f(self):
c = line_point.get_c(self)
d = line_point.get_d(self)
x3 = line_point.get_x3(self)
y3 = line_point.get_y3(self)
return (c * x3) - (d * y3)
def get_judge_m(self):
a = line_point.get_a(self)
b = line_point.get_b(self)
c = line_point.get_c(self)
d = line_point.get_d(self)
return (a * d) - (b * c)
def is_solvable(self):
a = line_point.get_a(self)
b = line_point.get_b(self)
c = line_point.get_c(self)
d = line_point.get_d(self)
e = line_point.get_e(self)
f = line_point.get_f(self)
if line_point.get_judge_m(self) == 0:
"The equation has no solution"
else:
x = ((e * d) - (b * f)) / line_point.get_judge_m(self)
y = ((a * f) - (e * c)) / line_point.get_judge_m(self)
return f"The intersecting point is:({x},{y})"
main的代码段
def main():
x1, y1, x2, y2 = eval(input('Enter the endpoints of the first line segment: '))
x3, y3, x4, y4 = eval(input('Enter the endpoints of the second line segment: '))
temp_test = exCode07.line_point(x1, y1, x2, y2, x3, y3, x4, y4)
print(temp_test.is_solvable())
main()
为什么后面是-1 我就真的不明白