Skip to content

Create linear_equations_in_two_variables.py #5222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
33 changes: 33 additions & 0 deletions maths/linear_equations_in_two_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
Solves linear equations in two variables
Inputs : Co-efficients of x and y in a pair of equations, Dependents
Outputs : Values of x and y
"""


def solve(
x_coefficient_1: float,
y_coefficient_1: float,
dependent_1: float,
x_coefficient_2: float,
y_coefficient_2: float,
dependent_2: float,
) -> list:
"""
>>> solve(4,5,20,1,2,13)
[-8.333333333333332, 10.666666666666666]
"""
import numpy

coefficients = numpy.array(
[[x_coefficient_1, y_coefficient_1], [x_coefficient_2, y_coefficient_2]]
)
dependents = numpy.array([dependent_1, dependent_2])
answers = numpy.linalg.solve(coefficients, dependents)
return list(answers)


if __name__ == "__main__":
from doctest import testmod

testmod()