# https://www.chilimath.com/lessons/advanced-algebra/cramers-rule-with-two-variables
# https://en.wikipedia.org/wiki/Cramer%27s_rule
from typing import List,Tuple
def cramers_rule_2x2(equation1: List[int], equation2: List[int]) -> Tuple[float, float]:
"""
解决包含两个变量的线性方程组。
:param: equation1: 包含3个数字的列表
:param: equation2: 包含3个数字的列表
:return: 结果的元组
输入格式: [a1, b1, d1], [a2, b2, d2]
行列式 = [[a1, b1], [a2, b2]]
行列式_x = [[d1, b1], [d2, b2]]
行列式_y = [[a1, d1], [a2, d2]]
>>> cramers_rule_2x2([2, 3, 0], [5, 1, 0])
(0.0, 0.0)
>>> cramers_rule_2x2([0, 4, 50], [2, 0, 26])
(13.0, 12.5)
>>> cramers_rule_2x2([11, 2, 30], [1, 0, 4])
(4.0, -7.0)
>>> cramers_rule_2x2([4, 7, 1], [1, 2, 0])
(2.0, -1.0)
>>> cramers_rule_2x2([1, 2, 3], [2, 4, 6])
Traceback (most recent call last):
...
ValueError: 无穷解(一致系统)
>>> cramers_rule_2x2([1, 2, 3], [2, 4, 7])
Traceback (most recent call last):
...
ValueError: 无解(不一致系统)
>>> cramers_rule_2x2([1, 2, 3], [11, 22])
Traceback (most recent call last):
...
ValueError: 请输入有效的方程。
>>> cramers_rule_2x2([0, 1, 6], [0, 0, 3])
Traceback (most recent call last):
...
ValueError: 无解(不一致系统)
>>> cramers_rule_2x2([0, 0, 6], [0, 0, 3])
Traceback (most recent call last):
...
ValueError: 两个方程的a和b不能都为零。
>>> cramers_rule_2x2([1, 2, 3], [1, 2, 3])
Traceback (most recent call last):
...
ValueError: 无穷解(一致系统)
>>> cramers_rule_2x2([0, 4, 50], [0, 3, 99])
Traceback (most recent call last):
...
ValueError: 无解(不一致系统)
"""
# 检查输入是否有效
if not len(equation1) == len(equation2) == 3:
raise ValueError("请输入有效的方程。")
if equation1[0] == equation1[1] == equation2[0] == equation2[1] == 0:
raise ValueError("两个方程的a和b不能都为零。")
# 提取系数
a1, b1, c1 = equation1
a2, b2, c2 = equation2
# 计算矩阵的行列式
determinant = a1 * b2 - a2 * b1
determinant_x = c1 * b2 - c2 * b1
determinant_y = a1 * c2 - a2 * c1
# 检查线性方程组是否有解(使用克莱姆法则)
if determinant == 0:
if determinant_x == determinant_y == 0:
raise ValueError("无穷解(一致系统)")
else:
raise ValueError("无解(不一致系统)")
else:
if determinant_x == determinant_y == 0:
# 平凡解(不一致系统)
return (0.0, 0.0)
else:
x = determinant_x / determinant
y = determinant_y / determinant
# 非平凡解(一致系统)
return (x, y)