Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a python module to solve linear equations?

Tags:

python

I want to solve a linear equation with three or more variables. Is there a good library in python to do it?

like image 537
station Avatar asked Jul 22 '11 12:07

station


People also ask

Can Python solve systems of equations?

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.

How do you solve equations in Python?

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.


2 Answers

Yes, the very-popular NumPy package has a function to do this. Their example:

Solve the system of equations 3 * x0 + x1 = 9 and x0 + 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

like image 180
Jeremy Avatar answered Sep 19 '22 19:09

Jeremy


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.

like image 41
agf Avatar answered Sep 22 '22 19:09

agf