I want to solve a linear equation with three or more variables. Is there a good library in python to do it?
In Python, NumPy (Numerical Python), SciPy (Scientific Python) and SymPy (Symbolic Python) libraries can be used to solve systems of linear equations. These libraries use the concept of vectorization which allow them to do matrix computations efficiently by avoiding many for loops.
To solve the two equations for the two variables x and y , we'll use SymPy's solve() function. The solve() function takes two arguments, a tuple of the equations (eq1, eq2) and a tuple of the variables to solve for (x, y) . The SymPy solution object is a Python dictionary.
Yes, the very-popular NumPy package has a function to do this. Their example:
Solve the system of equations
3 * x0 + x1 = 9
andx0 + 2 * x1 = 8
:>>> import numpy as np >>> a = np.array([[3,1], [1,2]]) >>> b = np.array([9,8]) >>> x = np.linalg.solve(a, b) >>> x array([ 2., 3.])
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.linalg.solve.html
See http://sympy.org/ and http://numpy.scipy.org/.
Specifically, http://docs.scipy.org/doc/numpy/reference/routines.linalg.html
And http://docs.sympy.org/0.7.0/tutorial.html#algebra, http://docs.sympy.org/dev/modules/solvers/solvers.html
Edit: Added solvers link from the comment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With