From 60e5156b0756ce1e39bc48a53f9aea91a45f9158 Mon Sep 17 00:00:00 2001 From: karthik Date: Sun, 23 Oct 2022 14:36:53 +0530 Subject: [PATCH 1/6] added script for solving system of linear equations in two variables --- ...ystem_of_linear_equation_in_2_variables.py | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 matrix/system_of_linear_equation_in_2_variables.py diff --git a/matrix/system_of_linear_equation_in_2_variables.py b/matrix/system_of_linear_equation_in_2_variables.py new file mode 100644 index 000000000000..aac84ce60072 --- /dev/null +++ b/matrix/system_of_linear_equation_in_2_variables.py @@ -0,0 +1,84 @@ +def calculate_x_and_y(eq1, eq2): + """ + Solves the system of linear equation in 2 variables. + :param: eq1: list of 3 numbers + :param: eq2: list of 3 numbers + :return: String of result + Theory:- + https://www.mathsisfun.com/algebra/systems-linear-equations-matrices.html + Cramer's rule for 2x2 matrix:- + https://www.chilimath.com/lessons/advanced-algebra/cramers-rule-with-two-variables + a1x + b1y = = d1 + a2x + b2y = = d2 + input format : [a1, b1, d1], [a2, b2, d2] + d_matrix = [[a1, b1], [a2, b2]] + d is determinant of matrix d_matrix + dx_matrix = [[d1, b1], [d2, b2]] + dx is determinant of matrix dx_matrix + dy_matrix = [[a1, d1], [a2, d2]] + dy is determinant of matrix dy_matrix + + >>> calculate_x_and_y([1, 2, 3], [2, 4, 6]) + 'Infinite solutions. (Consistent system)' + + >>> calculate_x_and_y([1, 2, 3], [2, 4, 7]) + 'No solution. (Inconsistent system)' + + >>> calculate_x_and_y([1, 2, 3], [11, 22]) + Traceback (most recent call last): + ... + ValueError: Please enter a valid equation. + + >>> calculate_x_and_y([11, 2, 30], [1, 0, 4]) + 'Non-Trivial Solution (Consistent system) x = 4.0, y = -7.0' + + >>> calculate_x_and_y([0, 1, 6], [0, 0, 3]) + 'No solution. (Inconsistent system)' + + >>> calculate_x_and_y([0, 0, 6], [0, 0, 3]) + Both a & b of two equations can't be zero. + + >>> calculate_x_and_y([4, 7, 1], [1, 2, 0]) + 'Non-Trivial Solution (Consistent system) x = 2.0, y = -1.0' + + >>> calculate_x_and_y([1, 2, 3], [1, 2, 3]) + 'Infinite solutions. (Consistent system)' + + >>> calculate_x_and_y([2, 3, 0], [5, 1, 0]) + 'Trivial solution. (Consistent system) x = 0 and y = 0' + + >>> calculate_x_and_y([0, 4, 50], [2, 0, 26]) + 'Non-Trivial Solution (Consistent system) x = 13.0, y = 12.5' + + >>> calculate_x_and_y([0, 4, 50], [0, 3, 99]) + 'No solution. (Inconsistent system)' + """ + + # Checking if the input is valid + if len(eq1) != 3 or len(eq2) != 3: + raise ValueError("Please enter a valid equation.") + elif (eq1[0] == 0 and eq1[1] == 0) and (eq2[0] == 0 and eq2[1] == 0): + print("Both a & b of two equations can't be zero.") + else: + # Extracting the coefficients + a1, b1, c1 = eq1 + a2, b2, c2 = eq2 + + # Calculating the determinant of matrix d_matrix, dx_matrix and dy_matrix + d = a1 * b2 - a2 * b1 + dx = c1 * b2 - c2 * b1 + dy = a1 * c2 - a2 * c1 + + # Checking if the system of linear equation has a solution (Using Cramer's rule) + if d == 0: + if dx == 0 and dy == 0: + return "Infinite solutions. (Consistent system)" + else: + return "No solution. (Inconsistent system)" + else: + if dx == 0 and dy == 0: + return f"Trivial solution. (Consistent system) x = {0} and y = {0}" + else: + x = dx / d + y = dy / d + return f"Non-Trivial Solution (Consistent system) x = {x}, y = {y}" From 6066033685c067c2b463128e6aee3b979c5e6b59 Mon Sep 17 00:00:00 2001 From: karthik Date: Sun, 23 Oct 2022 15:22:17 +0530 Subject: [PATCH 2/6] implemented all the suggested changes --- ...ystem_of_linear_equation_in_2_variables.py | 118 ++++++++++-------- 1 file changed, 68 insertions(+), 50 deletions(-) diff --git a/matrix/system_of_linear_equation_in_2_variables.py b/matrix/system_of_linear_equation_in_2_variables.py index aac84ce60072..10d747400c2e 100644 --- a/matrix/system_of_linear_equation_in_2_variables.py +++ b/matrix/system_of_linear_equation_in_2_variables.py @@ -1,15 +1,19 @@ -def calculate_x_and_y(eq1, eq2): - """ - Solves the system of linear equation in 2 variables. - :param: eq1: list of 3 numbers - :param: eq2: list of 3 numbers - :return: String of result - Theory:- +""" +Theory:- https://www.mathsisfun.com/algebra/systems-linear-equations-matrices.html Cramer's rule for 2x2 matrix:- https://www.chilimath.com/lessons/advanced-algebra/cramers-rule-with-two-variables a1x + b1y = = d1 a2x + b2y = = d2 +""" + + +def calculate_x_and_y(eq1: list[int], eq2: list[int]) -> None: + """ + Solves the system of linear equation in 2 variables. + :param: eq1: list of 3 numbers + :param: eq2: list of 3 numbers + :return: String of result input format : [a1, b1, d1], [a2, b2, d2] d_matrix = [[a1, b1], [a2, b2]] d is determinant of matrix d_matrix @@ -19,66 +23,80 @@ def calculate_x_and_y(eq1, eq2): dy is determinant of matrix dy_matrix >>> calculate_x_and_y([1, 2, 3], [2, 4, 6]) - 'Infinite solutions. (Consistent system)' - + Traceback (most recent call last): + ... + RuntimeError: Infinite solutions. (Consistent system) >>> calculate_x_and_y([1, 2, 3], [2, 4, 7]) - 'No solution. (Inconsistent system)' - + Traceback (most recent call last): + ... + RuntimeError: No solution. (Inconsistent system) >>> calculate_x_and_y([1, 2, 3], [11, 22]) Traceback (most recent call last): ... ValueError: Please enter a valid equation. - >>> calculate_x_and_y([11, 2, 30], [1, 0, 4]) - 'Non-Trivial Solution (Consistent system) x = 4.0, y = -7.0' - + Traceback (most recent call last): + ... + RuntimeError: Non-Trivial Solution (Consistent system) x = 4.0, y = -7.0 >>> calculate_x_and_y([0, 1, 6], [0, 0, 3]) - 'No solution. (Inconsistent system)' - + Traceback (most recent call last): + ... + RuntimeError: No solution. (Inconsistent system) >>> calculate_x_and_y([0, 0, 6], [0, 0, 3]) - Both a & b of two equations can't be zero. - + Traceback (most recent call last): + ... + ValueError: Both a & b of two equations can't be zero. >>> calculate_x_and_y([4, 7, 1], [1, 2, 0]) - 'Non-Trivial Solution (Consistent system) x = 2.0, y = -1.0' - + Traceback (most recent call last): + ... + RuntimeError: Non-Trivial Solution (Consistent system) x = 2.0, y = -1.0 >>> calculate_x_and_y([1, 2, 3], [1, 2, 3]) - 'Infinite solutions. (Consistent system)' - + Traceback (most recent call last): + ... + RuntimeError: Infinite solutions. (Consistent system) >>> calculate_x_and_y([2, 3, 0], [5, 1, 0]) - 'Trivial solution. (Consistent system) x = 0 and y = 0' - + Traceback (most recent call last): + ... + RuntimeError: Trivial solution. (Consistent system) x = 0 and y = 0 >>> calculate_x_and_y([0, 4, 50], [2, 0, 26]) - 'Non-Trivial Solution (Consistent system) x = 13.0, y = 12.5' - + Traceback (most recent call last): + ... + RuntimeError: Non-Trivial Solution (Consistent system) x = 13.0, y = 12.5 >>> calculate_x_and_y([0, 4, 50], [0, 3, 99]) - 'No solution. (Inconsistent system)' + Traceback (most recent call last): + ... + RuntimeError: No solution. (Inconsistent system) """ # Checking if the input is valid - if len(eq1) != 3 or len(eq2) != 3: + if not len(eq1) == len(eq2) == 3: raise ValueError("Please enter a valid equation.") - elif (eq1[0] == 0 and eq1[1] == 0) and (eq2[0] == 0 and eq2[1] == 0): - print("Both a & b of two equations can't be zero.") - else: - # Extracting the coefficients - a1, b1, c1 = eq1 - a2, b2, c2 = eq2 + elif eq1[0] == eq1[1] == eq2[0] == eq2[1] == 0: + raise ValueError("Both a & b of two equations can't be zero.") + + # Extracting the coefficients + a1, b1, c1 = eq1 + a2, b2, c2 = eq2 - # Calculating the determinant of matrix d_matrix, dx_matrix and dy_matrix - d = a1 * b2 - a2 * b1 - dx = c1 * b2 - c2 * b1 - dy = a1 * c2 - a2 * c1 + # Calculating the determinant of matrix d_matrix, dx_matrix and dy_matrix + d = a1 * b2 - a2 * b1 + dx = c1 * b2 - c2 * b1 + dy = a1 * c2 - a2 * c1 - # Checking if the system of linear equation has a solution (Using Cramer's rule) - if d == 0: - if dx == 0 and dy == 0: - return "Infinite solutions. (Consistent system)" - else: - return "No solution. (Inconsistent system)" + # Checking if the system of linear equation has a solution (Using Cramer's rule) + if d == 0: + if dx == dy == 0: + raise RuntimeError("Infinite solutions. (Consistent system)") + else: + raise RuntimeError("No solution. (Inconsistent system)") + else: + if dx == dy == 0: + raise RuntimeError( + f"Trivial solution. (Consistent system) x = {0} and y = {0}" + ) else: - if dx == 0 and dy == 0: - return f"Trivial solution. (Consistent system) x = {0} and y = {0}" - else: - x = dx / d - y = dy / d - return f"Non-Trivial Solution (Consistent system) x = {x}, y = {y}" + x = dx / d + y = dy / d + raise RuntimeError( + f"Non-Trivial Solution (Consistent system) x = {x}, y = {y}" + ) From 29c04858a75478e25c667e845a2b465174ecbbb0 Mon Sep 17 00:00:00 2001 From: karthik Date: Sun, 23 Oct 2022 15:26:37 +0530 Subject: [PATCH 3/6] changed RuntimeError to ValueError --- ...ystem_of_linear_equation_in_2_variables.py | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/matrix/system_of_linear_equation_in_2_variables.py b/matrix/system_of_linear_equation_in_2_variables.py index 10d747400c2e..1426860a565c 100644 --- a/matrix/system_of_linear_equation_in_2_variables.py +++ b/matrix/system_of_linear_equation_in_2_variables.py @@ -25,11 +25,11 @@ def calculate_x_and_y(eq1: list[int], eq2: list[int]) -> None: >>> calculate_x_and_y([1, 2, 3], [2, 4, 6]) Traceback (most recent call last): ... - RuntimeError: Infinite solutions. (Consistent system) + ValueError: Infinite solutions. (Consistent system) >>> calculate_x_and_y([1, 2, 3], [2, 4, 7]) Traceback (most recent call last): ... - RuntimeError: No solution. (Inconsistent system) + ValueError: No solution. (Inconsistent system) >>> calculate_x_and_y([1, 2, 3], [11, 22]) Traceback (most recent call last): ... @@ -37,11 +37,11 @@ def calculate_x_and_y(eq1: list[int], eq2: list[int]) -> None: >>> calculate_x_and_y([11, 2, 30], [1, 0, 4]) Traceback (most recent call last): ... - RuntimeError: Non-Trivial Solution (Consistent system) x = 4.0, y = -7.0 + ValueError: Non-Trivial Solution (Consistent system) x = 4.0, y = -7.0 >>> calculate_x_and_y([0, 1, 6], [0, 0, 3]) Traceback (most recent call last): ... - RuntimeError: No solution. (Inconsistent system) + ValueError: No solution. (Inconsistent system) >>> calculate_x_and_y([0, 0, 6], [0, 0, 3]) Traceback (most recent call last): ... @@ -49,23 +49,23 @@ def calculate_x_and_y(eq1: list[int], eq2: list[int]) -> None: >>> calculate_x_and_y([4, 7, 1], [1, 2, 0]) Traceback (most recent call last): ... - RuntimeError: Non-Trivial Solution (Consistent system) x = 2.0, y = -1.0 + ValueError: Non-Trivial Solution (Consistent system) x = 2.0, y = -1.0 >>> calculate_x_and_y([1, 2, 3], [1, 2, 3]) Traceback (most recent call last): ... - RuntimeError: Infinite solutions. (Consistent system) + ValueError: Infinite solutions. (Consistent system) >>> calculate_x_and_y([2, 3, 0], [5, 1, 0]) Traceback (most recent call last): ... - RuntimeError: Trivial solution. (Consistent system) x = 0 and y = 0 + ValueError: Trivial solution. (Consistent system) x = 0 and y = 0 >>> calculate_x_and_y([0, 4, 50], [2, 0, 26]) Traceback (most recent call last): ... - RuntimeError: Non-Trivial Solution (Consistent system) x = 13.0, y = 12.5 + ValueError: Non-Trivial Solution (Consistent system) x = 13.0, y = 12.5 >>> calculate_x_and_y([0, 4, 50], [0, 3, 99]) Traceback (most recent call last): ... - RuntimeError: No solution. (Inconsistent system) + ValueError: No solution. (Inconsistent system) """ # Checking if the input is valid @@ -86,17 +86,17 @@ def calculate_x_and_y(eq1: list[int], eq2: list[int]) -> None: # Checking if the system of linear equation has a solution (Using Cramer's rule) if d == 0: if dx == dy == 0: - raise RuntimeError("Infinite solutions. (Consistent system)") + raise ValueError("Infinite solutions. (Consistent system)") else: - raise RuntimeError("No solution. (Inconsistent system)") + raise ValueError("No solution. (Inconsistent system)") else: if dx == dy == 0: - raise RuntimeError( + raise ValueError( f"Trivial solution. (Consistent system) x = {0} and y = {0}" ) else: x = dx / d y = dy / d - raise RuntimeError( + raise ValueError( f"Non-Trivial Solution (Consistent system) x = {x}, y = {y}" ) From 0494477e1ca4e3c99ac967181e5bdf8022eb2a86 Mon Sep 17 00:00:00 2001 From: karthik Date: Sun, 23 Oct 2022 15:30:46 +0530 Subject: [PATCH 4/6] Update matrix/system_of_linear_equation_in_2_variables.py --- ...ystem_of_linear_equation_in_2_variables.py | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/matrix/system_of_linear_equation_in_2_variables.py b/matrix/system_of_linear_equation_in_2_variables.py index 1426860a565c..3730494c3b4a 100644 --- a/matrix/system_of_linear_equation_in_2_variables.py +++ b/matrix/system_of_linear_equation_in_2_variables.py @@ -1,18 +1,12 @@ -""" -Theory:- - https://www.mathsisfun.com/algebra/systems-linear-equations-matrices.html - Cramer's rule for 2x2 matrix:- - https://www.chilimath.com/lessons/advanced-algebra/cramers-rule-with-two-variables - a1x + b1y = = d1 - a2x + b2y = = d2 -""" +# https://www.chilimath.com/lessons/advanced-algebra/cramers-rule-with-two-variables +# https://en.wikipedia.org/wiki/Cramer%27s_rule -def calculate_x_and_y(eq1: list[int], eq2: list[int]) -> None: +def calculate_x_and_y(equation1: list[int], equation2: list[int]) -> None: """ Solves the system of linear equation in 2 variables. - :param: eq1: list of 3 numbers - :param: eq2: list of 3 numbers + :param: equation1: list of 3 numbers + :param: equation2: list of 3 numbers :return: String of result input format : [a1, b1, d1], [a2, b2, d2] d_matrix = [[a1, b1], [a2, b2]] @@ -69,14 +63,14 @@ def calculate_x_and_y(eq1: list[int], eq2: list[int]) -> None: """ # Checking if the input is valid - if not len(eq1) == len(eq2) == 3: + if not len(equation1) == len(equation2) == 3: raise ValueError("Please enter a valid equation.") - elif eq1[0] == eq1[1] == eq2[0] == eq2[1] == 0: + elif equation1[0] == equation1[1] == equation2[0] == equation2[1] == 0: raise ValueError("Both a & b of two equations can't be zero.") # Extracting the coefficients - a1, b1, c1 = eq1 - a2, b2, c2 = eq2 + a1, b1, c1 = equation1 + a2, b2, c2 = equation2 # Calculating the determinant of matrix d_matrix, dx_matrix and dy_matrix d = a1 * b2 - a2 * b1 From c8538e7c19ffa72b8bab4649174c26648d57db1b Mon Sep 17 00:00:00 2001 From: karthik Date: Sun, 23 Oct 2022 17:35:33 +0530 Subject: [PATCH 5/6] Update matrix/system_of_linear_equation_in_2_variables.py --- ...ystem_of_linear_equation_in_2_variables.py | 35 ++++++------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/matrix/system_of_linear_equation_in_2_variables.py b/matrix/system_of_linear_equation_in_2_variables.py index 3730494c3b4a..b77ced25c8b5 100644 --- a/matrix/system_of_linear_equation_in_2_variables.py +++ b/matrix/system_of_linear_equation_in_2_variables.py @@ -2,19 +2,16 @@ # https://en.wikipedia.org/wiki/Cramer%27s_rule -def calculate_x_and_y(equation1: list[int], equation2: list[int]) -> None: +def calculate_x_and_y(equation1: list[int], equation2: list[int]) -> str: """ Solves the system of linear equation in 2 variables. :param: equation1: list of 3 numbers :param: equation2: list of 3 numbers :return: String of result input format : [a1, b1, d1], [a2, b2, d2] - d_matrix = [[a1, b1], [a2, b2]] - d is determinant of matrix d_matrix - dx_matrix = [[d1, b1], [d2, b2]] - dx is determinant of matrix dx_matrix - dy_matrix = [[a1, d1], [a2, d2]] - dy is determinant of matrix dy_matrix + d --> determinant or determinant_matrix + dx --> determinant_x or determinant_matrix_x + dy --> determinant_x or determinant_matrix_y >>> calculate_x_and_y([1, 2, 3], [2, 4, 6]) Traceback (most recent call last): @@ -29,9 +26,7 @@ def calculate_x_and_y(equation1: list[int], equation2: list[int]) -> None: ... ValueError: Please enter a valid equation. >>> calculate_x_and_y([11, 2, 30], [1, 0, 4]) - Traceback (most recent call last): - ... - ValueError: Non-Trivial Solution (Consistent system) x = 4.0, y = -7.0 + 'Non-Trivial Solution (Consistent system) x = 4.0, y = -7.0' >>> calculate_x_and_y([0, 1, 6], [0, 0, 3]) Traceback (most recent call last): ... @@ -41,21 +36,15 @@ def calculate_x_and_y(equation1: list[int], equation2: list[int]) -> None: ... ValueError: Both a & b of two equations can't be zero. >>> calculate_x_and_y([4, 7, 1], [1, 2, 0]) - Traceback (most recent call last): - ... - ValueError: Non-Trivial Solution (Consistent system) x = 2.0, y = -1.0 + 'Non-Trivial Solution (Consistent system) x = 2.0, y = -1.0' >>> calculate_x_and_y([1, 2, 3], [1, 2, 3]) Traceback (most recent call last): ... ValueError: Infinite solutions. (Consistent system) >>> calculate_x_and_y([2, 3, 0], [5, 1, 0]) - Traceback (most recent call last): - ... - ValueError: Trivial solution. (Consistent system) x = 0 and y = 0 + 'Trivial solution. (Consistent system) x = 0 and y = 0' >>> calculate_x_and_y([0, 4, 50], [2, 0, 26]) - Traceback (most recent call last): - ... - ValueError: Non-Trivial Solution (Consistent system) x = 13.0, y = 12.5 + 'Non-Trivial Solution (Consistent system) x = 13.0, y = 12.5' >>> calculate_x_and_y([0, 4, 50], [0, 3, 99]) Traceback (most recent call last): ... @@ -85,12 +74,8 @@ def calculate_x_and_y(equation1: list[int], equation2: list[int]) -> None: raise ValueError("No solution. (Inconsistent system)") else: if dx == dy == 0: - raise ValueError( - f"Trivial solution. (Consistent system) x = {0} and y = {0}" - ) + return "Trivial solution. (Consistent system) x = 0 and y = 0" else: x = dx / d y = dy / d - raise ValueError( - f"Non-Trivial Solution (Consistent system) x = {x}, y = {y}" - ) + return f"Non-Trivial Solution (Consistent system) x = {x}, y = {y}" From a832ad864ed079d78195ac9c6787186b13f77844 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 23 Oct 2022 14:18:57 +0200 Subject: [PATCH 6/6] Update and rename system_of_linear_equation_in_2_variables.py to cramers_rule_2x2.py --- ..._in_2_variables.py => cramers_rule_2x2.py} | 65 ++++++++++--------- 1 file changed, 33 insertions(+), 32 deletions(-) rename matrix/{system_of_linear_equation_in_2_variables.py => cramers_rule_2x2.py} (59%) diff --git a/matrix/system_of_linear_equation_in_2_variables.py b/matrix/cramers_rule_2x2.py similarity index 59% rename from matrix/system_of_linear_equation_in_2_variables.py rename to matrix/cramers_rule_2x2.py index b77ced25c8b5..a635d66fbb6c 100644 --- a/matrix/system_of_linear_equation_in_2_variables.py +++ b/matrix/cramers_rule_2x2.py @@ -2,80 +2,81 @@ # https://en.wikipedia.org/wiki/Cramer%27s_rule -def calculate_x_and_y(equation1: list[int], equation2: list[int]) -> str: +def cramers_rule_2x2(equation1: list[int], equation2: list[int]) -> str: """ Solves the system of linear equation in 2 variables. :param: equation1: list of 3 numbers :param: equation2: list of 3 numbers :return: String of result input format : [a1, b1, d1], [a2, b2, d2] - d --> determinant or determinant_matrix - dx --> determinant_x or determinant_matrix_x - dy --> determinant_x or determinant_matrix_y + determinant = [[a1, b1], [a2, b2]] + determinant_x = [[d1, b1], [d2, b2]] + determinant_y = [[a1, d1], [a2, d2]] - >>> calculate_x_and_y([1, 2, 3], [2, 4, 6]) + >>> cramers_rule_2x2([2, 3, 0], [5, 1, 0]) + 'Trivial solution. (Consistent system) x = 0 and y = 0' + >>> cramers_rule_2x2([0, 4, 50], [2, 0, 26]) + 'Non-Trivial Solution (Consistent system) x = 13.0, y = 12.5' + >>> cramers_rule_2x2([11, 2, 30], [1, 0, 4]) + 'Non-Trivial Solution (Consistent system) x = 4.0, y = -7.0' + >>> cramers_rule_2x2([4, 7, 1], [1, 2, 0]) + 'Non-Trivial Solution (Consistent system) x = 2.0, y = -1.0' + + >>> cramers_rule_2x2([1, 2, 3], [2, 4, 6]) Traceback (most recent call last): ... ValueError: Infinite solutions. (Consistent system) - >>> calculate_x_and_y([1, 2, 3], [2, 4, 7]) + >>> cramers_rule_2x2([1, 2, 3], [2, 4, 7]) Traceback (most recent call last): ... ValueError: No solution. (Inconsistent system) - >>> calculate_x_and_y([1, 2, 3], [11, 22]) + >>> cramers_rule_2x2([1, 2, 3], [11, 22]) Traceback (most recent call last): ... ValueError: Please enter a valid equation. - >>> calculate_x_and_y([11, 2, 30], [1, 0, 4]) - 'Non-Trivial Solution (Consistent system) x = 4.0, y = -7.0' - >>> calculate_x_and_y([0, 1, 6], [0, 0, 3]) + >>> cramers_rule_2x2([0, 1, 6], [0, 0, 3]) Traceback (most recent call last): ... ValueError: No solution. (Inconsistent system) - >>> calculate_x_and_y([0, 0, 6], [0, 0, 3]) + >>> cramers_rule_2x2([0, 0, 6], [0, 0, 3]) Traceback (most recent call last): ... ValueError: Both a & b of two equations can't be zero. - >>> calculate_x_and_y([4, 7, 1], [1, 2, 0]) - 'Non-Trivial Solution (Consistent system) x = 2.0, y = -1.0' - >>> calculate_x_and_y([1, 2, 3], [1, 2, 3]) + >>> cramers_rule_2x2([1, 2, 3], [1, 2, 3]) Traceback (most recent call last): ... ValueError: Infinite solutions. (Consistent system) - >>> calculate_x_and_y([2, 3, 0], [5, 1, 0]) - 'Trivial solution. (Consistent system) x = 0 and y = 0' - >>> calculate_x_and_y([0, 4, 50], [2, 0, 26]) - 'Non-Trivial Solution (Consistent system) x = 13.0, y = 12.5' - >>> calculate_x_and_y([0, 4, 50], [0, 3, 99]) + >>> cramers_rule_2x2([0, 4, 50], [0, 3, 99]) Traceback (most recent call last): ... ValueError: No solution. (Inconsistent system) """ - # Checking if the input is valid + # Check if the input is valid if not len(equation1) == len(equation2) == 3: raise ValueError("Please enter a valid equation.") - elif equation1[0] == equation1[1] == equation2[0] == equation2[1] == 0: + if equation1[0] == equation1[1] == equation2[0] == equation2[1] == 0: raise ValueError("Both a & b of two equations can't be zero.") - # Extracting the coefficients + # Extract the coefficients a1, b1, c1 = equation1 a2, b2, c2 = equation2 - # Calculating the determinant of matrix d_matrix, dx_matrix and dy_matrix - d = a1 * b2 - a2 * b1 - dx = c1 * b2 - c2 * b1 - dy = a1 * c2 - a2 * c1 + # Calculate the determinants of the matrices + determinant = a1 * b2 - a2 * b1 + determinant_x = c1 * b2 - c2 * b1 + determinant_y = a1 * c2 - a2 * c1 - # Checking if the system of linear equation has a solution (Using Cramer's rule) - if d == 0: - if dx == dy == 0: + # Check if the system of linear equations has a solution (using Cramer's rule) + if determinant == 0: + if determinant_x == determinant_y == 0: raise ValueError("Infinite solutions. (Consistent system)") else: raise ValueError("No solution. (Inconsistent system)") else: - if dx == dy == 0: + if determinant_x == determinant_y == 0: return "Trivial solution. (Consistent system) x = 0 and y = 0" else: - x = dx / d - y = dy / d + x = determinant_x / determinant + y = determinant_y / determinant return f"Non-Trivial Solution (Consistent system) x = {x}, y = {y}"