Skip to content

Commit c3bcfbf

Browse files
kituuucclauss
andauthored
Add Cramer's rule for solving system of linear equations in two variables (TheAlgorithms#7547)
* added script for solving system of linear equations in two variables * implemented all the suggested changes * changed RuntimeError to ValueError * Update matrix/system_of_linear_equation_in_2_variables.py * Update matrix/system_of_linear_equation_in_2_variables.py * Update and rename system_of_linear_equation_in_2_variables.py to cramers_rule_2x2.py Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 2c959a7 commit c3bcfbf

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

matrix/cramers_rule_2x2.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# https://www.chilimath.com/lessons/advanced-algebra/cramers-rule-with-two-variables
2+
# https://en.wikipedia.org/wiki/Cramer%27s_rule
3+
4+
5+
def cramers_rule_2x2(equation1: list[int], equation2: list[int]) -> str:
6+
"""
7+
Solves the system of linear equation in 2 variables.
8+
:param: equation1: list of 3 numbers
9+
:param: equation2: list of 3 numbers
10+
:return: String of result
11+
input format : [a1, b1, d1], [a2, b2, d2]
12+
determinant = [[a1, b1], [a2, b2]]
13+
determinant_x = [[d1, b1], [d2, b2]]
14+
determinant_y = [[a1, d1], [a2, d2]]
15+
16+
>>> cramers_rule_2x2([2, 3, 0], [5, 1, 0])
17+
'Trivial solution. (Consistent system) x = 0 and y = 0'
18+
>>> cramers_rule_2x2([0, 4, 50], [2, 0, 26])
19+
'Non-Trivial Solution (Consistent system) x = 13.0, y = 12.5'
20+
>>> cramers_rule_2x2([11, 2, 30], [1, 0, 4])
21+
'Non-Trivial Solution (Consistent system) x = 4.0, y = -7.0'
22+
>>> cramers_rule_2x2([4, 7, 1], [1, 2, 0])
23+
'Non-Trivial Solution (Consistent system) x = 2.0, y = -1.0'
24+
25+
>>> cramers_rule_2x2([1, 2, 3], [2, 4, 6])
26+
Traceback (most recent call last):
27+
...
28+
ValueError: Infinite solutions. (Consistent system)
29+
>>> cramers_rule_2x2([1, 2, 3], [2, 4, 7])
30+
Traceback (most recent call last):
31+
...
32+
ValueError: No solution. (Inconsistent system)
33+
>>> cramers_rule_2x2([1, 2, 3], [11, 22])
34+
Traceback (most recent call last):
35+
...
36+
ValueError: Please enter a valid equation.
37+
>>> cramers_rule_2x2([0, 1, 6], [0, 0, 3])
38+
Traceback (most recent call last):
39+
...
40+
ValueError: No solution. (Inconsistent system)
41+
>>> cramers_rule_2x2([0, 0, 6], [0, 0, 3])
42+
Traceback (most recent call last):
43+
...
44+
ValueError: Both a & b of two equations can't be zero.
45+
>>> cramers_rule_2x2([1, 2, 3], [1, 2, 3])
46+
Traceback (most recent call last):
47+
...
48+
ValueError: Infinite solutions. (Consistent system)
49+
>>> cramers_rule_2x2([0, 4, 50], [0, 3, 99])
50+
Traceback (most recent call last):
51+
...
52+
ValueError: No solution. (Inconsistent system)
53+
"""
54+
55+
# Check if the input is valid
56+
if not len(equation1) == len(equation2) == 3:
57+
raise ValueError("Please enter a valid equation.")
58+
if equation1[0] == equation1[1] == equation2[0] == equation2[1] == 0:
59+
raise ValueError("Both a & b of two equations can't be zero.")
60+
61+
# Extract the coefficients
62+
a1, b1, c1 = equation1
63+
a2, b2, c2 = equation2
64+
65+
# Calculate the determinants of the matrices
66+
determinant = a1 * b2 - a2 * b1
67+
determinant_x = c1 * b2 - c2 * b1
68+
determinant_y = a1 * c2 - a2 * c1
69+
70+
# Check if the system of linear equations has a solution (using Cramer's rule)
71+
if determinant == 0:
72+
if determinant_x == determinant_y == 0:
73+
raise ValueError("Infinite solutions. (Consistent system)")
74+
else:
75+
raise ValueError("No solution. (Inconsistent system)")
76+
else:
77+
if determinant_x == determinant_y == 0:
78+
return "Trivial solution. (Consistent system) x = 0 and y = 0"
79+
else:
80+
x = determinant_x / determinant
81+
y = determinant_y / determinant
82+
return f"Non-Trivial Solution (Consistent system) x = {x}, y = {y}"

0 commit comments

Comments
 (0)