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] determinant = [[a1, b1], [a2, b2]] determinant_x = [[d1, b1], [d2, b2]]
(equation1: list[int], equation2: list[int])
| 3 | |
| 4 | |
| 5 | def cramers_rule_2x2(equation1: list[int], equation2: list[int]) -> tuple[float, float]: |
| 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 | (0.0, 0.0) |
| 18 | >>> cramers_rule_2x2([0, 4, 50], [2, 0, 26]) |
| 19 | (13.0, 12.5) |
| 20 | >>> cramers_rule_2x2([11, 2, 30], [1, 0, 4]) |
| 21 | (4.0, -7.0) |
| 22 | >>> cramers_rule_2x2([4, 7, 1], [1, 2, 0]) |
| 23 | (2.0, -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 |
nothing calls this directly
no outgoing calls
no test coverage detected