Skip to content

Commit 60e5156

Browse files
committed
added script for solving system of linear equations in two variables
1 parent e40c7b4 commit 60e5156

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
def calculate_x_and_y(eq1, eq2):
2+
"""
3+
Solves the system of linear equation in 2 variables.
4+
:param: eq1: list of 3 numbers
5+
:param: eq2: list of 3 numbers
6+
:return: String of result
7+
Theory:-
8+
https://www.mathsisfun.com/algebra/systems-linear-equations-matrices.html
9+
Cramer's rule for 2x2 matrix:-
10+
https://www.chilimath.com/lessons/advanced-algebra/cramers-rule-with-two-variables
11+
a1x + b1y = = d1
12+
a2x + b2y = = d2
13+
input format : [a1, b1, d1], [a2, b2, d2]
14+
d_matrix = [[a1, b1], [a2, b2]]
15+
d is determinant of matrix d_matrix
16+
dx_matrix = [[d1, b1], [d2, b2]]
17+
dx is determinant of matrix dx_matrix
18+
dy_matrix = [[a1, d1], [a2, d2]]
19+
dy is determinant of matrix dy_matrix
20+
21+
>>> calculate_x_and_y([1, 2, 3], [2, 4, 6])
22+
'Infinite solutions. (Consistent system)'
23+
24+
>>> calculate_x_and_y([1, 2, 3], [2, 4, 7])
25+
'No solution. (Inconsistent system)'
26+
27+
>>> calculate_x_and_y([1, 2, 3], [11, 22])
28+
Traceback (most recent call last):
29+
...
30+
ValueError: Please enter a valid equation.
31+
32+
>>> calculate_x_and_y([11, 2, 30], [1, 0, 4])
33+
'Non-Trivial Solution (Consistent system) x = 4.0, y = -7.0'
34+
35+
>>> calculate_x_and_y([0, 1, 6], [0, 0, 3])
36+
'No solution. (Inconsistent system)'
37+
38+
>>> calculate_x_and_y([0, 0, 6], [0, 0, 3])
39+
Both a & b of two equations can't be zero.
40+
41+
>>> calculate_x_and_y([4, 7, 1], [1, 2, 0])
42+
'Non-Trivial Solution (Consistent system) x = 2.0, y = -1.0'
43+
44+
>>> calculate_x_and_y([1, 2, 3], [1, 2, 3])
45+
'Infinite solutions. (Consistent system)'
46+
47+
>>> calculate_x_and_y([2, 3, 0], [5, 1, 0])
48+
'Trivial solution. (Consistent system) x = 0 and y = 0'
49+
50+
>>> calculate_x_and_y([0, 4, 50], [2, 0, 26])
51+
'Non-Trivial Solution (Consistent system) x = 13.0, y = 12.5'
52+
53+
>>> calculate_x_and_y([0, 4, 50], [0, 3, 99])
54+
'No solution. (Inconsistent system)'
55+
"""
56+
57+
# Checking if the input is valid
58+
if len(eq1) != 3 or len(eq2) != 3:
59+
raise ValueError("Please enter a valid equation.")
60+
elif (eq1[0] == 0 and eq1[1] == 0) and (eq2[0] == 0 and eq2[1] == 0):
61+
print("Both a & b of two equations can't be zero.")
62+
else:
63+
# Extracting the coefficients
64+
a1, b1, c1 = eq1
65+
a2, b2, c2 = eq2
66+
67+
# Calculating the determinant of matrix d_matrix, dx_matrix and dy_matrix
68+
d = a1 * b2 - a2 * b1
69+
dx = c1 * b2 - c2 * b1
70+
dy = a1 * c2 - a2 * c1
71+
72+
# Checking if the system of linear equation has a solution (Using Cramer's rule)
73+
if d == 0:
74+
if dx == 0 and dy == 0:
75+
return "Infinite solutions. (Consistent system)"
76+
else:
77+
return "No solution. (Inconsistent system)"
78+
else:
79+
if dx == 0 and dy == 0:
80+
return f"Trivial solution. (Consistent system) x = {0} and y = {0}"
81+
else:
82+
x = dx / d
83+
y = dy / d
84+
return f"Non-Trivial Solution (Consistent system) x = {x}, y = {y}"

0 commit comments

Comments
 (0)